add network manager api

This commit is contained in:
Simon Stürz 2016-09-30 19:23:17 +02:00 committed by Michael Zanetti
parent 642b5c8331
commit 22d5dc1bf6
16 changed files with 313 additions and 29 deletions

View File

@ -372,27 +372,37 @@ WebSocketServer *GuhCore::webSocketServer() const
return m_webSocketServer;
}
/*! Returns a pointer to the \l{CloudManager} instance owned by GuhCore. */
CloudManager *GuhCore::cloudManager() const
{
return m_cloudManager;
}
/*! Returns a pointer to the \l{ServerManager} instance owned by GuhCore. */
ServerManager *GuhCore::serverManager() const
{
return m_serverManager;
}
/*! Returns the list of available system languages. */
QStringList GuhCore::getAvailableLanguages()
{
// TODO: parse available translation files
return QStringList() << "en_US" << "de_DE";
}
/*! Returns a pointer to the \l{BluetoothServer} instance owned by GuhCore. */
BluetoothServer *GuhCore::bluetoothServer() const
{
return m_bluetoothServer;
}
/*! Returns a pointer to the \l{NetworkManager} instance owned by GuhCore. */
NetworkManager *GuhCore::networkManager() const
{
return m_networkManager;
}
#ifdef TESTING_ENABLED
MockTcpServer *GuhCore::tcpServer() const
{

View File

@ -87,6 +87,7 @@ public:
CloudManager *cloudManager() const;
ServerManager *serverManager() const;
BluetoothServer *bluetoothServer() const;
NetworkManager *networkManager() const;
static QStringList getAvailableLanguages();

View File

@ -57,6 +57,7 @@
#include "websocketserver.h"
#include "cloudhandler.h"
#include "configurationhandler.h"
#include "networkmanagerhandler.h"
#include <QJsonDocument>
#include <QStringList>
@ -174,6 +175,7 @@ void JsonRPCServer::setup()
registerHandler(new StateHandler(this));
registerHandler(new CloudHandler(this));
registerHandler(new ConfigurationHandler(this));
registerHandler(new NetworkManagerHandler(this));
}
void JsonRPCServer::processData(const QUuid &clientId, const QString &targetNamespace, const QString &method, const QVariantMap &message)

View File

