Skip to content

Commit

Permalink
Merge pull request #5 from sailfishos/shared_networkmanager
Browse files Browse the repository at this point in the history
Shared networkmanager API usage
  • Loading branch information
pvuorela committed Apr 2, 2024
2 parents 2a8b511 + c1957ba commit 37f76a0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 57 deletions.
22 changes: 8 additions & 14 deletions connd/qconnectionagent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "connectiond_adaptor.h"

#include <connman-qt5/useragent.h>
#include <connman-qt5/networkmanager.h>
#include <connman-qt5/networktechnology.h>
#include <connman-qt5/networkservice.h>

Expand All @@ -35,7 +34,7 @@ Q_LOGGING_CATEGORY(connAgent, "org.sailfishos.connectionagent", QtWarningMsg)
QConnectionAgent::QConnectionAgent(QObject *parent) :
QObject(parent),
ua(0),
netman(NetworkManagerFactory::createInstance()),
netman(NetworkManager::sharedInstance()),
currentNetworkState(QString()),
isEthernet(false),
connmanAvailable(false),
Expand All @@ -62,14 +61,14 @@ QConnectionAgent::QConnectionAgent(QObject *parent) :

connect(this, &QConnectionAgent::configurationNeeded, this, &QConnectionAgent::openConnectionDialog);

