diff options
author | Raquel Medina <raquel.medina@konsulko.com> | 2019-04-26 03:06:33 +0300 |
---|---|---|
committer | raquel medina <raquel.medina@konsulko.com> | 2019-05-13 14:23:10 +0000 |
commit | 1fbfc103a8fba891ee20735e5355045e46ce184c (patch) | |
tree | 9c64c99c916dc8bd9f5fb84562043bd2c7b3f22c /network/network.cpp | |
parent | fbb1b3bfeefed2e5095265aad121ac741e9ff792 (diff) |
network: refactor network class
Refactor network class to prepare for wired
connections.
- extract Wifi model and flags into new
WifiAdapter class
- fix wifi networks disappearing for prolonged
periods after disconnecting from a wifi service
(this issue is intermittent, depends on the
wifi signal variability)
Bug-AGL: SPEC-2293
Change-Id: I75f3076d094daa9c8d04e05fe2944df98f5cfb81
Signed-off-by: Raquel Medina <raquel.medina@konsulko.com>
Diffstat (limited to 'network/network.cpp')
-rw-r--r-- | network/network.cpp | 174 |
1 files changed, 64 insertions, 110 deletions
diff --git a/network/network.cpp b/network/network.cpp index 496638f..e3a4545 100644 --- a/network/network.cpp +++ b/network/network.cpp @@ -14,8 +14,6 @@ * limitations under the License. */ -#include <QMetaEnum> -#include <QSortFilterProxyModel> #include <QtQml/QQmlEngine> #include <vcard/vcard.h> @@ -25,36 +23,25 @@ #include "network.h" #include "networkmessage.h" #include "responsemessage.h" -#include "wifinetworkmodel.h" +#include "networkadapter.h" Network::Network (QUrl &url, QQmlContext *context, QObject * parent) : QObject(parent), - m_mloop(nullptr), - m_wifi(nullptr), - m_wifiConnected(false), - m_wifiEnabled(false), - m_wifiStrength(0) + m_mloop(nullptr) { m_mloop = new MessageEngine(url); - m_wifi = new WifiNetworkModel(); - - QSortFilterProxyModel *m_model = new QSortFilterProxyModel(); - m_model->setSourceModel(m_wifi); - m_model->setSortRole(WifiNetworkModel::WifiNetworkRoles::SsidRole); - m_model->setSortCaseSensitivity(Qt::CaseInsensitive); - m_model->sort(0); - context->setContextProperty("WifiNetworkModel", m_model); + m_adapters.append(new WifiAdapter(this, context, 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); - QObject::connect(m_wifi, &WifiNetworkModel::strengthChanged, this, &Network::updateWifiStrength); } Network::~Network() { delete m_mloop; - delete m_wifi; + while (!m_adapters.isEmpty()) + m_adapters.takeLast(); } void Network::connect(QString service) @@ -93,6 +80,14 @@ void Network::remove(QString service) delete nmsg; } +void Network::power(bool on, QString type) +{ + if (on) + enableTechnology(type); + else + disableTechnology(type); +} + void Network::input(int id, QString passphrase) { NetworkMessage *nmsg = new NetworkMessage(); @@ -107,91 +102,52 @@ void Network::input(int id, QString passphrase) delete nmsg; } -void Network::power(bool on) -{ - if (on) - enableTechnology("wifi"); - else - disableTechnology("wifi"); -} - -void Network::enableTechnology(QString type) +void Network::getServices() { NetworkMessage *nmsg = new NetworkMessage(); QJsonObject parameter; - parameter.insert("technology", type); - nmsg->createRequest("enable_technology", parameter); + nmsg->createRequest("services", parameter); m_mloop->sendMessage(nmsg); - delete nmsg; } -void Network::disableTechnology(QString type) +AdapterIf* Network::findAdapter(QString type) { - NetworkMessage *nmsg = new NetworkMessage(); - QJsonObject parameter; - - parameter.insert("technology", type); - nmsg->createRequest("disable_technology", parameter); - m_mloop->sendMessage(nmsg); - - delete nmsg; + QList<AdapterIf*>::iterator iter; + for (iter = m_adapters.begin(); iter != m_adapters.end(); ++iter) + if ((*iter)->getType() == type) + return (*iter); } -void Network::scanServices(QString type) +void Network::updateServiceProperties(QJsonObject data) { - NetworkMessage *nmsg = new NetworkMessage(); - QJsonObject parameter; - - parameter.insert("technology", type); - nmsg->createRequest("scan_services", parameter); - m_mloop->sendMessage(nmsg); + QString service = data.value("service").toString(); + QJsonObject properties = data.value("properties").toObject(); + QList<AdapterIf*>::iterator iter; + for (iter = m_adapters.begin(); iter != m_adapters.end(); ++iter) + (*iter)->updateProperties(service, properties); - delete nmsg; } bool Network::addService(QJsonObject service) { - // Ignore services that are already added - QString id = service.value("service").toString(); - if (m_wifi->getNetwork(id)) - return false; - - QJsonObject properties = service.value("properties").toObject(); - - // Ignore hidden SSIDs - QString ssid = properties.value("name").toString(); - if (ssid == "") - return false; - - // Ignore technologies other than WiFi - QString type = properties.value("type").toString(); - if (type != "wifi") - return false; - - // 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(); - QString state = properties.value("state").toString(); - int strength = properties.value("strength").toInt(); - - WifiNetwork *network = new WifiNetwork(address, security, id, ssid, state, strength); - m_wifi->addNetwork(network); - - if ((state == "ready") || (state == "online")) - updateWifiStrength(strength); + QString id = service.value("service").toString(); + QJsonObject properties = service.value("properties").toObject(); + QList<AdapterIf*>::iterator iter; + for (iter = m_adapters.begin(); iter != m_adapters.end(); ++iter) + if ((*iter)->addService(id, properties)) + return true; - return true; + return false; } void Network::removeService(QJsonObject service) { QString id = service.value("service").toString(); - WifiNetwork *network = m_wifi->getNetwork(id); - - if (network) - m_wifi->removeNetwork(network); + QList<AdapterIf*>::iterator iter; + for (iter = m_adapters.begin(); iter != m_adapters.end(); ++iter) + (*iter)->removeService(id); } void Network::addServices(QJsonArray services) @@ -200,35 +156,40 @@ void Network::addServices(QJsonArray services) addService(service.toObject()); } -void Network::getServices() +void Network::scanServices(QString type) { NetworkMessage *nmsg = new NetworkMessage(); QJsonObject parameter; - nmsg->createRequest("services", parameter); + parameter.insert("technology", type); + nmsg->createRequest("scan_services", parameter); m_mloop->sendMessage(nmsg); + delete nmsg; } -void Network::updateWifiStatus(QJsonObject properties) +void Network::disableTechnology(QString type) { - if (properties.contains("connected")) { - m_wifiConnected = properties.value("connected").toBool(); - emit wifiConnectedChanged(m_wifiConnected); - } + NetworkMessage *nmsg = new NetworkMessage(); + QJsonObject parameter; - if (properties.contains("powered")) { - m_wifiEnabled = properties.value("powered").toBool(); - emit wifiEnabledChanged(m_wifiEnabled); - if (m_wifiEnabled) - getServices(); - } + parameter.insert("technology", type); + nmsg->createRequest("disable_technology", parameter); + m_mloop->sendMessage(nmsg); + + delete nmsg; } -void Network::updateWifiStrength(int strength) +void Network::enableTechnology(QString type) { - m_wifiStrength = strength; - emit wifiStrengthChanged(m_wifiStrength); + NetworkMessage *nmsg = new NetworkMessage(); + QJsonObject parameter; + + parameter.insert("technology", type); + nmsg->createRequest("enable_technology", parameter); + m_mloop->sendMessage(nmsg); + + delete nmsg; } void Network::parseTechnologies(QJsonArray technologies) @@ -237,11 +198,10 @@ void Network::parseTechnologies(QJsonArray technologies) QJsonObject technology = value.toObject(); QJsonObject properties = technology.value("properties").toObject(); QString type = properties.value("type").toString(); + if (type == "wifi") { - updateWifiStatus(properties); - if (m_wifiEnabled) - getServices(); - break; + WifiAdapter* wifi_a = static_cast<WifiAdapter*>(findAdapter(type)); + wifi_a->updateWifiStatus(properties); } } } @@ -256,13 +216,6 @@ void Network::getTechnologies() delete nmsg; } -void Network::updateServiceProperties(QJsonObject data) -{ - QString service = data.value("service").toString(); - QJsonObject properties = data.value("properties").toObject(); - m_wifi->updateProperties(service, properties); -} - void Network::processEvent(NetworkMessage *nmsg) { if (nmsg->eventName() == "agent") { @@ -284,7 +237,7 @@ void Network::processEvent(NetworkMessage *nmsg) if (action == "changed") { addService(service); } else if (action == "removed") { - removeService(service); + removeService(service); } } } else if (nmsg->eventName() == "service_properties") { @@ -293,7 +246,8 @@ void Network::processEvent(NetworkMessage *nmsg) QJsonObject technology = nmsg->eventData(); if (technology.value("technology").toString() == "wifi") { QJsonObject properties = technology.value("properties").toObject(); - updateWifiStatus(properties); + WifiAdapter* wifi_a = static_cast<WifiAdapter*>(findAdapter("wifi")); + wifi_a->updateWifiStatus(properties); } } } @@ -353,5 +307,5 @@ void Network::onDisconnected() delete nmsg; } - m_wifi->removeAllNetworks(); + getTechnologies(); } |