@ -111,6 +111,7 @@ QVariantMap JsonTypes::s_timeDescriptor;
QVariantMap JsonTypes::s_calendarItem;
QVariantMap JsonTypes::s_timeEventItem;
QVariantMap JsonTypes::s_repeatingOption;
QVariantMap JsonTypes::s_wirelessAccessPoint;
void JsonTypes::init()
{
@ -320,6 +321,11 @@ void JsonTypes::init()
s_repeatingOption.insert("o:weekDays", QVariantList() << basicTypeToString(Int));
s_repeatingOption.insert("o:monthDays", QVariantList() << basicTypeToString(Int));
s_wirelessAccessPoint.insert("ssid", basicTypeToString(QVariant::String));
s_wirelessAccessPoint.insert("macAddress", basicTypeToString(QVariant::String));
s_wirelessAccessPoint.insert("frequency", basicTypeToString(QVariant::Double));
s_wirelessAccessPoint.insert("signalStrength", basicTypeToString(QVariant::Int));
s_initialized = true;
}
@ -391,6 +397,7 @@ QVariantMap JsonTypes::allTypes()
allTypes.insert("CalendarItem", calendarItemDescription());
allTypes.insert("TimeEventItem", timeEventItemDescription());
allTypes.insert("RepeatingOption", repeatingOptionDescription());
allTypes.insert("WirelessAccessPoint", wirelessAccessPointDescription());
return allTypes;
}
@ -943,6 +950,18 @@ QVariantMap JsonTypes::packTimeDescriptor(const TimeDescriptor &timeDescriptor)
return timeDescriptorVariant;
}
/*! Returns a variant map of the given \a wirelessAccessPoint. */
QVariantMap JsonTypes::packWirelessAccessPoint(WirelessAccessPoint *wirelessAccessPoint)
{
QVariantMap wirelessAccessPointVariant;
wirelessAccessPointVariant.insert("ssid", wirelessAccessPoint->ssid());
wirelessAccessPointVariant.insert("macAddress", wirelessAccessPoint->macAddress());
wirelessAccessPointVariant.insert("frequency", wirelessAccessPoint->frequency());
wirelessAccessPointVariant.insert("signalStrength", wirelessAccessPoint->signalStrength());
return wirelessAccessPointVariant;
}
/*! Returns a variant list of the supported vendors. */
QVariantList JsonTypes::packSupportedVendors()
{
@ -1503,6 +1522,10 @@ QPair<bool, QString> JsonTypes::validateProperty(const QVariant &templateValue,
QString errorString = QString("Param %1 is not a uint.").arg(value.toString());
return report(value.canConvert(QVariant::UInt), errorString);
}
if (strippedTemplateValue == JsonTypes::basicTypeToString(QVariant::Double)) {
QString errorString = QString("Param %1 is not a double.").arg(value.toString());
return report(value.canConvert(QVariant::Double), errorString);
}
if (strippedTemplateValue == JsonTypes::basicTypeToString(QVariant::Time)) {
QString errorString = QString("Param %1 is not a time (hh:mm).").arg(value.toString());
return report(value.canConvert(QVariant::Time), errorString);
@ -1697,6 +1720,12 @@ QPair<bool, QString> JsonTypes::validateVariant(const QVariant &templateVariant,
qCWarning(dcJsonRpc) << "TimeEventItem not matching";
return result;
}
} else if (refName == wirelessAccessPointRef()) {
QPair<bool, QString> result = validateMap(wirelessAccessPointDescription(), variant.toMap());
if (!result.first) {
qCWarning(dcJsonRpc) << "WirelessAccessPoint not matching";
return result;
}
} else if (refName == basicTypeRef()) {
QPair<bool, QString> result = validateBasicType(variant);
if (!result.first) {

View File

@ -47,6 +47,8 @@
#include "cloud/cloudconnection.h"
#include "networkmanager/wirelessaccesspoint.h"
#include <QObject>
#include <QVariantMap>
@ -154,6 +156,7 @@ public:
DECLARE_OBJECT(calendarItem, "CalendarItem")
DECLARE_OBJECT(timeEventItem, "TimeEventItem")
DECLARE_OBJECT(repeatingOption, "RepeatingOption")
DECLARE_OBJECT(wirelessAccessPoint, "WirelessAccessPoint")
// pack types
static QVariantMap packEventType(const EventType &eventType);
@ -182,6 +185,7 @@ public:
static QVariantMap packCalendarItem(const CalendarItem &calendarItem);
static QVariantMap packTimeEventItem(const TimeEventItem &timeEventItem);
static QVariantMap packTimeDescriptor(const TimeDescriptor &timeDescriptor);
static QVariantMap packWirelessAccessPoint(WirelessAccessPoint *wirelessAccessPoint);
// pack resources
static QVariantList packRules(const QList<Rule> rules);

View File

@ -0,0 +1,60 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* 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 "networkmanagerhandler.h"
#include "loggingcategories.h"
#include "guhcore.h"
#include "networkmanager/networkmanager.h"
namespace guhserver {
NetworkManagerHandler::NetworkManagerHandler(QObject *parent) :
JsonHandler(parent)
{
QVariantMap params;
QVariantMap returns;
params.clear(); returns.clear();
setDescription("GetWirelessAccessPoints", "Get the current list of wireless network access points.");
setParams("GetWirelessAccessPoints", params);
returns.insert("wirelessAccessPoints", QVariantList() << JsonTypes::wirelessAccessPointRef());
setReturns("GetWirelessAccessPoints", returns);
}
QString NetworkManagerHandler::name() const
{
return "NetworkManager";
}
JsonReply *NetworkManagerHandler::GetWirelessAccessPoints(const QVariantMap &params)
{
Q_UNUSED(params);
QVariantList wirelessAccessPoints;
foreach (WirelessAccessPoint *wirelessAccessPoint, GuhCore::instance()->networkManager()->wirelessNetworkManager()->accessPoints())
wirelessAccessPoints.append(JsonTypes::packWirelessAccessPoint(wirelessAccessPoint));
QVariantMap returns;
returns.insert("wirelessAccessPoints", wirelessAccessPoints);
return createReply(returns);
}
}

View File

@ -0,0 +1,48 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* 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 NETWORKMANAGERHANDLER_H
#define NETWORKMANAGERHANDLER_H
#include <QObject>
#include "jsonhandler.h"
namespace guhserver {
class NetworkManagerHandler : public JsonHandler
{
Q_OBJECT
public:
explicit NetworkManagerHandler(QObject *parent = 0);
QString name() const;
Q_INVOKABLE JsonReply *GetWirelessAccessPoints(const QVariantMap &params);
signals:
public slots:
};
}
#endif // NETWORKMANAGERHANDLER_H

View File

@ -34,6 +34,7 @@ static const QString deviceInterfaceString("org.freedesktop.NetworkManager.Devic
static const QString wirelessInterfaceString("org.freedesktop.NetworkManager.Device.Wireless");
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

@ -1,6 +1,64 @@
#include "networkconnection.h"
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* 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/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
NetworkConnection::NetworkConnection(QObject *parent) : QObject(parent)
#include "networkconnection.h"
#include "dbus-interfaces.h"
#include "loggingcategories.h"
#include <QJsonDocument>
namespace guhserver {
NetworkConnection::NetworkConnection(const QDBusObjectPath &objectPath, QObject *parent) :
QObject(parent),
m_objectPath(objectPath)
{
qDBusRegisterMetaType<ConnectionSettings>();
QDBusInterface connectionInterface(serviceString, m_objectPath.path(), connectionsInterfaceString, QDBusConnection::systemBus());
if(!connectionInterface.isValid()) {
qCWarning(dcNetworkManager()) << "Invalid connection dbus interface";
return;
}
QDBusMessage query = connectionInterface.call("GetSettings");
if(query.type() != QDBusMessage::ReplyMessage) {
qCWarning(dcNetworkManager()) << query.errorName() << query.errorMessage();
return;
}
if (query.arguments().isEmpty())
return;
const QDBusArgument &argument = query.arguments().at(0).value<QDBusArgument>();
ConnectionSettings settingsMap = qdbus_cast<ConnectionSettings>(argument);
foreach (const QString &category, settingsMap.keys())
if (category == "connection")
qCDebug(dcNetworkManager()) << " --> " << category << qUtf8Printable(QJsonDocument::fromVariant(settingsMap.value(category)).toJson(QJsonDocument::Indented));
}
QDBusObjectPath NetworkConnection::objectPath() const
{
return m_objectPath;
}
}

View File

@ -1,17 +1,58 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* 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 NETWORKCONNECTION_H
#define NETWORKCONNECTION_H
#include <QDebug>
#include <QObject>
#include <QDBusMetaType>
#include <QDBusObjectPath>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusArgument>
namespace guhserver {
typedef QMap<QString, QVariantMap> ConnectionSettings;
class NetworkConnection : public QObject
{
Q_OBJECT
public:
explicit NetworkConnection(QObject *parent = 0);
explicit NetworkConnection(const QDBusObjectPath &objectPath, QObject *parent = 0);
QDBusObjectPath objectPath() const;
private:
QDBusObjectPath m_objectPath;
signals:
public slots:
};
#endif // NETWORKCONNECTION_H
}
Q_DECLARE_METATYPE(guhserver::ConnectionSettings)
#endif // NETWORKCONNECTION_H

View File

@ -44,21 +44,7 @@ NetworkManager::NetworkManager(QObject *parent) :
m_wirelessEnabled = m_networkManagerInterface->property("WirelessEnabled").toBool();
qCDebug(dcNetworkManager()) << "Networkmanager version" << m_version;
// Get network devices
QDBusMessage query = m_networkManagerInterface->call("GetDevices");
if(query.type() != QDBusMessage::ReplyMessage) {
qCWarning(dcNetworkManager()) << query.errorName() << query.errorMessage();
return;
}
const QDBusArgument &argument = query.arguments().at(0).value<QDBusArgument>();
argument.beginArray();
while(!argument.atEnd()) {
QDBusObjectPath deviceObjectPath = qdbus_cast<QDBusObjectPath>(argument);
onDeviceAdded(deviceObjectPath);
}
argument.endArray();
loadDevices();
// Connect signals
QDBusConnection::systemBus().connect(serviceString, pathString, serviceString, "StateChanged", this, SLOT(onStateChanged(uint)));
@ -66,6 +52,7 @@ NetworkManager::NetworkManager(QObject *parent) :
QDBusConnection::systemBus().connect(serviceString, pathString, serviceString, "DeviceRemoved", this, SLOT(onDeviceRemoved(QDBusObjectPath)));
QDBusConnection::systemBus().connect(serviceString, pathString, serviceString, "PropertiesChanged", this, SLOT(onPropertiesChanged(QVariantMap)));
// Create settings
m_networkSettings = new NetworkSettings(this);
}
@ -108,6 +95,11 @@ QList<NetworkDevice *> NetworkManager::networkDevices() const
return m_networkDevices.values();
}
WirelessNetworkManager *NetworkManager::wirelessNetworkManager() const
{
return m_wirelessNetworkManager;
}
QString NetworkManager::version() const
{
return m_version;
@ -174,6 +166,27 @@ bool NetworkManager::enableWireless()
return m_networkManagerInterface->setProperty("WirelessEnabled", true);
}
void NetworkManager::loadDevices()
{
// Get network devices
QDBusMessage query = m_networkManagerInterface->call("GetDevices");
if(query.type() != QDBusMessage::ReplyMessage) {
qCWarning(dcNetworkManager()) << query.errorName() << query.errorMessage();
return;
}
if (query.arguments().isEmpty())
return;
const QDBusArgument &argument = query.arguments().at(0).value<QDBusArgument>();
argument.beginArray();
while(!argument.atEnd()) {
QDBusObjectPath deviceObjectPath = qdbus_cast<QDBusObjectPath>(argument);
onDeviceAdded(deviceObjectPath);
}
argument.endArray();
}
void NetworkManager::setVersion(const QString &version)
{
m_version = version;

View File

@ -33,7 +33,7 @@
#include "networkdevice.h"
#include "networksettings.h"
// Note: https://developer.gnome.org/NetworkManager/unstable/spec.html
// Docs: https://developer.gnome.org/NetworkManager/unstable/spec.html
namespace guhserver {
@ -71,15 +71,19 @@ public:
QList<NetworkDevice *> networkDevices() const;
WirelessNetworkManager *wirelessNetworkManager() const;
// Properties
QString version() const;
NetworkManagerState state() const;
NetworkManagerConnectivityState connectivityState() const;
// Networking
bool networkingEnabled() const;
bool enableNetworking();
bool disableNetworking();
// Wireless Networking
bool wirelessEnabled() const;
bool enableWireless();
bool disableWireless();
@ -98,6 +102,8 @@ private:
bool m_networkingEnabled;
bool m_wirelessEnabled;
void loadDevices();
void setVersion(const QString &version);
void setNetworkingEnabled(const bool &enabled);
void setWirelessEnabled(const bool &enabled);

View File

@ -21,29 +21,24 @@
#include "networksettings.h"
#include "dbus-interfaces.h"
#include "loggingcategories.h"
#include "networkconnection.h"
namespace guhserver {
NetworkSettings::NetworkSettings(QObject *parent) : QObject(parent)
{
QDBusConnection systemBus = QDBusConnection::systemBus();
if (!systemBus.isConnected()) {
qCWarning(dcNetworkManager()) << "System DBus not connected";
return;
}
m_settingsInterface = new QDBusInterface(serviceString, settingsPathString, settingsInterfaceString, QDBusConnection::systemBus(), this);
if(!m_settingsInterface->isValid()) {
qCWarning(dcNetworkManager()) << "Invalid DBus network settings interface";
return;
}
loadConnections();
QDBusConnection::systemBus().connect(serviceString, settingsPathString, settingsInterfaceString, "NewConnection", this, SLOT(connectionAdded(QDBusObjectPath)));
QDBusConnection::systemBus().connect(serviceString, settingsPathString, settingsInterfaceString, "ConnectionRemoved", this, SLOT(connectionRemoved(QDBusObjectPath)));
QDBusConnection::systemBus().connect(serviceString, settingsPathString, settingsInterfaceString, "PropertiesChanged", this, SLOT(propertiesChanged(QVariantMap)));
loadConnections();
}
void NetworkSettings::loadConnections()
@ -55,6 +50,9 @@ void NetworkSettings::loadConnections()
return;
}
if (query.arguments().isEmpty())
return;
const QDBusArgument &argument = query.arguments().at(0).value<QDBusArgument>();
argument.beginArray();
while(!argument.atEnd()) {
@ -67,7 +65,12 @@ void NetworkSettings::loadConnections()
void NetworkSettings::connectionAdded(const QDBusObjectPath &objectPath)
{
NetworkConnection *connection = new NetworkConnection(objectPath, this);
m_connections.insert(objectPath, connection);
qCDebug(dcNetworkManager()) << "Settings: [+]" << objectPath.path();
}
void NetworkSettings::connectionRemoved(const QDBusObjectPath &objectPath)

View File

@ -29,6 +29,8 @@
namespace guhserver {
class NetworkConnection;
class NetworkSettings : public QObject
{
Q_OBJECT
@ -37,6 +39,7 @@ public:
private:
QDBusInterface *m_settingsInterface;
QHash<QDBusObjectPath, NetworkConnection *> m_connections;
void loadConnections();

View File

@ -161,12 +161,15 @@ void WirelessNetworkManager::readAccessPoints()
return;
}
QDBusMessage query= wirelessInterface.call("GetAccessPoints");
QDBusMessage query = wirelessInterface.call("GetAccessPoints");
if(query.type() != QDBusMessage::ReplyMessage) {
qCWarning(dcNetworkManager()) << query.errorName() << query.errorMessage();
return;
}
if (query.arguments().isEmpty())
return;
const QDBusArgument &argument = query.arguments().at(0).value<QDBusArgument>();
argument.beginArray();
while(!argument.atEnd()) {

View File

@ -25,6 +25,7 @@ HEADERS += $$top_srcdir/server/guhcore.h \
$$top_srcdir/server/jsonrpc/logginghandler.h \
$$top_srcdir/server/jsonrpc/cloudhandler.h \
$$top_srcdir/server/jsonrpc/configurationhandler.h \
$$top_srcdir/server/jsonrpc/networkmanagerhandler.h \
$$top_srcdir/server/logging/logging.h \
$$top_srcdir/server/logging/logengine.h \
$$top_srcdir/server/logging/logfilter.h \
@ -90,6 +91,7 @@ SOURCES += $$top_srcdir/server/guhcore.cpp \
$$top_srcdir/server/jsonrpc/logginghandler.cpp \
$$top_srcdir/server/jsonrpc/cloudhandler.cpp \
$$top_srcdir/server/jsonrpc/configurationhandler.cpp \
$$top_srcdir/server/jsonrpc/networkmanagerhandler.cpp \
$$top_srcdir/server/logging/logengine.cpp \
$$top_srcdir/server/logging/logfilter.cpp \
$$top_srcdir/server/logging/logentry.cpp \