#pragma once #include "DBusEvent.hpp" #include "DBusLog.hpp" #include "DBusDispatcher.hpp" #include "dbus/hpp/org.freedesktop.NetworkManager.Device.WifiP2P.hpp" #include "dbus/hpp/org.freedesktop.NetworkManager.Device.hpp" #include "dbus/hpp/org.freedesktop.NetworkManager.WifiP2PPeer.hpp" #include "dbus/hpp/org.freedesktop.NetworkManager.hpp" namespace 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",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",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",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 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); } };