From 4042687b8d9ded5c71c2193561265fd49fc66070 Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Tue, 5 Sep 2023 20:00:25 +0530 Subject: [PATCH] Add support for sharing DiscoveryCallbacks across BluetoothDevices. --- .../implementation/linux/bluetooth_classic_device.cc | 4 ++++ .../implementation/linux/bluetooth_classic_device.h | 7 +++++++ .../implementation/linux/bluetooth_classic_medium.cc | 9 +++++---- .../implementation/linux/bluetooth_classic_medium.h | 2 +- .../platform/implementation/linux/bluetooth_devices.cc | 2 +- .../platform/implementation/linux/bluetooth_devices.h | 2 +- internal/platform/implementation/linux/bluez.h | 1 + 7 files changed, 20 insertions(+), 7 deletions(-) diff --git a/internal/platform/implementation/linux/bluetooth_classic_device.cc b/internal/platform/implementation/linux/bluetooth_classic_device.cc index 05f0b2c8..b940d62d 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_device.cc +++ b/internal/platform/implementation/linux/bluetooth_classic_device.cc @@ -163,6 +163,10 @@ void MonitoredBluetoothDevice::onPropertiesChanged( for (auto &observer : observers_.GetObservers()) { observer->DeviceConnectedStateChanged(*this, it->second); } + } else if (it->first == bluez::DEVICE_NAME) { + auto callback = discovery_cb_.lock(); + 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 60fc280b..0226056f 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_device.h +++ b/internal/platform/implementation/linux/bluetooth_classic_device.h @@ -96,6 +96,12 @@ class MonitoredBluetoothDevice final ObserverList &observers); ~MonitoredBluetoothDevice() override { unregisterProxy(); } + void SetDiscoveryCallback( + std::shared_ptr + &callback) { + discovery_cb_ = callback; + }; + protected: void onPropertiesChanged( const std::string &interfaceName, @@ -104,6 +110,7 @@ class MonitoredBluetoothDevice final private: ObserverList &observers_; + std::weak_ptr discovery_cb_; }; } // namespace linux diff --git a/internal/platform/implementation/linux/bluetooth_classic_medium.cc b/internal/platform/implementation/linux/bluetooth_classic_medium.cc index 4efb270e..3d3daa95 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_medium.cc +++ b/internal/platform/implementation/linux/bluetooth_classic_medium.cc @@ -64,9 +64,9 @@ void BluetoothClassicMedium::onInterfacesAdded( NEARBY_LOGS(INFO) << __func__ << ": Encountered new device at " << object; auto device = devices_->add_new_device(object); - - if (discovery_cb_.has_value() && + if (discovery_cb_ != nullptr && discovery_cb_->device_discovered_cb != nullptr) { + device->SetDiscoveryCallback(discovery_cb_); discovery_cb_->device_discovered_cb(*device); } @@ -98,7 +98,7 @@ void BluetoothClassicMedium::onInterfacesRemoved( NEARBY_LOGS(INFO) << __func__ << ": Device " << object << " has been removed"; - if (discovery_cb_.has_value() && + if (discovery_cb_ != nullptr && discovery_cb_->device_lost_cb != nullptr) { discovery_cb_->device_lost_cb(*device); } @@ -114,7 +114,8 @@ void BluetoothClassicMedium::onInterfacesRemoved( bool BluetoothClassicMedium::StartDiscovery( DiscoveryCallback discovery_callback) { - discovery_cb_ = std::move(discovery_callback); + discovery_cb_ = + std::make_shared(std::move(discovery_callback)); try { NEARBY_LOGS(INFO) << __func__ << ": Starting discovery on " diff --git a/internal/platform/implementation/linux/bluetooth_classic_medium.h b/internal/platform/implementation/linux/bluetooth_classic_medium.h index 96b511f0..40d369a7 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_medium.h +++ b/internal/platform/implementation/linux/bluetooth_classic_medium.h @@ -122,7 +122,7 @@ class BluetoothClassicMedium final BluetoothAdapter adapter_; std::unique_ptr devices_; - std::optional discovery_cb_; + std::shared_ptr discovery_cb_; std::unique_ptr profile_manager_; ObserverList observers_; diff --git a/internal/platform/implementation/linux/bluetooth_devices.cc b/internal/platform/implementation/linux/bluetooth_devices.cc index e0aba216..c50b7b93 100644 --- a/internal/platform/implementation/linux/bluetooth_devices.cc +++ b/internal/platform/implementation/linux/bluetooth_devices.cc @@ -49,7 +49,7 @@ void BluetoothDevices::remove_device_by_path( devices_by_path_.erase(device_object_path); } -std::shared_ptr BluetoothDevices::add_new_device( +std::shared_ptr BluetoothDevices::add_new_device( sdbus::ObjectPath device_object_path) { absl::MutexLock l(&devices_by_path_lock_); auto pair = devices_by_path_.emplace( diff --git a/internal/platform/implementation/linux/bluetooth_devices.h b/internal/platform/implementation/linux/bluetooth_devices.h index feb190eb..078dc0b2 100644 --- a/internal/platform/implementation/linux/bluetooth_devices.h +++ b/internal/platform/implementation/linux/bluetooth_devices.h @@ -43,7 +43,7 @@ class BluetoothDevices final { std::shared_ptr get_device_by_address(const std::string &); void remove_device_by_path(const sdbus::ObjectPath &) ABSL_LOCKS_EXCLUDED(devices_by_path_lock_); - std::shared_ptr add_new_device(sdbus::ObjectPath) + std::shared_ptr add_new_device(sdbus::ObjectPath) ABSL_LOCKS_EXCLUDED(devices_by_path_lock_); private: diff --git a/internal/platform/implementation/linux/bluez.h b/internal/platform/implementation/linux/bluez.h index 3be9eb7b..049503ae 100644 --- a/internal/platform/implementation/linux/bluez.h +++ b/internal/platform/implementation/linux/bluez.h @@ -43,6 +43,7 @@ static constexpr const char *DEVICE_PROP_ADDRESS = "Address"; static constexpr const char *DEVICE_PROP_ALIAS = "Alias"; static constexpr const char *DEVICE_PROP_PAIRED = "Paired"; static constexpr const char *DEVICE_PROP_CONNECTED = "Connected"; +static constexpr const char *DEVICE_NAME = "Name"; std::string device_object_path(const sdbus::ObjectPath &adapter_object_path, absl::string_view mac_address);