From bda5444fcdb144d330dc54e3abcf803a01b2de85 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Fri, 2 May 2025 12:05:05 -0700 Subject: [PATCH] Remove g3 BlePeripheral subclass. PiperOrigin-RevId: 754087426 --- internal/platform/implementation/ble_v2.h | 8 +++ internal/platform/implementation/g3/BUILD | 1 + internal/platform/implementation/g3/ble_v2.cc | 68 +++++++++++-------- internal/platform/implementation/g3/ble_v2.h | 38 ++++------- .../implementation/g3/bluetooth_adapter.h | 9 ++- internal/platform/medium_environment.cc | 1 - 6 files changed, 67 insertions(+), 58 deletions(-) diff --git a/internal/platform/implementation/ble_v2.h b/internal/platform/implementation/ble_v2.h index 98bd0281..669b6517 100644 --- a/internal/platform/implementation/ble_v2.h +++ b/internal/platform/implementation/ble_v2.h @@ -110,11 +110,19 @@ class BlePeripheral { // the BLE address is rotated. virtual UniqueId GetUniqueId() const { return unique_id_; }; + // Sets platform specific data that can be retrieved by `GetPlatformData()`. + void SetPlatformData(void* platform_data) { + platform_data_ = platform_data; + } + + void* GetPlatformData() const { return platform_data_; } + bool IsSet() const { return unique_id_ != 0 || address_.IsSet(); } private: UniqueId unique_id_ = 0; MacAddress address_; + void* platform_data_ = nullptr; }; // https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic diff --git a/internal/platform/implementation/g3/BUILD b/internal/platform/implementation/g3/BUILD index e7fb7fc2..f1fea3fd 100644 --- a/internal/platform/implementation/g3/BUILD +++ b/internal/platform/implementation/g3/BUILD @@ -90,6 +90,7 @@ cc_library( ":types", "//internal/platform:base", "//internal/platform:cancellation_flag", + "//internal/platform:mac_address", "//internal/platform:test_util", "//internal/platform:types", "//internal/platform:uuid", diff --git a/internal/platform/implementation/g3/ble_v2.cc b/internal/platform/implementation/g3/ble_v2.cc index 300f6d4b..e3b51bc9 100644 --- a/internal/platform/implementation/g3/ble_v2.cc +++ b/internal/platform/implementation/g3/ble_v2.cc @@ -39,6 +39,7 @@ #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/g3/bluetooth_adapter.h" #include "internal/platform/logging.h" +#include "internal/platform/mac_address.h" #include "internal/platform/medium_environment.h" #include "internal/platform/prng.h" #include "internal/platform/uuid.h" @@ -68,24 +69,13 @@ std::string TxPowerLevelToName(TxPowerLevel power_mode) { } // namespace -BleV2Peripheral::BleV2Peripheral(BluetoothAdapter* adapter) - : adapter_(*adapter) {} - -std::string BleV2Peripheral::GetAddress() const { - return adapter_.GetMacAddress(); -} - -api::ble_v2::BlePeripheral::UniqueId BleV2Peripheral::GetUniqueId() const { - return adapter_.GetUniqueId(); -} - -BleV2Peripheral* BleV2Socket::GetRemotePeripheral() { +api::ble_v2::BlePeripheral* BleV2Socket::GetRemotePeripheral() { BleV2Socket* remote_socket = GetRemoteSocket(); if (remote_socket == nullptr || remote_socket->adapter_ == nullptr || remote_socket->adapter_->GetBleV2Medium() == nullptr) { return nullptr; } - return &(static_cast(remote_socket->adapter_->GetBleV2Medium()) + return &(dynamic_cast(remote_socket->adapter_->GetBleV2Medium()) ->GetPeripheral()); } @@ -158,7 +148,8 @@ Exception BleV2ServerSocket::DoClose() { } BleV2Medium::BleV2Medium(api::BluetoothAdapter& adapter) - : adapter_(static_cast(&adapter)) { + : adapter_(static_cast(&adapter)), + peripheral_(adapter_->GetUniqueId(), adapter_->mac_address()) { adapter_->SetBleV2Medium(this); is_extended_advertisements_available_ = MediumEnvironment::Instance().IsBleExtendedAdvertisementsAvailable(); @@ -369,14 +360,21 @@ bool BleV2Medium::GetRemotePeripheral(const std::string& mac_address, return true; } } - BleV2Medium* remote_medium = static_cast( + BleV2Medium* remote_medium = dynamic_cast( MediumEnvironment::Instance().FindBleV2Medium(mac_address)); if (remote_medium == nullptr) { return false; } - auto id = remote_medium->GetPeripheral().GetUniqueId(); + api::ble_v2::BlePeripheral::UniqueId id = + remote_medium->GetPeripheral().GetUniqueId(); + BluetoothAdapter& adapter = remote_medium->GetAdapter(); + MacAddress address; + if (!MacAddress::FromString(adapter.GetMacAddress(), address)) { + return false; + } remote_peripherals_[id] = - std::make_unique(&remote_medium->GetAdapter()); + std::make_unique(id, address); + remote_peripherals_[id]->SetPlatformData(&adapter); callback(*remote_peripherals_[id]); return true; } @@ -390,14 +388,22 @@ bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, return true; } - BleV2Medium* remote_medium = static_cast( + BleV2Medium* remote_medium = dynamic_cast( MediumEnvironment::Instance().FindBleV2Medium(id)); if (remote_medium == nullptr) { NEARBY_LOGS(INFO) << "Peripheral not found, id= " << id; return false; } + BluetoothAdapter& adapter = remote_medium->GetAdapter(); + MacAddress address; + if (!MacAddress::FromString(adapter.GetMacAddress(), address)) { + LOG(ERROR) << "Adapter has invalid mac address: " + << adapter.GetMacAddress(); + return false; + } remote_peripherals_[id] = - std::make_unique(&remote_medium->GetAdapter()); + std::make_unique(id, address); + remote_peripherals_[id]->SetPlatformData(&adapter); callback(*remote_peripherals_[id]); return true; } @@ -405,8 +411,12 @@ bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, BleV2Medium::GattServer::GattServer( BleV2Medium& medium, api::ble_v2::ServerGattConnectionCallback callback) : medium_(medium), - callback_(std::move(callback)), - ble_peripheral_(&medium.GetAdapter()) { + callback_(std::move(callback)) { + BluetoothAdapter& adapter = medium.GetAdapter(); + MacAddress address; + MacAddress::FromString(adapter.GetMacAddress(), address); + ble_peripheral_ = api::ble_v2::BlePeripheral(adapter.GetUniqueId(), address); + ble_peripheral_.SetPlatformData(&adapter); MediumEnvironment::Instance().RegisterGattServer(medium_, &ble_peripheral_, lender_.GetBorrowable()); } @@ -480,7 +490,7 @@ absl::Status BleV2Medium::GattServer::NotifyCharacteristicChanged( } absl::StatusOr BleV2Medium::GattServer::ReadCharacteristic( - const BleV2Peripheral& remote_device, + const api::ble_v2::BlePeripheral& remote_device, const api::ble_v2::GattCharacteristic& characteristic, int offset) { { absl::MutexLock lock(&mutex_); @@ -509,7 +519,7 @@ absl::StatusOr BleV2Medium::GattServer::ReadCharacteristic( } absl::Status BleV2Medium::GattServer::WriteCharacteristic( - const BleV2Peripheral& remote_device, + const api::ble_v2::BlePeripheral& remote_device, const api::ble_v2::GattCharacteristic& characteristic, int offset, absl::string_view data) { if (HasCharacteristic(characteristic)) { @@ -528,7 +538,7 @@ absl::Status BleV2Medium::GattServer::WriteCharacteristic( } bool BleV2Medium::GattServer::AddCharacteristicSubscription( - const BleV2Peripheral& remote_device, + const api::ble_v2::BlePeripheral& remote_device, const api::ble_v2::GattCharacteristic& characteristic, absl::AnyInvocable callback) { absl::MutexLock lock(&mutex_); @@ -542,7 +552,7 @@ bool BleV2Medium::GattServer::AddCharacteristicSubscription( } bool BleV2Medium::GattServer::RemoveCharacteristicSubscription( - const BleV2Peripheral& remote_device, + const api::ble_v2::BlePeripheral& remote_device, const api::ble_v2::GattCharacteristic& characteristic) { absl::MutexLock lock(&mutex_); const auto it = characteristics_.find(characteristic); @@ -585,7 +595,7 @@ BleV2Medium::GattClient::GattClient( api::ble_v2::BlePeripheral& peripheral, Borrowable gatt_server, api::ble_v2::ClientGattConnectionCallback callback) - : peripheral_(static_cast(peripheral)), + : peripheral_(peripheral), gatt_server_(gatt_server), callback_(std::move(callback)) { Borrowed borrowed = gatt_server_.Borrow(); @@ -794,10 +804,10 @@ std::unique_ptr BleV2Medium::Connect( << ", peripheral=" << &GetPeripheral() << ", service_id=" << service_id; // First, find an instance of remote medium, that exposed this peripheral. - auto& remote_adapter = - static_cast(remote_peripheral).GetAdapter(); + BluetoothAdapter* remote_adapter = + static_cast(remote_peripheral.GetPlatformData()); auto* remote_medium = - static_cast(remote_adapter.GetBleV2Medium()); + dynamic_cast(remote_adapter->GetBleV2Medium()); if (!remote_medium) { return nullptr; } diff --git a/internal/platform/implementation/g3/ble_v2.h b/internal/platform/implementation/g3/ble_v2.h index 74d6d178..ea2b4353 100644 --- a/internal/platform/implementation/g3/ble_v2.h +++ b/internal/platform/implementation/g3/ble_v2.h @@ -46,19 +46,6 @@ namespace nearby { namespace g3 { -// BlePeripheral implementation. -class BleV2Peripheral : public api::ble_v2::BlePeripheral { - public: - explicit BleV2Peripheral(BluetoothAdapter* adapter); - std::string GetAddress() const override; - api::ble_v2::BlePeripheral::UniqueId GetUniqueId() const override; - - BluetoothAdapter& GetAdapter() { return adapter_; } - - private: - BluetoothAdapter& adapter_; -}; - class BleV2Socket : public api::ble_v2::BleSocket, public SocketBase { public: explicit BleV2Socket(BluetoothAdapter* adapter) : adapter_(adapter) {} @@ -84,7 +71,8 @@ class BleV2Socket : public api::ble_v2::BleSocket, public SocketBase { // Returns valid BlePeripheral pointer if there is a connection, and // nullptr otherwise. - BleV2Peripheral* GetRemotePeripheral() override ABSL_LOCKS_EXCLUDED(mutex_); + api::ble_v2::BlePeripheral* GetRemotePeripheral() override + ABSL_LOCKS_EXCLUDED(mutex_); private: BluetoothAdapter* adapter_ = nullptr; // Our Adapter. Read only. @@ -204,7 +192,7 @@ class BleV2Medium : public api::ble_v2::BleMedium { BluetoothAdapter& GetAdapter() { return *adapter_; } - BleV2Peripheral& GetPeripheral() { return peripheral_; } + api::ble_v2::BlePeripheral& GetPeripheral() { return peripheral_; } bool GetRemotePeripheral(const std::string& mac_address, GetRemotePeripheralCallback callback) override; @@ -244,20 +232,20 @@ class BleV2Medium : public api::ble_v2::BleMedium { const std::vector& characteristic_uuids); absl::StatusOr ReadCharacteristic( - const BleV2Peripheral& remote_device, + const api::ble_v2::BlePeripheral& remote_device, const api::ble_v2::GattCharacteristic& characteristic, int offset); absl::Status WriteCharacteristic( - const BleV2Peripheral& remote_device, + const api::ble_v2::BlePeripheral& remote_device, const api::ble_v2::GattCharacteristic& characteristic, int offset, absl::string_view data); bool AddCharacteristicSubscription( - const BleV2Peripheral& remote_device, + const api::ble_v2::BlePeripheral& remote_device, const api::ble_v2::GattCharacteristic& characteristic, absl::AnyInvocable); bool RemoveCharacteristicSubscription( - const BleV2Peripheral& remote_device, + const api::ble_v2::BlePeripheral& remote_device, const api::ble_v2::GattCharacteristic& characteristic); bool HasCharacteristic( @@ -267,14 +255,14 @@ class BleV2Medium : public api::ble_v2::BleMedium { void Disconnect(GattClient* client); private: - using SubscriberKey = - std::pair; + using SubscriberKey = std::pair; using SubscriberCallback = absl::AnyInvocable; absl::Mutex mutex_; BleV2Medium& medium_; api::ble_v2::ServerGattConnectionCallback callback_; - BleV2Peripheral ble_peripheral_; + api::ble_v2::BlePeripheral ble_peripheral_; absl::flat_hash_map> characteristics_ ABSL_GUARDED_BY(mutex_); @@ -323,7 +311,7 @@ class BleV2Medium : public api::ble_v2::BleMedium { // disconnected/*false*/, the instance needs to be created again to bring // it alive. std::atomic_bool is_connection_alive_ = true; - BleV2Peripheral& peripheral_; + api::ble_v2::BlePeripheral& peripheral_; Borrowable gatt_server_; api::ble_v2::ClientGattConnectionCallback callback_; }; @@ -331,9 +319,9 @@ class BleV2Medium : public api::ble_v2::BleMedium { bool IsStopped(Borrowable server); absl::Mutex mutex_; BluetoothAdapter* adapter_; // Our device adapter; read-only. - BleV2Peripheral peripheral_{adapter_}; + api::ble_v2::BlePeripheral peripheral_; absl::flat_hash_map> + std::unique_ptr> remote_peripherals_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map server_sockets_ ABSL_GUARDED_BY(mutex_); diff --git a/internal/platform/implementation/g3/bluetooth_adapter.h b/internal/platform/implementation/g3/bluetooth_adapter.h index e199d488..1d7df9fa 100644 --- a/internal/platform/implementation/g3/bluetooth_adapter.h +++ b/internal/platform/implementation/g3/bluetooth_adapter.h @@ -15,6 +15,7 @@ #ifndef PLATFORM_IMPL_G3_BLUETOOTH_ADAPTER_H_ #define PLATFORM_IMPL_G3_BLUETOOTH_ADAPTER_H_ +#include #include #include #include @@ -27,6 +28,7 @@ #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/g3/single_thread_executor.h" +#include "internal/platform/mac_address.h" namespace nearby { namespace g3 { @@ -111,7 +113,8 @@ class BluetoothAdapter : public api::BluetoothAdapter { ABSL_LOCKS_EXCLUDED(mutex_); // Returns BT MAC address assigned to this adapter. - std::string GetMacAddress() const override { return mac_address_; } + std::string GetMacAddress() const override { return mac_address_.ToString(); } + MacAddress mac_address() const { return mac_address_; } BluetoothDevice& GetDevice() { return device_; } @@ -129,7 +132,7 @@ class BluetoothAdapter : public api::BluetoothAdapter { api::ble_v2::BleMedium* GetBleV2Medium() { return ble_v2_medium_; } void SetMacAddress(absl::string_view mac_address) { - mac_address_ = std::string(mac_address); + MacAddress::FromString(mac_address, mac_address_); } std::uint64_t GetUniqueId() { return unique_id_; } @@ -141,7 +144,7 @@ class BluetoothAdapter : public api::BluetoothAdapter { api::BluetoothClassicMedium* bluetooth_classic_medium_ = nullptr; api::BleMedium* ble_medium_ = nullptr; api::ble_v2::BleMedium* ble_v2_medium_ = nullptr; - std::string mac_address_; + MacAddress mac_address_; ScanMode mode_ ABSL_GUARDED_BY(mutex_) = ScanMode::kNone; std::string name_ ABSL_GUARDED_BY(mutex_) = "unknown G3 BT device"; bool enabled_ ABSL_GUARDED_BY(mutex_) = true; diff --git a/internal/platform/medium_environment.cc b/internal/platform/medium_environment.cc index de8d091e..4b3064c3 100644 --- a/internal/platform/medium_environment.cc +++ b/internal/platform/medium_environment.cc @@ -28,7 +28,6 @@ #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" -#include "absl/types/optional.h" #include "internal/platform/borrowable.h" #include "internal/platform/byte_array.h" #include "internal/platform/count_down_latch.h"