From bc4c1e76c9b4dbbb9655a09ac17eb131cf87a629 Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Sat, 9 Sep 2023 21:41:37 +0530 Subject: [PATCH] Use shared_ptrs for dbus connections for top-level classes. --- .../implementation/linux/ble_v2_medium.cc | 30 +++++------ .../implementation/linux/ble_v2_medium.h | 7 +-- .../implementation/linux/bluetooth_adapter.h | 14 +++--- .../linux/bluetooth_classic_medium.cc | 11 ++-- .../linux/bluetooth_classic_medium.h | 5 +- .../platform/implementation/linux/dbus.cc | 50 ++++++------------- internal/platform/implementation/linux/dbus.h | 3 +- .../implementation/linux/device_info.cc | 12 ++--- .../implementation/linux/device_info.h | 4 +- .../implementation/linux/network_manager.cc | 6 +-- .../implementation/linux/network_manager.h | 28 ++++++++--- .../network_manager_active_connection.cc | 2 +- .../linux/network_manager_active_connection.h | 8 ++- .../platform/implementation/linux/platform.cc | 28 +++++------ .../implementation/linux/wifi_direct.cc | 14 +++--- .../implementation/linux/wifi_direct.h | 5 +- .../linux/wifi_direct_server_socket.cc | 6 +-- .../linux/wifi_direct_server_socket.h | 10 ++-- .../implementation/linux/wifi_hotspot.cc | 2 +- .../implementation/linux/wifi_hotspot.h | 13 ++--- .../linux/wifi_hotspot_server_socket.cc | 6 +-- .../linux/wifi_hotspot_server_socket.h | 10 ++-- .../platform/implementation/linux/wifi_lan.cc | 17 +++---- .../platform/implementation/linux/wifi_lan.h | 6 +-- .../linux/wifi_lan_server_socket.cc | 2 +- .../linux/wifi_lan_server_socket.h | 11 ++-- .../implementation/linux/wifi_medium.cc | 14 +++--- .../implementation/linux/wifi_medium.h | 6 ++- 28 files changed, 154 insertions(+), 176 deletions(-) diff --git a/internal/platform/implementation/linux/ble_v2_medium.cc b/internal/platform/implementation/linux/ble_v2_medium.cc index 8eaa6974..bbc31d8d 100644 --- a/internal/platform/implementation/linux/ble_v2_medium.cc +++ b/internal/platform/implementation/linux/ble_v2_medium.cc @@ -33,17 +33,17 @@ namespace nearby { namespace linux { -BleV2Medium::BleV2Medium(sdbus::IConnection &system_bus, - BluetoothAdapter &adapter) - : system_bus_(system_bus), +BleV2Medium::BleV2Medium(BluetoothAdapter &adapter) + : system_bus_(adapter.GetConnection()), adapter_(adapter), devices_(std::make_unique( - system_bus_, adapter_.GetObjectPath(), observers_)), + *system_bus_, adapter_.GetObjectPath(), observers_)), + root_object_manager_(std::make_unique(*system_bus_)), adv_monitor_manager_( bluez::AdvertisementMonitorManager:: - DiscoverAdvertisementMonitorManager(system_bus, adapter_)), - adv_manager_( - std::make_unique(system_bus, adapter)), + DiscoverAdvertisementMonitorManager(*system_bus_, adapter_)), + adv_manager_(std::make_unique(*system_bus_, + adapter)), cur_adv_(nullptr) { if (adv_monitor_manager_) { NEARBY_LOGS(VERBOSE) @@ -81,7 +81,7 @@ bool BleV2Medium::StartAdvertising( } cur_adv_ = bluez::LEAdvertisement::CreateLEAdvertisement( - system_bus_, advertising_data, advertise_set_parameters); + *system_bus_, advertising_data, advertise_set_parameters); NEARBY_LOGS(INFO) << __func__ << ": Registering advertisement " << cur_adv_->getObjectPath() << " on bluetooth adapter " @@ -136,7 +136,7 @@ BleV2Medium::StartAdvertising( } std::shared_ptr proxy = - sdbus::createProxy(system_bus_, "org.bluez", adapter_.GetObjectPath()); + sdbus::createProxy(*system_bus_, "org.bluez", adapter_.GetObjectPath()); proxy->finishRegistration(); std::shared_ptr shared_cb = @@ -144,7 +144,7 @@ BleV2Medium::StartAdvertising( absl::MutexLock lock(&advs_mutex_); advs_.push_front(bluez::LEAdvertisement::CreateLEAdvertisement( - system_bus_, advertising_data, advertise_set_parameters)); + *system_bus_, advertising_data, advertise_set_parameters)); auto adv_it = advs_.begin(); auto pending_call = @@ -201,7 +201,7 @@ BleV2Medium::StartAdvertising( std::unique_ptr BleV2Medium::StartGattServer( api::ble_v2::ServerGattConnectionCallback callback) { - return std::make_unique(system_bus_, adapter_, devices_, + return std::make_unique(*system_bus_, adapter_, devices_, std::move(callback)); } @@ -264,7 +264,7 @@ bool BleV2Medium::StartScanning(const Uuid &service_uuid, } auto monitor = std::make_unique( - system_bus_, service_uuid, tx_power_level, "or_patterns", devices_, + *system_bus_, service_uuid, tx_power_level, "or_patterns", devices_, std::move(callback)); try { monitor->emitInterfacesAddedSignal( @@ -279,7 +279,7 @@ bool BleV2Medium::StartScanning(const Uuid &service_uuid, } auto device_watcher = std::make_unique( - system_bus_, adapter_.GetObjectPath(), devices_); + *system_bus_, adapter_.GetObjectPath(), devices_); if (!StartLEDiscovery()) { NEARBY_LOGS(ERROR) << __func__ << ": Could not start LE discovery on adapter " @@ -360,7 +360,7 @@ BleV2Medium::StartScanning(const Uuid &service_uuid, } auto monitor = std::make_unique( - system_bus_, service_uuid, tx_power_level, "or_patterns", devices_, + *system_bus_, service_uuid, tx_power_level, "or_patterns", devices_, std::move(callback)); try { monitor->emitInterfacesAddedSignal( @@ -375,7 +375,7 @@ BleV2Medium::StartScanning(const Uuid &service_uuid, } auto device_watcher = std::make_unique( - system_bus_, adapter_.GetObjectPath(), devices_); + *system_bus_, adapter_.GetObjectPath(), devices_); if (!StartLEDiscovery()) { NEARBY_LOGS(ERROR) << __func__ << ": Could not start LE discovery on adapter " diff --git a/internal/platform/implementation/linux/ble_v2_medium.h b/internal/platform/implementation/linux/ble_v2_medium.h index 145f3106..8b59c0da 100644 --- a/internal/platform/implementation/linux/ble_v2_medium.h +++ b/internal/platform/implementation/linux/ble_v2_medium.h @@ -29,6 +29,7 @@ #include "internal/platform/implementation/linux/bluez_advertisement_monitor.h" #include "internal/platform/implementation/linux/bluez_advertisement_monitor_manager.h" #include "internal/platform/implementation/linux/bluez_le_advertisement.h" +#include "internal/platform/implementation/linux/dbus.h" #include "internal/platform/uuid.h" namespace nearby { @@ -40,8 +41,7 @@ class BleV2Medium final : public api::ble_v2::BleMedium { BleV2Medium &operator=(const BleV2Medium &) = delete; BleV2Medium &operator=(BleV2Medium &&) = delete; - BleV2Medium(sdbus::IConnection &system_bus ABSL_ATTRIBUTE_LIFETIME_BOUND, - BluetoothAdapter &adapter); + explicit BleV2Medium(BluetoothAdapter &adapter); ~BleV2Medium() override = default; bool StartAdvertising( @@ -113,11 +113,12 @@ class BleV2Medium final : public api::ble_v2::BleMedium { end; } - sdbus::IConnection &system_bus_; + std::shared_ptr system_bus_; BluetoothAdapter adapter_; ObserverList observers_ = {}; std::shared_ptr devices_; + std::unique_ptr root_object_manager_; std::unique_ptr adv_monitor_manager_; absl::Mutex active_adv_monitors_mutex_; absl::flat_hash_map< diff --git a/internal/platform/implementation/linux/bluetooth_adapter.h b/internal/platform/implementation/linux/bluetooth_adapter.h index 3d2bc900..810cae8f 100644 --- a/internal/platform/implementation/linux/bluetooth_adapter.h +++ b/internal/platform/implementation/linux/bluetooth_adapter.h @@ -37,15 +37,11 @@ class BluezAdapter : public sdbus::ProxyInterfaces { class BluetoothAdapter : public api::BluetoothAdapter { public: - BluetoothAdapter(const BluetoothAdapter &) = default; - BluetoothAdapter(BluetoothAdapter &&) = delete; - BluetoothAdapter &operator=(const BluetoothAdapter &) = default; - BluetoothAdapter &operator=(BluetoothAdapter &&) = delete; - - BluetoothAdapter(sdbus::IConnection &system_bus, + BluetoothAdapter(std::shared_ptr system_bus, const sdbus::ObjectPath &adapter_object_path) - : bluez_adapter_( - std::make_shared(system_bus, adapter_object_path)) {} + : system_bus_(std::move(system_bus)), + bluez_adapter_(std::make_shared(*system_bus_, + adapter_object_path)) {} ~BluetoothAdapter() override = default; @@ -76,8 +72,10 @@ class BluetoothAdapter : public api::BluetoothAdapter { } BluezAdapter &GetBluezAdapterObject() { return *bluez_adapter_; } + std::shared_ptr GetConnection() { return system_bus_; } private: + std::shared_ptr system_bus_; std::shared_ptr bluez_adapter_; }; } // namespace linux diff --git a/internal/platform/implementation/linux/bluetooth_classic_medium.cc b/internal/platform/implementation/linux/bluetooth_classic_medium.cc index 8a795f28..036315d6 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_medium.cc +++ b/internal/platform/implementation/linux/bluetooth_classic_medium.cc @@ -33,21 +33,20 @@ namespace nearby { namespace linux { -BluetoothClassicMedium::BluetoothClassicMedium(sdbus::IConnection &system_bus, - BluetoothAdapter &adapter) - : system_bus_(system_bus), +BluetoothClassicMedium::BluetoothClassicMedium(BluetoothAdapter &adapter) + : system_bus_(adapter.GetConnection()), adapter_(adapter), observers_(std::make_shared>()), devices_(std::make_shared( - system_bus, adapter.GetObjectPath(), *observers_)), + *system_bus_, adapter.GetObjectPath(), *observers_)), device_watcher_(nullptr), profile_manager_( - std::make_unique(system_bus, *devices_)) {} + std::make_unique(*system_bus_, *devices_)) {} bool BluetoothClassicMedium::StartDiscovery( DiscoveryCallback discovery_callback) { device_watcher_ = std::make_unique( - system_bus_, adapter_.GetObjectPath(), devices_, + *system_bus_, adapter_.GetObjectPath(), devices_, std::make_unique(std::move(discovery_callback)), observers_); diff --git a/internal/platform/implementation/linux/bluetooth_classic_medium.h b/internal/platform/implementation/linux/bluetooth_classic_medium.h index 18104108..7192206c 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_medium.h +++ b/internal/platform/implementation/linux/bluetooth_classic_medium.h @@ -38,8 +38,7 @@ namespace linux { // medium. class BluetoothClassicMedium : public api::BluetoothClassicMedium { public: - BluetoothClassicMedium(sdbus::IConnection &system_bus, - BluetoothAdapter &adapter); + explicit BluetoothClassicMedium(BluetoothAdapter &adapter); // https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery() // @@ -101,7 +100,7 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium { }; private: - sdbus::IConnection &system_bus_; + std::shared_ptr system_bus_; BluetoothAdapter adapter_; std::shared_ptr> observers_; diff --git a/internal/platform/implementation/linux/dbus.cc b/internal/platform/implementation/linux/dbus.cc index 1bbaa7b3..6a45b1f6 100644 --- a/internal/platform/implementation/linux/dbus.cc +++ b/internal/platform/implementation/linux/dbus.cc @@ -19,47 +19,29 @@ #include #include "absl/base/call_once.h" +#include "absl/synchronization/mutex.h" #include "internal/platform/implementation/linux/dbus.h" namespace nearby { namespace linux { -static std::unique_ptr global_system_bus_connection = - nullptr; -static std::unique_ptr global_default_bus_connection = - nullptr; -static std::unique_ptr system_root_object_manager = nullptr; -static std::unique_ptr default_root_object_manager = nullptr; -static absl::once_flag bus_connection_init_; -static void disconnectBus() { - system_root_object_manager = nullptr; - default_root_object_manager = nullptr; - global_system_bus_connection = nullptr; - global_default_bus_connection = nullptr; -} +namespace { +static absl::Mutex global_system_bus_mutex; +static std::weak_ptr global_system_bus_connection + ABSL_GUARDED_BY(global_system_bus_mutex); +} // namespace -static void initBusConnections() { - global_system_bus_connection = sdbus::createSystemBusConnection(); - global_system_bus_connection->enterEventLoopAsync(); - global_default_bus_connection = sdbus::createDefaultBusConnection(); - global_default_bus_connection->enterEventLoopAsync(); - system_root_object_manager = - std::make_unique(*global_system_bus_connection); - default_root_object_manager = - std::make_unique(*global_default_bus_connection); +std::shared_ptr getSystemBusConnection() { + absl::MutexLock lock(&global_system_bus_mutex); + auto bus = global_system_bus_connection.lock(); + if (bus == nullptr) { + bus = + std::shared_ptr(sdbus::createSystemBusConnection()); + bus->enterEventLoopAsync(); + global_system_bus_connection = bus; + } - atexit(disconnectBus); -} - -sdbus::IConnection &getSystemBusConnection() { - absl::call_once(bus_connection_init_, initBusConnections); - assert(global_system_bus_connection != nullptr); - return *global_system_bus_connection; -} -sdbus::IConnection &getDefaultBusConnection() { - absl::call_once(bus_connection_init_, initBusConnections); - assert(global_default_bus_connection != nullptr); - return *global_default_bus_connection; + return bus; } } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/dbus.h b/internal/platform/implementation/linux/dbus.h index a16b1fb6..d9908495 100644 --- a/internal/platform/implementation/linux/dbus.h +++ b/internal/platform/implementation/linux/dbus.h @@ -46,8 +46,7 @@ namespace nearby { namespace linux { -extern sdbus::IConnection &getSystemBusConnection(); -extern sdbus::IConnection &getDefaultBusConnection(); +extern std::shared_ptr getSystemBusConnection(); class RootObjectManager final : public sdbus::AdaptorInterfaces { diff --git a/internal/platform/implementation/linux/device_info.cc b/internal/platform/implementation/linux/device_info.cc index 8a97814e..02eb564c 100644 --- a/internal/platform/implementation/linux/device_info.cc +++ b/internal/platform/implementation/linux/device_info.cc @@ -58,13 +58,13 @@ void CurrentUserSession::onUnlock() { } } -DeviceInfo::DeviceInfo(sdbus::IConnection &system_bus) - : system_bus_(system_bus), - current_user_session_(std::make_unique(system_bus_)), - login_manager_(std::make_unique(system_bus_)) {} +DeviceInfo::DeviceInfo(std::shared_ptr system_bus) + : system_bus_(std::move(system_bus)), + current_user_session_(std::make_unique(*system_bus_)), + login_manager_(std::make_unique(*system_bus_)) {} std::optional DeviceInfo::GetOsDeviceName() const { - avahi::Server avahi(system_bus_); + avahi::Server avahi(*system_bus_); try { std::string hostname = avahi.GetHostNameFqdn(); std::wstring_convert, char16_t> convert; @@ -76,7 +76,7 @@ std::optional DeviceInfo::GetOsDeviceName() const { } api::DeviceInfo::DeviceType DeviceInfo::GetDeviceType() const { - Hostnamed hostnamed(system_bus_); + Hostnamed hostnamed(*system_bus_); try { std::string chasis = hostnamed.Chassis(); api::DeviceInfo::DeviceType device = api::DeviceInfo::DeviceType::kUnknown; diff --git a/internal/platform/implementation/linux/device_info.h b/internal/platform/implementation/linux/device_info.h index 67b54b30..f864117d 100644 --- a/internal/platform/implementation/linux/device_info.h +++ b/internal/platform/implementation/linux/device_info.h @@ -119,7 +119,7 @@ class LoginManager final class DeviceInfo final : public api::DeviceInfo { public: - explicit DeviceInfo(sdbus::IConnection &system_bus); + explicit DeviceInfo(std::shared_ptr system_bus); std::optional GetOsDeviceName() const override; api::DeviceInfo::DeviceType GetDeviceType() const override; @@ -160,7 +160,7 @@ class DeviceInfo final : public api::DeviceInfo { bool AllowSleep() override; private: - sdbus::IConnection &system_bus_; + std::shared_ptr system_bus_; std::unique_ptr current_user_session_; std::unique_ptr login_manager_; std::optional inhibit_fd_; diff --git a/internal/platform/implementation/linux/network_manager.cc b/internal/platform/implementation/linux/network_manager.cc index 4c10a96f..6089980e 100644 --- a/internal/platform/implementation/linux/network_manager.cc +++ b/internal/platform/implementation/linux/network_manager.cc @@ -47,7 +47,7 @@ NetworkManagerObjectManager::GetActiveConnectionForAccessPoint( for (auto &path : devices) { if (path == device_path) { return std::make_unique( - getProxy().getConnection(), object_path); + system_bus_, object_path); } } } @@ -80,8 +80,8 @@ NetworkManagerObjectManager::GetIp4Config( sdbus::ObjectPath specific_object = props["SpecificObject"]; if (specific_object == active_connection) { sdbus::ObjectPath ip4config = props["Ip4Config"]; - return std::make_unique( - getProxy().getConnection(), ip4config); + return std::make_unique(system_bus_, + ip4config); } } } diff --git a/internal/platform/implementation/linux/network_manager.h b/internal/platform/implementation/linux/network_manager.h index 1c7c51af..7f6d889d 100644 --- a/internal/platform/implementation/linux/network_manager.h +++ b/internal/platform/implementation/linux/network_manager.h @@ -34,9 +34,10 @@ class NetworkManager final NetworkManager(NetworkManager &&) = delete; NetworkManager &operator=(const NetworkManager &) = delete; NetworkManager &operator=(NetworkManager &&) = delete; - explicit NetworkManager(sdbus::IConnection &system_bus) - : ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager", + explicit NetworkManager(std::shared_ptr system_bus) + : ProxyInterfaces(*system_bus, "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager"), + system_bus_(std::move(system_bus)), state_(kNMStateUnknown) { registerProxy(); try { @@ -60,6 +61,7 @@ class NetworkManager final }; NMState getState() const { return state_; } + std::shared_ptr GetConnection() { return system_bus_; } protected: void onCheckPermissions() override {} @@ -90,6 +92,7 @@ class NetworkManager final #undef NM_STATE_CASE_SET }; + std::shared_ptr system_bus_; std::atomic state_; }; @@ -101,13 +104,17 @@ class NetworkManagerIP4Config NetworkManagerIP4Config(NetworkManagerIP4Config &&) = delete; NetworkManagerIP4Config &operator=(const NetworkManagerIP4Config &) = delete; NetworkManagerIP4Config &operator=(NetworkManagerIP4Config &&) = delete; - NetworkManagerIP4Config(sdbus::IConnection &system_bus, + NetworkManagerIP4Config(std::shared_ptr system_bus, const sdbus::ObjectPath &config_object_path) - : ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager", - config_object_path) { + : ProxyInterfaces(*system_bus, "org.freedesktop.NetworkManager", + config_object_path), + system_bus_(std::move(system_bus)) { registerProxy(); } ~NetworkManagerIP4Config() { unregisterProxy(); } + + private: + std::shared_ptr system_bus_; }; class NetworkManagerObjectManager final @@ -119,9 +126,11 @@ class NetworkManagerObjectManager final delete; NetworkManagerObjectManager &operator=(NetworkManagerObjectManager &&) = delete; - explicit NetworkManagerObjectManager(sdbus::IConnection &system_bus) - : ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager", - "/org/freedesktop") { + explicit NetworkManagerObjectManager( + std::shared_ptr system_bus) + : ProxyInterfaces(*system_bus, "org.freedesktop.NetworkManager", + "/org/freedesktop"), + system_bus_(std::move(system_bus)) { registerProxy(); } ~NetworkManagerObjectManager() { unregisterProxy(); } @@ -140,6 +149,9 @@ class NetworkManagerObjectManager final void onInterfacesRemoved( const sdbus::ObjectPath &objectPath, const std::vector &interfaces) override {} + + private: + std::shared_ptr system_bus_; }; } // namespace linux diff --git a/internal/platform/implementation/linux/network_manager_active_connection.cc b/internal/platform/implementation/linux/network_manager_active_connection.cc index b97c2f2d..8bb6a44b 100644 --- a/internal/platform/implementation/linux/network_manager_active_connection.cc +++ b/internal/platform/implementation/linux/network_manager_active_connection.cc @@ -79,7 +79,7 @@ std::vector NetworkManagerActiveConnection::GetIP4Addresses() { return {}; } - NetworkManagerIP4Config ip4config(getProxy().getConnection(), ip4config_path); + NetworkManagerIP4Config ip4config(system_bus_, ip4config_path); std::vector> address_data; try { address_data = ip4config.AddressData(); diff --git a/internal/platform/implementation/linux/network_manager_active_connection.h b/internal/platform/implementation/linux/network_manager_active_connection.h index 1d4aeb91..5ff19379 100644 --- a/internal/platform/implementation/linux/network_manager_active_connection.h +++ b/internal/platform/implementation/linux/network_manager_active_connection.h @@ -63,9 +63,11 @@ class NetworkManagerActiveConnection NetworkManagerActiveConnection &operator=(NetworkManagerActiveConnection &&) = delete; explicit NetworkManagerActiveConnection( - sdbus::IConnection &system_bus, sdbus::ObjectPath active_connection_path) - : ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager", + std::shared_ptr system_bus, + sdbus::ObjectPath active_connection_path) + : ProxyInterfaces(*system_bus, "org.freedesktop.NetworkManager", std::move(active_connection_path)), + system_bus_(std::move(system_bus)), state_(kStateUnknown), reason_(kStateReasonUnknown) { registerProxy(); @@ -99,6 +101,8 @@ class NetworkManagerActiveConnection std::vector GetIP4Addresses(); private: + std::shared_ptr system_bus_; + absl::Mutex state_mutex_; ActiveConnectionState state_ ABSL_GUARDED_BY(state_mutex_); ActiveConnectionStateReason reason_ ABSL_GUARDED_BY(state_mutex_); diff --git a/internal/platform/implementation/linux/platform.cc b/internal/platform/implementation/linux/platform.cc index 5895a0a3..632d3732 100644 --- a/internal/platform/implementation/linux/platform.cc +++ b/internal/platform/implementation/linux/platform.cc @@ -29,7 +29,6 @@ #include "internal/platform/implementation/input_file.h" #include "internal/platform/implementation/linux/atomic_boolean.h" #include "internal/platform/implementation/linux/atomic_uint32.h" -#include "internal/platform/implementation/linux/ble_medium.h" #include "internal/platform/implementation/linux/ble_v2_medium.h" #include "internal/platform/implementation/linux/bluetooth_adapter.h" #include "internal/platform/implementation/linux/bluetooth_classic_medium.h" @@ -166,15 +165,14 @@ ImplementationPlatform::CreateScheduledExecutor() { std::unique_ptr ImplementationPlatform::CreateBluetoothAdapter() { - auto manager = - linux::bluez::BluezObjectManager(linux::getSystemBusConnection()); + auto system_bus = linux::getSystemBusConnection(); + auto manager = linux::bluez::BluezObjectManager(*system_bus); try { auto interfaces = manager.GetManagedObjects(); for (auto &[object, properties] : interfaces) { if (properties.count(org::bluez::Adapter1_proxy::INTERFACE_NAME) == 1) { NEARBY_LOGS(INFO) << __func__ << ": found bluetooth adapter " << object; - return std::make_unique( - linux::getSystemBusConnection(), object); + return std::make_unique(system_bus, object); } } } catch (const sdbus::Error &e) { @@ -191,19 +189,17 @@ std::unique_ptr ImplementationPlatform::CreateBluetoothClassicMedium( BluetoothAdapter &adapter) { return std::make_unique( - linux::getSystemBusConnection(), dynamic_cast(adapter)); } std::unique_ptr ImplementationPlatform::CreateBleMedium( BluetoothAdapter &adapter) { - return nullptr; + return nullptr; } std::unique_ptr ImplementationPlatform::CreateBleV2Medium(api::BluetoothAdapter &adapter) { return std::make_unique( - linux::getSystemBusConnection(), dynamic_cast(adapter)); } @@ -219,8 +215,7 @@ static std::unique_ptr createWifiMedium( return nullptr; } - auto manager = - linux::NetworkManagerObjectManager(linux::getSystemBusConnection()); + auto manager = linux::NetworkManagerObjectManager(nm->GetConnection()); std::map>> @@ -239,8 +234,8 @@ static std::unique_ptr createWifiMedium( Wireless_proxy::INTERFACE_NAME) == 1) { NEARBY_LOGS(INFO) << __func__ << ": Found a wireless device at :" << device_path; - return std::make_unique( - nm, linux::getSystemBusConnection(), device_path); + return std::make_unique(nm, + device_path); } } } @@ -259,8 +254,9 @@ std::unique_ptr ImplementationPlatform::CreateWifiMedium() { std::unique_ptr ImplementationPlatform::CreateWifiLanMedium() { - return std::make_unique( - linux::getSystemBusConnection()); + auto nm = + std::make_shared(linux::getSystemBusConnection()); + return std::make_unique(nm); } std::unique_ptr @@ -275,7 +271,7 @@ ImplementationPlatform::CreateWifiHotspotMedium() { } return std::make_unique( - linux::getSystemBusConnection(), nm, std::move(wifiMedium)); + nm, std::move(wifiMedium)); } std::unique_ptr @@ -290,7 +286,7 @@ ImplementationPlatform::CreateWifiDirectMedium() { } return std::make_unique( - linux::getSystemBusConnection(), nm, std::move(wifiMedium)); + nm, std::move(wifiMedium)); } std::unique_ptr ImplementationPlatform::CreateTimer() { diff --git a/internal/platform/implementation/linux/wifi_direct.cc b/internal/platform/implementation/linux/wifi_direct.cc index 8aa605e2..b0e43ea4 100644 --- a/internal/platform/implementation/linux/wifi_direct.cc +++ b/internal/platform/implementation/linux/wifi_direct.cc @@ -104,7 +104,7 @@ NetworkManagerWifiDirectMedium::ListenForService(int port) { } return std::make_unique( - sock, system_bus_, active_connection->getObjectPath(), network_manager_); + sock, std::move(active_connection), network_manager_); } bool NetworkManagerWifiDirectMedium::ConnectWifiDirect( @@ -159,7 +159,8 @@ bool NetworkManagerWifiDirectMedium::StartWifiDirect( // medium is currently just a regular wifi hotspot. // auto wireless_device = std::make_unique( // network_manager_, system_bus_, wireless_device_->getObjectPath()); - // auto hotspot = NetworkManagerWifiHotspotMedium(system_bus_, network_manager_, + // auto hotspot = NetworkManagerWifiHotspotMedium(system_bus_, + // network_manager_, // std::move(wireless_device)); // HotspotCredentials hotspot_creds; @@ -168,17 +169,18 @@ bool NetworkManagerWifiDirectMedium::StartWifiDirect( // wifi_direct_credentials->SetSSID(hotspot_creds.GetSSID()); // wifi_direct_credentials->SetPassword(hotspot_creds.GetPassword()); // return true; - return false; + return false; } bool NetworkManagerWifiDirectMedium::StopWifiDirect() { // auto wireless_device = std::make_unique( // network_manager_, system_bus_, wireless_device_->getObjectPath()); - // auto hotspot = NetworkManagerWifiHotspotMedium(system_bus_, network_manager_, + // auto hotspot = NetworkManagerWifiHotspotMedium(system_bus_, + // network_manager_, // std::move(wireless_device)); - // return hotspot.DisconnectWifiHotspot(); - return false; + // return hotspot.DisconnectWifiHotspot(); + return false; } } // namespace linux diff --git a/internal/platform/implementation/linux/wifi_direct.h b/internal/platform/implementation/linux/wifi_direct.h index 3d376080..05711aaf 100644 --- a/internal/platform/implementation/linux/wifi_direct.h +++ b/internal/platform/implementation/linux/wifi_direct.h @@ -28,10 +28,9 @@ namespace linux { class NetworkManagerWifiDirectMedium : public api::WifiDirectMedium { public: NetworkManagerWifiDirectMedium( - sdbus::IConnection &system_bus, std::shared_ptr network_manager, std::unique_ptr wireless_device) - : system_bus_(system_bus), + : system_bus_(network_manager->GetConnection()), network_manager_(std::move(network_manager)), wireless_device_(std::move(wireless_device)) {} @@ -56,7 +55,7 @@ class NetworkManagerWifiDirectMedium : public api::WifiDirectMedium { private: bool ConnectedToWifi(); - sdbus::IConnection &system_bus_; + std::shared_ptr system_bus_; std::shared_ptr network_manager_; std::unique_ptr wireless_device_; }; diff --git a/internal/platform/implementation/linux/wifi_direct_server_socket.cc b/internal/platform/implementation/linux/wifi_direct_server_socket.cc index b2c9495f..099e6d3a 100644 --- a/internal/platform/implementation/linux/wifi_direct_server_socket.cc +++ b/internal/platform/implementation/linux/wifi_direct_server_socket.cc @@ -22,14 +22,12 @@ namespace nearby { namespace linux { std::string NetworkManagerWifiDirectServerSocket::GetIPAddress() const { - NetworkManagerActiveConnection active_conn(system_bus_, - active_connection_path_); - auto ip4addresses = active_conn.GetIP4Addresses(); + auto ip4addresses = active_conn_->GetIP4Addresses(); if (ip4addresses.empty()) { NEARBY_LOGS(ERROR) << __func__ << ": Could not find any IPv4 addresses for active connection " - << active_connection_path_; + << active_conn_->getObjectPath(); return std::string(); } return ip4addresses[0]; diff --git a/internal/platform/implementation/linux/wifi_direct_server_socket.h b/internal/platform/implementation/linux/wifi_direct_server_socket.h index 1f5fe879..34a88e9a 100644 --- a/internal/platform/implementation/linux/wifi_direct_server_socket.h +++ b/internal/platform/implementation/linux/wifi_direct_server_socket.h @@ -16,6 +16,7 @@ #define PLATFORM_IMPL_LINUX_WIFI_DIRECT_SERVER_SOCKET_H_ #include +#include "internal/platform/implementation/linux/network_manager_active_connection.h" #include "internal/platform/implementation/linux/wifi_medium.h" #include "internal/platform/implementation/wifi_direct.h" namespace nearby { @@ -24,12 +25,10 @@ class NetworkManagerWifiDirectServerSocket : public api::WifiDirectServerSocket { public: NetworkManagerWifiDirectServerSocket( - int socket, sdbus::IConnection &system_bus, - sdbus::ObjectPath active_connection_path, + int socket, std::unique_ptr active_conn, std::shared_ptr network_manager) : fd_(socket), - system_bus_(system_bus), - active_connection_path_(std::move(active_connection_path)), + active_conn_(std::move(active_conn)), network_manager_(std::move(network_manager)) {} std::string GetIPAddress() const override; @@ -39,8 +38,7 @@ class NetworkManagerWifiDirectServerSocket private: sdbus::UnixFd fd_; - sdbus::IConnection &system_bus_; - sdbus::ObjectPath active_connection_path_; + std::unique_ptr active_conn_; std::shared_ptr network_manager_; }; } // namespace linux diff --git a/internal/platform/implementation/linux/wifi_hotspot.cc b/internal/platform/implementation/linux/wifi_hotspot.cc index d1b1715f..151d4748 100644 --- a/internal/platform/implementation/linux/wifi_hotspot.cc +++ b/internal/platform/implementation/linux/wifi_hotspot.cc @@ -122,7 +122,7 @@ NetworkManagerWifiHotspotMedium::ListenForService(int port) { } return std::make_unique( - sock, system_bus_, active_connection->getObjectPath(), network_manager_); + sock, std::move(active_connection), network_manager_); } bool NetworkManagerWifiHotspotMedium::StartWifiHotspot( diff --git a/internal/platform/implementation/linux/wifi_hotspot.h b/internal/platform/implementation/linux/wifi_hotspot.h index f00c9b7a..d1b143db 100644 --- a/internal/platform/implementation/linux/wifi_hotspot.h +++ b/internal/platform/implementation/linux/wifi_hotspot.h @@ -26,22 +26,19 @@ namespace linux { class NetworkManagerWifiHotspotMedium : public api::WifiHotspotMedium { public: NetworkManagerWifiHotspotMedium( - sdbus::IConnection &system_bus, std::shared_ptr network_manager, sdbus::ObjectPath wireless_device_object_path) - : system_bus_(system_bus), + : system_bus_(network_manager->GetConnection()), wireless_device_(std::make_unique( - network_manager, system_bus, - std::move(wireless_device_object_path))), + network_manager, std::move(wireless_device_object_path))), network_manager_(std::move(network_manager)) {} NetworkManagerWifiHotspotMedium( - sdbus::IConnection &system_bus, std::shared_ptr network_manager, std::unique_ptr wireless_device) - : system_bus_(system_bus), + : system_bus_(network_manager->GetConnection()), wireless_device_(std::move(wireless_device)), network_manager_(std::move(network_manager)) {} - + bool IsInterfaceValid() const override { return true; } std::unique_ptr ConnectToService( absl::string_view ip_address, int port, @@ -64,7 +61,7 @@ class NetworkManagerWifiHotspotMedium : public api::WifiHotspotMedium { bool WifiHotspotActive(); bool ConnectedToWifi(); - sdbus::IConnection &system_bus_; + std::shared_ptr system_bus_; std::unique_ptr wireless_device_; std::shared_ptr network_manager_; }; diff --git a/internal/platform/implementation/linux/wifi_hotspot_server_socket.cc b/internal/platform/implementation/linux/wifi_hotspot_server_socket.cc index e4e880bb..d74f8f91 100644 --- a/internal/platform/implementation/linux/wifi_hotspot_server_socket.cc +++ b/internal/platform/implementation/linux/wifi_hotspot_server_socket.cc @@ -22,14 +22,12 @@ namespace nearby { namespace linux { std::string NetworkManagerWifiHotspotServerSocket::GetIPAddress() const { - NetworkManagerActiveConnection active_conn(system_bus_, - active_connection_path_); - auto ip4addresses = active_conn.GetIP4Addresses(); + auto ip4addresses = active_conn_->GetIP4Addresses(); if (ip4addresses.empty()) { NEARBY_LOGS(ERROR) << __func__ << ": Could not find any IPv4 addresses for active connection " - << active_connection_path_; + << active_conn_->getObjectPath(); return {}; } return ip4addresses[0]; diff --git a/internal/platform/implementation/linux/wifi_hotspot_server_socket.h b/internal/platform/implementation/linux/wifi_hotspot_server_socket.h index db4ff7f9..587ef20a 100644 --- a/internal/platform/implementation/linux/wifi_hotspot_server_socket.h +++ b/internal/platform/implementation/linux/wifi_hotspot_server_socket.h @@ -17,6 +17,7 @@ #include +#include "internal/platform/implementation/linux/network_manager_active_connection.h" #include "internal/platform/implementation/linux/wifi_medium.h" #include "internal/platform/implementation/wifi_hotspot.h" @@ -26,12 +27,10 @@ class NetworkManagerWifiHotspotServerSocket : public api::WifiHotspotServerSocket { public: NetworkManagerWifiHotspotServerSocket( - int socket, sdbus::IConnection &system_bus, - sdbus::ObjectPath active_connection_path, + int socket, std::unique_ptr active_conn, std::shared_ptr network_manager) : fd_(socket), - system_bus_(system_bus), - active_connection_path_(std::move(active_connection_path)), + active_conn_(std::move(active_conn)), network_manager_(std::move(network_manager)) {} std::string GetIPAddress() const override; @@ -41,8 +40,7 @@ class NetworkManagerWifiHotspotServerSocket private: sdbus::UnixFd fd_; - sdbus::IConnection &system_bus_; - sdbus::ObjectPath active_connection_path_; + std::unique_ptr active_conn_; std::shared_ptr network_manager_; }; } // namespace linux diff --git a/internal/platform/implementation/linux/wifi_lan.cc b/internal/platform/implementation/linux/wifi_lan.cc index 53413a3c..f1af9510 100644 --- a/internal/platform/implementation/linux/wifi_lan.cc +++ b/internal/platform/implementation/linux/wifi_lan.cc @@ -31,16 +31,16 @@ #include "internal/platform/implementation/linux/wifi_lan.h" #include "internal/platform/implementation/linux/wifi_lan_server_socket.h" #include "internal/platform/implementation/linux/wifi_lan_socket.h" -#include "internal/platform/implementation/linux/wifi_medium.h" #include "internal/platform/implementation/wifi_lan.h" #include "internal/platform/logging.h" namespace nearby { namespace linux { -WifiLanMedium::WifiLanMedium(sdbus::IConnection &system_bus) - : system_bus_(system_bus), - network_manager_(std::make_shared(system_bus)), - avahi_(std::make_shared(system_bus)) {} +WifiLanMedium::WifiLanMedium( + std::shared_ptr network_manager) + : system_bus_(network_manager->GetConnection()), + network_manager_(std::move(network_manager)), + avahi_(std::make_shared(*system_bus_)) {} bool WifiLanMedium::IsNetworkConnected() const { auto state = network_manager_->getState(); @@ -99,7 +99,7 @@ bool WifiLanMedium::StartAdvertising(const NsdServiceInfo &nsd_service_info) { } auto entry_group = - std::make_unique(system_bus_, entry_group_path); + std::make_unique(*system_bus_, entry_group_path); try { entry_group->AddService( @@ -166,7 +166,7 @@ bool WifiLanMedium::StartDiscovery( service_browsers_.emplace( service_type, std::make_unique( - system_bus_, browser_object_path, std::move(callback), avahi_)); + *system_bus_, browser_object_path, std::move(callback), avahi_)); } catch (const sdbus::Error &e) { DBUS_LOG_METHOD_CALL_ERROR(avahi_, "ServiceBrowserPrepare", e); return false; @@ -261,8 +261,7 @@ std::unique_ptr WifiLanMedium::ListenForService( NEARBY_LOGS(VERBOSE) << __func__ << "Listening for services on port " << port; - return std::make_unique(sock, network_manager_, - system_bus_); + return std::make_unique(sock, network_manager_); } absl::optional> GetDynamicPortRange() { diff --git a/internal/platform/implementation/linux/wifi_lan.h b/internal/platform/implementation/linux/wifi_lan.h index 60c56fae..11abe585 100644 --- a/internal/platform/implementation/linux/wifi_lan.h +++ b/internal/platform/implementation/linux/wifi_lan.h @@ -14,6 +14,7 @@ #ifndef PLATFORM_IMPL_LINUX_WIFI_LAN_H_ #define PLATFORM_IMPL_LINUX_WIFI_LAN_H_ +#include #include #include "absl/container/flat_hash_map.h" @@ -27,7 +28,7 @@ namespace nearby { namespace linux { class WifiLanMedium : public api::WifiLanMedium { public: - explicit WifiLanMedium(sdbus::IConnection &system_bus); + explicit WifiLanMedium(std::shared_ptr network_manager); bool IsNetworkConnected() const override; @@ -59,8 +60,7 @@ class WifiLanMedium : public api::WifiLanMedium { } private: - sdbus::IConnection &system_bus_; - + std::shared_ptr system_bus_; std::shared_ptr network_manager_; std::shared_ptr avahi_; diff --git a/internal/platform/implementation/linux/wifi_lan_server_socket.cc b/internal/platform/implementation/linux/wifi_lan_server_socket.cc index 52f54334..d4bbd1b9 100644 --- a/internal/platform/implementation/linux/wifi_lan_server_socket.cc +++ b/internal/platform/implementation/linux/wifi_lan_server_socket.cc @@ -42,7 +42,7 @@ std::string WifiLanServerSocket::GetIPAddress() const { } for (auto &path : connection_paths) { - auto active_connection = + auto active_connection = std::make_unique(system_bus_, path); std::string conn_type; try { diff --git a/internal/platform/implementation/linux/wifi_lan_server_socket.h b/internal/platform/implementation/linux/wifi_lan_server_socket.h index 3b5a75fe..44aec805 100644 --- a/internal/platform/implementation/linux/wifi_lan_server_socket.h +++ b/internal/platform/implementation/linux/wifi_lan_server_socket.h @@ -21,19 +21,18 @@ #include #include "internal/platform/exception.h" -#include "internal/platform/implementation/linux/wifi_medium.h" +#include "internal/platform/implementation/linux/network_manager.h" #include "internal/platform/implementation/wifi_lan.h" namespace nearby { namespace linux { class WifiLanServerSocket : public api::WifiLanServerSocket { public: - explicit WifiLanServerSocket(int socket, - std::shared_ptr network_manager, - sdbus::IConnection &system_bus) + explicit WifiLanServerSocket(int socket, + std::shared_ptr network_manager) : fd_(sdbus::UnixFd(socket)), network_manager_(std::move(network_manager)), - system_bus_(system_bus) {} + system_bus_(network_manager_->GetConnection()) {} std::string GetIPAddress() const override; int GetPort() const override; @@ -44,7 +43,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket { private: sdbus::UnixFd fd_; std::shared_ptr network_manager_; - sdbus::IConnection &system_bus_; + std::shared_ptr system_bus_; }; } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/wifi_medium.cc b/internal/platform/implementation/linux/wifi_medium.cc index da49c599..cc7d734c 100644 --- a/internal/platform/implementation/linux/wifi_medium.cc +++ b/internal/platform/implementation/linux/wifi_medium.cc @@ -64,8 +64,8 @@ api::WifiInformation &NetworkManagerWifiMedium::GetInformation() { information_ = api::WifiInformation{false}; return information_; } - active_access_point = std::make_unique( - getProxy().getConnection(), ap_path); + active_access_point = + std::make_unique(*system_bus_, ap_path); } catch (const sdbus::Error &e) { DBUS_LOG_PROPERTY_GET_ERROR(this, "ActiveAccessPoint", e); } @@ -77,7 +77,7 @@ api::WifiInformation &NetworkManagerWifiMedium::GetInformation() { information_ = api::WifiInformation{true, ssid, active_access_point->HwAddress(), to_signed(active_access_point->Frequency())}; - NetworkManagerObjectManager manager(getProxy().getConnection()); + NetworkManagerObjectManager manager(system_bus_); auto ip4config = manager.GetIp4Config(active_access_point->getObjectPath()); if (ip4config != nullptr) { @@ -211,7 +211,6 @@ static inline std::pair AuthAlgAndKeyMgmt( api::WifiAuthType auth_type) { switch (auth_type) { case api::WifiAuthType::kUnknown: - return {"open", "none"}; case api::WifiAuthType::kOpen: return {"open", "none"}; case api::WifiAuthType::kWpaPsk: @@ -289,8 +288,8 @@ api::WifiConnectionStatus NetworkManagerWifiMedium::ConnectToNetwork( NEARBY_LOGS(INFO) << __func__ << ": " << getObjectPath() << ": Added a new connection at " << connection_path; - auto active_connection = NetworkManagerActiveConnection( - getProxy().getConnection(), active_conn_path); + auto active_connection = + NetworkManagerActiveConnection(system_bus_, active_conn_path); auto [reason, timeout] = active_connection.WaitForConnection(); if (timeout) { NEARBY_LOGS(ERROR) @@ -345,7 +344,7 @@ NetworkManagerWifiMedium::GetActiveConnection() { return nullptr; } - auto object_manager = NetworkManagerObjectManager(getProxy().getConnection()); + auto object_manager = NetworkManagerObjectManager(system_bus_); auto conn = object_manager.GetActiveConnectionForAccessPoint(active_ap_path, getObjectPath()); @@ -357,6 +356,5 @@ NetworkManagerWifiMedium::GetActiveConnection() { } return conn; } - } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/wifi_medium.h b/internal/platform/implementation/linux/wifi_medium.h index b4ef095b..13dce06c 100644 --- a/internal/platform/implementation/linux/wifi_medium.h +++ b/internal/platform/implementation/linux/wifi_medium.h @@ -48,10 +48,11 @@ class NetworkManagerWifiMedium delete; NetworkManagerWifiMedium &operator=(NetworkManagerWifiMedium &&) = delete; NetworkManagerWifiMedium(std::shared_ptr network_manager, - sdbus::IConnection &system_bus, const sdbus::ObjectPath &wireless_device_object_path) - : ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager", + : ProxyInterfaces(*network_manager->GetConnection(), + "org.freedesktop.NetworkManager", wireless_device_object_path), + system_bus_(network_manager->GetConnection()), network_manager_(std::move(network_manager)), last_scan_(-1) { registerProxy(); @@ -111,6 +112,7 @@ class NetworkManagerWifiMedium std::vector &ssid) ABSL_LOCKS_EXCLUDED(known_access_points_lock_); + std::shared_ptr system_bus_; std::shared_ptr network_manager_; api::WifiCapability capability_;