Browse Source

bluedroid: support ble privacy by controller

chenjianhua 3 năm trước cách đây
mục cha
commit
175be1c42c

+ 17 - 1
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);

+ 5 - 0
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,

+ 1 - 1
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);

+ 19 - 1
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);

+ 15 - 0
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

+ 5 - 0
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__);
     }

+ 1 - 0
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);