Merge branch 'feature/btdm_get_and_remove_bond_device' into 'master'

component/bt: Add APIs of get and remove bond device

See merge request idf/esp-idf!2323
This commit is contained in:
Jiang Jiang Jian 2018-05-03 20:58:51 +08:00
commit 04384cb37a
10 changed files with 251 additions and 10 deletions

View file

@ -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();
}

View file

@ -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 */

View file

@ -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);

View file

@ -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

View file

@ -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, &param);
#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;

View file

@ -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 <string.h>
#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;
}

View file

@ -18,6 +18,7 @@
#include <stdint.h>
#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 */

View file

@ -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) */

View file

@ -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);

View file

@ -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;