diff --git a/internal/platform/implementation/linux/bluetooth_classic_device.cc b/internal/platform/implementation/linux/bluetooth_classic_device.cc index 83135672..9491061f 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_device.cc +++ b/internal/platform/implementation/linux/bluetooth_classic_device.cc @@ -159,7 +159,7 @@ void MonitoredBluetoothDevice::onPropertiesChanged( observer->DeviceConnectedStateChanged(*this, it->second); } } else if (it->first == bluez::DEVICE_NAME) { - auto callback = discovery_cb_.lock(); + auto callback = GetDiscoveryCallback(); if (callback != nullptr && callback->device_name_changed_cb != nullptr) callback->device_name_changed_cb(*this); } diff --git a/internal/platform/implementation/linux/bluetooth_classic_device.h b/internal/platform/implementation/linux/bluetooth_classic_device.h index 0f5c67fc..b60a0a67 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_device.h +++ b/internal/platform/implementation/linux/bluetooth_classic_device.h @@ -25,6 +25,7 @@ #include "absl/functional/any_invocable.h" #include "absl/strings/string_view.h" +#include "absl/synchronization/mutex.h" #include "internal/base/observer_list.h" #include "internal/platform/implementation/ble_v2.h" #include "internal/platform/implementation/bluetooth_classic.h" @@ -58,33 +59,34 @@ class BluetoothDevice std::string GetAddress() const override { return GetMacAddress(); } UniqueId GetUniqueId() const override { return unique_id_; }; - bool ConnectToProfile(absl::string_view service_uuid); - void set_pair_reply_callback( - absl::AnyInvocable cb) { + absl::AnyInvocable cb) + ABSL_LOCKS_EXCLUDED(pair_callback_lock_) { absl::MutexLock l(&pair_callback_lock_); on_pair_reply_cb_ = std::move(cb); } - void reset_pair_reply_callback() { + void reset_pair_reply_callback() ABSL_LOCKS_EXCLUDED(pair_callback_lock_) { absl::MutexLock l(&pair_callback_lock_); - on_pair_reply_cb_ = DefaultCallback(); + on_pair_reply_cb_ = nullptr; } + bool ConnectToProfile(absl::string_view service_uuid); void MarkLost() { lost_ = true; } void UnmarkLost() { lost_ = false; } bool Lost() const { return lost_; } protected: - void onPairReply(const sdbus::Error *error) override { + void onPairReply(const sdbus::Error *error) override + ABSL_LOCKS_EXCLUDED(pair_callback_lock_) { absl::ReaderMutexLock l(&pair_callback_lock_); - on_pair_reply_cb_(error); + if (on_pair_reply_cb_ != nullptr) on_pair_reply_cb_(error); }; private: absl::Mutex pair_callback_lock_; - absl::AnyInvocable on_pair_reply_cb_ = - DefaultCallback(); + absl::AnyInvocable on_pair_reply_cb_ + ABSL_GUARDED_BY(pair_callback_lock_) = nullptr; UniqueId unique_id_; std::atomic_bool lost_; @@ -112,8 +114,9 @@ class MonitoredBluetoothDevice final ~MonitoredBluetoothDevice() override { unregisterProxy(); } void SetDiscoveryCallback( - std::shared_ptr - &callback) { + std::shared_ptr &callback) + ABSL_LOCKS_EXCLUDED(discovery_cb_mutex_) { + absl::MutexLock lock(&discovery_cb_mutex_); discovery_cb_ = callback; }; @@ -124,8 +127,19 @@ class MonitoredBluetoothDevice final const std::vector &invalidatedProperties) override; private: + std::shared_ptr + GetDiscoveryCallback() ABSL_LOCKS_EXCLUDED(discovery_cb_mutex_) { + discovery_cb_mutex_.ReaderLock(); + auto callback = discovery_cb_.lock(); + discovery_cb_mutex_.ReaderUnlock(); + + return callback; + } + ObserverList &observers_; - std::weak_ptr discovery_cb_; + absl::Mutex discovery_cb_mutex_; + std::weak_ptr discovery_cb_ + ABSL_GUARDED_BY(discovery_cb_mutex_); }; } // namespace linux