diff --git a/internal/platform/implementation/linux/ble_v2_medium.cc b/internal/platform/implementation/linux/ble_v2_medium.cc index ae120b00..d65d8880 100644 --- a/internal/platform/implementation/linux/ble_v2_medium.cc +++ b/internal/platform/implementation/linux/ble_v2_medium.cc @@ -35,7 +35,6 @@ #include "ble_l2cap_server_socket.h" #include "ble_l2cap_socket.h" #include "ble_gatt_server.h" -#include "internal/base/observer_list.h" #include "internal/platform/implementation/linux/bluetooth_classic_device.h" #include "internal/platform/implementation/linux/bluetooth_devices.h" #include "internal/platform/implementation/linux/bluez.h" @@ -72,10 +71,8 @@ BleV2Medium::~BleV2Medium() { BleV2Medium::BleV2Medium(BluetoothAdapter& adapter) : system_bus_(adapter.GetConnection()), adapter_(adapter), - observers_(std::make_shared< - ObserverList>()), devices_(std::make_unique( - system_bus_, adapter_.GetObjectPath(), *observers_)), + system_bus_, adapter_.GetObjectPath())), gatt_discovery_(std::make_shared(system_bus_)), root_object_manager_(std::make_unique( *system_bus_, diff --git a/internal/platform/implementation/linux/ble_v2_medium.h b/internal/platform/implementation/linux/ble_v2_medium.h index b58f3398..d9bf109f 100644 --- a/internal/platform/implementation/linux/ble_v2_medium.h +++ b/internal/platform/implementation/linux/ble_v2_medium.h @@ -145,9 +145,6 @@ class BleV2Medium final : public api::ble::BleMedium { std::shared_ptr system_bus_; BluetoothAdapter adapter_; - // Why do we have observers her - std::shared_ptr> - observers_; std::shared_ptr devices_; std::shared_ptr gatt_discovery_; diff --git a/internal/platform/implementation/linux/bluetooth_classic_device.cc b/internal/platform/implementation/linux/bluetooth_classic_device.cc index f21c20c6..2573924c 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_device.cc +++ b/internal/platform/implementation/linux/bluetooth_classic_device.cc @@ -148,14 +148,12 @@ bool BluetoothDevice::ConnectToProfile(absl::string_view service_uuid) { } MonitoredBluetoothDevice::MonitoredBluetoothDevice( std::shared_ptr system_bus, - std::shared_ptr device, - ObserverList &observers) + std::shared_ptr device) : BluetoothDevice(device), ProxyInterfaces(*system_bus, sdbus::ServiceName(bluez::SERVICE_DEST), device->getProxy().getObjectPath()), - system_bus_(std::move(system_bus)), - observers_(observers) { + system_bus_(std::move(system_bus)) { registerProxy(); } @@ -170,30 +168,9 @@ void MonitoredBluetoothDevice::onPropertiesChanged( for (auto it = changedProperties.begin(); it != changedProperties.end(); it++) { - if (it->first == bluez::DEVICE_PROP_ADDRESS) { - LOG(INFO) << __func__ << ": " << getProxy().getObjectPath() - << ": Notifying observers about address change"; - std::string address = it->second.get(); - for (const auto &observer : observers_.GetObservers()) { - observer->DeviceAddressChanged(*this, address); - } - - } else if (it->first == bluez::DEVICE_PROP_PAIRED) { - LOG(INFO) << __func__ << ": " << getProxy().getObjectPath() - << "Notifying observers about paired status change."; - for (const auto &observer : observers_.GetObservers()) { - observer->DevicePairedChanged(*this, it->second.get()); - } - } else if (it->first == bluez::DEVICE_PROP_CONNECTED) { - LOG(INFO) - << __func__ << ": " << getProxy().getObjectPath() - << "Notifying observers about connected status change"; - for (const auto &observer : observers_.GetObservers()) { - observer->DeviceConnectedStateChanged(*this, it->second.get()); - } - } else if ( it -> first == "ServicesResolved"){ + if (it->first == "ServicesResolved"){ LOG(INFO) << ": ServicesResolved :" << it->second.get(); - }else if (it->first == bluez::DEVICE_NAME) { + } else if (it->first == bluez::DEVICE_NAME) { 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 0412db95..d0f3e768 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_device.h +++ b/internal/platform/implementation/linux/bluetooth_classic_device.h @@ -29,7 +29,6 @@ #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.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/linux/bluez_device.h" @@ -150,8 +149,7 @@ class MonitoredBluetoothDevice final MonitoredBluetoothDevice &operator=(MonitoredBluetoothDevice &&) = delete; MonitoredBluetoothDevice( std::shared_ptr system_bus, - std::shared_ptr device, - ObserverList &observers); + std::shared_ptr device); ~MonitoredBluetoothDevice() override { unregisterProxy(); } void SetDiscoveryCallback( @@ -178,7 +176,6 @@ class MonitoredBluetoothDevice final return callback; } - ObserverList &observers_; absl::Mutex discovery_cb_mutex_; std::weak_ptr discovery_cb_ ABSL_GUARDED_BY(discovery_cb_mutex_); diff --git a/internal/platform/implementation/linux/bluetooth_classic_medium.cc b/internal/platform/implementation/linux/bluetooth_classic_medium.cc index 208fb28e..39523e32 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_medium.cc +++ b/internal/platform/implementation/linux/bluetooth_classic_medium.cc @@ -19,8 +19,7 @@ #include #include "absl/strings/string_view.h" -#include "internal/base/observer_list.h" -#include "internal/platform/implementation/bluetooth_classic.h" +#include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/linux/bluetooth_adapter.h" #include "internal/platform/implementation/linux/bluez_agent.h" #include "internal/platform/implementation/linux/bluetooth_bluez_profile.h" @@ -43,15 +42,11 @@ constexpr char kBluezAgentPath[] = "/com/google/nearby/bluetooth/agent"; BluetoothClassicMedium::BluetoothClassicMedium(BluetoothAdapter &adapter) : system_bus_(adapter.GetConnection()), adapter_(adapter), - observers_(nullptr), devices_(nullptr), device_watcher_(nullptr), agent_manager_(std::make_unique(*system_bus_)), profile_manager_(nullptr) { - auto shared = - GetSharedBluetoothDevices(system_bus_, adapter_.GetObjectPath()); - observers_ = shared->observers; - devices_ = shared->devices; + devices_ = GetSharedBluetoothDevices(system_bus_, adapter_.GetObjectPath()); profile_manager_ = std::make_unique(*system_bus_, *devices_); @@ -67,9 +62,8 @@ BluetoothClassicMedium::BluetoothClassicMedium(BluetoothAdapter &adapter) bool BluetoothClassicMedium::StartDiscovery( DiscoveryCallback discovery_callback) { device_watcher_ = std::make_unique( - *system_bus_, adapter_.GetObjectPath(), adapter_, devices_, - std::make_unique(std::move(discovery_callback)), - observers_); + *system_bus_, adapter_.GetObjectPath(), adapter_, devices_, + std::make_unique(std::move(discovery_callback))); std::map filter; filter["Transport"] = sdbus::Variant("auto"); @@ -159,7 +153,7 @@ std::unique_ptr BluetoothClassicMedium::ConnectToService( new BluetoothSocket(device, fd.value())); } -std::unique_ptr +std::shared_ptr BluetoothClassicMedium::ListenForService(const std::string &service_name, const std::string &service_uuid) { if (!profile_manager_->ProfileRegistered(service_uuid)) { @@ -171,9 +165,9 @@ BluetoothClassicMedium::ListenForService(const std::string &service_name, } } - return std::unique_ptr( - new BluetoothServerSocket(*profile_manager_, service_uuid)); -} + return std::shared_ptr( + new BluetoothServerSocket(*profile_manager_, service_uuid)); +} api::BluetoothDevice *BluetoothClassicMedium::GetRemoteDevice( MacAddress mac_address) { diff --git a/internal/platform/implementation/linux/bluetooth_classic_medium.h b/internal/platform/implementation/linux/bluetooth_classic_medium.h index 0320d765..f9b2db7b 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_medium.h +++ b/internal/platform/implementation/linux/bluetooth_classic_medium.h @@ -27,7 +27,6 @@ #include #include "internal/platform/implementation/linux/bluez_agent.h" -#include "internal/base/observer_list.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/linux/bluetooth_adapter.h" #include "internal/platform/implementation/linux/bluetooth_bluez_profile.h" @@ -78,7 +77,7 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium { // UUID. // // Returns nullptr error. - std::unique_ptr ListenForService( + std::shared_ptr ListenForService( const std::string &service_name, const std::string &service_uuid) override; @@ -92,18 +91,10 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium { api::BluetoothDevice *GetRemoteDevice(MacAddress mac_address) override; - void AddObserver(Observer *observer) override { - observers_->AddObserver(observer); - }; - void RemoveObserver(Observer *observer) override { - observers_->RemoveObserver(observer); - }; - private: std::shared_ptr system_bus_; BluetoothAdapter adapter_; - std::shared_ptr> observers_; std::shared_ptr devices_; std::unique_ptr device_watcher_; diff --git a/internal/platform/implementation/linux/bluetooth_devices.cc b/internal/platform/implementation/linux/bluetooth_devices.cc index ee0740f3..e341f3e0 100644 --- a/internal/platform/implementation/linux/bluetooth_devices.cc +++ b/internal/platform/implementation/linux/bluetooth_devices.cc @@ -35,10 +35,10 @@ namespace nearby { namespace linux { static constexpr std::chrono::minutes kLostPeripheralsCleanupMinFreq(5); absl::Mutex g_shared_devices_lock; -absl::flat_hash_map> +absl::flat_hash_map> g_shared_devices ABSL_GUARDED_BY(g_shared_devices_lock); -std::shared_ptr GetSharedBluetoothDevices( +std::shared_ptr GetSharedBluetoothDevices( std::shared_ptr system_bus, const sdbus::ObjectPath& adapter_object_path) { const std::string key = adapter_object_path; @@ -49,13 +49,10 @@ std::shared_ptr GetSharedBluetoothDevices( return existing; } } - auto shared = std::make_shared(); - shared->observers = - std::make_shared>(); - shared->devices = std::make_shared( - std::move(system_bus), adapter_object_path, *shared->observers); - g_shared_devices[key] = shared; - return shared; + auto devices = std::make_shared( + std::move(system_bus), adapter_object_path); + g_shared_devices[key] = devices; + return devices; } std::shared_ptr BluetoothDevices::get_device_by_path( @@ -136,8 +133,7 @@ std::shared_ptr BluetoothDevices::add_new_device( std::string(device_object_path), std::make_shared( system_bus_, - std::make_shared(system_bus_, device_object_path), - observers_)); + std::make_shared(system_bus_, device_object_path))); if (!inserted) device_it->second->UnmarkLost(); return device_it->second; } @@ -163,11 +159,6 @@ void DeviceWatcher::onInterfacesAdded( discovery_cb_->device_discovered_cb(*device); } - if (observers_ != nullptr) { - for (const auto &observer : observers_->GetObservers()) { - observer->DeviceAdded(*device); - } - } } void DeviceWatcher::onInterfacesRemoved( @@ -196,10 +187,7 @@ void DeviceWatcher::onInterfacesRemoved( discovery_cb_->device_lost_cb(*device); } - if (observers_ != nullptr) { - for (const auto &observer : observers_->GetObservers()) { - observer->DeviceRemoved(*device); - } + if (discovery_cb_ != nullptr) { devices_->remove_device_by_path(objectPath); } else { devices_->mark_peripheral_lost(objectPath); diff --git a/internal/platform/implementation/linux/bluetooth_devices.h b/internal/platform/implementation/linux/bluetooth_devices.h index 3e471c7c..67339d06 100644 --- a/internal/platform/implementation/linux/bluetooth_devices.h +++ b/internal/platform/implementation/linux/bluetooth_devices.h @@ -28,8 +28,8 @@ #include "absl/container/flat_hash_map.h" #include "absl/synchronization/mutex.h" -#include "internal/base/observer_list.h" #include "internal/platform/bluetooth_utils.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/linux/bluetooth_classic_device.h" @@ -41,10 +41,8 @@ class BluetoothDevices final { public: BluetoothDevices( std::shared_ptr system_bus, - sdbus::ObjectPath adapter_object_path, - ObserverList &observers) + sdbus::ObjectPath adapter_object_path) : system_bus_(std::move(system_bus)), - observers_(observers), adapter_object_path_(std::move(adapter_object_path)) {} std::shared_ptr get_device_by_path(const sdbus::ObjectPath &) @@ -74,7 +72,6 @@ class BluetoothDevices final { private: std::shared_ptr system_bus_; - ObserverList &observers_; sdbus::ObjectPath adapter_object_path_; absl::Mutex devices_by_path_lock_; @@ -84,12 +81,7 @@ class BluetoothDevices final { ABSL_GUARDED_BY(devices_by_path_lock_); }; -struct SharedBluetoothDevices { - std::shared_ptr devices; - std::shared_ptr> observers; -}; - -std::shared_ptr GetSharedBluetoothDevices( +std::shared_ptr GetSharedBluetoothDevices( std::shared_ptr system_bus, const sdbus::ObjectPath& adapter_object_path); @@ -106,16 +98,13 @@ class DeviceWatcher final : sdbus::ProxyInterfaces { BluetoothAdapter &adapter, std::shared_ptr devices, std::unique_ptr - discovery_callback, - std::shared_ptr> - observers) + discovery_callback) : ProxyInterfaces(system_bus, sdbus::ServiceName("org.bluez"), sdbus::ObjectPath("/")), adapter_object_path_(adapter_object_path), adapter_(adapter), devices_(std::move(devices)), - discovery_cb_(std::move(discovery_callback)), - observers_(std::move(observers)) { + discovery_cb_(std::move(discovery_callback)) { notifyExistingDevices(); registerProxy(); } @@ -124,7 +113,7 @@ class DeviceWatcher final : sdbus::ProxyInterfaces { BluetoothAdapter &adapter, std::shared_ptr devices) : DeviceWatcher(system_bus, adapter_object_path, adapter, std::move(devices), - nullptr, nullptr) {} + nullptr) {} ~DeviceWatcher() { unregisterProxy(); } void onInterfacesAdded( @@ -143,8 +132,6 @@ class DeviceWatcher final : sdbus::ProxyInterfaces { BluetoothAdapter &adapter_; std::shared_ptr devices_; std::shared_ptr discovery_cb_; - std::shared_ptr> - observers_; }; } // namespace linux