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

Commit 3724657d authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Supports start CS distance measurement from peripheral" into main

parents 8703ebe1 5a096122
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -37,6 +37,8 @@ public:
  virtual void OnVendorSpecificReply(
          const RawAddress& address,
          const std::vector<VendorSpecificCharacteristic>& vendor_specific_reply) = 0;
  virtual void OnRasServerConnected(const RawAddress& identity_address) = 0;
  virtual void OnRasServerDisconnected(const RawAddress& identity_address) = 0;
};

class RasServer {
+20 −4
Original line number Diff line number Diff line
@@ -227,16 +227,32 @@ public:
      log::warn("Create new tracker");
    }
    trackers_[address].conn_id_ = p_data->conn.conn_id;

    RawAddress identity_address = p_data->conn.remote_bda;
    tBLE_ADDR_TYPE address_type = BLE_ADDR_PUBLIC_ID;
    btm_random_pseudo_to_identity_addr(&identity_address, &address_type);
    // TODO: optimize, remove this event, initialize the tracker within the GD on demand.
    callbacks_->OnRasServerConnected(identity_address);
  }

  void OnGattDisconnect(tBTA_GATTS* p_data) {
    auto address = p_data->conn.remote_bda;
    log::info("Address: {}, conn_id:{}", address, p_data->conn.conn_id);
    if (trackers_.find(address) != trackers_.end()) {
      trackers_.erase(address);
    auto remote_bda = p_data->conn.remote_bda;
    log::info("Address: {}, conn_id:{}", remote_bda, p_data->conn.conn_id);
    if (trackers_.find(remote_bda) != trackers_.end()) {
      NotifyRasServerDisconnected(remote_bda);
      trackers_.erase(remote_bda);
    }
  }

  void NotifyRasServerDisconnected(const RawAddress& remote_bda) {
    tBLE_BD_ADDR ble_identity_bd_addr;
    ble_identity_bd_addr.bda = remote_bda;
    ble_identity_bd_addr.type = BLE_ADDR_RANDOM;
    btm_random_pseudo_to_identity_addr(&ble_identity_bd_addr.bda, &ble_identity_bd_addr.type);

    callbacks_->OnRasServerDisconnected(ble_identity_bd_addr.bda);
  }

  void OnGattServerRegister(tBTA_GATTS* p_data) {
    tGATT_STATUS status = p_data->reg_oper.status;
    log::info("status: {}", gatt_status_text(p_data->reg_oper.status));
+79 −20
Original line number Diff line number Diff line
@@ -142,6 +142,7 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {

  struct CsTracker {
    Address address;
    hci::Role local_hci_role;
    uint16_t local_counter;
    uint16_t remote_counter;
    CsRole role;
@@ -257,7 +258,8 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
  }

  void start_distance_measurement(const Address address, uint16_t connection_handle,
                                  uint16_t interval, DistanceMeasurementMethod method) {
                                  hci::Role local_hci_role, uint16_t interval,
                                  DistanceMeasurementMethod method) {
    log::info("Address:{}, method:{}", address, method);

    // Remove this check if we support any connection less method
@@ -286,14 +288,14 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
        }
      } break;
      case METHOD_CS: {
        init_cs_tracker(address, connection_handle, interval);
        init_cs_tracker(address, connection_handle, local_hci_role, interval);
        start_distance_measurement_with_cs(address, connection_handle);
      } break;
    }
  }

  void init_cs_tracker(const Address& cs_remote_address, uint16_t connection_handle,
                       uint16_t interval) {
                       hci::Role local_hci_role, uint16_t interval) {
    if (cs_trackers_.find(connection_handle) != cs_trackers_.end() &&
        cs_trackers_[connection_handle].address != cs_remote_address) {
      log::debug("Remove old tracker for {}", cs_remote_address);
@@ -311,6 +313,7 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
    cs_trackers_[connection_handle].local_start = true;
    cs_trackers_[connection_handle].measurement_ongoing = true;
    cs_trackers_[connection_handle].waiting_for_start_callback = true;
    cs_trackers_[connection_handle].local_hci_role = local_hci_role;
  }

  void start_distance_measurement_with_cs(const Address& cs_remote_address,
@@ -378,7 +381,7 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
    }
  }

  void handle_ras_connected_event(
  void handle_ras_client_connected_event(
          const Address address, uint16_t connection_handle, uint16_t att_handle,
          const std::vector<hal::VendorSpecificCharacteristic> vendor_specific_data) {
    log::info(
@@ -404,7 +407,7 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
    start_distance_measurement_with_cs(tracker.address, connection_handle);
  }

  void handle_ras_disconnected_event(const Address address) {
  void handle_ras_client_disconnected_event(const Address address) {
    log::info("address:{}", address);
    for (auto it = cs_trackers_.begin(); it != cs_trackers_.end();) {
      if (it->second.address == address) {
@@ -421,16 +424,61 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
    }
  }

  void handle_vendor_specific_reply(
          const Address address, uint16_t connection_handle,
  void handle_ras_server_vendor_specific_reply(
          const Address& address, uint16_t connection_handle,
          const std::vector<hal::VendorSpecificCharacteristic> vendor_specific_reply) {
    cs_trackers_[connection_handle].address = address;
    if (cs_trackers_.find(connection_handle) == cs_trackers_.end()) {
      log::info("no cs tracker found for {}", connection_handle);
      return;
    }
    if (cs_trackers_[connection_handle].address != address) {
      log::info("the cs tracker address was changed as {}, not {}.",
                cs_trackers_[connection_handle].address, address);
      return;
    }
    if (ranging_hal_->IsBound()) {
      ranging_hal_->HandleVendorSpecificReply(connection_handle, vendor_specific_reply);
      return;
    }
  }

  void handle_ras_server_connected(const Address& identity_address, uint16_t connection_handle,
                                   hci::Role local_hci_role) {
    log::info("initialize the cs tracker for {}", connection_handle);
    // create CS tracker to serve the ras_server
    if (cs_trackers_.find(connection_handle) != cs_trackers_.end()) {
      if (cs_trackers_[connection_handle].local_start &&
          cs_trackers_[connection_handle].measurement_ongoing) {
        log::info("cs tracker for {} is being used for measurement.", identity_address);
        return;
      }
    }
    if (cs_trackers_[connection_handle].address != identity_address) {
      log::debug("Remove old tracker for {}", identity_address);
      cs_trackers_.erase(connection_handle);
    }

    cs_trackers_[connection_handle].address = identity_address;
    cs_trackers_[connection_handle].local_start = false;
    cs_trackers_[connection_handle].local_hci_role = local_hci_role;
  }

  void handle_ras_server_disconnected(const Address& identity_address, uint16_t connection_handle) {
    if (cs_trackers_.find(connection_handle) == cs_trackers_.end()) {
      log::info("no CS tracker available.");
      return;
    }
    if (cs_trackers_[connection_handle].address != identity_address) {
      log::info("cs tracker connection is associated with device {}, not device {}",
                cs_trackers_[connection_handle].address, identity_address);
      return;
    }
    if (!cs_trackers_[connection_handle].local_start) {
      log::info("erase cs tracker for {}, as ras server is disconnected.", connection_handle);
      cs_trackers_.erase(connection_handle);
    }
  }

  void handle_vendor_specific_reply_complete(const Address address, uint16_t connection_handle,
                                             bool success) {
    log::info("address:{}, connection_handle:0x{:04x}, success:{}", address, connection_handle,
@@ -631,11 +679,9 @@ struct DistanceMeasurementManager::impl : bluetooth::hal::RangingHalCallback {
    }
    send_le_cs_set_default_settings(event_view.GetConnectionHandle());
    if (cs_trackers_.find(connection_handle) != cs_trackers_.end() &&
        cs_trackers_[connection_handle].local_start) {
        cs_trackers_[connection_handle].local_hci_role == hci::Role::CENTRAL) {
      // send the cmd from the BLE central only.
      send_le_cs_security_enable(connection_handle);
    } else {
      // TODO(b/361567359): problematic? HACK_ may not get the identity address for sure.
      cs_trackers_[connection_handle].address = acl_manager_->HACK_GetLeAddress(connection_handle);
    }

    if (event_view.GetOptionalSubfeaturesSupported().phase_based_ranging_ == 0x01) {
@@ -1542,10 +1588,11 @@ void DistanceMeasurementManager::RegisterDistanceMeasurementCallbacks(

void DistanceMeasurementManager::StartDistanceMeasurement(const Address& address,
                                                          uint16_t connection_handle,
                                                          hci::Role local_hci_role,
                                                          uint16_t interval,
                                                          DistanceMeasurementMethod method) {
  CallOn(pimpl_.get(), &impl::start_distance_measurement, address, connection_handle, interval,
         method);
  CallOn(pimpl_.get(), &impl::start_distance_measurement, address, connection_handle,
         local_hci_role, interval, method);
}

void DistanceMeasurementManager::StopDistanceMeasurement(const Address& address,
@@ -1554,24 +1601,36 @@ void DistanceMeasurementManager::StopDistanceMeasurement(const Address& address,
  CallOn(pimpl_.get(), &impl::stop_distance_measurement, address, connection_handle, method);
}

void DistanceMeasurementManager::HandleRasConnectedEvent(
void DistanceMeasurementManager::HandleRasClientConnectedEvent(
        const Address& address, uint16_t connection_handle, uint16_t att_handle,
        const std::vector<hal::VendorSpecificCharacteristic>& vendor_specific_data) {
  CallOn(pimpl_.get(), &impl::handle_ras_connected_event, address, connection_handle, att_handle,
         vendor_specific_data);
  CallOn(pimpl_.get(), &impl::handle_ras_client_connected_event, address, connection_handle,
         att_handle, vendor_specific_data);
}

void DistanceMeasurementManager::HandleRasDisconnectedEvent(const Address& address) {
  CallOn(pimpl_.get(), &impl::handle_ras_disconnected_event, address);
void DistanceMeasurementManager::HandleRasClientDisconnectedEvent(const Address& address) {
  CallOn(pimpl_.get(), &impl::handle_ras_client_disconnected_event, address);
}

void DistanceMeasurementManager::HandleVendorSpecificReply(
        const Address& address, uint16_t connection_handle,
        const std::vector<hal::VendorSpecificCharacteristic>& vendor_specific_reply) {
  CallOn(pimpl_.get(), &impl::handle_vendor_specific_reply, address, connection_handle,
  CallOn(pimpl_.get(), &impl::handle_ras_server_vendor_specific_reply, address, connection_handle,
         vendor_specific_reply);
}

void DistanceMeasurementManager::HandleRasServerConnected(const Address& identity_address,
                                                          uint16_t connection_handle,
                                                          hci::Role local_hci_role) {
  CallOn(pimpl_.get(), &impl::handle_ras_server_connected, identity_address, connection_handle,
         local_hci_role);
}

void DistanceMeasurementManager::HandleRasServerDisconnected(
        const bluetooth::hci::Address& identity_address, uint16_t connection_handle) {
  CallOn(pimpl_.get(), &impl::handle_ras_server_disconnected, identity_address, connection_handle);
}

void DistanceMeasurementManager::HandleVendorSpecificReplyComplete(const Address& address,
                                                                   uint16_t connection_handle,
                                                                   bool success) {
+8 −3
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

#include "address.h"
#include "hal/ranging_hal.h"
#include "hci/hci_packets.h"
#include "module.h"

namespace bluetooth {
@@ -79,17 +80,21 @@ public:
  DistanceMeasurementManager& operator=(const DistanceMeasurementManager&) = delete;

  void RegisterDistanceMeasurementCallbacks(DistanceMeasurementCallbacks* callbacks);
  void StartDistanceMeasurement(const Address&, uint16_t connection_handle, uint16_t interval,
  void StartDistanceMeasurement(const Address&, uint16_t connection_handle,
                                hci::Role local_hci_role, uint16_t interval,
                                DistanceMeasurementMethod method);
  void StopDistanceMeasurement(const Address& address, uint16_t connection_handle,
                               DistanceMeasurementMethod method);
  void HandleRasConnectedEvent(
  void HandleRasClientConnectedEvent(
          const Address& address, uint16_t connection_handle, uint16_t att_handle,
          const std::vector<hal::VendorSpecificCharacteristic>& vendor_specific_data);
  void HandleRasDisconnectedEvent(const Address& address);
  void HandleRasClientDisconnectedEvent(const Address& address);
  void HandleVendorSpecificReply(
          const Address& address, uint16_t connection_handle,
          const std::vector<hal::VendorSpecificCharacteristic>& vendor_specific_reply);
  void HandleRasServerConnected(const Address& identity_address, uint16_t connection_handle,
                                hci::Role local_hci_role);
  void HandleRasServerDisconnected(const Address& identity_address, uint16_t connection_handle);
  void HandleVendorSpecificReplyComplete(const Address& address, uint16_t connection_handle,
                                         bool success);
  void HandleRemoteData(const Address& address, uint16_t connection_handle,
+34 −12
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include "bta/include/bta_ras_api.h"
#include "btif/include/btif_common.h"
#include "hci/distance_measurement_manager.h"
#include "hci/hci_packets.h"
#include "main/shim/entry.h"
#include "main/shim/helpers.h"
#include "stack/include/acl_api.h"
@@ -49,9 +50,12 @@ public:
   * @param bd_addr could be random, rpa or identity address.
   * @return BLE ACL handle
   */
  uint16_t GetConnectionHandle(const RawAddress& bd_addr) {
  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) {
        *hci_role = p_sec_dev_rec->role_central ? hci::Role::CENTRAL : hci::Role::PERIPHERAL;
      }
      return p_sec_dev_rec->get_ble_hci_handle();
    }
    return kIllegalConnectionHandle;
@@ -69,8 +73,10 @@ public:
  void DoStartDistanceMeasurement(RawAddress identity_addr, uint16_t interval, uint8_t method) {
    DistanceMeasurementMethod 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(
            bluetooth::ToGdAddress(identity_addr), GetConnectionHandle(identity_addr), interval,
            bluetooth::ToGdAddress(identity_addr), connection_handle, local_hci_role, interval,
            distance_measurement_method);
    if (distance_measurement_method == DistanceMeasurementMethod::METHOD_CS) {
      bluetooth::ras::GetRasClient()->Connect(identity_addr);
@@ -84,7 +90,7 @@ public:

  void DoStopDistanceMeasurement(RawAddress identity_addr, uint8_t method) {
    bluetooth::shim::GetDistanceMeasurementManager()->StopDistanceMeasurement(
            bluetooth::ToGdAddress(identity_addr), GetConnectionHandle(identity_addr),
            bluetooth::ToGdAddress(identity_addr), GetConnectionHandleAndRole(identity_addr),
            static_cast<DistanceMeasurementMethod>(method));
  }

@@ -169,9 +175,9 @@ public:

  // Must be called from main_thread
  // Callbacks of bluetooth::ras::RasServerCallbacks
  void OnVendorSpecificReply(
          const RawAddress& address,
          const std::vector<bluetooth::ras::VendorSpecificCharacteristic>& vendor_specific_reply) {
  void OnVendorSpecificReply(const RawAddress& address,
                             const std::vector<bluetooth::ras::VendorSpecificCharacteristic>&
                                     vendor_specific_reply) override {
    std::vector<bluetooth::hal::VendorSpecificCharacteristic> hal_vendor_specific_characteristics;
    for (auto& characteristic : vendor_specific_reply) {
      bluetooth::hal::VendorSpecificCharacteristic vendor_specific_characteristic;
@@ -181,10 +187,26 @@ public:
      hal_vendor_specific_characteristics.emplace_back(vendor_specific_characteristic);
    }
    bluetooth::shim::GetDistanceMeasurementManager()->HandleVendorSpecificReply(
            bluetooth::ToGdAddress(address), GetConnectionHandle(address),
            bluetooth::ToGdAddress(address), GetConnectionHandleAndRole(address),
            hal_vendor_specific_characteristics);
  }

  // Must be called from main_thread
  // Callbacks of bluetooth::ras::RasServerCallbacks
  void OnRasServerConnected(const RawAddress& identity_address) override {
    hci::Role local_hci_role;
    uint16_t connection_handle = GetConnectionHandleAndRole(identity_address, &local_hci_role);
    bluetooth::shim::GetDistanceMeasurementManager()->HandleRasServerConnected(
            bluetooth::ToGdAddress(identity_address), connection_handle, local_hci_role);
  }

  // Must be called from main_thread
  // Callbacks of bluetooth::ras::RasSeverCallbacks
  void OnRasServerDisconnected(const RawAddress& identity_address) override {
    bluetooth::shim::GetDistanceMeasurementManager()->HandleRasServerDisconnected(
            bluetooth::ToGdAddress(identity_address), GetConnectionHandleAndRole(identity_address));
  }

  // Must be called from main_thread
  // Callbacks of bluetooth::ras::RasClientCallbacks
  void OnConnected(const RawAddress& address, uint16_t att_handle,
@@ -199,26 +221,26 @@ public:
      hal_vendor_specific_characteristics.emplace_back(vendor_specific_characteristic);
    }

    bluetooth::shim::GetDistanceMeasurementManager()->HandleRasConnectedEvent(
            bluetooth::ToGdAddress(address), GetConnectionHandle(address), att_handle,
    bluetooth::shim::GetDistanceMeasurementManager()->HandleRasClientConnectedEvent(
            bluetooth::ToGdAddress(address), GetConnectionHandleAndRole(address), att_handle,
            hal_vendor_specific_characteristics);
  }

  void OnDisconnected(const RawAddress& address) {
    bluetooth::shim::GetDistanceMeasurementManager()->HandleRasDisconnectedEvent(
    bluetooth::shim::GetDistanceMeasurementManager()->HandleRasClientDisconnectedEvent(
            bluetooth::ToGdAddress(address));
  }

  // Must be called from main_thread
  void OnWriteVendorSpecificReplyComplete(const RawAddress& address, bool success) {
    bluetooth::shim::GetDistanceMeasurementManager()->HandleVendorSpecificReplyComplete(
            bluetooth::ToGdAddress(address), GetConnectionHandle(address), success);
            bluetooth::ToGdAddress(address), GetConnectionHandleAndRole(address), success);
  }

  // Must be called from main_thread
  void OnRemoteData(const RawAddress& address, const std::vector<uint8_t>& data) {
    bluetooth::shim::GetDistanceMeasurementManager()->HandleRemoteData(
            bluetooth::ToGdAddress(address), GetConnectionHandle(address), data);
            bluetooth::ToGdAddress(address), GetConnectionHandleAndRole(address), data);
  }

private: