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

Commit 15721c0f authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by android-build-merger
Browse files

btsnd_hcic_ble_rand refactor am: 64b2fa73 am: 3f8e2383

am: 83cb3a9f

Change-Id: Ie7921f2064b874ef6a2e1edce0960fdbc9efb556
parents 9f4f53f3 83cb3a9f
Loading
Loading
Loading
Loading
+32 −105
Original line number Original line Diff line number Diff line
@@ -27,6 +27,7 @@


#include "bt_target.h"
#include "bt_target.h"


#include <base/bind.h>
#include <string.h>
#include <string.h>


#include "bt_types.h"
#include "bt_types.h"
@@ -2253,52 +2254,6 @@ static void btm_notify_new_key(uint8_t key_type) {
  }
  }
}
}


/*******************************************************************************
 *
 * Function         btm_ble_process_er2
 *
 * Description      This function is called when ER is generated, store it in
 *                  local control block.
 *
 * Returns          void
 *
 ******************************************************************************/
static void btm_ble_process_er2(tBTM_RAND_ENC* p) {
  BTM_TRACE_DEBUG("btm_ble_process_er2");

  if (p && p->opcode == HCI_BLE_RAND) {
    memcpy(&btm_cb.devcb.ble_encryption_key_value[8], p->param_buf,
           BT_OCTET8_LEN);
    btm_notify_new_key(BTM_BLE_KEY_TYPE_ER);
  } else {
    BTM_TRACE_ERROR("Generating ER2 exception.");
    memset(&btm_cb.devcb.ble_encryption_key_value, 0, sizeof(BT_OCTET16));
  }
}

/*******************************************************************************
 *
 * Function         btm_ble_process_er
 *
 * Description      This function is called when ER is generated, store it in
 *                  local control block.
 *
 * Returns          void
 *
 ******************************************************************************/
static void btm_ble_process_er(tBTM_RAND_ENC* p) {
  BTM_TRACE_DEBUG("btm_ble_process_er");

  if (p && p->opcode == HCI_BLE_RAND) {
    memcpy(&btm_cb.devcb.ble_encryption_key_value[0], p->param_buf,
           BT_OCTET8_LEN);

    btsnd_hcic_ble_rand((void*)btm_ble_process_er2);
  } else {
    BTM_TRACE_ERROR("Generating ER1 exception.");
  }
}

/*******************************************************************************
/*******************************************************************************
 *
 *
 * Function         btm_ble_process_irk
 * Function         btm_ble_process_irk
@@ -2318,7 +2273,7 @@ static void btm_ble_process_irk(tSMP_ENC* p) {
#if (BLE_PRIVACY_SPT == TRUE)
#if (BLE_PRIVACY_SPT == TRUE)
    /* if privacy is enabled, new RPA should be calculated */
    /* if privacy is enabled, new RPA should be calculated */
    if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
    if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
      btm_gen_resolvable_private_addr((void*)btm_gen_resolve_paddr_low);
      btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));
    }
    }
#endif
#endif
  } else {
  } else {
@@ -2326,7 +2281,15 @@ static void btm_ble_process_irk(tSMP_ENC* p) {
  }
  }


  /* proceed generate ER */
  /* proceed generate ER */
  btsnd_hcic_ble_rand((void*)btm_ble_process_er);
  btsnd_hcic_ble_rand(base::Bind([](BT_OCTET8 rand1) {
    memcpy(&btm_cb.devcb.ble_encryption_key_value[0], rand1, BT_OCTET8_LEN);

    btsnd_hcic_ble_rand(base::Bind([](BT_OCTET8 rand2) {
      memcpy(&btm_cb.devcb.ble_encryption_key_value[8], rand2, BT_OCTET8_LEN);
      btm_notify_new_key(BTM_BLE_KEY_TYPE_ER);
    }));

  }));
}
}


/*******************************************************************************
/*******************************************************************************
@@ -2366,25 +2329,29 @@ static void btm_ble_process_dhk(tSMP_ENC* p) {


/*******************************************************************************
/*******************************************************************************
 *
 *
 * Function         btm_ble_process_ir2
 * Function         btm_ble_reset_id
 *
 * Description      This function is called when IR is generated, proceed to
 *                  calculate
 *                  DHK = Eir({0x03, 0, 0 ...})
 *
 *
 * Description      This function is called to reset LE device identity.
 *
 *
 * Returns          void
 * Returns          void
 *
 *
 ******************************************************************************/
 ******************************************************************************/
