Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_some_ble_bug_v5.1' into 'release/v5.1'
Browse files Browse the repository at this point in the history
Fixed some BLE bugs (backport v5.1)

See merge request espressif/esp-idf!23324
  • Loading branch information
jack0c committed Apr 24, 2023
2 parents a12e124 + d0011b7 commit 1644050
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ int bt_le_update_white_list(struct bt_mesh_white_list *wl)
}

if (BTM_BleUpdateAdvWhitelist(wl->add_remove, wl->remote_bda,
wl->addr_type, (tBTM_ADD_WHITELIST_CBACK *)wl->update_wl_comp_cb) == false) {
wl->addr_type, (tBTM_UPDATE_WHITELIST_CBACK *)wl->update_wl_comp_cb) == false) {
return -EIO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ typedef enum {
typedef enum{
ESP_BLE_WHITELIST_REMOVE = 0X00, /*!< remove mac from whitelist */
ESP_BLE_WHITELIST_ADD = 0X01, /*!< add address to whitelist */
ESP_BLE_WHITELIST_CLEAR = 0x02, /*!< clear all device in whitelist */
} esp_ble_wl_operation_t;
#if (BLE_42_FEATURE_SUPPORT == TRUE)
typedef enum {
Expand Down
4 changes: 2 additions & 2 deletions components/bt/host/bluedroid/bta/dm/bta_dm_act.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,14 +845,14 @@ void bta_dm_ble_set_channels (tBTA_DM_MSG *p_data)
void bta_dm_update_white_list(tBTA_DM_MSG *p_data)
{
#if (BLE_INCLUDED == TRUE)
BTM_BleUpdateAdvWhitelist(p_data->white_list.add_remove, p_data->white_list.remote_addr, p_data->white_list.addr_type, p_data->white_list.add_wl_cb);
BTM_BleUpdateAdvWhitelist(p_data->white_list.add_remove, p_data->white_list.remote_addr, p_data->white_list.addr_type, p_data->white_list.update_wl_cb);
#endif ///BLE_INCLUDED == TRUE
}

void bta_dm_clear_white_list(tBTA_DM_MSG *p_data)
{
#if (BLE_INCLUDED == TRUE)
BTM_BleClearWhitelist();
BTM_BleClearWhitelist(p_data->white_list.update_wl_cb);
#endif
}

Expand Down
12 changes: 6 additions & 6 deletions components/bt/host/bluedroid/bta/dm/bta_dm_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,26 +324,26 @@ void BTA_DmBleSetChannels(const uint8_t *channels, tBTA_CMPL_CB *set_channels_c

}

void BTA_DmUpdateWhiteList(BOOLEAN add_remove, BD_ADDR remote_addr, tBLE_ADDR_TYPE addr_type, tBTA_ADD_WHITELIST_CBACK *add_wl_cb)
void BTA_DmUpdateWhiteList(BOOLEAN add_remove, BD_ADDR remote_addr, tBLE_ADDR_TYPE addr_type, tBTA_UPDATE_WHITELIST_CBACK *update_wl_cb)
{
tBTA_DM_API_UPDATE_WHITE_LIST *p_msg;
if ((p_msg = (tBTA_DM_API_UPDATE_WHITE_LIST *)osi_malloc(sizeof(tBTA_DM_API_UPDATE_WHITE_LIST))) != NULL) {
p_msg->hdr.event = BTA_DM_API_UPDATE_WHITE_LIST_EVT;
p_msg->add_remove = add_remove;
p_msg->addr_type = addr_type;
p_msg->add_wl_cb = add_wl_cb;
p_msg->update_wl_cb = update_wl_cb;
memcpy(p_msg->remote_addr, remote_addr, sizeof(BD_ADDR));

bta_sys_sendmsg(p_msg);
}
}

void BTA_DmClearWhiteList(void)
void BTA_DmClearWhiteList(tBTA_UPDATE_WHITELIST_CBACK *update_wl_cb)
{
tBTA_DM_API_ENABLE *p_msg;
if ((p_msg = (tBTA_DM_API_ENABLE *)osi_malloc(sizeof(tBTA_DM_API_ENABLE))) != NULL) {
tBTA_DM_API_UPDATE_WHITE_LIST *p_msg;
if ((p_msg = (tBTA_DM_API_UPDATE_WHITE_LIST *)osi_malloc(sizeof(tBTA_DM_API_UPDATE_WHITE_LIST))) != NULL) {
p_msg->hdr.event = BTA_DM_API_CLEAR_WHITE_LIST_EVT;
p_msg->p_sec_cback = NULL;
p_msg->update_wl_cb = update_wl_cb;

bta_sys_sendmsg(p_msg);
}
Expand Down
2 changes: 1 addition & 1 deletion components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ typedef struct {
BOOLEAN add_remove;
BD_ADDR remote_addr;
tBLE_ADDR_TYPE addr_type;
tBTA_ADD_WHITELIST_CBACK *add_wl_cb;
tBTA_UPDATE_WHITELIST_CBACK *update_wl_cb;
}tBTA_DM_API_UPDATE_WHITE_LIST;

typedef struct {
Expand Down
18 changes: 10 additions & 8 deletions components/bt/host/bluedroid/bta/gatt/bta_gattc_co.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ void bta_gattc_co_get_addr_list(BD_ADDR *addr_list)
void bta_gattc_co_cache_addr_save(BD_ADDR bd_addr, hash_key_t hash_key)
{
esp_err_t err_code;
UINT8 num = ++cache_env->num_addr;
UINT8 index = 0;
UINT8 new_index = cache_env->num_addr;
UINT8 *p_buf = osi_malloc(MAX_ADDR_LIST_CACHE_BUF);
// check the address list has the same hash key or not
if (bta_gattc_co_find_hash_in_cache(hash_key) != INVALID_ADDR_NUM) {
Expand All @@ -530,20 +530,22 @@ void bta_gattc_co_cache_addr_save(BD_ADDR bd_addr, hash_key_t hash_key)
memcpy(cache_env->cache_addr[index].hash_key, hash_key, sizeof(hash_key_t));
} else {
//if the bd_addr didn't in the address list, added the bd_addr to the last of the address list.
memcpy(cache_env->cache_addr[num - 1].hash_key, hash_key, sizeof(hash_key_t));
memcpy(cache_env->cache_addr[num - 1].addr, bd_addr, sizeof(BD_ADDR));
memcpy(cache_env->cache_addr[new_index].hash_key, hash_key, sizeof(hash_key_t));
memcpy(cache_env->cache_addr[new_index].addr, bd_addr, sizeof(BD_ADDR));
cache_env->num_addr++;
}

} else {
APPL_TRACE_DEBUG("%s(), num = %d", __func__, num);
memcpy(cache_env->cache_addr[num - 1].addr, bd_addr, sizeof(BD_ADDR));
memcpy(cache_env->cache_addr[num - 1].hash_key, hash_key, sizeof(hash_key_t));
APPL_TRACE_DEBUG("%s(), num = %d", __func__, new_index + 1);
memcpy(cache_env->cache_addr[new_index].addr, bd_addr, sizeof(BD_ADDR));
memcpy(cache_env->cache_addr[new_index].hash_key, hash_key, sizeof(hash_key_t));
cache_env->num_addr++;
}

nvs_handle_t *fp = &cache_env->addr_fp;
UINT16 length = num*(sizeof(BD_ADDR) + sizeof(hash_key_t));
UINT16 length = cache_env->num_addr * (sizeof(BD_ADDR) + sizeof(hash_key_t));

for (UINT8 i = 0; i < num; i++) {
for (UINT8 i = 0; i < cache_env->num_addr; i++) {
//copy the address to the buffer.
memcpy(p_buf + i*(sizeof(BD_ADDR) + sizeof(hash_key_t)), cache_env->cache_addr[i].addr, sizeof(BD_ADDR));
//copy the hash key to the buffer.
Expand Down
6 changes: 3 additions & 3 deletions components/bt/host/bluedroid/bta/include/bta/bta_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ typedef tBTM_START_ADV_CMPL_CBACK tBTA_START_ADV_CMPL_CBACK;

typedef tBTM_START_STOP_ADV_CMPL_CBACK tBTA_START_STOP_ADV_CMPL_CBACK;

typedef tBTM_ADD_WHITELIST_CBACK tBTA_ADD_WHITELIST_CBACK;
typedef tBTM_UPDATE_WHITELIST_CBACK tBTA_UPDATE_WHITELIST_CBACK;

typedef tBTM_SET_PKT_DATA_LENGTH_CBACK tBTA_SET_PKT_DATA_LENGTH_CBACK;

Expand Down Expand Up @@ -1756,9 +1756,9 @@ void BTA_DmSetQos(BD_ADDR bd_addr, UINT32 t_poll, tBTM_CMPL_CB *p_cb);
*******************************************************************************/
void BTA_DmBleSetChannels(const uint8_t *channels, tBTA_CMPL_CB *set_channels_cb);

extern void BTA_DmUpdateWhiteList(BOOLEAN add_remove, BD_ADDR remote_addr, tBLE_ADDR_TYPE addr_type, tBTA_ADD_WHITELIST_CBACK *add_wl_cb);
extern void BTA_DmUpdateWhiteList(BOOLEAN add_remove, BD_ADDR remote_addr, tBLE_ADDR_TYPE addr_type, tBTA_UPDATE_WHITELIST_CBACK *update_wl_cb);

extern void BTA_DmClearWhiteList(void);
extern void BTA_DmClearWhiteList(tBTA_UPDATE_WHITELIST_CBACK *update_wl_cb);

extern void BTA_DmBleReadAdvTxPower(tBTA_CMPL_CB *cmpl_cb);
#endif ///BLE_INCLUDED == TRUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ static void btc_gap_ble_set_channels_cmpl_callback(void *p_data)
}


static void btc_add_whitelist_complete_callback(UINT8 status, tBTM_WL_OPERATION wl_opration)
static void btc_update_whitelist_complete_callback(UINT8 status, tBTM_WL_OPERATION wl_opration)
{
esp_ble_gap_cb_param_t param;
bt_status_t ret;
Expand Down Expand Up @@ -1607,10 +1607,10 @@ void btc_gap_ble_call_handler(btc_msg_t *msg)
btc_ble_config_local_icon(arg->cfg_local_icon.icon);
break;
case BTC_GAP_BLE_ACT_UPDATE_WHITE_LIST:
BTA_DmUpdateWhiteList(arg->update_white_list.add_remove, arg->update_white_list.remote_bda, arg->update_white_list.wl_addr_type, btc_add_whitelist_complete_callback);
BTA_DmUpdateWhiteList(arg->update_white_list.add_remove, arg->update_white_list.remote_bda, arg->update_white_list.wl_addr_type, btc_update_whitelist_complete_callback);
break;
case BTC_GAP_BLE_ACT_CLEAR_WHITE_LIST:
BTA_DmClearWhiteList();
BTA_DmClearWhiteList(btc_update_whitelist_complete_callback);
break;
case BTC_GAP_BLE_ACT_READ_RSSI:
BTA_DmReadRSSI(arg->read_rssi.remote_addr, BTA_TRANSPORT_LE, btc_read_ble_rssi_cmpl_callback);
Expand Down
46 changes: 28 additions & 18 deletions components/bt/host/bluedroid/stack/btm/btm_ble_bgconn.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,12 @@ void btm_enq_wl_dev_operation(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_TYPE ad
** the white list.
**
*******************************************************************************/
BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_TYPE addr_type, tBTM_ADD_WHITELIST_CBACK *add_wl_cb)
BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_TYPE addr_type, tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb)
{
if(addr_type > BLE_ADDR_RANDOM) {
BTM_TRACE_ERROR("%s address type is error, unable to add device", __func__);
if (add_wl_cb){
add_wl_cb(HCI_ERR_ILLEGAL_PARAMETER_FMT,to_add);
if (update_wl_cb){
update_wl_cb(HCI_ERR_ILLEGAL_PARAMETER_FMT,to_add);
}
return FALSE;
}
Expand Down Expand Up @@ -313,8 +313,8 @@ BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_
// do nothing
} else {
BTC_TRACE_ERROR(" controller not support resolvable address");
if (add_wl_cb){
add_wl_cb(HCI_ERR_ILLEGAL_PARAMETER_FMT,to_add);
if (update_wl_cb){
update_wl_cb(HCI_ERR_ILLEGAL_PARAMETER_FMT,to_add);
}
return FALSE;
}
Expand All @@ -325,8 +325,8 @@ BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_

if (to_add && p_cb->white_list_avail_size == 0) {
BTM_TRACE_ERROR("%s Whitelist full, unable to add device", __func__);
if (add_wl_cb){
add_wl_cb(HCI_ERR_MEMORY_FULL,to_add);
if (update_wl_cb){
update_wl_cb(HCI_ERR_MEMORY_FULL,to_add);
}
return FALSE;
}
Expand All @@ -335,25 +335,25 @@ BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_
/* added the bd_addr to the connection hash map queue */
if(!background_connection_add((bt_bdaddr_t *)bd_addr)) {
/* if the bd_addr already exist in whitelist, just callback return TRUE */
if (add_wl_cb){
add_wl_cb(HCI_SUCCESS,to_add);
if (update_wl_cb){
update_wl_cb(HCI_SUCCESS,to_add);
}
return TRUE;
}
} else {
/* remove the bd_addr to the connection hash map queue */
if(!background_connection_remove((bt_bdaddr_t *)bd_addr)){
/* if the bd_addr don't exist in whitelist, just callback return TRUE */
if (add_wl_cb){
add_wl_cb(HCI_SUCCESS,to_add);
if (update_wl_cb){
update_wl_cb(HCI_SUCCESS,to_add);
}
return TRUE;
}
}

if (add_wl_cb){
if (update_wl_cb){
//save add whitelist complete callback
p_cb->add_wl_cb = add_wl_cb;
p_cb->update_wl_cb = update_wl_cb;
}
/* stop the auto connect */
btm_suspend_wl_activity(p_cb->wl_state);
Expand All @@ -371,11 +371,17 @@ BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_
** Description This function clears the white list.
**
*******************************************************************************/
void btm_ble_clear_white_list (void)
void btm_ble_clear_white_list (tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb)
{
tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;

BTM_TRACE_EVENT ("btm_ble_clear_white_list");
btsnd_hcic_ble_clear_white_list();
background_connections_clear();

if (update_wl_cb) {
p_cb->update_wl_cb = update_wl_cb;
}
}

/*******************************************************************************
Expand All @@ -399,6 +405,10 @@ void btm_ble_clear_white_list_complete(UINT8 *p_data, UINT16 evt_len)
} else {
BTM_TRACE_ERROR ("%s failed, status 0x%x\n", __func__, status);
}

if (p_cb->update_wl_cb) {
(*p_cb->update_wl_cb)(status, BTM_WHITELIST_CLEAR);
}
}

/*******************************************************************************
Expand Down Expand Up @@ -429,9 +439,9 @@ void btm_ble_add_2_white_list_complete(UINT8 status)
--btm_cb.ble_ctr_cb.white_list_avail_size;
}
// add whitelist complete callback
if (p_cb->add_wl_cb)
if (p_cb->update_wl_cb)
{
(*p_cb->add_wl_cb)(status, BTM_WHITELIST_ADD);
(*p_cb->update_wl_cb)(status, BTM_WHITELIST_ADD);
}

}
Expand All @@ -451,9 +461,9 @@ void btm_ble_remove_from_white_list_complete(UINT8 *p, UINT16 evt_len)
if (*p == HCI_SUCCESS) {
++btm_cb.ble_ctr_cb.white_list_avail_size;
}
if (p_cb->add_wl_cb)
if (p_cb->update_wl_cb)
{
(*p_cb->add_wl_cb)(*p, BTM_WHITELIST_REMOVE);
(*p_cb->update_wl_cb)(*p, BTM_WHITELIST_REMOVE);
}
}

Expand Down
10 changes: 5 additions & 5 deletions components/bt/host/bluedroid/stack/btm/btm_ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ void BTM_BleRegiseterConnParamCallback(tBTM_UPDATE_CONN_PARAM_CBACK *update_conn
** Returns void
**
*******************************************************************************/
BOOLEAN BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove, BD_ADDR remote_bda, tBLE_ADDR_TYPE addr_type, tBTM_ADD_WHITELIST_CBACK *add_wl_cb)
BOOLEAN BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove, BD_ADDR remote_bda, tBLE_ADDR_TYPE addr_type, tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb)
{
return btm_update_dev_to_white_list(add_remove, remote_bda, addr_type, add_wl_cb);
return btm_update_dev_to_white_list(add_remove, remote_bda, addr_type, update_wl_cb);
}

/*******************************************************************************
Expand All @@ -335,9 +335,9 @@ BOOLEAN BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove, BD_ADDR remote_bda, tBLE_A
** Returns void
**
*******************************************************************************/
void BTM_BleClearWhitelist(void)
void BTM_BleClearWhitelist(tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb)
{
btm_ble_clear_white_list();
btm_ble_clear_white_list(update_wl_cb);
}

/*******************************************************************************
Expand Down Expand Up @@ -1273,7 +1273,7 @@ BOOLEAN BTM_BleSetBgConnType(tBTM_BLE_CONN_TYPE bg_conn_type,
void BTM_BleClearBgConnDev(void)
{
btm_ble_start_auto_conn(FALSE);
btm_ble_clear_white_list();
btm_ble_clear_white_list(NULL);
gatt_reset_bgdev_list();
}

Expand Down
6 changes: 3 additions & 3 deletions components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ typedef struct {
tBTM_BLE_SEL_CBACK *p_select_cback;
/* white list information */
UINT8 white_list_avail_size;
tBTM_ADD_WHITELIST_CBACK *add_wl_cb;
tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb;
tBTM_BLE_WL_STATE wl_state;

fixed_queue_t *conn_pending_q;
Expand Down Expand Up @@ -442,10 +442,10 @@ void btm_ble_update_sec_key_size(BD_ADDR bd_addr, UINT8 enc_key_size);
UINT8 btm_ble_read_sec_key_size(BD_ADDR bd_addr);

/* white list function */
BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_TYPE addr_type, tBTM_ADD_WHITELIST_CBACK *add_wl_cb);
BOOLEAN btm_update_dev_to_white_list(BOOLEAN to_add, BD_ADDR bd_addr, tBLE_ADDR_TYPE addr_type, tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb);
void btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy);
void btm_update_adv_filter_policy(tBTM_BLE_AFP adv_policy);
void btm_ble_clear_white_list (void);
void btm_ble_clear_white_list (tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb);
void btm_read_white_list_size_complete(UINT8 *p, UINT16 evt_len);
void btm_ble_add_2_white_list_complete(UINT8 status);
void btm_ble_remove_from_white_list_complete(UINT8 *p, UINT16 evt_len);
Expand Down
3 changes: 2 additions & 1 deletion components/bt/host/bluedroid/stack/include/stack/btm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ typedef struct {
typedef enum{
BTM_WHITELIST_REMOVE = 0X00,
BTM_WHITELIST_ADD = 0X01,
BTM_WHITELIST_CLEAR = 0x02,
}tBTM_WL_OPERATION;


Expand Down Expand Up @@ -190,7 +191,7 @@ typedef void (tBTM_SET_PKT_DATA_LENGTH_CBACK) (UINT8 status, tBTM_LE_SET_PKT_DAT

typedef void (tBTM_SET_RAND_ADDR_CBACK) (UINT8 status);

typedef void (tBTM_ADD_WHITELIST_CBACK) (UINT8 status, tBTM_WL_OPERATION wl_opration);
typedef void (tBTM_UPDATE_WHITELIST_CBACK) (UINT8 status, tBTM_WL_OPERATION wl_opration);

typedef void (tBTM_SET_LOCAL_PRIVACY_CBACK) (UINT8 status);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2270,7 +2270,7 @@ void BTM_BleTurnOnPrivacyOnRemote(BD_ADDR bd_addr,
**
*******************************************************************************/
//extern
BOOLEAN BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove, BD_ADDR emote_bda, tBLE_ADDR_TYPE addr_type, tBTM_ADD_WHITELIST_CBACK *add_wl_cb);
BOOLEAN BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove, BD_ADDR emote_bda, tBLE_ADDR_TYPE addr_type, tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb);

/*******************************************************************************
**
Expand All @@ -2281,7 +2281,7 @@ BOOLEAN BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove, BD_ADDR emote_bda, tBLE_AD
** Returns void
**
*******************************************************************************/
void BTM_BleClearWhitelist(void);
void BTM_BleClearWhitelist(tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb);

/*******************************************************************************
**
Expand Down

0 comments on commit 1644050

Please sign in to comment.