From d7abb1775109297033767342e02408f10deeb8ce Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Mon, 11 Sep 2023 18:38:48 +0530 Subject: [PATCH] Share one notification session for all calls between Start/StopNotify --- .../linux/bluez_gatt_characteristic_server.cc | 12 ++++++++---- .../linux/bluez_gatt_characteristic_server.h | 8 ++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/internal/platform/implementation/linux/bluez_gatt_characteristic_server.cc b/internal/platform/implementation/linux/bluez_gatt_characteristic_server.cc index cddfbbf0..4f104a82 100644 --- a/internal/platform/implementation/linux/bluez_gatt_characteristic_server.cc +++ b/internal/platform/implementation/linux/bluez_gatt_characteristic_server.cc @@ -33,8 +33,8 @@ void GattCharacteristicServer::Update(const nearby::ByteArray &value) { static_value_ = std::move(bytes); } -absl::Status GattCharacteristicServer::NotifyChanged(bool confirm, - const ByteArray &new_value) { +absl::Status GattCharacteristicServer::NotifyChanged( + bool confirm, const ByteArray &new_value) { std::vector bytes(new_value.size()); const auto *buf = new_value.data(); for (auto i = 0; i < new_value.size(); i++) bytes[i] = buf[i]; @@ -187,8 +187,12 @@ void GattCharacteristicServer::StopNotify() { if ((characteristic_.property | api::ble_v2::GattCharacteristic::Property::kNotify) == api::ble_v2::GattCharacteristic::Property::kNotify) { - server_cb_->characteristic_unsubscription_cb(characteristic_); - notifying_ = false; + if (notify_sessions_.fetch_sub(0) == 1) { + if (server_cb_->characteristic_unsubscription_cb != nullptr) { + server_cb_->characteristic_unsubscription_cb(characteristic_); + } + notifying_ = false; + } } else { throw(sdbus::Error("org.bluez.Error.Failed")); } diff --git a/internal/platform/implementation/linux/bluez_gatt_characteristic_server.h b/internal/platform/implementation/linux/bluez_gatt_characteristic_server.h index 6f7d5c16..3fcd7652 100644 --- a/internal/platform/implementation/linux/bluez_gatt_characteristic_server.h +++ b/internal/platform/implementation/linux/bluez_gatt_characteristic_server.h @@ -45,7 +45,8 @@ class GattCharacteristicServer final public: GattCharacteristicServer(const GattCharacteristicServer &) = delete; GattCharacteristicServer(GattCharacteristicServer &&) = delete; - GattCharacteristicServer &operator=(const GattCharacteristicServer &) = delete; + GattCharacteristicServer &operator=(const GattCharacteristicServer &) = + delete; GattCharacteristicServer &operator=(GattCharacteristicServer &&) = delete; GattCharacteristicServer( @@ -61,7 +62,8 @@ class GattCharacteristicServer final characteristic_(characteristic), service_object_path_(service_object_path), notifying_(false), - confirmed_(false) { + confirmed_(false), + notify_sessions_(0) { registerAdaptor(); NEARBY_LOGS(VERBOSE) << __func__ << "Creating a " @@ -116,6 +118,8 @@ class GattCharacteristicServer final absl::Mutex confirmed_mutex_; bool confirmed_; + + std::atomic_size_t notify_sessions_; }; } // namespace bluez