From 9bdf5455999bbf0115471ee79605223645ea37f0 Mon Sep 17 00:00:00 2001 From: zwj Date: Mon, 27 Aug 2018 21:32:39 +0800 Subject: [PATCH] component/bt: Fix adv/scan not support RPA_public and RPA_random --- components/bt/bluedroid/api/esp_gap_ble_api.c | 1 - .../api/include/api/esp_gap_ble_api.h | 6 +- components/bt/bluedroid/bta/dm/bta_dm_act.c | 8 + components/bt/bluedroid/bta/dm/bta_dm_api.c | 10 + components/bt/bluedroid/bta/dm/bta_dm_main.c | 3 +- .../bt/bluedroid/bta/dm/include/bta_dm_int.h | 7 + .../bt/bluedroid/bta/include/bta/bta_api.h | 1 + .../btc/profile/std/gap/btc_gap_ble.c | 9 + .../btc/profile/std/include/btc_gap_ble.h | 1 + .../bt/bluedroid/stack/btm/btm_ble_addr.c | 10 +- .../bt/bluedroid/stack/btm/btm_ble_gap.c | 193 +++++++++++++----- components/bt/bluedroid/stack/btm/btm_dev.c | 4 + .../bluedroid/stack/btm/include/btm_ble_int.h | 6 + .../stack/include/stack/btm_ble_api.h | 2 + components/bt/bluedroid/stack/l2cap/l2c_ble.c | 2 +- .../bt/bluedroid/stack/l2cap/l2c_utils.c | 8 +- components/bt/bluedroid/stack/smp/smp_utils.c | 12 +- 17 files changed, 216 insertions(+), 67 deletions(-) diff --git a/components/bt/bluedroid/api/esp_gap_ble_api.c b/components/bt/bluedroid/api/esp_gap_ble_api.c index 0dcd89cfe..be3305f52 100644 --- a/components/bt/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/bluedroid/api/esp_gap_ble_api.c @@ -179,7 +179,6 @@ esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr) return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } - esp_err_t esp_ble_gap_config_local_privacy (bool privacy_enable) { btc_msg_t msg; 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 86fa62241..908a5f174 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 @@ -838,10 +838,8 @@ esp_err_t esp_ble_gap_update_conn_params(esp_ble_conn_update_params_t *params); */ esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_data_length); - - /** - * @brief This function set the random address for the application + * @brief This function sets the random address for the application * * @param[in] rand_addr: the random address which should be setting * @@ -852,8 +850,6 @@ esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_ */ esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr); - - /** * @brief Enable/disable privacy on the local device * diff --git a/components/bt/bluedroid/bta/dm/bta_dm_act.c b/components/bt/bluedroid/bta/dm/bta_dm_act.c index 05d5e6ef2..81aa260ae 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_act.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_act.c @@ -770,6 +770,8 @@ void bta_dm_remove_device(tBTA_DM_MSG *p_data) if (continue_delete_dev) { bta_dm_process_remove_device(p_dev->bd_addr, transport); } + + BTM_ClearInqDb (p_dev->bd_addr); } /******************************************************************************* @@ -4653,6 +4655,12 @@ void bta_dm_ble_set_rand_address(tBTA_DM_MSG *p_data) } +void bta_dm_ble_clear_rand_address(tBTA_DM_MSG *p_data) +{ + UNUSED(p_data); + BTM_BleClearRandAddress(); +} + /******************************************************************************* ** ** Function bta_dm_ble_stop_advertising diff --git a/components/bt/bluedroid/bta/dm/bta_dm_api.c b/components/bt/bluedroid/bta/dm/bta_dm_api.c index 717723722..eb57029f5 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_api.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_api.c @@ -2371,6 +2371,16 @@ extern void BTA_DmSetRandAddress(BD_ADDR rand_addr, tBTA_SET_RAND_ADDR_CBACK *p_ } } +void BTA_DmClearRandAddress(void) +{ + tBTA_DM_APT_CLEAR_ADDR *p_msg; + if ((p_msg = (tBTA_DM_APT_CLEAR_ADDR *) osi_malloc(sizeof(tBTA_DM_APT_CLEAR_ADDR))) != NULL) { + memset(p_msg, 0, sizeof(tBTA_DM_APT_CLEAR_ADDR)); + p_msg->hdr.event = BTA_DM_API_CLEAR_RAND_ADDR_EVT; + bta_sys_sendmsg(p_msg); + } +} + /******************************************************************************* ** ** Function BTA_VendorInit diff --git a/components/bt/bluedroid/bta/dm/bta_dm_main.c b/components/bt/bluedroid/bta/dm/bta_dm_main.c index f3a15b09e..e9e2677f4 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_main.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_main.c @@ -104,7 +104,8 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = { /* This handler function added by Yulong at 2016/9/9 to support the random address setting for the APP */ - bta_dm_ble_set_rand_address, /* BTA_DM_API_SET_RAND_ADDR_EVT */ + bta_dm_ble_set_rand_address, /* BTA_DM_API_SET_RAND_ADDR_EVT*/ + bta_dm_ble_clear_rand_address, /* BTA_DM_API_CLEAR_RAND_ADDR_EVT */ /* This handler function added by Yulong at 2016/10/19 to support stop the ble advertising setting diff --git a/components/bt/bluedroid/bta/dm/include/bta_dm_int.h b/components/bt/bluedroid/bta/dm/include/bta_dm_int.h index 27a50b0b4..2b8eea4f7 100644 --- a/components/bt/bluedroid/bta/dm/include/bta_dm_int.h +++ b/components/bt/bluedroid/bta/dm/include/bta_dm_int.h @@ -104,6 +104,7 @@ enum { /*******This event added by Yulong at 2016/9/9 to support the random address setting for the APP******/ BTA_DM_API_SET_RAND_ADDR_EVT, + BTA_DM_API_CLEAR_RAND_ADDR_EVT, /*******This event added by Yulong at 2016/10/19 to support stop the ble advertising setting by the APP******/ BTA_DM_API_BLE_STOP_ADV_EVT, @@ -562,6 +563,10 @@ typedef struct { tBTA_SET_RAND_ADDR_CBACK *p_set_rand_addr_cback; } tBTA_DM_APT_SET_DEV_ADDR; +typedef struct { + BT_HDR hdr; +} tBTA_DM_APT_CLEAR_ADDR; + /* set adv parameter for BLE advertising */ typedef struct { BT_HDR hdr; @@ -819,6 +824,7 @@ typedef union { tBTA_DM_API_UPDATE_CONN_PARAM ble_update_conn_params; tBTA_DM_API_BLE_SET_DATA_LENGTH ble_set_data_length; tBTA_DM_APT_SET_DEV_ADDR set_addr; + tBTA_DM_APT_CLEAR_ADDR clear_addr; tBTA_DM_API_BLE_MULTI_ADV_ENB ble_multi_adv_enb; tBTA_DM_API_BLE_MULTI_ADV_PARAM ble_multi_adv_param; tBTA_DM_API_BLE_MULTI_ADV_DATA ble_multi_adv_data; @@ -1214,6 +1220,7 @@ extern void bta_dm_ble_scan (tBTA_DM_MSG *p_data); extern void bta_dm_ble_update_conn_params (tBTA_DM_MSG *p_data); extern void bta_dm_ble_disconnect (tBTA_DM_MSG *p_data); extern void bta_dm_ble_set_rand_address(tBTA_DM_MSG *p_data); +extern void bta_dm_ble_clear_rand_address(tBTA_DM_MSG *p_data); extern void bta_dm_ble_stop_advertising(tBTA_DM_MSG *p_data); extern void bta_dm_ble_config_local_privacy (tBTA_DM_MSG *p_data); extern void bta_dm_ble_config_local_icon (tBTA_DM_MSG *p_data); diff --git a/components/bt/bluedroid/bta/include/bta/bta_api.h b/components/bt/bluedroid/bta/include/bta/bta_api.h index fdd06426a..575b277ca 100644 --- a/components/bt/bluedroid/bta/include/bta/bta_api.h +++ b/components/bt/bluedroid/bta/include/bta/bta_api.h @@ -2108,6 +2108,7 @@ extern void BTA_DmBleScan(BOOLEAN start, UINT32 duration, extern void BTA_DmBleStopAdvertising(void); extern void BTA_DmSetRandAddress(BD_ADDR rand_addr, tBTA_SET_RAND_ADDR_CBACK *p_set_rand_addr_cback); +extern void BTA_DmClearRandAddress(void); #endif 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 4490e1ffe..d1fc6a6ba 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 @@ -846,6 +846,11 @@ static void btc_ble_set_rand_addr (BD_ADDR rand_addr, tBTA_SET_RAND_ADDR_CBACK * } } +static void btc_ble_clear_rand_addr (void) +{ + BTA_DmClearRandAddress(); +} + 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); @@ -1050,6 +1055,10 @@ void btc_gap_ble_call_handler(btc_msg_t *msg) btc_ble_set_rand_addr(bd_addr, btc_set_rand_addr_callback); break; } + case BTC_GAP_BLE_ACT_CLEAR_RAND_ADDRESS: { + btc_ble_clear_rand_addr(); + break; + } case BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY: btc_ble_config_local_privacy(arg->cfg_local_privacy.privacy_enable, btc_set_local_privacy_callback); break; 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 c9e0f5645..b73759481 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 @@ -30,6 +30,7 @@ typedef enum { BTC_GAP_BLE_ACT_UPDATE_CONN_PARAM, BTC_GAP_BLE_ACT_SET_PKT_DATA_LEN, BTC_GAP_BLE_ACT_SET_RAND_ADDRESS, + BTC_GAP_BLE_ACT_CLEAR_RAND_ADDRESS, BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY, BTC_GAP_BLE_ACT_CONFIG_LOCAL_ICON, BTC_GAP_BLE_ACT_UPDATE_WHITE_LIST, diff --git a/components/bt/bluedroid/stack/btm/btm_ble_addr.c b/components/bt/bluedroid/stack/btm/btm_ble_addr.c index 785ed733f..730d7895b 100644 --- a/components/bt/bluedroid/stack/btm/btm_ble_addr.c +++ b/components/bt/bluedroid/stack/btm/btm_ble_addr.c @@ -59,7 +59,8 @@ static void btm_gen_resolve_paddr_cmpl(tSMP_ENC *p) /* set it to controller */ btsnd_hcic_ble_set_random_addr(p_cb->private_addr); - p_cb->own_addr_type = BLE_ADDR_RANDOM; + p_cb->exist_addr_bit |= BTM_BLE_GAP_ADDR_BIT_RESOLVABLE; + memcpy(p_cb->resolvale_addr, p_cb->private_addr, BD_ADDR_LEN); if (p_cb->set_local_privacy_cback){ (*p_cb->set_local_privacy_cback)(BTM_SET_PRIVACY_SUCCESS); p_cb->set_local_privacy_cback = NULL; @@ -613,12 +614,7 @@ void btm_ble_refresh_local_resolvable_private_addr(BD_ADDR pseudo_addr, BD_ADDR dummy_bda = {0}; if (p != NULL) { -/* - * Temporary solutions for pair with random address: - * use BLE_ADDR_RANDOM when adverting with random adress or in privacy mode - * We will do futher work here - */ - if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE || btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) { + if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) { p->conn_addr_type = BLE_ADDR_RANDOM; if (memcmp(local_rpa, dummy_bda, BD_ADDR_LEN)) { memcpy(p->conn_addr, local_rpa, BD_ADDR_LEN); diff --git a/components/bt/bluedroid/stack/btm/btm_ble_gap.c b/components/bt/bluedroid/stack/btm/btm_ble_gap.c index 3a0088cab..cba05f6d1 100644 --- a/components/bt/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/bluedroid/stack/btm/btm_ble_gap.c @@ -786,20 +786,6 @@ BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode, tBTM_SET_LOCAL_PRIVACY_CBACK return FALSE; } -/* - * Temporary solutions for pair with random address: - * can't set privacy when advertising, scaning or using static random address - * We will do futher work here - */ - 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 || 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){ @@ -813,6 +799,8 @@ BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode, tBTM_SET_LOCAL_PRIVACY_CBACK uint8_t addr_resolution = 0; #endif /* defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */ if (!privacy_mode) { /* if privacy disabled, always use public address */ + p_cb->addr_mgnt_cb.exist_addr_bit &= (~BTM_BLE_GAP_ADDR_BIT_RESOLVABLE); + memset(p_cb->addr_mgnt_cb.resolvale_addr, 0, BD_ADDR_LEN); p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; p_cb->privacy_mode = BTM_PRIVACY_NONE; if (random_cb && random_cb->set_local_privacy_cback){ @@ -821,7 +809,6 @@ BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode, tBTM_SET_LOCAL_PRIVACY_CBACK } } else { /* privacy is turned on*/ /* always set host random address, used when privacy 1.1 or priavcy 1.2 is disabled */ - p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low); if (BTM_BleMaxMultiAdvInstanceCount() > 0) { @@ -1258,17 +1245,66 @@ tBTM_STATUS BTM_BleSetAdvParamsStartAdv(UINT16 adv_int_min, UINT16 adv_int_max, return BTM_ILLEGAL_VALUE; } -/* - * Temporary solutions for pair with random address: - * can't set advertising with BLE_ADDR_PUBLIC when having set random adress or in privacy mode - * We will do futher work here - */ - 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); + if(own_bda_type == BLE_ADDR_RANDOM) { + if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) == BTM_BLE_GAP_ADDR_BIT_RANDOM) { + //close privacy + #if BLE_PRIVACY_SPT == TRUE + if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { + BTM_BleConfigPrivacy(FALSE, NULL); + } + #endif + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN); + // set address to controller + btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); + + } else if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr, BD_ADDR_LEN); + btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr); + }else { + BTM_TRACE_ERROR ("No random address yet, please set random address and try\n"); + if(adv_cb) { + (* adv_cb)(HCI_ERR_ESP_VENDOR_FAIL); + } + return BTM_ILLEGAL_VALUE; } - return BTM_ILLEGAL_VALUE; + } else if(own_bda_type == BLE_ADDR_PUBLIC_ID || own_bda_type == BLE_ADDR_RANDOM_ID) { + if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { + own_bda_type = BLE_ADDR_RANDOM; + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr, BD_ADDR_LEN); + btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr); + } else { + #if BLE_PRIVACY_SPT == TRUE + if(btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { + BTM_TRACE_ERROR ("Error state\n"); + if(adv_cb) { + (* adv_cb)(HCI_ERR_ESP_VENDOR_FAIL); + } + return BTM_ILLEGAL_VALUE; + } + #endif + if(own_bda_type == BLE_ADDR_PUBLIC_ID) { + own_bda_type = BLE_ADDR_PUBLIC; + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; + } else { //own_bda_type == BLE_ADDR_RANDOM_ID + if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) == BTM_BLE_GAP_ADDR_BIT_RANDOM) { + own_bda_type = BLE_ADDR_RANDOM; + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN); + btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); + } else { + BTM_TRACE_ERROR ("No RPA and no random address yet, please set RPA or random address and try\n"); + if(adv_cb) { + (* adv_cb)(HCI_ERR_ESP_VENDOR_FAIL); + } + return BTM_ILLEGAL_VALUE; + } + } + } + } else { + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; } if (!BTM_BLE_ISVALID_PARAM(adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) || @@ -1440,17 +1476,67 @@ void BTM_BleSetScanFilterParams(tGATT_IF client_if, UINT32 scan_interval, UINT32 return; } -/* - * Temporary solutions for pair with random address: - * can't set scan with BLE_ADDR_PUBLIC when having set random adress or in privacy mode - * We will do futher work here - */ - if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM && addr_type_own == BLE_ADDR_PUBLIC) { - BTM_TRACE_ERROR ("own_addr_type is BLE_ADDR_RANDOM but use BLE_ADDR_PUBLIC\n"); - if (scan_setup_status_cback != NULL) { - scan_setup_status_cback(client_if, BTM_ILLEGAL_VALUE); + if(addr_type_own == BLE_ADDR_RANDOM) { + if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) == BTM_BLE_GAP_ADDR_BIT_RANDOM) { + //close privacy + #if BLE_PRIVACY_SPT == TRUE + if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { + BTM_BleConfigPrivacy(FALSE, NULL); + } + #endif + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN); + // set address to controller + btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); + + } else if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr, BD_ADDR_LEN); + btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr); + }else { + if (scan_setup_status_cback != NULL) { + scan_setup_status_cback(client_if, BTM_ILLEGAL_VALUE); + } + BTM_TRACE_ERROR ("No random address yet, please set random address and try\n"); + return; } - return; + } else if(addr_type_own == BLE_ADDR_PUBLIC_ID || addr_type_own == BLE_ADDR_RANDOM_ID) { + if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) { + addr_type_own = BLE_ADDR_RANDOM; + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr, BD_ADDR_LEN); + btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr); + } else { + #if BLE_PRIVACY_SPT == TRUE + if(btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { + BTM_TRACE_ERROR ("Error state\n"); + if (scan_setup_status_cback != NULL) { + scan_setup_status_cback(client_if, BTM_ILLEGAL_VALUE); + } + return; + } + #endif + if(addr_type_own == BLE_ADDR_PUBLIC_ID) { + addr_type_own = BLE_ADDR_PUBLIC; + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; + } else { + //own_bda_type == BLE_ADDR_RANDOM_ID + if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) == BTM_BLE_GAP_ADDR_BIT_RANDOM) { + addr_type_own = BLE_ADDR_RANDOM; + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN); + btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); + } else { + BTM_TRACE_ERROR ("No RPA and no random address yet, please set RPA or random address and try\n"); + if (scan_setup_status_cback != NULL) { + scan_setup_status_cback(client_if, BTM_ILLEGAL_VALUE); + } + return; + } + } + } + } else { + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; } /* If not supporting extended scan support, use the older range for checking */ @@ -1640,32 +1726,43 @@ tBTM_STATUS BTM_BleSetRandAddress(BD_ADDR rand_addr) return BTM_SET_STATIC_RAND_ADDR_FAIL; } -/* - * Temporary solutions for pair with random address: - * can't set rand address when advertising, scaning or in privacy mode - * We will do futher work here - */ -#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 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_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; + memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, rand_addr, BD_ADDR_LEN); //send the set random address to the controller if(btsnd_hcic_ble_set_random_addr(rand_addr)) { + btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit |= BTM_BLE_GAP_ADDR_BIT_RANDOM; return BTM_SUCCESS; } else { return BTM_SET_STATIC_RAND_ADDR_FAIL; } } +/******************************************************************************* +** +** Function BTM_BleClearRandAddress +** +** Description This function is called to clear the LE random address. +** +** Parameters: None. +** +** Returns void +** +*******************************************************************************/ +void BTM_BleClearRandAddress(void) +{ + tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb; + if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM && (!(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 restore public address "); + return; + } + memset(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, 0, BD_ADDR_LEN); + btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit &= (~BTM_BLE_GAP_ADDR_BIT_RANDOM); + btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; +} /******************************************************************************* ** ** Function BTM_BleGetCurrentAddress diff --git a/components/bt/bluedroid/stack/btm/btm_dev.c b/components/bt/bluedroid/stack/btm/btm_dev.c index 11d461941..2e877a85e 100644 --- a/components/bt/bluedroid/stack/btm/btm_dev.c +++ b/components/bt/bluedroid/stack/btm/btm_dev.c @@ -368,6 +368,10 @@ void btm_sec_free_dev (tBTM_SEC_DEV_REC *p_dev_rec, tBT_TRANSPORT transport) btm_sec_clear_ble_keys (p_dev_rec); #endif } + /* No BLE keys and BT keys, clear the sec_flags */ + if(p_dev_rec->sec_flags == BTM_SEC_IN_USE) { + p_dev_rec->sec_flags = 0; + } } /******************************************************************************* diff --git a/components/bt/bluedroid/stack/btm/include/btm_ble_int.h b/components/bt/bluedroid/stack/btm/include/btm_ble_int.h index 42bb4d66c..b3c751403 100644 --- a/components/bt/bluedroid/stack/btm/include/btm_ble_int.h +++ b/components/bt/bluedroid/stack/btm/include/btm_ble_int.h @@ -181,9 +181,15 @@ typedef void (tBTM_BLE_RESOLVE_CBACK) (void *match_rec, void *p); typedef void (tBTM_BLE_ADDR_CBACK) (BD_ADDR_PTR static_random, void *p); +#define BTM_BLE_GAP_ADDR_BIT_RANDOM (1<<0) +#define BTM_BLE_GAP_ADDR_BIT_RESOLVABLE (1<<1) + /* random address management control block */ typedef struct { tBLE_ADDR_TYPE own_addr_type; /* local device LE address type */ + UINT8 exist_addr_bit; + BD_ADDR static_rand_addr; + BD_ADDR resolvale_addr; BD_ADDR private_addr; BD_ADDR random_bda; BOOLEAN busy; diff --git a/components/bt/bluedroid/stack/include/stack/btm_ble_api.h b/components/bt/bluedroid/stack/include/stack/btm_ble_api.h index baa0cc8eb..a019dd4fe 100644 --- a/components/bt/bluedroid/stack/include/stack/btm_ble_api.h +++ b/components/bt/bluedroid/stack/include/stack/btm_ble_api.h @@ -997,6 +997,8 @@ tBTM_STATUS BTM_BleWriteAdvDataRaw(UINT8 *p_raw_adv, UINT32 raw_adv_len); tBTM_STATUS BTM_BleSetRandAddress(BD_ADDR rand_addr); +void BTM_BleClearRandAddress(void); + /******************************************************************************* ** diff --git a/components/bt/bluedroid/stack/l2cap/l2c_ble.c b/components/bt/bluedroid/stack/l2cap/l2c_ble.c index 60526c49c..9a26969fa 100644 --- a/components/bt/bluedroid/stack/l2cap/l2c_ble.c +++ b/components/bt/bluedroid/stack/l2cap/l2c_ble.c @@ -805,7 +805,7 @@ BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb) memcpy(peer_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN); #if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE)) - own_addr_type = btm_cb.ble_ctr_cb.privacy_mode ? BLE_ADDR_RANDOM : BLE_ADDR_PUBLIC; + own_addr_type = btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type; if(dev_rec_exist) { // if the current address information is valid, get the real address information if(p_dev_rec->ble.current_addr_valid) { diff --git a/components/bt/bluedroid/stack/l2cap/l2c_utils.c b/components/bt/bluedroid/stack/l2cap/l2c_utils.c index d51ea96ed..4768d8808 100644 --- a/components/bt/bluedroid/stack/l2cap/l2c_utils.c +++ b/components/bt/bluedroid/stack/l2cap/l2c_utils.c @@ -2216,13 +2216,17 @@ BOOLEAN l2cu_create_conn (tL2C_LCB *p_lcb, tBT_TRANSPORT transport) #if (BLE_INCLUDED == TRUE) tBT_DEVICE_TYPE dev_type; tBLE_ADDR_TYPE addr_type = p_lcb->open_addr_type; - BTM_ReadDevInfo(p_lcb->remote_bd_addr, &dev_type, &addr_type); + if(addr_type == BLE_ADDR_UNKNOWN_TYPE) { + BTM_ReadDevInfo(p_lcb->remote_bd_addr, &dev_type, &addr_type); + } if (transport == BT_TRANSPORT_LE) { if (!controller_get_interface()->supports_ble()) { return FALSE; } - + if(addr_type > BLE_ADDR_TYPE_MAX) { + addr_type = BLE_ADDR_PUBLIC; + } p_lcb->ble_addr_type = addr_type; p_lcb->transport = BT_TRANSPORT_LE; diff --git a/components/bt/bluedroid/stack/smp/smp_utils.c b/components/bt/bluedroid/stack/smp/smp_utils.c index dd3acb4cb..0cc2100e9 100644 --- a/components/bt/bluedroid/stack/smp/smp_utils.c +++ b/components/bt/bluedroid/stack/smp/smp_utils.c @@ -591,8 +591,16 @@ static BT_HDR *smp_build_id_addr_cmd(UINT8 cmd_code, tSMP_CB *p_cb) p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET; UINT8_TO_STREAM (p, SMP_OPCODE_ID_ADDR); - UINT8_TO_STREAM (p, 0); - BDADDR_TO_STREAM (p, controller_get_interface()->get_address()->address); + /* Identity Address Information is used in the Transport Specific Key Distribution phase to distribute + its public device address or static random address. if slave using static random address is encrypted, + it should distribute its static random address */ + if(btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM && memcmp(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr,6) == 0) { + UINT8_TO_STREAM (p, 0x01); + BDADDR_TO_STREAM (p, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr); + } else { + UINT8_TO_STREAM (p, 0); + BDADDR_TO_STREAM (p, controller_get_interface()->get_address()->address); + } p_buf->offset = L2CAP_MIN_OFFSET; p_buf->len = SMP_ID_ADDR_SIZE;