static void btm_ble_process_ir2(tBTM_RAND_ENC* p) {
void btm_ble_reset_id(void) {
  BTM_TRACE_DEBUG("btm_ble_reset_id");

  /* Regenerate Identity Root*/
  btsnd_hcic_ble_rand(base::Bind([](BT_OCTET8 rand) {
    BTM_TRACE_DEBUG("btm_ble_process_ir1");
    memcpy(btm_cb.devcb.id_keys.ir, rand, BT_OCTET8_LEN);

    btsnd_hcic_ble_rand(base::Bind([](BT_OCTET8 rand) {
      uint8_t btm_ble_dhk_pt = 0x03;
      uint8_t btm_ble_dhk_pt = 0x03;
      tSMP_ENC output;
      tSMP_ENC output;


      BTM_TRACE_DEBUG("btm_ble_process_ir2");
      BTM_TRACE_DEBUG("btm_ble_process_ir2");


  if (p && p->opcode == HCI_BLE_RAND) {
      /* remembering in control block */
      /* remembering in control block */
    memcpy(&btm_cb.devcb.id_keys.ir[8], p->param_buf, BT_OCTET8_LEN);
      memcpy(&btm_cb.devcb.id_keys.ir[8], rand, BT_OCTET8_LEN);
      /* generate DHK= Eir({0x03, 0x00, 0x00 ...}) */
      /* generate DHK= Eir({0x03, 0x00, 0x00 ...}) */


      SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_dhk_pt, 1,
      SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_dhk_pt, 1,
@@ -2392,48 +2359,8 @@ static void btm_ble_process_ir2(tBTM_RAND_ENC* p) {
      btm_ble_process_dhk(&output);
      btm_ble_process_dhk(&output);


      BTM_TRACE_DEBUG("BLE IR generated.");
      BTM_TRACE_DEBUG("BLE IR generated.");
  } else {
    }));
    memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
  }));
  }
}

/*******************************************************************************
 *
 * Function         btm_ble_process_ir
 *
 * Description      This function is called when IR is generated, proceed to
 *                  calculate
 *                  DHK = Eir({0x02, 0, 0 ...})
 *
 *
 * Returns          void
 *
 ******************************************************************************/
static void btm_ble_process_ir(tBTM_RAND_ENC* p) {
  BTM_TRACE_DEBUG("btm_ble_process_ir");

  if (p && p->opcode == HCI_BLE_RAND) {
    /* remembering in control block */
    memcpy(btm_cb.devcb.id_keys.ir, p->param_buf, BT_OCTET8_LEN);

    btsnd_hcic_ble_rand((void*)btm_ble_process_ir2);
  }
}

/*******************************************************************************
 *
 * Function         btm_ble_reset_id
 *
 * Description      This function is called to reset LE device identity.
 *
 * Returns          void
 *
 ******************************************************************************/
void btm_ble_reset_id(void) {
  BTM_TRACE_DEBUG("btm_ble_reset_id");

  /* regenrate Identity Root*/
  btsnd_hcic_ble_rand((void*)btm_ble_process_ir);
}
}


#if BTM_BLE_CONFORMANCE_TESTING == TRUE
#if BTM_BLE_CONFORMANCE_TESTING == TRUE
+23 −29
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@
 *
 *
 ******************************************************************************/
 ******************************************************************************/


#include <base/bind.h>
#include <string.h>
#include <string.h>


#include "bt_types.h"
#include "bt_types.h"
@@ -83,28 +84,26 @@ static void btm_gen_resolve_paddr_cmpl(tSMP_ENC* p) {
 * Returns          void
 * Returns          void
 *
 *
 ******************************************************************************/
 ******************************************************************************/
void btm_gen_resolve_paddr_low(tBTM_RAND_ENC* p) {
void btm_gen_resolve_paddr_low(BT_OCTET8 rand) {
  tBTM_LE_RANDOM_CB* p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
  tBTM_LE_RANDOM_CB* p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
  tSMP_ENC output;
  tSMP_ENC output;


  BTM_TRACE_EVENT("btm_gen_resolve_paddr_low");
  BTM_TRACE_EVENT("btm_gen_resolve_paddr_low");
  if (p) {
  rand[2] &= (~BLE_RESOLVE_ADDR_MASK);
    p->param_buf[2] &= (~BLE_RESOLVE_ADDR_MASK);
  rand[2] |= BLE_RESOLVE_ADDR_MSB;
    p->param_buf[2] |= BLE_RESOLVE_ADDR_MSB;


    p_cb->private_addr[2] = p->param_buf[0];
  p_cb->private_addr[2] = rand[0];
    p_cb->private_addr[1] = p->param_buf[1];
  p_cb->private_addr[1] = rand[1];
    p_cb->private_addr[0] = p->param_buf[2];
  p_cb->private_addr[0] = rand[2];


  /* encrypt with ur IRK */
  /* encrypt with ur IRK */
    if (!SMP_Encrypt(btm_cb.devcb.id_keys.irk, BT_OCTET16_LEN, p->param_buf, 3,
  if (!SMP_Encrypt(btm_cb.devcb.id_keys.irk, BT_OCTET16_LEN, rand, 3,
                   &output)) {
                   &output)) {
    btm_gen_resolve_paddr_cmpl(NULL);
    btm_gen_resolve_paddr_cmpl(NULL);
  } else {
  } else {
    btm_gen_resolve_paddr_cmpl(&output);
    btm_gen_resolve_paddr_cmpl(&output);
  }
  }
}
}
}
/*******************************************************************************
/*******************************************************************************
 *
 *
 * Function         btm_gen_resolvable_private_addr
 * Function         btm_gen_resolvable_private_addr
@@ -114,10 +113,10 @@ void btm_gen_resolve_paddr_low(tBTM_RAND_ENC* p) {
 * Returns          void
 * Returns          void
 *
 *
 ******************************************************************************/
 ******************************************************************************/
void btm_gen_resolvable_private_addr(void* p_cmd_cplt_cback) {
void btm_gen_resolvable_private_addr(base::Callback<void(BT_OCTET8)> cb) {
  BTM_TRACE_EVENT("%s", __func__);
  BTM_TRACE_EVENT("%s", __func__);
  /* generate 3B rand as BD LSB, SRK with it, get BD MSB */
  /* generate 3B rand as BD LSB, SRK with it, get BD MSB */
  btsnd_hcic_ble_rand(p_cmd_cplt_cback);
  btsnd_hcic_ble_rand(std::move(cb));
}
}
/*******************************************************************************
/*******************************************************************************
 *
 *
@@ -129,7 +128,7 @@ void btm_gen_resolvable_private_addr(void* p_cmd_cplt_cback) {
 * Returns          void
 * Returns          void
 *
 *
 ******************************************************************************/
 ******************************************************************************/
static void btm_gen_non_resolve_paddr_cmpl(tBTM_RAND_ENC* p) {
static void btm_gen_non_resolve_paddr_cmpl(BT_OCTET8 rand) {
  tBTM_LE_RANDOM_CB* p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
  tBTM_LE_RANDOM_CB* p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
  tBTM_BLE_ADDR_CBACK* p_cback = p_cb->p_generate_cback;
  tBTM_BLE_ADDR_CBACK* p_cback = p_cb->p_generate_cback;
  void* p_data = p_cb->p;
  void* p_data = p_cb->p;
@@ -139,18 +138,13 @@ static void btm_gen_non_resolve_paddr_cmpl(tBTM_RAND_ENC* p) {
  BTM_TRACE_EVENT("btm_gen_non_resolve_paddr_cmpl");
  BTM_TRACE_EVENT("btm_gen_non_resolve_paddr_cmpl");


  p_cb->p_generate_cback = NULL;
  p_cb->p_generate_cback = NULL;
  if (p) {
  pp = rand;
    pp = p->param_buf;
  STREAM_TO_BDADDR(static_random, pp);
  STREAM_TO_BDADDR(static_random, pp);
  /* mask off the 2 MSB */
  /* mask off the 2 MSB */
  static_random[0] &= BLE_STATIC_PRIVATE_MSB_MASK;
  static_random[0] &= BLE_STATIC_PRIVATE_MSB_MASK;


  /* report complete */
  /* report complete */
  if (p_cback) (*p_cback)(static_random, p_data);
  if (p_cback) (*p_cback)(static_random, p_data);
  } else {
    BTM_TRACE_DEBUG("btm_gen_non_resolvable_private_addr failed");
    if (p_cback) (*p_cback)(NULL, p_data);
  }
}
}
/*******************************************************************************
/*******************************************************************************
 *
 *
@@ -172,7 +166,7 @@ void btm_gen_non_resolvable_private_addr(tBTM_BLE_ADDR_CBACK* p_cback,


  p_mgnt_cb->p_generate_cback = p_cback;
  p_mgnt_cb->p_generate_cback = p_cback;
  p_mgnt_cb->p = p;
  p_mgnt_cb->p = p;
  btsnd_hcic_ble_rand((void*)btm_gen_non_resolve_paddr_cmpl);
  btsnd_hcic_ble_rand(base::Bind(&btm_gen_non_resolve_paddr_cmpl));
}
}


/*******************************************************************************
/*******************************************************************************
+3 −2
Original line number Original line Diff line number Diff line
@@ -24,6 +24,7 @@


#define LOG_TAG "bt_btm_ble"
#define LOG_TAG "bt_btm_ble"


#include <base/bind.h>
#include <base/callback.h>
#include <base/callback.h>
#include <base/strings/string_number_conversions.h>
#include <base/strings/string_number_conversions.h>
#include <stddef.h>
#include <stddef.h>
@@ -723,7 +724,7 @@ bool BTM_BleConfigPrivacy(bool privacy_mode) {
    /* always set host random address, used when privacy 1.1 or priavcy 1.2 is
    /* always set host random address, used when privacy 1.1 or priavcy 1.2 is
     * disabled */
     * disabled */
    p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM;
    p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM;
    btm_gen_resolvable_private_addr((void*)btm_gen_resolve_paddr_low);
    btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));


    /* 4.2 controller only allow privacy 1.2 or mixed mode, resolvable private
    /* 4.2 controller only allow privacy 1.2 or mixed mode, resolvable private
     * address in controller */
     * address in controller */
@@ -2515,7 +2516,7 @@ static void btm_ble_observer_timer_timeout(UNUSED_ATTR void* data) {
void btm_ble_refresh_raddr_timer_timeout(UNUSED_ATTR void* data) {
void btm_ble_refresh_raddr_timer_timeout(UNUSED_ATTR void* data) {
  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) {
    /* refresh the random addr */
    /* refresh the random addr */
    btm_gen_resolvable_private_addr((void*)btm_gen_resolve_paddr_low);
    btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));
  }
  }
}
}


