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

Commit b58cae3d authored by Steven Liu's avatar Steven Liu
Browse files

Makes the ras code run in the BT main thread.

Flag: com.android.bluetooth.flags.channel_sounding_in_stack
Bug: 324185011
Bug: 372468999
Test: m com.android.btservice
Change-Id: If935b737d59ab1dc3b87d1eae9ca42c80ffb8652
parent e4f55827
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
#include "stack/include/bt_types.h"
#include "stack/include/btm_ble_addr.h"
#include "stack/include/gap_api.h"
#include "stack/include/main_thread.h"

using namespace bluetooth;
using namespace ::ras;
@@ -102,6 +103,10 @@ public:
  };

  void Initialize() override {
    do_in_main_thread(base::BindOnce(&RasClientImpl::do_initialize, base::Unretained(this)));
  }

  void do_initialize() {
    auto controller = bluetooth::shim::GetController();
    if (controller && !controller->SupportsBleChannelSounding()) {
      log::info("controller does not support channel sounding.");
+5 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include "os/logging/log_adapter.h"
#include "stack/include/bt_types.h"
#include "stack/include/btm_ble_addr.h"
#include "stack/include/main_thread.h"

using namespace bluetooth;
using namespace ::ras;
@@ -77,6 +78,10 @@ public:
  };

  void Initialize() override {
    do_in_main_thread(base::BindOnce(&RasServerImpl::do_initialize, base::Unretained(this)));
  }

  void do_initialize() {
    auto controller = bluetooth::shim::GetController();
    if (controller && !controller->SupportsBleChannelSounding()) {
      log::info("controller does not support channel sounding.");
+25 −17
Original line number Diff line number Diff line
@@ -41,8 +41,10 @@ public:
  void Init() {
    // Register callback
    bluetooth::shim::GetDistanceMeasurementManager()->RegisterDistanceMeasurementCallbacks(this);
    bluetooth::ras::GetRasServer()->RegisterCallbacks(this);
    bluetooth::ras::GetRasClient()->RegisterCallbacks(this);
    do_in_main_thread(base::BindOnce(&bluetooth::ras::RasServer::RegisterCallbacks,
                                     base::Unretained(bluetooth::ras::GetRasServer()), this));
    do_in_main_thread(base::BindOnce(&bluetooth::ras::RasClient::RegisterCallbacks,
                                     base::Unretained(bluetooth::ras::GetRasClient()), this));
  }

  /**
@@ -50,7 +52,8 @@ public:
   * @param bd_addr could be random, rpa or identity address.
   * @return BLE ACL handle
   */
  uint16_t GetConnectionHandleAndRole(const RawAddress& bd_addr, hci::Role* hci_role = nullptr) {
  static uint16_t GetConnectionHandleAndRole(const RawAddress& bd_addr,
                                             hci::Role* hci_role = nullptr) {
    tBTM_SEC_DEV_REC* p_sec_dev_rec = btm_find_dev(bd_addr);
    if (p_sec_dev_rec != nullptr) {
      if (hci_role != nullptr) {
@@ -71,8 +74,7 @@ public:
  }

  void DoStartDistanceMeasurement(RawAddress identity_addr, uint16_t interval, uint8_t method) {
    DistanceMeasurementMethod distance_measurement_method =
            static_cast<DistanceMeasurementMethod>(method);
    auto distance_measurement_method = static_cast<DistanceMeasurementMethod>(method);
    hci::Role local_hci_role;
    uint16_t connection_handle = GetConnectionHandleAndRole(identity_addr, &local_hci_role);
    bluetooth::shim::GetDistanceMeasurementManager()->StartDistanceMeasurement(
@@ -134,13 +136,15 @@ public:
  }

  void OnRasFragmentReady(bluetooth::hci::Address address, uint16_t procedure_counter, bool is_last,
                          std::vector<uint8_t> raw_data) {
    bluetooth::ras::GetRasServer()->PushProcedureData(bluetooth::ToRawAddress(address),
                                                      procedure_counter, is_last, raw_data);
                          std::vector<uint8_t> raw_data) override {
    do_in_main_thread(base::BindOnce(&bluetooth::ras::RasServer::PushProcedureData,
                                     base::Unretained(bluetooth::ras::GetRasServer()),
                                     bluetooth::ToRawAddress(address), procedure_counter, is_last,
                                     std::move(raw_data)));
  }

  void OnVendorSpecificCharacteristics(std::vector<bluetooth::hal::VendorSpecificCharacteristic>
                                               vendor_specific_characteristics) {
                                               vendor_specific_characteristics) override {
    std::vector<bluetooth::ras::VendorSpecificCharacteristic> ras_vendor_specific_characteristics;
    for (auto& characteristic : vendor_specific_characteristics) {
      bluetooth::ras::VendorSpecificCharacteristic vendor_specific_characteristic;
@@ -149,13 +153,14 @@ public:
      vendor_specific_characteristic.value_ = characteristic.value_;
      ras_vendor_specific_characteristics.emplace_back(vendor_specific_characteristic);
    }
    bluetooth::ras::GetRasServer()->SetVendorSpecificCharacteristic(
            ras_vendor_specific_characteristics);
    do_in_main_thread(base::BindOnce(&bluetooth::ras::RasServer::SetVendorSpecificCharacteristic,
                                     base::Unretained(bluetooth::ras::GetRasServer()),
                                     std::move(ras_vendor_specific_characteristics)));
  }

  void OnVendorSpecificReply(bluetooth::hci::Address address,
                             std::vector<bluetooth::hal::VendorSpecificCharacteristic>
                                     vendor_specific_characteristics) {
                                     vendor_specific_characteristics) override {
    std::vector<bluetooth::ras::VendorSpecificCharacteristic> ras_vendor_specific_characteristics;
    for (auto& characteristic : vendor_specific_characteristics) {
      bluetooth::ras::VendorSpecificCharacteristic vendor_specific_characteristic;
@@ -164,13 +169,16 @@ public:
      vendor_specific_characteristic.value_ = characteristic.value_;
      ras_vendor_specific_characteristics.emplace_back(vendor_specific_characteristic);
    }
    bluetooth::ras::GetRasClient()->SendVendorSpecificReply(bluetooth::ToRawAddress(address),
                                                            ras_vendor_specific_characteristics);
    do_in_main_thread(base::BindOnce(&bluetooth::ras::RasClient::SendVendorSpecificReply,
                                     base::Unretained(bluetooth::ras::GetRasClient()),
                                     bluetooth::ToRawAddress(address),
                                     std::move(ras_vendor_specific_characteristics)));
  }

  void OnHandleVendorSpecificReplyComplete(bluetooth::hci::Address address, bool success) {
    bluetooth::ras::GetRasServer()->HandleVendorSpecificReplyComplete(
            bluetooth::ToRawAddress(address), success);
  void OnHandleVendorSpecificReplyComplete(bluetooth::hci::Address address, bool success) override {
    do_in_main_thread(base::BindOnce(&bluetooth::ras::RasServer::HandleVendorSpecificReplyComplete,
                                     base::Unretained(bluetooth::ras::GetRasServer()),
                                     bluetooth::ToRawAddress(address), success));
  }

  // Must be called from main_thread