Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit caa41d66 authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by Andre Eisenbach
Browse files

Simplify btm_ble_resolve_random_addr

Bug: 30622771
Test: manual testing
Change-Id: I604d0e909a6fe270e2b413abbdb497d622780261
parent e630f985
Loading
Loading
Loading
Loading
+33 −80
Original line number Diff line number Diff line
@@ -1680,66 +1680,6 @@ uint8_t btm_ble_br_keys_req(tBTM_SEC_DEV_REC* p_dev_rec,
  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_t* p = (uint8_t*)p_data;
  tBTM_SEC_DEV_REC* match_rec = (tBTM_SEC_DEV_REC*)p_rec;
  uint8_t role, bda_type;
  uint16_t handle;
  BD_ADDR bda;
  uint16_t conn_interval, conn_latency, conn_timeout;
  bool 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
@@ -1817,7 +1757,7 @@ void btm_ble_connected(uint8_t* bda, uint16_t handle, uint8_t enc_mode,
void btm_ble_conn_complete(uint8_t* p, UNUSED_ATTR uint16_t evt_len,
                           bool enhanced) {
#if (BLE_PRIVACY_SPT == TRUE)
  uint8_t *p_data = p, peer_addr_type;
  uint8_t peer_addr_type;
  BD_ADDR local_rpa, peer_rpa;
#endif
  uint8_t role, status, bda_type;
@@ -1842,19 +1782,33 @@ void btm_ble_conn_complete(uint8_t* p, UNUSED_ATTR uint16_t evt_len,
      STREAM_TO_BDADDR(peer_rpa, p);
    }

    /* 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);
    } 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);

    /* possiblly receive connection complete with resolvable random while
       the device has been paired */
    if (!match && BTM_BLE_IS_RESOLVE_BDA(bda)) {
      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__);
      }
    }
#endif

    btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type,
                      match);

@@ -1870,7 +1824,6 @@ void btm_ble_conn_complete(uint8_t* p, UNUSED_ATTR uint16_t evt_len,
                                                     BLE_ADDR_RANDOM);
    }
#endif
    }
  } else {
    role = HCI_ROLE_UNKNOWN;
    if (status != HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT) {
+17 −28
Original line number Diff line number Diff line
@@ -282,13 +282,13 @@ bool btm_ble_addr_resolvable(BD_ADDR rpa, tBTM_SEC_DEV_REC* p_dev_rec) {
 *
 ******************************************************************************/
static bool btm_ble_match_random_bda(void* data, void* context) {
  uint8_t* 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_t 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__);

@@ -318,31 +318,20 @@ static bool 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_LE_RANDOM_CB* p_mgnt_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;

tBTM_SEC_DEV_REC* btm_ble_resolve_random_addr(BD_ADDR random_bda) {
  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 = NULL;
    if (n != NULL) p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(list_node(n));
      list_foreach(btm_cb.sec_dev_rec, btm_ble_match_random_bda, random_bda);
  tBTM_SEC_DEV_REC* p_dev_rec = nullptr;
  if (n != nullptr) p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(list_node(n));

  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);
  }
                  (p_dev_rec == nullptr ? "not " : ""));
  return p_dev_rec;
}

/*******************************************************************************
+16 −46
Original line number Diff line number Diff line
@@ -693,48 +693,6 @@ extern uint8_t BTM_BleMaxMultiAdvInstanceCount(void) {
             : 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_t addr_type = BLE_ADDR_RANDOM;
  BD_ADDR bda;
  uint8_t* pp = (uint8_t*)p + 1;
  uint8_t 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
@@ -2139,10 +2097,22 @@ void btm_ble_process_adv_pkt(uint8_t* p_data) {
                    bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
    /* 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);
    } else
      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);
        }
      }
    }
#endif

    btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, p);

    STREAM_TO_UINT8(data_len, p);
+2 −3
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@
#include "btm_ble_api.h"
#include "btm_ble_int_types.h"
#include "btm_int.h"
#include "btm_int_types.h"
#include "hcidefs.h"
#include "smp_api.h"

@@ -144,9 +145,7 @@ extern void btm_ble_dequeue_direct_conn_req(BD_ADDR rem_bda);
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 */
+0 −1
Original line number Diff line number Diff line
@@ -182,7 +182,6 @@ typedef struct {
  tBLE_ADDR_TYPE own_addr_type; /* local device LE address type */
  BD_ADDR private_addr;
  BD_ADDR random_bda;
  bool busy;
  tBTM_BLE_ADDR_CBACK* p_generate_cback;
  void* p;
  alarm_t* refresh_raddr_timer;