From e89c4886888a322a21cf7c89c1ee2864dbb4e815 Mon Sep 17 00:00:00 2001 From: Raquel Medina Date: Mon, 17 Jun 2019 19:39:23 +0200 Subject: network: add wired adapter & model -Add WiredAdapter implementation -Add WiredNetworkModel implementation Bug-AGL: SPEC-2293 Signed-off-by: Raquel Medina Change-Id: Ibed93ae3a3cca1bc8e7a23c872c7ab3b8271e586 --- network/CMakeLists.txt | 10 +++- network/network.cpp | 19 ++++---- network/network.h | 3 +- network/networkadapter.cpp | 111 ------------------------------------------ network/networkadapter.h | 42 ++-------------- network/wifiadapter.cpp | 109 +++++++++++++++++++++++++++++++++++++++++ network/wifiadapter.h | 69 ++++++++++++++++++++++++++ network/wiredadapter.cpp | 96 ++++++++++++++++++++++++++++++++++++ network/wiredadapter.h | 58 ++++++++++++++++++++++ network/wirednetworkmodel.cpp | 57 ++++++++++++++++++++++ network/wirednetworkmodel.h | 28 +++++++++++ 11 files changed, 440 insertions(+), 162 deletions(-) delete mode 100644 network/networkadapter.cpp create mode 100644 network/wifiadapter.cpp create mode 100644 network/wifiadapter.h create mode 100644 network/wiredadapter.cpp create mode 100644 network/wiredadapter.h create mode 100644 network/wirednetworkmodel.cpp create mode 100644 network/wirednetworkmodel.h (limited to 'network') 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(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(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.cpp b/network/networkadapter.cpp deleted file mode 100644 index 6cfb9be..0000000 --- a/network/networkadapter.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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 -#include -#include - -#include "network.h" -#include "networkadapter.h" -#include "wifinetworkmodel.h" -#include "connectionprofile.h" - -WifiAdapter::WifiAdapter(Network *network, QQmlContext *context, QObject *parent) : - QObject(parent), - AdapterIf(), - m_wifiConnected(false), - m_wifiEnabled(false), - m_wifiStrength(0), - m_model(nullptr), - nw(network) -{ - m_model = new WifiNetworkModel(); - QSortFilterProxyModel *model = new QSortFilterProxyModel(); - model->setSourceModel(m_model); - model->setSortRole(WifiNetworkModel::WifiNetworkRoles::SsidRole); - model->setSortCaseSensitivity(Qt::CaseInsensitive); - model->sort(0); - - context->setContextProperty("WifiNetworkModel", m_model); - QObject::connect(m_model, &WifiNetworkModel::strengthChanged, this, &WifiAdapter::updateWifiStrength); - context->setContextProperty("WifiAdapter", this); -} - -WifiAdapter::~WifiAdapter() -{ - delete m_model; -} - -void WifiAdapter::updateWifiStatus(QJsonObject properties) -{ - if (properties.contains("connected")) { - m_wifiConnected = properties.value("connected").toBool(); - emit wifiConnectedChanged(m_wifiConnected); - } - - if (properties.contains("powered")) { - m_wifiEnabled = properties.value("powered").toBool(); - emit wifiEnabledChanged(m_wifiEnabled); - if (m_wifiEnabled) - nw->getServices(); - } -} - -void WifiAdapter::updateWifiStrength(int strength) -{ - m_wifiStrength = strength; - emit wifiStrengthChanged(m_wifiStrength); -} - -void WifiAdapter::updateProperties(QString id, QJsonObject properties) -{ - if (m_model->getNetwork(id)) - m_model->updateProperties(id, properties); -} - -bool WifiAdapter::addService(QString id, QJsonObject properties) -{ - 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(); - QString security = properties.value("security").toArray().at(0).toString(); - - // Ignore hidden SSIDs or services already added - 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); - - if ((state == "ready") || (state == "online")) - updateWifiStrength(strength); - - return true; -} - -void WifiAdapter::removeService(QString id) -{ - m_model->removeNetwork(m_model->getNetwork(id)); - -} - 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/wifiadapter.cpp b/network/wifiadapter.cpp new file mode 100644 index 0000000..440e541 --- /dev/null +++ b/network/wifiadapter.cpp @@ -0,0 +1,109 @@ +/* + * 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 +#include +#include + +#include "network.h" +#include "networkadapter.h" +#include "wifinetworkmodel.h" +#include "connectionprofile.h" + +WifiAdapter::WifiAdapter(Network *network, QQmlContext *context, QObject *parent) : + QObject(parent), + AdapterIf(), + m_wifiConnected(false), + m_wifiEnabled(false), + m_wifiStrength(0), + m_model(nullptr), + nw(network) +{ + m_model = new WifiNetworkModel(); + QSortFilterProxyModel *model = new QSortFilterProxyModel(); + model->setSourceModel(m_model); + model->setSortRole(WifiNetworkModel::WifiNetworkRoles::SsidRole); + model->setSortCaseSensitivity(Qt::CaseInsensitive); + model->sort(0); + + context->setContextProperty("WifiNetworkModel", m_model); + QObject::connect(m_model, &WifiNetworkModel::strengthChanged, this, &WifiAdapter::updateWifiStrength); + context->setContextProperty("WifiAdapter", this); +} + +WifiAdapter::~WifiAdapter() +{ + delete m_model; +} + +void WifiAdapter::updateStatus(QJsonObject properties) +{ + if (properties.contains("connected")) { + m_wifiConnected = properties.value("connected").toBool(); + emit wifiConnectedChanged(m_wifiConnected); + } + + if (properties.contains("powered")) { + m_wifiEnabled = properties.value("powered").toBool(); + emit wifiEnabledChanged(m_wifiEnabled); + if (m_wifiEnabled) + nw->getServices(); + } +} + +void WifiAdapter::updateWifiStrength(int strength) +{ + m_wifiStrength = strength; + emit wifiStrengthChanged(m_wifiStrength); +} + +void WifiAdapter::updateProperties(QString id, QJsonObject properties) +{ + if (m_model->getNetwork(id)) + m_model->updateProperties(id, 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(); + 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(); + QString security = properties.value("security").toArray().at(0).toString(); + + // Ignore hidden SSIDs or services already added + if (m_model->getNetwork(id) || (ssid == "")) + return false; + + ConnectionProfile *network = new ConnectionProfile(address, security, id, state, ssid, strength); + m_model->addNetwork(network); + + if ((state == "ready") || (state == "online")) + updateWifiStrength(strength); + + return true; +} + +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 +#include +#include +#include +#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 +#include +#include + +#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 +#include +#include +#include +#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 + +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 WiredNetworkModel::roleNames() const { + QHash 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 roleNames() const; + +}; +#endif // WIRED_NETWORK_MODEL_H -- cgit 1.2.3-korg