connect(netman, &NetworkManager::availabilityChanged, this, &QConnectionAgent::connmanAvailabilityChanged);
connect(netman, &NetworkManager::servicesListChanged, this, &QConnectionAgent::servicesListChanged);
connect(netman, &NetworkManager::stateChanged, this, &QConnectionAgent::networkStateChanged);
connect(netman, &NetworkManager::offlineModeChanged, this, &QConnectionAgent::offlineModeChanged);
connect(netman, &NetworkManager::servicesChanged, this, [=]() {
connect(netman.data(), &NetworkManager::availabilityChanged, this, &QConnectionAgent::connmanAvailabilityChanged);
connect(netman.data(), &NetworkManager::servicesListChanged, this, &QConnectionAgent::servicesListChanged);
connect(netman.data(), &NetworkManager::stateChanged, this, &QConnectionAgent::networkStateChanged);
connect(netman.data(), &NetworkManager::offlineModeChanged, this, &QConnectionAgent::offlineModeChanged);
connect(netman.data(), &NetworkManager::servicesChanged, this, [=]() {
updateServices();
});
connect(netman, &NetworkManager::technologiesChanged, this, &QConnectionAgent::techChanged);
connect(netman.data(), &NetworkManager::technologiesChanged, this, &QConnectionAgent::techChanged);

QFile connmanConf("/etc/connman/main.conf");
if (connmanConf.open(QIODevice::ReadOnly | QIODevice::Text)) {
Expand Down Expand Up @@ -262,9 +261,6 @@ void QConnectionAgent::serviceStateChanged(const QString &state)
// from plugin/qml
void QConnectionAgent::connectToType(const QString &type)
{
if (!netman)
return;

if (netman->technologyPathForType(type).isEmpty()) {
Q_EMIT errorReported("","Type not valid");
return;
Expand Down Expand Up @@ -470,8 +466,6 @@ void QConnectionAgent::technologyPowerChanged(bool powered)

void QConnectionAgent::techChanged()
{
if (!netman)
return;
if (netman->getTechnologies().isEmpty()) {
knownTechnologies.clear();
}
Expand Down Expand Up @@ -625,7 +619,7 @@ void QConnectionAgent::openConnectionDialog(const QString &type)

void QConnectionAgent::startTethering(const QString &type)
{
if (!netman | (type != "wifi" && type !="bluetooth")) { // support wifi and bt
if (type != "wifi" && type !="bluetooth") { // support wifi and bt
return;
}
qCDebug(connAgent) << "startTethering" << type;
Expand Down
5 changes: 3 additions & 2 deletions connd/qconnectionagent.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
#include <QVector>
#include <QLoggingCategory>

#include "networkmanager.h"

class UserAgent;
class NetworkManager;
class NetworkService;
class NetworkTechnology;
class QTimer;
Expand Down Expand Up @@ -110,7 +111,7 @@ public Q_SLOTS:
bool shouldSuppressError(const QString &error, bool cellular) const;

UserAgent *ua;
NetworkManager *netman;
QSharedPointer<NetworkManager> netman;
QString currentNetworkState;
ServiceList orderedServicesList;
QStringList techPreferenceList;
Expand Down
6 changes: 3 additions & 3 deletions connectionagentplugin/declarativeconnectionagent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ DeclarativeConnectionAgent::DeclarativeConnectionAgent(QObject *parent)
: QObject(parent),
connManagerInterface(nullptr)
{
connectiondWatcher = new QDBusServiceWatcher(CONND_SERVICE,QDBusConnection::sessionBus(),
QDBusServiceWatcher::WatchForRegistration |
QDBusServiceWatcher::WatchForUnregistration, this);
connectiondWatcher = new QDBusServiceWatcher(CONND_SERVICE, QDBusConnection::sessionBus(),
QDBusServiceWatcher::WatchForRegistration
| QDBusServiceWatcher::WatchForUnregistration, this);

connect(connectiondWatcher, &QDBusServiceWatcher::serviceRegistered,
this, &DeclarativeConnectionAgent::connectToConnectiond);
Expand Down
26 changes: 3 additions & 23 deletions test/auto/tst_connectionagent/tst_connectionagent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Tst_connectionagent : public QObject

private Q_SLOTS:
void tst_onErrorReported();
void tst_onConnectionRequest();

private:
QConnectionAgent agent;
Expand All @@ -44,41 +43,22 @@ private Q_SLOTS:
void Tst_connectionagent::tst_onErrorReported()
{
QSignalSpy spy(&agent, SIGNAL(errorReported(QString,QString)));
agent.onErrorReported("test_path","Test error");
agent.onErrorReported("test_path", "Test error");

QCOMPARE(spy.count(),1);
QCOMPARE(spy.count(), 1);
QList<QVariant> arguments;
arguments = spy.takeFirst();
QCOMPARE(arguments.at(0).toString(), QString("test_path"));
QCOMPARE(arguments.at(1).toString(), QString("Test error"));

agent.connectToType("test");
QCOMPARE(spy.count(),1);
QCOMPARE(spy.count(), 1);
arguments = spy.takeFirst();
QCOMPARE(arguments.at(0).toString(), QString(""));
QCOMPARE(arguments.at(1).toString(), QString("Type not valid"));

}

void Tst_connectionagent::tst_onConnectionRequest()
{
NetworkManager *netman = NetworkManagerFactory::createInstance();
QString currentState = netman->state();
if (currentState == "online") {
NetworkService *service = netman->defaultRoute();
service->requestDisconnect();
// service->requestConnect();
}
QSignalSpy spy(&agent, SIGNAL(connectionRequest()));
agent.onConnectionRequest();

if (currentState == "online")
QCOMPARE(spy.count(),0);
else
QCOMPARE(spy.count(),0);

}

QTEST_APPLESS_MAIN(Tst_connectionagent)

#include "tst_connectionagent.moc"
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ private Q_SLOTS:

private:
DeclarativeConnectionAgent *plugin;
NetworkManager *netman;
QSharedPointer<NetworkManager> netman;
};

Tst_connectionagent_pluginTest::Tst_connectionagent_pluginTest()
{
plugin = new DeclarativeConnectionAgent(this);
netman = NetworkManagerFactory::createInstance();
netman = NetworkManager::sharedInstance();
QTest::qWait(5000);
}

Expand All @@ -74,7 +74,6 @@ void Tst_connectionagent_pluginTest::testRequestConnection_data()
void Tst_connectionagent_pluginTest::testRequestConnection()
{
QFETCH(QString, tech);
NetworkManager *netman = NetworkManagerFactory::createInstance();

QString techPath = netman->technologyPathForType(tech);
NetworkTechnology netTech;
Expand All @@ -99,10 +98,8 @@ void Tst_connectionagent_pluginTest::testRequestConnection()
qDebug() << reply->error();
}
QTest::qWait(2000);
QCOMPARE(spy2.count(),1);
plugin->sendConnectReply("Clear",0);


QCOMPARE(spy2.count(), 1);
plugin->sendConnectReply("Clear", 0);
}

void Tst_connectionagent_pluginTest::testErrorReported()
Expand All @@ -123,7 +120,6 @@ void Tst_connectionagent_pluginTest::testUserInputRequested_data()
void Tst_connectionagent_pluginTest::testUserInputRequested()
{
QFETCH(QString, tech);
NetworkManager *netman = NetworkManagerFactory::createInstance();

QString techPath = netman->technologyPathForType(tech);
NetworkTechnology netTech;
Expand All @@ -146,12 +142,11 @@ void Tst_connectionagent_pluginTest::testUserInputRequested()
}

QTest::qWait(2000);
QCOMPARE(spy_userInput.count(),1);
QCOMPARE(spy_userInput.count(), 1);
QVariantMap map;
plugin->sendUserReply(map); //cancel
}


void Tst_connectionagent_pluginTest::tst_tethering()
{
NetworkService *wlanService = 0;
Expand Down Expand Up @@ -181,7 +176,7 @@ void Tst_connectionagent_pluginTest::tst_tethering()
QVERIFY(wifiSpy.isValid());
QVERIFY(wifiSpy.wait(7000));

QCOMPARE(wifiSpy.count(),1);
QCOMPARE(wifiSpy.count(), 1);
QList<QVariant> arguments;
arguments = wifiSpy.takeFirst();
QCOMPARE(arguments.at(0).toBool(), true);
Expand All @@ -193,7 +188,7 @@ void Tst_connectionagent_pluginTest::tst_tethering()
plugin->stopTethering("wifi");
QTest::qWait(2500);

QCOMPARE(wifiSpy.count(),1);
QCOMPARE(wifiSpy.count(), 1);
arguments = wifiSpy.takeFirst();
QCOMPARE(arguments.at(0).toBool(), false);

Expand All @@ -211,15 +206,15 @@ void Tst_connectionagent_pluginTest::tst_tethering()
QVERIFY(btSpy.isValid());
QVERIFY(btSpy.wait(7000));

QCOMPARE(btSpy.count(),1);
QCOMPARE(btSpy.count(), 1);
QList<QVariant> arguments;
arguments = btSpy.takeFirst();
QCOMPARE(arguments.at(0).toBool(), true);

plugin->stopTethering("bluetooth");
QTest::qWait(2500);

QCOMPARE(btSpy.count(),1);
QCOMPARE(btSpy.count(), 1);
arguments = btSpy.takeFirst();
QCOMPARE(arguments.at(0).toBool(), false);
}
Expand All @@ -232,7 +227,6 @@ void Tst_connectionagent_pluginTest::tst_tethering()
// arguments = spy.takeFirst();
// QCOMPARE(arguments.at(0).toBool(), false);

// NetworkManager *netman = NetworkManagerFactory::createInstance();
// NetworkService *cellServices = netman->getServices("cellular").at(0);
// QVERIFY(cellServices->state() == "idle");
}
Expand Down

0 comments on commit 37f76a0

Please sign in to comment.