From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jakub Pawlowski <jpawlowski@google.com>
Date: Tue, 6 Dec 2016 15:40:58 -0800
Subject: [PATCH] Simplify btm_ble_resolve_random_addr

Bug: 30622771
Test: manual testing
Change-Id: I604d0e909a6fe270e2b413abbdb497d622780261
---
 stack/btm/btm_ble.c      | 119 ++++++++++++---------------------------
 stack/btm/btm_ble_addr.c |  41 ++++++--------
 stack/btm/btm_ble_gap.c  |  66 ++++++----------------
 stack/btm/btm_ble_int.h  |   3 +-
 4 files changed, 74 insertions(+), 155 deletions(-)

diff --git a/stack/btm/btm_ble.c b/stack/btm/btm_ble.c
index c6e699d49..69a497454 100644
--- a/stack/btm/btm_ble.c
+++ b/stack/btm/btm_ble.c
@@ -1800,69 +1800,6 @@ UINT8 btm_ble_br_keys_req(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_LE_IO_REQ *p_data)
     return callback_rc;
 }
 
-#if (BLE_PRIVACY_SPT == TRUE )
-/*******************************************************************************
-**
-** Function         btm_ble_resolve_random_addr_on_conn_cmpl
-**
-** Description      resolve random address complete on connection complete event.
-**
-** Returns          void
-**
-*******************************************************************************/
-static void btm_ble_resolve_random_addr_on_conn_cmpl(void * p_rec, void *p_data)
-{
-    UINT8   *p = (UINT8 *)p_data;
-    tBTM_SEC_DEV_REC    *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
-    UINT8       role, bda_type;
-    UINT16      handle;
-    BD_ADDR     bda;
-    UINT16      conn_interval, conn_latency, conn_timeout;
-    BOOLEAN     match = FALSE;
-
-    ++p;
-    STREAM_TO_UINT16   (handle, p);
-    STREAM_TO_UINT8    (role, p);
-    STREAM_TO_UINT8    (bda_type, p);
-    STREAM_TO_BDADDR   (bda, p);
-    STREAM_TO_UINT16   (conn_interval, p);
-    STREAM_TO_UINT16   (conn_latency, p);
-    STREAM_TO_UINT16   (conn_timeout, p);
-
-    handle = HCID_GET_HANDLE (handle);
-
-    BTM_TRACE_EVENT ("%s", __func__);
-
-    if (match_rec)
-    {
-        LOG_INFO(LOG_TAG, "%s matched and resolved random address", __func__);
-        match = TRUE;
-        match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
-        memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
-        if (!btm_ble_init_pseudo_addr (match_rec, bda))
-        {
-            /* assign the original address to be the current report address */
-            memcpy(bda, match_rec->ble.pseudo_addr, BD_ADDR_LEN);
-        }
-        else
-        {
-            memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
-        }
-    }
-    else
-    {
-        LOG_INFO(LOG_TAG, "%s unable to match and resolve random address", __func__);
-    }
-
-    btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
-
-    l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
-                      conn_latency, conn_timeout);
-
-    return;
-}
-#endif
-
 /*******************************************************************************
 **
 ** Function         btm_ble_connected
@@ -1946,7 +1883,7 @@ void btm_ble_connected (UINT8 *bda, UINT16 handle, UINT8 enc_mode, UINT8 role,
 void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len, BOOLEAN enhanced)
 {
 #if (BLE_PRIVACY_SPT == TRUE )
-    UINT8       *p_data = p, peer_addr_type;
+    UINT8        peer_addr_type;
     BD_ADDR     local_rpa, peer_rpa;
 #endif
     UINT8       role, status, bda_type;
@@ -1974,35 +1911,53 @@ void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len, BOOLEAN enhanced)
             STREAM_TO_BDADDR   (peer_rpa, p);
         }
 
+        STREAM_TO_UINT16(conn_interval, p);
+        STREAM_TO_UINT16(conn_latency, p);
+        STREAM_TO_UINT16(conn_timeout, p);
+        handle = HCID_GET_HANDLE(handle);
+
         /* possiblly receive connection complete with resolvable random while
            the device has been paired */
         if (!match && BTM_BLE_IS_RESOLVE_BDA(bda))
         {
-            btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_conn_cmpl, p_data);
+            tBTM_SEC_DEV_REC* match_rec = btm_ble_resolve_random_addr(bda);
+            if (match_rec)
+            {
+                LOG_INFO(LOG_TAG, "%s matched and resolved random address", __func__);
+                match = true;
+                match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
+                memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
+                if (!btm_ble_init_pseudo_addr(match_rec, bda))
+                {
+                    /* assign the original address to be the current report address */
+                    memcpy(bda, match_rec->ble.pseudo_addr, BD_ADDR_LEN);
+                }
+                else
+                {
+                    memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
+                }
+            }
+            else
+            {
+                LOG_INFO(LOG_TAG, "%s unable to match and resolve random address",
+                         __func__);
+            }
         }
