From 4eda7b124d639c296a0da67a5d13962b1ca75507 Mon Sep 17 00:00:00 2001 From: Chen Jian Hua Date: Thu, 12 Dec 2024 12:33:44 +0800 Subject: [PATCH 1/2] feat(bt/bluedroid): Added config for saving BLE bonding keys to NVS (cherry picked from commit 6d1160b2e61a3e782699cb14fb0f70c05b121b30) Co-authored-by: chenjianhua --- components/bt/host/bluedroid/Kconfig.in | 7 +++++++ components/bt/host/bluedroid/btc/core/btc_dm.c | 13 +++++++++---- .../common/include/common/bluedroid_user_config.h | 6 ++++++ .../bluedroid/common/include/common/bt_target.h | 6 ++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/components/bt/host/bluedroid/Kconfig.in b/components/bt/host/bluedroid/Kconfig.in index dc235c2b99..dfe68eabb4 100644 --- a/components/bt/host/bluedroid/Kconfig.in +++ b/components/bt/host/bluedroid/Kconfig.in @@ -358,6 +358,13 @@ config BT_BLE_SMP_ID_RESET_ENABLE of a previously paired peer to be used to determine whether a device with which it previously shared an IRK is within range. +config BT_BLE_SMP_BOND_NVS_FLASH + bool "Save SMP bonding keys to nvs flash" + depends on BT_BLE_SMP_ENABLE + default y + help + This select can save SMP bonding keys to nvs flash + config BT_STACK_NO_LOG bool "Disable BT debug logs (minimize bin size)" depends on BT_BLUEDROID_ENABLED diff --git a/components/bt/host/bluedroid/btc/core/btc_dm.c b/components/bt/host/bluedroid/btc/core/btc_dm.c index 52dbd77c18..c50d888474 100644 --- a/components/bt/host/bluedroid/btc/core/btc_dm.c +++ b/components/bt/host/bluedroid/btc/core/btc_dm.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -182,11 +182,12 @@ static void btc_dm_remove_ble_bonding_keys(void) btc_storage_remove_ble_bonding_keys(&bd_addr); } +#if BLE_SMP_BOND_NVS_FLASH static void btc_dm_save_ble_bonding_keys(void) { if (!(btc_dm_cb.pairing_cb.ble.is_penc_key_rcvd || btc_dm_cb.pairing_cb.ble.is_pid_key_rcvd || btc_dm_cb.pairing_cb.ble.is_pcsrk_key_rcvd || - btc_dm_cb.pairing_cb.ble.is_lenc_key_rcvd || btc_dm_cb.pairing_cb.ble.is_lcsrk_key_rcvd || btc_dm_cb.pairing_cb.ble.is_lidk_key_rcvd)) { - return ; + btc_dm_cb.pairing_cb.ble.is_lenc_key_rcvd || btc_dm_cb.pairing_cb.ble.is_lcsrk_key_rcvd || btc_dm_cb.pairing_cb.ble.is_lidk_key_rcvd)) { + return; } bt_bdaddr_t bd_addr; @@ -244,13 +245,13 @@ static void btc_dm_save_ble_bonding_keys(void) btc_dm_cb.pairing_cb.ble.is_lidk_key_rcvd = false; } } +#endif static void btc_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) { /* Save link key, if not temporary */ BTC_TRACE_DEBUG("%s, status = %d", __func__, p_auth_cmpl->success); bt_status_t status = BT_STATUS_FAIL; - int addr_type; bt_bdaddr_t bdaddr; bdcpy(bdaddr.address, p_auth_cmpl->bd_addr); bdcpy(btc_dm_cb.pairing_cb.bd_addr, p_auth_cmpl->bd_addr); @@ -266,6 +267,9 @@ static void btc_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) return; } +#if BLE_SMP_BOND_NVS_FLASH + int addr_type; + 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); @@ -276,6 +280,7 @@ static void btc_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) } btc_storage_set_ble_dev_auth_mode(&bdaddr, p_auth_cmpl->auth_mode, true); btc_dm_save_ble_bonding_keys(); +#endif } else { /*Map the HCI fail reason to bt status */ switch (p_auth_cmpl->fail_reason) { diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index 482c1c9a45..9726981f8e 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -251,6 +251,12 @@ #define UC_BT_BLE_SMP_ID_RESET_ENABLE FALSE #endif +#ifdef CONFIG_BT_BLE_SMP_BOND_NVS_FLASH +#define UC_BT_BLE_SMP_BOND_NVS_FLASH CONFIG_BT_BLE_SMP_BOND_NVS_FLASH +#else +#define UC_BT_BLE_SMP_BOND_NVS_FLASH FALSE +#endif + //Device Name Maximum Length #ifdef CONFIG_BT_MAX_DEVICE_NAME_LEN #define UC_MAX_LOC_BD_NAME_LEN CONFIG_BT_MAX_DEVICE_NAME_LEN diff --git a/components/bt/host/bluedroid/common/include/common/bt_target.h b/components/bt/host/bluedroid/common/include/common/bt_target.h index 0e869dad45..6aee3bd0bf 100644 --- a/components/bt/host/bluedroid/common/include/common/bt_target.h +++ b/components/bt/host/bluedroid/common/include/common/bt_target.h @@ -316,6 +316,12 @@ #define BLE_SMP_ID_RESET_ENABLE FALSE #endif +#if (UC_BT_BLE_SMP_BOND_NVS_FLASH) +#define BLE_SMP_BOND_NVS_FLASH TRUE +#else +#define BLE_SMP_BOND_NVS_FLASH FALSE +#endif + #ifdef UC_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP #define BLE_ADV_REPORT_FLOW_CONTROL (UC_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP && BLE_INCLUDED) #endif /* UC_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP */ From d70e7d6534ca0e938c913d523b656398956d2eca Mon Sep 17 00:00:00 2001 From: Chen Jian Hua Date: Thu, 12 Dec 2024 12:33:47 +0800 Subject: [PATCH 2/2] fix(bt/bluedroid): Fixed failure to get host status when host is not enabled (cherry picked from commit 23455ea9c209c431a05bbaf8fed94335571cb713) Co-authored-by: chenjianhua --- components/bt/host/bluedroid/btc/core/btc_main.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/bt/host/bluedroid/btc/core/btc_main.c b/components/bt/host/bluedroid/btc/core/btc_main.c index de90708e6b..5fe01627fc 100644 --- a/components/bt/host/bluedroid/btc/core/btc_main.c +++ b/components/bt/host/bluedroid/btc/core/btc_main.c @@ -122,6 +122,11 @@ uint32_t btc_get_ble_status(void) { uint32_t status = BTC_BLE_STATUS_IDLE; + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + BTC_TRACE_ERROR("%s Bluedroid not enabled", __func__); + return status; + } + #if (BLE_INCLUDED == TRUE) // Number of active advertising extern uint8_t btm_ble_adv_active_count(void);