Skip to content

Commit

Permalink
Merge branch 'bugfix/unable_to_load_bonded_ble_devices_from_nvs_v5.1'…
Browse files Browse the repository at this point in the history
… into 'release/v5.1'

fix(bt&ble): fix BLE unable to load bonded device info after reboot. (backport v5.1)

See merge request espressif/esp-idf!26465
  • Loading branch information
jack0c committed Nov 14, 2023
2 parents 822aa69 + f54e3eb commit 378bb1f
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 107 deletions.
26 changes: 26 additions & 0 deletions components/bt/common/osi/config.c
Expand Up @@ -254,6 +254,32 @@ bool config_remove_section(config_t *config, const char *section)
return list_remove(config->sections, sec);
}

bool config_update_newest_section(config_t *config, const char *section)
{
assert(config != NULL);
assert(section != NULL);

list_node_t *first_node = list_begin(config->sections);
if (first_node == NULL) {
return false;
}
section_t *first_sec = list_node(first_node);
if (strcmp(first_sec->name, section) == 0) {
return true;
}

for (const list_node_t *node = list_begin(config->sections); node != list_end(config->sections); node = list_next(node)) {
section_t *sec = list_node(node);
if (strcmp(sec->name, section) == 0) {
list_delete(config->sections, sec);
list_prepend(config->sections, sec);
return true;
}
}

return false;
}

