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

Commit 78082cdf authored by Hui Peng's avatar Hui Peng
Browse files

[Invisalign2] Make BTA_DmXXX synchronous

BTA_DmXXX APIs in bta_dm_sec_api.cc are currently implemented
as asynchronous by posting a task to main thread. They are
called from BTIF which are already running on the main thread.
Here they are posted a second time.

This change make them synchronous and avoid posting to main
thread again.

Bug: 311196228
Bug: 301661850
Test: atest net_test_btif_stack
Change-Id: I193f283125c56ce263b3e2e0fd4ff2f6fb6f4dd6
parent 3e0b7824
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -201,10 +201,13 @@ cc_library_static {
        "-fvisibility=default",
    ],
    static_libs: [
        "bluetooth_flags_c_lib",
        "lib-bt-packets",
        "libbase",
        "libbt-platform-protos-lite",
        "libbt_shim_bridge",
        "libcom.android.sysprop.bluetooth.wrapped",
        "server_configurable_flags",
    ],
    apex_available: [
        "com.android.btservices",
@@ -1208,6 +1211,7 @@ cc_test {
        "libbase",
        "libcrypto",
        "liblog",
        "server_configurable_flags",
    ],
    static_libs: [
        "libbluetooth-types",
+91 −26
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@

#include <base/functional/bind.h>

#include "android_bluetooth_flags.h"
#include "bta/dm/bta_dm_sec_int.h"
#include "stack/btm/btm_sec.h"
#include "stack/include/bt_octets.h"
@@ -34,15 +35,23 @@
/** This function initiates a bonding procedure with a peer device */
void BTA_DmBond(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
                tBT_TRANSPORT transport, tBT_DEVICE_TYPE device_type) {
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_bond(bd_addr, addr_type, transport, device_type);
  } else {
    do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_bond, bd_addr, addr_type,
                                                transport, device_type));
  }
}

/** This function cancels the bonding procedure with a peer device
 */
void BTA_DmBondCancel(const RawAddress& bd_addr) {
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_bond_cancel(bd_addr);
  } else {
    do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_bond_cancel, bd_addr));
  }
}

/*******************************************************************************
 *
@@ -67,9 +76,13 @@ void BTA_DmPinReply(const RawAddress& bd_addr, bool accept, uint8_t pin_len,
    memcpy(msg->p_pin, p_pin, pin_len);
  }

  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_pin_reply(std::move(msg));
  } else {
    do_in_main_thread(FROM_HERE,
                      base::Bind(bta_dm_pin_reply, base::Passed(&msg)));
  }
}

/*******************************************************************************
 *
@@ -85,8 +98,12 @@ void BTA_DmPinReply(const RawAddress& bd_addr, bool accept, uint8_t pin_len,
 *
 ******************************************************************************/
void BTA_DmLocalOob(void) {
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    BTM_ReadLocalOobData();
  } else {
    do_in_main_thread(FROM_HERE, base::BindOnce(BTM_ReadLocalOobData));
  }
}

/*******************************************************************************
 *
@@ -99,7 +116,12 @@ void BTA_DmLocalOob(void) {
 *
 ******************************************************************************/
void BTA_DmConfirm(const RawAddress& bd_addr, bool accept) {
  do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_confirm, bd_addr, accept));
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_confirm(bd_addr, accept);
  } else {
    do_in_main_thread(FROM_HERE,
                      base::BindOnce(bta_dm_confirm, bd_addr, accept));
  }
}

/*******************************************************************************
@@ -133,14 +155,22 @@ void BTA_DmAddDevice(const RawAddress& bd_addr, DEV_CLASS dev_class,
  memset(msg->bd_name, 0, BD_NAME_LEN + 1);
  msg->pin_length = pin_length;

  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_add_device(std::move(msg));
  } else {
    do_in_main_thread(FROM_HERE,
                      base::Bind(bta_dm_add_device, base::Passed(&msg)));
  }
}

/** This function removes a device fromthe security database list of peer
 * device. It manages unpairing even while connected */
tBTA_STATUS BTA_DmRemoveDevice(const RawAddress& bd_addr) {
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_remove_device(bd_addr);
  } else {
    do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_remove_device, bd_addr));
  }
  return BTA_SUCCESS;
}

@@ -162,9 +192,13 @@ tBTA_STATUS BTA_DmRemoveDevice(const RawAddress& bd_addr) {
 ******************************************************************************/
void BTA_DmAddBleKey(const RawAddress& bd_addr, tBTA_LE_KEY_VALUE* p_le_key,
                     tBTM_LE_KEY_TYPE key_type) {
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_add_blekey(bd_addr, *p_le_key, key_type);
  } else {
    do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_add_blekey, bd_addr,
                                                *p_le_key, key_type));
  }
}

/*******************************************************************************
 *
@@ -183,9 +217,13 @@ void BTA_DmAddBleKey(const RawAddress& bd_addr, tBTA_LE_KEY_VALUE* p_le_key,
 ******************************************************************************/
void BTA_DmAddBleDevice(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
                        tBT_DEVICE_TYPE dev_type) {
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_add_ble_device(bd_addr, addr_type, dev_type);
  } else {
    do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_add_ble_device, bd_addr,
                                                addr_type, dev_type));
  }
}

/*******************************************************************************
 *
@@ -203,8 +241,13 @@ void BTA_DmAddBleDevice(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
 ******************************************************************************/
void BTA_DmBlePasskeyReply(const RawAddress& bd_addr, bool accept,
                           uint32_t passkey) {
  do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_ble_passkey_reply, bd_addr,
                                              accept, accept ? passkey : 0));
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_ble_passkey_reply(bd_addr, accept, accept ? passkey : 0);
  } else {
    do_in_main_thread(FROM_HERE,
                      base::BindOnce(bta_dm_ble_passkey_reply, bd_addr, accept,
                                     accept ? passkey : 0));
  }
}

/*******************************************************************************
@@ -221,8 +264,12 @@ void BTA_DmBlePasskeyReply(const RawAddress& bd_addr, bool accept,
 *
 ******************************************************************************/
void BTA_DmBleConfirmReply(const RawAddress& bd_addr, bool accept) {
  do_in_main_thread(FROM_HERE,
                    base::BindOnce(bta_dm_ble_confirm_reply, bd_addr, accept));
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_ble_confirm_reply(bd_addr, accept);
  } else {
    do_in_main_thread(
        FROM_HERE, base::BindOnce(bta_dm_ble_confirm_reply, bd_addr, accept));
  }
}

/*******************************************************************************
@@ -239,7 +286,12 @@ void BTA_DmBleConfirmReply(const RawAddress& bd_addr, bool accept) {
 ******************************************************************************/
void BTA_DmBleSecurityGrant(const RawAddress& bd_addr,
                            tBTA_DM_BLE_SEC_GRANT res) {
  do_in_main_thread(FROM_HERE, base::BindOnce(BTM_SecurityGrant, bd_addr, res));
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    BTM_SecurityGrant(bd_addr, res);
  } else {
    do_in_main_thread(FROM_HERE,
                      base::BindOnce(BTM_SecurityGrant, bd_addr, res));
  }
}

/*******************************************************************************
@@ -268,8 +320,13 @@ void BTA_DmSetEncryption(const RawAddress& bd_addr, tBT_TRANSPORT transport,
                         tBTA_DM_ENCRYPT_CBACK* p_callback,
                         tBTM_BLE_SEC_ACT sec_act) {
  LOG_VERBOSE("%s", __func__);
  do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_set_encryption, bd_addr,
                                              transport, p_callback, sec_act));
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_set_encryption(bd_addr, transport, p_callback, sec_act);
  } else {
    do_in_main_thread(FROM_HERE,
                      base::BindOnce(bta_dm_set_encryption, bd_addr, transport,
                                     p_callback, sec_act));
  }
}

/*******************************************************************************
@@ -286,9 +343,13 @@ void BTA_DmSetEncryption(const RawAddress& bd_addr, tBT_TRANSPORT transport,
 ******************************************************************************/
void BTA_DmSirkSecCbRegister(tBTA_DM_SEC_CBACK* p_cback) {
  LOG_DEBUG("");
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_ble_sirk_sec_cb_register(p_cback);
  } else {
    do_in_main_thread(FROM_HERE,
                      base::BindOnce(bta_dm_ble_sirk_sec_cb_register, p_cback));
  }
}

/*******************************************************************************
 *
@@ -305,8 +366,12 @@ void BTA_DmSirkSecCbRegister(tBTA_DM_SEC_CBACK* p_cback) {
 ******************************************************************************/
void BTA_DmSirkConfirmDeviceReply(const RawAddress& bd_addr, bool accept) {
  LOG_DEBUG("");
  if (IS_FLAG_ENABLED(synchronous_bta_sec)) {
    bta_dm_ble_sirk_confirm_device_reply(bd_addr, accept);
  } else {
    do_in_main_thread(
        FROM_HERE,
        base::BindOnce(bta_dm_ble_sirk_confirm_device_reply, bd_addr, accept));
  }
}