+2 −2
Original line number Original line Diff line number Diff line
@@ -144,11 +144,11 @@ extern void btm_ble_enqueue_direct_conn_req(void* p_param);
extern void btm_ble_dequeue_direct_conn_req(BD_ADDR rem_bda);
extern void btm_ble_dequeue_direct_conn_req(BD_ADDR rem_bda);


/* BLE address management */
/* BLE address management */
extern void btm_gen_resolvable_private_addr(void* p_cmd_cplt_cback);
extern void btm_gen_resolvable_private_addr(base::Callback<void(BT_OCTET8)> cb);
extern void btm_gen_non_resolvable_private_addr(tBTM_BLE_ADDR_CBACK* p_cback,
extern void btm_gen_non_resolvable_private_addr(tBTM_BLE_ADDR_CBACK* p_cback,
                                                void* p);
                                                void* p);
extern tBTM_SEC_DEV_REC* btm_ble_resolve_random_addr(BD_ADDR random_bda);
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);
extern void btm_gen_resolve_paddr_low(BT_OCTET8 rand);


/*  privacy function */
/*  privacy function */
#if (BLE_PRIVACY_SPT == TRUE)
#if (BLE_PRIVACY_SPT == TRUE)
+10 −25
Original line number Original line Diff line number Diff line
@@ -33,6 +33,8 @@


