diff options
author | Raquel Medina <raquel.medina@konsulko.com> | 2019-06-17 19:39:23 +0200 |
---|---|---|
committer | raquel medina <raquel.medina@konsulko.com> | 2019-06-18 11:20:43 +0000 |
commit | e89c4886888a322a21cf7c89c1ee2864dbb4e815 (patch) | |
tree | 1f943c3afd60bf56f79d4e28803ffe1aca55f654 | |
parent | 9d896c9c8f5622370362091f38bbc5d1f4189901 (diff) |
network: add wired adapter & model
-Add WiredAdapter implementation
-Add WiredNetworkModel implementation
Bug-AGL: SPEC-2293
Signed-off-by: Raquel Medina <raquel.medina@konsulko.com>
Change-Id: Ibed93ae3a3cca1bc8e7a23c872c7ab3b8271e586
-rw-r--r-- | network/CMakeLists.txt | 10 | ||||
-rw-r--r-- | network/network.cpp | 19 | ||||
-rw-r--r-- | network/network.h | 3 | ||||
-rw-r--r-- | network/networkadapter.h | 42 | ||||
-rw-r--r-- | network/wifiadapter.cpp (renamed from network/networkadapter.cpp) | 12 | ||||
-rw-r--r-- | network/wifiadapter.h | 69 | ||||
-rw-r--r-- | network/wiredadapter.cpp | 96 | ||||
-rw-r--r-- | network/wiredadapter.h | 58 | ||||
-rw-r--r-- | network/wirednetworkmodel.cpp | 57 | ||||
-rw-r--r-- | network/wirednetworkmodel.h | 28 |
10 files changed, 336 insertions, 58 deletions
diff --git a/network/CMakeLists.txt b/network/CMakeLists.txt index cfefeb0..e6d93bd 100644 --- a/network/CMakeLists.txt +++ b/network/CMakeLists.txt @@ -1,2 +1,8 @@ -add_headers(network.h networkmessage.h networkadapter.h wifinetworkmodel.h abstractnetworkmodel.h connectionprofile.h) -add_sources(network.cpp networkmessage.cpp networkadapter.cpp wifinetworkmodel.cpp abstractnetworkmodel.cpp connectionprofile.cpp) +add_headers(network.h networkmessage.h + networkadapter.h wifiadapter.h wiredadapter.h + wifinetworkmodel.h wirednetworkmodel.h abstractnetworkmodel.h + connectionprofile.h) +add_sources(network.cpp networkmessage.cpp + wifiadapter.cpp wiredadapter.cpp + wifinetworkmodel.cpp wirednetworkmodel.cpp abstractnetworkmodel.cpp + connectionprofile.cpp) diff --git a/network/network.cpp b/network/network.cpp index 9574837..679f374 100644 --- a/network/network.cpp +++ b/network/network.cpp @@ -33,6 +33,8 @@ Network::Network (QUrl &url, QQmlContext *context, QObject * parent) : QObject::connect(m_mloop, &MessageEngine::connected, this, &Network::onConnected); QObject::connect(m_mloop, &MessageEngine::disconnected, this, &Network::onDisconnected); QObject::connect(m_mloop, &MessageEngine::messageReceived, this, &Network::onMessageReceived); + + m_adapters.append(new WiredAdapter(this, context, parent)); } Network::~Network() @@ -198,10 +200,9 @@ void Network::parseTechnologies(QJsonArray technologies) QJsonObject properties = technology.value("properties").toObject(); QString type = properties.value("type").toString(); - if (type == "wifi") { - WifiAdapter* wifi_a = static_cast<WifiAdapter*>(findAdapter(type)); - wifi_a->updateWifiStatus(properties); - } + AdapterIf* adapter = findAdapter(type); + if (adapter) + adapter->updateStatus(properties); } } @@ -243,11 +244,11 @@ void Network::processEvent(NetworkMessage *nmsg) updateServiceProperties(nmsg->eventData()); } else if (nmsg->eventName() == "technology_properties") { QJsonObject technology = nmsg->eventData(); - if (technology.value("technology").toString() == "wifi") { - QJsonObject properties = technology.value("properties").toObject(); - WifiAdapter* wifi_a = static_cast<WifiAdapter*>(findAdapter("wifi")); - wifi_a->updateWifiStatus(properties); - } + QJsonObject properties = technology.value("properties").toObject(); + QString type = technology.value("technology").toString(); + AdapterIf* adapter = findAdapter(type); + if (adapter) + adapter->updateStatus(properties); } } diff --git a/network/network.h b/network/network.h index 21720bc..b289f42 100644 --- a/network/network.h +++ b/network/network.h @@ -26,7 +26,8 @@ #include "messageengine.h" #include "networkmessage.h" #include "responsemessage.h" -#include "networkadapter.h" +#include "wifiadapter.h" +#include "wiredadapter.h" class Network : public QObject { diff --git a/network/networkadapter.h b/network/networkadapter.h index 4aac701..b3291f0 100644 --- a/network/networkadapter.h +++ b/network/networkadapter.h @@ -24,6 +24,7 @@ class Network; class WifiNetworkModel; +class WiredNetworkModel; class AdapterIf { @@ -33,47 +34,10 @@ class AdapterIf virtual bool addService(QString id, QJsonObject properties) = 0; virtual void removeService(QString id) = 0; virtual void updateProperties(QString service, QJsonObject properties) = 0; + virtual QString getType() = 0; + virtual void updateStatus(QJsonObject properties) = 0; }; - Q_DECLARE_INTERFACE(AdapterIf, "AdapterIf") -class WifiAdapter : public QObject, public AdapterIf -{ - Q_OBJECT - Q_INTERFACES(AdapterIf) - Q_PROPERTY(bool wifiConnected READ wifiConnected NOTIFY wifiConnectedChanged) - Q_PROPERTY(bool wifiEnabled READ wifiEnabled NOTIFY wifiEnabledChanged) - Q_PROPERTY(int wifiStrength READ wifiStrength NOTIFY wifiStrengthChanged) - - public: - explicit WifiAdapter(Network *network, QQmlContext *context, QObject *parent); - virtual ~WifiAdapter(); - - bool wifiConnected() const { return m_wifiConnected; } - bool wifiEnabled() const { return m_wifiEnabled; } - int wifiStrength() const { return m_wifiStrength; } - void updateWifiStatus(QJsonObject properties); - - bool addService(QString id, QJsonObject properties) override; - void removeService(QString id) override; - void updateProperties(QString service, QJsonObject properties) override; - QString getType() override { return "wifi"; } - - //slots - void updateWifiStrength(int); - - signals: - void wifiConnectedChanged(bool connected); - void wifiEnabledChanged(bool enabled); - void wifiStrengthChanged(int strength); - - private: - bool m_wifiConnected; - bool m_wifiEnabled; - int m_wifiStrength; - WifiNetworkModel *m_model; - Network *nw; -}; - #endif // ADAPTER_H diff --git a/network/networkadapter.cpp b/network/wifiadapter.cpp index 6cfb9be..440e541 100644 --- a/network/networkadapter.cpp +++ b/network/wifiadapter.cpp @@ -49,7 +49,7 @@ WifiAdapter::~WifiAdapter() delete m_model; } -void WifiAdapter::updateWifiStatus(QJsonObject properties) +void WifiAdapter::updateStatus(QJsonObject properties) { if (properties.contains("connected")) { m_wifiConnected = properties.value("connected").toBool(); @@ -78,9 +78,12 @@ void WifiAdapter::updateProperties(QString id, QJsonObject properties) bool WifiAdapter::addService(QString id, QJsonObject properties) { + QString type = properties.value("type").toString(); + if (type != getType()) + return false; + QString ssid = properties.value("name").toString(); QString state = properties.value("state").toString(); - QString type = properties.value("type").toString(); int strength = properties.value("strength").toInt(); // Initially support only IPv4 and the first security method found QString address = properties.value("ipv4").toObject().value("address").toString(); @@ -90,10 +93,6 @@ bool WifiAdapter::addService(QString id, QJsonObject properties) if (m_model->getNetwork(id) || (ssid == "")) return false; - // only process wifi services - if (type != getType()) - return false; - ConnectionProfile *network = new ConnectionProfile(address, security, id, state, ssid, strength); m_model->addNetwork(network); @@ -108,4 +107,3 @@ void WifiAdapter::removeService(QString id) m_model->removeNetwork(m_model->getNetwork(id)); } - diff --git a/network/wifiadapter.h b/network/wifiadapter.h new file mode 100644 index 0000000..ddb44fb --- /dev/null +++ b/network/wifiadapter.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2019 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WIFIADAPTER_H +#define WIFIADAPTER_H + +#include <QDebug> +#include <QObject> +#include <QJsonArray> +#include <QtQml/QQmlContext> +#include "networkadapter.h" + +class Network; +class WifiNetworkModel; + + +class WifiAdapter : public QObject, public AdapterIf +{ + Q_OBJECT + Q_INTERFACES(AdapterIf) + Q_PROPERTY(bool wifiConnected READ wifiConnected NOTIFY wifiConnectedChanged) + Q_PROPERTY(bool wifiEnabled READ wifiEnabled NOTIFY wifiEnabledChanged) + Q_PROPERTY(int wifiStrength READ wifiStrength NOTIFY wifiStrengthChanged) + + public: + explicit WifiAdapter(Network *network, QQmlContext *context, QObject *parent); + virtual ~WifiAdapter(); + + bool wifiConnected() const { return m_wifiConnected; } + bool wifiEnabled() const { return m_wifiEnabled; } + int wifiStrength() const { return m_wifiStrength; } + + bool addService(QString id, QJsonObject properties) override; + void removeService(QString id) override; + void updateProperties(QString service, QJsonObject properties) override; + + QString getType() override { return "wifi"; } + void updateStatus(QJsonObject properties) override; + + //slots + void updateWifiStrength(int); + + signals: + void wifiConnectedChanged(bool connected); + void wifiEnabledChanged(bool enabled); + void wifiStrengthChanged(int strength); + + private: + bool m_wifiConnected; + bool m_wifiEnabled; + int m_wifiStrength; + WifiNetworkModel *m_model; + Network *nw; +}; + +#endif // WIFIADAPTER_H diff --git a/network/wiredadapter.cpp b/network/wiredadapter.cpp new file mode 100644 index 0000000..224d13a --- /dev/null +++ b/network/wiredadapter.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2019 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <QMetaEnum> +#include <QSortFilterProxyModel> +#include <QtQml/QQmlEngine> + +#include "network.h" +#include "networkadapter.h" +#include "wirednetworkmodel.h" +#include "connectionprofile.h" + +WiredAdapter::WiredAdapter(Network *network, QQmlContext *context, QObject *parent) : + QObject(parent), + AdapterIf(), + m_wiredConnected(false), + m_wiredEnabled(false), + m_model(nullptr), + nw(network) +{ + m_model = new WiredNetworkModel(); + QSortFilterProxyModel *model = new QSortFilterProxyModel(); + model->setSourceModel(m_model); + model->setSortRole(WiredNetworkModel::WiredNetworkRoles::ServiceRole); + model->setSortCaseSensitivity(Qt::CaseInsensitive); + model->sort(0); + + context->setContextProperty("WiredNetworkModel", m_model); + context->setContextProperty("WiredAdapter", this); +} + +WiredAdapter::~WiredAdapter() +{ + delete m_model; +} + +void WiredAdapter::updateStatus(QJsonObject properties) +{ + if (properties.contains("connected")) { + m_wiredConnected = properties.value("connected").toBool(); + emit wiredConnectedChanged(m_wiredConnected); + } + + if (properties.contains("powered")) { + m_wiredEnabled = properties.value("powered").toBool(); + emit wiredEnabledChanged(m_wiredEnabled); + if (m_wiredEnabled) + nw->getServices(); + } +} + +void WiredAdapter::updateProperties(QString id, QJsonObject properties) +{ + if (m_model->getNetwork(id)) + m_model->updateProperties(id, properties); +} + +bool WiredAdapter::addService(QString id, QJsonObject properties) +{ + QString type = properties.value("type").toString(); + if (type != getType()) + return false; + + QString state = properties.value("state").toString(); + // Initially support only IPv4 and the first security method found + QString address = properties.value("ipv4").toObject().value("address").toString(); + QString security = properties.value("security").toArray().at(0).toString(); + + // Ignore services already added + if (m_model->getNetwork(id)) + return false; + + ConnectionProfile *network = new ConnectionProfile(address, security, id, state, "",0); + m_model->addNetwork(network); + + return true; +} + +void WiredAdapter::removeService(QString id) +{ + m_model->removeNetwork(m_model->getNetwork(id)); + +} diff --git a/network/wiredadapter.h b/network/wiredadapter.h new file mode 100644 index 0000000..141ccfb --- /dev/null +++ b/network/wiredadapter.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2019 Konsulko Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WIREDADAPTER_H +#define WIREDADAPTER_H + +#include <QDebug> +#include <QObject> +#include <QJsonArray> +#include <QtQml/QQmlContext> +#include "networkadapter.h" + +class Network; +class WiredNetworkModel; + +class WiredAdapter : public QObject, public AdapterIf +{ + Q_OBJECT + Q_INTERFACES(AdapterIf) + public: + explicit WiredAdapter(Network *network, QQmlContext *context, QObject *parent); + virtual ~WiredAdapter(); + + bool wiredConnected() const { return m_wiredConnected; } + bool wiredEnabled() const { return m_wiredEnabled; } + + bool addService(QString id, QJsonObject properties) override; + void removeService(QString id) override; + void updateProperties(QString service, QJsonObject properties) override; + + QString getType() override { return "ethernet"; } + void updateStatus(QJsonObject properties) override; + + signals: + void wiredConnectedChanged(bool connected); + void wiredEnabledChanged(bool enabled); + + private: + bool m_wiredConnected; + bool m_wiredEnabled; + WiredNetworkModel *m_model; + Network *nw; +}; + +#endif // WIREDADAPTER_H diff --git a/network/wirednetworkmodel.cpp b/network/wirednetworkmodel.cpp new file mode 100644 index 0000000..d14bce4 --- /dev/null +++ b/network/wirednetworkmodel.cpp @@ -0,0 +1,57 @@ +#include "wirednetworkmodel.h" +#include "connectionprofile.h" +#include <QDebug> + +WiredNetworkModel::WiredNetworkModel(QObject *parent) + : AbstractNetworkModel(parent) +{ +} + +QVariant WiredNetworkModel::data(const QModelIndex &index, int role) const +{ + if (index.row() < 0 || index.row() >= m_networks.count()) + return QVariant(); + + const ConnectionProfile *network = m_networks[index.row()]; + + switch (role) { + case AddressRole: + return network->address(); + case SecurityRole: + return network->security(); + case ServiceRole: + return network->service(); + case StateRole: + return network->state(); + } + + return QVariant(); +} + +QHash<int, QByteArray> WiredNetworkModel::roleNames() const { + QHash<int, QByteArray> roles; + roles[AddressRole] = "address"; + roles[SecurityRole] = "security"; + roles[ServiceRole] = "service"; + roles[StateRole] = "sstate"; + + return roles; +} + +void WiredNetworkModel::updateProperties(QString service, QJsonObject properties) +{ + ConnectionProfile *network; + + // FIXME: add role parameter to emits + if ((network = getNetwork(service))) { + if (properties.contains("ipv4")) { + QString address = properties.value("ipv4").toObject().value("address").toString(); + network->setAddress(address); + emit dataChanged(indexOf(network), indexOf(network)); + } + if (properties.contains("state")) { + network->setState(properties.value("state").toString()); + emit dataChanged(indexOf(network), indexOf(network)); + } + } +} diff --git a/network/wirednetworkmodel.h b/network/wirednetworkmodel.h new file mode 100644 index 0000000..b73fa3f --- /dev/null +++ b/network/wirednetworkmodel.h @@ -0,0 +1,28 @@ +#ifndef WIRED_NETWORK_MODEL_H +#define WIRED_NETWORK_MODEL_H + +#include "abstractnetworkmodel.h" + +class WiredNetworkModel : public AbstractNetworkModel +{ + Q_OBJECT + + public: + enum WiredNetworkRoles { + AddressRole = Qt::UserRole + 1, + SecurityRole, + ServiceRole, + StateRole, + }; + + WiredNetworkModel(QObject *parent = Q_NULLPTR); + + QString getType() const override { return "wired"; } + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + void updateProperties(QString service, QJsonObject properties) override; + + protected: + QHash<int, QByteArray> roleNames() const; + +}; +#endif // WIRED_NETWORK_MODEL_H |