bool config_remove_key(config_t *config, const char *section, const char *key)
{
assert(config != NULL);
Expand Down
5 changes: 5 additions & 0 deletions components/bt/common/osi/include/osi/config.h
Expand Up @@ -99,6 +99,11 @@ void config_set_string(config_t *config, const char *section, const char *key, c
// Neither |config| nor |section| may be NULL.
bool config_remove_section(config_t *config, const char *section);

// Updates |section| to be the first section in |config|. Return true if |section| is in
// |config| and updated successfully, false otherwise.
// Neither |config| nor |section| may be NULL.
bool config_update_newest_section(config_t *config, const char *section);

// Removes one specific |key| residing in |section| of the |config|. Returns true
// if the section and key were found and the key was removed, false otherwise.
// None of |config|, |section|, or |key| may be NULL.
Expand Down
66 changes: 9 additions & 57 deletions components/bt/host/bluedroid/btc/core/btc_ble_storage.c
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -22,7 +22,7 @@ static void _btc_storage_save(void)
{
uint16_t addr_section_count = 0;
bt_bdaddr_t bd_addr;
uint32_t device_type = 0;

const btc_config_section_iter_t *need_remove_iter = NULL;
const btc_config_section_iter_t *iter = btc_config_section_begin();

Expand All @@ -47,9 +47,7 @@ static void _btc_storage_save(void)
continue;
}

if (!string_is_bdaddr(section) ||
!btc_config_get_int(section, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) ||
((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) {
if (!string_is_bdaddr(section)) {
iter = btc_config_section_next(iter);
continue;
}
Expand All @@ -64,16 +62,15 @@ static void _btc_storage_save(void)
if (need_remove_iter) {
while(need_remove_iter != btc_config_section_end()) {
const char *need_remove_section = btc_config_section_name(need_remove_iter);
if (!string_is_bdaddr(need_remove_section) ||
!btc_config_get_int(need_remove_section, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) ||
((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) {
if (!string_is_bdaddr(need_remove_section)) {
need_remove_iter = btc_config_section_next(need_remove_iter);
continue;
}
need_remove_iter = btc_config_section_next(need_remove_iter);
//delete device info
string_to_bdaddr(need_remove_section, &bd_addr);
BTM_SecDeleteDevice(bd_addr.address, BT_TRANSPORT_LE);
BTA_DmRemoveDevice(bd_addr.address, BT_TRANSPORT_LE);
BTA_DmRemoveDevice(bd_addr.address, BT_TRANSPORT_BR_EDR);
//delete config info
if(btc_config_remove_section(need_remove_section)) {
BTIF_TRACE_WARNING("exceeded the maximum nubmer of bonded devices, delete the last device info : %s", need_remove_section);
Expand Down Expand Up @@ -758,7 +755,7 @@ static void _btc_read_le_key(const uint8_t key_type, const size_t key_len, bt_bd
*key_found = true;
}
}
static bt_status_t _btc_storage_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add)
bt_status_t _btc_storage_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add)
{
uint32_t device_type;
int addr_type;
Expand Down Expand Up @@ -800,58 +797,13 @@ static bt_status_t _btc_storage_in_fetch_bonded_ble_device(const char *remote_bd

if (key_found) {
return BT_STATUS_SUCCESS;
} else {
BTC_TRACE_DEBUG("Remote device:%s, no link key or ble key found", remote_bd_addr);
}

return BT_STATUS_FAIL;
}

static bt_status_t btc_storage_in_fetch_bonded_ble_devices(int add)
{
bt_status_t status = BT_STATUS_FAIL;
uint32_t device_type = 0;

btc_config_lock();
for (const btc_config_section_iter_t *iter = btc_config_section_begin(); iter != btc_config_section_end();
iter = btc_config_section_next(iter)) {
const char *name = btc_config_section_name(iter);

if (!string_is_bdaddr(name) ||
!btc_config_get_int(name, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) ||
((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) {
continue;
}
BTC_TRACE_DEBUG("%s, name = %s", __func__, name);
if (_btc_storage_in_fetch_bonded_ble_device(name, add) != BT_STATUS_SUCCESS) {
BTC_TRACE_DEBUG("Remote device:%s, no link key or ble key found", name);
} else {
status = BT_STATUS_SUCCESS;
}
}
btc_config_unlock();

return status;
}

/*******************************************************************************
**
** Function btc_storage_load_bonded_devices
**
** Description btc storage API - Loads all the bonded devices from NVRAM
** and adds to the BTA.
** Additionally, this API also invokes the adaper_properties_cb
** and remote_device_properties_cb for each of the bonded devices.
**
** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
**
*******************************************************************************/
bt_status_t btc_storage_load_bonded_ble_devices(void)
{
bt_status_t status;
status = btc_storage_in_fetch_bonded_ble_devices(1);
BTC_TRACE_DEBUG("Storage load rslt %d\n", status);
return status;
}

bt_status_t btc_storage_get_bonded_ble_devices_list(esp_ble_bond_dev_t *bond_dev, int dev_num)
{
bt_bdaddr_t bd_addr;
Expand Down
8 changes: 8 additions & 0 deletions components/bt/host/bluedroid/btc/core/btc_config.c
Expand Up @@ -299,6 +299,14 @@ bool btc_config_remove_section(const char *section)
return config_remove_section(config, section);
}

bool btc_config_update_newest_section(const char *section)
{
assert(config != NULL);
assert(section != NULL);

return config_update_newest_section(config, section);
}

void btc_config_flush(void)
{
assert(config != NULL);
Expand Down
17 changes: 13 additions & 4 deletions components/bt/host/bluedroid/btc/core/btc_dm.c
Expand Up @@ -684,6 +684,7 @@ static void btc_dm_acl_link_stat(tBTA_DM_ACL_LINK_STAT *p_acl_link_stat)
#if (BTC_GAP_BT_INCLUDED == TRUE)
esp_bt_gap_cb_param_t param;
esp_bt_gap_cb_event_t event = ESP_BT_GAP_EVT_MAX;
bt_bdaddr_t bt_addr;

switch (p_acl_link_stat->event) {
case BTA_ACL_LINK_STAT_CONN_CMPL: {
Expand All @@ -706,6 +707,17 @@ static void btc_dm_acl_link_stat(tBTA_DM_ACL_LINK_STAT *p_acl_link_stat)
}
}

if (p_acl_link_stat->event == BTA_ACL_LINK_STAT_CONN_CMPL &&
p_acl_link_stat->link_act.conn_cmpl.status == HCI_SUCCESS) {
memcpy(bt_addr.address, p_acl_link_stat->link_act.conn_cmpl.bd_addr, sizeof(bt_addr.address));
if (btc_storage_update_active_device(&bt_addr)) {
BTC_TRACE_EVENT("Device: %02x:%02x:%02x:%02x:%02x:%02x, is not in bond list",
bt_addr.address[0], bt_addr.address[1],
bt_addr.address[2], bt_addr.address[3],
bt_addr.address[4], bt_addr.address[5]);
}
}

esp_bt_gap_cb_t cb = (esp_bt_gap_cb_t)btc_profile_cb_get(BTC_PID_GAP_BT);
if (cb) {
cb(event, &param);
Expand Down Expand Up @@ -737,11 +749,8 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
case BTA_DM_ENABLE_EVT: {
btc_clear_services_mask();
#if (SMP_INCLUDED == TRUE)
btc_storage_load_bonded_devices();
#if (BLE_INCLUDED == TRUE)
//load the bonding device to the btm layer
btc_storage_load_bonded_ble_devices();
#endif ///BLE_INCLUDED == TRUE
btc_storage_load_bonded_devices();
#endif ///SMP_INCLUDED == TRUE

/* Set initial device name, it can be overwritten later */
Expand Down

0 comments on commit 378bb1f

Please sign in to comment.