From 41adc0d887fda5b41dce627ec933c12ff36d20ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Fri, 14 Oct 2016 18:12:22 +0200 Subject: [PATCH] continue and improve networkmanager api and functionality --- server/jsonrpc/cloudhandler.cpp | 14 ++ server/jsonrpc/cloudhandler.h | 1 - server/jsonrpc/jsontypes.cpp | 58 ++++-- server/jsonrpc/jsontypes.h | 9 +- server/jsonrpc/networkmanagerhandler.cpp | 174 ++++++++++++++---- server/jsonrpc/networkmanagerhandler.h | 28 ++- server/networkmanager/dbus-interfaces.h | 1 + server/networkmanager/networkdevice.cpp | 49 ++--- server/networkmanager/networkdevice.h | 6 +- server/networkmanager/networkmanager.cpp | 125 ++++++++++--- server/networkmanager/networkmanager.h | 27 ++- server/networkmanager/networksettings.cpp | 8 +- server/networkmanager/wirednetworkdevice.cpp | 95 ++++++++++ server/networkmanager/wirednetworkdevice.h | 61 ++++++ server/networkmanager/wirelessaccesspoint.cpp | 1 - ...kmanager.cpp => wirelessnetworkdevice.cpp} | 105 +++++++---- ...tworkmanager.h => wirelessnetworkdevice.h} | 28 +-- server/server.pri | 6 +- 18 files changed, 635 insertions(+), 161 deletions(-) create mode 100644 server/networkmanager/wirednetworkdevice.cpp create mode 100644 server/networkmanager/wirednetworkdevice.h rename server/networkmanager/{wirelessnetworkmanager.cpp => wirelessnetworkdevice.cpp} (56%) rename server/networkmanager/{wirelessnetworkmanager.h => wirelessnetworkdevice.h} (80%) diff --git a/server/jsonrpc/cloudhandler.cpp b/server/jsonrpc/cloudhandler.cpp index c41f5aa2..6b39188d 100644 --- a/server/jsonrpc/cloudhandler.cpp +++ b/server/jsonrpc/cloudhandler.cpp @@ -44,6 +44,13 @@ CloudHandler::CloudHandler(QObject *parent) : returns.insert("authenticated", JsonTypes::basicTypeToString(JsonTypes::Bool)); setReturns("GetConnectionStatus", returns); + params.clear(); returns.clear(); + setDescription("Enable", "Enable or disable the cloud connection."); + params.insert("enable", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setParams("Enable", params); + returns.insert("cloudError", JsonTypes::cloudErrorRef()); + setReturns("Enable", returns); + // Notification params.clear(); returns.clear(); setDescription("ConnectionStatusChanged", "Emitted whenever the status of the cloud connection changed."); @@ -91,6 +98,13 @@ JsonReply *CloudHandler::GetConnectionStatus(const QVariantMap ¶ms) const return createReply(returns); } +JsonReply *CloudHandler::Enable(const QVariantMap ¶ms) const +{ + bool enable = params.value("enable").toBool(); + GuhCore::instance()->configuration()->setCloudEnabled(enable); + return createReply(statusToReply(Cloud::CloudErrorNoError)); +} + void CloudHandler::onConnectionStatusChanged() { QVariantMap params; diff --git a/server/jsonrpc/cloudhandler.h b/server/jsonrpc/cloudhandler.h index f9cce8cf..733ef831 100644 --- a/server/jsonrpc/cloudhandler.h +++ b/server/jsonrpc/cloudhandler.h @@ -40,7 +40,6 @@ public: Q_INVOKABLE JsonReply *Authenticate(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *GetConnectionStatus(const QVariantMap ¶ms) const; Q_INVOKABLE JsonReply *Enable(const QVariantMap ¶ms) const; - Q_INVOKABLE JsonReply *Disable(const QVariantMap ¶ms) const; private: QList m_asyncAuthenticationReplies; diff --git a/server/jsonrpc/jsontypes.cpp b/server/jsonrpc/jsontypes.cpp index fb2f79af..e91e52fd 100644 --- a/server/jsonrpc/jsontypes.cpp +++ b/server/jsonrpc/jsontypes.cpp @@ -115,7 +115,8 @@ QVariantMap JsonTypes::s_calendarItem; QVariantMap JsonTypes::s_timeEventItem; QVariantMap JsonTypes::s_repeatingOption; QVariantMap JsonTypes::s_wirelessAccessPoint; -QVariantMap JsonTypes::s_networkDevice; +QVariantMap JsonTypes::s_wiredNetworkDevice; +QVariantMap JsonTypes::s_wirelessNetworkDevice; void JsonTypes::init() { @@ -334,9 +335,19 @@ void JsonTypes::init() s_wirelessAccessPoint.insert("frequency", basicTypeToString(QVariant::Double)); s_wirelessAccessPoint.insert("signalStrength", basicTypeToString(QVariant::Int)); - // NetworkDevice - s_networkDevice.insert("name", basicTypeToString(QVariant::String)); - s_networkDevice.insert("type", basicTypeToString(QVariant::String)); + // WiredNetworkDevice + s_wiredNetworkDevice.insert("interface", basicTypeToString(QVariant::String)); + s_wiredNetworkDevice.insert("macAddress", basicTypeToString(QVariant::String)); + s_wiredNetworkDevice.insert("state", networkDeviceStateRef()); + s_wiredNetworkDevice.insert("bitRate", basicTypeToString(QVariant::String)); + s_wiredNetworkDevice.insert("pluggedIn", basicTypeToString(QVariant::Bool)); + + // WirelessNetworkDevice + s_wirelessNetworkDevice.insert("interface", basicTypeToString(QVariant::String)); + s_wirelessNetworkDevice.insert("macAddress", basicTypeToString(QVariant::String)); + s_wirelessNetworkDevice.insert("state", networkDeviceStateRef()); + s_wirelessNetworkDevice.insert("bitRate", basicTypeToString(QVariant::String)); + s_wirelessNetworkDevice.insert("o:currentAccessPoint", wirelessAccessPointRef()); s_initialized = true; } @@ -412,7 +423,8 @@ QVariantMap JsonTypes::allTypes() allTypes.insert("TimeEventItem", timeEventItemDescription()); allTypes.insert("RepeatingOption", repeatingOptionDescription()); allTypes.insert("WirelessAccessPoint", wirelessAccessPointDescription()); - allTypes.insert("NetworkDevice", networkDeviceDescription()); + allTypes.insert("WiredNetworkDevice", wiredNetworkDeviceDescription()); + allTypes.insert("WirelessNetworkDevice", wirelessNetworkDeviceDescription()); return allTypes; } @@ -971,11 +983,29 @@ QVariantMap JsonTypes::packWirelessAccessPoint(WirelessAccessPoint *wirelessAcce return wirelessAccessPointVariant; } -QVariantMap JsonTypes::packNetworkDevice(NetworkDevice *networkDevice) +/*! Returns a variant map of the given \a networkDevice. */ +QVariantMap JsonTypes::packWiredNetworkDevice(WiredNetworkDevice *networkDevice) { QVariantMap networkDeviceVariant; - networkDeviceVariant.insert("name", networkDevice->interface()); - networkDeviceVariant.insert("type", NetworkDevice::deviceTypeToString(networkDevice->deviceType())); + networkDeviceVariant.insert("interface", networkDevice->interface()); + networkDeviceVariant.insert("macAddress", networkDevice->macAddress()); + networkDeviceVariant.insert("state", networkDevice->deviceStateString()); + networkDeviceVariant.insert("bitRate", QString("%1 [Mb/s]").arg(QString::number(networkDevice->bitRate()))); + networkDeviceVariant.insert("pluggedIn", networkDevice->pluggedIn()); + return networkDeviceVariant; +} + +/*! Returns a variant map of the given \a networkDevice. */ +QVariantMap JsonTypes::packWirelessNetworkDevice(WirelessNetworkDevice *networkDevice) +{ + QVariantMap networkDeviceVariant; + networkDeviceVariant.insert("interface", networkDevice->interface()); + networkDeviceVariant.insert("macAddress", networkDevice->macAddress()); + networkDeviceVariant.insert("state", networkDevice->deviceStateString()); + networkDeviceVariant.insert("bitRate", QString("%1 [Mb/s]").arg(QString::number(networkDevice->bitRate()))); + if (networkDevice->activeAccessPoint()) + networkDeviceVariant.insert("currentAccessPoint", JsonTypes::packWirelessAccessPoint(networkDevice->activeAccessPoint())); + return networkDeviceVariant; } @@ -1744,10 +1774,16 @@ QPair JsonTypes::validateVariant(const QVariant &templateVariant, qCWarning(dcJsonRpc) << "WirelessAccessPoint not matching"; return result; } - } else if (refName == networkDeviceRef()) { - QPair result = validateMap(networkDeviceDescription(), variant.toMap()); + } else if (refName == wiredNetworkDeviceRef()) { + QPair result = validateMap(wiredNetworkDeviceDescription(), variant.toMap()); if (!result.first) { - qCWarning(dcJsonRpc) << "NetworkDevice not matching"; + qCWarning(dcJsonRpc) << "WiredNetworkDevice not matching"; + return result; + } + } else if (refName == wirelessNetworkDeviceRef()) { + QPair result = validateMap(wirelessNetworkDeviceDescription(), variant.toMap()); + if (!result.first) { + qCWarning(dcJsonRpc) << "WirelessNetworkDevice not matching"; return result; } } else if (refName == basicTypeRef()) { diff --git a/server/jsonrpc/jsontypes.h b/server/jsonrpc/jsontypes.h index 4966d4cd..7b4c904b 100644 --- a/server/jsonrpc/jsontypes.h +++ b/server/jsonrpc/jsontypes.h @@ -48,7 +48,8 @@ #include "cloud/cloudconnection.h" #include "networkmanager/networkmanager.h" -#include "networkmanager/networkdevice.h" +#include "networkmanager/wirednetworkdevice.h" +#include "networkmanager/wirelessnetworkdevice.h" #include "networkmanager/wirelessaccesspoint.h" #include @@ -161,7 +162,8 @@ public: DECLARE_OBJECT(timeEventItem, "TimeEventItem") DECLARE_OBJECT(repeatingOption, "RepeatingOption") DECLARE_OBJECT(wirelessAccessPoint, "WirelessAccessPoint") - DECLARE_OBJECT(networkDevice, "NetworkDevice") + DECLARE_OBJECT(wiredNetworkDevice, "WiredNetworkDevice") + DECLARE_OBJECT(wirelessNetworkDevice, "WirelessNetworkDevice") // pack types static QVariantMap packEventType(const EventType &eventType); @@ -191,7 +193,8 @@ public: static QVariantMap packTimeEventItem(const TimeEventItem &timeEventItem); static QVariantMap packTimeDescriptor(const TimeDescriptor &timeDescriptor); static QVariantMap packWirelessAccessPoint(WirelessAccessPoint *wirelessAccessPoint); - static QVariantMap packNetworkDevice(NetworkDevice *networkDevice); + static QVariantMap packWiredNetworkDevice(WiredNetworkDevice *networkDevice); + static QVariantMap packWirelessNetworkDevice(WirelessNetworkDevice *networkDevice); // pack resources static QVariantList packRules(const QList rules); diff --git a/server/jsonrpc/networkmanagerhandler.cpp b/server/jsonrpc/networkmanagerhandler.cpp index 867e9070..b624e694 100644 --- a/server/jsonrpc/networkmanagerhandler.cpp +++ b/server/jsonrpc/networkmanagerhandler.cpp @@ -58,27 +58,38 @@ NetworkManagerHandler::NetworkManagerHandler(QObject *parent) : setReturns("EnableWirelessNetworking", returns); params.clear(); returns.clear(); - setDescription("GetWirelessAccessPoints", "Get the current list of wireless network access points."); + setDescription("GetWirelessAccessPoints", "Get the current list of wireless network access points for the given interface. The interface has to be a WirelessNetworkDevice."); + params.insert("interface", JsonTypes::basicTypeToString(QVariant::String)); setParams("GetWirelessAccessPoints", params); returns.insert("o:wirelessAccessPoints", QVariantList() << JsonTypes::wirelessAccessPointRef()); returns.insert("networkManagerError", JsonTypes::networkManagerErrorRef()); setReturns("GetWirelessAccessPoints", returns); + params.clear(); returns.clear(); + setDescription("DisconnectInterface", "Disconnect the given network interface. The interface will remain disconnected until the user connect it again."); + params.insert("interface", JsonTypes::basicTypeToString(QVariant::String)); + setParams("DisconnectInterface", params); + returns.insert("networkManagerError", JsonTypes::networkManagerErrorRef()); + setReturns("DisconnectInterface", returns); + params.clear(); returns.clear(); setDescription("GetNetworkDevices", "Get the list of current network devices."); setParams("GetNetworkDevices", params); - returns.insert("o:networkDevices", QVariantList() << JsonTypes::networkDeviceRef()); + returns.insert("wiredNetworkDevices", QVariantList() << JsonTypes::wiredNetworkDeviceRef()); + returns.insert("wirelessNetworkDevices", QVariantList() << JsonTypes::wirelessNetworkDeviceRef()); returns.insert("networkManagerError", JsonTypes::networkManagerErrorRef()); setReturns("GetNetworkDevices", returns); params.clear(); returns.clear(); setDescription("ScanWifiNetworks", "Start a wifi scan for searching new networks."); + params.insert("interface", JsonTypes::basicTypeToString(QVariant::String)); setParams("ScanWifiNetworks", params); returns.insert("networkManagerError", JsonTypes::networkManagerErrorRef()); setReturns("ScanWifiNetworks", returns); params.clear(); returns.clear(); setDescription("ConnectWifiNetwork", "Connect to the wifi network with the given ssid and password."); + params.insert("interface", JsonTypes::basicTypeToString(QVariant::String)); params.insert("ssid", JsonTypes::basicTypeToString(QVariant::String)); params.insert("o:password", JsonTypes::basicTypeToString(QVariant::String)); setParams("ConnectWifiNetwork", params); @@ -92,23 +103,47 @@ NetworkManagerHandler::NetworkManagerHandler(QObject *parent) : setParams("NetworkStatusChanged", params); params.clear(); returns.clear(); - setDescription("NetworkDeviceChanged", "Emitted whenever a NetworkDevice has changed."); - params.insert("networkDevice", JsonTypes::networkDeviceRef()); - setParams("NetworkDeviceChanged", params); + setDescription("WirelessNetworkDeviceAdded", "Emitted whenever a new WirelessNetworkDevice was added."); + params.insert("wirelessNetworkDevice", JsonTypes::wirelessNetworkDeviceRef()); + setParams("WirelessNetworkDeviceAdded", params); params.clear(); returns.clear(); - setDescription("NetworkDeviceAdded", "Emitted whenever a new NetworkDevice was added."); - params.insert("networkDevice", JsonTypes::networkDeviceRef()); - setParams("NetworkDeviceAdded", params); + setDescription("WirelessNetworkDeviceRemoved", "Emitted whenever a WirelessNetworkDevice was removed."); + params.insert("interface", JsonTypes::basicTypeToString(QVariant::String)); + setParams("WirelessNetworkDeviceRemoved", params); params.clear(); returns.clear(); - setDescription("NetworkDeviceRemoved", "Emitted whenever a NetworkDevice was removed."); - params.insert("networkDevice", JsonTypes::networkDeviceRef()); - setParams("NetworkDeviceRemoved", params); + setDescription("WirelessNetworkDeviceChanged", "Emitted whenever the given WirelessNetworkDevice has changed."); + params.insert("wirelessNetworkDevice", JsonTypes::wirelessNetworkDeviceRef()); + setParams("WirelessNetworkDeviceChanged", params); + + params.clear(); returns.clear(); + setDescription("WiredNetworkDeviceAdded", "Emitted whenever a new WiredNetworkDevice was added."); + params.insert("wiredNetworkDevice", JsonTypes::wiredNetworkDeviceRef()); + setParams("WiredNetworkDeviceAdded", params); + + params.clear(); returns.clear(); + setDescription("WiredNetworkDeviceRemoved", "Emitted whenever a WiredNetworkDevice was removed."); + params.insert("interface", JsonTypes::basicTypeToString(QVariant::String)); + setParams("WiredNetworkDeviceRemoved", params); + + params.clear(); returns.clear(); + setDescription("WiredNetworkDeviceChanged", "Emitted whenever the given WiredNetworkDevice has changed."); + params.insert("wiredNetworkDevice", JsonTypes::wiredNetworkDeviceRef()); + setParams("WiredNetworkDeviceChanged", params); connect(GuhCore::instance()->networkManager(), &NetworkManager::stateChanged, this, &NetworkManagerHandler::onNetworkManagerStatusChanged); connect(GuhCore::instance()->networkManager(), &NetworkManager::networkingEnabledChanged, this, &NetworkManagerHandler::onNetworkManagerStatusChanged); connect(GuhCore::instance()->networkManager(), &NetworkManager::wirelessEnabledChanged, this, &NetworkManagerHandler::onNetworkManagerStatusChanged); + + connect(GuhCore::instance()->networkManager(), &NetworkManager::wirelessDeviceAdded, this, &NetworkManagerHandler::onWirelessNetworkDeviceAdded); + connect(GuhCore::instance()->networkManager(), &NetworkManager::wirelessDeviceRemoved, this, &NetworkManagerHandler::onWirelessNetworkDeviceRemoved); + connect(GuhCore::instance()->networkManager(), &NetworkManager::wirelessDeviceChanged, this, &NetworkManagerHandler::onWirelessNetworkDeviceChanged); + + connect(GuhCore::instance()->networkManager(), &NetworkManager::wiredDeviceAdded, this, &NetworkManagerHandler::onWiredNetworkDeviceAdded); + connect(GuhCore::instance()->networkManager(), &NetworkManager::wiredDeviceRemoved, this, &NetworkManagerHandler::onWiredNetworkDeviceRemoved); + connect(GuhCore::instance()->networkManager(), &NetworkManager::wiredDeviceChanged, this, &NetworkManagerHandler::onWiredNetworkDeviceChanged); + } QString NetworkManagerHandler::name() const @@ -127,7 +162,7 @@ JsonReply *NetworkManagerHandler::GetNetworkStatus(const QVariantMap ¶ms) // Pack network manager status QVariantMap returns; returns.insert("status", packNetworkManagerStatus()); - returns.insert("networkManagerError", statusToReply(NetworkManager::NetworkManagerErrorNoError)); + returns.insert("networkManagerError", JsonTypes::networkManagerErrorToString(NetworkManager::NetworkManagerErrorNoError)); return createReply(returns); } @@ -160,8 +195,6 @@ JsonReply *NetworkManagerHandler::EnableWirelessNetworking(const QVariantMap &pa JsonReply *NetworkManagerHandler::GetWirelessAccessPoints(const QVariantMap ¶ms) { - Q_UNUSED(params); - if (!GuhCore::instance()->networkManager()->available()) return createReply(statusToReply(NetworkManager::NetworkManagerErrorNetworkManagerNotAvailable)); @@ -174,14 +207,26 @@ JsonReply *NetworkManagerHandler::GetWirelessAccessPoints(const QVariantMap &par if (!GuhCore::instance()->networkManager()->wirelessEnabled()) return createReply(statusToReply(NetworkManager::NetworkManagerErrorWirelessNetworkingDisabled)); - QVariantList wirelessAccessPoints; - foreach (WirelessAccessPoint *wirelessAccessPoint, GuhCore::instance()->networkManager()->wirelessNetworkManager()->accessPoints()) - wirelessAccessPoints.append(JsonTypes::packWirelessAccessPoint(wirelessAccessPoint)); + QString interface = params.value("interface").toString(); - QVariantMap returns; - returns.insert("wirelessAccessPoints", wirelessAccessPoints); - returns.insert("networkManagerError", statusToReply(NetworkManager::NetworkManagerErrorNoError)); - return createReply(returns); + if (!GuhCore::instance()->networkManager()->getNetworkDevice(interface)) + return createReply(statusToReply(NetworkManager::NetworkManagerErrorNetworkInterfaceNotFound)); + + foreach (WirelessNetworkDevice *networkDevice, GuhCore::instance()->networkManager()->wirelessNetworkDevices()) { + if (networkDevice->interface() == interface) { + QVariantList wirelessAccessPoints; + foreach (WirelessAccessPoint *wirelessAccessPoint, networkDevice->accessPoints()) + wirelessAccessPoints.append(JsonTypes::packWirelessAccessPoint(wirelessAccessPoint)); + + QVariantMap returns; + returns.insert("wirelessAccessPoints", wirelessAccessPoints); + returns.insert("networkManagerError", JsonTypes::networkManagerErrorToString(NetworkManager::NetworkManagerErrorNoError)); + return createReply(returns); + + } + } + + return createReply(statusToReply(NetworkManager::NetworkManagerErrorInvalidNetworkDeviceType)); } JsonReply *NetworkManagerHandler::GetNetworkDevices(const QVariantMap ¶ms) @@ -191,13 +236,18 @@ JsonReply *NetworkManagerHandler::GetNetworkDevices(const QVariantMap ¶ms) if (!GuhCore::instance()->networkManager()->available()) return createReply(statusToReply(NetworkManager::NetworkManagerErrorNetworkManagerNotAvailable)); - QVariantList networkDevices; - foreach (NetworkDevice *networkDevice, GuhCore::instance()->networkManager()->networkDevices()) - networkDevices.append(JsonTypes::packNetworkDevice(networkDevice)); + QVariantList wirelessNetworkDevices; + foreach (WirelessNetworkDevice *networkDevice, GuhCore::instance()->networkManager()->wirelessNetworkDevices()) + wirelessNetworkDevices.append(JsonTypes::packWirelessNetworkDevice(networkDevice)); + + QVariantList wiredNetworkDevices; + foreach (WiredNetworkDevice *networkDevice, GuhCore::instance()->networkManager()->wiredNetworkDevices()) + wiredNetworkDevices.append(JsonTypes::packWiredNetworkDevice(networkDevice)); QVariantMap returns; - returns.insert("networkDevices", networkDevices); - returns.insert("networkManagerError", statusToReply(NetworkManager::NetworkManagerErrorNoError)); + returns.insert("wirelessNetworkDevices", wirelessNetworkDevices); + returns.insert("wiredNetworkDevices", wiredNetworkDevices); + returns.insert("networkManagerError", JsonTypes::networkManagerErrorToString(NetworkManager::NetworkManagerErrorNoError)); return createReply(returns); } @@ -218,8 +268,19 @@ JsonReply *NetworkManagerHandler::ScanWifiNetworks(const QVariantMap ¶ms) return createReply(statusToReply(NetworkManager::NetworkManagerErrorWirelessNetworkingDisabled)); - GuhCore::instance()->networkManager()->wirelessNetworkManager()->scanWirelessNetworks(); - return createReply(statusToReply(NetworkManager::NetworkManagerErrorNoError)); + QString interface = params.value("interface").toString(); + + if (!GuhCore::instance()->networkManager()->getNetworkDevice(interface)) + return createReply(statusToReply(NetworkManager::NetworkManagerErrorNetworkInterfaceNotFound)); + + foreach (WirelessNetworkDevice *networkDevice, GuhCore::instance()->networkManager()->wirelessNetworkDevices()) { + if (networkDevice->interface() == interface) { + networkDevice->scanWirelessNetworks(); + return createReply(statusToReply(NetworkManager::NetworkManagerErrorNoError)); + } + } + + return createReply(statusToReply(NetworkManager::NetworkManagerErrorInvalidNetworkDeviceType)); } JsonReply *NetworkManagerHandler::ConnectWifiNetwork(const QVariantMap ¶ms) @@ -239,9 +300,23 @@ JsonReply *NetworkManagerHandler::ConnectWifiNetwork(const QVariantMap ¶ms) QString ssid = params.value("ssid").toString(); QString password = params.value("password").toString(); - NetworkManager::NetworkManagerError error = GuhCore::instance()->networkManager()->connectWifi(ssid, password); + QString interface = params.value("interface").toString(); - return createReply(statusToReply(error)); + return createReply(statusToReply(GuhCore::instance()->networkManager()->connectWifi(interface, ssid, password))); +} + +JsonReply *NetworkManagerHandler::DisconnectInterface(const QVariantMap ¶ms) +{ + if (!GuhCore::instance()->networkManager()->available()) + return createReply(statusToReply(NetworkManager::NetworkManagerErrorNetworkManagerNotAvailable)); + + QString interface = params.value("interface").toString(); + NetworkDevice *networkDevice = GuhCore::instance()->networkManager()->getNetworkDevice(interface); + if (!networkDevice) + return createReply(statusToReply(NetworkManager::NetworkManagerErrorNetworkInterfaceNotFound)); + + networkDevice->disconnectDevice(); + return createReply(statusToReply(NetworkManager::NetworkManagerErrorNoError)); } QVariantMap NetworkManagerHandler::packNetworkManagerStatus() @@ -258,19 +333,46 @@ void NetworkManagerHandler::onNetworkManagerStatusChanged() emit NetworkStatusChanged(packNetworkManagerStatus()); } -void NetworkManagerHandler::onNetworkDeviceChanged(NetworkDevice *networkDevice) +void NetworkManagerHandler::onWirelessNetworkDeviceAdded(WirelessNetworkDevice *networkDevice) { - Q_UNUSED(networkDevice) + QVariantMap notification; + notification.insert("wirelessNetworkDevice", JsonTypes::packWirelessNetworkDevice(networkDevice)); + emit WirelessNetworkDeviceAdded(notification); } -void NetworkManagerHandler::onNetworkDeviceAdded(NetworkDevice *networkDevice) +void NetworkManagerHandler::onWirelessNetworkDeviceRemoved(const QString &interface) { - Q_UNUSED(networkDevice) + QVariantMap notification; + notification.insert("interface", interface); + emit WirelessNetworkDeviceRemoved(notification); } -void NetworkManagerHandler::onNetworkDeviceRemoved(NetworkDevice *networkDevice) +void NetworkManagerHandler::onWirelessNetworkDeviceChanged(WirelessNetworkDevice *networkDevice) { - Q_UNUSED(networkDevice) + QVariantMap notification; + notification.insert("wirelessNetworkDevice", JsonTypes::packWirelessNetworkDevice(networkDevice)); + emit WirelessNetworkDeviceChanged(notification); +} + +void NetworkManagerHandler::onWiredNetworkDeviceAdded(WiredNetworkDevice *networkDevice) +{ + QVariantMap notification; + notification.insert("wiredNetworkDevice", JsonTypes::packWiredNetworkDevice(networkDevice)); + emit WiredNetworkDeviceAdded(notification); +} + +void NetworkManagerHandler::onWiredNetworkDeviceRemoved(const QString &interface) +{ + QVariantMap notification; + notification.insert("interface", interface); + emit WiredNetworkDeviceRemoved(notification); +} + +void NetworkManagerHandler::onWiredNetworkDeviceChanged(WiredNetworkDevice *networkDevice) +{ + QVariantMap notification; + notification.insert("wiredNetworkDevice", JsonTypes::packWiredNetworkDevice(networkDevice)); + emit WiredNetworkDeviceChanged(notification); } } diff --git a/server/jsonrpc/networkmanagerhandler.h b/server/jsonrpc/networkmanagerhandler.h index c0c8ca79..74b6dd66 100644 --- a/server/jsonrpc/networkmanagerhandler.h +++ b/server/jsonrpc/networkmanagerhandler.h @@ -42,21 +42,37 @@ public: Q_INVOKABLE JsonReply *GetNetworkDevices(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *ScanWifiNetworks(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *ConnectWifiNetwork(const QVariantMap ¶ms); + Q_INVOKABLE JsonReply *DisconnectInterface(const QVariantMap ¶ms); private: QVariantMap packNetworkManagerStatus(); signals: + // NetworkManager void NetworkStatusChanged(const QVariantMap ¶ms); - void NetworkDeviceChanged(const QVariantMap ¶ms); - void NetworkDeviceAdded(const QVariantMap ¶ms); - void NetworkDeviceRemoved(const QVariantMap ¶ms); + + // NetworkDevices + void WiredNetworkDeviceAdded(const QVariantMap ¶ms); + void WiredNetworkDeviceRemoved(const QVariantMap ¶ms); + void WiredNetworkDeviceChanged(const QVariantMap ¶ms); + + void WirelessNetworkDeviceAdded(const QVariantMap ¶ms); + void WirelessNetworkDeviceRemoved(const QVariantMap ¶ms); + void WirelessNetworkDeviceChanged(const QVariantMap ¶ms); private slots: + // NetworkManager void onNetworkManagerStatusChanged(); - void onNetworkDeviceChanged(NetworkDevice *networkDevice); - void onNetworkDeviceAdded(NetworkDevice *networkDevice); - void onNetworkDeviceRemoved(NetworkDevice *networkDevice); + + // NetworkDevices + void onWirelessNetworkDeviceAdded(WirelessNetworkDevice *networkDevice); + void onWirelessNetworkDeviceRemoved(const QString &interface); + void onWirelessNetworkDeviceChanged(WirelessNetworkDevice *networkDevice); + + void onWiredNetworkDeviceAdded(WiredNetworkDevice *networkDevice); + void onWiredNetworkDeviceRemoved(const QString &interface); + void onWiredNetworkDeviceChanged(WiredNetworkDevice *networkDevice); + }; } diff --git a/server/networkmanager/dbus-interfaces.h b/server/networkmanager/dbus-interfaces.h index dd39e024..40b046f5 100644 --- a/server/networkmanager/dbus-interfaces.h +++ b/server/networkmanager/dbus-interfaces.h @@ -32,6 +32,7 @@ static const QString settingsPathString("/org/freedesktop/NetworkManager/Setting static const QString deviceInterfaceString("org.freedesktop.NetworkManager.Device"); static const QString wirelessInterfaceString("org.freedesktop.NetworkManager.Device.Wireless"); +static const QString wiredInterfaceString("org.freedesktop.NetworkManager.Device.Wired"); static const QString accessPointInterfaceString("org.freedesktop.NetworkManager.AccessPoint"); static const QString settingsInterfaceString("org.freedesktop.NetworkManager.Settings"); static const QString connectionsInterfaceString("org.freedesktop.NetworkManager.Settings.Connection"); diff --git a/server/networkmanager/networkdevice.cpp b/server/networkmanager/networkdevice.cpp index 24f9306f..4102117c 100644 --- a/server/networkmanager/networkdevice.cpp +++ b/server/networkmanager/networkdevice.cpp @@ -39,30 +39,31 @@ NetworkDevice::NetworkDevice(const QDBusObjectPath &objectPath, QObject *parent) return; } - QDBusInterface networkDeviceInterface(serviceString, m_objectPath.path(), deviceInterfaceString, QDBusConnection::systemBus()); - if(!networkDeviceInterface.isValid()) { + m_networkDeviceInterface = new QDBusInterface(serviceString, m_objectPath.path(), deviceInterfaceString, QDBusConnection::systemBus(), this); + if(!m_networkDeviceInterface->isValid()) { qCWarning(dcNetworkManager()) << "NetworkDevice: Invalid DBus device interface" << m_objectPath.path(); return; } - m_udi = networkDeviceInterface.property("Udi").toString(); - m_interface = networkDeviceInterface.property("Interface").toString(); - m_ipInterface = networkDeviceInterface.property("IpInterface").toString(); - m_driver = networkDeviceInterface.property("Driver").toString(); - m_driverVersion = networkDeviceInterface.property("DriverVersion").toString(); - m_firmwareVersion = networkDeviceInterface.property("FirmwareVersion").toString(); - m_physicalPortId = networkDeviceInterface.property("PhysicalPortId").toString(); - m_mtu = networkDeviceInterface.property("Mtu").toUInt(); - m_metered = networkDeviceInterface.property("Metered").toUInt(); - m_autoconnect = networkDeviceInterface.property("Autoconnect").toBool(); + m_udi = m_networkDeviceInterface->property("Udi").toString(); + m_interface = m_networkDeviceInterface->property("Interface").toString(); + m_ipInterface = m_networkDeviceInterface->property("IpInterface").toString(); + m_driver = m_networkDeviceInterface->property("Driver").toString(); + m_driverVersion = m_networkDeviceInterface->property("DriverVersion").toString(); + m_firmwareVersion = m_networkDeviceInterface->property("FirmwareVersion").toString(); + m_physicalPortId = m_networkDeviceInterface->property("PhysicalPortId").toString(); + m_mtu = m_networkDeviceInterface->property("Mtu").toUInt(); + m_metered = m_networkDeviceInterface->property("Metered").toUInt(); + m_autoconnect = m_networkDeviceInterface->property("Autoconnect").toBool(); - m_deviceState = NetworkDeviceState(networkDeviceInterface.property("State").toUInt()); - m_deviceType = DeviceType(networkDeviceInterface.property("DeviceType").toUInt()); + m_deviceState = NetworkDeviceState(m_networkDeviceInterface->property("State").toUInt()); + m_deviceType = DeviceType(m_networkDeviceInterface->property("DeviceType").toUInt()); - m_activeConnection = qdbus_cast(networkDeviceInterface.property("ActiveConnection")); - m_ip4Config = qdbus_cast(networkDeviceInterface.property("Ip4Config")); - m_ip6Config = qdbus_cast(networkDeviceInterface.property("Ip6Config")); + m_activeConnection = qdbus_cast(m_networkDeviceInterface->property("ActiveConnection")); + m_ip4Config = qdbus_cast(m_networkDeviceInterface->property("Ip4Config")); + m_ip6Config = qdbus_cast(m_networkDeviceInterface->property("Ip6Config")); + QDBusConnection::systemBus().connect(serviceString, m_objectPath.path(), deviceInterfaceString, "StateChanged", this, SLOT(onStateChanged(uint,uint,uint))); } QDBusObjectPath NetworkDevice::objectPath() const @@ -155,6 +156,14 @@ QList NetworkDevice::availableConnections() const return m_availableConnections; } +void NetworkDevice::disconnectDevice() +{ + QDBusMessage query = m_networkDeviceInterface->call("Disconnect"); + if(query.type() != QDBusMessage::ReplyMessage) + qCWarning(dcNetworkManager()) << query.errorName() << query.errorMessage(); + +} + QString NetworkDevice::deviceTypeToString(const NetworkDevice::DeviceType &deviceType) { QMetaObject metaObject = NetworkDevice::staticMetaObject; @@ -182,11 +191,9 @@ QString NetworkDevice::deviceStateReasonToString(const NetworkDevice::NetworkDev void NetworkDevice::onStateChanged(uint newState, uint oldState, uint reason) { Q_UNUSED(oldState); - - qCDebug(dcNetworkManager()) << m_interface << deviceStateToString(NetworkDeviceState(newState)) << ":" << deviceStateReasonToString(NetworkDeviceStateReason(reason)); - + qCDebug(dcNetworkManager()) << m_interface << "--> State changed:" << deviceStateToString(NetworkDeviceState(newState)) << ":" << deviceStateReasonToString(NetworkDeviceStateReason(reason)); m_deviceState = NetworkDeviceState(newState); - emit deviceStateChanged(); + emit deviceChanged(); } QDebug operator<<(QDebug debug, NetworkDevice *device) diff --git a/server/networkmanager/networkdevice.h b/server/networkmanager/networkdevice.h index 35883fbd..7cbf77a7 100644 --- a/server/networkmanager/networkdevice.h +++ b/server/networkmanager/networkdevice.h @@ -169,11 +169,15 @@ public: QDBusObjectPath ip4Config() const; QList availableConnections() const; + // Method + void disconnectDevice(); + static QString deviceTypeToString(const DeviceType &deviceType); static QString deviceStateToString(const NetworkDeviceState &deviceState); static QString deviceStateReasonToString(const NetworkDeviceStateReason &deviceStateReason); private: + QDBusInterface *m_networkDeviceInterface; QDBusObjectPath m_objectPath; // Device properties @@ -201,7 +205,7 @@ private slots: void onStateChanged(uint newState, uint oldState, uint reason); signals: - void deviceStateChanged(); + void deviceChanged(); }; diff --git a/server/networkmanager/networkmanager.cpp b/server/networkmanager/networkmanager.cpp index 19951c70..00036817 100644 --- a/server/networkmanager/networkmanager.cpp +++ b/server/networkmanager/networkmanager.cpp @@ -30,7 +30,6 @@ namespace guhserver { NetworkManager::NetworkManager(QObject *parent) : QObject(parent), m_networkManagerInterface(0), - m_wirelessNetworkManager(0), m_available(false), m_wifiAvailable(false), m_state(NetworkManagerStateUnknown), @@ -70,7 +69,6 @@ NetworkManager::NetworkManager(QObject *parent) : // Create settings m_networkSettings = new NetworkSettings(this); - } bool NetworkManager::available() @@ -88,9 +86,23 @@ QList NetworkManager::networkDevices() const return m_networkDevices.values(); } -WirelessNetworkManager *NetworkManager::wirelessNetworkManager() const +QList NetworkManager::wirelessNetworkDevices() const { - return m_wirelessNetworkManager; + return m_wirelessNetworkDevices.values(); +} + +QList NetworkManager::wiredNetworkDevices() const +{ + return m_wiredNetworkDevices.values(); +} + +NetworkDevice *NetworkManager::getNetworkDevice(const QString &interface) +{ + foreach (NetworkDevice *device, m_networkDevices.values()) { + if (device->interface() == interface) + return device; + } + return Q_NULLPTR; } QString NetworkManager::version() const @@ -113,8 +125,27 @@ NetworkManager::NetworkManagerConnectivityState NetworkManager::connectivityStat return m_connectivityState; } -NetworkManager::NetworkManagerError NetworkManager::connectWifi(const QString &ssid, const QString &password) +NetworkManager::NetworkManagerError NetworkManager::connectWifi(const QString &interface, const QString &ssid, const QString &password) { + // Check interface + if (!getNetworkDevice(interface)) + return NetworkManagerErrorNetworkInterfaceNotFound; + + // Get wirelessNetworkDevice + WirelessNetworkDevice *wirelessNetworkDevice = 0; + foreach (WirelessNetworkDevice *networkDevice, wirelessNetworkDevices()) { + if (networkDevice->interface() == interface) + wirelessNetworkDevice = networkDevice; + } + + if (!wirelessNetworkDevice) + return NetworkManagerErrorInvalidNetworkDeviceType; + + // Get the access point object path + WirelessAccessPoint *accessPoint = wirelessNetworkDevice->getAccessPoint(ssid); + if (!accessPoint) + return NetworkManagerErrorAccessPointNotFound; + // https://developer.gnome.org/NetworkManager/stable/ref-settings.html QVariantMap connectionSettings; @@ -146,12 +177,7 @@ NetworkManager::NetworkManagerError NetworkManager::connectWifi(const QString &s settings.insert("ipv4", ipv4Settings); settings.insert("ipv6", ipv6Settings); - // Get the access point object path - WirelessAccessPoint *accessPoint = m_wirelessNetworkManager->getAccessPoint(ssid); - if (!accessPoint) { - qCWarning(dcNetworkManager()) << "Could not find access point with ssid:" << ssid; - return NetworkManagerErrorAccessPointNotFound; - } + // TODO: check if connection exists // Add connection QDBusObjectPath connectionObjectPath = m_networkSettings->addConnection(settings); @@ -159,9 +185,7 @@ NetworkManager::NetworkManagerError NetworkManager::connectWifi(const QString &s return NetworkManagerErrorWirelessConnectionFailed; // Activate connection - QDBusMessage query = m_networkManagerInterface->call("ActivateConnection", QVariant::fromValue(connectionObjectPath), - QVariant::fromValue(m_wirelessNetworkManager->objectPath()), - QVariant::fromValue(accessPoint->objectPath())); + QDBusMessage query = m_networkManagerInterface->call("ActivateConnection", QVariant::fromValue(connectionObjectPath), QVariant::fromValue(wirelessNetworkDevice->objectPath()), QVariant::fromValue(accessPoint->objectPath())); if(query.type() != QDBusMessage::ReplyMessage) { qCWarning(dcNetworkManager()) << query.errorName() << query.errorMessage(); return NetworkManagerErrorWirelessConnectionFailed; @@ -280,15 +304,42 @@ void NetworkManager::onDeviceAdded(const QDBusObjectPath &deviceObjectPath) return; } - NetworkDevice *networkDevice = new NetworkDevice(deviceObjectPath, this); - qCDebug(dcNetworkManager()) << "[+]" << networkDevice; - - if (!m_wirelessNetworkManager && networkDevice->deviceType() == NetworkDevice::DeviceTypeWifi) { - m_wifiAvailable = true; - m_wirelessNetworkManager = new WirelessNetworkManager(networkDevice->objectPath(), this); + // Get device Type + QDBusInterface networkDeviceInterface(serviceString, deviceObjectPath.path(), deviceInterfaceString, QDBusConnection::systemBus()); + if(!networkDeviceInterface.isValid()) { + qCWarning(dcNetworkManager()) << "NetworkDevice: Invalid DBus device interface" << deviceObjectPath.path(); + return; } - m_networkDevices.insert(deviceObjectPath, networkDevice); + // Create object + NetworkDevice::DeviceType deviceType = NetworkDevice::DeviceType(networkDeviceInterface.property("DeviceType").toUInt()); + switch (deviceType) { + case NetworkDevice::DeviceTypeWifi: { + WirelessNetworkDevice *wirelessNetworkDevice = new WirelessNetworkDevice(deviceObjectPath, this); + qCDebug(dcNetworkManager()) << "[+]" << wirelessNetworkDevice; + m_wifiAvailable = true; + m_networkDevices.insert(deviceObjectPath, wirelessNetworkDevice); + m_wirelessNetworkDevices.insert(deviceObjectPath, wirelessNetworkDevice); + connect(wirelessNetworkDevice, &WirelessNetworkDevice::deviceChanged, this, &NetworkManager::onWirelessDeviceChanged); + emit wirelessDeviceAdded(wirelessNetworkDevice); + break; + } + case NetworkDevice::DeviceTypeEthernet: { + WiredNetworkDevice *wiredNetworkDevice = new WiredNetworkDevice(deviceObjectPath, this); + qCDebug(dcNetworkManager()) << "[+]" << wiredNetworkDevice; + m_networkDevices.insert(deviceObjectPath, wiredNetworkDevice); + m_wiredNetworkDevices.insert(deviceObjectPath, wiredNetworkDevice); + + connect(wiredNetworkDevice, &WiredNetworkDevice::deviceChanged, this, &NetworkManager::onWiredDeviceChanged); + emit wiredDeviceAdded(wiredNetworkDevice); + break; + } + default: + NetworkDevice *networkDevice = new NetworkDevice(deviceObjectPath, this); + qCDebug(dcNetworkManager()) << "[+]" << networkDevice; + m_networkDevices.insert(deviceObjectPath, networkDevice); + break; + } } void NetworkManager::onDeviceRemoved(const QDBusObjectPath &deviceObjectPath) @@ -299,14 +350,28 @@ void NetworkManager::onDeviceRemoved(const QDBusObjectPath &deviceObjectPath) } NetworkDevice *networkDevice = m_networkDevices.take(deviceObjectPath); - qCDebug(dcNetworkManager()) << "[-]" << networkDevice; + + if (m_wiredNetworkDevices.contains(deviceObjectPath)) { + qCDebug(dcNetworkManager()) << "[-]" << m_wiredNetworkDevices.value(deviceObjectPath); + m_wiredNetworkDevices.remove(deviceObjectPath); + emit wiredDeviceRemoved(networkDevice->interface()); + } else if (m_wirelessNetworkDevices.contains(deviceObjectPath)) { + qCDebug(dcNetworkManager()) << "[-]" << m_wirelessNetworkDevices.value(deviceObjectPath); + m_wirelessNetworkDevices.remove(deviceObjectPath); + emit wirelessDeviceRemoved(networkDevice->interface()); + } else { + qCDebug(dcNetworkManager()) << "[-]" << networkDevice; + } + + // Check if wireless is still available + if (m_wirelessNetworkDevices.isEmpty()) + m_wifiAvailable = false; + networkDevice->deleteLater(); } void NetworkManager::onPropertiesChanged(const QVariantMap &properties) { - //qCDebug(dcNetworkManager()) << "Network manager properties changed" << properties; - if (properties.contains("Version")) setVersion(properties.value("Version").toString()); @@ -324,4 +389,16 @@ void NetworkManager::onPropertiesChanged(const QVariantMap &properties) } +void NetworkManager::onWirelessDeviceChanged() +{ + WirelessNetworkDevice *networkDevice = static_cast(sender()); + emit wirelessDeviceChanged(networkDevice); +} + +void NetworkManager::onWiredDeviceChanged() +{ + WiredNetworkDevice *networkDevice = static_cast(sender()); + emit wiredDeviceChanged(networkDevice); +} + } diff --git a/server/networkmanager/networkmanager.h b/server/networkmanager/networkmanager.h index 6790a8eb..bdd818e9 100644 --- a/server/networkmanager/networkmanager.h +++ b/server/networkmanager/networkmanager.h @@ -28,9 +28,9 @@ #include #include -#include "wirelessnetworkmanager.h" #include "dbus-interfaces.h" -#include "networkdevice.h" +#include "wirednetworkdevice.h" +#include "wirelessnetworkdevice.h" #include "networksettings.h" // Docs: https://developer.gnome.org/NetworkManager/unstable/spec.html @@ -69,6 +69,8 @@ public: NetworkManagerErrorUnknownError, NetworkManagerErrorWirelessNotAvailable, NetworkManagerErrorAccessPointNotFound, + NetworkManagerErrorNetworkInterfaceNotFound, + NetworkManagerErrorInvalidNetworkDeviceType, NetworkManagerErrorWirelessNetworkingDisabled, NetworkManagerErrorWirelessConnectionFailed, NetworkManagerErrorNetworkingDisabled, @@ -81,7 +83,10 @@ public: bool wifiAvailable(); QList networkDevices() const; - WirelessNetworkManager *wirelessNetworkManager() const; + QList wirelessNetworkDevices() const; + QList wiredNetworkDevices() const; + + NetworkDevice *getNetworkDevice(const QString &interface); // Properties QString version() const; @@ -89,7 +94,7 @@ public: QString stateString() const; NetworkManagerConnectivityState connectivityState() const; - NetworkManagerError connectWifi(const QString &ssid, const QString &password); + NetworkManagerError connectWifi(const QString &interface, const QString &ssid, const QString &password); // Networking bool networkingEnabled() const; @@ -101,10 +106,12 @@ public: private: QDBusInterface *m_networkManagerInterface; + QHash m_networkDevices; + QHash m_wirelessNetworkDevices; + QHash m_wiredNetworkDevices; NetworkSettings *m_networkSettings; - WirelessNetworkManager *m_wirelessNetworkManager; bool m_available; bool m_wifiAvailable; @@ -134,11 +141,21 @@ signals: void stateChanged(); void connectivityStateChanged(); + void wirelessDeviceAdded(WirelessNetworkDevice *wirelessDevice); + void wirelessDeviceRemoved(const QString &interface); + void wirelessDeviceChanged(WirelessNetworkDevice *wirelessDevice); + + void wiredDeviceAdded(WiredNetworkDevice *wirelessDevice); + void wiredDeviceRemoved(const QString &interface); + void wiredDeviceChanged(WiredNetworkDevice *wirelessDevice); + private slots: void onDeviceAdded(const QDBusObjectPath &deviceObjectPath); void onDeviceRemoved(const QDBusObjectPath &deviceObjectPath); void onPropertiesChanged(const QVariantMap &properties); + void onWirelessDeviceChanged(); + void onWiredDeviceChanged(); }; } diff --git a/server/networkmanager/networksettings.cpp b/server/networkmanager/networksettings.cpp index 6bbb2220..b605e666 100644 --- a/server/networkmanager/networksettings.cpp +++ b/server/networkmanager/networksettings.cpp @@ -79,17 +79,19 @@ void NetworkSettings::connectionAdded(const QDBusObjectPath &objectPath) NetworkConnection *connection = new NetworkConnection(objectPath, this); m_connections.insert(objectPath, connection); - qCDebug(dcNetworkManager()) << "Settings: [+]" << connection; + //qCDebug(dcNetworkManager()) << "Settings: [+]" << connection; } void NetworkSettings::connectionRemoved(const QDBusObjectPath &objectPath) { - qCDebug(dcNetworkManager()) << "Settings: [-]" << objectPath.path(); + Q_UNUSED(objectPath); + //qCDebug(dcNetworkManager()) << "Settings: [-]" << objectPath.path(); } void NetworkSettings::propertiesChanged(const QVariantMap &properties) { - qCDebug(dcNetworkManager()) << "Settins: properties changed" << properties; + Q_UNUSED(properties); + //qCDebug(dcNetworkManager()) << "Settins: properties changed" << properties; } } diff --git a/server/networkmanager/wirednetworkdevice.cpp b/server/networkmanager/wirednetworkdevice.cpp new file mode 100644 index 00000000..62ee2604 --- /dev/null +++ b/server/networkmanager/wirednetworkdevice.cpp @@ -0,0 +1,95 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2016 Simon Stürz * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "wirednetworkdevice.h" +#include "loggingcategories.h" + +namespace guhserver { + +WiredNetworkDevice::WiredNetworkDevice(const QDBusObjectPath &objectPath, QObject *parent) : + NetworkDevice(objectPath, parent) +{ + QDBusConnection systemBus = QDBusConnection::systemBus(); + if (!systemBus.isConnected()) { + qCWarning(dcNetworkManager()) << "WiredNetworkDevice: System DBus not connected"; + return; + } + + m_wiredInterface = new QDBusInterface(serviceString, this->objectPath().path(), wiredInterfaceString, systemBus, this); + if(!m_wiredInterface->isValid()) { + qCWarning(dcNetworkManager()) << "WiredNetworkDevice: Invalid wired dbus interface"; + return; + } + + setMacAddress(m_wiredInterface->property("HwAddress").toString()); + setBitRate(m_wiredInterface->property("Bitrate").toInt()); + setPluggedIn(m_wiredInterface->property("Carrier").toBool()); + + QDBusConnection::systemBus().connect(serviceString, this->objectPath().path(), wiredInterfaceString, "PropertiesChanged", this, SLOT(propertiesChanged(QVariantMap))); +} + +QString WiredNetworkDevice::macAddress() const +{ + return m_macAddress; +} + +int WiredNetworkDevice::bitRate() const +{ + return m_bitRate; +} + +bool WiredNetworkDevice::pluggedIn() const +{ + return m_pluggedIn; +} + +void WiredNetworkDevice::setMacAddress(const QString &macAddress) +{ + m_macAddress = macAddress; +} + +void WiredNetworkDevice::setBitRate(const int &bitRate) +{ + m_bitRate = bitRate; +} + +void WiredNetworkDevice::setPluggedIn(const bool &pluggedIn) +{ + m_pluggedIn = pluggedIn; +} + +void WiredNetworkDevice::propertiesChanged(const QVariantMap &properties) +{ + if (properties.contains("Carrier")) + setPluggedIn(properties.value("Carrier").toBool()); + +} + +QDebug operator<<(QDebug debug, WiredNetworkDevice *networkDevice) +{ + debug.nospace() << "WiredNetworkDevice(" << networkDevice->interface() << ", "; + debug.nospace() << networkDevice->macAddress() << ", "; + debug.nospace() << networkDevice->bitRate() << " [Mb/s], "; + debug.nospace() << networkDevice->pluggedIn() << ", "; + debug.nospace() << networkDevice->deviceStateString() << ") "; + return debug; +} + +} diff --git a/server/networkmanager/wirednetworkdevice.h b/server/networkmanager/wirednetworkdevice.h new file mode 100644 index 00000000..cd846c48 --- /dev/null +++ b/server/networkmanager/wirednetworkdevice.h @@ -0,0 +1,61 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2016 Simon Stürz * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef WIREDNETWORKDEVICE_H +#define WIREDNETWORKDEVICE_H + +#include +#include + +#include "networkdevice.h" + +namespace guhserver { + +class WiredNetworkDevice : public NetworkDevice +{ + Q_OBJECT +public: + explicit WiredNetworkDevice(const QDBusObjectPath &objectPath, QObject *parent = 0); + + QString macAddress() const; + int bitRate() const; + bool pluggedIn() const; + +private: + QDBusInterface *m_wiredInterface; + + QString m_macAddress; + int m_bitRate; + bool m_pluggedIn; + + void setMacAddress(const QString &macAddress); + void setBitRate(const int &bitRate); + void setPluggedIn(const bool &pluggedIn); + +private slots: + void propertiesChanged(const QVariantMap &properties); + +}; + +QDebug operator<<(QDebug debug, WiredNetworkDevice *networkDevice); + +} + +#endif // WIREDNETWORKDEVICE_H diff --git a/server/networkmanager/wirelessaccesspoint.cpp b/server/networkmanager/wirelessaccesspoint.cpp index 6b8be99a..7f2a95fc 100644 --- a/server/networkmanager/wirelessaccesspoint.cpp +++ b/server/networkmanager/wirelessaccesspoint.cpp @@ -102,7 +102,6 @@ void WirelessAccessPoint::setSecurityFlags(const WirelessAccessPoint::ApSecurity void WirelessAccessPoint::onPropertiesChanged(const QVariantMap &properties) { - //qCDebug(dcNetworkManager()) << "AccessPoint" << ssid() << ": Properties changed" << properties; if (properties.contains("Strength")) setSignalStrength(properties.value("Strength").toUInt()); diff --git a/server/networkmanager/wirelessnetworkmanager.cpp b/server/networkmanager/wirelessnetworkdevice.cpp similarity index 56% rename from server/networkmanager/wirelessnetworkmanager.cpp rename to server/networkmanager/wirelessnetworkdevice.cpp index da1839ae..7202f7a5 100644 --- a/server/networkmanager/wirelessnetworkmanager.cpp +++ b/server/networkmanager/wirelessnetworkdevice.cpp @@ -18,7 +18,7 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#include "wirelessnetworkmanager.h" +#include "wirelessnetworkdevice.h" #include "dbus-interfaces.h" #include "loggingcategories.h" @@ -28,18 +28,19 @@ namespace guhserver { -WirelessNetworkManager::WirelessNetworkManager(const QDBusObjectPath &objectPath, QObject *parent) : - NetworkDevice(objectPath, parent) +WirelessNetworkDevice::WirelessNetworkDevice(const QDBusObjectPath &objectPath, QObject *parent) : + NetworkDevice(objectPath, parent), + m_activeAccessPoint(Q_NULLPTR) { QDBusConnection systemBus = QDBusConnection::systemBus(); if (!systemBus.isConnected()) { - qCWarning(dcNetworkManager()) << "WirelessNetworkManager: System DBus not connected"; + qCWarning(dcNetworkManager()) << "WirelessNetworkDevice: System DBus not connected"; return; } m_wirelessInterface = new QDBusInterface(serviceString, this->objectPath().path(), wirelessInterfaceString, systemBus, this); - if(!m_wirelessInterface->isValid()) { - qCWarning(dcNetworkManager()) << "WirelessNetworkManager: Invalid wireless dbus interface"; + if (!m_wirelessInterface->isValid()) { + qCWarning(dcNetworkManager()) << "WirelessNetworkDevice: Invalid wireless dbus interface"; return; } @@ -47,39 +48,44 @@ WirelessNetworkManager::WirelessNetworkManager(const QDBusObjectPath &objectPath QDBusConnection::systemBus().connect(serviceString, this->objectPath().path(), wirelessInterfaceString, "AccessPointRemoved", this, SLOT(accessPointRemoved(QDBusObjectPath))); QDBusConnection::systemBus().connect(serviceString, this->objectPath().path(), wirelessInterfaceString, "PropertiesChanged", this, SLOT(propertiesChanged(QVariantMap))); - m_macAddress = m_wirelessInterface->property("HwAddress").toString(); - m_bitrate = m_wirelessInterface->property("Bitrate").toInt() / 1000; - - qCDebug(dcNetworkManager()) << this; - readAccessPoints(); + + setMacAddress(m_wirelessInterface->property("HwAddress").toString()); + setBitrate(m_wirelessInterface->property("Bitrate").toInt()); + setActiveAccessPoint(qdbus_cast(m_wirelessInterface->property("ActiveAccessPoint"))); } -QString WirelessNetworkManager::macAddress() const +QString WirelessNetworkDevice::macAddress() const { return m_macAddress; } -int WirelessNetworkManager::bitrate() const +int WirelessNetworkDevice::bitRate() const { - return m_bitrate; + return m_bitRate; } -void WirelessNetworkManager::scanWirelessNetworks() +WirelessAccessPoint *WirelessNetworkDevice::activeAccessPoint() { + return m_activeAccessPoint; +} + +void WirelessNetworkDevice::scanWirelessNetworks() +{ + qCDebug(dcNetworkManager()) << this << "Request scan"; QDBusMessage query = m_wirelessInterface->call("RequestScan", QVariantMap()); - if(query.type() != QDBusMessage::ReplyMessage) { + if (query.type() != QDBusMessage::ReplyMessage) { qCWarning(dcNetworkManager()) << "Scan error:" << query.errorName() << query.errorMessage(); return; } } -QList WirelessNetworkManager::accessPoints() +QList WirelessNetworkDevice::accessPoints() { return m_accessPointsTable.values(); } -WirelessAccessPoint *WirelessNetworkManager::getAccessPoint(const QString &ssid) +WirelessAccessPoint *WirelessNetworkDevice::getAccessPoint(const QString &ssid) { foreach (WirelessAccessPoint *accessPoint, m_accessPointsTable.values()) { if (accessPoint->ssid() == ssid) @@ -88,12 +94,12 @@ WirelessAccessPoint *WirelessNetworkManager::getAccessPoint(const QString &ssid) return Q_NULLPTR; } -WirelessAccessPoint *WirelessNetworkManager::getAccessPoint(const QDBusObjectPath &objectPath) +WirelessAccessPoint *WirelessNetworkDevice::getAccessPoint(const QDBusObjectPath &objectPath) { return m_accessPointsTable.value(objectPath); } -void WirelessNetworkManager::readAccessPoints() +void WirelessNetworkDevice::readAccessPoints() { QDBusMessage query = m_wirelessInterface->call("GetAccessPoints"); if(query.type() != QDBusMessage::ReplyMessage) { @@ -106,54 +112,81 @@ void WirelessNetworkManager::readAccessPoints() const QDBusArgument &argument = query.arguments().at(0).value(); argument.beginArray(); - while(!argument.atEnd()) { + while (!argument.atEnd()) { QDBusObjectPath accessPointObjectPath = qdbus_cast(argument); accessPointAdded(accessPointObjectPath); } argument.endArray(); } -void WirelessNetworkManager::accessPointAdded(const QDBusObjectPath &objectPath) +void WirelessNetworkDevice::setMacAddress(const QString &macAddress) +{ + m_macAddress = macAddress; +} + +void WirelessNetworkDevice::setBitrate(const int &bitRate) +{ + m_bitRate = bitRate / 1000; +} + +void WirelessNetworkDevice::setActiveAccessPoint(const QDBusObjectPath &activeAccessPointObjectPath) +{ + if (m_accessPointsTable.contains(activeAccessPointObjectPath)) { + m_activeAccessPoint = m_accessPointsTable.value(activeAccessPointObjectPath); + } else { + m_activeAccessPoint = Q_NULLPTR; + } +} + +void WirelessNetworkDevice::accessPointAdded(const QDBusObjectPath &objectPath) { QDBusInterface accessPointInterface(serviceString, objectPath.path(), accessPointInterfaceString, QDBusConnection::systemBus()); - if(!accessPointInterface.isValid()) { - qCWarning(dcNetworkManager()) << "WirelessNetworkManager: Invalid access point dbus interface"; + if (!accessPointInterface.isValid()) { + qCWarning(dcNetworkManager()) << "WirelessNetworkDevice: Invalid access point dbus interface"; return; } if (m_accessPointsTable.keys().contains(objectPath)) { - qCWarning(dcNetworkManager()) << "WirelessNetworkManager: Access point already added" << objectPath.path(); + qCWarning(dcNetworkManager()) << "WirelessNetworkDevice: Access point already added" << objectPath.path(); return; } WirelessAccessPoint *accessPoint = new WirelessAccessPoint(objectPath, this); - //qCDebug(dcNetworkManager()) << "WirelessNetworkManager: [+]" << accessPoint; - - // Add access point + //qCDebug(dcNetworkManager()) << "WirelessNetworkDevice: [+]" << accessPoint; m_accessPointsTable.insert(objectPath, accessPoint); } -void WirelessNetworkManager::accessPointRemoved(const QDBusObjectPath &objectPath) +void WirelessNetworkDevice::accessPointRemoved(const QDBusObjectPath &objectPath) { if (!m_accessPointsTable.keys().contains(objectPath)) return; - // Remove access point WirelessAccessPoint *accessPoint = m_accessPointsTable.take(objectPath); - //qCDebug(dcNetworkManager()) << "WirelessNetworkManager: [-]" << accessPoint; + if (accessPoint == m_activeAccessPoint) + m_activeAccessPoint = Q_NULLPTR; + + //qCDebug(dcNetworkManager()) << "WirelessNetworkDevice: [-]" << accessPoint; accessPoint->deleteLater(); } -void WirelessNetworkManager::propertiesChanged(const QVariantMap &properties) +void WirelessNetworkDevice::propertiesChanged(const QVariantMap &properties) { - qCDebug(dcNetworkManager()) << "WirelessNetworkManager: Property changed" << properties; + //qCDebug(dcNetworkManager()) << "WirelessNetworkDevice: Property changed" << properties; + + if (properties.contains("Bitrate")) + setBitrate(properties.value("Bitrate").toInt()); + + if (properties.contains("ActiveAccessPoint")) + setActiveAccessPoint(qdbus_cast(properties.value("ActiveAccessPoint"))); + + emit deviceChanged(); } -QDebug operator<<(QDebug debug, WirelessNetworkManager *manager) +QDebug operator<<(QDebug debug, WirelessNetworkDevice *manager) { - debug.nospace() << "WirelessDevice(" << manager->interface() << ", "; + debug.nospace() << "WirelessNetworkDevice(" << manager->interface() << ", "; debug.nospace() << manager->macAddress() << ", "; - debug.nospace() << manager->bitrate() << " [Mb/s], "; + debug.nospace() << manager->bitRate() << " [Mb/s], "; debug.nospace() << manager->deviceStateString() << ") "; return debug; } diff --git a/server/networkmanager/wirelessnetworkmanager.h b/server/networkmanager/wirelessnetworkdevice.h similarity index 80% rename from server/networkmanager/wirelessnetworkmanager.h rename to server/networkmanager/wirelessnetworkdevice.h index 8380209d..0190f55e 100644 --- a/server/networkmanager/wirelessnetworkmanager.h +++ b/server/networkmanager/wirelessnetworkdevice.h @@ -34,34 +34,40 @@ namespace guhserver { -class WirelessNetworkManager : public NetworkDevice +class WirelessNetworkDevice : public NetworkDevice { Q_OBJECT public: - explicit WirelessNetworkManager(const QDBusObjectPath &objectPath, QObject *parent = 0); + explicit WirelessNetworkDevice(const QDBusObjectPath &objectPath, QObject *parent = 0); + // Properties QString macAddress() const; - int bitrate() const; - - void scanWirelessNetworks(); + int bitRate() const; + WirelessAccessPoint *activeAccessPoint(); + // Accesspoints QList accessPoints(); WirelessAccessPoint *getAccessPoint(const QString &ssid); WirelessAccessPoint *getAccessPoint(const QDBusObjectPath &objectPath); + // Methods + void scanWirelessNetworks(); + private: QDBusInterface *m_wirelessInterface; + QString m_macAddress; - int m_bitrate; + int m_bitRate; + WirelessAccessPoint *m_activeAccessPoint; QHash m_accessPointsTable; void readAccessPoints(); - void setConnected(const bool &connected); - void setState(const NetworkDeviceState &state); - void setStateReason(const NetworkDeviceStateReason &stateReason); + void setMacAddress(const QString &macAddress); + void setBitrate(const int &bitRate); + void setActiveAccessPoint(const QDBusObjectPath &activeAccessPointObjectPath); private slots: void accessPointAdded(const QDBusObjectPath &objectPath); @@ -69,11 +75,11 @@ private slots: void propertiesChanged(const QVariantMap &properties); signals: - void connectedChanged(const bool &connected); + void bitRateChanged(const bool &connected); void stateChanged(const NetworkDeviceState &state); }; -QDebug operator<<(QDebug debug, WirelessNetworkManager *manager); +QDebug operator<<(QDebug debug, WirelessNetworkDevice *manager); } diff --git a/server/server.pri b/server/server.pri index 28d62ecb..92c07748 100644 --- a/server/server.pri +++ b/server/server.pri @@ -62,9 +62,10 @@ HEADERS += $$top_srcdir/server/guhcore.h \ $$top_srcdir/server/networkmanager/networkmanager.h \ $$top_srcdir/server/networkmanager/networkdevice.h \ $$top_srcdir/server/networkmanager/wirelessaccesspoint.h \ - $$top_srcdir/server/networkmanager/wirelessnetworkmanager.h \ + $$top_srcdir/server/networkmanager/wirelessnetworkdevice.h \ $$top_srcdir/server/networkmanager/networksettings.h \ $$top_srcdir/server/networkmanager/networkconnection.h \ + $$top_srcdir/server/networkmanager/wirednetworkdevice.h \ SOURCES += $$top_srcdir/server/guhcore.cpp \ @@ -125,7 +126,8 @@ SOURCES += $$top_srcdir/server/guhcore.cpp \ $$top_srcdir/server/networkmanager/networkmanager.cpp \ $$top_srcdir/server/networkmanager/networkdevice.cpp \ $$top_srcdir/server/networkmanager/wirelessaccesspoint.cpp \ - $$top_srcdir/server/networkmanager/wirelessnetworkmanager.cpp \ + $$top_srcdir/server/networkmanager/wirelessnetworkdevice.cpp \ $$top_srcdir/server/networkmanager/networksettings.cpp \ $$top_srcdir/server/networkmanager/networkconnection.cpp \ + $$top_srcdir/server/networkmanager/wirednetworkdevice.cpp \