-        else
 #endif
-        {
-            STREAM_TO_UINT16   (conn_interval, p);
-            STREAM_TO_UINT16   (conn_latency, p);
-            STREAM_TO_UINT16   (conn_timeout, p);
-            handle = HCID_GET_HANDLE (handle);
+        btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
 
-            btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
-
-            l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
-                              conn_latency, conn_timeout);
+        l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
+                          conn_latency, conn_timeout);
 
 #if (BLE_PRIVACY_SPT == TRUE)
-            if (enhanced)
-            {
-                btm_ble_refresh_local_resolvable_private_addr(bda, local_rpa);
+        if (enhanced)
+        {
+            btm_ble_refresh_local_resolvable_private_addr(bda, local_rpa);
 
-                if (peer_addr_type & BLE_ADDR_TYPE_ID_BIT)
-                    btm_ble_refresh_peer_resolvable_private_addr(bda, peer_rpa, BLE_ADDR_RANDOM);
-            }
-#endif
+            if (peer_addr_type & BLE_ADDR_TYPE_ID_BIT)
+                btm_ble_refresh_peer_resolvable_private_addr(bda, peer_rpa, BLE_ADDR_RANDOM);
         }
+#endif
     }
     else
     {
diff --git a/stack/btm/btm_ble_addr.c b/stack/btm/btm_ble_addr.c
index 81fff5349..b389aae5c 100644
--- a/stack/btm/btm_ble_addr.c
+++ b/stack/btm/btm_ble_addr.c
@@ -318,13 +318,13 @@ BOOLEAN btm_ble_addr_resolvable (BD_ADDR rpa, tBTM_SEC_DEV_REC *p_dev_rec)
 static BOOLEAN btm_ble_match_random_bda(void *data, void *context)
 {
 #if (BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
+    UINT8 *random_bda = (uint8_t*)context;
     /* use the 3 MSB of bd address as prand */
 
-    tBTM_LE_RANDOM_CB *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
     UINT8 rand[3];
-    rand[0] = p_mgnt_cb->random_bda[2];
-    rand[1] = p_mgnt_cb->random_bda[1];
-    rand[2] = p_mgnt_cb->random_bda[0];
+    rand[0] = random_bda[2];
+    rand[1] = random_bda[1];
+    rand[2] = random_bda[0];
 
     BTM_TRACE_EVENT("%s next iteration", __func__);
 
@@ -356,28 +356,21 @@ static BOOLEAN btm_ble_match_random_bda(void *data, void *context)
 **                  address is matched to.
 **
 *******************************************************************************/
-void btm_ble_resolve_random_addr(BD_ADDR random_bda, tBTM_BLE_RESOLVE_CBACK * p_cback, void *p)
+tBTM_SEC_DEV_REC* btm_ble_resolve_random_addr(BD_ADDR random_bda)
 {
-    tBTM_LE_RANDOM_CB   *p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
-
     BTM_TRACE_EVENT("%s", __func__);
-    if ( !p_mgnt_cb->busy) {
-        p_mgnt_cb->p = p;
-        p_mgnt_cb->busy = TRUE;
-        memcpy(p_mgnt_cb->random_bda, random_bda, BD_ADDR_LEN);
-        /* start to resolve random address */
-        /* check for next security record */
-
-        list_node_t * n = list_foreach(btm_cb.sec_dev_rec, btm_ble_match_random_bda, NULL);
-        tBTM_SEC_DEV_REC *p_dev_rec = n ? list_node(n) : NULL;
-
-        BTM_TRACE_EVENT("%s:  %sresolved", __func__, (p_dev_rec == NULL ? "not " : ""));
-        p_mgnt_cb->busy = FALSE;
-
-        (*p_cback)(p_dev_rec, p);
-    } else {
-        (*p_cback)(NULL, p);
-    }
+
+    /* start to resolve random address */
+    /* check for next security record */
+
+    list_node_t* n =
+        list_foreach(btm_cb.sec_dev_rec, btm_ble_match_random_bda, random_bda);
+    tBTM_SEC_DEV_REC* p_dev_rec = NULL;
+    if (n != NULL) p_dev_rec = (tBTM_SEC_DEV_REC*)list_node(n);
+
+    BTM_TRACE_EVENT("%s:  %sresolved", __func__,
+                  (p_dev_rec == NULL ? "not " : ""));
+    return p_dev_rec;
 }
 #endif
 
diff --git a/stack/btm/btm_ble_gap.c b/stack/btm/btm_ble_gap.c
index f8c06342f..fe8ee6d27 100644
--- a/stack/btm/btm_ble_gap.c
+++ b/stack/btm/btm_ble_gap.c
@@ -729,51 +729,6 @@ extern UINT8  BTM_BleMaxMultiAdvInstanceCount(void)
         btm_cb.cmn_ble_vsc_cb.adv_inst_max : BTM_BLE_MULTI_ADV_MAX;
 }
 
-#if BLE_PRIVACY_SPT == TRUE
-/*******************************************************************************
-**
-** Function         btm_ble_resolve_random_addr_on_adv
-**
-** Description      resolve random address complete callback.
-**
-** Returns          void
-**
-*******************************************************************************/
-static void btm_ble_resolve_random_addr_on_adv(void * p_rec, void *p)
-{
-    tBTM_SEC_DEV_REC    *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
-    UINT8       addr_type = BLE_ADDR_RANDOM;
-    BD_ADDR     bda;
-    UINT8       *pp = (UINT8 *)p + 1;
-    UINT8           evt_type;
-
-    BTM_TRACE_EVENT ("btm_ble_resolve_random_addr_on_adv ");
-
-    STREAM_TO_UINT8    (evt_type, pp);
-    STREAM_TO_UINT8    (addr_type, pp);
-    STREAM_TO_BDADDR   (bda, pp);
-
-    if (match_rec)
-    {
-        BTM_TRACE_DEBUG("Random match");
-        match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
-        memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
-
-        if (btm_ble_init_pseudo_addr(match_rec, bda))
-        {
-            memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
-        } else {
-            // Assign the original address to be the current report address
-            memcpy(bda, match_rec->ble.pseudo_addr, BD_ADDR_LEN);
-        }
-    }
-
-    btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, pp);
-
-    return;
-}
-#endif
-
 /*******************************************************************************
 **
 ** Function         BTM_BleLocalPrivacyEnabled
@@ -2766,11 +2721,26 @@ void btm_ble_process_adv_pkt (UINT8 *p_data)
         /* always do RRA resolution on host */
         if (!match && BTM_BLE_IS_RESOLVE_BDA(bda))
         {
-            btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_adv, p_data);
+            tBTM_SEC_DEV_REC* match_rec = btm_ble_resolve_random_addr(bda);
+            if (match_rec)
+            {
+                BTM_TRACE_DEBUG("Random match");
+                match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
+                memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
+
+                if (btm_ble_init_pseudo_addr(match_rec, bda))
+                {
+                    memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
+                }
+                else
+                {
+                    // Assign the original address to be the current report address
+                    memcpy(bda, match_rec->ble.pseudo_addr, BD_ADDR_LEN);
+                }
+            }
         }
