From 1003ced6e95f4c0831646e46120b68a80f4bddac Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Sat, 8 Feb 2025 09:54:17 +0800 Subject: [PATCH] feat(bt/bluedroid): Support BLE vendor hci event reporting --- .../api/include/api/esp_gap_ble_api.h | 9 +++++ .../btc/profile/std/gap/btc_gap_ble.c | 40 +++++++++++++++++++ .../bt/host/bluedroid/stack/btm/btm_ble_gap.c | 29 ++++++++++++++ .../bluedroid/stack/include/stack/btm_api.h | 2 + .../stack/include/stack/btm_ble_api.h | 1 + .../bluedroid/stack/include/stack/hcidefs.h | 3 ++ 6 files changed, 84 insertions(+) diff --git a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h index a28abfa41b..748c34c2ca 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h @@ -231,6 +231,7 @@ typedef enum { ESP_GAP_BLE_SET_PRIVACY_MODE_COMPLETE_EVT, /*!< When set privacy mode complete, the event comes */ ESP_GAP_BLE_SET_CSA_SUPPORT_COMPLETE_EVT, /*!< When set CSA support complete, the event comes */ ESP_GAP_BLE_SET_VENDOR_EVT_MASK_COMPLETE_EVT, /*!< When set vendor event mask complete, the event comes */ + ESP_GAP_BLE_VENDOR_HCI_EVT, /*!< When BLE vendor HCI event received, the event comes */ ESP_GAP_BLE_EVT_MAX, /*!< when maximum advertising event complete, the event comes */ } esp_gap_ble_cb_event_t; @@ -1588,6 +1589,14 @@ typedef union { struct ble_set_vendor_evt_mask_cmpl_evt_param { esp_bt_status_t status; /*!< Indicate set vendor event mask operation success status */ } set_vendor_evt_mask_cmpl; /*!< Event parameter of ESP_GAP_BLE_SET_VENDOR_EVT_MASK_COMPLETE_EVT */ + /** + * @brief ESP_GAP_BLE_VENDOR_HCI_EVT + */ + struct ble_vendor_hci_event_evt_param { + uint8_t subevt_code; /*!< Subevent code for vendor HCI event, the range is 0xC0 to 0xFF */ + uint8_t param_len; /*!< The length of the event parameter buffer */ + uint8_t *param_buf; /*!< The pointer of the event parameter buffer */ + } vendor_hci_evt; /*!< Event parameter buffer of ESP_GAP_BLE_VENDOR_HCI_EVT */ } esp_ble_gap_cb_param_t; /** diff --git a/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c b/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c index d291648850..c4cbdc0a1d 100644 --- a/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c +++ b/components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c @@ -1400,6 +1400,26 @@ static void btc_ble_set_vendor_evt_mask_callback(UINT8 status) } } +static void btc_ble_vendor_hci_event_callback(UINT8 subevt_code, UINT8 param_len, UINT8 *params) +{ + esp_ble_gap_cb_param_t param = {0}; + bt_status_t ret; + btc_msg_t msg = {0}; + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_GAP_BLE; + msg.act = ESP_GAP_BLE_VENDOR_HCI_EVT; + + param.vendor_hci_evt.subevt_code = subevt_code; + param.vendor_hci_evt.param_len = param_len; + param.vendor_hci_evt.param_buf = params; + ret = btc_transfer_context(&msg, ¶m, sizeof(esp_ble_gap_cb_param_t), btc_gap_ble_cb_deep_copy, btc_gap_ble_cb_deep_free); + + if (ret != BT_STATUS_SUCCESS) { + BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__); + } +} + void btc_get_whitelist_size(uint16_t *length) { BTM_BleGetWhiteListSize(length); @@ -1822,6 +1842,18 @@ void btc_gap_ble_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) } break; } + case ESP_GAP_BLE_VENDOR_HCI_EVT: { + if (src->vendor_hci_evt.param_len) { + dst->vendor_hci_evt.param_buf = osi_malloc(src->vendor_hci_evt.param_len); + if (dst->vendor_hci_evt.param_buf) { + memcpy(dst->vendor_hci_evt.param_buf, src->vendor_hci_evt.param_buf, + src->vendor_hci_evt.param_len); + } else { + BTC_TRACE_ERROR("%s, malloc failed\n", __func__); + } + } + break; + } default: BTC_TRACE_ERROR("%s, Unhandled deep copy %d\n", __func__, msg->act); break; @@ -1966,6 +1998,13 @@ void btc_gap_ble_cb_deep_free(btc_msg_t *msg) } break; } + case ESP_GAP_BLE_VENDOR_HCI_EVT: { + void *value = ((esp_ble_gap_cb_param_t *)msg->arg)->vendor_hci_evt.param_buf; + if (value) { + osi_free(value); + } + break; + } default: BTC_TRACE_DEBUG("Unhandled deep free %d", msg->act); break; @@ -2498,6 +2537,7 @@ void btc_gap_callback_init(void) #if (BLE_50_FEATURE_SUPPORT == TRUE) BTM_BleGapRegisterCallback(btc_ble_5_gap_callback); #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE) + BTM_BleRegisterVendorHciEventCallback(btc_ble_vendor_hci_event_callback); } bool btc_gap_ble_init(void) diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c index 1e70ecd4f6..185e57d172 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c @@ -71,6 +71,8 @@ static tBTM_BLE_CTRL_FEATURES_CBACK *p_ctrl_le_feature_rd_cmpl_cback = NULL; #endif tBTM_CallbackFunc conn_callback_func; +// BLE vendor HCI event callback +static tBTM_BLE_VENDOR_HCI_EVT_CBACK *ble_vs_evt_callback = NULL; /******************************************************************************* ** Local functions *******************************************************************************/ @@ -351,6 +353,11 @@ void BTM_BleRegiseterPktLengthChangeCallback(tBTM_SET_PKT_DATA_LENGTH_CBACK *ptk conn_callback_func.set_pkt_data_length_cb = ptk_len_chane_cb; } +void BTM_BleRegisterVendorHciEventCallback(tBTM_BLE_VENDOR_HCI_EVT_CBACK *vendor_hci_evt_cb) +{ + ble_vs_evt_callback = vendor_hci_evt_cb; +} + /******************************************************************************* ** ** Function BTM_BleUpdateAdvWhitelist @@ -4542,6 +4549,26 @@ BOOLEAN btm_ble_update_mode_operation(UINT8 link_role, BD_ADDR bd_addr, UINT8 st return bg_con; } +static void btm_ble_vs_evt_callback(UINT8 len, UINT8 *p) +{ + UINT8 sub_event; + + if (!len || !p) { + return; + } + + STREAM_TO_UINT8(sub_event, p); + len--; + + if (sub_event < HCI_VSE_LE_LEGACY_SCAN_REQ_RECEIVED_EVT) { + return; + } + + if (ble_vs_evt_callback) { + ble_vs_evt_callback(sub_event, len, p); + } +} + /******************************************************************************* ** ** Function btm_ble_init @@ -4600,6 +4627,8 @@ void btm_ble_init (void) btm_ble_adv_filter_init(); #endif // #if BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE #endif + + BTM_RegisterForVSEvents(btm_ble_vs_evt_callback, TRUE); } /******************************************************************************* diff --git a/components/bt/host/bluedroid/stack/include/stack/btm_api.h b/components/bt/host/bluedroid/stack/include/stack/btm_api.h index 596b024f92..8e51118f58 100644 --- a/components/bt/host/bluedroid/stack/include/stack/btm_api.h +++ b/components/bt/host/bluedroid/stack/include/stack/btm_api.h @@ -201,6 +201,8 @@ typedef void (tBTM_SET_LOCAL_PRIVACY_CBACK) (UINT8 status); typedef void (tBTM_SET_RPA_TIMEOUT_CMPL_CBACK) (UINT8 status); typedef void (tBTM_ADD_DEV_TO_RESOLVING_LIST_CMPL_CBACK) (UINT8 status); + +typedef void (tBTM_BLE_VENDOR_HCI_EVT_CBACK) (UINT8 subevt_code, UINT8 param_len, UINT8 *params); /******************************* ** Device Coexist status ********************************/ diff --git a/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h b/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h index 38f907bdcf..d10c02279e 100644 --- a/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h +++ b/components/bt/host/bluedroid/stack/include/stack/btm_ble_api.h @@ -1417,6 +1417,7 @@ extern "C" { *******************************************************************************/ void BTM_BleRegiseterConnParamCallback(tBTM_UPDATE_CONN_PARAM_CBACK *update_conn_param_cb); void BTM_BleRegiseterPktLengthChangeCallback(tBTM_SET_PKT_DATA_LENGTH_CBACK *ptk_len_chane_cb); +void BTM_BleRegisterVendorHciEventCallback(tBTM_BLE_VENDOR_HCI_EVT_CBACK *vendor_hci_evt_cb); /******************************************************************************* ** diff --git a/components/bt/host/bluedroid/stack/include/stack/hcidefs.h b/components/bt/host/bluedroid/stack/include/stack/hcidefs.h index 42773f0c8b..b9b2206979 100644 --- a/components/bt/host/bluedroid/stack/include/stack/hcidefs.h +++ b/components/bt/host/bluedroid/stack/include/stack/hcidefs.h @@ -832,6 +832,9 @@ #define HCI_BLE_CHNL_MAP_SIZE 5 #define HCI_VENDOR_SPECIFIC_EVT 0xFF /* Vendor specific events */ +#define HCI_VSE_LE_LEGACY_SCAN_REQ_RECEIVED_EVT 0xC0 +#define HCI_VSE_LE_CHAN_MAP_UPDATE_CMPL_EVT 0xC1 +#define HCI_VSE_LE_EVT_MAX 0xFF #define HCI_NAP_TRACE_EVT 0xFF /* was define 0xFE, 0xFD, change to 0xFF because conflict w/ TCI_EVT and per specification compliant */