From aa236809ad72139b19e3b43a541cb5686582456d Mon Sep 17 00:00:00 2001 From: zhiweijian Date: Mon, 26 Mar 2018 11:40:14 +0800 Subject: [PATCH] Component/bt: fix no set rand add callback event --- components/bt/bluedroid/bta/dm/bta_dm_act.c | 10 ++-- components/bt/bluedroid/bta/dm/bta_dm_api.c | 5 +- components/bt/bluedroid/bta/include/bta_api.h | 4 +- .../bt/bluedroid/bta/include/bta_dm_int.h | 1 + .../btc/profile/std/gap/btc_gap_ble.c | 52 ++++++++++++------- components/bt/bluedroid/stack/btm/btm_ble.c | 9 +++- .../bt/bluedroid/stack/btm/btm_ble_gap.c | 38 ++++++++++---- .../bt/bluedroid/stack/include/btm_api.h | 4 ++ .../bt/bluedroid/stack/include/btm_ble_api.h | 2 +- .../bt/bluedroid/stack/include/btm_ble_int.h | 1 + .../bt/bluedroid/stack/include/hcidefs.h | 3 ++ 11 files changed, 90 insertions(+), 39 deletions(-) diff --git a/components/bt/bluedroid/bta/dm/bta_dm_act.c b/components/bt/bluedroid/bta/dm/bta_dm_act.c index 0a14a35ac..f7cbcbcfa 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_act.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_act.c @@ -4625,14 +4625,18 @@ void bta_dm_ble_disconnect (tBTA_DM_MSG *p_data) *******************************************************************************/ void bta_dm_ble_set_rand_address(tBTA_DM_MSG *p_data) { - BOOLEAN set_flag = false; + tBTM_STATUS status = BTM_SET_STATIC_RAND_ADDR_FAIL; if (p_data->set_addr.addr_type != BLE_ADDR_RANDOM) { APPL_TRACE_ERROR("Invalid random adress type = %d\n", p_data->set_addr.addr_type); + if(p_data->set_addr.p_set_rand_addr_cback) { + (*p_data->set_addr.p_set_rand_addr_cback)(status); + } return; } //send the setting random address to BTM layer - if ((set_flag = BTM_BleSetRandAddress(p_data->set_addr.address) != TRUE)){ - APPL_TRACE_ERROR("%s,set random address fail.", __func__); + status = BTM_BleSetRandAddress(p_data->set_addr.address); + if(p_data->set_addr.p_set_rand_addr_cback) { + (*p_data->set_addr.p_set_rand_addr_cback)(status); } } diff --git a/components/bt/bluedroid/bta/dm/bta_dm_api.c b/components/bt/bluedroid/bta/dm/bta_dm_api.c index 1af6ee7ed..060692fcd 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_api.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_api.c @@ -2284,12 +2284,12 @@ extern void BTA_DmBleStopAdvertising(void) ** Description This function set the random address for the APP ** ** Parameters rand_addr: the random address whith should be setting -** +** p_set_rand_addr_cback: complete callback ** Returns void ** ** *******************************************************************************/ -extern void BTA_DmSetRandAddress(BD_ADDR rand_addr) +extern void BTA_DmSetRandAddress(BD_ADDR rand_addr, tBTA_SET_RAND_ADDR_CBACK *p_set_rand_addr_cback) { tBTA_DM_APT_SET_DEV_ADDR *p_msg; APPL_TRACE_API("set the random address "); @@ -2298,6 +2298,7 @@ extern void BTA_DmSetRandAddress(BD_ADDR rand_addr) memcpy(p_msg->address, rand_addr, BD_ADDR_LEN); p_msg->hdr.event = BTA_DM_API_SET_RAND_ADDR_EVT; p_msg->addr_type = BLE_ADDR_RANDOM; + p_msg->p_set_rand_addr_cback = p_set_rand_addr_cback; //start sent the msg to the bta system control moudle bta_sys_sendmsg(p_msg); } diff --git a/components/bt/bluedroid/bta/include/bta_api.h b/components/bt/bluedroid/bta/include/bta_api.h index a5a7edd26..a13c08227 100644 --- a/components/bt/bluedroid/bta/include/bta_api.h +++ b/components/bt/bluedroid/bta/include/bta_api.h @@ -409,6 +409,8 @@ typedef tBTM_ADD_WHITELIST_CBACK tBTA_ADD_WHITELIST_CBACK; typedef tBTM_SET_PKT_DATA_LENGTH_CBACK tBTA_SET_PKT_DATA_LENGTH_CBACK; +typedef tBTM_SET_RAND_ADDR_CBACK tBTA_SET_RAND_ADDR_CBACK; + typedef tBTM_SET_LOCAL_PRIVACY_CBACK tBTA_SET_LOCAL_PRIVACY_CBACK; typedef tBTM_CMPL_CB tBTA_CMPL_CB; @@ -2060,7 +2062,7 @@ extern void BTA_DmBleScan(BOOLEAN start, UINT32 duration, extern void BTA_DmBleStopAdvertising(void); -extern void BTA_DmSetRandAddress(BD_ADDR rand_addr); +extern void BTA_DmSetRandAddress(BD_ADDR rand_addr, tBTA_SET_RAND_ADDR_CBACK *p_set_rand_addr_cback); #endif diff --git a/components/bt/bluedroid/bta/include/bta_dm_int.h b/components/bt/bluedroid/bta/include/bta_dm_int.h index 10f21726d..a95bb676e 100644 --- a/components/bt/bluedroid/bta/include/bta_dm_int.h +++ b/components/bt/bluedroid/bta/include/bta_dm_int.h @@ -532,6 +532,7 @@ typedef struct { BT_HDR hdr; tBLE_ADDR_TYPE addr_type; BD_ADDR address; + tBTA_SET_RAND_ADDR_CBACK *p_set_rand_addr_cback; } tBTA_DM_APT_SET_DEV_ADDR; /* set adv parameter for BLE advertising */ 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 761fc76d4..19c47b607 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 @@ -115,6 +115,9 @@ static esp_bt_status_t btc_hci_to_esp_status(uint8_t hci_status) case HCI_SUCCESS: esp_status = ESP_BT_STATUS_SUCCESS; break; + case HCI_ERR_ESP_VENDOR_FAIL: + esp_status = ESP_BT_STATUS_FAIL; + break; case HCI_ERR_HOST_TIMEOUT: esp_status = ESP_BT_STATUS_TIMEOUT; break; @@ -169,6 +172,12 @@ static esp_bt_status_t btc_btm_status_to_esp_status (uint8_t btm_status) case BTM_SET_PRIVACY_FAIL: esp_status = ESP_BT_STATUS_FAIL; break; + case BTM_INVALID_STATIC_RAND_ADDR: + esp_status = ESP_BT_STATUS_INVALID_STATIC_RAND_ADDR; + break; + case BTM_SET_STATIC_RAND_ADDR_FAIL: + esp_status = ESP_BT_STATUS_FAIL; + break; default: esp_status = ESP_BT_STATUS_FAIL; break; @@ -714,6 +723,24 @@ static void btc_add_whitelist_complete_callback(UINT8 status, tBTM_WL_OPERATION } } +static void btc_set_rand_addr_callback(UINT8 status) +{ + esp_ble_gap_cb_param_t param; + bt_status_t ret; + btc_msg_t msg; + param.set_rand_addr_cmpl.status = btc_btm_status_to_esp_status(status); //todo status + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_GAP_BLE; + msg.act = ESP_GAP_BLE_SET_STATIC_RAND_ADDR_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\n", __func__); + } + +} + static void btc_set_local_privacy_callback(UINT8 status) { esp_ble_gap_cb_param_t param; @@ -821,13 +848,8 @@ static void btc_ble_set_pkt_data_len(BD_ADDR remote_device, uint16_t tx_data_len BTA_DmBleSetDataLength(remote_device, tx_data_length, p_set_pkt_data_cback); } -static void btc_ble_set_rand_addr (BD_ADDR rand_addr) +static void btc_ble_set_rand_addr (BD_ADDR rand_addr, tBTA_SET_RAND_ADDR_CBACK *p_set_rand_addr_cback) { - esp_ble_gap_cb_param_t param; - bt_status_t ret; - btc_msg_t msg; - param.set_rand_addr_cmpl.status = ESP_BT_STATUS_SUCCESS; - if (rand_addr != NULL) { /* A static address is a 48-bit randomly generated address and shall meet the following requirements: @@ -842,25 +864,15 @@ static void btc_ble_set_rand_addr (BD_ADDR rand_addr) if((rand_addr[0] & BT_STATIC_RAND_ADDR_MASK) == BT_STATIC_RAND_ADDR_MASK && memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) != 0 && memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) != 0){ - BTA_DmSetRandAddress(rand_addr); + BTA_DmSetRandAddress(rand_addr, btc_set_rand_addr_callback); } else { - param.set_rand_addr_cmpl.status = ESP_BT_STATUS_INVALID_STATIC_RAND_ADDR; + btc_set_rand_addr_callback(BTM_INVALID_STATIC_RAND_ADDR); LOG_ERROR("Invalid random address, the high bit should be 0b11, all bits of the random part shall not be to 1 or 0"); } } else { - param.set_rand_addr_cmpl.status = ESP_BT_STATUS_INVALID_STATIC_RAND_ADDR; + btc_set_rand_addr_callback(BTM_INVALID_STATIC_RAND_ADDR); LOG_ERROR("Invalid random addressm, the address value is NULL"); } - - msg.sig = BTC_SIG_API_CB; - msg.pid = BTC_PID_GAP_BLE; - msg.act = ESP_GAP_BLE_SET_STATIC_RAND_ADDR_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\n", __func__); - } } static void btc_ble_config_local_privacy(bool privacy_enable, tBTA_SET_LOCAL_PRIVACY_CBACK *set_local_privacy_cback) @@ -1057,7 +1069,7 @@ void btc_gap_ble_call_handler(btc_msg_t *msg) case BTC_GAP_BLE_ACT_SET_RAND_ADDRESS: { BD_ADDR bd_addr; memcpy(bd_addr, arg->set_rand_addr.rand_addr, sizeof(BD_ADDR)); - btc_ble_set_rand_addr(bd_addr); + btc_ble_set_rand_addr(bd_addr, btc_set_rand_addr_callback); break; } case BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY: diff --git a/components/bt/bluedroid/stack/btm/btm_ble.c b/components/bt/bluedroid/stack/btm/btm_ble.c index 79b93aaed..87da0c4fa 100644 --- a/components/bt/bluedroid/stack/btm/btm_ble.c +++ b/components/bt/bluedroid/stack/btm/btm_ble.c @@ -1937,7 +1937,14 @@ void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len, BOOLEAN enhanced) handle = HCID_GET_HANDLE (handle); btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match); - + if(role == HCI_ROLE_SLAVE) { + //clear p_cb->state, controller will stop adv when ble connected. + tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var; + if(p_cb) { + p_cb->adv_mode = BTM_BLE_ADV_DISABLE; + p_cb->state = BTM_BLE_STOP_ADV; + } + } l2cble_conn_comp (handle, role, bda, bda_type, conn_interval, conn_latency, conn_timeout); diff --git a/components/bt/bluedroid/stack/btm/btm_ble_gap.c b/components/bt/bluedroid/stack/btm/btm_ble_gap.c index 14c60f44e..b5faafe16 100644 --- a/components/bt/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/bluedroid/stack/btm/btm_ble_gap.c @@ -756,10 +756,18 @@ BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode, tBTM_SET_LOCAL_PRIVACY_CBACK if (p_cb->privacy_mode == BTM_PRIVACY_NONE && random_cb->own_addr_type == BLE_ADDR_RANDOM) { BTM_TRACE_ERROR("Have set random adress, can't set privacy "); + if (random_cb && random_cb->set_local_privacy_cback){ + (*random_cb->set_local_privacy_cback)(BTM_SET_PRIVACY_FAIL); + random_cb->set_local_privacy_cback = NULL; + } return FALSE; } - if (!(p_cb->inq_var.state != BTM_BLE_STOP_SCAN && p_cb->inq_var.state != BTM_BLE_STOP_ADV)) { + if (!(p_cb->inq_var.state == BTM_BLE_STOP_SCAN || p_cb->inq_var.state == BTM_BLE_STOP_ADV || p_cb->inq_var.state == BTM_BLE_IDLE)) { BTM_TRACE_ERROR("Advertising or scaning now, can't set privacy "); + if (random_cb && random_cb->set_local_privacy_cback){ + (*random_cb->set_local_privacy_cback)(BTM_SET_PRIVACY_FAIL); + random_cb->set_local_privacy_cback = NULL; + } return FALSE; } @@ -1199,11 +1207,18 @@ tBTM_STATUS BTM_BleSetAdvParamsStartAdv(UINT16 adv_int_min, UINT16 adv_int_max, */ if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM && own_bda_type == BLE_ADDR_PUBLIC) { BTM_TRACE_ERROR ("own_addr_type is BLE_ADDR_RANDOM but use BLE_ADDR_PUBLIC\n"); + if(adv_cb) { + (* adv_cb)(HCI_ERR_ESP_VENDOR_FAIL); + } return BTM_ILLEGAL_VALUE; } if (!BTM_BLE_ISVALID_PARAM(adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) || !BTM_BLE_ISVALID_PARAM(adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX)) { + BTM_TRACE_ERROR ("adv_int_min or adv_int_max is invalid\n"); + if(adv_cb) { + (* adv_cb)(HCI_ERR_ESP_VENDOR_FAIL); + } return BTM_ILLEGAL_VALUE; } @@ -1558,12 +1573,10 @@ tBTM_STATUS BTM_BleWriteAdvDataRaw(UINT8 *p_raw_adv, UINT32 raw_adv_len) ** Returns void ** *******************************************************************************/ -BOOLEAN BTM_BleSetRandAddress(BD_ADDR rand_addr) +tBTM_STATUS BTM_BleSetRandAddress(BD_ADDR rand_addr) { - BOOLEAN set_flag = false; - if (rand_addr == NULL) - return set_flag; + return BTM_SET_STATIC_RAND_ADDR_FAIL; /* * Temporary solutions for pair with random address: @@ -1573,19 +1586,22 @@ BOOLEAN BTM_BleSetRandAddress(BD_ADDR rand_addr) #if BLE_PRIVACY_SPT == TRUE if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { BTM_TRACE_ERROR("privacy_mode is not BTM_PRIVACY_NONE "); - return set_flag; + return BTM_SET_STATIC_RAND_ADDR_FAIL; } #endif - if (!(btm_cb.ble_ctr_cb.inq_var.state != BTM_BLE_STOP_SCAN && btm_cb.ble_ctr_cb.inq_var.state != BTM_BLE_STOP_ADV)) { - BTM_TRACE_ERROR("Advertising or scaning now, can't set randaddress "); - return FALSE; + if (!(btm_cb.ble_ctr_cb.inq_var.state == BTM_BLE_STOP_SCAN || btm_cb.ble_ctr_cb.inq_var.state == BTM_BLE_STOP_ADV || btm_cb.ble_ctr_cb.inq_var.state == BTM_BLE_IDLE)) { + BTM_TRACE_ERROR("Advertising or scaning now, can't set randaddress %d", btm_cb.ble_ctr_cb.inq_var.state); + return BTM_SET_STATIC_RAND_ADDR_FAIL; } memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, rand_addr, BD_ADDR_LEN); btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; //send the set random address to the controller - set_flag = btsnd_hcic_ble_set_random_addr(rand_addr); - return set_flag; + if(btsnd_hcic_ble_set_random_addr(rand_addr)) { + return BTM_SUCCESS; + } else { + return BTM_SET_STATIC_RAND_ADDR_FAIL; + } } /******************************************************************************* diff --git a/components/bt/bluedroid/stack/include/btm_api.h b/components/bt/bluedroid/stack/include/btm_api.h index ec27a5963..b685cdb77 100644 --- a/components/bt/bluedroid/stack/include/btm_api.h +++ b/components/bt/bluedroid/stack/include/btm_api.h @@ -72,6 +72,8 @@ enum { BTM_CONTROL_LE_DATA_LEN_UNSUPPORTED,/* 22 controller setting data length is unsupported*/ BTM_SET_PRIVACY_SUCCESS, /* 23 enable/disable local privacy success */ BTM_SET_PRIVACY_FAIL, /* 24 enable/disable local privacy failed*/ + BTM_SET_STATIC_RAND_ADDR_FAIL, /* 25 Command failed */ + BTM_INVALID_STATIC_RAND_ADDR, /* 26 invalid static rand addr */ }; typedef uint8_t tBTM_STATUS; @@ -182,6 +184,8 @@ typedef void (tBTM_UPDATE_CONN_PARAM_CBACK) (UINT8 status, BD_ADDR bd_addr, tBTM typedef void (tBTM_SET_PKT_DATA_LENGTH_CBACK) (UINT8 status, tBTM_LE_SET_PKT_DATA_LENGTH_PARAMS *data_length_params); +typedef void (tBTM_SET_RAND_ADDR_CBACK) (UINT8 status); + typedef void (tBTM_ADD_WHITELIST_CBACK) (UINT8 status, tBTM_WL_OPERATION wl_opration); typedef void (tBTM_SET_LOCAL_PRIVACY_CBACK) (UINT8 status); diff --git a/components/bt/bluedroid/stack/include/btm_ble_api.h b/components/bt/bluedroid/stack/include/btm_ble_api.h index 0587b65be..6d6837a3d 100644 --- a/components/bt/bluedroid/stack/include/btm_ble_api.h +++ b/components/bt/bluedroid/stack/include/btm_ble_api.h @@ -982,7 +982,7 @@ tBTM_STATUS BTM_BleWriteAdvData(tBTM_BLE_AD_MASK data_mask, tBTM_STATUS BTM_BleWriteAdvDataRaw(UINT8 *p_raw_adv, UINT32 raw_adv_len); -BOOLEAN BTM_BleSetRandAddress(BD_ADDR rand_addr); +tBTM_STATUS BTM_BleSetRandAddress(BD_ADDR rand_addr); /******************************************************************************* diff --git a/components/bt/bluedroid/stack/include/btm_ble_int.h b/components/bt/bluedroid/stack/include/btm_ble_int.h index a88108d73..e1f8f400d 100644 --- a/components/bt/bluedroid/stack/include/btm_ble_int.h +++ b/components/bt/bluedroid/stack/include/btm_ble_int.h @@ -104,6 +104,7 @@ typedef UINT8 tBTM_BLE_SEC_REQ_ACT; #define BTM_VSC_CHIP_CAPABILITY_M_VERSION 95 typedef enum { + BTM_BLE_IDLE, BTM_BLE_SCANNING, BTM_BLE_SCAN_PENDING, BTM_BLE_STOP_SCAN, diff --git a/components/bt/bluedroid/stack/include/hcidefs.h b/components/bt/bluedroid/stack/include/hcidefs.h index 87dec34a0..44fff2e52 100644 --- a/components/bt/bluedroid/stack/include/hcidefs.h +++ b/components/bt/bluedroid/stack/include/hcidefs.h @@ -792,6 +792,9 @@ #define HCI_ERR_MAX_ERR 0x43 +//ESP vendor error code +#define HCI_ERR_ESP_VENDOR_FAIL 0xE0 + #define HCI_HINT_TO_RECREATE_AMP_PHYS_LINK 0xFF /*