Skip to content

Commit 55043e2

Browse files
committed
feat(ble/bluedroid): Implement BLE channel map reading API
1 parent a928656 commit 55043e2

File tree

13 files changed

+242
-1
lines changed

13 files changed

+242
-1
lines changed

components/bt/host/bluedroid/api/esp_gap_ble_api.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ esp_err_t esp_ble_gap_read_rssi(esp_bd_addr_t remote_addr)
581581

582582
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
583583
}
584+
584585
#if (BLE_42_FEATURE_SUPPORT == TRUE)
585586
esp_err_t esp_ble_gap_config_scan_rsp_data_raw(uint8_t *raw_data, uint32_t raw_data_len)
586587
{
@@ -929,6 +930,26 @@ esp_err_t esp_gap_ble_set_channels(esp_gap_ble_channels channels)
929930
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
930931
}
931932

933+
esp_err_t esp_ble_gap_read_channel_map(esp_bd_addr_t bd_addr)
934+
{
935+
if (!bd_addr) {
936+
return ESP_ERR_INVALID_ARG;
937+
}
938+
939+
btc_msg_t msg;
940+
btc_ble_gap_args_t arg;
941+
942+
ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
943+
944+
msg.sig = BTC_SIG_API_CALL;
945+
msg.pid = BTC_PID_GAP_BLE;
946+
msg.act = BTC_GAP_BLE_READ_CHANNEL_MAP;
947+
948+
memcpy(arg.read_channel_map.bd_addr, bd_addr, ESP_BD_ADDR_LEN);
949+
return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL)
950+
== BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
951+
}
952+
932953
esp_err_t esp_gap_ble_set_authorization(esp_bd_addr_t bd_addr, bool authorize)
933954
{
934955
if (!bd_addr) {

components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ typedef enum {
245245
ESP_GAP_BLE_SUBRATE_REQUEST_COMPLETE_EVT, /*!< when subrate request command complete, the event comes */
246246
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 */
247247
ESP_GAP_BLE_SET_HOST_FEATURE_CMPL_EVT, /*!< When host feature set complete, the event comes */
248+
ESP_GAP_BLE_READ_CHANNEL_MAP_COMPLETE_EVT, /*!< When BLE channel map result is received, the event comes */
248249
ESP_GAP_BLE_EVT_MAX, /*!< when maximum advertising event complete, the event comes */
249250
} esp_gap_ble_cb_event_t;
250251

@@ -1324,6 +1325,14 @@ typedef union {
13241325
if the RSSI cannot be read, the RSSI metric shall be set to 127. */
13251326
esp_bd_addr_t remote_addr; /*!< The remote device address */
13261327
} read_rssi_cmpl; /*!< Event parameter of ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT */
1328+
/**
1329+
* @brief ESP_GAP_BLE_READ_CHANNEL_MAP_COMPLETE_EVT
1330+
*/
1331+
struct ble_read_ble_channel_map_cmpl_evt_param {
1332+
esp_bt_status_t status; /*!< Status of the read channel map operation */
1333+
uint8_t channel_map[ESP_GAP_BLE_CHANNELS_LEN]; /*!< The BLE channel map, represented as a 5-byte array */
1334+
esp_bd_addr_t remote_addr; /*!< The remote device address */
1335+
} read_ble_channel_map_cmpl; /*!< Event parameter of ESP_GAP_BLE_READ_CHANNEL_MAP_COMPLETE_EVT */
13271336
/**
13281337
* @brief ESP_GAP_BLE_UPDATE_WHITELIST_COMPLETE_EVT
13291338
*/
@@ -2194,6 +2203,7 @@ esp_err_t esp_ble_gap_config_scan_rsp_data_raw(uint8_t *raw_data, uint32_t raw_d
21942203
* - other : failed
21952204
*/
21962205
esp_err_t esp_ble_gap_read_rssi(esp_bd_addr_t remote_addr);
2206+
21972207
#if (BLE_42_FEATURE_SUPPORT == TRUE)
21982208
/**
21992209
* @brief This function is called to add a device info into the duplicate scan exceptional list.
@@ -2441,6 +2451,18 @@ esp_err_t esp_ble_get_current_conn_params(esp_bd_addr_t bd_addr, esp_gap_conn_pa
24412451
*/
24422452
esp_err_t esp_gap_ble_set_channels(esp_gap_ble_channels channels);
24432453

2454+
/**
2455+
* @brief This function is used to read the current channel map
2456+
* for the connection identified by remote address.
2457+
*
2458+
* @param[in] bd_addr : BD address of the peer device
2459+
*
2460+
* @return - ESP_OK : success
2461+
* - other : failed
2462+
*
2463+
*/
2464+
esp_err_t esp_ble_gap_read_channel_map(esp_bd_addr_t bd_addr);
2465+
24442466
/**
24452467
* @brief This function is called to authorized a link after Authentication(MITM protection)
24462468
*

components/bt/host/bluedroid/bta/dm/bta_dm_act.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5865,6 +5865,13 @@ void bta_dm_ble_gap_set_vendor_evt_mask(tBTA_DM_MSG *p_data)
58655865
BTM_BleSetVendorEventMask(p_data->ble_set_vendor_evt_mask.evt_mask, p_data->ble_set_vendor_evt_mask.p_cback);
58665866
}
58675867

5868+
void bta_dm_read_ble_channel_map(tBTA_DM_MSG *p_data)
5869+
{
5870+
if (p_data && p_data->ch_map.read_ch_map_cb) {
5871+
BTM_ReadChannelMap(p_data->ch_map.remote_addr, p_data->ch_map.read_ch_map_cb);
5872+
}
5873+
}
5874+
58685875
#if (BLE_50_DTM_TEST_EN == TRUE)
58695876
void bta_dm_ble_gap_dtm_enhance_tx_start(tBTA_DM_MSG *p_data)
58705877
{

components/bt/host/bluedroid/bta/dm/bta_dm_api.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,20 @@ void BTA_DmBleReadAdvTxPower(tBTA_CMPL_CB *cmpl_cb)
500500
}
501501
#endif // BLE_HOST_READ_TX_POWER_EN
502502

503+
void BTA_DmBleReadChannelMap(BD_ADDR remote_device, tBTA_CMPL_CB *p_callback)
504+
{
505+
if (!remote_device || !p_callback) {
506+
return;
507+
}
508+
tBTA_DM_API_READ_CH_MAP *p_msg;
509+
510+
if ((p_msg = (tBTA_DM_API_READ_CH_MAP *)osi_malloc(sizeof(tBTA_DM_API_READ_CH_MAP))) != NULL) {
511+
p_msg->hdr.event = BTA_DM_API_BLE_READ_CH_MAP_EVT;
512+
memcpy(p_msg->remote_addr, remote_device, sizeof(BD_ADDR));
513+
p_msg->read_ch_map_cb = p_callback;
514+
bta_sys_sendmsg(p_msg);
515+
}
516+
}
503517
#endif ///BLE_INCLUDED == TRUE
504518

505519
void BTA_DmReadRSSI(BD_ADDR remote_addr, tBTA_TRANSPORT transport, tBTA_CMPL_CB *cmpl_cb)

components/bt/host/bluedroid/bta/dm/bta_dm_main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = {
286286
bta_dm_ble_gap_set_privacy_mode, /* BTA_DM_API_SET_PRIVACY_MODE_EVT */
287287
bta_dm_ble_gap_set_csa_support, /* BTA_DM_API_BLE_SET_CSA_SUPPORT_EVT */
288288
bta_dm_ble_gap_set_vendor_evt_mask, /* BTA_DM_API_BLE_SET_VENDOR_EVT_MASK_EVT */
289+
bta_dm_read_ble_channel_map, /* BTA_DM_API_BLE_READ_CH_MAP_EVT */
289290
#endif
290291
#if (BLE_FEAT_ISO_EN == TRUE)
291292
#if (BLE_FEAT_ISO_BIG_BROCASTER_EN == TRUE)

components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ enum {
277277
BTA_DM_API_SET_PRIVACY_MODE_EVT,
278278
BTA_DM_API_BLE_SET_CSA_SUPPORT_EVT,
279279
BTA_DM_API_BLE_SET_VENDOR_EVT_MASK_EVT,
280+
BTA_DM_API_BLE_READ_CH_MAP_EVT,
280281
#endif
281282
#if (BLE_FEAT_ISO_EN == TRUE)
282283
#if (BLE_FEAT_ISO_BIG_BROCASTER_EN == TRUE)
@@ -490,6 +491,12 @@ typedef struct {
490491
tBTA_CMPL_CB *read_rssi_cb;
491492
}tBTA_DM_API_READ_RSSI;
492493

494+
typedef struct {
495+
BT_HDR hdr;
496+
BD_ADDR remote_addr;
497+
tBTA_CMPL_CB *read_ch_map_cb;
498+
} tBTA_DM_API_READ_CH_MAP;
499+
493500
/* data type for BTA_DM_API_SET_VISIBILITY_EVT */
494501
typedef struct {
495502
BT_HDR hdr;
@@ -1639,6 +1646,8 @@ typedef union {
16391646
#endif ///BLE_INCLUDED == TRUE
16401647
tBTA_DM_API_READ_RSSI rssi;
16411648

1649+
tBTA_DM_API_READ_CH_MAP ch_map;
1650+
16421651
tBTA_DM_API_SET_VISIBILITY set_visibility;
16431652

16441653
tBTA_DM_API_ADD_DEVICE add_dev;
@@ -2250,6 +2259,7 @@ extern void bta_dm_update_white_list(tBTA_DM_MSG *p_data);
22502259
extern void bta_dm_clear_white_list(tBTA_DM_MSG *p_data);
22512260
extern void bta_dm_ble_read_adv_tx_power(tBTA_DM_MSG *p_data);
22522261
extern void bta_dm_read_rssi(tBTA_DM_MSG *p_data);
2262+
extern void bta_dm_read_ble_channel_map(tBTA_DM_MSG *p_data);
22532263
#if (CLASSIC_BT_INCLUDED == TRUE)
22542264
extern void bta_dm_set_visibility (tBTA_DM_MSG *p_data);
22552265
#endif // #if (CLASSIC_BT_INCLUDED == TRUE)

components/bt/host/bluedroid/bta/include/bta/bta_api.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ typedef tBTM_TX_POWER_RESULTS tBTA_TX_POWER_RESULTS;
451451

452452
typedef tBTM_RSSI_RESULTS tBTA_RSSI_RESULTS;
453453

454+
typedef tBTM_BLE_CH_MAP_RESULTS tBTA_BLE_CH_MAP_RESULTS;
455+
454456
typedef tBTM_SET_AFH_CHANNELS_RESULTS tBTA_SET_AFH_CHANNELS_RESULTS;
455457
typedef tBTM_BLE_SET_CHANNELS_RESULTS tBTA_BLE_SET_CHANNELS_RESULTS;
456458

@@ -2067,6 +2069,7 @@ extern void BTA_DmBleReadAdvTxPower(tBTA_CMPL_CB *cmpl_cb);
20672069
#endif ///BLE_INCLUDED == TRUE
20682070

20692071
extern void BTA_DmReadRSSI(BD_ADDR remote_addr, tBTA_TRANSPORT transport, tBTA_CMPL_CB *cmpl_cb);
2072+
extern void BTA_DmBleReadChannelMap(BD_ADDR remote_device, tBTA_CMPL_CB *p_callback);
20702073

20712074
/*******************************************************************************
20722075
**

components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,29 @@ static void btc_read_ble_rssi_cmpl_callback(void *p_data)
935935
}
936936
}
937937

938+
static void btc_ble_read_channel_map_callback(void *p_data)
939+
{
940+
tBTA_BLE_CH_MAP_RESULTS *result = (tBTA_BLE_CH_MAP_RESULTS *)p_data;
941+
esp_ble_gap_cb_param_t param;
942+
bt_status_t ret;
943+
btc_msg_t msg = {0};
944+
945+
msg.sig = BTC_SIG_API_CB;
946+
msg.pid = BTC_PID_GAP_BLE;
947+
msg.act = ESP_GAP_BLE_READ_CHANNEL_MAP_COMPLETE_EVT;
948+
949+
param.read_ble_channel_map_cmpl.status = btc_btm_status_to_esp_status(result->status);
950+
memcpy(param.read_ble_channel_map_cmpl.channel_map, result->channel_map, 5);
951+
memcpy(param.read_ble_channel_map_cmpl.remote_addr, result->rem_bda, sizeof(BD_ADDR));
952+
953+
ret = btc_transfer_context(&msg, &param, sizeof(esp_ble_gap_cb_param_t), NULL, NULL);
954+
955+
if (ret != BT_STATUS_SUCCESS) {
956+
BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
957+
}
958+
}
959+
960+
938961
#if (BLE_50_FEATURE_SUPPORT == TRUE)
939962
void btc_ble_5_gap_callback(tBTA_DM_BLE_5_GAP_EVENT event,
940963
tBTA_DM_BLE_5_GAP_CB_PARAMS *params)
@@ -2178,6 +2201,9 @@ void btc_gap_ble_call_handler(btc_msg_t *msg)
21782201
case BTC_GAP_BLE_ACT_READ_RSSI:
21792202
BTA_DmReadRSSI(arg->read_rssi.remote_addr, BTA_TRANSPORT_LE, btc_read_ble_rssi_cmpl_callback);
21802203
break;
2204+
case BTC_GAP_BLE_READ_CHANNEL_MAP:
2205+
BTA_DmBleReadChannelMap(arg->read_channel_map.bd_addr, btc_ble_read_channel_map_callback);
2206+
break;
21812207
#if (BLE_42_FEATURE_SUPPORT == TRUE)
21822208
case BTC_GAP_BLE_ACT_SET_CONN_PARAMS:
21832209
BTA_DmSetBlePrefConnParams(arg->set_conn_params.bd_addr, arg->set_conn_params.min_conn_int,

components/bt/host/bluedroid/btc/profile/std/include/btc_gap_ble.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ typedef enum {
136136
#if (BLE_50_FEATURE_SUPPORT == TRUE)
137137
BTC_GAP_ACT_SET_HOST_FEATURE,
138138
#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
139+
BTC_GAP_BLE_READ_CHANNEL_MAP,
139140
} btc_gap_ble_act_t;
140141

141142
/* btc_ble_gap_args_t */
@@ -314,6 +315,9 @@ typedef union {
314315
struct set_vendor_evt_mask_args {
315316
uint32_t evt_mask;
316317
} set_vendor_evt_mask;
318+
struct read_channel_map_args {
319+
esp_bd_addr_t bd_addr;
320+
} read_channel_map;
317321
} btc_ble_gap_args_t;
318322

319323
#if (BLE_50_FEATURE_SUPPORT == TRUE)

components/bt/host/bluedroid/stack/btm/btm_acl.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,54 @@ void btm_acl_pkt_types_changed(UINT8 status, UINT16 handle, UINT16 pkt_types)
21892189
}
21902190

21912191
#if (BLE_INCLUDED == TRUE)
2192+
2193+
/*******************************************************************************
2194+
**
2195+
** Function BTM_ReadChannelMap
2196+
**
2197+
** Description This function is called to read the current channel map
2198+
** for the given connection. The results are returned via
2199+
** the callback (tBTM_BLE_CH_MAP_RESULTS).
2200+
**
2201+
** Returns BTM_CMD_STARTED if successfully initiated or error code
2202+
**
2203+
*******************************************************************************/
2204+
tBTM_STATUS BTM_ReadChannelMap(BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2205+
{
2206+
tACL_CONN *p;
2207+
tBTM_BLE_CH_MAP_RESULTS result;
2208+
2209+
BTM_TRACE_DEBUG("BTM_ReadChannelMap: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
2210+
remote_bda[0], remote_bda[1], remote_bda[2],
2211+
remote_bda[3], remote_bda[4], remote_bda[5]);
2212+
2213+
memset(result.channel_map, 0, sizeof(result.channel_map)); // Clear channel map data
2214+
/* If someone already waiting for the channel map, do not allow another */
2215+
if (btm_cb.devcb.p_ble_ch_map_cmpl_cb) {
2216+
result.status = BTM_BUSY;
2217+
(*p_cb)(&result);
2218+
return BTM_BUSY;
2219+
}
2220+
p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
2221+
if (p != NULL) {
2222+
btm_cb.devcb.p_ble_ch_map_cmpl_cb = p_cb;
2223+
2224+
if (!btsnd_hcic_ble_read_chnl_map(p->hci_handle)) {
2225+
btm_cb.devcb.p_ble_ch_map_cmpl_cb = NULL;
2226+
result.status = BTM_NO_RESOURCES;
2227+
(*p_cb)(&result);
2228+
return BTM_NO_RESOURCES;
2229+
} else {
2230+
return BTM_CMD_STARTED;
2231+
}
2232+
}
2233+
2234+
/* If here, no BD Addr found */
2235+
result.status = BTM_UNKNOWN_ADDR;
2236+
(*p_cb)(&result);
2237+
return BTM_UNKNOWN_ADDR;
2238+
}
2239+
21922240
#if (BLE_HOST_READ_TX_POWER_EN == TRUE)
21932241
tBTM_STATUS BTM_BleReadAdvTxPower(tBTM_CMPL_CB *p_cb)
21942242
{
@@ -2297,6 +2345,62 @@ void btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble)
22972345
}
22982346
#endif // #if (BLE_HOST_READ_TX_POWER_EN == TRUE)
22992347

2348+
/*******************************************************************************
2349+
**
2350+
** Function btm_read_channel_map_complete
2351+
**
2352+
** Description This function is called when the command complete message
2353+
** is received from the HCI for the read channel map request.
2354+
** It processes the received channel map data and invokes the
2355+
** registered callback function with the results.
2356+
**
2357+
** Returns void
2358+
**
2359+
*******************************************************************************/
2360+
void btm_read_channel_map_complete(UINT8 *p)
2361+
{
2362+
tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_ble_ch_map_cmpl_cb;
2363+
tBTM_BLE_CH_MAP_RESULTS results;
2364+
UINT16 handle;
2365+
tACL_CONN *p_acl_cb = NULL;
2366+
2367+
BTM_TRACE_DEBUG("btm_read_channel_map_complete\n");
2368+
2369+
/* Reset the callback pointer to prevent duplicate calls */
2370+
btm_cb.devcb.p_ble_ch_map_cmpl_cb = NULL;
2371+
2372+
if (p_cb) {
2373+
/* Extract HCI status from the response */
2374+
STREAM_TO_UINT8(results.hci_status, p);
2375+
2376+
if (results.hci_status == HCI_SUCCESS) {
2377+
results.status = BTM_SUCCESS;
2378+
2379+
/* Extract the connection handle and channel map */
2380+
STREAM_TO_UINT16(handle, p);
2381+
STREAM_TO_ARRAY(results.channel_map, p, 5);
2382+
2383+
BTM_TRACE_DEBUG("BTM Channel Map Complete: handle 0x%04x, hci status 0x%02x", handle, results.hci_status);
2384+
BTM_TRACE_DEBUG("Channel Map: %02x %02x %02x %02x %02x",
2385+
results.channel_map[0], results.channel_map[1], results.channel_map[2],
2386+
results.channel_map[3], results.channel_map[4]);
2387+
2388+
/* Retrieve the remote BD address using the connection handle */
2389+
p_acl_cb = btm_handle_to_acl(handle);
2390+
if (p_acl_cb) {
2391+
memcpy(results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2392+
}
2393+
} else {
2394+
results.status = BTM_ERR_PROCESSING;
2395+
BTM_TRACE_ERROR("BTM Channel Map Read Failed: hci status 0x%02x", results.hci_status);
2396+
}
2397+
2398+
/* Invoke the registered callback with the results */
2399+
(*p_cb)(&results);
2400+
}
2401+
}
2402+
2403+
23002404
/*******************************************************************************
23012405
**
23022406
** Function btm_read_rssi_complete

0 commit comments

Comments
 (0)