continue and improve networkmanager api and functionality

This commit is contained in:
Simon Stürz 2016-10-14 18:12:22 +02:00 committed by Michael Zanetti
parent 8045b5a0e7
commit 41adc0d887
18 changed files with 635 additions and 161 deletions

View File

@ -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 &params) const
return createReply(returns);
}
JsonReply *CloudHandler::Enable(const QVariantMap &params) const
{
bool enable = params.value("enable").toBool();
GuhCore::instance()->configuration()->setCloudEnabled(enable);
return createReply(statusToReply(Cloud::CloudErrorNoError));
}
void CloudHandler::onConnectionStatusChanged()
{
QVariantMap params;

View File

@ -40,7 +40,6 @@ public:
Q_INVOKABLE JsonReply *Authenticate(const QVariantMap &params);
Q_INVOKABLE JsonReply *GetConnectionStatus(const QVariantMap &params) const;
Q_INVOKABLE JsonReply *Enable(const QVariantMap &params) const;
Q_INVOKABLE JsonReply *Disable(const QVariantMap &params) const;
private:
QList<JsonReply *> m_asyncAuthenticationReplies;

View File

@ -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<bool, QString> JsonTypes::validateVariant(const QVariant &templateVariant,
qCWarning(dcJsonRpc) << "WirelessAccessPoint not matching";
return result;
}
} else if (refName == networkDeviceRef()) {
QPair<bool, QString> result = validateMap(networkDeviceDescription(), variant.toMap());
} else if (refName == wiredNetworkDeviceRef()) {
QPair<bool, QString> 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<bool, QString> result = validateMap(wirelessNetworkDeviceDescription(), variant.toMap());
if (!result.first) {
qCWarning(dcJsonRpc) << "WirelessNetworkDevice not matching";
return result;
}
} else if (refName == basicTypeRef()) {

View File

@ -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 <QObject>
@ -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<Rule> rules);

View File

@ -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 &params)
// 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 &params)
{
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 &params)
@ -191,13 +236,18 @@ JsonReply *NetworkManagerHandler::GetNetworkDevices(const QVariantMap &params)
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 &params)
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 &params)
@ -239,9 +300,23 @@ JsonReply *NetworkManagerHandler::ConnectWifiNetwork(const QVariantMap &params)
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 &params)
{
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);
}
}

View File

@ -42,21 +42,37 @@ public:
Q_INVOKABLE JsonReply *GetNetworkDevices(const QVariantMap &params);
Q_INVOKABLE JsonReply *ScanWifiNetworks(const QVariantMap &params);
Q_INVOKABLE JsonReply *ConnectWifiNetwork(const QVariantMap &params);
Q_INVOKABLE JsonReply *DisconnectInterface(const QVariantMap &params);
private:
QVariantMap packNetworkManagerStatus();
signals:
// NetworkManager
void NetworkStatusChanged(const QVariantMap &params);
void NetworkDeviceChanged(const QVariantMap &params);
void NetworkDeviceAdded(const QVariantMap &params);
void NetworkDeviceRemoved(const QVariantMap &params);
// NetworkDevices
void WiredNetworkDeviceAdded(const QVariantMap &params);
void WiredNetworkDeviceRemoved(const QVariantMap &params);
void WiredNetworkDeviceChanged(const QVariantMap &params);
void WirelessNetworkDeviceAdded(const QVariantMap &params);
void WirelessNetworkDeviceRemoved(const QVariantMap &params);
void WirelessNetworkDeviceChanged(const QVariantMap &params);
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);
};
}

View File

@ -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");

View File

@ -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<QDBusObjectPath>(networkDeviceInterface.property("ActiveConnection"));
m_ip4Config = qdbus_cast<QDBusObjectPath>(networkDeviceInterface.property("Ip4Config"));
m_ip6Config = qdbus_cast<QDBusObjectPath>(networkDeviceInterface.property("Ip6Config"));
m_activeConnection = qdbus_cast<QDBusObjectPath>(m_networkDeviceInterface->property("ActiveConnection"));
m_ip4Config = qdbus_cast<QDBusObjectPath>(m_networkDeviceInterface->property("Ip4Config"));
m_ip6Config = qdbus_cast<QDBusObjectPath>(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<QDBusObjectPath> 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)

View File

@ -169,11 +169,15 @@ public:
QDBusObjectPath ip4Config() const;
QList<QDBusObjectPath> 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();
};

View File

@ -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<NetworkDevice *> NetworkManager::networkDevices() const
return m_networkDevices.values();
}
WirelessNetworkManager *NetworkManager::wirelessNetworkManager() const
QList<WirelessNetworkDevice *> NetworkManager::wirelessNetworkDevices() const
{
return m_wirelessNetworkManager;
return m_wirelessNetworkDevices.values();
}
QList<WiredNetworkDevice *> 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<WirelessNetworkDevice *>(sender());
emit wirelessDeviceChanged(networkDevice);
}
void NetworkManager::onWiredDeviceChanged()
{
WiredNetworkDevice *networkDevice = static_cast<WiredNetworkDevice *>(sender());
emit wiredDeviceChanged(networkDevice);
}
}

View File

@ -28,9 +28,9 @@
#include <QDBusContext>
#include <QDBusArgument>
#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<NetworkDevice *> networkDevices() const;
WirelessNetworkManager *wirelessNetworkManager() const;
QList<WirelessNetworkDevice *> wirelessNetworkDevices() const;
QList<WiredNetworkDevice *> 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<QDBusObjectPath, NetworkDevice *> m_networkDevices;
QHash<QDBusObjectPath, WirelessNetworkDevice *> m_wirelessNetworkDevices;
QHash<QDBusObjectPath, WiredNetworkDevice *> 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();
};
}

View File

@ -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;
}
}

View File

@ -0,0 +1,95 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2016 Simon Stürz <simon.stuerz@guh.io> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#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;
}
}

View File

@ -0,0 +1,61 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2016 Simon Stürz <simon.stuerz@guh.io> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef WIREDNETWORKDEVICE_H
#define WIREDNETWORKDEVICE_H
#include <QObject>
#include <QDBusObjectPath>
#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

View File

@ -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());

View File

@ -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<QDBusObjectPath>(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<WirelessAccessPoint *> WirelessNetworkManager::accessPoints()
QList<WirelessAccessPoint *> 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<QDBusArgument>();
argument.beginArray();
while(!argument.atEnd()) {
while (!argument.atEnd()) {
QDBusObjectPath accessPointObjectPath = qdbus_cast<QDBusObjectPath>(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<QDBusObjectPath>(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;
}

View File

@ -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<WirelessAccessPoint *> 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<QDBusObjectPath, WirelessAccessPoint *> 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);
}

View File

@ -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 \