diff --git a/internal/platform/ble_v2.h b/internal/platform/ble_v2.h index 14c35505..59206e4d 100644 --- a/internal/platform/ble_v2.h +++ b/internal/platform/ble_v2.h @@ -23,7 +23,6 @@ #include #include "absl/base/thread_annotations.h" -#include "absl/container/flat_hash_set.h" #include "absl/functional/any_invocable.h" #include "absl/strings/escaping.h" #include "absl/strings/str_format.h" @@ -462,27 +461,24 @@ class BleV2Medium final { }; struct ServerGattConnectionCallback { - using BlePeripheral = api::ble_v2::BlePeripheral; - using GattCharacteristic = api::ble_v2::GattCharacteristic; - using ReadValueCallback = - api::ble_v2::ServerGattConnectionCallback::ReadValueCallback; - using WriteValueCallback = - api::ble_v2::ServerGattConnectionCallback::WriteValueCallback; - - absl::AnyInvocable + absl::AnyInvocable characteristic_subscription_cb = - nearby::DefaultCallback(); - absl::AnyInvocable + nearby::DefaultCallback(); + absl::AnyInvocable characteristic_unsubscription_cb = - nearby::DefaultCallback(); - absl::AnyInvocable + nearby::DefaultCallback(); + absl::AnyInvocable on_characteristic_read_cb; - absl::AnyInvocable + absl::AnyInvocable on_characteristic_write_cb; }; // TODO(b/231318879): Remove this wrapper callback and use impl callback if diff --git a/internal/platform/ble_v2_test.cc b/internal/platform/ble_v2_test.cc index 80df2c0d..44be2fd8 100644 --- a/internal/platform/ble_v2_test.cc +++ b/internal/platform/ble_v2_test.cc @@ -715,7 +715,7 @@ TEST_F(BleV2MediumTest, GattClientOperatiosOnCharacteristic) { [&](const api::ble_v2::BlePeripheral::UniqueId remote_device_id, const api::ble_v2::GattCharacteristic& characteristic, int offset, absl::string_view data, - BleV2Medium::ServerGattConnectionCallback::WriteValueCallback + api::ble_v2::ServerGattConnectionCallback::WriteValueCallback callback) { written_data = data; callback(absl::OkStatus()); diff --git a/internal/platform/implementation/g3/ble_v2.cc b/internal/platform/implementation/g3/ble_v2.cc index a36047aa..94095edf 100644 --- a/internal/platform/implementation/g3/ble_v2.cc +++ b/internal/platform/implementation/g3/ble_v2.cc @@ -38,7 +38,6 @@ #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" @@ -74,8 +73,12 @@ api::ble_v2::BlePeripheral::UniqueId BleV2Socket::GetRemotePeripheralId() { remote_socket->adapter_->GetBleV2Medium() == nullptr) { return 0LL; } - return dynamic_cast(remote_socket->adapter_->GetBleV2Medium()) - ->GetPeripheral().GetUniqueId(); + BleV2Medium* medium = + dynamic_cast(remote_socket->adapter_->GetBleV2Medium()); + if (medium == nullptr) { + return 0LL; + } + return medium->GetAdapter().GetUniqueId(); } std::unique_ptr BleV2ServerSocket::Accept() { @@ -146,13 +149,14 @@ Exception BleV2ServerSocket::DoClose() { } BleV2Medium::BleV2Medium(api::BluetoothAdapter& adapter) - : adapter_(static_cast(&adapter)), - peripheral_(adapter_->GetUniqueId(), adapter_->mac_address()) { + : adapter_(dynamic_cast(&adapter)) { + CHECK(adapter_); adapter_->SetBleV2Medium(this); is_extended_advertisements_available_ = MediumEnvironment::Instance().IsBleExtendedAdvertisementsAvailable(); - MediumEnvironment::Instance().RegisterBleV2Medium(*this, &peripheral_); + MediumEnvironment::Instance().RegisterBleV2Medium(*this, + adapter_->GetUniqueId()); } BleV2Medium::~BleV2Medium() { @@ -179,7 +183,7 @@ bool BleV2Medium::StartAdvertising( absl::MutexLock lock(&mutex_); MediumEnvironment::Instance().UpdateBleV2MediumForAdvertising( - /*enabled=*/true, *this, GetPeripheral(), advertising_data); + /*enabled=*/true, *this, adapter_->GetUniqueId(), advertising_data); return true; } @@ -189,7 +193,7 @@ bool BleV2Medium::StopAdvertising() { BleAdvertisementData empty_advertisement_data = {}; MediumEnvironment::Instance().UpdateBleV2MediumForAdvertising( - /*enabled=*/false, *this, /*mutable=*/GetPeripheral(), + /*enabled=*/false, *this, adapter_->GetUniqueId(), empty_advertisement_data); return true; } @@ -217,7 +221,7 @@ std::unique_ptr BleV2Medium::StartAdvertising( } absl::MutexLock lock(&mutex_); MediumEnvironment::Instance().UpdateBleV2MediumForAdvertising( - /*enabled=*/true, *this, GetPeripheral(), advertising_data); + /*enabled=*/true, *this, adapter_->GetUniqueId(), advertising_data); return std::make_unique( AdvertisingSession{.stop_advertising = [this] { return StopAdvertising() @@ -353,12 +357,8 @@ BleV2Medium::GattServer::GattServer( BleV2Medium& medium, api::ble_v2::ServerGattConnectionCallback callback) : medium_(medium), 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()); + MediumEnvironment::Instance().RegisterGattServer( + medium_, adapter.GetUniqueId(), lender_.GetBorrowable()); } BleV2Medium::GattServer::~GattServer() { @@ -736,7 +736,7 @@ std::unique_ptr BleV2Medium::Connect( CancellationFlag* cancellation_flag) { LOG(INFO) << "G3 Ble Connect [self]: medium=" << this << ", adapter=" << &GetAdapter() - << ", peripheral=" << &GetPeripheral() + << ", peripheral id=" << adapter_->GetUniqueId() << ", service_id=" << service_id; // First, find an instance of remote medium, that exposed this peripheral. BleV2Medium* remote_medium = dynamic_cast( diff --git a/internal/platform/implementation/g3/ble_v2.h b/internal/platform/implementation/g3/ble_v2.h index 54e3c1b8..0757146f 100644 --- a/internal/platform/implementation/g3/ble_v2.h +++ b/internal/platform/implementation/g3/ble_v2.h @@ -190,8 +190,6 @@ class BleV2Medium : public api::ble_v2::BleMedium { BluetoothAdapter& GetAdapter() { return *adapter_; } - api::ble_v2::BlePeripheral& GetPeripheral() { return peripheral_; } - private: class GattClient; // A concrete implementation for GattServer. @@ -251,7 +249,6 @@ class BleV2Medium : public api::ble_v2::BleMedium { absl::Mutex mutex_; BleV2Medium& medium_; api::ble_v2::ServerGattConnectionCallback callback_; - api::ble_v2::BlePeripheral ble_peripheral_; absl::flat_hash_map> characteristics_ ABSL_GUARDED_BY(mutex_); @@ -308,10 +305,6 @@ class BleV2Medium : public api::ble_v2::BleMedium { bool IsStopped(Borrowable server); absl::Mutex mutex_; BluetoothAdapter* adapter_; // Our device adapter; read-only. - api::ble_v2::BlePeripheral peripheral_; - absl::flat_hash_map> - remote_peripherals_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map server_sockets_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_set> diff --git a/internal/platform/medium_environment.cc b/internal/platform/medium_environment.cc index 4af47edb..c8a45a1a 100644 --- a/internal/platform/medium_environment.cc +++ b/internal/platform/medium_environment.cc @@ -244,37 +244,14 @@ api::BluetoothDevice* MediumEnvironment::FindBluetoothDevice( } api::ble_v2::BleMedium* MediumEnvironment::FindBleV2Medium( - absl::string_view address) { - api::ble_v2::BleMedium* device = nullptr; - CountDownLatch latch(1); - LOG(INFO) << "FindBleV2Medium " << address; - RunOnMediumEnvironmentThread([&]() { - for (auto& item : ble_v2_mediums_) { - auto* medium = item.first; - auto* peripheral = item.second.ble_peripheral; - if (peripheral != nullptr && peripheral->GetAddress() == address) { - device = medium; - break; - } - } - latch.CountDown(); - }); - latch.Await(); - if (device == nullptr) { - LOG(INFO) << "FindBleV2Medium, not found: " << address; - } - return device; -} - -api::ble_v2::BleMedium* MediumEnvironment::FindBleV2Medium(uint64_t id) { + api::ble_v2::BlePeripheral::UniqueId id) { api::ble_v2::BleMedium* device = nullptr; CountDownLatch latch(1); LOG(INFO) << "FindBleV2Medium " << id; RunOnMediumEnvironmentThread([&]() { for (auto& item : ble_v2_mediums_) { auto* medium = item.first; - auto* peripheral = item.second.ble_peripheral; - if (peripheral != nullptr && peripheral->GetUniqueId() == id) { + if (item.second.ble_peripheral_id == id) { device = medium; break; } @@ -313,22 +290,22 @@ void MediumEnvironment::OnBlePeripheralStateChanged( void MediumEnvironment::OnBleV2PeripheralStateChanged( bool enabled, BleV2MediumContext& context, const Uuid& service_id, const api::ble_v2::BleAdvertisementData& ble_advertisement_data, - api::ble_v2::BlePeripheral& peripheral) { + api::ble_v2::BlePeripheral::UniqueId peripheral_id) { if (!enabled_) return; - LOG(INFO) << "OnBleServiceStateChanged [peripheral impl=" << &peripheral + LOG(INFO) << "OnBleServiceStateChanged [peripheral id=" << peripheral_id << "]; medium_context=" << &context << "; notify=" << enable_notifications_.load(); if (!enable_notifications_) return; - LOG(INFO) << "[Run] OnBleServiceStateChanged [peripheral impl=" << &peripheral + LOG(INFO) << "[Run] OnBleServiceStateChanged [peripheral id=" << peripheral_id << "]; context=" << &context << "; notify=" << enabled; for (auto& element : context.scan_callback_map) { if (element.first.first == service_id) { if (enabled) { - element.second.advertisement_found_cb(peripheral.GetUniqueId(), + element.second.advertisement_found_cb(peripheral_id, ble_advertisement_data); } else { - element.second.advertisement_lost_cb(peripheral.GetUniqueId()); + element.second.advertisement_lost_cb(peripheral_id); } } } @@ -643,21 +620,22 @@ void MediumEnvironment::CallBleAcceptedConnectionCallback( } void MediumEnvironment::RegisterBleV2Medium( - api::ble_v2::BleMedium& medium, api::ble_v2::BlePeripheral* peripheral) { + api::ble_v2::BleMedium& medium, + api::ble_v2::BlePeripheral::UniqueId peripheral_id) { if (!enabled_) return; - RunOnMediumEnvironmentThread([this, &medium, peripheral]() { + RunOnMediumEnvironmentThread([this, &medium, peripheral_id]() { ble_v2_mediums_.insert( - {&medium, BleV2MediumContext{.ble_peripheral = peripheral}}); + {&medium, BleV2MediumContext{.ble_peripheral_id = peripheral_id}}); LOG(INFO) << "Registered: BLE V2 medium:" << &medium; }); } void MediumEnvironment::UpdateBleV2MediumForAdvertising( bool enabled, api::ble_v2::BleMedium& medium, - api::ble_v2::BlePeripheral& peripheral, + api::ble_v2::BlePeripheral::UniqueId peripheral_id, const api::ble_v2::BleAdvertisementData& advertisement_data) { if (!enabled_) return; - RunOnMediumEnvironmentThread([this, &medium, &peripheral, + RunOnMediumEnvironmentThread([this, &medium, peripheral_id, advertisement_data = advertisement_data, enabled]() { auto it = ble_v2_mediums_.find(&medium); @@ -667,13 +645,13 @@ void MediumEnvironment::UpdateBleV2MediumForAdvertising( return; } auto& context = it->second; - context.ble_peripheral = &peripheral; + context.ble_peripheral_id = peripheral_id; context.advertising = enabled; context.advertisement_data = advertisement_data; LOG(INFO) << "UpdateBleV2MediumForAdvertising: this=" << this << ", medium=" << &medium << ", medium_context=" << &context - << ", peripheral=" << &peripheral << ", enabled=" << enabled; + << ", peripheral id=" << peripheral_id << ", enabled=" << enabled; for (auto& medium_info : ble_v2_mediums_) { const api::ble_v2::BleMedium* remote_medium = medium_info.first; @@ -703,11 +681,11 @@ void MediumEnvironment::UpdateBleV2MediumForAdvertising( << remote_medium << ", remote_medium_context=" << &remote_context << ", remote_context.peripheral=" - << remote_context.ble_peripheral + << remote_context.ble_peripheral_id << ". Ready to call OnBleV2PeripheralStateChanged."; OnBleV2PeripheralStateChanged( enabled, remote_context, remote_scanning_service_uuid, - context.advertisement_data, *context.ble_peripheral); + context.advertisement_data, context.ble_peripheral_id); } } }); @@ -761,7 +739,7 @@ void MediumEnvironment::UpdateBleV2MediumForScanning( << ". Ready to call OnBleV2PeripheralStateChanged."; OnBleV2PeripheralStateChanged(enabled, context, scanning_service_uuid, remote_context.advertisement_data, - *remote_context.ble_peripheral); + remote_context.ble_peripheral_id); } } } else { @@ -1289,10 +1267,11 @@ std::optional MediumEnvironment::GetSimulatedClock() { } void MediumEnvironment::RegisterGattServer( - api::ble_v2::BleMedium& medium, api::ble_v2::BlePeripheral* peripheral, + api::ble_v2::BleMedium& medium, + api::ble_v2::BlePeripheral::UniqueId peripheral_id, Borrowable gatt_server) { if (!enabled_) return; - RunOnMediumEnvironmentThread([this, &medium, peripheral, gatt_server]() { + RunOnMediumEnvironmentThread([this, &medium, peripheral_id, gatt_server]() { auto it = ble_v2_mediums_.find(&medium); if (it == ble_v2_mediums_.end()) { LOG(WARNING) << "Register GattServer failed. There is no medium" @@ -1303,8 +1282,8 @@ void MediumEnvironment::RegisterGattServer( CHECK_EQ(context.gatt_server, nullptr); context.gatt_server = std::make_unique>(gatt_server); - context.ble_peripheral = peripheral; - LOG(INFO) << "Registered: GattServer for " << peripheral->GetAddress() + context.ble_peripheral_id = peripheral_id; + LOG(INFO) << "Registered: GattServer for peripheral id:" << peripheral_id << " on medium:" << &medium; }); } @@ -1322,11 +1301,11 @@ void MediumEnvironment::UnregisterGattServer(api::ble_v2::BleMedium& medium) { return; } auto& context = it->second; - LOG(INFO) << "Unregistered GattServer for " - << context.ble_peripheral->GetAddress() + LOG(INFO) << "Unregistered GattServer for peripheral id:" + << context.ble_peripheral_id << " on medium:" << &medium; context.gatt_server = nullptr; - context.ble_peripheral = nullptr; + context.ble_peripheral_id = 0LL; latch.CountDown(); }); latch.Await(); @@ -1340,10 +1319,8 @@ Borrowable MediumEnvironment::GetGattServer( RunOnMediumEnvironmentThread([&]() { for (const auto& medium_info : ble_v2_mediums_) { const BleV2MediumContext& remote_context = medium_info.second; - const api::ble_v2::BlePeripheral* ble_peripheral = - remote_context.ble_peripheral; - if (remote_context.gatt_server != nullptr && ble_peripheral != nullptr && - (ble_peripheral->GetUniqueId() == peripheral_id)) { + if (remote_context.gatt_server != nullptr && + remote_context.ble_peripheral_id == peripheral_id) { if (remote_context.gatt_server == nullptr) { break; } diff --git a/internal/platform/medium_environment.h b/internal/platform/medium_environment.h index 2a576101..4d1c8a0b 100644 --- a/internal/platform/medium_environment.h +++ b/internal/platform/medium_environment.h @@ -26,7 +26,6 @@ #include "absl/container/flat_hash_map.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" -#include "absl/types/optional.h" #include "internal/base/observer_list.h" #include "internal/platform/borrowable.h" #include "internal/platform/implementation/awdl.h" @@ -163,9 +162,6 @@ class MediumEnvironment { // Returns a Bluetooth Device object matching given mac address to nullptr. api::BluetoothDevice* FindBluetoothDevice(const std::string& mac_address); - api::ble_v2::BlePeripheral* FindBleV2Peripheral( - absl::string_view mac_address); - const EnvironmentConfig& GetEnvironmentConfig(); #ifndef NO_WEBRTC // Registers |message_callback| to receive messages sent to device with id @@ -242,13 +238,13 @@ class MediumEnvironment { // The registered `medium` must refer to a valid instance that outlives this // object. void RegisterBleV2Medium(api::ble_v2::BleMedium& medium, - api::ble_v2::BlePeripheral* peripheral); + api::ble_v2::BlePeripheral::UniqueId peripheral_id); // Updates advertising info to indicate the current medium is exposing // advertising event. void UpdateBleV2MediumForAdvertising( bool enabled, api::ble_v2::BleMedium& medium, - api::ble_v2::BlePeripheral& peripheral, + api::ble_v2::BlePeripheral::UniqueId peripheral_id, const api::ble_v2::BleAdvertisementData& advertisement_data); // Updates discovery callback info to allow for dispatch of discovery events. @@ -379,11 +375,11 @@ class MediumEnvironment { std::optional GetSimulatedClock(); - api::ble_v2::BleMedium* FindBleV2Medium(absl::string_view address); - api::ble_v2::BleMedium* FindBleV2Medium(uint64_t id); + api::ble_v2::BleMedium* FindBleV2Medium( + api::ble_v2::BlePeripheral::UniqueId id); void RegisterGattServer(api::ble_v2::BleMedium& medium, - api::ble_v2::BlePeripheral* peripheral, + api::ble_v2::BlePeripheral::UniqueId peripheral_id, Borrowable gatt_server); void UnregisterGattServer(api::ble_v2::BleMedium& medium); @@ -447,7 +443,7 @@ class MediumEnvironment { absl::flat_hash_map, BleScanCallback> scan_callback_map; // using the same ble peripheral for different advertisement. - api::ble_v2::BlePeripheral* ble_peripheral; + api::ble_v2::BlePeripheral::UniqueId ble_peripheral_id; api::ble_v2::BleAdvertisementData advertisement_data; bool advertising = false; bool scanning = false; @@ -518,7 +514,7 @@ class MediumEnvironment { void OnBleV2PeripheralStateChanged( bool enabled, BleV2MediumContext& context, const Uuid& service_id, const api::ble_v2::BleAdvertisementData& ble_advertisement_data, - api::ble_v2::BlePeripheral& peripheral); + api::ble_v2::BlePeripheral::UniqueId peripheral_id); void OnWifiLanServiceStateChanged(WifiLanMediumContext& info, const NsdServiceInfo& service_info,