From c22c070e073341ccb8921239c78107beb60c179f Mon Sep 17 00:00:00 2001 From: baohongde Date: Thu, 3 May 2018 19:05:57 +0800 Subject: [PATCH] component/bt: Add APIs of get and remove bond device 1. Add APIs of get and remove bond device 2. Add ESP_BT_GAP_AUTH_CMPL_EVT --- components/bt/bluedroid/api/esp_gap_ble_api.c | 10 +-- components/bt/bluedroid/api/esp_gap_bt_api.c | 51 +++++++++++++- .../api/include/api/esp_gap_ble_api.h | 2 +- .../api/include/api/esp_gap_bt_api.h | 51 +++++++++++++- components/bt/bluedroid/btc/core/btc_dm.c | 21 +++++- .../bt/bluedroid/btc/core/btc_storage.c | 68 +++++++++++++++++++ .../bluedroid/btc/include/btc/btc_storage.h | 24 +++++++ .../btc/profile/std/gap/btc_gap_bt.c | 18 ++++- .../btc/profile/std/include/btc_gap_bt.h | 6 ++ examples/bluetooth/a2dp_source/main/main.c | 10 +++ 10 files changed, 251 insertions(+), 10 deletions(-) diff --git a/components/bt/bluedroid/api/esp_gap_ble_api.c b/components/bt/bluedroid/api/esp_gap_ble_api.c index 1089d7236..8ece6ecff 100644 --- a/components/bt/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/bluedroid/api/esp_gap_ble_api.c @@ -35,7 +35,7 @@ esp_err_t esp_ble_gap_config_adv_data(esp_ble_adv_data_t *adv_data) { btc_msg_t msg; btc_ble_gap_args_t arg; - + ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); if (adv_data == NULL) { @@ -60,7 +60,7 @@ esp_err_t esp_ble_gap_set_scan_params(esp_ble_scan_params_t *scan_params) { btc_msg_t msg; btc_ble_gap_args_t arg; - + ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); if (scan_params == NULL) { @@ -353,7 +353,7 @@ esp_err_t esp_ble_gap_get_local_used_addr(esp_bd_addr_t local_used_addr, uint8_t if(esp_bluedroid_get_status() != (ESP_BLUEDROID_STATUS_ENABLED)) { LOG_ERROR("%s, bluedroid status error", __func__); return ESP_FAIL; - } + } if(!BTM_BleGetCurrentAddress(local_used_addr, addr_type)) { return ESP_FAIL; } @@ -542,7 +542,9 @@ esp_err_t esp_ble_remove_bond_device(esp_bd_addr_t bd_addr) int esp_ble_get_bond_device_num(void) { - ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_FAIL; + } return btc_storage_get_num_ble_bond_devices(); } diff --git a/components/bt/bluedroid/api/esp_gap_bt_api.c b/components/bt/bluedroid/api/esp_gap_bt_api.c index 8e357200b..ebe3d5bfc 100644 --- a/components/bt/bluedroid/api/esp_gap_bt_api.c +++ b/components/bt/bluedroid/api/esp_gap_bt_api.c @@ -19,6 +19,7 @@ #include "common/bt_trace.h" #include "btc/btc_manage.h" #include "btc_gap_bt.h" +#include "btc/btc_storage.h" #if (BTC_GAP_BT_INCLUDED == TRUE) @@ -178,11 +179,11 @@ esp_err_t esp_bt_gap_get_cod(esp_bt_cod_t *cod) return btc_gap_bt_get_cod(cod); } + esp_err_t esp_bt_gap_read_rssi_delta(esp_bd_addr_t remote_addr) { btc_msg_t msg; btc_gap_bt_args_t arg; - msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_GAP_BT; msg.act = BTC_GAP_BT_ACT_READ_RSSI_DELTA; @@ -191,4 +192,52 @@ esp_err_t esp_bt_gap_read_rssi_delta(esp_bd_addr_t remote_addr) return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_bt_gap_remove_bond_device(esp_bd_addr_t bd_addr) +{ + btc_msg_t msg; + btc_gap_bt_args_t arg; + + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_ERR_INVALID_STATE; + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GAP_BT; + msg.act = BTC_GAP_BT_ACT_REMOVE_BOND_DEVICE; + + memcpy(arg.rm_bond_device.bda.address, bd_addr, sizeof(esp_bd_addr_t)); + return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +int esp_bt_gap_get_bond_device_num(void) +{ + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_FAIL; + } + return btc_storage_get_num_bt_bond_devices(); +} + +esp_err_t esp_bt_gap_get_bond_device_list(int *dev_num, esp_bd_addr_t *dev_list) +{ + int ret; + int dev_num_total; + + if (dev_num == NULL || dev_list == NULL) { + return ESP_ERR_INVALID_ARG; + } + + if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { + return ESP_ERR_INVALID_STATE; + } + + dev_num_total = btc_storage_get_num_bt_bond_devices(); + if (*dev_num > dev_num_total) { + *dev_num = dev_num_total; + } + + ret = btc_storage_get_bonded_bt_devices_list((bt_bdaddr_t *)dev_list, *dev_num); + + return (ret == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + #endif /* #if BTC_GAP_BT_INCLUDED == TRUE */ diff --git a/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h b/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h index 5bf7e0b13..08feb13ca 100644 --- a/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h +++ b/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h @@ -1056,7 +1056,7 @@ esp_err_t esp_ble_remove_bond_device(esp_bd_addr_t bd_addr); * It will return the device bonded number immediately. * * @return - >= 0 : bonded devices number. -* - < 0 : failed +* - ESP_FAIL : failed * */ int esp_ble_get_bond_device_num(void); diff --git a/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h b/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h index 8631bac76..d30767e72 100644 --- a/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h +++ b/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h @@ -148,7 +148,8 @@ typedef enum { ESP_BT_GAP_DISC_STATE_CHANGED_EVT, /*!< discovery state changed event */ ESP_BT_GAP_RMT_SRVCS_EVT, /*!< get remote services event */ ESP_BT_GAP_RMT_SRVC_REC_EVT, /*!< get remote service record event */ - ESP_BT_GAP_READ_RSSI_DELTA_EVT, /*!< read rssi event */ + ESP_BT_GAP_AUTH_CMPL_EVT, /*!< AUTH complete event */ + ESP_BT_GAP_READ_RSSI_DELTA_EVT, /*!< read rssi event */ ESP_BT_GAP_EVT_MAX, } esp_bt_gap_cb_event_t; @@ -206,6 +207,15 @@ typedef union { esp_bt_status_t stat; /*!< read rssi status */ int8_t rssi_delta; /*!< rssi delta value range -128 ~127, The value zero indicates that the RSSI is inside the Golden Receive Power Range, the Golden Receive Power Range is from ESP_BT_GAP_RSSI_LOW_THRLD to ESP_BT_GAP_RSSI_HIGH_THRLD */ } read_rssi_delta; /*!< read rssi parameter struct */ + + /** + * @brief ESP_BT_GAP_AUTH_CMPL_EVT + */ + struct auth_cmpl_param { + esp_bd_addr_t bda; /*!< remote bluetooth device address*/ + esp_bt_status_t stat; /*!< authentication complete status */ + uint8_t device_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1]; /*!< device name */ + } auth_cmpl; /*!< authentication complete parameter struct */ } esp_bt_gap_cb_param_t; /** @@ -398,6 +408,45 @@ esp_err_t esp_bt_gap_get_cod(esp_bt_cod_t *cod); */ esp_err_t esp_bt_gap_read_rssi_delta(esp_bd_addr_t remote_addr); +/** +* @brief Removes a device from the security database list of +* peer device. +* +* @param[in] bd_addr : BD address of the peer device +* +* @return - ESP_OK : success +* - ESP_FAIL : failed +* +*/ +esp_err_t esp_bt_gap_remove_bond_device(esp_bd_addr_t bd_addr); + +/** +* @brief Get the device number from the security database list of peer device. +* It will return the device bonded number immediately. +* +* @return - >= 0 : bonded devices number. +* - ESP_FAIL : failed +* +*/ +int esp_bt_gap_get_bond_device_num(void); + +/** +* @brief Get the device from the security database list of peer device. +* 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(). +* +* @param[out] dev_list: an array(buffer) of `esp_bd_addr_t` type. Use for storing the bonded devices address. +* The dev_list should be allocated by who call this API. +* +* @return +* - ESP_OK : Succeed +* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled +* - ESP_FAIL: others +*/ +esp_err_t esp_bt_gap_get_bond_device_list(int *dev_num, esp_bd_addr_t *dev_list); + #ifdef __cplusplus } #endif diff --git a/components/bt/bluedroid/btc/core/btc_dm.c b/components/bt/bluedroid/btc/core/btc_dm.c index ff82b45f5..d6d41e234 100644 --- a/components/bt/bluedroid/btc/core/btc_dm.c +++ b/components/bt/bluedroid/btc/core/btc_dm.c @@ -27,11 +27,20 @@ #include "bta/bta_api.h" #include "bta/bta_gatt_api.h" #include "osi/allocator.h" +#include "btc/btc_manage.h" + #if (BTC_GAP_BT_INCLUDED == TRUE) #include "btc_gap_bt.h" -#endif /* BTC_GAP_BT_INCLUDED == TRUE */ +static inline void btc_gap_bt_cb_to_app(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) +{ + esp_bt_gap_cb_t cb = (esp_bt_gap_cb_t)btc_profile_cb_get(BTC_PID_GAP_BT); + if (cb) { + cb(event, param); + } +} +#endif /* BTC_GAP_BT_INCLUDED == TRUE */ /****************************************************************************** ** Constants & Macros ******************************************************************************/ @@ -309,6 +318,7 @@ static void btc_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) // Skip SDP for certain HID Devices if (p_auth_cmpl->success) { + status = BT_STATUS_SUCCESS; } else { // Map the HCI fail reason to bt status switch (p_auth_cmpl->fail_reason) { @@ -348,6 +358,13 @@ static void btc_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl) status = BT_STATUS_FAIL; } } +#if (BTC_GAP_BT_INCLUDED == TRUE) + esp_bt_gap_cb_param_t param; + param.auth_cmpl.stat = status; + memcpy(param.auth_cmpl.bda, p_auth_cmpl->bd_addr, ESP_BD_ADDR_LEN); + memcpy(param.auth_cmpl.device_name, p_auth_cmpl->bd_name, ESP_BT_GAP_MAX_BDNAME_LEN + 1); + btc_gap_bt_cb_to_app(ESP_BT_GAP_AUTH_CMPL_EVT, ¶m); +#endif /* BTC_GAP_BT_INCLUDED == TRUE */ (void) status; } @@ -467,7 +484,7 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg) #if (SMP_INCLUDED == TRUE) bt_bdaddr_t bd_addr; rsp_app = true; - LOG_ERROR("BTA_DM_DEV_UNPAIRED_EVT"); + LOG_DEBUG("BTA_DM_DEV_UNPAIRED_EVT"); 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); param.remove_bond_dev_cmpl.status = ESP_BT_STATUS_FAIL; diff --git a/components/bt/bluedroid/btc/core/btc_storage.c b/components/bt/bluedroid/btc/core/btc_storage.c index 4109bb7a8..5114a6bef 100644 --- a/components/bt/bluedroid/btc/core/btc_storage.c +++ b/components/bt/bluedroid/btc/core/btc_storage.c @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +#include #include "btc/btc_storage.h" #include "btc/btc_util.h" @@ -165,3 +166,70 @@ bt_status_t btc_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr) return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } + +/******************************************************************************* +** +** Function btc_storage_get_num_bt_bond_devices +** +** Description BTC storage API - get the num of the bonded device from NVRAM +** +** Returns the num of the bonded device +** +*******************************************************************************/ +int btc_storage_get_num_bt_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) && + btc_config_exist(name, BTC_STORAGE_LINK_KEY_TYPE_STR) && + btc_config_exist(name, BTC_STORAGE_PIN_LENGTH_STR) && + btc_config_exist(name, BTC_STORAGE_LINK_KEY_STR)) { + num_dev++; + } + } + btc_config_unlock(); + + return num_dev; +} + +/******************************************************************************* +** +** Function btc_storage_get_bonded_bt_devices_list +** +** Description BTC storage API - get the list of the bonded device from NVRAM +** +** Returns BT_STATUS_SUCCESS if get the list successful, +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ +bt_status_t btc_storage_get_bonded_bt_devices_list(bt_bdaddr_t *bond_dev, int dev_num) +{ + bt_bdaddr_t bd_addr; + + btc_config_lock(); + for (const btc_config_section_iter_t *iter = btc_config_section_begin(); iter != btc_config_section_end(); + iter = btc_config_section_next(iter)) { + + if (dev_num-- <= 0) { + break; + } + + const char *name = btc_config_section_name(iter); + + if (string_is_bdaddr(name) && + btc_config_exist(name, BTC_STORAGE_LINK_KEY_TYPE_STR) && + btc_config_exist(name, BTC_STORAGE_PIN_LENGTH_STR) && + btc_config_exist(name, BTC_STORAGE_LINK_KEY_STR)) { + string_to_bdaddr(name, &bd_addr); + memcpy(bond_dev, &bd_addr, sizeof(bt_bdaddr_t)); + bond_dev++; + } + } + btc_config_unlock(); + + return BT_STATUS_SUCCESS; +} \ No newline at end of file diff --git a/components/bt/bluedroid/btc/include/btc/btc_storage.h b/components/bt/bluedroid/btc/include/btc/btc_storage.h index 5e54cb705..f40b169d4 100644 --- a/components/bt/bluedroid/btc/include/btc/btc_storage.h +++ b/components/bt/bluedroid/btc/include/btc/btc_storage.h @@ -18,6 +18,7 @@ #include #include "common/bt_defs.h" #include "stack/bt_types.h" +#include "esp_gap_bt_api.h" #define BTC_STORAGE_DEV_CLASS_STR "DevClass" @@ -65,4 +66,27 @@ bt_status_t btc_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr); *******************************************************************************/ bt_status_t btc_storage_load_bonded_devices(void); +/******************************************************************************* +** +** Function btc_storage_get_num_bt_bond_devices +** +** Description BTC storage API - get the num of the bonded device from NVRAM +** +** Returns the num of the bonded device +** +*******************************************************************************/ +int btc_storage_get_num_bt_bond_devices(void); + +/******************************************************************************* +** +** Function btc_storage_get_bonded_bt_devices_list +** +** Description BTC storage API - get the list of the bonded device from NVRAM +** +** Returns BT_STATUS_SUCCESS if get the list successful, +** BT_STATUS_FAIL otherwise +** +*******************************************************************************/ +bt_status_t btc_storage_get_bonded_bt_devices_list(bt_bdaddr_t *bond_dev, int dev_num); + #endif /* BTC_STORAGE_H */ diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c index ad588568a..c612009c1 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c @@ -16,6 +16,7 @@ #include "esp_bt_defs.h" #include "esp_gap_bt_api.h" #include "btc_gap_bt.h" +#include "btc/btc_storage.h" #include "bta/bta_api.h" #include "common/bt_trace.h" #include "common/bt_target.h" @@ -620,6 +621,17 @@ static void btc_gap_bt_read_rssi_delta(btc_gap_bt_args_t *arg) BTA_DmBleReadRSSI(arg->read_rssi_delta.bda.address, btc_gap_bt_read_rssi_delta_cmpl_callback); } +esp_err_t btc_gap_bt_remove_bond_device(btc_gap_bt_args_t *arg) +{ + BD_ADDR bd_addr; + memcpy(bd_addr, arg->rm_bond_device.bda.address, sizeof(BD_ADDR)); + if(BTA_DmRemoveDevice(bd_addr) == BTA_SUCCESS){ + btc_storage_remove_bonded_device(&(arg->rm_bond_device.bda)); + return ESP_BT_STATUS_SUCCESS; + } + return ESP_BT_STATUS_FAIL; +} + void btc_gap_bt_call_handler(btc_msg_t *msg) { btc_gap_bt_args_t *arg = (btc_gap_bt_args_t *)msg->arg; @@ -665,6 +677,10 @@ void btc_gap_bt_call_handler(btc_msg_t *msg) btc_gap_bt_read_rssi_delta(msg->arg); break; } + case BTC_GAP_BT_ACT_REMOVE_BOND_DEVICE:{ + btc_gap_bt_remove_bond_device(msg->arg); + break; + } default: break; } @@ -697,6 +713,6 @@ void btc_gap_bt_cb_handler(btc_msg_t *msg) } else { LOG_ERROR("%s, unknow msg->act = %d", __func__, msg->act); } - + } #endif /* (BTC_GAP_BT_INCLUDED == TRUE) */ diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h b/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h index 4ca1803fd..d33ab9182 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h @@ -35,6 +35,7 @@ typedef enum { BTC_GAP_BT_ACT_SEARCH_SERVICE_RECORD, BTC_GAP_BT_ACT_SET_COD, BTC_GAP_BT_ACT_READ_RSSI_DELTA, + BTC_GAP_BT_ACT_REMOVE_BOND_DEVICE, } btc_gap_bt_act_t; /* btc_bt_gap_args_t */ @@ -70,6 +71,11 @@ typedef union { struct bt_read_rssi_delta_args { bt_bdaddr_t bda; } read_rssi_delta; + + // BTC_GAP_BT_ACT_REMOVE_BOND_DEVICE + struct rm_bond_device_args { + bt_bdaddr_t bda; + } rm_bond_device; } btc_gap_bt_args_t; void btc_gap_bt_call_handler(btc_msg_t *msg); diff --git a/examples/bluetooth/a2dp_source/main/main.c b/examples/bluetooth/a2dp_source/main/main.c index e6dd46f63..a03080923 100644 --- a/examples/bluetooth/a2dp_source/main/main.c +++ b/examples/bluetooth/a2dp_source/main/main.c @@ -244,6 +244,16 @@ void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) } case ESP_BT_GAP_RMT_SRVCS_EVT: case ESP_BT_GAP_RMT_SRVC_REC_EVT: + break; + case ESP_BT_GAP_AUTH_CMPL_EVT:{ + if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) { + ESP_LOGI(BT_AV_TAG, "authentication success: %s", param->auth_cmpl.device_name); + esp_log_buffer_hex(BT_AV_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN); + } else { + ESP_LOGI(BT_AV_TAG, "authentication failed, status:%d", param->auth_cmpl.stat); + } + } + default: { ESP_LOGI(BT_AV_TAG, "event: %d", event); break;