-        else
 #endif
-            btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, p);
+        btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, p);
 
         STREAM_TO_UINT8(data_len, p);
 
diff --git a/stack/btm/btm_ble_int.h b/stack/btm/btm_ble_int.h
index 437503e12..4bcf5a7e9 100644
--- a/stack/btm/btm_ble_int.h
+++ b/stack/btm/btm_ble_int.h
@@ -31,6 +31,7 @@
 #include "hcidefs.h"
 #include "btm_ble_api.h"
 #include "btm_int.h"
+#include "btm_int_types.h"
 
 #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
 #include "smp_api.h"
@@ -132,7 +133,7 @@ extern void btm_ble_dequeue_direct_conn_req(BD_ADDR rem_bda);
 /* BLE address management */
 extern void btm_gen_resolvable_private_addr (void *p_cmd_cplt_cback);
 extern void btm_gen_non_resolvable_private_addr (tBTM_BLE_ADDR_CBACK *p_cback, void *p);
-extern void btm_ble_resolve_random_addr(BD_ADDR random_bda, tBTM_BLE_RESOLVE_CBACK * p_cback, void *p);
+extern tBTM_SEC_DEV_REC* btm_ble_resolve_random_addr(BD_ADDR random_bda);
 extern void btm_gen_resolve_paddr_low(tBTM_RAND_ENC *p);
 
 /*  privacy function */
