From a5060a3c182bfb322c0b0f0c5405b40ba56a2728 Mon Sep 17 00:00:00 2001 From: Janusz Sobczak Date: Wed, 31 May 2023 09:32:57 -0700 Subject: [PATCH] Call gatt disconnect callback in tests PiperOrigin-RevId: 536737632 --- internal/platform/ble_v2.cc | 10 +---- internal/platform/ble_v2.h | 2 - internal/platform/ble_v2_test.cc | 26 +++++++++++ internal/platform/implementation/g3/ble_v2.cc | 45 +++++++++++++++++++ internal/platform/implementation/g3/ble_v2.h | 18 +++++--- 5 files changed, 86 insertions(+), 15 deletions(-) diff --git a/internal/platform/ble_v2.cc b/internal/platform/ble_v2.cc index 1348159f..21fbc9ee 100644 --- a/internal/platform/ble_v2.cc +++ b/internal/platform/ble_v2.cc @@ -247,20 +247,14 @@ std::unique_ptr BleV2Medium::StartGattServer( std::unique_ptr BleV2Medium::ConnectToGattServer( BleV2Peripheral peripheral, TxPowerLevel tx_power_level, ClientGattConnectionCallback callback) { - { - MutexLock lock(&mutex_); - client_gatt_connection_callback_ = std::move(callback); - } - std::unique_ptr api_gatt_client; peripheral.GetImpl([&](api::ble_v2::BlePeripheral& device) { api_gatt_client = impl_->ConnectToGattServer( device, tx_power_level, { .disconnected_cb = - [this]() { - MutexLock lock(&mutex_); - client_gatt_connection_callback_.disconnected_cb(); + [callback = std::move(callback)]() mutable { + callback.disconnected_cb(); }, }); }); diff --git a/internal/platform/ble_v2.h b/internal/platform/ble_v2.h index 197be021..41099ace 100644 --- a/internal/platform/ble_v2.h +++ b/internal/platform/ble_v2.h @@ -455,8 +455,6 @@ class BleV2Medium final { BluetoothAdapter& adapter_; ServerGattConnectionCallback server_gatt_connection_callback_ ABSL_GUARDED_BY(mutex_); - ClientGattConnectionCallback client_gatt_connection_callback_ - ABSL_GUARDED_BY(mutex_); absl::flat_hash_set peripherals_ ABSL_GUARDED_BY(mutex_); ScanCallback scan_callback_ ABSL_GUARDED_BY(mutex_); diff --git a/internal/platform/ble_v2_test.cc b/internal/platform/ble_v2_test.cc index 876ff147..783536c6 100644 --- a/internal/platform/ble_v2_test.cc +++ b/internal/platform/ble_v2_test.cc @@ -571,6 +571,32 @@ TEST_F(BleV2MediumTest, GattClientConnectToGattServerWorks) { env_.Stop(); } +TEST_F(BleV2MediumTest, GattClientNotifiedWhenServerDisconnects) { + env_.Start(); + BluetoothAdapter adapter_a; + BluetoothAdapter adapter_b; + BleV2Medium ble_a(adapter_a); + BleV2Medium ble_b(adapter_b); + std::unique_ptr gatt_server = + ble_a.StartGattServer(/*ServerGattConnectionCallback=*/{}); + ASSERT_NE(gatt_server, nullptr); + CountDownLatch disconnected_latch(1); + // Start GattClient + BleV2Peripheral ble_peripheral = + ble_b.GetRemotePeripheral(*gatt_server->GetBlePeripheral().GetAddress()); + std::unique_ptr gatt_client = ble_b.ConnectToGattServer( + BleV2Peripheral(ble_peripheral), kTxPowerLevel, + /*ClientGattConnectionCallback=*/{.disconnected_cb = [&]() { + disconnected_latch.CountDown(); + }}); + ASSERT_NE(gatt_client, nullptr); + + gatt_server->Stop(); + + disconnected_latch.Await(); + env_.Stop(); +} + TEST_F(BleV2MediumTest, GattClientOperatiosOnCharacteristic) { env_.Start(); BluetoothAdapter adapter_a; diff --git a/internal/platform/implementation/g3/ble_v2.cc b/internal/platform/implementation/g3/ble_v2.cc index b85dfa22..bb2da6c6 100644 --- a/internal/platform/implementation/g3/ble_v2.cc +++ b/internal/platform/implementation/g3/ble_v2.cc @@ -432,6 +432,7 @@ BleV2Medium::GattServer::GattServer( } BleV2Medium::GattServer::~GattServer() { + Stop(); lender_.Release(); MediumEnvironment::Instance().UnregisterGattServer(medium_); } @@ -573,6 +574,33 @@ bool BleV2Medium::GattServer::HasCharacteristic( void BleV2Medium::GattServer::Stop() { NEARBY_LOGS(INFO) << "G3 Ble GattServer Stop"; characteristics_.clear(); + for (auto& client : connected_clients_) { + client->OnServerDisconnected(); + } +} + +BleV2Medium::GattClient::GattClient( + api::ble_v2::BlePeripheral& peripheral, + Borrowable gatt_server, + api::ble_v2::ClientGattConnectionCallback callback) + : peripheral_(static_cast(peripheral)), + gatt_server_(gatt_server), + callback_(std::move(callback)) { + Borrowed borrowed = gatt_server_.Borrow(); + if (borrowed) { + BleV2Medium::GattServer* gatt_server = + static_cast(*borrowed); + gatt_server->Connect(this); + } +} + +BleV2Medium::GattClient::~GattClient() { + Borrowed borrowed = gatt_server_.Borrow(); + if (borrowed) { + BleV2Medium::GattServer* gatt_server = + static_cast(*borrowed); + gatt_server->Disconnect(this); + } } bool BleV2Medium::GattClient::DiscoverServiceAndCharacteristics( @@ -718,6 +746,23 @@ void BleV2Medium::GattClient::Disconnect() { absl::MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << "G3 Ble GattClient Disconnect"; is_connection_alive_ = false; + Borrowed borrowed = gatt_server_.Borrow(); + if (borrowed) { + BleV2Medium::GattServer* gatt_server = + static_cast(*borrowed); + gatt_server->Disconnect(this); + } +} + +void BleV2Medium::GattClient::OnServerDisconnected() { + { + absl::MutexLock lock(&mutex_); + NEARBY_LOGS(INFO) << "G3 Ble GattServer disconnected"; + is_connection_alive_ = false; + } + if (callback_.disconnected_cb != nullptr) { + callback_.disconnected_cb(); + } } std::unique_ptr BleV2Medium::OpenServerSocket( diff --git a/internal/platform/implementation/g3/ble_v2.h b/internal/platform/implementation/g3/ble_v2.h index a827f970..ab3b3da0 100644 --- a/internal/platform/implementation/g3/ble_v2.h +++ b/internal/platform/implementation/g3/ble_v2.h @@ -228,6 +228,7 @@ class BleV2Medium : public api::ble_v2::BleMedium { GetRemotePeripheralCallback callback) override; private: + class GattClient; // A concrete implementation for GattServer. class GattServer : public api::ble_v2::GattServer { public: @@ -277,6 +278,13 @@ class BleV2Medium : public api::ble_v2::BleMedium { bool HasCharacteristic( const api::ble_v2::GattCharacteristic& characteristic); + void Connect(GattClient* client) { connected_clients_.push_back(client); } + void Disconnect(GattClient* client) { + connected_clients_.erase(std::remove(connected_clients_.begin(), + connected_clients_.end(), client), + connected_clients_.end()); + } + private: using SubscriberKey = std::pair; @@ -289,7 +297,7 @@ class BleV2Medium : public api::ble_v2::BleMedium { absl::StatusOr> characteristics_; absl::flat_hash_map subscribers_; - + std::vector connected_clients_; Lender lender_{this}; }; @@ -298,10 +306,8 @@ class BleV2Medium : public api::ble_v2::BleMedium { public: GattClient(api::ble_v2::BlePeripheral& peripheral, Borrowable gatt_server, - api::ble_v2::ClientGattConnectionCallback callback) - : peripheral_(static_cast(peripheral)), - gatt_server_(gatt_server), - callback_(std::move(callback)) {} + api::ble_v2::ClientGattConnectionCallback callback); + ~GattClient() override; bool DiscoverServiceAndCharacteristics( const Uuid& service_uuid, const std::vector& characteristic_uuids) override; @@ -324,6 +330,8 @@ class BleV2Medium : public api::ble_v2::BleMedium { void Disconnect() override; + void OnServerDisconnected(); + private: absl::Mutex mutex_;