From 175be1c42c613442674fd1bc0d398622430ce4f5 Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Mon, 9 Jan 2023 18:58:24 +0800 Subject: [PATCH 1/2] bluedroid: support ble privacy by controller --- .../bt/host/bluedroid/stack/btm/btm_ble.c | 18 ++++++++++++++++- .../host/bluedroid/stack/btm/btm_ble_5_gap.c | 5 +++++ .../host/bluedroid/stack/btm/btm_ble_addr.c | 2 +- .../bt/host/bluedroid/stack/btm/btm_ble_gap.c | 20 ++++++++++++++++++- .../bluedroid/stack/btm/btm_ble_privacy.c | 15 ++++++++++++++ .../bt/host/bluedroid/stack/btm/btm_sec.c | 5 +++++ .../bluedroid/stack/btm/include/btm_ble_int.h | 1 + 7 files changed, 63 insertions(+), 3 deletions(-) diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble.c b/components/bt/host/bluedroid/stack/btm/btm_ble.c index 0d357849adc..69e5404a2e9 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble.c @@ -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; @@ -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; @@ -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); diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c b/components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c index cf05f99fdf6..d1131d39dad 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c @@ -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; @@ -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, diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c b/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c index 036377c4a51..d7ed2095cd7 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c @@ -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); diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c index f8624b3f1bf..9c7b52aa9e4 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c @@ -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); @@ -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); } @@ -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) { @@ -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 /******************************************************************************* @@ -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); diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c b/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c index 0624727e7da..5477be2b1cb 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c @@ -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 diff --git a/components/bt/host/bluedroid/stack/btm/btm_sec.c b/components/bt/host/bluedroid/stack/btm/btm_sec.c index 61ed28ab341..990adf979f9 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_sec.c +++ b/components/bt/host/bluedroid/stack/btm/btm_sec.c @@ -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__); } diff --git a/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h b/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h index 94da2358b44..51f909513fd 100644 --- a/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h +++ b/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h @@ -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); From 5fea7313f8839da2584942b600beb4407f7babe1 Mon Sep 17 00:00:00 2001 From: chenjianhua Date: Mon, 9 Jan 2023 19:28:23 +0800 Subject: [PATCH 2/2] examples: fix ble address type of adv and scan params --- examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c | 2 +- .../ble/gatt_security_client/main/example_ble_sec_gattc_demo.c | 2 +- .../ble/gatt_security_server/main/example_ble_sec_gatts_demo.c | 2 +- examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/main/main.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c b/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c index 1e0c0aa2430..adc4d9822d5 100644 --- a/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c +++ b/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c @@ -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, }; diff --git a/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c b/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c index 013a69dc3ad..b7759224f5d 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c +++ b/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c @@ -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, diff --git a/examples/bluetooth/bluedroid/ble/gatt_security_server/main/example_ble_sec_gatts_demo.c b/examples/bluetooth/bluedroid/ble/gatt_security_server/main/example_ble_sec_gatts_demo.c index 5eeac671d7a..29c1f7c5a66 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_security_server/main/example_ble_sec_gatts_demo.c +++ b/examples/bluetooth/bluedroid/ble/gatt_security_server/main/example_ble_sec_gatts_demo.c @@ -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, }; diff --git a/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/main/main.c b/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/main/main.c index afbcdfa6ae9..e813eac80e4 100644 --- a/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/main/main.c +++ b/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/main/main.c @@ -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, };