From cb83656eabc88fd6f875c640ae6ee843c8597495 Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Wed, 30 Aug 2023 01:19:00 +0530 Subject: [PATCH] Minor refactoring. --- .../implementation/linux/bluetooth_adapter.cc | 4 +- .../linux/bluetooth_bluez_profile.cc | 36 +++-- .../linux/bluetooth_bluez_profile.h | 72 ++++++---- .../linux/bluetooth_classic_device.cc | 4 +- .../linux/bluetooth_classic_device.h | 37 +++-- .../linux/bluetooth_classic_medium.cc | 17 ++- .../linux/bluetooth_classic_medium.h | 109 +++++++-------- .../linux/bluetooth_classic_server_socket.h | 37 +++-- .../linux/bluetooth_classic_socket.cc | 2 +- .../linux/bluetooth_classic_socket.h | 14 +- .../implementation/linux/bluetooth_devices.h | 7 +- .../implementation/linux/bluetooth_pairing.cc | 8 +- .../implementation/linux/bluetooth_pairing.h | 3 +- .../implementation/linux/device_info.cc | 10 +- .../implementation/linux/device_info.h | 64 +++++---- .../implementation/linux/thread_pool.h | 19 +-- .../implementation/linux/wifi_direct.h | 3 +- .../linux/wifi_direct_server_socket.h | 41 +++--- .../implementation/linux/wifi_direct_socket.h | 5 +- .../implementation/linux/wifi_hotspot.h | 56 ++++---- .../linux/wifi_hotspot_server_socket.h | 7 +- .../linux/wifi_hotspot_socket.h | 3 +- .../platform/implementation/linux/wifi_lan.h | 3 +- .../linux/wifi_lan_server_socket.h | 5 +- .../implementation/linux/wifi_lan_socket.h | 3 +- .../implementation/linux/wifi_medium.h | 128 +++++++++++------- 26 files changed, 392 insertions(+), 305 deletions(-) diff --git a/internal/platform/implementation/linux/bluetooth_adapter.cc b/internal/platform/implementation/linux/bluetooth_adapter.cc index fb83b31c..d57dc56f 100644 --- a/internal/platform/implementation/linux/bluetooth_adapter.cc +++ b/internal/platform/implementation/linux/bluetooth_adapter.cc @@ -91,7 +91,7 @@ std::string BluetoothAdapter::GetName() const { return bluez_adapter_->Alias(); } catch (const sdbus::Error &e) { DBUS_LOG_PROPERTY_GET_ERROR(bluez_adapter_, "Alias", e); - return std::string(); + return {}; } } @@ -115,7 +115,7 @@ std::string BluetoothAdapter::GetMacAddress() const { return bluez_adapter_->Address(); } catch (const sdbus::Error &e) { DBUS_LOG_PROPERTY_GET_ERROR(bluez_adapter_, "Address", e); - return std::string(); + return {}; } } diff --git a/internal/platform/implementation/linux/bluetooth_bluez_profile.cc b/internal/platform/implementation/linux/bluetooth_bluez_profile.cc index af0ec0a8..c88026ab 100644 --- a/internal/platform/implementation/linux/bluetooth_bluez_profile.cc +++ b/internal/platform/implementation/linux/bluetooth_bluez_profile.cc @@ -37,9 +37,9 @@ namespace nearby { namespace linux { bool ProfileManager::ProfileRegistered(absl::string_view service_uuid) { - registered_service_uuids_lock_.ReaderLock(); + registered_service_uuids_mutex_.ReaderLock(); bool registered = registered_services_.count(std::string(service_uuid)) == 1; - registered_service_uuids_lock_.ReaderUnlock(); + registered_service_uuids_mutex_.ReaderUnlock(); return registered; } @@ -78,9 +78,9 @@ void Profile::NewConnection( absl::MutexLock l(&connections_lock_); if (connections_.count(mac_addr) != 0) { - connections_[mac_addr].push_back(std::pair(fd, std::move(props))); + connections_[mac_addr].push_back(std::pair(fd, props)); } else { - connections_[mac_addr] = std::vector{std::pair(fd, std::move(props))}; + connections_[mac_addr] = std::vector{std::pair(fd, props)}; } } @@ -135,7 +135,7 @@ bool ProfileManager::Register(std::optional name, } { - absl::MutexLock l(®istered_service_uuids_lock_); + absl::MutexLock l(®istered_service_uuids_mutex_); registered_services_.emplace(service_uuid, profile); } @@ -165,7 +165,7 @@ void ProfileManager::Unregister(absl::string_view service_uuid) { } { - absl::MutexLock l(®istered_service_uuids_lock_); + absl::MutexLock l(®istered_service_uuids_mutex_); registered_services_.erase(std::string(service_uuid)); } } @@ -184,9 +184,9 @@ ProfileManager::GetServiceRecordFD(api::BluetoothDevice &remote_device, auto mac_addr = remote_device.GetMacAddress(); - registered_service_uuids_lock_.ReaderLock(); + registered_service_uuids_mutex_.ReaderLock(); auto profile = registered_services_[std::string(service_uuid)]; - registered_service_uuids_lock_.ReaderUnlock(); + registered_service_uuids_mutex_.ReaderUnlock(); NEARBY_LOGS(VERBOSE) << __func__ << ": " << profile->getObjectPath() << ": Attempting to get a FD for service " @@ -226,16 +226,19 @@ ProfileManager::GetServiceRecordFD(absl::string_view service_uuid) { return std::nullopt; } - registered_service_uuids_lock_.ReaderLock(); + registered_service_uuids_mutex_.ReaderLock(); auto profile = registered_services_[std::string(service_uuid)]; - registered_service_uuids_lock_.ReaderUnlock(); + registered_service_uuids_mutex_.ReaderUnlock(); NEARBY_LOGS(VERBOSE) << __func__ << ": " << profile->getObjectPath() << ": Attempting to get a FD for service " - << profile->getObjectPath(); + << service_uuid; profile->connections_lock_.Lock(); - auto cond = [profile]() { return !profile->connections_.empty(); }; + auto cond = [profile]() { + profile->connections_lock_.AssertReaderHeld(); + return !profile->connections_.empty(); + }; profile->connections_lock_.Await(absl::Condition(&cond)); auto it = profile->connections_.begin(); @@ -246,7 +249,14 @@ ProfileManager::GetServiceRecordFD(absl::string_view service_uuid) { profile->connections_.erase(it); profile->connections_lock_.Unlock(); - return std::pair(devices_.get_device_by_address(mac_addr).value(), fd); + auto maybe_device = devices_.get_device_by_address(mac_addr); + if (!maybe_device.has_value()) { + NEARBY_LOGS(ERROR) << __func__ << ": Device " << mac_addr + << " is no longer available"; + return std::nullopt; + } + + return std::pair(*maybe_device, fd); } } // namespace linux diff --git a/internal/platform/implementation/linux/bluetooth_bluez_profile.h b/internal/platform/implementation/linux/bluetooth_bluez_profile.h index ff447480..06ee742e 100644 --- a/internal/platform/implementation/linux/bluetooth_bluez_profile.h +++ b/internal/platform/implementation/linux/bluetooth_bluez_profile.h @@ -38,26 +38,38 @@ #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/linux/bluetooth_devices.h" #include "internal/platform/implementation/linux/bluez.h" -#include "internal/platform/implementation/linux/generated/dbus/bluez/profile_server.h" #include "internal/platform/implementation/linux/generated/dbus/bluez/profile_manager_client.h" +#include "internal/platform/implementation/linux/generated/dbus/bluez/profile_server.h" #include "internal/platform/logging.h" namespace nearby { namespace linux { -class Profile : public sdbus::AdaptorInterfaces { -public: +class ProfileManager; + +class Profile final + : public sdbus::AdaptorInterfaces { + public: + Profile(const Profile &) = delete; + Profile(Profile &&) = delete; + Profile &operator=(const Profile &) = delete; + Profile &operator=(Profile &&) = delete; Profile(sdbus::IConnection &system_bus, absl::string_view profile_object_path, BluetoothDevices &devices) : AdaptorInterfaces(system_bus, std::string(profile_object_path)), - released_(false), devices_(devices) { + released_(false), + devices_(devices) { registerAdaptor(); NEARBY_LOGS(VERBOSE) << __func__ << ": Created a new BlueZ profile at :" << getObjectPath(); } ~Profile() { unregisterAdaptor(); } + private: + friend class ProfileManager; + struct FDProperties { - FDProperties(const std::map &fd_props) { + explicit FDProperties(const std::map &fd_props) + : version(std::nullopt), features(std::nullopt) { if (fd_props.count("Version") == 1) { version = fd_props.at("Version"); } @@ -72,21 +84,27 @@ public: void Release() override; void NewConnection(const sdbus::ObjectPath &, const sdbus::UnixFd &, - const std::map &) override; - void RequestDisconnection(const sdbus::ObjectPath &) override; + const std::map &) override + ABSL_LOCKS_EXCLUDED(connections_lock_); + void RequestDisconnection(const sdbus::ObjectPath &) override + ABSL_LOCKS_EXCLUDED(connections_lock_); std::atomic_bool released_; absl::Mutex connections_lock_; std::map>> - connections_; + connections_ ABSL_GUARDED_BY(connections_lock_); BluetoothDevices &devices_; }; -class ProfileManager +class ProfileManager final : private sdbus::ProxyInterfaces { -public: + public: + ProfileManager(const ProfileManager &) = delete; + ProfileManager(ProfileManager &&) = delete; + ProfileManager &operator=(const ProfileManager &) = delete; + ProfileManager &operator=(ProfileManager &&) = delete; ProfileManager(sdbus::IConnection &system_bus, BluetoothDevices &devices) : ProxyInterfaces(system_bus, bluez::SERVICE_DEST, "/org/bluez"), devices_(devices) { @@ -94,29 +112,35 @@ public: } ~ProfileManager() { unregisterProxy(); } - bool ProfileRegistered(absl::string_view service_uuid); + bool ProfileRegistered(absl::string_view service_uuid) + ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_); bool Register(std::optional service_name, - absl::string_view service_uuid); - bool Register(absl::string_view service_uuid) { + absl::string_view service_uuid) + ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_); + bool Register(absl::string_view service_uuid) + ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_) { return Register(std::nullopt, service_uuid); } - void Unregister(absl::string_view service_uuid); + void Unregister(absl::string_view service_uuid) + ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_); - std::optional - GetServiceRecordFD(api::BluetoothDevice &remote_device, - absl::string_view service_uuid, - CancellationFlag *cancellation_flag); + std::optional GetServiceRecordFD( + api::BluetoothDevice &remote_device, absl::string_view service_uuid, + CancellationFlag *cancellation_flag) + ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_); std::optional< std::pair, sdbus::UnixFd>> - GetServiceRecordFD(absl::string_view service_uuid); + GetServiceRecordFD(absl::string_view service_uuid) + ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_); -private: + private: BluetoothDevices &devices_; // Maps service UUIDs to RegisteredService - std::map> registered_services_; - absl::Mutex registered_service_uuids_lock_; + absl::Mutex registered_service_uuids_mutex_; + std::map> registered_services_ + ABSL_GUARDED_BY(registered_service_uuids_mutex_); }; -} // namespace linux -} // namespace nearby +} // namespace linux +} // namespace nearby #endif diff --git a/internal/platform/implementation/linux/bluetooth_classic_device.cc b/internal/platform/implementation/linux/bluetooth_classic_device.cc index 8170c7f9..2d1f9d36 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_device.cc +++ b/internal/platform/implementation/linux/bluetooth_classic_device.cc @@ -25,9 +25,9 @@ namespace nearby { namespace linux { BluetoothDevice::BluetoothDevice(sdbus::IConnection &system_bus, - const sdbus::ObjectPath &device_object_path) + sdbus::ObjectPath device_object_path) : ProxyInterfaces(system_bus, bluez::SERVICE_DEST, - std::string(device_object_path)) { + std::move(device_object_path)) { registerProxy(); try { last_known_name_ = Alias(); diff --git a/internal/platform/implementation/linux/bluetooth_classic_device.h b/internal/platform/implementation/linux/bluetooth_classic_device.h index c5d5073e..7f7f5aa2 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_device.h +++ b/internal/platform/implementation/linux/bluetooth_classic_device.h @@ -34,21 +34,27 @@ class BluetoothDevice : public api::BluetoothDevice, public sdbus::ProxyInterfaces { public: - BluetoothDevice(sdbus::IConnection &system_bus, const sdbus::ObjectPath &); - ~BluetoothDevice() = default; + BluetoothDevice(const BluetoothDevice &) = delete; + BluetoothDevice(BluetoothDevice &&) = delete; + BluetoothDevice &operator=(const BluetoothDevice &) = delete; + BluetoothDevice &operator=(BluetoothDevice &&) = delete; + BluetoothDevice(sdbus::IConnection &system_bus, sdbus::ObjectPath device_object_path); + ~BluetoothDevice() override { + unregisterProxy(); + } - // https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getName() - std::string GetName() const override; + // https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getName() + std::string GetName() const override; - // Returns BT MAC address assigned to this device. - std::string GetMacAddress() const override; + // Returns BT MAC address assigned to this device. + std::string GetMacAddress() const override; - bool ConnectToProfile(absl::string_view service_uuid); + bool ConnectToProfile(absl::string_view service_uuid); - void - set_pair_reply_callback(absl::AnyInvocable cb) { - absl::MutexLock l(&pair_callback_lock_); - on_pair_reply_cb_ = std::move(cb); + void set_pair_reply_callback( + absl::AnyInvocable cb) { + absl::MutexLock l(&pair_callback_lock_); + on_pair_reply_cb_ = std::move(cb); } void reset_pair_reply_callback() { @@ -73,7 +79,7 @@ private: mutable std::string last_known_address_ ABSL_GUARDED_BY(properties_mutex_); }; -class MonitoredBluetoothDevice +class MonitoredBluetoothDevice final : public BluetoothDevice, public sdbus::ProxyInterfaces { public: @@ -81,10 +87,15 @@ public: using sdbus::ProxyInterfaces::unregisterProxy; using sdbus::ProxyInterfaces::getObjectPath; + MonitoredBluetoothDevice(const MonitoredBluetoothDevice &) = delete; + MonitoredBluetoothDevice(MonitoredBluetoothDevice &&) = delete; + MonitoredBluetoothDevice &operator=(const MonitoredBluetoothDevice &) = + delete; + MonitoredBluetoothDevice &operator=(MonitoredBluetoothDevice &&) = delete; MonitoredBluetoothDevice( sdbus::IConnection &system_bus, const sdbus::ObjectPath &, ObserverList &observers); - ~MonitoredBluetoothDevice() { unregisterProxy(); } + ~MonitoredBluetoothDevice() override { unregisterProxy(); } protected: void onPropertiesChanged( diff --git a/internal/platform/implementation/linux/bluetooth_classic_medium.cc b/internal/platform/implementation/linux/bluetooth_classic_medium.cc index 91d7cb67..8b2b7f0d 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_medium.cc +++ b/internal/platform/implementation/linux/bluetooth_classic_medium.cc @@ -47,8 +47,6 @@ BluetoothClassicMedium::BluetoothClassicMedium( registerProxy(); } -BluetoothClassicMedium::~BluetoothClassicMedium() { unregisterProxy(); } - void BluetoothClassicMedium::onInterfacesAdded( const sdbus::ObjectPath &object, const std::map> @@ -74,7 +72,7 @@ void BluetoothClassicMedium::onInterfacesAdded( discovery_cb_->device_discovered_cb(device); } - for (auto &observer : observers_.GetObservers()) { + for (const auto &observer : observers_.GetObservers()) { observer->DeviceAdded(device); } } @@ -88,7 +86,7 @@ void BluetoothClassicMedium::onInterfacesRemoved( return; } - for (auto &interface : interfaces) { + for (const auto &interface : interfaces) { if (interface == org::bluez::Device1_proxy::INTERFACE_NAME) { { auto device = devices_->get_device_by_path(object); @@ -107,7 +105,7 @@ void BluetoothClassicMedium::onInterfacesRemoved( discovery_cb_->device_lost_cb(*device); } - for (auto &observer : observers_.GetObservers()) { + for (const auto &observer : observers_.GetObservers()) { observer->DeviceRemoved(*device); } } @@ -166,7 +164,10 @@ BluetoothClassicMedium::ConnectToService(api::BluetoothDevice &remote_device, } } - auto &device = devices_->get_device_by_path(device_object_path).value().get(); + auto maybe_device = devices_->get_device_by_path(device_object_path); + if (!maybe_device.has_value()) return nullptr; + + auto &device = maybe_device->get(); device.ConnectToProfile(service_uuid); auto fd = profile_manager_->GetServiceRecordFD(remote_device, service_uuid, @@ -202,7 +203,7 @@ BluetoothClassicMedium::ListenForService(const std::string &service_name, api::BluetoothDevice * BluetoothClassicMedium::GetRemoteDevice(const std::string &mac_address) { auto device = devices_->get_device_by_address(mac_address); - if (device.has_value()) + if (!device.has_value()) return nullptr; return &(device->get()); @@ -211,6 +212,8 @@ BluetoothClassicMedium::GetRemoteDevice(const std::string &mac_address) { std::unique_ptr BluetoothClassicMedium::CreatePairing(api::BluetoothDevice &remote_device) { auto device = devices_->get_device_by_address(remote_device.GetMacAddress()); + if (!device.has_value()) return nullptr; + return std::unique_ptr( new BluetoothPairing(*adapter_, *device)); } diff --git a/internal/platform/implementation/linux/bluetooth_classic_medium.h b/internal/platform/implementation/linux/bluetooth_classic_medium.h index 913a91fe..68cd26b3 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_medium.h +++ b/internal/platform/implementation/linux/bluetooth_classic_medium.h @@ -39,69 +39,70 @@ namespace nearby { namespace linux { // Container of operations that can be performed over the Bluetooth Classic // medium. -class BluetoothClassicMedium +class BluetoothClassicMedium final : public api::BluetoothClassicMedium, sdbus::ProxyInterfaces { public: - BluetoothClassicMedium(sdbus::IConnection &system_bus, - const sdbus::ObjectPath &adapter_object_path); - ~BluetoothClassicMedium() override; + BluetoothClassicMedium(const BluetoothClassicMedium &) = delete; + BluetoothClassicMedium(BluetoothClassicMedium &&) = delete; + BluetoothClassicMedium &operator=(const BluetoothClassicMedium &) = delete; + BluetoothClassicMedium &operator=(BluetoothClassicMedium &&) = delete; + BluetoothClassicMedium(sdbus::IConnection &system_bus, + const sdbus::ObjectPath &adapter_object_path); + ~BluetoothClassicMedium() override { unregisterProxy(); }; - // https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery() - // - // Returns true once the process of discovery has been initiated. - bool StartDiscovery(DiscoveryCallback discovery_callback) override; - // https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#cancelDiscovery() - // - // Returns true once discovery is well and truly stopped; after this returns, - // there must be no more invocations of the DiscoveryCallback passed in to - // StartDiscovery(). - bool StopDiscovery() override; + // https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery() + // + // Returns true once the process of discovery has been initiated. + bool StartDiscovery(DiscoveryCallback discovery_callback) override; + // https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#cancelDiscovery() + // + // Returns true once discovery is well and truly stopped; after this returns, + // there must be no more invocations of the DiscoveryCallback passed in to + // StartDiscovery(). + bool StopDiscovery() override; - // A combination of - // https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createInsecureRfcommSocketToServiceRecord - // followed by - // https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#connect(). - // - // service_uuid is the canonical textual representation - // (https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) of a - // type 3 name-based - // (https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based)) - // UUID. - // - // On success, returns a new BluetoothSocket. - // On error, returns nullptr. - std::unique_ptr - ConnectToService(api::BluetoothDevice &remote_device, - const std::string &service_uuid, - CancellationFlag *cancellation_flag) override; + // A combination of + // https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createInsecureRfcommSocketToServiceRecord + // followed by + // https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#connect(). + // + // service_uuid is the canonical textual representation + // (https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) of a + // type 3 name-based + // (https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based)) + // UUID. + // + // On success, returns a new BluetoothSocket. + // On error, returns nullptr. + std::unique_ptr ConnectToService( + api::BluetoothDevice &remote_device, const std::string &service_uuid, + CancellationFlag *cancellation_flag) override; - // https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#listenUsingInsecureRfcommWithServiceRecord - // - // service_uuid is the canonical textual representation - // (https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) of a - // type 3 name-based - // (https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based)) - // UUID. - // - // Returns nullptr error. - std::unique_ptr - ListenForService(const std::string &service_name, - const std::string &service_uuid) override; + // https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#listenUsingInsecureRfcommWithServiceRecord + // + // service_uuid is the canonical textual representation + // (https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) of a + // type 3 name-based + // (https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based)) + // UUID. + // + // Returns nullptr error. + std::unique_ptr ListenForService( + const std::string &service_name, const std::string &service_uuid) override; - // https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createBond() - // - // Start the bonding (pairing) process with the remote device. - // Return a Bluetooth pairing instance to handle the pairing process with the - // remote device. - std::unique_ptr - CreatePairing(api::BluetoothDevice &remote_device) override; + // https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createBond() + // + // Start the bonding (pairing) process with the remote device. + // Return a Bluetooth pairing instance to handle the pairing process with the + // remote device. + std::unique_ptr CreatePairing( + api::BluetoothDevice &remote_device) override; - api::BluetoothDevice * - GetRemoteDevice(const std::string &mac_address) override; + api::BluetoothDevice *GetRemoteDevice(const std::string &mac_address) override; - void AddObserver(Observer *observer) override { - observers_.AddObserver(observer); + void AddObserver(Observer *observer) override { + observers_.AddObserver(observer); }; void RemoveObserver(Observer *observer) override { observers_.RemoveObserver(observer); diff --git a/internal/platform/implementation/linux/bluetooth_classic_server_socket.h b/internal/platform/implementation/linux/bluetooth_classic_server_socket.h index d898794e..b503b3ec 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_server_socket.h +++ b/internal/platform/implementation/linux/bluetooth_classic_server_socket.h @@ -22,28 +22,27 @@ namespace nearby { namespace linux { -class BluetoothServerSocket : public api::BluetoothServerSocket { +class BluetoothServerSocket final : public api::BluetoothServerSocket { public: - BluetoothServerSocket(ProfileManager &profile_manager, - absl::string_view service_uuid) - : profile_manager_(profile_manager), service_uuid_(service_uuid) {} - ~BluetoothServerSocket() = default; + BluetoothServerSocket(ProfileManager &profile_manager, + absl::string_view service_uuid) + : profile_manager_(profile_manager), service_uuid_(service_uuid) {} - // https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html#accept() - // - // Blocks until either: - // - at least one incoming connection request is available, or - // - ServerSocket is closed. - // On success, returns connected socket, ready to exchange data. - // Returns nullptr on error. - // Once error is reported, it is permanent, and ServerSocket has to be - // closed. - std::unique_ptr Accept() override; + // https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html#accept() + // + // Blocks until either: + // - at least one incoming connection request is available, or + // - ServerSocket is closed. + // On success, returns connected socket, ready to exchange data. + // Returns nullptr on error. + // Once error is reported, it is permanent, and ServerSocket has to be + // closed. + std::unique_ptr Accept() override; - // https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html#close() - // - // Returns Exception::kIo on error, Exception::kSuccess otherwise. - Exception Close() override; + // https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html#close() + // + // Returns Exception::kIo on error, Exception::kSuccess otherwise. + Exception Close() override; private: ProfileManager &profile_manager_; diff --git a/internal/platform/implementation/linux/bluetooth_classic_socket.cc b/internal/platform/implementation/linux/bluetooth_classic_socket.cc index 859f9014..2f79ef5c 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_socket.cc +++ b/internal/platform/implementation/linux/bluetooth_classic_socket.cc @@ -55,7 +55,7 @@ Exception OutputStream::Write(const ByteArray &data) { if (!fd_.has_value()) return Exception{Exception::kIo}; - ssize_t written = 0; + size_t written = 0; while (written < data.size()) { ssize_t ret = write(fd_->get(), data.data(), data.size()); if (ret < 1) { diff --git a/internal/platform/implementation/linux/bluetooth_classic_socket.h b/internal/platform/implementation/linux/bluetooth_classic_socket.h index 18266236..dcefc4e5 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_socket.h +++ b/internal/platform/implementation/linux/bluetooth_classic_socket.h @@ -27,15 +27,15 @@ namespace nearby { namespace linux { -class BluetoothSocket : public api::BluetoothSocket { +class BluetoothSocket final : public api::BluetoothSocket { public: - BluetoothSocket(api::BluetoothDevice &device, sdbus::UnixFd fd) - : device_(device), output_stream_(fd), input_stream_(fd) {} + BluetoothSocket(api::BluetoothDevice &device, sdbus::UnixFd fd) + : device_(device), output_stream_(fd), input_stream_(fd) {} - nearby::InputStream &GetInputStream() override { return input_stream_; } - nearby::OutputStream &GetOutputStream() override { return output_stream_; } - Exception Close() override; - api::BluetoothDevice *GetRemoteDevice() override { return &device_; }; + nearby::InputStream &GetInputStream() override { return input_stream_; } + nearby::OutputStream &GetOutputStream() override { return output_stream_; } + Exception Close() override; + api::BluetoothDevice *GetRemoteDevice() override { return &device_; }; private: api::BluetoothDevice &device_; diff --git a/internal/platform/implementation/linux/bluetooth_devices.h b/internal/platform/implementation/linux/bluetooth_devices.h index 67ef813a..49804517 100644 --- a/internal/platform/implementation/linux/bluetooth_devices.h +++ b/internal/platform/implementation/linux/bluetooth_devices.h @@ -28,15 +28,14 @@ namespace nearby { namespace linux { -class BluetoothDevices { +class BluetoothDevices final { public: BluetoothDevices( sdbus::IConnection &system_bus, - const sdbus::ObjectPath &adapter_object_path, + sdbus::ObjectPath adapter_object_path, ObserverList &observers) : system_bus_(system_bus), observers_(observers), - adapter_object_path_(adapter_object_path) {} - ~BluetoothDevices() = default; + adapter_object_path_(std::move(adapter_object_path)) {} std::optional> get_device_by_path(const sdbus::ObjectPath &); diff --git a/internal/platform/implementation/linux/bluetooth_pairing.cc b/internal/platform/implementation/linux/bluetooth_pairing.cc index da612a3b..d4ec92c6 100644 --- a/internal/platform/implementation/linux/bluetooth_pairing.cc +++ b/internal/platform/implementation/linux/bluetooth_pairing.cc @@ -29,8 +29,9 @@ namespace linux { void BluetoothPairing::pairing_reply_handler(const sdbus::Error *error) { if (error != nullptr && error->isValid()) { - auto name = error->getName(); - api::BluetoothPairingCallback::PairingError err; + const auto &name = error->getName(); + api::BluetoothPairingCallback::PairingError err = + api::BluetoothPairingCallback::PairingError::kAuthFailed; NEARBY_LOGS(ERROR) << __func__ << ": " << "Got error '" << error->getName() @@ -46,8 +47,6 @@ void BluetoothPairing::pairing_reply_handler(const sdbus::Error *error) { err = api::BluetoothPairingCallback::PairingError::kAuthRejected; } else if (name == "org.bluez.Error.AuthenticationTimeout") { err = api::BluetoothPairingCallback::PairingError::kAuthTimeout; - } else { - err = api::BluetoothPairingCallback::PairingError::kAuthFailed; } if (pairing_cb_.on_pairing_error_cb != nullptr) { @@ -59,7 +58,6 @@ void BluetoothPairing::pairing_reply_handler(const sdbus::Error *error) { if (pairing_cb_.on_paired_cb != nullptr) { pairing_cb_.on_paired_cb(); } - return; } BluetoothPairing::BluetoothPairing(BluetoothAdapter &adapter, diff --git a/internal/platform/implementation/linux/bluetooth_pairing.h b/internal/platform/implementation/linux/bluetooth_pairing.h index 917a7278..2c2fbac2 100644 --- a/internal/platform/implementation/linux/bluetooth_pairing.h +++ b/internal/platform/implementation/linux/bluetooth_pairing.h @@ -29,10 +29,9 @@ namespace nearby { namespace linux { -class BluetoothPairing : public api::BluetoothPairing { +class BluetoothPairing final : public api::BluetoothPairing { public: BluetoothPairing(BluetoothAdapter &adapter, BluetoothDevice &remote_device); - ~BluetoothPairing() override = default; bool InitiatePairing(api::BluetoothPairingCallback pairing_cb) override; bool FinishPairing(std::optional pin_code) override; diff --git a/internal/platform/implementation/linux/device_info.cc b/internal/platform/implementation/linux/device_info.cc index c8894843..17287dbd 100644 --- a/internal/platform/implementation/linux/device_info.cc +++ b/internal/platform/implementation/linux/device_info.cc @@ -108,7 +108,7 @@ std::optional DeviceInfo::GetFullName() const { std::optional DeviceInfo::GetProfileUserName() const { struct passwd *pwd = getpwuid(getuid()); - if (!pwd) { + if (pwd == nullptr) { return std::nullopt; } char *name = strtok(pwd->pw_gecos, ","); @@ -122,7 +122,7 @@ std::optional DeviceInfo::GetDownloadPath() const { std::optional DeviceInfo::GetLocalAppDataPath() const { char *dir = getenv("XDG_CONFIG_HOME"); - if (dir == NULL) { + if (dir == nullptr) { return std::filesystem::path("/tmp"); } return std::filesystem::path(std::string(dir)) / "Google Nearby"; @@ -130,7 +130,7 @@ std::optional DeviceInfo::GetLocalAppDataPath() const { std::optional DeviceInfo::GetTemporaryPath() const { char *dir = getenv("XDG_RUNTIME_PATH"); - if (dir == NULL) { + if (dir == nullptr) { return std::filesystem::path("/tmp"); } return std::filesystem::path(std::string(dir)) / "Google Nearby"; @@ -138,7 +138,7 @@ std::optional DeviceInfo::GetTemporaryPath() const { std::optional DeviceInfo::GetLogPath() const { char *dir = getenv("XDG_STATE_HOME"); - if (dir == NULL) { + if (dir == nullptr) { return std::filesystem::path("/tmp"); } return std::filesystem::path(std::string(dir)) / "Google Nearby" / "logs"; @@ -146,7 +146,7 @@ std::optional DeviceInfo::GetLogPath() const { std::optional DeviceInfo::GetCrashDumpPath() const { char *dir = getenv("XDG_STATE_HOME"); - if (dir == NULL) { + if (dir == nullptr) { return std::filesystem::path("/tmp"); } return std::filesystem::path(std::string(dir)) / "Google Nearby" / "crashes"; diff --git a/internal/platform/implementation/linux/device_info.h b/internal/platform/implementation/linux/device_info.h index 139885fe..281884fd 100644 --- a/internal/platform/implementation/linux/device_info.h +++ b/internal/platform/implementation/linux/device_info.h @@ -34,15 +34,19 @@ namespace nearby { namespace linux { -class CurrentUserSession +class CurrentUserSession final : public sdbus::ProxyInterfaces { -public: - CurrentUserSession(sdbus::IConnection &system_bus) + public: + CurrentUserSession(const CurrentUserSession &) = delete; + CurrentUserSession(CurrentUserSession &&) = delete; + CurrentUserSession &operator=(const CurrentUserSession &) = delete; + CurrentUserSession &operator=(CurrentUserSession &&) = delete; + ~CurrentUserSession() { unregisterProxy(); } + explicit CurrentUserSession(sdbus::IConnection &system_bus) : ProxyInterfaces(system_bus, "org.freedesktop.login1", "/org/freedesktop/login1/session/auto") { registerProxy(); } - ~CurrentUserSession() { unregisterProxy(); } void RegisterScreenLockedListener( absl::string_view listener_name, @@ -51,7 +55,7 @@ public: void UnregisterScreenLockedListener(absl::string_view listener_name) ABSL_LOCKS_EXCLUDED(screen_lock_listeners_mutex_); -protected: + protected: void onPauseDevice(const uint32_t &major, const uint32_t &minor, const std::string &type) override {} void onResumeDevice(const uint32_t &major, const uint32_t &minor, @@ -60,7 +64,7 @@ protected: void onLock() override ABSL_LOCKS_EXCLUDED(screen_lock_listeners_mutex_); void onUnlock() override ABSL_LOCKS_EXCLUDED(screen_lock_listeners_mutex_); -private: + private: absl::Mutex screen_lock_listeners_mutex_; absl::flat_hash_map> @@ -69,26 +73,33 @@ private: class Hostnamed : public sdbus::ProxyInterfaces { -public: - Hostnamed(sdbus::IConnection &system_bus) + public: + Hostnamed(const Hostnamed &) = delete; + Hostnamed(Hostnamed &&) = delete; + Hostnamed &operator=(const Hostnamed &) = delete; + Hostnamed &operator=(Hostnamed &&) = delete; + explicit Hostnamed(sdbus::IConnection &system_bus) : ProxyInterfaces(system_bus, "org.freedesktop.hostname1", "/org/freedesktop/hostname1") { registerProxy(); } - ~Hostnamed() { unregisterProxy(); } + ~Hostnamed() { unregisterProxy(); } }; -class LoginManager +class LoginManager final : public sdbus::ProxyInterfaces { -public: - LoginManager(sdbus::IConnection &system_bus) - : ProxyInterfaces("org.freedesktop.login1", - "/org/freedesktop/login1") { + public: + LoginManager(const LoginManager &) = delete; + LoginManager(LoginManager &&) = delete; + LoginManager &operator=(const LoginManager &) = delete; + LoginManager &operator=(LoginManager &&) = delete; + explicit LoginManager(sdbus::IConnection &system_bus) + : ProxyInterfaces(system_bus, "org.freedesktop.login1", "/org/freedesktop/login1") { registerProxy(); } ~LoginManager() { unregisterProxy(); } -protected: + protected: void onSessionNew(const std::string &session_id, const sdbus::ObjectPath &object_path) override {} void onSessionRemoved(const std::string &session_id, @@ -105,15 +116,14 @@ protected: void onPrepareForSleep(const bool &start) override {} }; -class DeviceInfo : public api::DeviceInfo { -public: - DeviceInfo(sdbus::IConnection &system_bus); - ~DeviceInfo() override = default; +class DeviceInfo final : public api::DeviceInfo { + public: + explicit DeviceInfo(sdbus::IConnection &system_bus); std::optional GetOsDeviceName() const override; api::DeviceInfo::DeviceType GetDeviceType() const override; api::DeviceInfo::OsType GetOsType() const override { - return api::DeviceInfo::OsType::kWindows; // Or ChromeOS? + return api::DeviceInfo::OsType::kWindows; // Or ChromeOS? } std::optional GetFullName() const override; std::optional GetGivenName() const override { @@ -138,23 +148,23 @@ public: absl::string_view listener_name, std::function callback) override { current_user_session_->RegisterScreenLockedListener(listener_name, - std::move(callback)); + std::move(callback)); } - void - UnregisterScreenLockedListener(absl::string_view listener_name) override { + void UnregisterScreenLockedListener( + absl::string_view listener_name) override { current_user_session_->UnregisterScreenLockedListener(listener_name); } bool PreventSleep() override; bool AllowSleep() override; -private: + private: sdbus::IConnection &system_bus_; std::unique_ptr current_user_session_; std::unique_ptr login_manager_; std::optional inhibit_fd_; }; -} // namespace linux -} // namespace nearby +} // namespace linux +} // namespace nearby -#endif // PLATFORM_IMPL_LINUX_DEVICE_INFO_H_ +#endif // PLATFORM_IMPL_LINUX_DEVICE_INFO_H_ diff --git a/internal/platform/implementation/linux/thread_pool.h b/internal/platform/implementation/linux/thread_pool.h index a5a9aa42..7f2b0fc1 100644 --- a/internal/platform/implementation/linux/thread_pool.h +++ b/internal/platform/implementation/linux/thread_pool.h @@ -31,21 +31,24 @@ namespace linux { class ThreadPool { public: - ThreadPool(size_t max_pool_size); - ~ThreadPool(); + ThreadPool(const ThreadPool &) = delete; + ThreadPool(ThreadPool &&) = delete; + ThreadPool &operator=(const ThreadPool &) = delete; + ThreadPool &operator=(ThreadPool &&) = delete; + explicit ThreadPool(size_t max_pool_size); + ~ThreadPool(); - bool Start() ABSL_LOCKS_EXCLUDED(mutex_); + bool Start() ABSL_LOCKS_EXCLUDED(mutex_); - // Runs a task on thread pool. The result indicates whether the task is put - // into the thread pool. - bool Run(Runnable &&task) ABSL_LOCKS_EXCLUDED(mutex_); + // Runs a task on thread pool. The result indicates whether the task is put + // into the thread pool. + bool Run(Runnable &&task) ABSL_LOCKS_EXCLUDED(mutex_); - void ShutDown() ABSL_LOCKS_EXCLUDED(mutex_); + void ShutDown() ABSL_LOCKS_EXCLUDED(mutex_); private: Runnable NextTask() ABSL_LOCKS_EXCLUDED(mutex_); -private: size_t max_pool_size_; std::atomic_bool shut_down_; diff --git a/internal/platform/implementation/linux/wifi_direct.h b/internal/platform/implementation/linux/wifi_direct.h index 3e5025ad..bbaf720c 100644 --- a/internal/platform/implementation/linux/wifi_direct.h +++ b/internal/platform/implementation/linux/wifi_direct.h @@ -30,9 +30,8 @@ public: sdbus::IConnection &system_bus, std::shared_ptr network_manager, std::unique_ptr wireless_device) - : system_bus_(system_bus), network_manager_(network_manager), + : system_bus_(system_bus), network_manager_(std::move(network_manager)), wireless_device_(std::move(wireless_device)) {} - ~NetworkManagerWifiDirectMedium() {} bool IsInterfaceValid() const override { return true; } std::unique_ptr diff --git a/internal/platform/implementation/linux/wifi_direct_server_socket.h b/internal/platform/implementation/linux/wifi_direct_server_socket.h index f0261667..1f5fe879 100644 --- a/internal/platform/implementation/linux/wifi_direct_server_socket.h +++ b/internal/platform/implementation/linux/wifi_direct_server_socket.h @@ -15,30 +15,35 @@ #ifndef PLATFORM_IMPL_LINUX_WIFI_DIRECT_SERVER_SOCKET_H_ #define PLATFORM_IMPL_LINUX_WIFI_DIRECT_SERVER_SOCKET_H_ +#include #include "internal/platform/implementation/linux/wifi_medium.h" #include "internal/platform/implementation/wifi_direct.h" -#include namespace nearby { - namespace linux { - class NetworkManagerWifiDirectServerSocket - : public api::WifiDirectServerSocket { -public: - NetworkManagerWifiDirectServerSocket(int socket, sdbus::IConnection &system_bus, - const sdbus::ObjectPath &active_connection_path, - std::shared_ptr network_manager) : fd_(socket), system_bus_(system_bus), active_connection_path_(active_connection_path), network_manager_(network_manager) {} - ~NetworkManagerWifiDirectServerSocket() {} +namespace linux { +class NetworkManagerWifiDirectServerSocket + : public api::WifiDirectServerSocket { + public: + NetworkManagerWifiDirectServerSocket( + int socket, sdbus::IConnection &system_bus, + sdbus::ObjectPath active_connection_path, + std::shared_ptr network_manager) + : fd_(socket), + system_bus_(system_bus), + active_connection_path_(std::move(active_connection_path)), + network_manager_(std::move(network_manager)) {} - std::string GetIPAddress() const override; - int GetPort() const override; - std::unique_ptr Accept() override; - Exception Close() override; -private: + std::string GetIPAddress() const override; + int GetPort() const override; + std::unique_ptr Accept() override; + Exception Close() override; + + private: sdbus::UnixFd fd_; sdbus::IConnection &system_bus_; sdbus::ObjectPath active_connection_path_; - std::shared_ptr network_manager_ ; - }; - } -} + std::shared_ptr network_manager_; +}; +} // namespace linux +} // namespace nearby #endif diff --git a/internal/platform/implementation/linux/wifi_direct_socket.h b/internal/platform/implementation/linux/wifi_direct_socket.h index 069eb0c2..25472d8f 100644 --- a/internal/platform/implementation/linux/wifi_direct_socket.h +++ b/internal/platform/implementation/linux/wifi_direct_socket.h @@ -23,9 +23,8 @@ namespace nearby { namespace linux { class WifiDirectSocket : public api::WifiDirectSocket { public: - WifiDirectSocket(int socket) - : fd_(sdbus::UnixFd(socket)), output_stream_(fd_), input_stream_(fd_) {} - ~WifiDirectSocket() = default; + explicit WifiDirectSocket(int socket) + : fd_(sdbus::UnixFd(socket)), output_stream_(fd_), input_stream_(fd_) {} InputStream &GetInputStream() override { return input_stream_; }; OutputStream &GetOutputStream() override { return output_stream_; }; diff --git a/internal/platform/implementation/linux/wifi_hotspot.h b/internal/platform/implementation/linux/wifi_hotspot.h index 9873ea3d..d1061e47 100644 --- a/internal/platform/implementation/linux/wifi_hotspot.h +++ b/internal/platform/implementation/linux/wifi_hotspot.h @@ -25,38 +25,38 @@ namespace nearby { namespace linux { class NetworkManagerWifiHotspotMedium : public api::WifiHotspotMedium { public: - NetworkManagerWifiHotspotMedium( - sdbus::IConnection &system_bus, - std::shared_ptr network_manager, - const sdbus::ObjectPath &wireless_device_object_path) - : system_bus_(system_bus), - wireless_device_(std::make_unique( - network_manager, system_bus, wireless_device_object_path)), - network_manager_(network_manager) {} - NetworkManagerWifiHotspotMedium( - sdbus::IConnection &system_bus, - std::shared_ptr network_manager, - std::unique_ptr wireless_device) - : system_bus_(system_bus), wireless_device_(std::move(wireless_device)), - network_manager_(network_manager) {} - ~NetworkManagerWifiHotspotMedium() {} + NetworkManagerWifiHotspotMedium( + sdbus::IConnection &system_bus, + std::shared_ptr network_manager, + sdbus::ObjectPath wireless_device_object_path) + : system_bus_(system_bus), + wireless_device_(std::make_unique( + network_manager, system_bus, 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), + 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, - CancellationFlag *cancellation_flag) override; - std::unique_ptr - ListenForService(int port) override; + bool IsInterfaceValid() const override { return true; } + std::unique_ptr ConnectToService( + absl::string_view ip_address, int port, + CancellationFlag *cancellation_flag) override; + std::unique_ptr ListenForService( + int port) override; - bool StartWifiHotspot(HotspotCredentials *hotspot_credentials) override; - bool StopWifiHotspot() override; + bool StartWifiHotspot(HotspotCredentials *hotspot_credentials) override; + bool StopWifiHotspot() override; - bool ConnectWifiHotspot(HotspotCredentials *hotspot_credentials) override; - bool DisconnectWifiHotspot() override; + bool ConnectWifiHotspot(HotspotCredentials *hotspot_credentials) override; + bool DisconnectWifiHotspot() override; - absl::optional> - GetDynamicPortRange() override { - return absl::nullopt; + absl::optional> GetDynamicPortRange() + override { + return absl::nullopt; } private: diff --git a/internal/platform/implementation/linux/wifi_hotspot_server_socket.h b/internal/platform/implementation/linux/wifi_hotspot_server_socket.h index 2097df01..77746040 100644 --- a/internal/platform/implementation/linux/wifi_hotspot_server_socket.h +++ b/internal/platform/implementation/linux/wifi_hotspot_server_socket.h @@ -27,12 +27,11 @@ class NetworkManagerWifiHotspotServerSocket public: NetworkManagerWifiHotspotServerSocket( int socket, sdbus::IConnection &system_bus, - const sdbus::ObjectPath &active_connection_path, + sdbus::ObjectPath active_connection_path, std::shared_ptr network_manager) : fd_(socket), system_bus_(system_bus), - active_connection_path_(active_connection_path), - network_manager_(network_manager) {} - ~NetworkManagerWifiHotspotServerSocket() {} + active_connection_path_(std::move(active_connection_path)), + network_manager_(std::move(network_manager)) {} std::string GetIPAddress() const override; int GetPort() const override; diff --git a/internal/platform/implementation/linux/wifi_hotspot_socket.h b/internal/platform/implementation/linux/wifi_hotspot_socket.h index 2a62668a..0d208cc1 100644 --- a/internal/platform/implementation/linux/wifi_hotspot_socket.h +++ b/internal/platform/implementation/linux/wifi_hotspot_socket.h @@ -22,10 +22,9 @@ namespace nearby { namespace linux { class WifiHotspotSocket : public api::WifiHotspotSocket { public: - WifiHotspotSocket(int connection_fd) + explicit WifiHotspotSocket(int connection_fd) : fd_(sdbus::UnixFd(connection_fd)), output_stream_(fd_), input_stream_(fd_) {} - ~WifiHotspotSocket() {} nearby::InputStream &GetInputStream() override { return input_stream_; }; nearby::OutputStream &GetOutputStream() override { return output_stream_; }; diff --git a/internal/platform/implementation/linux/wifi_lan.h b/internal/platform/implementation/linux/wifi_lan.h index dc52fb30..6cee1102 100644 --- a/internal/platform/implementation/linux/wifi_lan.h +++ b/internal/platform/implementation/linux/wifi_lan.h @@ -27,8 +27,7 @@ namespace nearby { namespace linux { class WifiLanMedium : public api::WifiLanMedium { public: - WifiLanMedium(sdbus::IConnection &system_bus); - ~WifiLanMedium() override = default; + explicit WifiLanMedium(sdbus::IConnection &system_bus); bool IsNetworkConnected() const override; diff --git a/internal/platform/implementation/linux/wifi_lan_server_socket.h b/internal/platform/implementation/linux/wifi_lan_server_socket.h index 27de3785..32ec7bca 100644 --- a/internal/platform/implementation/linux/wifi_lan_server_socket.h +++ b/internal/platform/implementation/linux/wifi_lan_server_socket.h @@ -28,12 +28,11 @@ namespace nearby { namespace linux { class WifiLanServerSocket : public api::WifiLanServerSocket { public: - WifiLanServerSocket(int socket, + explicit WifiLanServerSocket(int socket, std::shared_ptr network_manager, sdbus::IConnection &system_bus) - : fd_(sdbus::UnixFd(socket)), network_manager_(network_manager), + : fd_(sdbus::UnixFd(socket)), network_manager_(std::move(network_manager)), system_bus_(system_bus) {} - ~WifiLanServerSocket() override = default; std::string GetIPAddress() const override; int GetPort() const override; diff --git a/internal/platform/implementation/linux/wifi_lan_socket.h b/internal/platform/implementation/linux/wifi_lan_socket.h index f2ec43a0..276c8d2c 100644 --- a/internal/platform/implementation/linux/wifi_lan_socket.h +++ b/internal/platform/implementation/linux/wifi_lan_socket.h @@ -28,9 +28,8 @@ namespace nearby { namespace linux { class WifiLanSocket : public api::WifiLanSocket { public: - WifiLanSocket(sdbus::UnixFd fd) + explicit WifiLanSocket(sdbus::UnixFd fd) : fd_(fd), output_stream_(fd), input_stream_(fd) {} - ~WifiLanSocket() = default; nearby::InputStream &GetInputStream() override { return input_stream_; diff --git a/internal/platform/implementation/linux/wifi_medium.h b/internal/platform/implementation/linux/wifi_medium.h index 4b395310..065cb406 100644 --- a/internal/platform/implementation/linux/wifi_medium.h +++ b/internal/platform/implementation/linux/wifi_medium.h @@ -21,30 +21,35 @@ #include #include -#include #include #include #include #include +#include #include "absl/synchronization/mutex.h" #include "internal/platform/implementation/linux/dbus.h" #include "internal/platform/implementation/linux/generated/dbus/networkmanager/access_point_client.h" -#include "internal/platform/implementation/linux/generated/dbus/networkmanager/networkmanager_client.h" #include "internal/platform/implementation/linux/generated/dbus/networkmanager/connection_active_client.h" #include "internal/platform/implementation/linux/generated/dbus/networkmanager/device_wireless_client.h" #include "internal/platform/implementation/linux/generated/dbus/networkmanager/ip4config_client.h" +#include "internal/platform/implementation/linux/generated/dbus/networkmanager/networkmanager_client.h" #include "internal/platform/implementation/wifi.h" #include "internal/platform/logging.h" namespace nearby { namespace linux { -class NetworkManager +class NetworkManager final : public sdbus::ProxyInterfaces { -public: - NetworkManager(sdbus::IConnection &system_bus) + public: + NetworkManager(const NetworkManager &) = delete; + NetworkManager(NetworkManager &&) = delete; + NetworkManager &operator=(const NetworkManager &) = delete; + NetworkManager &operator=(NetworkManager &&) = delete; + explicit NetworkManager(sdbus::IConnection &system_bus) : ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager", - "/org/freedesktop/NetworkManager") { + "/org/freedesktop/NetworkManager"), + state_(0) { registerProxy(); try { state_ = State(); @@ -56,20 +61,24 @@ public: std::uint32_t getState() const { return state_; } -protected: + protected: void onCheckPermissions() override {} void onStateChanged(const uint32_t &state) override { state_ = state; } void onDeviceAdded(const sdbus::ObjectPath &device_path) override {} void onDeviceRemoved(const sdbus::ObjectPath &device_path) override {} -private: + private: std::atomic_uint32_t state_; }; class NetworkManagerIP4Config : public sdbus::ProxyInterfaces< org::freedesktop::NetworkManager::IP4Config_proxy> { -public: + public: + NetworkManagerIP4Config(const NetworkManagerIP4Config &) = delete; + NetworkManagerIP4Config(NetworkManagerIP4Config &&) = delete; + NetworkManagerIP4Config &operator=(const NetworkManagerIP4Config &) = delete; + NetworkManagerIP4Config &operator=(NetworkManagerIP4Config &&) = delete; NetworkManagerIP4Config(sdbus::IConnection &system_bus, const sdbus::ObjectPath &config_object_path) : ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager", @@ -82,11 +91,16 @@ public: class NetworkManagerAccessPoint : public sdbus::ProxyInterfaces< org::freedesktop::NetworkManager::AccessPoint_proxy> { -public: + public: + NetworkManagerAccessPoint(const NetworkManagerAccessPoint &) = delete; + NetworkManagerAccessPoint(NetworkManagerAccessPoint &&) = delete; + NetworkManagerAccessPoint &operator=(const NetworkManagerAccessPoint &) = + delete; + NetworkManagerAccessPoint &operator=(NetworkManagerAccessPoint &&) = delete; NetworkManagerAccessPoint(sdbus::IConnection &system_bus, - const sdbus::ObjectPath &access_point_object_path) + sdbus::ObjectPath access_point_object_path) : ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager", - access_point_object_path) { + std::move(access_point_object_path)) { registerProxy(); } ~NetworkManagerAccessPoint() { unregisterProxy(); } @@ -123,12 +137,20 @@ extern std::ostream &operator<<(std::ostream &s, class NetworkManagerActiveConnection : public sdbus::ProxyInterfaces< org::freedesktop::NetworkManager::Connection::Active_proxy> { -public: - NetworkManagerActiveConnection( - sdbus::IConnection &system_bus, - const sdbus::ObjectPath &active_connection_path) + public: + NetworkManagerActiveConnection(const NetworkManagerActiveConnection &) = + delete; + NetworkManagerActiveConnection(NetworkManagerActiveConnection &&) = delete; + NetworkManagerActiveConnection &operator=( + const NetworkManagerActiveConnection &) = delete; + NetworkManagerActiveConnection &operator=(NetworkManagerActiveConnection &&) = + delete; + explicit NetworkManagerActiveConnection( + sdbus::IConnection &system_bus, sdbus::ObjectPath active_connection_path) : ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager", - active_connection_path) { + std::move(active_connection_path)), + state_(kStateUnknown), + reason_(kStateReasonUnknown) { registerProxy(); try { auto state = State(); @@ -139,9 +161,9 @@ public: DBUS_LOG_PROPERTY_GET_ERROR(this, "State", e); } } - ~NetworkManagerActiveConnection() { unregisterProxy(); } + virtual ~NetworkManagerActiveConnection() { unregisterProxy(); } -protected: + protected: void onStateChanged(const uint32_t &state, const uint32_t &reason) override ABSL_LOCKS_EXCLUDED(state_mutex_) { absl::MutexLock l(&state_mutex_); @@ -153,9 +175,9 @@ protected: } } -public: - std::pair, bool> - WaitForConnection(absl::Duration timeout = absl::Seconds(10)) + public: + std::pair, bool> WaitForConnection( + absl::Duration timeout = absl::Seconds(10)) ABSL_LOCKS_EXCLUDED(state_mutex_) { NEARBY_LOGS(VERBOSE) << __func__ << ": Waiting for an update to " << getObjectPath() << "'s state"; @@ -208,36 +230,42 @@ public: return ip4addresses; } -private: + private: absl::Mutex state_mutex_; ActiveConnectionState state_ ABSL_GUARDED_BY(state_mutex_); ActiveConnectionStateReason reason_ ABSL_GUARDED_BY(state_mutex_); }; -class NetworkManagerObjectManager +class NetworkManagerObjectManager final : public sdbus::ProxyInterfaces { -public: - NetworkManagerObjectManager(sdbus::IConnection &system_bus) + public: + NetworkManagerObjectManager(const NetworkManagerObjectManager &) = delete; + NetworkManagerObjectManager(NetworkManagerObjectManager &&) = delete; + NetworkManagerObjectManager &operator=(const NetworkManagerObjectManager &) = + delete; + NetworkManagerObjectManager &operator=(NetworkManagerObjectManager &&) = + delete; + explicit NetworkManagerObjectManager(sdbus::IConnection &system_bus) : ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager", "/org/freedesktop") { registerProxy(); } ~NetworkManagerObjectManager() { unregisterProxy(); } - std::unique_ptr - GetIp4Config(const sdbus::ObjectPath &access_point); + std::unique_ptr GetIp4Config( + const sdbus::ObjectPath &access_point); std::unique_ptr GetActiveConnectionForAccessPoint(const sdbus::ObjectPath &access_point_path, const sdbus::ObjectPath &device_path); -protected: + protected: void onInterfacesAdded( const sdbus::ObjectPath &objectPath, const std::map> &interfacesAndProperties) override {} - void - onInterfacesRemoved(const sdbus::ObjectPath &objectPath, - const std::vector &interfaces) override {} + void onInterfacesRemoved( + const sdbus::ObjectPath &objectPath, + const std::vector &interfaces) override {} }; class NetworkManagerWifiMedium @@ -245,21 +273,26 @@ class NetworkManagerWifiMedium public sdbus::ProxyInterfaces< org::freedesktop::NetworkManager::Device::Wireless_proxy, sdbus::Properties_proxy> { -public: + public: + NetworkManagerWifiMedium(const NetworkManagerWifiMedium &) = delete; + NetworkManagerWifiMedium(NetworkManagerWifiMedium &&) = delete; + NetworkManagerWifiMedium &operator=(const 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", wireless_device_object_path), - network_manager_(std::move(network_manager)) { + network_manager_(std::move(network_manager)), + last_scan_(-1) { registerProxy(); } ~NetworkManagerWifiMedium() override { unregisterProxy(); } class ScanResultCallback : public api::WifiMedium::ScanResultCallback { - public: - ~ScanResultCallback() override = default; + public: void OnScanResults( const std::vector &scan_results) override { // TODO: Add implementation at some point @@ -272,21 +305,20 @@ public: bool Scan( const api::WifiMedium::ScanResultCallback &scan_result_callback) override; - std::shared_ptr - SearchBySSID(absl::string_view ssid, - absl::Duration scan_timeout = absl::Seconds(15)) + std::shared_ptr SearchBySSID( + absl::string_view ssid, absl::Duration scan_timeout = absl::Seconds(15)) ABSL_LOCKS_EXCLUDED(known_access_points_lock_); - api::WifiConnectionStatus - ConnectToNetwork(absl::string_view ssid, absl::string_view password, - api::WifiAuthType auth_type) override; + api::WifiConnectionStatus ConnectToNetwork( + absl::string_view ssid, absl::string_view password, + api::WifiAuthType auth_type) override; bool VerifyInternetConnectivity() override; std::string GetIpAddress() override; std::unique_ptr GetActiveConnection(); -protected: + protected: void onPropertiesChanged( const std::string &interfaceName, const std::map &changedProperties, @@ -306,9 +338,9 @@ protected: known_access_points_.erase(access_point); } -private: - std::shared_ptr - SearchBySSIDNoScan(std::vector &ssid) + private: + std::shared_ptr SearchBySSIDNoScan( + std::vector &ssid) ABSL_LOCKS_EXCLUDED(known_access_points_lock_); std::shared_ptr network_manager_; @@ -329,7 +361,7 @@ private: std::int64_t last_scan_ ABSL_GUARDED_BY(last_scan_lock_); }; -} // namespace linux -} // namespace nearby +} // namespace linux +} // namespace nearby #endif