From 38102f414871318d108d863f03aff7fb3a293ae0 Mon Sep 17 00:00:00 2001 From: Tian Hao Date: Mon, 28 Aug 2017 17:14:49 +0800 Subject: [PATCH] component/bt : fix btc security storage bug 1. Remove the bond_device global variable. 2. Fix bond_device add cause memory exception 3. Modify the code location. 4. modify thread security 5. remove esp_ble_clear_bond_device_list() API 6. modify esp_ble_get_gond_device_list() to return list immediately, rather than wait event. 7. modify the gatt_security_server demo to show how to remove all the bonded devices. 8. fix some code bugs may cause something error. --- components/bt/bluedroid/api/esp_gap_ble_api.c | 35 +- .../bluedroid/api/include/esp_gap_ble_api.h | 40 +- .../bt/bluedroid/btc/core/btc_ble_storage.c | 1145 ++++++++++------- components/bt/bluedroid/btc/core/btc_config.c | 147 +-- components/bt/bluedroid/btc/core/btc_dm.c | 117 +- .../bt/bluedroid/btc/core/btc_storage.c | 39 +- .../bluedroid/btc/include/btc_ble_storage.h | 127 +- .../bt/bluedroid/btc/include/btc_config.h | 6 +- components/bt/bluedroid/btc/include/btc_dm.h | 46 + .../bt/bluedroid/btc/include/btc_storage.h | 6 + .../btc/profile/std/gap/btc_gap_ble.c | 104 +- .../btc/profile/std/include/btc_gap_ble.h | 2 - components/bt/bluedroid/btif/bta_dm_co.c | 2 +- .../main/example_ble_sec_gatts_demo.c | 55 +- 14 files changed, 978 insertions(+), 893 deletions(-) diff --git a/components/bt/bluedroid/api/esp_gap_ble_api.c b/components/bt/bluedroid/api/esp_gap_ble_api.c index 521f748b1..6b4ba6801 100644 --- a/components/bt/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/bluedroid/api/esp_gap_ble_api.c @@ -20,6 +20,7 @@ #include "bt_trace.h" #include "btc_manage.h" #include "btc_gap_ble.h" +#include "btc_ble_storage.h" esp_err_t esp_ble_gap_register_callback(esp_gap_ble_cb_t callback) @@ -364,26 +365,32 @@ esp_err_t esp_ble_remove_bond_device(esp_bd_addr_t bd_addr) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } -esp_err_t esp_ble_clear_bond_device_list(void) +int esp_ble_get_bond_device_num(void) { - btc_msg_t msg; - msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_GAP_BLE; - msg.act = BTC_GAP_BLE_CLEAR_BOND_DEV_EVT; + ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); - return (btc_transfer_context(&msg, NULL, 0, NULL) - == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); + return btc_storage_get_num_ble_bond_devices(); } -esp_err_t esp_ble_get_bond_device_list(void) +esp_err_t esp_ble_get_bond_device_list(int *dev_num, esp_ble_bond_dev_t *dev_list) { - btc_msg_t msg; - msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_GAP_BLE; - msg.act = BTC_GAP_BLE_GET_BOND_DEV_EVT; + int ret; + int dev_num_total; - return (btc_transfer_context(&msg, NULL, 0, NULL) - == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); + if (dev_num == NULL || dev_list == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); + + dev_num_total = btc_storage_get_num_ble_bond_devices(); + if (*dev_num > dev_num_total) { + *dev_num = dev_num_total; + } + + ret = btc_storage_get_bonded_ble_devices_list(dev_list, *dev_num); + + return (ret == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } esp_err_t esp_ble_gap_disconnect(esp_bd_addr_t remote_device) diff --git a/components/bt/bluedroid/api/include/esp_gap_ble_api.h b/components/bt/bluedroid/api/include/esp_gap_ble_api.h index a7cafcd35..942a5499f 100644 --- a/components/bt/bluedroid/api/include/esp_gap_ble_api.h +++ b/components/bt/bluedroid/api/include/esp_gap_ble_api.h @@ -95,8 +95,6 @@ typedef enum { ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT, /*!< When set pkt lenght complete, the event comes */ ESP_GAP_BLE_SET_LOCAL_PRIVACY_COMPLETE_EVT, /*!< When Enable/disable privacy on the local device complete, the event comes */ ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT, /*!< When remove the bond device complete, the event comes */ - ESP_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT, /*!< When clear the bond device clear complete, the event comes */ - ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT, /*!< When get the bond device list complete, the event comes */ ESP_GAP_BLE_EVT_MAX, } esp_gap_ble_cb_event_t; @@ -577,20 +575,6 @@ typedef union { esp_bt_status_t status; /*!< Indicate the remove bond device operation success status */ esp_bd_addr_t bd_addr; /*!< The device address which has been remove from the bond list */ }remove_bond_dev_cmpl; /*!< Event parameter of ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT */ - /** - * @brief ESP_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT - */ - struct ble_clear_bond_dev_cmpl_evt_param { - esp_bt_status_t status; /*!< Indicate the clear bond device operation success status */ - }clear_bond_dev_cmpl; /*!< Event parameter of ESP_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT */ - /** - * @brief ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT - */ - struct ble_get_bond_dev_cmpl_evt_param { - esp_bt_status_t status; /*!< Indicate the get bond device operation success status */ - uint8_t dev_num; /*!< Indicate the get number device in the bond list */ - esp_ble_bond_dev_t *bond_dev; /*!< the pointer to the bond device Structure */ - }get_bond_dev_cmpl; /*!< Event parameter of ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT */ } esp_ble_gap_cb_param_t; /** @@ -879,24 +863,30 @@ esp_err_t esp_ble_confirm_reply(esp_bd_addr_t bd_addr, bool accept); esp_err_t esp_ble_remove_bond_device(esp_bd_addr_t bd_addr); /** -* @brief Removes all of the device from the security database list of -* peer device. It manages unpairing event while connected. +* @brief Get the device number from the security database list of peer device. +* It will return the device bonded number immediately. * -* @return - ESP_OK : success -* - other : failed +* @return - >= 0 : bonded devices number. +* - < 0 : failed * */ -esp_err_t esp_ble_clear_bond_device_list(void); +int esp_ble_get_bond_device_num(void); + /** * @brief Get the device from the security database list of peer device. -* It will return the device bonded information from the ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT event. +* It will return the device bonded information immediately. +* @param[inout] dev_num: Indicate the dev_list array(buffer) size as input. +* If dev_num is large enough, it means the actual number as output. +* Suggest that dev_num value equal to esp_ble_get_bond_device_num(). * -* @return - ESP_OK : success -* - other : failed +* @param[out] dev_list: an array(buffer) of `esp_ble_bond_dev_t` type. Use for storing the bonded devices address. +* The dev_list should be allocated by who call this API. +* @return - ESP_OK : success +* - other : failed * */ -esp_err_t esp_ble_get_bond_device_list(void); +esp_err_t esp_ble_get_bond_device_list(int *dev_num, esp_ble_bond_dev_t *dev_list); /** * @brief This function is to disconnect the physical connection of the peer device diff --git a/components/bt/bluedroid/btc/core/btc_ble_storage.c b/components/bt/bluedroid/btc/core/btc_ble_storage.c index c10aa1e4e..538e97835 100644 --- a/components/bt/bluedroid/btc/core/btc_ble_storage.c +++ b/components/bt/bluedroid/btc/core/btc_ble_storage.c @@ -23,159 +23,587 @@ #if (SMP_INCLUDED == TRUE) -btc_dm_pairing_cb_t pairing_cb; -btc_dm_local_key_cb_t ble_local_key_cb; -btc_bonded_devices_t bonded_devices; +static void _btc_storage_save(void) +{ + 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)) { + iter = btc_config_section_next(iter); + continue; + } + + if (!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; + } + + iter = btc_config_section_next(iter); + } + + btc_config_flush(); +} + +void btc_storage_save(void) +{ + btc_config_lock(); + _btc_storage_save(); + btc_config_unlock(); +} + +static bt_status_t _btc_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, + char *key, + uint8_t key_type, + uint8_t key_length) +{ + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + const char* name; + + switch (key_type) { + case BTM_LE_KEY_PENC: + name = BTC_BLE_STORAGE_LE_KEY_PENC_STR; + break; + case BTM_LE_KEY_PID: + name = BTC_BLE_STORAGE_LE_KEY_PID_STR; + break; + case BTM_LE_KEY_PCSRK: + name = BTC_BLE_STORAGE_LE_KEY_PCSRK_STR; + break; + case BTM_LE_KEY_LENC: + name = BTC_BLE_STORAGE_LE_KEY_LENC_STR; + break; + case BTM_LE_KEY_LCSRK: + name = BTC_BLE_STORAGE_LE_KEY_LCSRK_STR; + break; + case BTM_LE_KEY_LID: + name = BTC_BLE_STORAGE_LE_KEY_LID_STR; + break; + default: + return BT_STATUS_FAIL; + } + + int ret = btc_config_set_bin(bdstr, name, (const uint8_t *)key, key_length); + _btc_storage_save(); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; +} + +bt_status_t btc_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, + char *key, + uint8_t key_type, + uint8_t key_length) +{ + bt_status_t ret; + + btc_config_lock(); + ret = _btc_storage_add_ble_bonding_key(remote_bd_addr, key, key_type, key_length); + btc_config_unlock(); + + return ret; +} /******************************************************************************* ** -** Function btc_storage_load_bonded_devices +** Function btc_storage_get_ble_bonding_key ** -** 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. +** Description ** -** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise +** Returns BT_STATUS_SUCCESS if the fetch was successful, +** BT_STATUS_FAIL otherwise ** *******************************************************************************/ -bt_status_t btc_storage_load_bonded_ble_devices(void) +static bt_status_t _btc_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, + uint8_t key_type, + char *key_value, + int key_length) { - bt_status_t status; - status = btc_in_fetch_bonded_ble_devices(1); - LOG_DEBUG("Storage load rslt %d\n", status); - return status; + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + const char* name; + switch (key_type) { + case BTM_LE_KEY_PENC: + name = BTC_BLE_STORAGE_LE_KEY_PENC_STR; + break; + case BTM_LE_KEY_PID: + name = BTC_BLE_STORAGE_LE_KEY_PID_STR; + break; + case BTM_LE_KEY_PCSRK: + name = BTC_BLE_STORAGE_LE_KEY_PCSRK_STR; + break; + case BTM_LE_KEY_LENC: + name = BTC_BLE_STORAGE_LE_KEY_LENC_STR; + break; + case BTM_LE_KEY_LCSRK: + name = BTC_BLE_STORAGE_LE_KEY_LCSRK_STR; + break; + case BTM_LE_KEY_LID: + name = BTC_BLE_STORAGE_LE_KEY_LID_STR; + default: + return BT_STATUS_FAIL; + } + size_t length = key_length; + int ret = btc_config_get_bin(bdstr, name, (uint8_t *)key_value, &length); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + } -bt_status_t btc_in_fetch_bonded_ble_devices(int add) +bt_status_t btc_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, + uint8_t key_type, + char *key_value, + int key_length) { - bt_status_t status = BT_STATUS_FAIL; - int device_type = 0; - 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_LE_DEV_TYPE, &device_type) || - ((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) { - continue; - } - LOG_DEBUG("%s, name = %s", __func__, name); - if (btc_in_fetch_bonded_ble_device(name, add, &bonded_devices) != BT_STATUS_SUCCESS) { - LOG_DEBUG("Remote device:%s, no link key or ble key found", name); - } else { - status = BT_STATUS_SUCCESS; - } + bt_status_t ret; + + btc_config_lock(); + ret = _btc_storage_get_ble_bonding_key(remote_bd_addr, key_type, key_value, key_length); + btc_config_unlock(); + + return ret; +} + +/******************************************************************************* +** +** Function btc_storage_remove_ble_bonding_keys +** +** Description btc storage API - Deletes the bonded device from NVRAM +** +** Returns BT_STATUS_SUCCESS if the deletion was successful, +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ +static bt_status_t _btc_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr) +{ + int ret = 1; + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + + 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); + } + //here don't remove section, because config_save will check it + _btc_storage_save(); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; +} + +bt_status_t btc_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr) +{ + bt_status_t ret; + + btc_config_lock(); + ret = _btc_storage_remove_ble_bonding_keys(remote_bd_addr); + btc_config_unlock(); + + return ret; +} + +/******************************************************************************* +** +** Function btc_storage_add_ble_local_key +** +** Description BTIF storage API - Adds the ble key to NVRAM +** +** Returns BT_STATUS_SUCCESS if the store was successful, +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ +static bt_status_t _btc_storage_add_ble_local_key(char *key, + uint8_t key_type, + uint8_t key_length) +{ + const char* name; + switch (key_type) { + case BTC_LE_LOCAL_KEY_IR: + name = BTC_BLE_STORAGE_LE_LOCAL_KEY_IR_STR; + break; + case BTC_LE_LOCAL_KEY_IRK: + name = BTC_BLE_STORAGE_LE_LOCAL_KEY_IRK_STR; + break; + case BTC_LE_LOCAL_KEY_DHK: + name = BTC_BLE_STORAGE_LE_LOCAL_KEY_DHK_STR; + break; + case BTC_LE_LOCAL_KEY_ER: + name = BTC_BLE_STORAGE_LE_LOCAL_KEY_ER_STR; + break; + default: + return BT_STATUS_FAIL; } - return status; + int ret = btc_config_set_bin(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, name, (const uint8_t *)key, key_length); + _btc_storage_save(); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } -bt_status_t btc_get_bonded_ble_devices_list(esp_ble_bond_dev_t *bond_dev) +bt_status_t btc_storage_add_ble_local_key(char *key, + uint8_t key_type, + uint8_t key_length) { - bt_bdaddr_t bd_addr; - int device_type = 0; - char buffer[sizeof(tBTM_LE_KEY_VALUE)] = {0}; - 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_LE_DEV_TYPE, &device_type) || - ((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) { - continue; - } + bt_status_t ret; - string_to_bdaddr(name, &bd_addr); - memcpy(bond_dev->bd_addr, bd_addr.address, sizeof(bt_bdaddr_t)); - //resolve the peer device long term key - if (btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PENC, buffer, sizeof(tBTM_LE_PENC_KEYS)) - == BT_STATUS_SUCCESS) { - bond_dev->bond_key.key_mask |= ESP_BLE_ENC_KEY_MASK; - memcpy(&bond_dev->bond_key.penc_key, buffer, sizeof(tBTM_LE_PENC_KEYS)); - } - //resolve the peer device csrk - if (btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PCSRK, buffer, sizeof(tBTM_LE_PCSRK_KEYS)) - == BT_STATUS_SUCCESS) { - bond_dev->bond_key.key_mask |= ESP_BLE_CSR_KEY_MASK; - memcpy(&bond_dev->bond_key.pcsrk_key, buffer, sizeof(tBTM_LE_PCSRK_KEYS)); - } - //resolve the peer device irk - if (btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PID, buffer, sizeof(tBTM_LE_PID_KEYS)) - == BT_STATUS_SUCCESS) { - bond_dev->bond_key.key_mask |= ESP_BLE_ID_KEY_MASK; - memcpy(&bond_dev->bond_key.pid_key, buffer, sizeof(tBTM_LE_PID_KEYS)); - } - //serch for the next bond device - bond_dev++; + btc_config_lock(); + ret = _btc_storage_add_ble_local_key(key, key_type, key_length); + btc_config_unlock(); + + return ret; +} + +/******************************************************************************* +** +** Function btc_storage_get_ble_local_key +** +** Description +** +** Returns BT_STATUS_SUCCESS if the fetch was successful, +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ +static bt_status_t _btc_storage_get_ble_local_key(uint8_t key_type, + char *key_value, + int key_length) +{ + const char* name; + switch (key_type) { + case BTC_LE_LOCAL_KEY_IR: + name = BTC_BLE_STORAGE_LE_LOCAL_KEY_IR_STR; + break; + case BTC_LE_LOCAL_KEY_IRK: + name = BTC_BLE_STORAGE_LE_LOCAL_KEY_IRK_STR; + break; + case BTC_LE_LOCAL_KEY_DHK: + name = BTC_BLE_STORAGE_LE_LOCAL_KEY_DHK_STR; + break; + case BTC_LE_LOCAL_KEY_ER: + name = BTC_BLE_STORAGE_LE_LOCAL_KEY_ER_STR; + break; + default: + return BT_STATUS_FAIL; + } + size_t length = key_length; + + int ret = btc_config_get_bin(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, name, (uint8_t *)key_value, &length); + + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; +} + +bt_status_t btc_storage_get_ble_local_key(uint8_t key_type, + char *key_value, + int key_length) +{ + bt_status_t ret; + + btc_config_lock(); + ret = _btc_storage_get_ble_local_key(key_type, key_value, key_length); + btc_config_unlock(); + + return ret; +} + +/******************************************************************************* +** +** Function btc_storage_remove_ble_local_keys +** +** Description BTC storage API - Deletes the bonded device from NVRAM +** +** Returns BT_STATUS_SUCCESS if the deletion was successful, +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ +static bt_status_t _btc_storage_remove_ble_local_keys(void) +{ + int ret = 1; + + if (btc_config_exist(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_IR_STR)) { + ret &= btc_config_remove(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_IR_STR); + } + if (btc_config_exist(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_IRK_STR)) { + ret &= btc_config_remove(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_IRK_STR); + } + if (btc_config_exist(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_DHK_STR)) { + ret &= btc_config_remove(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_DHK_STR); + } + if (btc_config_exist(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_ER_STR)) { + ret &= btc_config_remove(BTC_BLE_STORAGE_LOCAL_ADAPTER_STR, BTC_BLE_STORAGE_LE_LOCAL_KEY_ER_STR); + } + _btc_storage_save(); + + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; +} + +bt_status_t btc_storage_remove_ble_local_keys(void) +{ + bt_status_t ret; + + btc_config_lock(); + ret = _btc_storage_remove_ble_local_keys(); + btc_config_unlock(); + + return ret; +} + +bool _btc_storage_compare_address_key_value(bt_bdaddr_t *remote_bd_addr, + uint8_t key_type, void *key_value, int key_length) +{ + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + char *key_type_str; + switch (key_type) { + case BTM_LE_KEY_PENC: + key_type_str = BTC_BLE_STORAGE_LE_KEY_PENC_STR; + break; + case BTM_LE_KEY_PID: + key_type_str = BTC_BLE_STORAGE_LE_KEY_PID_STR; + break; + case BTM_LE_KEY_PCSRK: + key_type_str = BTC_BLE_STORAGE_LE_KEY_PCSRK_STR; + break; + case BTM_LE_KEY_LENC: + key_type_str = BTC_BLE_STORAGE_LE_KEY_LENC_STR; + break; + case BTM_LE_KEY_LCSRK: + key_type_str = BTC_BLE_STORAGE_LE_KEY_LCSRK_STR; + break; + case BTM_LE_KEY_LID: + key_type_str = BTC_BLE_STORAGE_LE_KEY_LID_STR; + default: + return false; + } + + return btc_compare_address_key_value(bdstr, key_type_str, key_value, 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) +{ + bool ret; + + btc_config_lock(); + ret = _btc_storage_compare_address_key_value(remote_bd_addr, key_type, key_value, key_length); + btc_config_unlock(); + + return ret; +} + +static bt_status_t _btc_storage_set_ble_dev_type(bt_bdaddr_t *bd_addr, bool flush) +{ + bool ret = 1; + bdstr_t bdstr; + uint32_t dev_type = 0; + + bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)); + + btc_config_get_int(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&dev_type); + ret = btc_config_set_int(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR, BT_DEVICE_TYPE_BLE|dev_type); + if (ret == false) { + return BT_STATUS_FAIL; + } + + if (flush) { + _btc_storage_save(); } return BT_STATUS_SUCCESS; } -void btc_dm_remove_ble_bonding_keys(void) +bt_status_t btc_storage_set_ble_dev_type(bt_bdaddr_t *bd_addr, bool flush) { - bt_bdaddr_t bd_addr; - LOG_DEBUG("%s\n",__func__); + bt_status_t ret; - bdcpy(bd_addr.address, pairing_cb.bd_addr); - btc_storage_remove_ble_bonding_keys(&bd_addr); + btc_config_lock(); + ret = _btc_storage_set_ble_dev_type(bd_addr, flush); + btc_config_unlock(); + + return ret; } -void btc_save_ble_bonding_keys(void) +static bool _btc_storage_get_ble_dev_type(bt_bdaddr_t *bd_addr) { - bt_bdaddr_t bd_addr; - - bdcpy(bd_addr.address, pairing_cb.bd_addr); + bool ret = 1; bdstr_t bdstr; - bdaddr_to_string(&bd_addr, bdstr, sizeof(bdstr)); - btc_config_set_int(bdstr, BTC_LE_DEV_TYPE, BT_DEVICE_TYPE_BLE); - LOG_DEBUG("%s, penc = %d, pid = %d", __func__, pairing_cb.ble.is_penc_key_rcvd, pairing_cb.ble.is_pid_key_rcvd); - if (pairing_cb.ble.is_penc_key_rcvd) { - btc_storage_add_ble_bonding_key(&bd_addr, - (char *) &pairing_cb.ble.penc_key, - BTM_LE_KEY_PENC, - sizeof(tBTM_LE_PENC_KEYS)); + uint32_t dev_type = 0; + + bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)); + + BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr); + + ret = btc_config_get_int(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&dev_type); + if (ret == false) { + return false; } - if (pairing_cb.ble.is_pid_key_rcvd) { - btc_storage_add_ble_bonding_key(&bd_addr, - (char *) &pairing_cb.ble.pid_key, - BTM_LE_KEY_PID, - sizeof(tBTM_LE_PID_KEYS)); - } - - - if (pairing_cb.ble.is_pcsrk_key_rcvd) { - btc_storage_add_ble_bonding_key(&bd_addr, - (char *) &pairing_cb.ble.pcsrk_key, - BTM_LE_KEY_PCSRK, - sizeof(tBTM_LE_PCSRK_KEYS)); - } - - - if (pairing_cb.ble.is_lenc_key_rcvd) { - btc_storage_add_ble_bonding_key(&bd_addr, - (char *) &pairing_cb.ble.lenc_key, - BTM_LE_KEY_LENC, - sizeof(tBTM_LE_LENC_KEYS)); - } - - if (pairing_cb.ble.is_lcsrk_key_rcvd) { - btc_storage_add_ble_bonding_key(&bd_addr, - (char *) &pairing_cb.ble.lcsrk_key, - BTM_LE_KEY_LCSRK, - sizeof(tBTM_LE_LCSRK_KEYS)); - } - - if (pairing_cb.ble.is_lidk_key_rcvd) { - btc_storage_add_ble_bonding_key(&bd_addr, - NULL, - BTM_LE_KEY_LID, - 0); - } + return (dev_type & BT_DEVICE_TYPE_BLE); } -static void btc_read_le_key(const uint8_t key_type, const size_t key_len, bt_bdaddr_t bd_addr, +bool btc_storage_get_ble_dev_type(bt_bdaddr_t *bd_addr) +{ + bt_status_t ret; + + btc_config_lock(); + ret = _btc_storage_get_ble_dev_type(bd_addr); + btc_config_unlock(); + + return ret; +} + + +static bt_status_t _btc_storage_remove_ble_dev_type(bt_bdaddr_t *remote_bd_addr, bool flush) +{ + bool ret = true; + bdstr_t bdstr; + uint32_t dev_type = 0; + + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + + BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr); + + ret = btc_config_get_int(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&dev_type); + if (ret == false) { + //cannot find the key, just return SUCCESS, indicate already removed + return BT_STATUS_SUCCESS; + } + + if (dev_type == BT_DEVICE_TYPE_DUMO) { + ret = btc_config_set_int(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR, BT_DEVICE_TYPE_BREDR); + } else if (dev_type == BT_DEVICE_TYPE_BLE) { + ret = btc_config_remove(bdstr, BTC_BLE_STORAGE_DEV_TYPE_STR); + } + + if (ret == false) { + return BT_STATUS_FAIL; + } + + if (flush) { + _btc_storage_save(); + } + + return BT_STATUS_SUCCESS; +} + +bt_status_t btc_storage_remove_ble_dev_type(bt_bdaddr_t *remote_bd_addr, bool flush) +{ + bt_status_t ret; + + btc_config_lock(); + ret = _btc_storage_remove_ble_dev_type(remote_bd_addr, flush); + btc_config_unlock(); + + return ret; +} + +static bt_status_t _btc_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr, uint8_t addr_type, bool flush) +{ + int ret; + bdstr_t bdstr; + + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr_t)); + ret = btc_config_set_int(bdstr, BTC_BLE_STORAGE_ADDR_TYPE_STR, (int)addr_type); + if (ret == false) { + return BT_STATUS_FAIL; + } + + if (flush) { + _btc_storage_save(); + } + + return BT_STATUS_SUCCESS; +} + +bt_status_t btc_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr, uint8_t addr_type, bool flush) +{ + bt_status_t ret; + + btc_config_lock(); + ret = _btc_storage_set_remote_addr_type(remote_bd_addr, addr_type, flush); + btc_config_unlock(); + + return ret; +} + +static bt_status_t _btc_storage_remove_remote_addr_type(bt_bdaddr_t *remote_bd_addr, bool flush) +{ + bool ret = true; + bdstr_t bdstr; + uint32_t dev_type = 0; + + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + + ret = btc_config_get_int(bdstr, BTC_BLE_STORAGE_ADDR_TYPE_STR, (int *)&dev_type); + if (ret == false) { + //cannot find the key, just return SUCCESS, indicate already removed + return BT_STATUS_SUCCESS; + } + + ret = btc_config_remove(bdstr, BTC_BLE_STORAGE_ADDR_TYPE_STR); + if (ret == false) { + return BT_STATUS_FAIL; + } + + if (flush) { + _btc_storage_save(); + } + + return BT_STATUS_SUCCESS; +} + +bt_status_t btc_storage_remove_remote_addr_type(bt_bdaddr_t *remote_bd_addr, bool flush) +{ + bt_status_t ret; + + btc_config_lock(); + ret = _btc_storage_remove_remote_addr_type(remote_bd_addr, flush); + btc_config_unlock(); + + return ret; +} + +static bt_status_t _btc_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr, + int*addr_type) +{ + bdstr_t bdstr; + bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + int ret = btc_config_get_int(bdstr, BTC_BLE_STORAGE_ADDR_TYPE_STR, addr_type); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; +} + +bt_status_t btc_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr, + int*addr_type) +{ + bt_status_t ret; + + btc_config_lock(); + ret = _btc_storage_get_remote_addr_type(remote_bd_addr, addr_type); + btc_config_unlock(); + + return ret; +} + +static void _btc_read_le_key(const uint8_t key_type, const size_t key_len, bt_bdaddr_t bd_addr, const uint8_t addr_type, const bool add_key, bool *device_added, bool *key_found) { assert(device_added); @@ -183,7 +611,10 @@ static void btc_read_le_key(const uint8_t key_type, const size_t key_len, bt_bda char buffer[100]; memset(buffer, 0, sizeof(buffer)); - if (btc_storage_get_ble_bonding_key(&bd_addr, key_type, buffer, key_len) == BT_STATUS_SUCCESS) { + + bt_status_t ret = _btc_storage_get_ble_bonding_key(&bd_addr, key_type, buffer, key_len); + + if (ret == BT_STATUS_SUCCESS) { if (add_key) { BD_ADDR bta_bd_addr; bdcpy(bta_bd_addr, bd_addr.address); @@ -202,310 +633,16 @@ static void btc_read_le_key(const uint8_t key_type, const size_t key_len, bt_bda *key_found = true; } } - -bt_status_t btc_storage_add_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, - char *key, - uint8_t key_type, - uint8_t key_length) +static bt_status_t _btc_storage_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add) { - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - const char* name; - switch (key_type) { - case BTM_LE_KEY_PENC: - name = "LE_KEY_PENC"; - break; - case BTM_LE_KEY_PID: - name = "LE_KEY_PID"; - break; - case BTM_LE_KEY_PCSRK: - name = "LE_KEY_PCSRK"; - break; - case BTM_LE_KEY_LENC: - name = "LE_KEY_LENC"; - break; - case BTM_LE_KEY_LCSRK: - name = "LE_KEY_LCSRK"; - break; - case BTM_LE_KEY_LID: - name = "LE_KEY_LID"; - break; - default: - return BT_STATUS_FAIL; - } - - int ret = btc_config_set_bin(bdstr, name, (const uint8_t *)key, key_length); - btc_config_save(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; -} - - -/******************************************************************************* -** -** Function btc_storage_get_ble_bonding_key -** -** Description -** -** Returns BT_STATUS_SUCCESS if the fetch was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btc_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, - uint8_t key_type, - char *key_value, - int key_length) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - const char* name; - switch (key_type) { - case BTM_LE_KEY_PENC: - name = "LE_KEY_PENC"; - break; - case BTM_LE_KEY_PID: - name = "LE_KEY_PID"; - break; - case BTM_LE_KEY_PCSRK: - name = "LE_KEY_PCSRK"; - break; - case BTM_LE_KEY_LENC: - name = "LE_KEY_LENC"; - break; - case BTM_LE_KEY_LCSRK: - name = "LE_KEY_LCSRK"; - break; - case BTM_LE_KEY_LID: - name = "LE_KEY_LID"; - default: - return BT_STATUS_FAIL; - } - size_t length = key_length; - int ret = btc_config_get_bin(bdstr, name, (uint8_t *)key_value, &length); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; - -} - -bool btc_storage_compare_address_key_value(bt_bdaddr_t *remote_bd_addr, - uint8_t key_type, void *key_value, int key_length) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - char *key_type_str; - switch (key_type) { - case BTM_LE_KEY_PENC: - key_type_str = "LE_KEY_PENC"; - break; - case BTM_LE_KEY_PID: - key_type_str = "LE_KEY_PID"; - break; - case BTM_LE_KEY_PCSRK: - key_type_str = "LE_KEY_PCSRK"; - break; - case BTM_LE_KEY_LENC: - key_type_str = "LE_KEY_LENC"; - break; - case BTM_LE_KEY_LCSRK: - key_type_str = "LE_KEY_LCSRK"; - break; - case BTM_LE_KEY_LID: - key_type_str = "LE_KEY_LID"; - default: - return false; - } - - return btc_compare_address_key_value(bdstr, key_type_str, key_value, key_length); -} - - -/******************************************************************************* -** -** Function btc_storage_remove_ble_bonding_keys -** -** Description btc storage API - Deletes the bonded device from NVRAM -** -** Returns BT_STATUS_SUCCESS if the deletion was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btc_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr) -{ - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - BTIF_TRACE_DEBUG(" %s in bd addr:%s",__FUNCTION__, bdstr); - int ret = 1; - if (btc_config_exist(bdstr, BTC_LE_DEV_TYPE)) { - ret &= btc_config_remove(bdstr, BTC_LE_DEV_TYPE); - } - if (btc_config_exist(bdstr, "LE_KEY_PENC")) { - ret &= btc_config_remove(bdstr, "LE_KEY_PENC"); - } - if (btc_config_exist(bdstr, "LE_KEY_PID")) { - ret &= btc_config_remove(bdstr, "LE_KEY_PID"); - } - if (btc_config_exist(bdstr, "LE_KEY_PCSRK")) { - ret &= btc_config_remove(bdstr, "LE_KEY_PCSRK"); - } - if (btc_config_exist(bdstr, "LE_KEY_LENC")) { - ret &= btc_config_remove(bdstr, "LE_KEY_LENC"); - } - if (btc_config_exist(bdstr, "LE_KEY_LCSRK")) { - ret &= btc_config_remove(bdstr, "LE_KEY_LCSRK"); - } - //remove the address information after delete the ble key. - ret = btc_config_remove_section(bdstr); - btc_config_save(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; -} - -bt_status_t btc_storage_clear_bond_devices(void) -{ - bt_bdaddr_t bd_addr; - int device_type = 0; - 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_LE_DEV_TYPE, &device_type) && - ((device_type & BT_DEVICE_TYPE_BLE) != BT_DEVICE_TYPE_BLE)) { - continue; - } - - string_to_bdaddr(name, &bd_addr); - //remove the ble bonding keys from the config and then save the config to the flash - if (btc_storage_remove_ble_bonding_keys(&bd_addr) != BT_STATUS_SUCCESS) { - LOG_ERROR("%s, remove bonding key faild", __func__); - return BT_STATUS_FAIL; - } - // the bonded_devices Structure record the devices which has been added to the BTM layer global variable - for (int i = 0; i < bonded_devices.num_devices; i++) { - //if the address is equal to the record device address, remove it from the BTM layer global variable - if (!memcmp(bd_addr.address, bonded_devices.devices[i].address, sizeof(bt_bdaddr_t))) { - BD_ADDR bta_addr; - memcpy(bta_addr, bd_addr.address, sizeof(BD_ADDR)); - if(BTA_DmRemoveDevice(bta_addr) != BTA_SUCCESS) { - LOG_ERROR("%s, remove device faild", __func__); - return BT_STATUS_FAIL; - } - } - } - } - - return BT_STATUS_SUCCESS; -} - -/******************************************************************************* -** -** Function btc_storage_add_ble_local_key -** -** Description BTIF storage API - Adds the ble key to NVRAM -** -** Returns BT_STATUS_SUCCESS if the store was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btc_storage_add_ble_local_key(char *key, - uint8_t key_type, - uint8_t key_length) -{ - const char* name; - switch (key_type) { - case BTC_LE_LOCAL_KEY_IR: - name = "LE_LOCAL_KEY_IR"; - break; - case BTC_LE_LOCAL_KEY_IRK: - name = "LE_LOCAL_KEY_IRK"; - break; - case BTC_LE_LOCAL_KEY_DHK: - name = "LE_LOCAL_KEY_DHK"; - break; - case BTC_LE_LOCAL_KEY_ER: - name = "LE_LOCAL_KEY_ER"; - break; - default: - return BT_STATUS_FAIL; - } - int ret = btc_config_set_bin("Adapter", name, (const uint8_t *)key, key_length); - btc_config_save(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; -} - -/******************************************************************************* -** -** Function btc_storage_get_ble_local_key -** -** Description -** -** Returns BT_STATUS_SUCCESS if the fetch was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btc_storage_get_ble_local_key(uint8_t key_type, - char *key_value, - int key_length) -{ - const char* name; - switch (key_type) { - case BTC_LE_LOCAL_KEY_IR: - name = "LE_LOCAL_KEY_IR"; - break; - case BTC_LE_LOCAL_KEY_IRK: - name = "LE_LOCAL_KEY_IRK"; - break; - case BTC_LE_LOCAL_KEY_DHK: - name = "LE_LOCAL_KEY_DHK"; - break; - case BTC_LE_LOCAL_KEY_ER: - name = "LE_LOCAL_KEY_ER"; - break; - default: - return BT_STATUS_FAIL; - } - size_t length = key_length; - int ret = btc_config_get_bin("Adapter", name, (uint8_t *)key_value, &length); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; -} - -/******************************************************************************* -** -** Function btc_storage_remove_ble_local_keys -** -** Description BTC storage API - Deletes the bonded device from NVRAM -** -** Returns BT_STATUS_SUCCESS if the deletion was successful, -** BT_STATUS_FAIL otherwise -** -*******************************************************************************/ -bt_status_t btc_storage_remove_ble_local_keys(void) -{ - int ret = 1; - if (btc_config_exist("Adapter", "LE_LOCAL_KEY_IR")) { - ret &= btc_config_remove("Adapter", "LE_LOCAL_KEY_IR"); - } - if (btc_config_exist("Adapter", "LE_LOCAL_KEY_IRK")) { - ret &= btc_config_remove("Adapter", "LE_LOCAL_KEY_IRK"); - } - if (btc_config_exist("Adapter", "LE_LOCAL_KEY_DHK")) { - ret &= btc_config_remove("Adapter", "LE_LOCAL_KEY_DHK"); - } - if (btc_config_exist("Adapter", "LE_LOCAL_KEY_ER")) { - ret &= btc_config_remove("Adapter", "LE_LOCAL_KEY_ER"); - } - btc_config_save(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; -} - - -bt_status_t btc_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add, - btc_bonded_devices_t *p_bonded_devices) -{ - int device_type; + uint32_t device_type; int addr_type; bt_bdaddr_t bd_addr; BD_ADDR bta_bd_addr; bool device_added = false; bool key_found = false; - if (!btc_config_get_int(remote_bd_addr, BTC_LE_DEV_TYPE, &device_type)) { + if (!btc_config_get_int(remote_bd_addr, BTC_BLE_STORAGE_DEV_TYPE_STR, (int *)&device_type)) { LOG_ERROR("%s, device_type = %x", __func__, device_type); return BT_STATUS_FAIL; } @@ -513,36 +650,29 @@ bt_status_t btc_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add, string_to_bdaddr(remote_bd_addr, &bd_addr); bdcpy(bta_bd_addr, bd_addr.address); - if (btc_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS) { + if (_btc_storage_get_remote_addr_type(&bd_addr, &addr_type) != BT_STATUS_SUCCESS) { addr_type = BLE_ADDR_PUBLIC; - btc_storage_set_remote_addr_type(&bd_addr, BLE_ADDR_PUBLIC); + _btc_storage_set_remote_addr_type(&bd_addr, BLE_ADDR_PUBLIC, true); } - btc_read_le_key(BTM_LE_KEY_PENC, sizeof(tBTM_LE_PENC_KEYS), + _btc_read_le_key(BTM_LE_KEY_PENC, sizeof(tBTM_LE_PENC_KEYS), bd_addr, addr_type, add, &device_added, &key_found); - btc_read_le_key(BTM_LE_KEY_PID, sizeof(tBTM_LE_PID_KEYS), + _btc_read_le_key(BTM_LE_KEY_PID, sizeof(tBTM_LE_PID_KEYS), bd_addr, addr_type, add, &device_added, &key_found); - btc_read_le_key(BTM_LE_KEY_LID, sizeof(tBTM_LE_PID_KEYS), + _btc_read_le_key(BTM_LE_KEY_LID, sizeof(tBTM_LE_PID_KEYS), bd_addr, addr_type, add, &device_added, &key_found); - btc_read_le_key(BTM_LE_KEY_PCSRK, sizeof(tBTM_LE_PCSRK_KEYS), + _btc_read_le_key(BTM_LE_KEY_PCSRK, sizeof(tBTM_LE_PCSRK_KEYS), bd_addr, addr_type, add, &device_added, &key_found); - btc_read_le_key(BTM_LE_KEY_LENC, sizeof(tBTM_LE_LENC_KEYS), + _btc_read_le_key(BTM_LE_KEY_LENC, sizeof(tBTM_LE_LENC_KEYS), bd_addr, addr_type, add, &device_added, &key_found); - btc_read_le_key(BTM_LE_KEY_LCSRK, sizeof(tBTM_LE_LCSRK_KEYS), + _btc_read_le_key(BTM_LE_KEY_LCSRK, sizeof(tBTM_LE_LCSRK_KEYS), bd_addr, addr_type, add, &device_added, &key_found); - // Fill in the bonded devices - if (device_added) - { - memcpy(&p_bonded_devices->devices[p_bonded_devices->num_devices++], - &bd_addr, sizeof(bt_bdaddr_t)); - } - if (key_found) { return BT_STATUS_SUCCESS; } @@ -550,91 +680,120 @@ bt_status_t btc_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add, return BT_STATUS_FAIL; } -bt_status_t btc_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr, - uint8_t addr_type) +static bt_status_t btc_storage_in_fetch_bonded_ble_devices(int add) { - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bt_bdaddr_t)); - int ret = btc_config_set_int(bdstr, "AddrType", (int)addr_type); - btc_config_save(); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + 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; + } + LOG_DEBUG("%s, name = %s", __func__, name); + if (_btc_storage_in_fetch_bonded_ble_device(name, add) != BT_STATUS_SUCCESS) { + LOG_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_get_remote_addr_type +** Function btc_storage_load_bonded_devices ** -** Description btc storage API - Fetches the remote addr type +** 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 fetch was successful, -** BT_STATUS_FAIL otherwise +** Returns BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise ** *******************************************************************************/ -bt_status_t btc_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr, - int*addr_type) +bt_status_t btc_storage_load_bonded_ble_devices(void) { - bdstr_t bdstr; - bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); - int ret = btc_config_get_int(bdstr, "AddrType", addr_type); - return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; + bt_status_t status; + status = btc_storage_in_fetch_bonded_ble_devices(1); + LOG_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; + uint32_t device_type = 0; + char buffer[sizeof(tBTM_LE_KEY_VALUE)] = {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)) { + + if (dev_num-- <= 0) { + break; + } + + 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)) { + continue; + } + + string_to_bdaddr(name, &bd_addr); + memcpy(bond_dev->bd_addr, bd_addr.address, sizeof(bt_bdaddr_t)); + //resolve the peer device long term key + if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PENC, buffer, sizeof(tBTM_LE_PENC_KEYS)) == BT_STATUS_SUCCESS) { + bond_dev->bond_key.key_mask |= ESP_BLE_ENC_KEY_MASK; + memcpy(&bond_dev->bond_key.penc_key, buffer, sizeof(tBTM_LE_PENC_KEYS)); + } + //resolve the peer device csrk + if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PCSRK, buffer, sizeof(tBTM_LE_PCSRK_KEYS)) == BT_STATUS_SUCCESS) { + bond_dev->bond_key.key_mask |= ESP_BLE_CSR_KEY_MASK; + memcpy(&bond_dev->bond_key.pcsrk_key, buffer, sizeof(tBTM_LE_PCSRK_KEYS)); + } + //resolve the peer device irk + if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PID, buffer, sizeof(tBTM_LE_PID_KEYS)) == BT_STATUS_SUCCESS) { + bond_dev->bond_key.key_mask |= ESP_BLE_ID_KEY_MASK; + memcpy(&bond_dev->bond_key.pid_key, buffer, sizeof(tBTM_LE_PID_KEYS)); + } + //serch for the next bond device + bond_dev++; + } + btc_config_unlock(); + + return BT_STATUS_SUCCESS; } int btc_storage_get_num_ble_bond_devices(void) { int num_dev = 0; - int device_type = 0; + 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_LE_DEV_TYPE, &device_type) && - device_type != BT_DEVICE_TYPE_BLE) { + 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)) { continue; } num_dev++; } + btc_config_unlock(); return num_dev; } - -void btc_dm_load_ble_local_keys(void) -{ - memset(&ble_local_key_cb, 0, sizeof(btc_dm_local_key_cb_t)); - - if (btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0], - BT_OCTET16_LEN)== BT_STATUS_SUCCESS) { - ble_local_key_cb.is_er_rcvd = TRUE; - LOG_DEBUG("%s BLE ER key loaded",__func__ ); - } - - if ((btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0], - BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&& - (btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0], - BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&& - (btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0], - BT_OCTET16_LEN)== BT_STATUS_SUCCESS)) { - ble_local_key_cb.is_id_keys_rcvd = TRUE; - LOG_DEBUG("%s BLE ID keys loaded", __func__); - } - -} -void btc_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er, - tBTA_BLE_LOCAL_ID_KEYS *p_id_keys) -{ - if (ble_local_key_cb.is_er_rcvd ) { - memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16)); - *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER; - } - - if (ble_local_key_cb.is_id_keys_rcvd) { - memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16)); - memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16)); - memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16)); - *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID; - } - LOG_DEBUG("%s *p_key_mask=0x%02x",__func__, *p_key_mask); -} - #endif ///SMP_INCLUDED == TRUE diff --git a/components/bt/bluedroid/btc/core/btc_config.c b/components/bt/bluedroid/btc/core/btc_config.c index 947388064..9249cfd8f 100644 --- a/components/bt/bluedroid/btc/core/btc_config.c +++ b/components/bt/bluedroid/btc/core/btc_config.c @@ -33,49 +33,6 @@ static const char *CONFIG_FILE_PATH = "bt_config.conf"; static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000; static void btc_key_value_to_string(uint8_t *key_vaule, char *value_str, int key_length); - -// TODO(zachoverflow): Move these two functions out, because they are too specific for this file -// {grumpy-cat/no, monty-python/you-make-me-sad} -bool btc_get_device_type(const BD_ADDR bd_addr, int *p_device_type) -{ - if (p_device_type == NULL) { - return FALSE; - } - - bt_bdaddr_t bda; - bdcpy(bda.address, bd_addr); - - bdstr_t bd_addr_str; - bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str)); - - if (!btc_config_get_int(bd_addr_str, BTC_LE_DEV_TYPE, p_device_type)) { - return FALSE; - } - - LOG_DEBUG("%s: Device [%s] type %d\n", __FUNCTION__, bd_addr_str, *p_device_type); - return TRUE; -} - -bool btc_get_address_type(const BD_ADDR bd_addr, int *p_addr_type) -{ - if (p_addr_type == NULL) { - return FALSE; - } - - bt_bdaddr_t bda; - bdcpy(bda.address, bd_addr); - - bdstr_t bd_addr_str; - bdaddr_to_string(&bda, bd_addr_str, sizeof(bd_addr_str)); - - if (!btc_config_get_int(bd_addr_str, "AddrType", p_addr_type)) { - return FALSE; - } - - LOG_DEBUG("%s: Device [%s] address type %d\n", __FUNCTION__, bd_addr_str, *p_addr_type); - return TRUE; -} - static osi_mutex_t lock; // protects operations on |config|. static config_t *config; @@ -88,11 +45,9 @@ bool btc_compare_address_key_value(const char *section, char *key_type, void *ke return false; } btc_key_value_to_string((uint8_t *)key_value, value_str, key_length); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); if ((status = config_has_key_in_section(config, key_type, value_str)) == true) { config_remove_section(config, section); } - osi_mutex_unlock(&lock); return status; } @@ -160,11 +115,7 @@ bool btc_config_has_section(const char *section) assert(config != NULL); assert(section != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); - bool ret = config_has_section(config, section); - osi_mutex_unlock(&lock); - - return ret; + return config_has_section(config, section); } bool btc_config_exist(const char *section, const char *key) @@ -173,11 +124,7 @@ bool btc_config_exist(const char *section, const char *key) assert(section != NULL); assert(key != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); - bool ret = config_has_key(config, section, key); - osi_mutex_unlock(&lock); - - return ret; + return config_has_key(config, section, key); } bool btc_config_get_int(const char *section, const char *key, int *value) @@ -187,12 +134,10 @@ bool btc_config_get_int(const char *section, const char *key, int *value) assert(key != NULL); assert(value != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); bool ret = config_has_key(config, section, key); if (ret) { *value = config_get_int(config, section, key, *value); } - osi_mutex_unlock(&lock); return ret; } @@ -203,9 +148,7 @@ bool btc_config_set_int(const char *section, const char *key, int value) assert(section != NULL); assert(key != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); config_set_int(config, section, key, value); - osi_mutex_unlock(&lock); return true; } @@ -218,9 +161,7 @@ bool btc_config_get_str(const char *section, const char *key, char *value, int * assert(value != NULL); assert(size_bytes != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); const char *stored_value = config_get_string(config, section, key, NULL); - osi_mutex_unlock(&lock); if (!stored_value) { return false; @@ -239,9 +180,7 @@ bool btc_config_set_str(const char *section, const char *key, const char *value) assert(key != NULL); assert(value != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); config_set_string(config, section, key, value, false); - osi_mutex_unlock(&lock); return true; } @@ -254,9 +193,7 @@ bool btc_config_get_bin(const char *section, const char *key, uint8_t *value, si assert(value != NULL); assert(length != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); const char *value_str = config_get_string(config, section, key, NULL); - osi_mutex_unlock(&lock); if (!value_str) { return false; @@ -287,9 +224,7 @@ size_t btc_config_get_bin_length(const char *section, const char *key) assert(section != NULL); assert(key != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); const char *value_str = config_get_string(config, section, key, NULL); - osi_mutex_unlock(&lock); if (!value_str) { return 0; @@ -321,9 +256,7 @@ bool btc_config_set_bin(const char *section, const char *key, const uint8_t *val str[(i * 2) + 1] = lookup[value[i] & 0x0F]; } - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); config_set_string(config, section, key, str, false); - osi_mutex_unlock(&lock); osi_free(str); return true; @@ -355,17 +288,15 @@ const char *btc_config_section_name(const btc_config_section_iter_t *section) return config_section_name((const config_section_node_t *)section); } + + bool btc_config_remove(const char *section, const char *key) { assert(config != NULL); assert(section != NULL); assert(key != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); - bool ret = config_remove_key(config, section, key); - osi_mutex_unlock(&lock); - - return ret; + return config_remove_key(config, section, key); } bool btc_config_remove_section(const char *section) @@ -373,81 +304,37 @@ bool btc_config_remove_section(const char *section) assert(config != NULL); assert(section != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); - bool ret = config_remove_section(config, section); - osi_mutex_unlock(&lock); - - return ret; -} - -void btc_config_save(void) -{ - assert(config != NULL); - // Garbage collection process: the config file accumulates - // cached information about remote devices during regular - // inquiry scans. We remove some of these junk entries - // so the file doesn't grow indefinitely. We have to take care - // to make sure we don't remove information about bonded - // devices (hence the check for link keys). - static const size_t CACHE_MAX = 256; - const char *keys[CACHE_MAX]; - size_t num_keys = 0; - size_t total_candidates = 0; - - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); - for (const config_section_node_t *snode = config_section_begin(config); snode != config_section_end(config); snode = config_section_next(snode)) { - const char *section = config_section_name(snode); - if (!string_is_bdaddr(section)) { - continue; - } - - if (config_has_key(config, section, "LinkKey") || - config_has_key(config, section, "LE_KEY_PENC") || - config_has_key(config, section, "LE_KEY_PID") || - config_has_key(config, section, "LE_KEY_PCSRK") || - config_has_key(config, section, "LE_KEY_LENC") || - config_has_key(config, section, "LE_KEY_LCSRK")) { - continue; - } - - if (num_keys < CACHE_MAX) { - keys[num_keys++] = section; - } - - ++total_candidates; - } - - if (total_candidates > CACHE_MAX * 2) - while (num_keys > 0) { - config_remove_section(config, keys[--num_keys]); - } - config_save(config, CONFIG_FILE_PATH); - osi_mutex_unlock(&lock); + return config_remove_section(config, section); } void btc_config_flush(void) { assert(config != NULL); - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); + config_save(config, CONFIG_FILE_PATH); - osi_mutex_unlock(&lock); } int btc_config_clear(void) { assert(config != NULL); - - osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); config_free(config); config = config_new_empty(); if (config == NULL) { - osi_mutex_unlock(&lock); return false; } int ret = config_save(config, CONFIG_FILE_PATH); - osi_mutex_unlock(&lock); return ret; } +void btc_config_lock(void) +{ + osi_mutex_lock(&lock, OSI_MUTEX_MAX_TIMEOUT); +} + +void btc_config_unlock(void) +{ + osi_mutex_unlock(&lock); +} + diff --git a/components/bt/bluedroid/btc/core/btc_dm.c b/components/bt/bluedroid/btc/core/btc_dm.c index 06d78a2b4..e43b5ede9 100644 --- a/components/bt/bluedroid/btc/core/btc_dm.c +++ b/components/bt/bluedroid/btc/core/btc_dm.c @@ -38,6 +38,11 @@ ** Static variables ******************************************************************************/ static tBTA_SERVICE_MASK btc_enabled_services = 0; +#if (SMP_INCLUDED == TRUE) +static btc_dm_pairing_cb_t pairing_cb; +static btc_dm_local_key_cb_t ble_local_key_cb; +#endif + /****************************************************************************** ** Static functions ******************************************************************************/ @@ -117,6 +122,110 @@ static void btc_disable_bluetooth_evt(void) } #if (SMP_INCLUDED == TRUE) +void btc_dm_load_ble_local_keys(void) +{ + memset(&ble_local_key_cb, 0, sizeof(btc_dm_local_key_cb_t)); + + if (btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0], + BT_OCTET16_LEN)== BT_STATUS_SUCCESS) { + ble_local_key_cb.is_er_rcvd = TRUE; + LOG_DEBUG("%s BLE ER key loaded",__func__ ); + } + + if ((btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0], + BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&& + (btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0], + BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&& + (btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0], + BT_OCTET16_LEN)== BT_STATUS_SUCCESS)) { + ble_local_key_cb.is_id_keys_rcvd = TRUE; + LOG_DEBUG("%s BLE ID keys loaded", __func__); + } + +} +void btc_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er, + tBTA_BLE_LOCAL_ID_KEYS *p_id_keys) +{ + if (ble_local_key_cb.is_er_rcvd ) { + memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16)); + *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER; + } + + if (ble_local_key_cb.is_id_keys_rcvd) { + memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16)); + memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16)); + memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16)); + *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID; + } + LOG_DEBUG("%s *p_key_mask=0x%02x",__func__, *p_key_mask); +} + + +static void btc_dm_remove_ble_bonding_keys(void) +{ + bt_bdaddr_t bd_addr; + LOG_DEBUG("%s\n",__func__); + + bdcpy(bd_addr.address, pairing_cb.bd_addr); + + btc_storage_remove_remote_addr_type(&bd_addr, false); + btc_storage_remove_ble_dev_type(&bd_addr, false); + btc_storage_remove_ble_bonding_keys(&bd_addr); +} + +static void btc_dm_save_ble_bonding_keys(void) +{ + bt_bdaddr_t bd_addr; + + bdcpy(bd_addr.address, pairing_cb.bd_addr); + + btc_storage_set_ble_dev_type(&bd_addr, false); + LOG_DEBUG("%s, penc = %d, pid = %d", __func__, pairing_cb.ble.is_penc_key_rcvd, pairing_cb.ble.is_pid_key_rcvd); + if (pairing_cb.ble.is_penc_key_rcvd) { + btc_storage_add_ble_bonding_key(&bd_addr, + (char *) &pairing_cb.ble.penc_key, + BTM_LE_KEY_PENC, + sizeof(tBTM_LE_PENC_KEYS)); + } + + if (pairing_cb.ble.is_pid_key_rcvd) { + btc_storage_add_ble_bonding_key(&bd_addr, + (char *) &pairing_cb.ble.pid_key, + BTM_LE_KEY_PID, + sizeof(tBTM_LE_PID_KEYS)); + } + + + if (pairing_cb.ble.is_pcsrk_key_rcvd) { + btc_storage_add_ble_bonding_key(&bd_addr, + (char *) &pairing_cb.ble.pcsrk_key, + BTM_LE_KEY_PCSRK, + sizeof(tBTM_LE_PCSRK_KEYS)); + } + + + if (pairing_cb.ble.is_lenc_key_rcvd) { + btc_storage_add_ble_bonding_key(&bd_addr, + (char *) &pairing_cb.ble.lenc_key, + BTM_LE_KEY_LENC, + sizeof(tBTM_LE_LENC_KEYS)); + } + + if (pairing_cb.ble.is_lcsrk_key_rcvd) { + btc_storage_add_ble_bonding_key(&bd_addr, + (char *) &pairing_cb.ble.lcsrk_key, + BTM_LE_KEY_LCSRK, + sizeof(tBTM_LE_LCSRK_KEYS)); + } + + if (pairing_cb.ble.is_lidk_key_rcvd) { + btc_storage_add_ble_bonding_key(&bd_addr, + NULL, + BTM_LE_KEY_LID, + 0); + } +} + static void btc_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) { /* Save link key, if not temporary */ @@ -136,7 +245,7 @@ static void btc_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) (pairing_cb.bd_addr[0] << 24) + (pairing_cb.bd_addr[1] << 16) + (pairing_cb.bd_addr[2] << 8) + pairing_cb.bd_addr[3], (pairing_cb.bd_addr[4] << 8) + pairing_cb.bd_addr[5]); 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); + btc_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type, true); } /* check the irk has been save in the flash or not, if the irk has already save, means that the peer device has bonding before. */ @@ -144,7 +253,7 @@ static void btc_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) btc_storage_compare_address_key_value(&bdaddr, BTM_LE_KEY_PID, (void *)&pairing_cb.ble.pid_key, sizeof(tBTM_LE_PID_KEYS)); } - btc_save_ble_bonding_keys(); + btc_dm_save_ble_bonding_keys(); } else { /*Map the HCI fail reason to bt status */ switch (p_auth_cmpl->fail_reason) { @@ -358,7 +467,9 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg) memcpy(bd_addr.address, p_data->link_down.bd_addr, sizeof(BD_ADDR)); btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN); //remove the bonded key in the config and nvs flash. - //btc_storage_remove_ble_bonding_keys(&bd_addr); + btc_storage_remove_ble_dev_type(&bd_addr, false); + btc_storage_remove_remote_addr_type(&bd_addr, false); + btc_storage_remove_ble_bonding_keys(&bd_addr); ble_msg.act = ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT; param.remove_bond_dev_cmpl.status = (p_data->link_down.status == HCI_SUCCESS) ? ESP_BT_STATUS_SUCCESS : ESP_BT_STATUS_FAIL; memcpy(param.remove_bond_dev_cmpl.bd_addr, p_data->link_down.bd_addr, sizeof(BD_ADDR)); diff --git a/components/bt/bluedroid/btc/core/btc_storage.c b/components/bt/bluedroid/btc/core/btc_storage.c index 4f18cbfc9..333b768af 100644 --- a/components/bt/bluedroid/btc/core/btc_storage.c +++ b/components/bt/bluedroid/btc/core/btc_storage.c @@ -43,11 +43,14 @@ bt_status_t btc_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr, bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); LOG_DEBUG("add to storage: Remote device:%s\n", bdstr); - int ret = btc_config_set_int(bdstr, "LinkKeyType", (int)key_type); - ret &= btc_config_set_int(bdstr, "PinLength", (int)pin_length); - ret &= btc_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY)); + btc_config_lock(); + int ret = btc_config_set_int(bdstr, BTC_STORAGE_LINK_KEY_TYPE_STR, (int)key_type); + ret &= btc_config_set_int(bdstr, BTC_STORAGE_PIN_LENGTH_STR, (int)pin_length); + ret &= btc_config_set_bin(bdstr, BTC_STORAGE_LINK_KEY_STR, link_key, sizeof(LINK_KEY)); /* write bonded info immediately */ btc_config_flush(); + btc_config_unlock(); + LOG_DEBUG("Storage add rslt %d\n", ret); return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } @@ -66,6 +69,7 @@ static bt_status_t btc_in_fetch_bonded_devices(int add) { BOOLEAN bt_linkkey_file_found = FALSE; + 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)) { @@ -75,21 +79,19 @@ static bt_status_t btc_in_fetch_bonded_devices(int add) LOG_DEBUG("Remote device:%s\n", name); LINK_KEY link_key; size_t size = sizeof(link_key); - if (btc_config_get_bin(name, "LinkKey", link_key, &size)) { + if (btc_config_get_bin(name, BTC_STORAGE_LINK_KEY_STR, link_key, &size)) { int linkkey_type; - if (btc_config_get_int(name, "LinkKeyType", &linkkey_type)) { - //int pin_len; - //btc_config_get_int(name, "PinLength", &pin_len)) + if (btc_config_get_int(name, BTC_STORAGE_LINK_KEY_TYPE_STR, &linkkey_type)) { 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, "DevClass", &cod)) { + if (btc_config_get_int(name, BTC_STORAGE_DEV_CLASS_STR, &cod)) { uint2devclass((UINT32)cod, dev_class); } - btc_config_get_int(name, "PinLength", &pin_length); + btc_config_get_int(name, BTC_STORAGE_PIN_LENGTH_STR, &pin_length); #if (SMP_INCLUDED == TRUE) BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0, (UINT8)linkkey_type, 0, pin_length); @@ -104,6 +106,8 @@ static bt_status_t btc_in_fetch_bonded_devices(int add) LOG_DEBUG("Remote device:%s, no link key\n", name); } } + btc_config_unlock(); + return BT_STATUS_SUCCESS; } @@ -142,19 +146,22 @@ bt_status_t btc_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr) { bdstr_t bdstr; bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr)); + int ret = 1; LOG_DEBUG("Add to storage: Remote device:%s\n", bdstr); - int ret = 1; - if (btc_config_exist(bdstr, "LinkKeyType")) { - ret &= btc_config_remove(bdstr, "LinkKeyType"); + btc_config_lock(); + if (btc_config_exist(bdstr, BTC_STORAGE_LINK_KEY_TYPE_STR)) { + ret &= btc_config_remove(bdstr, BTC_STORAGE_LINK_KEY_TYPE_STR); } - if (btc_config_exist(bdstr, "PinLength")) { - ret &= btc_config_remove(bdstr, "PinLength"); + if (btc_config_exist(bdstr, BTC_STORAGE_PIN_LENGTH_STR)) { + ret &= btc_config_remove(bdstr, BTC_STORAGE_PIN_LENGTH_STR); } - if (btc_config_exist(bdstr, "LinkKey")) { - ret &= btc_config_remove(bdstr, "LinkKey"); + if (btc_config_exist(bdstr, BTC_STORAGE_LINK_KEY_STR)) { + ret &= btc_config_remove(bdstr, BTC_STORAGE_LINK_KEY_STR); } /* write bonded info immediately */ btc_config_flush(); + btc_config_unlock(); + return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } diff --git a/components/bt/bluedroid/btc/include/btc_ble_storage.h b/components/bt/bluedroid/btc/include/btc_ble_storage.h index 111f1f0a2..0d4d43e7c 100644 --- a/components/bt/bluedroid/btc/include/btc_ble_storage.h +++ b/components/bt/bluedroid/btc/include/btc_ble_storage.h @@ -23,52 +23,25 @@ #define BTC_LE_LOCAL_KEY_DHK (1<<2) #define BTC_LE_LOCAL_KEY_ER (1<<3) +#define BTC_BLE_STORAGE_DEV_TYPE_STR "DevType" +#define BTC_BLE_STORAGE_ADDR_TYPE_STR "AddrType" +#define BTC_BLE_STORAGE_LINK_KEY_STR "LinkKey" +#define BTC_BLE_STORAGE_LE_KEY_PENC_STR "LE_KEY_PENC" +#define BTC_BLE_STORAGE_LE_KEY_PID_STR "LE_KEY_PID" +#define BTC_BLE_STORAGE_LE_KEY_PCSRK_STR "LE_KEY_PCSRK" +#define BTC_BLE_STORAGE_LE_KEY_LENC_STR "LE_KEY_LENC" +#define BTC_BLE_STORAGE_LE_KEY_LID_STR "LE_KEY_LID" +#define BTC_BLE_STORAGE_LE_KEY_LCSRK_STR "LE_KEY_LCSRK" + +#define BTC_BLE_STORAGE_LOCAL_ADAPTER_STR "Adapter" +#define BTC_BLE_STORAGE_LE_LOCAL_KEY_IR_STR "LE_LOCAL_KEY_IR" +#define BTC_BLE_STORAGE_LE_LOCAL_KEY_IRK_STR "LE_LOCAL_KEY_IRK" +#define BTC_BLE_STORAGE_LE_LOCAL_KEY_DHK_STR "LE_LOCAL_KEY_DHK" +#define BTC_BLE_STORAGE_LE_LOCAL_KEY_ER_STR "LE_LOCAL_KEY_ER" + /************************************************************************************ ** Local type definitions ************************************************************************************/ -typedef struct -{ - uint32_t num_devices; - bt_bdaddr_t devices[BTM_SEC_MAX_DEVICE_RECORDS]; -} btc_bonded_devices_t; - -typedef struct -{ - bool is_penc_key_rcvd; - tBTM_LE_PENC_KEYS penc_key; /* received peer encryption key */ - bool is_pcsrk_key_rcvd; - tBTM_LE_PCSRK_KEYS pcsrk_key; /* received peer device SRK */ - bool is_pid_key_rcvd; - tBTM_LE_PID_KEYS pid_key; /* peer device ID key */ - bool is_lenc_key_rcvd; - tBTM_LE_LENC_KEYS lenc_key; /* local encryption reproduction keys LTK = = d1(ER,DIV,0)*/ - bool is_lcsrk_key_rcvd; - tBTM_LE_LCSRK_KEYS lcsrk_key; /* local device CSRK = d1(ER,DIV,1)*/ - bool is_lidk_key_rcvd; /* local identity key received */ -} btc_dm_ble_cb_t; - -typedef struct -{ - bt_bdaddr_t static_bdaddr; - BD_ADDR bd_addr; - btc_dm_ble_cb_t ble; -} btc_dm_pairing_cb_t; - -typedef struct -{ - uint8_t ir[BT_OCTET16_LEN]; - uint8_t irk[BT_OCTET16_LEN]; - uint8_t dhk[BT_OCTET16_LEN]; -}btc_dm_local_key_id_t; - -typedef struct -{ - bool is_er_rcvd; - uint8_t er[BT_OCTET16_LEN]; - bool is_id_keys_rcvd; - btc_dm_local_key_id_t id_keys; /* ID kyes */ -}btc_dm_local_key_cb_t; - typedef struct { BT_OCTET16 sp_c; @@ -77,63 +50,37 @@ typedef struct } btc_dm_oob_cb_t; -extern btc_dm_pairing_cb_t pairing_cb; -extern btc_dm_local_key_cb_t ble_local_key_cb; -extern btc_bonded_devices_t bonded_devices; +void btc_storage_save(void); -bt_status_t btc_storage_load_bonded_ble_devices(void); +bt_status_t btc_storage_add_ble_bonding_key( bt_bdaddr_t *remote_bd_addr, char *key, uint8_t key_type, uint8_t key_length); -bt_status_t btc_get_bonded_ble_devices_list(esp_ble_bond_dev_t *bond_dev); - -bt_status_t btc_in_fetch_bonded_ble_devices(int add); - -void btc_dm_remove_ble_bonding_keys(void); - -bt_status_t btc_storage_add_ble_bonding_key( bt_bdaddr_t *remote_bd_addr, - char *key, - uint8_t key_type, - uint8_t key_length); - -bool btc_compare_le_key_value(const uint8_t key_type, const size_t key_len, const tBTA_LE_KEY_VALUE *key_vaule, - bt_bdaddr_t bd_addr); - -void btc_save_ble_bonding_keys(void); - -bt_status_t btc_in_fetch_bonded_ble_device(const char *remote_bd_addr, int add, - btc_bonded_devices_t *p_bonded_devices); - -bt_status_t btc_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, - uint8_t key_type, - char *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_add_ble_local_key(char *key, - uint8_t key_type, - uint8_t key_length); +bt_status_t btc_storage_get_ble_bonding_key(bt_bdaddr_t *remote_bd_addr, uint8_t key_type, char *key_value, int key_length); bt_status_t btc_storage_remove_ble_bonding_keys(bt_bdaddr_t *remote_bd_addr); -bt_status_t btc_storage_clear_bond_devices(void); +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_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_get_ble_local_key(uint8_t key_type, - char *key_value, - int key_len); +bt_status_t btc_storage_get_ble_local_key(uint8_t key_type, char *key_value, int key_len); -bt_status_t btc_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr, - int *addr_type); +bt_status_t btc_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr, int *addr_type); + +bt_status_t btc_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr, uint8_t addr_type, bool flush); + +bt_status_t btc_storage_remove_remote_addr_type(bt_bdaddr_t *remote_bd_addr, bool flush); + +bt_status_t btc_storage_set_ble_dev_type(bt_bdaddr_t *bd_addr, bool flush); + +bt_status_t btc_storage_remove_ble_dev_type(bt_bdaddr_t *remote_bd_addr, bool flush); + +bt_status_t btc_storage_load_bonded_ble_devices(void); + +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); -bt_status_t btc_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr, - uint8_t addr_type); - -void btc_dm_load_ble_local_keys(void); - -void btc_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er, - tBTA_BLE_LOCAL_ID_KEYS *p_id_keys); #endif ///SMP_INCLUDED == TRUE -#endif ///__BTC_BLE_STORAGE_H__ \ No newline at end of file +#endif ///__BTC_BLE_STORAGE_H__ diff --git a/components/bt/bluedroid/btc/include/btc_config.h b/components/bt/bluedroid/btc/include/btc_config.h index 2367c53b4..79f6137e8 100644 --- a/components/bt/bluedroid/btc/include/btc_config.h +++ b/components/bt/bluedroid/btc/include/btc_config.h @@ -20,8 +20,6 @@ #include "bt_types.h" -#define BTC_LE_DEV_TYPE "DevType" - typedef struct btc_config_section_iter_t btc_config_section_iter_t; bool btc_config_init(void); @@ -46,7 +44,6 @@ const btc_config_section_iter_t *btc_config_section_end(void); const btc_config_section_iter_t *btc_config_section_next(const btc_config_section_iter_t *section); const char *btc_config_section_name(const btc_config_section_iter_t *section); -void btc_config_save(void); void btc_config_flush(void); int btc_config_clear(void); @@ -55,4 +52,7 @@ bool btc_get_address_type(const BD_ADDR bd_addr, int *p_addr_type); bool btc_compare_address_key_value(const char *section, char *key_type, void *key_value, int key_length); bool btc_get_device_type(const BD_ADDR bd_addr, int *p_device_type); +void btc_config_lock(void); +void btc_config_unlock(void); + #endif diff --git a/components/bt/bluedroid/btc/include/btc_dm.h b/components/bt/bluedroid/btc/include/btc_dm.h index e397d82b4..44f4d84c1 100644 --- a/components/bt/bluedroid/btc/include/btc_dm.h +++ b/components/bt/bluedroid/btc/include/btc_dm.h @@ -29,6 +29,45 @@ typedef union { tBTA_DM_SEC sec; } btc_dm_sec_args_t; +typedef struct +{ + bool is_penc_key_rcvd; + tBTM_LE_PENC_KEYS penc_key; /* received peer encryption key */ + bool is_pcsrk_key_rcvd; + tBTM_LE_PCSRK_KEYS pcsrk_key; /* received peer device SRK */ + bool is_pid_key_rcvd; + tBTM_LE_PID_KEYS pid_key; /* peer device ID key */ + bool is_lenc_key_rcvd; + tBTM_LE_LENC_KEYS lenc_key; /* local encryption reproduction keys LTK = = d1(ER,DIV,0)*/ + bool is_lcsrk_key_rcvd; + tBTM_LE_LCSRK_KEYS lcsrk_key; /* local device CSRK = d1(ER,DIV,1)*/ + bool is_lidk_key_rcvd; /* local identity key received */ +} btc_dm_ble_cb_t; + +typedef struct +{ + bt_bdaddr_t static_bdaddr; + BD_ADDR bd_addr; + btc_dm_ble_cb_t ble; +} btc_dm_pairing_cb_t; + +typedef struct +{ + uint8_t ir[BT_OCTET16_LEN]; + uint8_t irk[BT_OCTET16_LEN]; + uint8_t dhk[BT_OCTET16_LEN]; +} btc_dm_local_key_id_t; + +typedef struct +{ + bool is_er_rcvd; + uint8_t er[BT_OCTET16_LEN]; + bool is_id_keys_rcvd; + btc_dm_local_key_id_t id_keys; /* ID kyes */ +} btc_dm_local_key_cb_t; + + + // void btc_dm_call_handler(btc_msg_t *msg); void btc_dm_sec_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *data); void btc_dm_sec_cb_handler(btc_msg_t *msg); @@ -37,4 +76,11 @@ void btc_dm_sec_arg_deep_copy(btc_msg_t *msg, void *dst, void *src); bt_status_t btc_dm_enable_service(tBTA_SERVICE_ID service_id); bt_status_t btc_dm_disable_service(tBTA_SERVICE_ID service_id); +#if (SMP_INCLUDED == TRUE) +void btc_dm_load_ble_local_keys(void); + +void btc_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er, + tBTA_BLE_LOCAL_ID_KEYS *p_id_keys); +#endif + #endif /* __BTC_DM_H__ */ diff --git a/components/bt/bluedroid/btc/include/btc_storage.h b/components/bt/bluedroid/btc/include/btc_storage.h index c2fb6ccbb..9e69b4139 100644 --- a/components/bt/bluedroid/btc/include/btc_storage.h +++ b/components/bt/bluedroid/btc/include/btc_storage.h @@ -19,6 +19,12 @@ #include "bt_defs.h" #include "bt_types.h" + +#define BTC_STORAGE_DEV_CLASS_STR "DevClass" +#define BTC_STORAGE_LINK_KEY_STR "LinkKey" /* same as the ble */ +#define BTC_STORAGE_LINK_KEY_TYPE_STR "LinkKeyType" +#define BTC_STORAGE_PIN_LENGTH_STR "PinLength" + /******************************************************************************* ** ** Function btc_storage_add_bonded_device diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c index a4dccae0e..5869f393d 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c @@ -26,6 +26,7 @@ #include "esp_bt_defs.h" #include "esp_gap_ble_api.h" #include "btc_ble_storage.h" +#include "btc_dm.h" static tBTA_BLE_ADV_DATA gl_bta_adv_data; static tBTA_BLE_ADV_DATA gl_bta_scan_rsp_data; @@ -793,71 +794,6 @@ static void btc_ble_set_rand_addr (BD_ADDR rand_addr) } } -#if (SMP_INCLUDED) -static void btc_ble_remove_bond_device(esp_bt_status_t status) -{ - int ret; - esp_ble_gap_cb_param_t param; - btc_msg_t msg; - param.remove_bond_dev_cmpl.status = status; - msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_GAP_BLE; - msg.act = ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT; - - ret = btc_transfer_context(&msg, ¶m, - sizeof(esp_ble_gap_cb_param_t), NULL); - - if (ret != BT_STATUS_SUCCESS) { - LOG_ERROR("%s btc_transfer_context failed", __func__); - } -} - -static void btc_ble_clear_bond_device(void) -{ - int ret; - esp_ble_gap_cb_param_t param; - btc_msg_t msg; - ret = btc_storage_clear_bond_devices(); - param.clear_bond_dev_cmpl.status = ret; - msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_GAP_BLE; - msg.act = ESP_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT; - - ret = btc_transfer_context(&msg, ¶m, - sizeof(esp_ble_gap_cb_param_t), NULL); - - if (ret != BT_STATUS_SUCCESS) { - LOG_ERROR("%s btc_transfer_context failed", __func__); - } - -} - -static void btc_ble_get_bond_device_list(void) -{ - int ret; - esp_ble_gap_cb_param_t param; - esp_ble_bond_dev_t *bond_dev; - btc_msg_t msg; - int num_dev = btc_storage_get_num_ble_bond_devices(); - bond_dev = (esp_ble_bond_dev_t *)osi_malloc(sizeof(esp_ble_bond_dev_t)*num_dev); - - param.get_bond_dev_cmpl.status = btc_get_bonded_ble_devices_list(bond_dev); - param.get_bond_dev_cmpl.dev_num = num_dev; - param.get_bond_dev_cmpl.bond_dev = bond_dev; - msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_GAP_BLE; - msg.act = ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT; - - ret = btc_transfer_context(&msg, ¶m, sizeof(esp_ble_gap_cb_param_t), btc_gap_ble_cb_deep_copy); - - if (ret != BT_STATUS_SUCCESS) { - LOG_ERROR("%s btc_transfer_context failed", __func__); - } - // release the buffer after used. - osi_free(bond_dev); -} -#endif /* #if (SMP_INCLUDED) */ - static void btc_ble_config_local_privacy(bool privacy_enable, tBTA_SET_LOCAL_PRIVACY_CBACK *set_local_privacy_cback) { BTA_DmBleConfigLocalPrivacy(privacy_enable, set_local_privacy_cback); @@ -954,21 +890,6 @@ void btc_gap_ble_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) void btc_gap_ble_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) { switch (msg->act) { - case ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT: { - esp_ble_gap_cb_param_t *src = (esp_ble_gap_cb_param_t *)p_src; - esp_ble_gap_cb_param_t *dst = (esp_ble_gap_cb_param_t *)p_dest; - uint16_t length = 0; - if (src->get_bond_dev_cmpl.bond_dev) { - length = (src->get_bond_dev_cmpl.dev_num)*sizeof(esp_ble_bond_dev_t); - dst->get_bond_dev_cmpl.bond_dev = (esp_ble_bond_dev_t *)osi_malloc(length); - if (dst->get_bond_dev_cmpl.bond_dev != NULL) { - memcpy(dst->get_bond_dev_cmpl.bond_dev, src->get_bond_dev_cmpl.bond_dev, length); - } else { - LOG_ERROR("%s %d no mem", __func__, msg->act); - } - } - break; - } default: LOG_ERROR("%s, Unhandled deep copy %d\n", __func__, msg->act); break; @@ -1018,13 +939,6 @@ void btc_gap_ble_cb_deep_free(btc_msg_t *msg) { LOG_DEBUG("%s", __func__); switch (msg->act) { - case ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT: { - esp_ble_bond_dev_t *bond_dev = ((esp_ble_gap_cb_param_t *)msg->arg)->get_bond_dev_cmpl.bond_dev; - if (bond_dev) { - osi_free(bond_dev); - } - break; - } default: LOG_DEBUG("Unhandled deep free %d", msg->act); break; @@ -1158,24 +1072,10 @@ void btc_gap_ble_call_handler(btc_msg_t *msg) } case BTC_GAP_BLE_REMOVE_BOND_DEV_EVT: { BD_ADDR bd_addr; - bt_bdaddr_t bt_addr; memcpy(bd_addr, arg->remove_bond_device.bd_addr, sizeof(BD_ADDR)); - memcpy(bt_addr.address, arg->remove_bond_device.bd_addr, sizeof(bt_bdaddr_t)); - LOG_DEBUG("BTC_GAP_BLE_REMOVE_BOND_DEV_EVT"); - if (btc_storage_remove_ble_bonding_keys(&bt_addr) == BT_STATUS_SUCCESS) { - BTA_DmRemoveDevice(bd_addr); - } else { - LOG_ERROR("remove device failed: the address[%x:%x:%x:%x:%x:%x] didn't in the bonding list", bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]); - btc_ble_remove_bond_device(ESP_BT_STATUS_FAIL); - } + BTA_DmRemoveDevice(bd_addr); break; } - case BTC_GAP_BLE_CLEAR_BOND_DEV_EVT: - btc_ble_clear_bond_device(); - break; - case BTC_GAP_BLE_GET_BOND_DEV_EVT: - btc_ble_get_bond_device_list(); - break; #endif ///SMP_INCLUDED == TRUE case BTC_GAP_BLE_DISCONNECT_EVT: btc_ble_disconnect(arg->disconnect.remote_device); diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h b/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h index b6da53606..601167a5a 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h @@ -41,8 +41,6 @@ typedef enum { BTC_GAP_BLE_CONFIRM_REPLY_EVT, BTC_GAP_BLE_DISCONNECT_EVT, BTC_GAP_BLE_REMOVE_BOND_DEV_EVT, - BTC_GAP_BLE_CLEAR_BOND_DEV_EVT, - BTC_GAP_BLE_GET_BOND_DEV_EVT, } btc_gap_ble_act_t; /* btc_ble_gap_args_t */ diff --git a/components/bt/bluedroid/btif/bta_dm_co.c b/components/bt/bluedroid/btif/bta_dm_co.c index bd40a044e..1feb6c332 100644 --- a/components/bt/bluedroid/btif/bta_dm_co.c +++ b/components/bt/bluedroid/btif/bta_dm_co.c @@ -22,7 +22,7 @@ #include "bta_sys.h" #include "bta_dm_co.h" #include "bta_dm_ci.h" -#include "btc_ble_storage.h" +#include "btc_dm.h" #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) #include "bt_utils.h" #if (BTM_OOB_INCLUDED == TRUE) diff --git a/examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c b/examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c index 2bee5d759..12d33f2de 100644 --- a/examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c +++ b/examples/bluetooth/gatt_security_server/main/example_ble_sec_gatts_demo.c @@ -233,6 +233,35 @@ static char *esp_key_type_to_str(esp_ble_key_type_t key_type) return key_str; } +static void show_bonded_devices(void) +{ + int dev_num = esp_ble_get_bond_device_num(); + + esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); + esp_ble_get_bond_device_list(&dev_num, dev_list); + ESP_LOGI(GATTS_TABLE_TAG, "Bonded devices number : %d\n", dev_num); + + ESP_LOGI(GATTS_TABLE_TAG, "Bonded devices list : %d\n", dev_num); + for (int i = 0; i < dev_num; i++) { + esp_log_buffer_hex(GATTS_TABLE_TAG, (void *)dev_list[i].bd_addr, sizeof(esp_bd_addr_t)); + } + + free(dev_list); +} + +static void __attribute__((unused)) remove_all_bonded_devices(void) +{ + int dev_num = esp_ble_get_bond_device_num(); + + esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); + esp_ble_get_bond_device_list(&dev_num, dev_list); + for (int i = 0; i < dev_num; i++) { + esp_ble_remove_bond_device(dev_list[i].bd_addr); + } + + free(dev_list); +} + static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { ESP_LOGV(GATTS_TABLE_TAG, "GAP_EVT, event %d\n", event); @@ -279,23 +308,15 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param (bd_addr[4] << 8) + bd_addr[5]); ESP_LOGI(GATTS_TABLE_TAG, "address type = %d", param->ble_security.auth_cmpl.addr_type); ESP_LOGI(GATTS_TABLE_TAG, "pair status = %s",param->ble_security.auth_cmpl.success ? "success" : "fail"); + show_bonded_devices(); break; } case ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT: { - ESP_LOGI(GATTS_TABLE_TAG, "ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT status = %d", param->remove_bond_dev_cmpl.status); - break; - } - case ESP_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT: { - ESP_LOGI(GATTS_TABLE_TAG, "ESP_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT status = %d", param->clear_bond_dev_cmpl.status); - break; - } - case ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT: { - ESP_LOGI(GATTS_TABLE_TAG, "ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT status = %d, num = %d", param->get_bond_dev_cmpl.status, param->get_bond_dev_cmpl.dev_num); - esp_ble_bond_dev_t *bond_dev = param->get_bond_dev_cmpl.bond_dev; - for(int i = 0; i < param->get_bond_dev_cmpl.dev_num; i++) { - ESP_LOGI(GATTS_TABLE_TAG, "mask = %x", bond_dev[i].bond_key.key_mask); - esp_log_buffer_hex(GATTS_TABLE_TAG, (void *)bond_dev[i].bd_addr, sizeof(esp_bd_addr_t)); - } + ESP_LOGD(GATTS_TABLE_TAG, "ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT status = %d", param->remove_bond_dev_cmpl.status); + ESP_LOGI(GATTS_TABLE_TAG, "ESP_GAP_BLE_REMOVE_BOND_DEV"); + ESP_LOGI(GATTS_TABLE_TAG, "-----ESP_GAP_BLE_REMOVE_BOND_DEV----"); + esp_log_buffer_hex(GATTS_TABLE_TAG, (void *)param->remove_bond_dev_cmpl.bd_addr, sizeof(esp_bd_addr_t)); + ESP_LOGI(GATTS_TABLE_TAG, "------------------------------------"); break; } case ESP_GAP_BLE_SET_LOCAL_PRIVACY_COMPLETE_EVT: @@ -493,6 +514,12 @@ void app_main() esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t)); + /* Just show how to clear all the bonded devices + * Delay 30s, clear all the bonded devices + * + * vTaskDelay(30000 / portTICK_PERIOD_MS); + * remove_all_bonded_devices(); + */ }