using base::Bind;
using base::Bind;
using multiadv_cb = base::Callback<void(uint8_t /* status */)>;
using multiadv_cb = base::Callback<void(uint8_t /* status */)>;
extern void btm_gen_resolvable_private_addr(
    base::Callback<void(uint8_t[8])> cb);


extern fixed_queue_t* btu_general_alarm_queue;
extern fixed_queue_t* btu_general_alarm_queue;


@@ -69,17 +71,6 @@ namespace {
void DoNothing(uint8_t) {}
void DoNothing(uint8_t) {}
void DoNothing2(uint8_t, uint8_t) {}
void DoNothing2(uint8_t, uint8_t) {}


std::queue<base::Callback<void(tBTM_RAND_ENC* p)>>* rand_gen_inst_id = nullptr;

/* RPA generation completion callback for each adv instance. Will continue write
 * the new RPA into controller. */
void btm_ble_multi_adv_gen_rpa_cmpl(tBTM_RAND_ENC* p) {
  /* Retrieve the index of adv instance from stored Q */
  base::Callback<void(tBTM_RAND_ENC * p)> cb = rand_gen_inst_id->front();
  rand_gen_inst_id->pop();
  cb.Run(p);
}

bool is_legacy_connectable(uint16_t advertising_event_properties) {
bool is_legacy_connectable(uint16_t advertising_event_properties) {
  if (((advertising_event_properties & 0x10) != 0) &&
  if (((advertising_event_properties & 0x10) != 0) &&
      ((advertising_event_properties & 0x01) != 0)) {
      ((advertising_event_properties & 0x01) != 0)) {
@@ -135,24 +126,23 @@ class BleAdvertisingManagerImpl
    }
    }
  }
  }


  void OnRpaGenerationComplete(uint8_t inst_id, tBTM_RAND_ENC* p) {
  void OnRpaGenerationComplete(uint8_t inst_id, uint8_t rand[8]) {
    LOG(INFO) << "inst_id = " << +inst_id;
    LOG(INFO) << "inst_id = " << +inst_id;


    AdvertisingInstance* p_inst = &adv_inst[inst_id];
    AdvertisingInstance* p_inst = &adv_inst[inst_id];
    if (!p) return;


    p->param_buf[2] &= (~BLE_RESOLVE_ADDR_MASK);
    rand[2] &= (~BLE_RESOLVE_ADDR_MASK);
    p->param_buf[2] |= BLE_RESOLVE_ADDR_MSB;
    rand[2] |= BLE_RESOLVE_ADDR_MSB;


    p_inst->rpa[2] = p->param_buf[0];
    p_inst->rpa[2] = rand[0];
    p_inst->rpa[1] = p->param_buf[1];
    p_inst->rpa[1] = rand[1];
    p_inst->rpa[0] = p->param_buf[2];
    p_inst->rpa[0] = rand[2];


    BT_OCTET16 irk;
    BT_OCTET16 irk;
    BTM_GetDeviceIDRoot(irk);
    BTM_GetDeviceIDRoot(irk);
    tSMP_ENC output;
    tSMP_ENC output;


    if (!SMP_Encrypt(irk, BT_OCTET16_LEN, p->param_buf, 3, &output))
    if (!SMP_Encrypt(irk, BT_OCTET16_LEN, rand, 3, &output))
      LOG_ASSERT(false) << "SMP_Encrypt failed";
      LOG_ASSERT(false) << "SMP_Encrypt failed";


    /* set hash to be LSB of rpAddress */
    /* set hash to be LSB of rpAddress */
@@ -166,14 +156,9 @@ class BleAdvertisingManagerImpl
  }
  }


  void ConfigureRpa(uint8_t inst_id) {
  void ConfigureRpa(uint8_t inst_id) {
    if (rand_gen_inst_id == nullptr)
    btm_gen_resolvable_private_addr(
      rand_gen_inst_id =
          new std::queue<base::Callback<void(tBTM_RAND_ENC * p)>>();

    rand_gen_inst_id->push(
        Bind(&BleAdvertisingManagerImpl::OnRpaGenerationComplete,
        Bind(&BleAdvertisingManagerImpl::OnRpaGenerationComplete,
             base::Unretained(this), inst_id));
             base::Unretained(this), inst_id));
    btm_gen_resolvable_private_addr((void*)btm_ble_multi_adv_gen_rpa_cmpl);
  }
  }


  void RegisterAdvertiser(
  void RegisterAdvertiser(
Loading