Skip to content

Commit

Permalink
Merge branch 'feature/support_ble_privacy_by_controller' into 'master'
Browse files Browse the repository at this point in the history
Support ble privacy by controller on bluedroid

See merge request espressif/esp-idf!21966
  • Loading branch information
wmy-espressif committed Feb 10, 2023
2 parents db59ab5 + 5fea731 commit e0e3700
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 7 deletions.
18 changes: 17 additions & 1 deletion components/bt/host/bluedroid/stack/btm/btm_ble.c
Expand Up @@ -40,6 +40,7 @@
//#define LOG_TAG "bt_btm_ble"
//#include "osi/include/log.h"
#if BLE_INCLUDED == TRUE
extern void BTM_UpdateAddrInfor(uint8_t addr_type, BD_ADDR bda);
#if SMP_INCLUDED == TRUE
// The temp variable to pass parameter between functions when in the connected event callback.
static BOOLEAN temp_enhanced = FALSE;
Expand Down Expand Up @@ -2043,6 +2044,17 @@ void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len, BOOLEAN enhanced)
if (enhanced) {
STREAM_TO_BDADDR (local_rpa, p);
STREAM_TO_BDADDR (peer_rpa, p);
#if (CONTROLLER_RPA_LIST_ENABLE == TRUE)
BD_ADDR dummy_bda = {0};
/* For controller generates RPA, if resolving list contains no matching entry, it use identity address.
* So we should update own addr type in Host */
if (memcmp(local_rpa, dummy_bda, BD_ADDR_LEN)) {
btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type |= (BLE_ADDR_TYPE_ID_BIT);
BTM_UpdateAddrInfor(btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, local_rpa);
} else {
btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type &= (~BLE_ADDR_TYPE_ID_BIT);
}
#endif
}
#if (BLE_PRIVACY_SPT == TRUE )
peer_addr_type = bda_type;
Expand Down Expand Up @@ -2630,7 +2642,11 @@ static void btm_ble_process_irk(tSMP_ENC *p)
memcpy(btm_cb.devcb.id_keys.irk, p->param_buf, BT_OCTET16_LEN);
btm_notify_new_key(BTM_BLE_KEY_TYPE_ID);

#if BLE_PRIVACY_SPT == TRUE
#if (CONTROLLER_RPA_LIST_ENABLE == TRUE)
btm_ble_add_default_entry_to_resolving_list();
#endif

#if (BLE_PRIVACY_SPT == TRUE) && (CONTROLLER_RPA_LIST_ENABLE == FALSE)
/* if privacy is enabled, new RPA should be calculated */
if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
Expand Down
5 changes: 5 additions & 0 deletions components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c
Expand Up @@ -367,6 +367,8 @@ tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PAR
} else {
extend_adv_cb.inst[instance].legacy_pdu = false;
}

#if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
// if own_addr_type == BLE_ADDR_PUBLIC_ID or BLE_ADDR_RANDOM_ID,
if((params->own_addr_type == BLE_ADDR_PUBLIC_ID || params->own_addr_type == BLE_ADDR_RANDOM_ID) && BTM_GetLocalResolvablePrivateAddr(rand_addr)) {
params->own_addr_type = BLE_ADDR_RANDOM;
Expand All @@ -376,6 +378,9 @@ tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PAR
} else if (params->own_addr_type == BLE_ADDR_RANDOM_ID) {
params->own_addr_type = BLE_ADDR_RANDOM;
}
#else
btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = params->own_addr_type;
#endif

