feat(ble/bluedroid): Implement BLE channel map reading API

This commit is contained in:
zhanghaipeng
2025-03-24 20:24:40 +08:00
parent 2fd56b7ed8
commit 6886dc78c6
13 changed files with 242 additions and 1 deletions

View File

@@ -581,6 +581,7 @@ esp_err_t esp_ble_gap_read_rssi(esp_bd_addr_t remote_addr)
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
#if (BLE_42_FEATURE_SUPPORT == TRUE)
esp_err_t esp_ble_gap_config_scan_rsp_data_raw(uint8_t *raw_data, uint32_t raw_data_len)
{
@@ -929,6 +930,26 @@ esp_err_t esp_gap_ble_set_channels(esp_gap_ble_channels channels)
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
esp_err_t esp_ble_gap_read_channel_map(esp_bd_addr_t bd_addr)
{
if (!bd_addr) {
return ESP_ERR_INVALID_ARG;
}
btc_msg_t msg;
btc_ble_gap_args_t arg;
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_READ_CHANNEL_MAP;
memcpy(arg.read_channel_map.bd_addr, bd_addr, ESP_BD_ADDR_LEN);
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL)
== BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
esp_err_t esp_gap_ble_set_authorization(esp_bd_addr_t bd_addr, bool authorize)
{
if (!bd_addr) {

View File

@@ -245,6 +245,7 @@ typedef enum {
ESP_GAP_BLE_SUBRATE_REQUEST_COMPLETE_EVT, /*!< when subrate request command complete, the event comes */
ESP_GAP_BLE_SUBRATE_CHANGE_EVT, /*!< when Connection Subrate Update procedure has completed and some parameters of the specified connection have changed, the event comes */
ESP_GAP_BLE_SET_HOST_FEATURE_CMPL_EVT, /*!< When host feature set complete, the event comes */
ESP_GAP_BLE_READ_CHANNEL_MAP_COMPLETE_EVT, /*!< When BLE channel map result is received, the event comes */
ESP_GAP_BLE_EVT_MAX, /*!< when maximum advertising event complete, the event comes */
} esp_gap_ble_cb_event_t;
@@ -1331,6 +1332,14 @@ typedef union {
if the RSSI cannot be read, the RSSI metric shall be set to 127. */
esp_bd_addr_t remote_addr; /*!< The remote device address */
} read_rssi_cmpl; /*!< Event parameter of ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT */
/**
* @brief ESP_GAP_BLE_READ_CHANNEL_MAP_COMPLETE_EVT
*/
struct ble_read_ble_channel_map_cmpl_evt_param {
esp_bt_status_t status; /*!< Status of the read channel map operation */
uint8_t channel_map[ESP_GAP_BLE_CHANNELS_LEN]; /*!< The BLE channel map, represented as a 5-byte array */
esp_bd_addr_t remote_addr; /*!< The remote device address */
} read_ble_channel_map_cmpl; /*!< Event parameter of ESP_GAP_BLE_READ_CHANNEL_MAP_COMPLETE_EVT */
/**
* @brief ESP_GAP_BLE_UPDATE_WHITELIST_COMPLETE_EVT
*/
@@ -2200,6 +2209,7 @@ esp_err_t esp_ble_gap_config_scan_rsp_data_raw(uint8_t *raw_data, uint32_t raw_d
* - other : failed
*/
esp_err_t esp_ble_gap_read_rssi(esp_bd_addr_t remote_addr);
#if (BLE_42_FEATURE_SUPPORT == TRUE)
/**
* @brief This function is called to add a device info into the duplicate scan exceptional list.
@@ -2447,6 +2457,18 @@ esp_err_t esp_ble_get_current_conn_params(esp_bd_addr_t bd_addr, esp_gap_conn_pa
*/
esp_err_t esp_gap_ble_set_channels(esp_gap_ble_channels channels);
/**
* @brief This function is used to read the current channel map
* for the connection identified by remote address.
*
* @param[in] bd_addr : BD address of the peer device
*
* @return - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gap_read_channel_map(esp_bd_addr_t bd_addr);
/**
* @brief This function is called to authorized a link after Authentication(MITM protection)
*

View File

@@ -5855,6 +5855,13 @@ void bta_dm_ble_gap_set_vendor_evt_mask(tBTA_DM_MSG *p_data)
BTM_BleSetVendorEventMask(p_data->ble_set_vendor_evt_mask.evt_mask, p_data->ble_set_vendor_evt_mask.p_cback);
}
void bta_dm_read_ble_channel_map(tBTA_DM_MSG *p_data)
{
if (p_data && p_data->ch_map.read_ch_map_cb) {
BTM_ReadChannelMap(p_data->ch_map.remote_addr, p_data->ch_map.read_ch_map_cb);
}
}
#if (BLE_50_DTM_TEST_EN == TRUE)
void bta_dm_ble_gap_dtm_enhance_tx_start(tBTA_DM_MSG *p_data)
{

View File

@@ -500,6 +500,20 @@ void BTA_DmBleReadAdvTxPower(tBTA_CMPL_CB *cmpl_cb)
}
#endif // BLE_HOST_READ_TX_POWER_EN
void BTA_DmBleReadChannelMap(BD_ADDR remote_device, tBTA_CMPL_CB *p_callback)
{
if (!remote_device || !p_callback) {
return;
}
tBTA_DM_API_READ_CH_MAP *p_msg;
if ((p_msg = (tBTA_DM_API_READ_CH_MAP *)osi_malloc(sizeof(tBTA_DM_API_READ_CH_MAP))) != NULL) {
p_msg->hdr.event = BTA_DM_API_BLE_READ_CH_MAP_EVT;
memcpy(p_msg->remote_addr, remote_device, sizeof(BD_ADDR));
p_msg->read_ch_map_cb = p_callback;
bta_sys_sendmsg(p_msg);
}
}
#endif ///BLE_INCLUDED == TRUE
void BTA_DmReadRSSI(BD_ADDR remote_addr, tBTA_TRANSPORT transport, tBTA_CMPL_CB *cmpl_cb)

View File

@@ -286,6 +286,7 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = {
bta_dm_ble_gap_set_privacy_mode, /* BTA_DM_API_SET_PRIVACY_MODE_EVT */
bta_dm_ble_gap_set_csa_support, /* BTA_DM_API_BLE_SET_CSA_SUPPORT_EVT */
bta_dm_ble_gap_set_vendor_evt_mask, /* BTA_DM_API_BLE_SET_VENDOR_EVT_MASK_EVT */
bta_dm_read_ble_channel_map, /* BTA_DM_API_BLE_READ_CH_MAP_EVT */
#endif
#if (BLE_FEAT_ISO_EN == TRUE)
#if (BLE_FEAT_ISO_BIG_BROCASTER_EN == TRUE)

View File

@@ -277,6 +277,7 @@ enum {
BTA_DM_API_SET_PRIVACY_MODE_EVT,
BTA_DM_API_BLE_SET_CSA_SUPPORT_EVT,
BTA_DM_API_BLE_SET_VENDOR_EVT_MASK_EVT,
BTA_DM_API_BLE_READ_CH_MAP_EVT,
#endif
#if (BLE_FEAT_ISO_EN == TRUE)
#if (BLE_FEAT_ISO_BIG_BROCASTER_EN == TRUE)
@@ -490,6 +491,12 @@ typedef struct {
tBTA_CMPL_CB *read_rssi_cb;
}tBTA_DM_API_READ_RSSI;
typedef struct {
BT_HDR hdr;
BD_ADDR remote_addr;
tBTA_CMPL_CB *read_ch_map_cb;
} tBTA_DM_API_READ_CH_MAP;
/* data type for BTA_DM_API_SET_VISIBILITY_EVT */
typedef struct {
BT_HDR hdr;
@@ -1639,6 +1646,8 @@ typedef union {
#endif ///BLE_INCLUDED == TRUE
tBTA_DM_API_READ_RSSI rssi;
tBTA_DM_API_READ_CH_MAP ch_map;
tBTA_DM_API_SET_VISIBILITY set_visibility;
tBTA_DM_API_ADD_DEVICE add_dev;
@@ -2250,6 +2259,7 @@ extern void bta_dm_update_white_list(tBTA_DM_MSG *p_data);
extern void bta_dm_clear_white_list(tBTA_DM_MSG *p_data);
extern void bta_dm_ble_read_adv_tx_power(tBTA_DM_MSG *p_data);
extern void bta_dm_read_rssi(tBTA_DM_MSG *p_data);
extern void bta_dm_read_ble_channel_map(tBTA_DM_MSG *p_data);
#if (CLASSIC_BT_INCLUDED == TRUE)
extern void bta_dm_set_visibility (tBTA_DM_MSG *p_data);
#endif // #if (CLASSIC_BT_INCLUDED == TRUE)

View File

@@ -451,6 +451,8 @@ typedef tBTM_TX_POWER_RESULTS tBTA_TX_POWER_RESULTS;
typedef tBTM_RSSI_RESULTS tBTA_RSSI_RESULTS;
typedef tBTM_BLE_CH_MAP_RESULTS tBTA_BLE_CH_MAP_RESULTS;
typedef tBTM_SET_AFH_CHANNELS_RESULTS tBTA_SET_AFH_CHANNELS_RESULTS;
typedef tBTM_BLE_SET_CHANNELS_RESULTS tBTA_BLE_SET_CHANNELS_RESULTS;
@@ -2067,6 +2069,7 @@ extern void BTA_DmBleReadAdvTxPower(tBTA_CMPL_CB *cmpl_cb);
#endif ///BLE_INCLUDED == TRUE
extern void BTA_DmReadRSSI(BD_ADDR remote_addr, tBTA_TRANSPORT transport, tBTA_CMPL_CB *cmpl_cb);
extern void BTA_DmBleReadChannelMap(BD_ADDR remote_device, tBTA_CMPL_CB *p_callback);
/*******************************************************************************
**

View File

@@ -935,6 +935,29 @@ static void btc_read_ble_rssi_cmpl_callback(void *p_data)
}
}
static void btc_ble_read_channel_map_callback(void *p_data)
{
tBTA_BLE_CH_MAP_RESULTS *result = (tBTA_BLE_CH_MAP_RESULTS *)p_data;
esp_ble_gap_cb_param_t param;
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_READ_CHANNEL_MAP_COMPLETE_EVT;
param.read_ble_channel_map_cmpl.status = btc_btm_status_to_esp_status(result->status);
memcpy(param.read_ble_channel_map_cmpl.channel_map, result->channel_map, 5);
memcpy(param.read_ble_channel_map_cmpl.remote_addr, result->rem_bda, sizeof(BD_ADDR));
ret = btc_transfer_context(&msg, &param, sizeof(esp_ble_gap_cb_param_t), NULL, NULL);
if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
}
}
#if (BLE_50_FEATURE_SUPPORT == TRUE)
static void btc_ble_5_gap_callback(tBTA_DM_BLE_5_GAP_EVENT event,
tBTA_DM_BLE_5_GAP_CB_PARAMS *params)
@@ -2177,6 +2200,9 @@ void btc_gap_ble_call_handler(btc_msg_t *msg)
case BTC_GAP_BLE_ACT_READ_RSSI:
BTA_DmReadRSSI(arg->read_rssi.remote_addr, BTA_TRANSPORT_LE, btc_read_ble_rssi_cmpl_callback);
break;
case BTC_GAP_BLE_READ_CHANNEL_MAP:
BTA_DmBleReadChannelMap(arg->read_channel_map.bd_addr, btc_ble_read_channel_map_callback);
break;
#if (BLE_42_FEATURE_SUPPORT == TRUE)
case BTC_GAP_BLE_ACT_SET_CONN_PARAMS:
BTA_DmSetBlePrefConnParams(arg->set_conn_params.bd_addr, arg->set_conn_params.min_conn_int,

View File

@@ -136,6 +136,7 @@ typedef enum {
#if (BLE_50_FEATURE_SUPPORT == TRUE)
BTC_GAP_ACT_SET_HOST_FEATURE,
#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
BTC_GAP_BLE_READ_CHANNEL_MAP,
} btc_gap_ble_act_t;
/* btc_ble_gap_args_t */
@@ -314,6 +315,9 @@ typedef union {
struct set_vendor_evt_mask_args {
uint32_t evt_mask;
} set_vendor_evt_mask;
struct read_channel_map_args {
esp_bd_addr_t bd_addr;
} read_channel_map;
} btc_ble_gap_args_t;
#if (BLE_50_FEATURE_SUPPORT == TRUE)

View File

@@ -2184,6 +2184,54 @@ void btm_acl_pkt_types_changed(UINT8 status, UINT16 handle, UINT16 pkt_types)
}
#if (BLE_INCLUDED == TRUE)
/*******************************************************************************
**
** Function BTM_ReadChannelMap
**
** Description This function is called to read the current channel map
** for the given connection. The results are returned via
** the callback (tBTM_BLE_CH_MAP_RESULTS).
**
** Returns BTM_CMD_STARTED if successfully initiated or error code
**
*******************************************************************************/
tBTM_STATUS BTM_ReadChannelMap(BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
{
tACL_CONN *p;
tBTM_BLE_CH_MAP_RESULTS result;
BTM_TRACE_DEBUG("BTM_ReadChannelMap: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
remote_bda[0], remote_bda[1], remote_bda[2],
remote_bda[3], remote_bda[4], remote_bda[5]);
memset(result.channel_map, 0, sizeof(result.channel_map)); // Clear channel map data
/* If someone already waiting for the channel map, do not allow another */
if (btm_cb.devcb.p_ble_ch_map_cmpl_cb) {
result.status = BTM_BUSY;
(*p_cb)(&result);
return BTM_BUSY;
}
p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
if (p != NULL) {
btm_cb.devcb.p_ble_ch_map_cmpl_cb = p_cb;
if (!btsnd_hcic_ble_read_chnl_map(p->hci_handle)) {
btm_cb.devcb.p_ble_ch_map_cmpl_cb = NULL;
result.status = BTM_NO_RESOURCES;
(*p_cb)(&result);
return BTM_NO_RESOURCES;
} else {
return BTM_CMD_STARTED;
}
}
/* If here, no BD Addr found */
result.status = BTM_UNKNOWN_ADDR;
(*p_cb)(&result);
return BTM_UNKNOWN_ADDR;
}
#if (BLE_HOST_READ_TX_POWER_EN == TRUE)
tBTM_STATUS BTM_BleReadAdvTxPower(tBTM_CMPL_CB *p_cb)
{
@@ -2280,6 +2328,62 @@ void btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble)
}
#endif // #if (BLE_HOST_READ_TX_POWER_EN == TRUE)
/*******************************************************************************
**
** Function btm_read_channel_map_complete
**
** Description This function is called when the command complete message
** is received from the HCI for the read channel map request.
** It processes the received channel map data and invokes the
** registered callback function with the results.
**
** Returns void
**
*******************************************************************************/
void btm_read_channel_map_complete(UINT8 *p)
{
tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_ble_ch_map_cmpl_cb;
tBTM_BLE_CH_MAP_RESULTS results;
UINT16 handle;
tACL_CONN *p_acl_cb = NULL;
BTM_TRACE_DEBUG("btm_read_channel_map_complete\n");
/* Reset the callback pointer to prevent duplicate calls */
btm_cb.devcb.p_ble_ch_map_cmpl_cb = NULL;
if (p_cb) {
/* Extract HCI status from the response */
STREAM_TO_UINT8(results.hci_status, p);
if (results.hci_status == HCI_SUCCESS) {
results.status = BTM_SUCCESS;
/* Extract the connection handle and channel map */
STREAM_TO_UINT16(handle, p);
STREAM_TO_ARRAY(results.channel_map, p, 5);
BTM_TRACE_DEBUG("BTM Channel Map Complete: handle 0x%04x, hci status 0x%02x", handle, results.hci_status);
BTM_TRACE_DEBUG("Channel Map: %02x %02x %02x %02x %02x",
results.channel_map[0], results.channel_map[1], results.channel_map[2],
results.channel_map[3], results.channel_map[4]);
/* Retrieve the remote BD address using the connection handle */
p_acl_cb = btm_handle_to_acl(handle);
if (p_acl_cb) {
memcpy(results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
}
} else {
results.status = BTM_ERR_PROCESSING;
BTM_TRACE_ERROR("BTM Channel Map Read Failed: hci status 0x%02x", results.hci_status);
}
/* Invoke the registered callback with the results */
(*p_cb)(&results);
}
}
/*******************************************************************************
**
** Function btm_read_rssi_complete

View File

@@ -191,6 +191,10 @@ tBTM_CMPL_CB *p_rln_cmpl_cb; /* Callback function to be called when
TIMER_LIST_ENT rssi_timer;
tBTM_CMPL_CB *p_rssi_cmpl_cb; /* Callback function to be called when */
/* read rssi function completes */
tBTM_CMPL_CB *p_ble_ch_map_cmpl_cb; /* Callback function to be called when */
/* read channel map function completes */
TIMER_LIST_ENT lnk_quality_timer;
tBTM_CMPL_CB *p_lnk_qual_cmpl_cb;/* Callback function to be called when */
/* read link quality function completes */
@@ -1068,6 +1072,7 @@ void btm_cont_rswitch (tACL_CONN *p,
tACL_CONN *btm_handle_to_acl (UINT16 hci_handle);
void btm_read_link_policy_complete (UINT8 *p);
void btm_read_rssi_complete (UINT8 *p);
void btm_read_channel_map_complete (UINT8 *p);
void btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble);
void btm_acl_pkt_types_changed(UINT8 status, UINT16 handle, UINT16 pkt_types);
void btm_read_link_quality_complete (UINT8 *p);

View File

@@ -1153,7 +1153,9 @@ static void btu_hcif_hdl_command_complete (UINT16 opcode, UINT8 *p, UINT16 evt_l
case HCI_READ_RSSI:
btm_read_rssi_complete (p);
break;
case HCI_BLE_READ_CHNL_MAP:
btm_read_channel_map_complete (p);
break;
case HCI_READ_TRANSMIT_POWER_LEVEL:
#if (BLE_HOST_READ_TX_POWER_EN == TRUE)
btm_read_tx_power_complete(p, FALSE);

View File

@@ -811,6 +811,16 @@ typedef struct {
BD_ADDR rem_bda;
} tBTM_RSSI_RESULTS;
/* Structure returned with read channel map event (in tBTM_CMPL_CB callback function)
** in response to BTM_ReadChannelMap call.
*/
typedef struct {
tBTM_STATUS status; /* BTM operation status */
UINT8 hci_status; /* HCI command complete status */
UINT8 channel_map[5]; /* Channel map (5 bytes) */
BD_ADDR rem_bda; /* Remote device Bluetooth address */
} tBTM_BLE_CH_MAP_RESULTS;
/* Structure returned with read current TX power event (in tBTM_CMPL_CB callback function)
** in response to BTM_ReadTxPower call.
*/
@@ -3024,6 +3034,18 @@ tBTM_STATUS BTM_SwitchRole (BD_ADDR remote_bd_addr,
//extern
tBTM_STATUS BTM_ReadRSSI (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb);
/*******************************************************************************
**
** Function BTM_ReadChannelMap
**
** Description This function is called to read the current channel map
** for the given connection. The results are returned via
** the callback (tBTM_BLE_CH_MAP_RESULTS).
**
** Returns BTM_CMD_STARTED if successfully initiated or error code
**
*******************************************************************************/
tBTM_STATUS BTM_ReadChannelMap(BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb);
#if (BLE_HOST_READ_TX_POWER_EN == TRUE)
/*******************************************************************************
**