mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Call gatt disconnect callback in tests
PiperOrigin-RevId: 536737632
This commit is contained in:
committed by
Copybara-Service
parent
996f86d93f
commit
a5060a3c18
@@ -247,20 +247,14 @@ std::unique_ptr<GattServer> BleV2Medium::StartGattServer(
|
||||
std::unique_ptr<GattClient> BleV2Medium::ConnectToGattServer(
|
||||
BleV2Peripheral peripheral, TxPowerLevel tx_power_level,
|
||||
ClientGattConnectionCallback callback) {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
client_gatt_connection_callback_ = std::move(callback);
|
||||
}
|
||||
|
||||
std::unique_ptr<api::ble_v2::GattClient> 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();
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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<api::ble_v2::BlePeripheral*> peripherals_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
ScanCallback scan_callback_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
@@ -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<GattServer> 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<GattClient> 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;
|
||||
|
||||
@@ -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<api::ble_v2::GattServer*> gatt_server,
|
||||
api::ble_v2::ClientGattConnectionCallback callback)
|
||||
: peripheral_(static_cast<BleV2Peripheral&>(peripheral)),
|
||||
gatt_server_(gatt_server),
|
||||
callback_(std::move(callback)) {
|
||||
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
|
||||
if (borrowed) {
|
||||
BleV2Medium::GattServer* gatt_server =
|
||||
static_cast<BleV2Medium::GattServer*>(*borrowed);
|
||||
gatt_server->Connect(this);
|
||||
}
|
||||
}
|
||||
|
||||
BleV2Medium::GattClient::~GattClient() {
|
||||
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
|
||||
if (borrowed) {
|
||||
BleV2Medium::GattServer* gatt_server =
|
||||
static_cast<BleV2Medium::GattServer*>(*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<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
|
||||
if (borrowed) {
|
||||
BleV2Medium::GattServer* gatt_server =
|
||||
static_cast<BleV2Medium::GattServer*>(*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<api::ble_v2::BleServerSocket> BleV2Medium::OpenServerSocket(
|
||||
|
||||
@@ -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<const BleV2Peripheral*, api::ble_v2::GattCharacteristic>;
|
||||
@@ -289,7 +297,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
absl::StatusOr<ByteArray>>
|
||||
characteristics_;
|
||||
absl::flat_hash_map<SubscriberKey, SubscriberCallback> subscribers_;
|
||||
|
||||
std::vector<GattClient*> connected_clients_;
|
||||
Lender<api::ble_v2::GattServer*> lender_{this};
|
||||
};
|
||||
|
||||
@@ -298,10 +306,8 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
public:
|
||||
GattClient(api::ble_v2::BlePeripheral& peripheral,
|
||||
Borrowable<api::ble_v2::GattServer*> gatt_server,
|
||||
api::ble_v2::ClientGattConnectionCallback callback)
|
||||
: peripheral_(static_cast<BleV2Peripheral&>(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<Uuid>& characteristic_uuids) override;
|
||||
@@ -324,6 +330,8 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
|
||||
void Disconnect() override;
|
||||
|
||||
void OnServerDisconnected();
|
||||
|
||||
private:
|
||||
absl::Mutex mutex_;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user