if ((err = btsnd_hcic_ble_set_ext_adv_params(instance, params->type, params->interval_min, params->interval_max,
params->channel_map, params->own_addr_type, params->peer_addr_type,
Expand Down
2 changes: 1 addition & 1 deletion components/bt/host/bluedroid/stack/btm/btm_ble_addr.c
Expand Up @@ -591,7 +591,7 @@ void btm_ble_refresh_local_resolvable_private_addr(BD_ADDR pseudo_addr,
BD_ADDR dummy_bda = {0};

if (p != NULL) {
if (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);
Expand Down
20 changes: 19 additions & 1 deletion components/bt/host/bluedroid/stack/btm/btm_ble_gap.c
Expand Up @@ -893,8 +893,16 @@ BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode, tBTM_SET_LOCAL_PRIVACY_CBACK
// Disable RPA function
btsnd_hcic_ble_set_addr_resolution_enable(FALSE);
} else { /* privacy is turned on*/
#if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
/* always set host random address, used when privacy 1.1 or priavcy 1.2 is disabled */
btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
#else
/* Controller generates RPA, Host don't need to set random address */
if (random_cb && random_cb->set_local_privacy_cback){
(*random_cb->set_local_privacy_cback)(BTM_SET_PRIVACY_SUCCESS);
random_cb->set_local_privacy_cback = NULL;
}
#endif

if (BTM_BleMaxMultiAdvInstanceCount() > 0) {
btm_ble_multi_adv_enb_privacy(privacy_mode);
Expand All @@ -910,7 +918,7 @@ BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode, tBTM_SET_LOCAL_PRIVACY_CBACK
} else { /* 4.1/4.0 controller */
p_cb->privacy_mode = BTM_PRIVACY_1_1;
}
// Disable RPA function
// Enable RPA function
btsnd_hcic_ble_set_addr_resolution_enable(TRUE);
}

Expand Down Expand Up @@ -990,6 +998,7 @@ void BTM_BleSetStaticAddr(BD_ADDR rand_addr)
btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit |= BTM_BLE_GAP_ADDR_BIT_RANDOM;
}

#if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb)
{
if(*own_bda_type == BLE_ADDR_RANDOM) {
Expand Down Expand Up @@ -1056,6 +1065,13 @@ uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *

return BTM_SUCCESS;
}
#else
uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb)
{
btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = *own_bda_type;
return BTM_SUCCESS;
}
#endif


/*******************************************************************************
Expand Down Expand Up @@ -4238,8 +4254,10 @@ void btm_ble_timeout(TIMER_LIST_ENT *p_tle)
case BTU_TTYPE_BLE_RANDOM_ADDR:
if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) {
if (NULL == (void *)(p_tle->param)) {
#if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
/* refresh the random addr */
btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
#endif
} else {
if (BTM_BleMaxMultiAdvInstanceCount() > 0) {
btm_ble_multi_adv_configure_rpa((tBTM_BLE_MULTI_ADV_INST *)p_tle->param);
Expand Down
15 changes: 15 additions & 0 deletions components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c
Expand Up @@ -1021,4 +1021,19 @@ void btm_ble_resolving_list_cleanup(void)
}

}

void btm_ble_add_default_entry_to_resolving_list(void)
{
/*
* Add local IRK entry with 00:00:00:00:00:00 address. This entry will
* be used to generate RPA for non-directed advertising if own_addr_type
* is set to rpa_pub since we use all-zero address as peer addres in
* such case. Peer IRK should be left all-zero since this is not for an
* actual peer.
*/
BD_ADDR peer_addr = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
BT_OCTET16 peer_irk = {0x0};

btsnd_hcic_ble_add_device_resolving_list (BLE_ADDR_PUBLIC, peer_addr, peer_irk, btm_cb.devcb.id_keys.irk);
}
#endif
5 changes: 5 additions & 0 deletions components/bt/host/bluedroid/stack/btm/btm_sec.c
Expand Up @@ -240,6 +240,11 @@ BOOLEAN BTM_SecRegister(tBTM_APPL_INFO *p_cb_info)
if (memcmp(btm_cb.devcb.id_keys.ir, &temp_value, sizeof(BT_OCTET16)) == 0) {
btm_ble_reset_id();
}
#if (CONTROLLER_RPA_LIST_ENABLE == TRUE)
else {
btm_ble_add_default_entry_to_resolving_list();
}
#endif
} else {
BTM_TRACE_WARNING("%s p_cb_info->p_le_callback == NULL\n", __func__);
}
Expand Down
Expand Up @@ -495,6 +495,7 @@ BOOLEAN btm_ble_disable_resolving_list(UINT8 rl_mask, BOOLEAN to_resume);
void btm_ble_enable_resolving_list_for_platform (UINT8 rl_mask);
void btm_ble_resolving_list_init(UINT8 max_irk_list_sz);
void btm_ble_resolving_list_cleanup(void);
void btm_ble_add_default_entry_to_resolving_list(void);
#endif

void btm_ble_multi_adv_configure_rpa (tBTM_BLE_MULTI_ADV_INST *p_inst);
Expand Down
Expand Up @@ -99,7 +99,7 @@ static esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x100,
.adv_int_max = 0x100,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
Expand Down
Expand Up @@ -55,7 +55,7 @@ static const char remote_device_name[] = "ESP_BLE_SECURITY";

static esp_ble_scan_params_t ble_scan_params = {
.scan_type = BLE_SCAN_TYPE_ACTIVE,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC,
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
.scan_interval = 0x50,
.scan_window = 0x30,
Expand Down
Expand Up @@ -71,7 +71,7 @@ static esp_ble_adv_params_t heart_rate_adv_params = {
.adv_int_min = 0x100,
.adv_int_max = 0x100,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
Expand Down
Expand Up @@ -98,7 +98,7 @@ static esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x060,
.adv_int_max = 0x060,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
Expand Down

0 comments on commit e0e3700

Please sign in to comment.