mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-30 10:47:19 +02:00
fix(bt/bluedroid): ble fails to load bonded device info after reboot
1: add a quantity check in bt when writing paired device info into nvs. 2: delete the exceeded device info from NVS when get bonded list during bluedroid initialization. 3: unify the process of bt and ble to load bonded list during bluedroid initialization.
This commit is contained in:
@ -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
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -47,9 +47,7 @@ static void _btc_storage_save(void)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string_is_bdaddr(section) ||
|
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);
|
iter = btc_config_section_next(iter);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -64,16 +62,15 @@ static void _btc_storage_save(void)
|
|||||||
if (need_remove_iter) {
|
if (need_remove_iter) {
|
||||||
while(need_remove_iter != btc_config_section_end()) {
|
while(need_remove_iter != btc_config_section_end()) {
|
||||||
const char *need_remove_section = btc_config_section_name(need_remove_iter);
|
const char *need_remove_section = btc_config_section_name(need_remove_iter);
|
||||||
if (!string_is_bdaddr(need_remove_section) ||
|
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)) {
|
|
||||||
need_remove_iter = btc_config_section_next(need_remove_iter);
|
need_remove_iter = btc_config_section_next(need_remove_iter);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
need_remove_iter = btc_config_section_next(need_remove_iter);
|
need_remove_iter = btc_config_section_next(need_remove_iter);
|
||||||
//delete device info
|
//delete device info
|
||||||
string_to_bdaddr(need_remove_section, &bd_addr);
|
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
|
//delete config info
|
||||||
if(btc_config_remove_section(need_remove_section)) {
|
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);
|
BTIF_TRACE_WARNING("exceeded the maximum nubmer of bonded devices, delete the last device info : %s", need_remove_section);
|
||||||
@ -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;
|
*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;
|
uint32_t device_type;
|
||||||
int addr_type;
|
int addr_type;
|
||||||
@ -800,6 +797,8 @@ static bt_status_t _btc_storage_in_fetch_bonded_ble_device(const char *remote_bd
|
|||||||
|
|
||||||
if (key_found) {
|
if (key_found) {
|
||||||
return BT_STATUS_SUCCESS;
|
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;
|
return BT_STATUS_FAIL;
|
||||||
|
@ -737,11 +737,8 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg)
|
|||||||
case BTA_DM_ENABLE_EVT: {
|
case BTA_DM_ENABLE_EVT: {
|
||||||
btc_clear_services_mask();
|
btc_clear_services_mask();
|
||||||
#if (SMP_INCLUDED == TRUE)
|
#if (SMP_INCLUDED == TRUE)
|
||||||
|
//load the bonding device to the btm layer
|
||||||
btc_storage_load_bonded_devices();
|
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
|
|
||||||
#endif ///SMP_INCLUDED == TRUE
|
#endif ///SMP_INCLUDED == TRUE
|
||||||
|
|
||||||
/* Set initial device name, it can be overwritten later */
|
/* Set initial device name, it can be overwritten later */
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "btc/btc_storage.h"
|
#include "btc/btc_storage.h"
|
||||||
|
#include "btc/btc_ble_storage.h"
|
||||||
#include "btc/btc_util.h"
|
#include "btc/btc_util.h"
|
||||||
#include "osi/osi.h"
|
#include "osi/osi.h"
|
||||||
#include "osi/allocator.h"
|
#include "osi/allocator.h"
|
||||||
@ -35,8 +36,31 @@ bt_status_t btc_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
|
|||||||
BOOLEAN sc_support)
|
BOOLEAN sc_support)
|
||||||
{
|
{
|
||||||
bdstr_t bdstr;
|
bdstr_t bdstr;
|
||||||
|
bt_bdaddr_t bd_addr;
|
||||||
bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
|
bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
|
||||||
|
|
||||||
|
/* device not in bond list and exceed the maximum number of bonded devices, delete the first bonded device */
|
||||||
|
if (btc_storage_get_num_all_bond_devices() >= BTM_SEC_MAX_DEVICE_RECORDS && !btc_config_has_section(bdstr)) {
|
||||||
|
const btc_config_section_iter_t *iter = btc_config_section_begin();
|
||||||
|
const btc_config_section_iter_t *remove_iter = iter;
|
||||||
|
/* find the first device(the last node) */
|
||||||
|
while (iter != btc_config_section_end()) {
|
||||||
|
remove_iter = iter;
|
||||||
|
iter = btc_config_section_next(iter);
|
||||||
|
}
|
||||||
|
const char *remove_section = btc_config_section_name(remove_iter);
|
||||||
|
|
||||||
|
// delete device info
|
||||||
|
string_to_bdaddr(remove_section, &bd_addr);
|
||||||
|
BTA_DmRemoveDevice(bd_addr.address, BT_TRANSPORT_BR_EDR);
|
||||||
|
BTA_DmRemoveDevice(bd_addr.address, BT_TRANSPORT_LE);
|
||||||
|
|
||||||
|
// delete config info
|
||||||
|
if (btc_config_remove_section(remove_section)) {
|
||||||
|
BTC_TRACE_WARNING("exceeded the maximum nubmer of bonded devices, delete the first device info : %s\n", remove_section);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BTC_TRACE_DEBUG("add to storage: Remote device:%s\n", bdstr);
|
BTC_TRACE_DEBUG("add to storage: Remote device:%s\n", bdstr);
|
||||||
|
|
||||||
btc_config_lock();
|
btc_config_lock();
|
||||||
@ -52,6 +76,46 @@ bt_status_t btc_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
|
|||||||
return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
|
return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if (SMP_INCLUDED == TRUE)
|
||||||
|
static bt_status_t _btc_storage_in_fetch_bonded_bt_device(const char *remote_bd_addr, int add)
|
||||||
|
{
|
||||||
|
BOOLEAN bt_linkkey_file_found = FALSE;
|
||||||
|
UINT8 sc_support = 0;
|
||||||
|
|
||||||
|
BTC_TRACE_DEBUG("Remote device:%s\n", remote_bd_addr);
|
||||||
|
LINK_KEY link_key;
|
||||||
|
size_t size = sizeof(link_key);
|
||||||
|
if (btc_config_get_bin(remote_bd_addr, BTC_STORAGE_LINK_KEY_STR, link_key, &size)) {
|
||||||
|
int linkkey_type;
|
||||||
|
if (btc_config_get_int(remote_bd_addr, BTC_STORAGE_LINK_KEY_TYPE_STR, &linkkey_type)) {
|
||||||
|
bt_bdaddr_t bd_addr;
|
||||||
|
string_to_bdaddr(remote_bd_addr, &bd_addr);
|
||||||
|
if (add) {
|
||||||
|
DEV_CLASS dev_class = {0, 0, 0};
|
||||||
|
int cod;
|
||||||
|
int pin_length = 0;
|
||||||
|
if (btc_config_get_int(remote_bd_addr, BTC_STORAGE_DEV_CLASS_STR, &cod)) {
|
||||||
|
uint2devclass((UINT32)cod, dev_class);
|
||||||
|
}
|
||||||
|
btc_config_get_int(remote_bd_addr, BTC_STORAGE_PIN_LENGTH_STR, &pin_length);
|
||||||
|
size = sizeof(sc_support);
|
||||||
|
btc_config_get_bin(remote_bd_addr, BTC_STORAGE_SC_SUPPORT, &sc_support, &size);
|
||||||
|
|
||||||
|
BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0,
|
||||||
|
(UINT8)linkkey_type, 0, pin_length, (UINT8)sc_support);
|
||||||
|
}
|
||||||
|
bt_linkkey_file_found = TRUE;
|
||||||
|
} else {
|
||||||
|
BTC_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid\n", remote_bd_addr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!bt_linkkey_file_found) {
|
||||||
|
BTC_TRACE_DEBUG("Remote device:%s, no link key\n", remote_bd_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return BT_STATUS_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
**
|
||||||
** Function btc_in_fetch_bonded_devices
|
** Function btc_in_fetch_bonded_devices
|
||||||
@ -64,8 +128,9 @@ bt_status_t btc_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
static bt_status_t btc_in_fetch_bonded_devices(int add)
|
static bt_status_t btc_in_fetch_bonded_devices(int add)
|
||||||
{
|
{
|
||||||
BOOLEAN bt_linkkey_file_found = FALSE;
|
bt_status_t status = BT_STATUS_FAIL;
|
||||||
UINT8 sc_support = 0;
|
uint16_t dev_cnt = 0;
|
||||||
|
const btc_config_section_iter_t *remove_iter = NULL;
|
||||||
|
|
||||||
btc_config_lock();
|
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)) {
|
for (const btc_config_section_iter_t *iter = btc_config_section_begin(); iter != btc_config_section_end(); iter = btc_config_section_next(iter)) {
|
||||||
@ -73,45 +138,44 @@ static bt_status_t btc_in_fetch_bonded_devices(int add)
|
|||||||
if (!string_is_bdaddr(name)) {
|
if (!string_is_bdaddr(name)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
dev_cnt ++;
|
||||||
BTC_TRACE_DEBUG("Remote device:%s\n", name);
|
/* if the number of device stored in nvs not exceed to BTM_SEC_MAX_DEVICE_RECORDS, load it */
|
||||||
LINK_KEY link_key;
|
if (dev_cnt <= BTM_SEC_MAX_DEVICE_RECORDS) {
|
||||||
size_t size = sizeof(link_key);
|
if (btc_config_exist(name, BTC_STORAGE_LINK_KEY_TYPE_STR) && btc_config_exist(name, BTC_STORAGE_PIN_LENGTH_STR) &&
|
||||||
if (btc_config_get_bin(name, BTC_STORAGE_LINK_KEY_STR, link_key, &size)) {
|
btc_config_exist(name, BTC_STORAGE_SC_SUPPORT) && btc_config_exist(name, BTC_STORAGE_LINK_KEY_STR)) {
|
||||||
int linkkey_type;
|
/* load bt device */
|
||||||
if (btc_config_get_int(name, BTC_STORAGE_LINK_KEY_TYPE_STR, &linkkey_type)) {
|
status = _btc_storage_in_fetch_bonded_bt_device(name, add);
|
||||||
bt_bdaddr_t bd_addr;
|
|
||||||
string_to_bdaddr(name, &bd_addr);
|
|
||||||
if (add) {
|
|
||||||
DEV_CLASS dev_class = {0, 0, 0};
|
|
||||||
int cod;
|
|
||||||
int pin_length = 0;
|
|
||||||
if (btc_config_get_int(name, BTC_STORAGE_DEV_CLASS_STR, &cod)) {
|
|
||||||
uint2devclass((UINT32)cod, dev_class);
|
|
||||||
}
|
|
||||||
btc_config_get_int(name, BTC_STORAGE_PIN_LENGTH_STR, &pin_length);
|
|
||||||
size = sizeof(sc_support);
|
|
||||||
btc_config_get_bin(name, BTC_STORAGE_SC_SUPPORT, &sc_support, &size);
|
|
||||||
#if (SMP_INCLUDED == TRUE)
|
|
||||||
BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0,
|
|
||||||
(UINT8)linkkey_type, 0, pin_length, (UINT8)sc_support);
|
|
||||||
#endif ///SMP_INCLUDED == TRUE
|
|
||||||
}
|
|
||||||
bt_linkkey_file_found = TRUE;
|
|
||||||
} else {
|
} else {
|
||||||
BTC_TRACE_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid\n", name);
|
#if (BLE_INCLUDED == TRUE)
|
||||||
|
/* load ble device */
|
||||||
|
status = _btc_storage_in_fetch_bonded_ble_device(name, add);
|
||||||
|
#endif ///BLE_INCLUDED == TRUE
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
if (!bt_linkkey_file_found) {
|
/* delete the exceeded device info from nvs */
|
||||||
BTC_TRACE_DEBUG("Remote device:%s, no link key\n", name);
|
remove_iter = iter;
|
||||||
|
while (remove_iter != btc_config_section_end()) {
|
||||||
|
const char *remove_section = btc_config_section_name(remove_iter);
|
||||||
|
if (!string_is_bdaddr(remove_section)) {
|
||||||
|
remove_iter = btc_config_section_next(remove_iter);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
remove_iter = btc_config_section_next(remove_iter);
|
||||||
|
/* delete config info */
|
||||||
|
if (btc_config_remove_section(remove_section)) {
|
||||||
|
BTC_TRACE_WARNING("exceeded the maximum number of bonded devices, delete the exceed device info : %s", remove_section);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* write into nvs */
|
||||||
|
btc_config_flush();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
btc_config_unlock();
|
btc_config_unlock();
|
||||||
|
|
||||||
return BT_STATUS_SUCCESS;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
**
|
||||||
** Function btc_storage_load_bonded_devices
|
** Function btc_storage_load_bonded_devices
|
||||||
@ -131,6 +195,7 @@ bt_status_t btc_storage_load_bonded_devices(void)
|
|||||||
BTC_TRACE_DEBUG("Storage load rslt %d\n", status);
|
BTC_TRACE_DEBUG("Storage load rslt %d\n", status);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
#endif ///SMP_INCLUDED == TRUE
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
**
|
||||||
@ -490,3 +555,18 @@ bt_status_t btc_storage_remove_hidd(bt_bdaddr_t *remote_bd_addr)
|
|||||||
return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
|
return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
|
||||||
}
|
}
|
||||||
#endif //(defined BTC_HD_INCLUDED && BTC_HD_INCLUDED == TRUE)
|
#endif //(defined BTC_HD_INCLUDED && BTC_HD_INCLUDED == TRUE)
|
||||||
|
|
||||||
|
int btc_storage_get_num_all_bond_devices(void) {
|
||||||
|
int num_dev = 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)) {
|
||||||
|
num_dev++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
btc_config_unlock();
|
||||||
|
return num_dev;
|
||||||
|
}
|
||||||
|
@ -61,6 +61,8 @@ bt_status_t btc_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr);
|
|||||||
|
|
||||||
bool btc_storage_compare_address_key_value(bt_bdaddr_t *remote_bd_addr, uint8_t key_type, void *key_value, int key_length);
|
bool btc_storage_compare_address_key_value(bt_bdaddr_t *remote_bd_addr, uint8_t key_type, void *key_value, int key_length);
|
||||||
|
|
||||||
|
bt_status_t _btc_storage_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add);
|
||||||
|
|
||||||
bt_status_t btc_storage_add_ble_local_key(char *key, uint8_t key_type, uint8_t key_length);
|
bt_status_t btc_storage_add_ble_local_key(char *key, uint8_t key_type, uint8_t key_length);
|
||||||
|
|
||||||
bt_status_t btc_storage_remove_ble_local_keys(void);
|
bt_status_t btc_storage_remove_ble_local_keys(void);
|
||||||
|
@ -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
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -54,12 +54,14 @@ bt_status_t btc_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr);
|
|||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
**
|
||||||
** Function btc_storage_remove_bonded_device
|
** Function btc_storage_load_bonded_devices
|
||||||
**
|
**
|
||||||
** Description BTC storage API - Deletes the bonded device from NVRAM
|
** 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 the deletion was successful,
|
** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
|
||||||
** BT_STATUS_FAIL otherwise
|
|
||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
bt_status_t btc_storage_load_bonded_devices(void);
|
bt_status_t btc_storage_load_bonded_devices(void);
|
||||||
@ -167,5 +169,15 @@ bt_status_t btc_storage_remove_hidd(bt_bdaddr_t *remote_bd_addr);
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
** Function btc_storage_get_num_all_bond_devices
|
||||||
|
**
|
||||||
|
** Description BTC storage API - get all the num of the bonded device from NVRAM
|
||||||
|
**
|
||||||
|
** Returns the num of the bonded device
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
int btc_storage_get_num_all_bond_devices(void);
|
||||||
|
|
||||||
#endif /* BTC_STORAGE_H */
|
#endif /* BTC_STORAGE_H */
|
||||||
|
Reference in New Issue
Block a user