fix(ble/bluedroid): fixed BLE bond error after reboot

This commit is contained in:
zhiweijian 2023-10-10 15:47:26 +08:00 committed by BOT
parent 5c61c89308
commit e01f4c4d24
4 changed files with 182 additions and 23 deletions

View File

@ -198,6 +198,170 @@ bt_status_t btc_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr,
return ret;
}
static bt_status_t _btc_storage_remove_all_ble_keys(const char *name)
{
int ret = 0;
if (name == NULL) {
return BT_STATUS_FAIL;
}
if (btc_config_exist(name, BTC_BLE_STORAGE_ADDR_TYPE_STR)) {
ret |= btc_config_remove(name, BTC_BLE_STORAGE_ADDR_TYPE_STR);
}
if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_PENC_STR)) {
ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_PENC_STR);
}
if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_PID_STR)) {
ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_PID_STR);
}
if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR)) {
ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR);
}
if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_LENC_STR)) {
ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_LENC_STR);
}
if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR)) {
ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR);
}
if (btc_config_exist(name, BTC_BLE_STORAGE_LE_KEY_LID_STR)) {
ret |= btc_config_remove(name, BTC_BLE_STORAGE_LE_KEY_LID_STR);
}
return ret;
}
void btc_storage_remove_unused_sections(uint8_t *cur_addr, tBTM_LE_PID_KEYS *del_pid_key)
{
bt_bdaddr_t bd_addr;
uint32_t device_type = 0;
if (del_pid_key == NULL) {
return;
}
btc_config_lock();
const btc_config_section_iter_t *iter = btc_config_section_begin();
while (iter != btc_config_section_end()) {
//store the next iter, if remove section, then will not loss the point
const char *section = btc_config_section_name(iter);
if (string_is_bdaddr(section) &&
!btc_config_exist(section, BTC_BLE_STORAGE_DEV_TYPE_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_ADDR_TYPE_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_LINK_KEY_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PENC_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PID_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LENC_STR) &&
!btc_config_exist(section, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR)) {
iter = btc_config_section_next(iter);
btc_config_remove_section(section);
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)) {
iter = btc_config_section_next(iter);
continue;
}
string_to_bdaddr(section, &bd_addr);
char buffer[sizeof(tBTM_LE_KEY_VALUE)] = {0};
if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PID, buffer, sizeof(tBTM_LE_PID_KEYS)) == BT_STATUS_SUCCESS) {
tBTM_LE_PID_KEYS *pid_key = (tBTM_LE_PID_KEYS *) buffer;
iter = btc_config_section_next(iter);
if (memcmp(del_pid_key->static_addr, pid_key->static_addr, 6) == 0 && memcmp(cur_addr, bd_addr.address, 6) != 0 && del_pid_key->addr_type == pid_key->addr_type) {
if (device_type == BT_DEVICE_TYPE_DUMO) {
btc_config_set_int(section, BTC_BLE_STORAGE_DEV_TYPE_STR, BT_DEVICE_TYPE_BREDR);
_btc_storage_remove_all_ble_keys(section);
} else {
//delete unused sections
BTIF_TRACE_DEBUG("delete section %s\n", section);
btc_config_remove_section(section);
}
}
} else {
iter = btc_config_section_next(iter);
}
}
btc_config_unlock();
}
void btc_storage_delete_duplicate_ble_devices(void)
{
bt_bdaddr_t bd_addr;
char buffer[sizeof(tBTM_LE_KEY_VALUE)] = {0};
char temp_buffer[sizeof(tBTM_LE_KEY_VALUE)] = {0};
tBTM_LE_PID_KEYS *pid_key;
tBTM_LE_PID_KEYS *temp_pid_key;
uint32_t device_type = 0;
bt_bdaddr_t temp_bd_addr;
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;
}
string_to_bdaddr(name, &bd_addr);
if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PID, buffer, sizeof(tBTM_LE_PID_KEYS)) == BT_STATUS_SUCCESS)
{
pid_key = (tBTM_LE_PID_KEYS *) buffer;
const btc_config_section_iter_t *temp_iter = btc_config_section_next(iter);
while (temp_iter != NULL)
{
const char *temp_name = btc_config_section_name(temp_iter);
if (!string_is_bdaddr(temp_name) || !btc_config_get_int(temp_name, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type) ||
((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) {
temp_iter = btc_config_section_next(temp_iter);
continue;
}
string_to_bdaddr(temp_name, &temp_bd_addr);
if (_btc_storage_get_ble_bonding_key(&temp_bd_addr, BTM_LE_KEY_PID, temp_buffer, sizeof(tBTM_LE_PID_KEYS)) == BT_STATUS_SUCCESS)
{
temp_pid_key = (tBTM_LE_PID_KEYS *) temp_buffer;
if (memcmp(pid_key->static_addr, temp_pid_key->static_addr, 6) == 0 && pid_key->addr_type == temp_pid_key->addr_type) {
const char *temp_name = btc_config_section_name(temp_iter);
temp_iter = btc_config_section_next(temp_iter);
if (device_type == BT_DEVICE_TYPE_DUMO) {
btc_config_set_int(temp_name, BTC_BLE_STORAGE_DEV_TYPE_STR, BT_DEVICE_TYPE_BREDR);
_btc_storage_remove_all_ble_keys(temp_name);
} else {
BTC_TRACE_DEBUG("delete %s\n", temp_name);
btc_config_remove_section(temp_name);
}
} else {
temp_iter = btc_config_section_next(temp_iter);
}
} else {
temp_iter = btc_config_section_next(temp_iter);
}
}
}
}
btc_config_unlock();
}
/*******************************************************************************
**
** Function btc_storage_remove_ble_bonding_keys
@ -216,27 +380,8 @@ static bt_status_t _btc_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_a
BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr);
if (btc_config_exist(bdstr, BTC_BLE_STORAGE_ADDR_TYPE_STR)) {
ret |= btc_config_remove(bdstr, BTC_BLE_STORAGE_ADDR_TYPE_STR);
}
if (btc_config_exist(bdstr, BTC_BLE_STORAGE_LE_KEY_PENC_STR)) {
ret |= btc_config_remove(bdstr, BTC_BLE_STORAGE_LE_KEY_PENC_STR);
}
if (btc_config_exist(bdstr, BTC_BLE_STORAGE_LE_KEY_PID_STR)) {
ret |= btc_config_remove(bdstr, BTC_BLE_STORAGE_LE_KEY_PID_STR);
}
if (btc_config_exist(bdstr, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR)) {
ret |= btc_config_remove(bdstr, BTC_BLE_STORAGE_LE_KEY_PCSRK_STR);
}
if (btc_config_exist(bdstr, BTC_BLE_STORAGE_LE_KEY_LENC_STR)) {
ret |= btc_config_remove(bdstr, BTC_BLE_STORAGE_LE_KEY_LENC_STR);
}
if (btc_config_exist(bdstr, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR)) {
ret |= btc_config_remove(bdstr, BTC_BLE_STORAGE_LE_KEY_LCSRK_STR);
}
if (btc_config_exist(bdstr, BTC_BLE_STORAGE_LE_KEY_LID_STR)) {
ret |= btc_config_remove(bdstr, BTC_BLE_STORAGE_LE_KEY_LID_STR);
}
_btc_storage_remove_all_ble_keys(bdstr);
//here don't remove section, because config_save will check it
_btc_storage_save();
return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;

View File

@ -265,7 +265,12 @@ static void btc_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
return;
}
if (btc_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS) {
if (btc_dm_cb.pairing_cb.ble.is_pid_key_rcvd) {
// delete unused section in NVS
btc_storage_remove_unused_sections(p_auth_cmpl->bd_addr, &btc_dm_cb.pairing_cb.ble.pid_key);
}
if (btc_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS) {
btc_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type, true);
}
btc_storage_set_ble_dev_auth_mode(&bdaddr, p_auth_cmpl->auth_mode, true);
@ -749,6 +754,9 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
case BTA_DM_ENABLE_EVT: {
btc_clear_services_mask();
#if (SMP_INCLUDED == TRUE)
#if (BLE_INCLUDED == TRUE)
btc_storage_delete_duplicate_ble_devices();
#endif ///BLE_INCLUDED == TRUE
//load the bonding device to the btm layer
btc_storage_load_bonded_devices();
#endif ///SMP_INCLUDED == TRUE

View File

@ -16,6 +16,7 @@
#include "stack/bt_types.h"
#include "common/bt_target.h"
#include "esp_gap_ble_api.h"
#include "bta/bta_api.h"
#if (SMP_INCLUDED == TRUE)
#define BTC_LE_LOCAL_KEY_IR (1<<0)
@ -88,6 +89,11 @@ bt_status_t btc_storage_remove_ble_dev_type(bt_bdaddr_t *remote_bd_addr, bool fl
bt_status_t btc_storage_get_bonded_ble_devices_list(esp_ble_bond_dev_t *bond_dev, int dev_num);
int btc_storage_get_num_ble_bond_devices(void);
void btc_storage_delete_duplicate_ble_devices(void);
void btc_storage_remove_unused_sections(uint8_t *cur_addr, tBTM_LE_PID_KEYS *del_pid_key);
#endif ///SMP_INCLUDED == TRUE
#define BTC_BLE_STORAGE_GATT_CL_SUPP_FEAT_STR "GATT_CL_SUPP_FEAT"

View File

@ -592,7 +592,7 @@ void btm_consolidate_dev(tBTM_SEC_DEV_REC *p_target_rec)
p_target_rec->ble.ble_addr_type = p_dev_rec->ble.ble_addr_type;
p_target_rec->device_type |= p_dev_rec->device_type;
/* Remove the unused device from the list */
list_remove(btm_cb.p_sec_dev_rec_list, p_dev_rec);
list_remove(btm_cb.p_sec_dev_rec_list, p_dev_rec);
}
break;
}