#pragma once #include "DBusEvent.hpp" #include "DBusLog.hpp" #include "DBusDispatcher.hpp" #include "dbus/org.freedesktop.NetworkManager.Device.WifiP2P.hpp" #include "dbus/org.freedesktop.NetworkManager.Device.hpp" #include "dbus/org.freedesktop.NetworkManager.Device.Wireless.hpp" #include "dbus/org.freedesktop.NetworkManager.Settings.Connection.hpp" #include "dbus/org.freedesktop.NetworkManager.WifiP2PPeer.hpp" #include "dbus/org.freedesktop.NetworkManager.AccessPoint.hpp" #include "dbus/org.freedesktop.NetworkManager.Connection.Active.hpp" #include "dbus/org.freedesktop.NetworkManager.hpp" #include "libnm/nm-dbus-interface.h" namespace dbus::networkmanager { namespace nm_impl { extern std::string NetworkManagerStateToString(uint32_t state); std::string NetworkManagerDeviceStateToString(uint32_t state); }; class NetworkManager : public sdbus::ProxyInterfaces { public: using ptr = std::unique_ptr; NetworkManager(DBusDispatcher &dispatcher) : sdbus::ProxyInterfaces( dispatcher.Connection(), INTERFACE_NAME, "/org/freedesktop/NetworkManager") { registerProxy(); } virtual ~NetworkManager() { unregisterProxy(); } static ptr Create(DBusDispatcher &dispatcher) { return std::make_unique(dispatcher); } DBusEvent<> OnCheckPermissions; DBusEvent OnStateChanged; DBusEvent OnDeviceAdded; DBusEvent OnDeviceRemoved; private: void EventTrace(const char *method, const std::string &message) { LogTrace(getObjectPath(), method, message); } virtual void onStateChanged(const uint32_t &state) { EventTrace("onStateChanged", nm_impl::NetworkManagerStateToString(state)); OnStateChanged.fire(state); } virtual void onCheckPermissions() { OnCheckPermissions.fire(); } virtual void onDeviceAdded(const sdbus::ObjectPath &device_path) { EventTrace("onDeviceAdded", device_path.c_str()); OnDeviceAdded.fire(device_path); } virtual void onDeviceRemoved(const sdbus::ObjectPath &device_path) { EventTrace("onDeviceRemoved", device_path.c_str()); OnDeviceRemoved.fire(device_path); } }; class Device : public sdbus::ProxyInterfaces { public: using ptr = std::unique_ptr; Device(DBusDispatcher &dispatcher, const sdbus::ObjectPath &path) : sdbus::ProxyInterfaces( dispatcher.Connection(), "org.freedesktop.NetworkManager", path) { registerProxy(); } virtual ~Device() { unregisterProxy(); } static ptr Create(DBusDispatcher &dispatcher, const sdbus::ObjectPath &path) { return std::make_unique(dispatcher, path); } DBusEvent OnStateChanged; private: const uint32_t NM_DEVICE_TYPE_WIFI_P2P = 30; // from lbnm-dev package. public: bool IsP2pDevice() { return this->DeviceType() == NM_DEVICE_TYPE_WIFI_P2P; } private: void EventTrace(const char *method, const std::string &message) { LogTrace(getObjectPath(), method, message); } virtual void onStateChanged(const uint32_t &new_state, const uint32_t &old_state, const uint32_t &reason) { EventTrace("onStateChanged", nm_impl::NetworkManagerDeviceStateToString(new_state)); OnStateChanged.fire(new_state, old_state, reason); } }; class WifiP2P : public sdbus::ProxyInterfaces< org::freedesktop::NetworkManager::Device::WifiP2P_proxy> { public: using ptr = std::unique_ptr; using base = sdbus::ProxyInterfaces< org::freedesktop::NetworkManager::Device::WifiP2P_proxy>; WifiP2P(DBusDispatcher &dispatcher, const std::string &objectPath) : base(dispatcher.Connection(), "org.freedesktop.NetworkManager", objectPath) { registerProxy(); } virtual ~WifiP2P() { unregisterProxy(); } static ptr Create(DBusDispatcher &dispatcher, const std::string &objectPath) { return std::make_unique(dispatcher, objectPath); } DBusEvent OnPeerAdded; DBusEvent OnPeerRemoved; DBusEvent OnStateChanged; private: void EventTrace(const char *method, const std::string &message) { LogTrace(getObjectPath(), method, message); } virtual void onStateChanged(const uint32_t &new_state, const uint32_t &old_state, const uint32_t &reason) { EventTrace("onStateChanged", nm_impl::NetworkManagerStateToString(new_state)); OnStateChanged.fire(new_state); } virtual void onPeerAdded(const sdbus::ObjectPath &peer) { EventTrace("onPeerAdded", peer); OnPeerAdded.fire(peer); } virtual void onPeerRemoved(const sdbus::ObjectPath &peer) { EventTrace("onPeerRemoved", peer); OnPeerRemoved.fire(peer); } }; class Connection : public sdbus::ProxyInterfaces { public: using self = Connection; using ptr = std::unique_ptr; Connection(DBusDispatcher &dispatcher, const sdbus::ObjectPath &objectPath) : sdbus::ProxyInterfaces( dispatcher.Connection(), "org.freedesktop.NetworkManager", objectPath) { registerProxy(); } static ptr Create(DBusDispatcher &dispatcher, const sdbus::ObjectPath &objectPath) { return std::make_unique(dispatcher, objectPath); } virtual ~Connection() { unregisterProxy(); } DBusEvent<> OnUpdated; DBusEvent<> OnRemoved; private: virtual void onUpdated() override { OnUpdated.fire(); } virtual void onRemoved() override { OnRemoved.fire(); } }; class WifiP2PPeer : public sdbus::ProxyInterfaces { public: using self = WifiP2PPeer; using ptr = std::unique_ptr; WifiP2PPeer(DBusDispatcher &dispatcher, const sdbus::ObjectPath &objectPath) : sdbus::ProxyInterfaces( dispatcher.Connection(), "org.freedesktop.NetworkManager", objectPath) { registerProxy(); } virtual ~WifiP2PPeer() { unregisterProxy(); } static ptr Create(DBusDispatcher &dispatcher, const sdbus::ObjectPath &objectPath) { return std::make_unique(dispatcher, objectPath); } DBusEvent OnPeerAdded; DBusEvent OnPeerRemoved; private: virtual void onPeerAdded(const sdbus::ObjectPath &peer) { OnPeerAdded.fire(peer); } virtual void onPeerRemoved(const sdbus::ObjectPath &peer) { OnPeerRemoved.fire(peer); } }; class DeviceWireless : public sdbus::ProxyInterfaces { public: using self = DeviceWireless; using ptr = std::unique_ptr; using proxy_t = org::freedesktop::NetworkManager::Device::Wireless_proxy; DeviceWireless(DBusDispatcher &dispatcher, const sdbus::ObjectPath &path) : sdbus::ProxyInterfaces( dispatcher.Connection(), "org.freedesktop.NetworkManager", path) { registerProxy(); } virtual ~DeviceWireless() { unregisterProxy(); } static ptr Create(DBusDispatcher &dispatcher, const sdbus::ObjectPath &path) { return std::make_unique(dispatcher, path); } DBusEvent OnAccessPointAdded; DBusEvent OnAccessPointRemoved; protected: void EventTrace(const char *method, const std::string &message) { LogTrace(getObjectPath(), method, message); } virtual void onAccessPointAdded(const sdbus::ObjectPath& access_point) override { EventTrace("onAccessPointAdded", access_point.c_str()); OnAccessPointAdded.fire(access_point); } virtual void onAccessPointRemoved(const sdbus::ObjectPath& access_point) { EventTrace("onAccessPointRemoved",access_point.c_str()); OnAccessPointRemoved.fire(access_point); } }; class AccessPoint : public sdbus::ProxyInterfaces { public: using self = AccessPoint; using ptr = std::unique_ptr; using proxy_t = org::freedesktop::NetworkManager::AccessPoint_proxy; AccessPoint(DBusDispatcher &dispatcher, const sdbus::ObjectPath &path) : sdbus::ProxyInterfaces( dispatcher.Connection(), "org.freedesktop.NetworkManager", path) { registerProxy(); } virtual ~AccessPoint() { unregisterProxy(); } static ptr Create(DBusDispatcher &dispatcher, const sdbus::ObjectPath &path) { return std::make_unique(dispatcher, path); } protected: void EventTrace(const char *method, const std::string &message) { LogTrace(getObjectPath(), method, message); } }; class ActiveConnection : public sdbus::ProxyInterfaces { public: using self = ActiveConnection; using ptr = std::unique_ptr; using proxy_t = org::freedesktop::NetworkManager::Connection::Active_proxy; ActiveConnection(DBusDispatcher &dispatcher, const sdbus::ObjectPath &path) : sdbus::ProxyInterfaces( dispatcher.Connection(), "org.freedesktop.NetworkManager", path) { registerProxy(); } virtual ~ActiveConnection() { unregisterProxy(); } static ptr Create(DBusDispatcher &dispatcher, const sdbus::ObjectPath &path) { return std::make_unique(dispatcher, path); } DBusEvent OnStateChanged; protected: void EventTrace(const char *method, const std::string &message) { LogTrace(getObjectPath(), method, message); } virtual void onStateChanged(const uint32_t& state, const uint32_t& reason) { OnStateChanged.fire(state,reason); } }; } // namespace