Commit 2be9875d authored by Pekka Vuorela's avatar Pekka Vuorela

Merge pull request #97 from pvuorela/master

refactor, cleanup, polish
parents ba458ea0 949b18c0
......@@ -2,7 +2,7 @@
QT = core network dbus
TARGET = connectionagent
PKGCONFIG += connman-qt5 qofono-qt5
PKGCONFIG += connman-qt5
packagesExist(qt5-boostable) {
DEFINES += HAS_BOOSTER
......@@ -30,15 +30,15 @@ SOURCES += main.cpp \
qconnectionagent.cpp \
wakeupwatcher.cpp
HEADERS+= \
HEADERS += \
qconnectionagent.h \
wakeupwatcher.h
target.path = /usr/bin
INSTALLS += target
MOC_DIR=.moc
OBJECTS_DIR=.obj
MOC_DIR = .moc
OBJECTS_DIR = .obj
......@@ -18,7 +18,6 @@
#include <QTimer>
#include <QtGlobal>
#include <QDebug>
#include <QDBusConnection>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
......@@ -79,13 +78,13 @@ static void daemonize(void)
}
static QtMessageHandler previousMessageHandler;
bool toggleDebug;
static bool toggleDebug;
void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &str)
{
{
if (toggleDebug)
previousMessageHandler(type,context,str);
}
}
Q_DECL_EXPORT int main(int argc, char *argv[])
{
......@@ -104,13 +103,13 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
QCoreApplication::setApplicationVersion("1.0");
QCoreApplication a(argc, argv);
if (QDBusConnection::sessionBus().interface()->isServiceRegistered("com.jolla.Connectiond")) {
qDebug() << "Connectionagent service is already running. Exiting now";
QConnectionAgent agent;
if (!agent.isValid()) {
qDebug() << "Connectionagent service failed to register. Exiting now";
return 1;
}
QConnectionAgent::instance();
return a.exec();
}
......@@ -23,20 +23,12 @@
#include <connman-qt5/networktechnology.h>
#include <connman-qt5/networkservice.h>
#include <connman-qt5/sessionagent.h>
#include <qofono-qt5/qofonoconnectioncontext.h>
#include <qofono-qt5/qofonoconnectionmanager.h>
#include <qofono-qt5/qofononetworkregistration.h>
#include <qofono-qt5/qofonomanager.h>
#include <QtDBus/QDBusConnection>
#include <QObject>
#include <QSettings>
#define CONNMAN_1_21
QConnectionAgent* QConnectionAgent::self = NULL;
#define CONND_SERVICE "com.jolla.Connectiond"
#define CONND_PATH "/Connectiond"
#define CONND_SESSION_PATH = "/ConnectionSession"
......@@ -48,30 +40,31 @@ QConnectionAgent::QConnectionAgent(QObject *parent) :
currentNetworkState(QString()),
isEthernet(false),
connmanAvailable(false),
oContext(0),
tetheringWifiTech(0),
tetheringEnabled(false),
flightModeSuppression(false),
scanTimeoutInterval(1),
delayedTethering(false)
delayedTethering(false),
valid(true)
{
qDebug() << Q_FUNC_INFO;
connect(netman,SIGNAL(availabilityChanged(bool)),this,SLOT(connmanAvailabilityChanged(bool)));
connectionAdaptor = new ConnAdaptor(this);
new ConnAdaptor(this);
QDBusConnection dbus = QDBusConnection::sessionBus();
if (!dbus.registerService(CONND_SERVICE)) {
qDebug() << "XXXXXXXXXXX could not register service XXXXXXXXXXXXXXXXXX";
valid = false;
}
if (!dbus.registerObject(CONND_PATH, this)) {
qDebug() << "XXXXXXXXXXX could not register object XXXXXXXXXXXXXXXXXX";
valid = false;
}
connect(this,SIGNAL(configurationNeeded(QString)),this,SLOT(openConnectionDialog(QString)));
connect(netman,SIGNAL(availabilityChanged(bool)),this,SLOT(connmanAvailabilityChanged(bool)));
connect(netman,SIGNAL(servicesListChanged(QStringList)),this,SLOT(servicesListChanged(QStringList)));
connect(netman,SIGNAL(stateChanged(QString)),this,SLOT(networkStateChanged(QString)));
connect(netman,SIGNAL(offlineModeChanged(bool)),this,SLOT(offlineModeChanged(bool)));
......@@ -102,23 +95,17 @@ QConnectionAgent::QConnectionAgent(QObject *parent) :
scanTimer = new QTimer(this);
connect(scanTimer,SIGNAL(timeout()),this,SLOT(scanTimeout()));
scanTimer->setSingleShot(true);
if (connmanAvailable)
if (connmanAvailable && valid)
setup();
}
QConnectionAgent::~QConnectionAgent()
{
delete self;
}
QConnectionAgent & QConnectionAgent::instance()
bool QConnectionAgent::isValid() const
{
qDebug() << Q_FUNC_INFO;
if (!self) {
self = new QConnectionAgent;
}
return *self;
return valid;
}
// from useragent
......@@ -387,20 +374,6 @@ void QConnectionAgent::servicesError(const QString &errorMessage)
Q_EMIT onErrorReported(serv->path(), errorMessage);
}
void QConnectionAgent::ofonoServicesError(const QString &errorMessage)
{
QOfonoConnectionContext *context = static_cast<QOfonoConnectionContext *>(sender());
QVector<NetworkService*> services = netman->getServices("cellular");
Q_FOREACH (NetworkService *serv, services) {
if (context->contextPath().contains(serv->path().section("_",2,2))) {
Q_EMIT onErrorReported(serv->path(), errorMessage);
qDebug() << serv->name() << errorMessage;
return;
}
}
qWarning() << "ofono error but could not discover connman service";
}
void QConnectionAgent::networkStateChanged(const QString &state)
{
qDebug() << state;
......@@ -448,12 +421,6 @@ void QConnectionAgent::connmanAvailabilityChanged(bool b)
}
}
void QConnectionAgent::serviceAdded(const QString &srv)
{
qDebug() << Q_FUNC_INFO << "<<<<"<< srv;
updateServices();
}
void QConnectionAgent::setup()
{
qDebug() << Q_FUNC_INFO
......@@ -462,9 +429,7 @@ void QConnectionAgent::setup()
if (connmanAvailable) {
qDebug() << Q_FUNC_INFO
<< netman->state();
if (ua)
delete ua;
delete ua;
ua = new UserAgent(this);
connect(ua,SIGNAL(userInputRequested(QString,QVariantMap)),
......@@ -595,8 +560,7 @@ void QConnectionAgent::offlineModeChanged(bool b)
void QConnectionAgent::flightModeDialogSuppressionTimeout()
{
if (flightModeSuppression)
flightModeSuppression = false;
flightModeSuppression = false;
}
void QConnectionAgent::displayStateChanged(const QString &state)
......@@ -641,7 +605,7 @@ bool QConnectionAgent::isBestService(NetworkService *service)
void QConnectionAgent::scanTimeout()
{
if (!tetheringWifiTech || tetheringWifiTech->tethering())
return;
return;
if (tetheringWifiTech->powered() && !tetheringWifiTech->connected() && netman->defaultRoute()->type() != "wifi" ) {
tetheringWifiTech->scan();
......
......@@ -31,10 +31,8 @@
class UserAgent;
class SessionAgent;
class ConnAdaptor;
class NetworkManager;
class NetworkService;
class QOfonoConnectionContext;
class NetworkTechnology;
class WakeupWatcher;
class QTimer;
......@@ -44,9 +42,10 @@ class QConnectionAgent : public QObject
Q_OBJECT
public:
explicit QConnectionAgent(QObject *parent = 0);
~QConnectionAgent();
static QConnectionAgent &instance();
bool isValid() const;
Q_SIGNALS:
......@@ -82,41 +81,45 @@ private:
class Service
{
public:
QString path;
NetworkService *service;
QString path;
NetworkService *service;
bool operator==(const Service &other) const {
return other.path == path;
}
bool operator==(const Service &other) const {
return other.path == path;
}
};
class ServiceList : public QVector<Service>
{
public:
int indexOf(const QString &path, int from = 0) const {
Service key;
key.path = path;
return QVector<Service>::indexOf(key, from);
}
bool contains(const QString &path) const {
Service key;
key.path = path;
return QVector<Service>::indexOf(key) >= 0;
}
void remove(const QString &path) {
Service key;
key.path = path;
int pos = QVector<Service>::indexOf(key);
if (pos >= 0)
QVector<Service>::remove(pos);
}
int indexOf(const QString &path, int from = 0) const {
Service key;
key.path = path;
return QVector<Service>::indexOf(key, from);
}
bool contains(const QString &path) const {
Service key;
key.path = path;
return QVector<Service>::indexOf(key) >= 0;
}
void remove(const QString &path) {
Service key;
key.path = path;
int pos = QVector<Service>::indexOf(key);
if (pos >= 0)
QVector<Service>::remove(pos);
}
};
explicit QConnectionAgent(QObject *parent = 0);
static QConnectionAgent *self;
ConnAdaptor *connectionAdaptor;
void setup();
void updateServices();
bool isStateOnline(const QString &state);
bool isBestService(NetworkService *service);
QString findBestConnectableService();
void removeAllTypes(const QString &type);
UserAgent *ua;
NetworkManager *netman;
......@@ -130,8 +133,6 @@ private:
bool isEthernet;
bool connmanAvailable;
bool isStateOnline(const QString &state);
QOfonoConnectionContext *oContext;
NetworkTechnology *tetheringWifiTech;
bool tetheringEnabled;
bool flightModeSuppression;
......@@ -140,29 +141,23 @@ private:
QTimer *scanTimer;
QStringList knownTechnologies;
bool isBestService(NetworkService *service);
QString findBestConnectableService();
void removeAllTypes(const QString &type);
bool tetheringStarted;
bool delayedTethering;
bool valid;
private slots:
void onScanFinished();
void updateServices();
void serviceErrorChanged(const QString &error);
void serviceStateChanged(const QString &state);
void networkStateChanged(const QString &state);
void connmanAvailabilityChanged(bool b);
void setup();
void servicesError(const QString &);
void ofonoServicesError(const QString &);
void technologyPowerChanged(bool);
void browserRequest(const QString &servicePath, const QString &url);
void techChanged();
void serviceAdded(const QString &);
void servicesListChanged(const QStringList &);
void offlineModeChanged(bool);
void flightModeDialogSuppressionTimeout();
......
......@@ -19,6 +19,18 @@
#include "wakeupwatcher.h"
#define MCE_SERVICE "com.nokia.mce"
#define MCE_SIGNAL_PATH "/com/nokia/mce/signal"
#define MCE_SIGNAL_INTERFACE "com.nokia.mce.signal"
#define MCE_PSM_STATE_IND "psm_state_ind"
#define MCE_DISPLAY_IND "display_status_ind"
#define MCE_REQUEST_PATH "/com/nokia/mce/request"
#define MCE_REQUEST_INTERFACE "com.nokia.mce.request"
#define MCE_DISPLAY_STATUS_GET "get_display_status"
#define MCE_PSM_STATE_GET "get_psm_state"
WakeupWatcher::WakeupWatcher(QObject *parent) :
QObject(parent),
currentPowerSave(false)
......
......@@ -20,17 +20,6 @@
#include <QObject>
#include <QDBusInterface>
#define MCE_SERVICE "com.nokia.mce"
#define MCE_SIGNAL_PATH "/com/nokia/mce/signal"
#define MCE_SIGNAL_INTERFACE "com.nokia.mce.signal"
#define MCE_PSM_STATE_IND "psm_state_ind"
#define MCE_DISPLAY_IND "display_status_ind"
#define MCE_REQUEST_PATH "/com/nokia/mce/request"
#define MCE_REQUEST_INTERFACE "com.nokia.mce.request"
#define MCE_DISPLAY_STATUS_GET "get_display_status"
#define MCE_PSM_STATE_GET "get_psm_state"
class WakeupWatcher : public QObject
{
Q_OBJECT
......@@ -40,11 +29,11 @@ public:
signals:
void displayStateChanged(const QString&);
void sleepStateChanged(bool);
public slots:
private slots:
void mceDisplayStateChanged(const QString &state);
void mceSleepStateChanged(bool mode);
private:
QDBusInterface *mceInterface;
QString currentDisplayState;
......
......@@ -8,7 +8,4 @@ SUBDIRS = \
test.depends = connd # xml interface
OTHER_FILES += rpm/connectionagent-qt5.spec \
rpm/connectionagent-qt5.yaml \
rpm/connectionagent.tracing
OTHER_FILES += rpm/connectionagent-qt5.spec
......@@ -6,12 +6,11 @@ CONFIG += qt plugin
uri = com.jolla.connection
SOURCES += \
connectionagentplugin_plugin.cpp \
connectionagentplugin.cpp
plugin.cpp \
declarativeconnectionagent.cpp
HEADERS += \
connectionagentplugin_plugin.h \
connectionagentplugin.h
declarativeconnectionagent.h
DBUS_INTERFACES = connectiond_interface
connectiond_interface.files = ../connd/com.jollamobile.Connectiond.xml
......
/****************************************************************************
**
** Copyright (C) 2013 Jolla Ltd
** Contact: lorn.potter@gmail.com
**
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
****************************************************************************/
#include "connectionagentplugin_plugin.h"
#include "connectionagentplugin.h"
/*
*This class is for accessing connman's UserAgent from multiple sources.
*This is because currently, there can only be one UserAgent per system.
*
*It also makes use of a patch to connman, that allows the UserAgent
*to get signaled when a connection is needed. This is the real reason
*this daemon is needed. An InputRequest is short lived, and thus, may
*not clash with other apps that need to use UserAgent.
*
*When you are trying to intercept a connection request, you need a long
*living process to wait until such time. This will immediately clash if
*a wlan needs user Input signal from connman, and the configure will never
*get the proper signal.
*
*This qml type can be used as such:
*
*import com.jolla.connection 1.0
*
* ConnectionAgent {
* id: userAgent
* onUserInputRequested: {
* console.log(" onUserInputRequested:")
* }
*
* onConnectionRequest: {
* console.log("onConnectionRequest ")
* sendSuppress()
* }
* onErrorReported: {
* console.log("Got error from connman: " + error);
* }
* }
*
**/
void ConnectionagentpluginPlugin::registerTypes(const char *uri)
{
// @uri com.jolla.connection
qmlRegisterType<ConnectionAgentPlugin>(uri, 1, 0, "ConnectionAgent");
}
......@@ -14,7 +14,7 @@
**
****************************************************************************/
#include "connectionagentplugin.h"
#include "declarativeconnectionagent.h"
#include "connectiond_interface.h"
#include <connman-qt5/networkmanager.h>
......@@ -26,7 +26,7 @@
#define CONND_SERVICE "com.jolla.Connectiond"
#define CONND_PATH "/Connectiond"
ConnectionAgentPlugin::ConnectionAgentPlugin(QObject *parent):
DeclarativeConnectionAgent::DeclarativeConnectionAgent(QObject *parent):
QObject(parent),
connManagerInterface(0)
{
......@@ -42,11 +42,11 @@ ConnectionAgentPlugin::ConnectionAgentPlugin(QObject *parent):
connectToConnectiond();
}
ConnectionAgentPlugin::~ConnectionAgentPlugin()
DeclarativeConnectionAgent::~DeclarativeConnectionAgent()
{
}
void ConnectionAgentPlugin::connectToConnectiond(QString)
void DeclarativeConnectionAgent::connectToConnectiond(QString)
{
if (connManagerInterface) {
delete connManagerInterface;
......@@ -92,7 +92,7 @@ void ConnectionAgentPlugin::connectToConnectiond(QString)
this,SLOT(onTetheringFinished(bool)));
}
void ConnectionAgentPlugin::sendUserReply(const QVariantMap &input)
void DeclarativeConnectionAgent::sendUserReply(const QVariantMap &input)
{
if (!connManagerInterface || !connManagerInterface->isValid()) {
Q_EMIT errorReported("","ConnectionAgent not available");
......@@ -106,7 +106,7 @@ void ConnectionAgentPlugin::sendUserReply(const QVariantMap &input)
}
}
void ConnectionAgentPlugin::sendConnectReply(const QString &replyMessage, int timeout)
void DeclarativeConnectionAgent::sendConnectReply(const QString &replyMessage, int timeout)
{
if (!connManagerInterface || !connManagerInterface->isValid()) {
Q_EMIT errorReported("","ConnectionAgent not available");
......@@ -115,7 +115,7 @@ void ConnectionAgentPlugin::sendConnectReply(const QString &replyMessage, int ti
connManagerInterface->sendConnectReply(replyMessage,timeout);
}
void ConnectionAgentPlugin::connectToType(const QString &type)
void DeclarativeConnectionAgent::connectToType(const QString &type)
{
if (!connManagerInterface || !connManagerInterface->isValid()) {
Q_EMIT errorReported("","ConnectionAgent not available");
......@@ -125,18 +125,18 @@ void ConnectionAgentPlugin::connectToType(const QString &type)
connManagerInterface->connectToType(type);
}
void ConnectionAgentPlugin::onErrorReported(const QString &servicePath, const QString &error)
void DeclarativeConnectionAgent::onErrorReported(const QString &servicePath, const QString &error)
{
Q_EMIT errorReported(servicePath, error);
}
void ConnectionAgentPlugin::onRequestBrowser(const QString &url)
void DeclarativeConnectionAgent::onRequestBrowser(const QString &url)
{
qDebug() << Q_FUNC_INFO <<url;
Q_EMIT browserRequested(url);
}
void ConnectionAgentPlugin::onUserInputRequested(const QString &service, const QVariantMap &fields)
void DeclarativeConnectionAgent::onUserInputRequested(const QString &service, const QVariantMap &fields)
{
// we do this as qtdbus does not understand QVariantMap very well.
// we need to manually demarshall
......@@ -152,13 +152,13 @@ void ConnectionAgentPlugin::onUserInputRequested(const QString &service, const Q
Q_EMIT userInputRequested(service, map);
}
void ConnectionAgentPlugin::onConnectionRequested()
void DeclarativeConnectionAgent::onConnectionRequested()
{
qDebug() << Q_FUNC_INFO;
Q_EMIT connectionRequest();
}
void ConnectionAgentPlugin::connectiondUnregistered(QString)
void DeclarativeConnectionAgent::connectiondUnregistered(QString)
{
if (connManagerInterface) {
delete connManagerInterface;
......@@ -166,23 +166,23 @@ void ConnectionAgentPlugin::connectiondUnregistered(QString)
}
}
void ConnectionAgentPlugin::onConnectionState(const QString &state, const QString &type)
void DeclarativeConnectionAgent::onConnectionState(const QString &state, const QString &type)
{
qDebug() << Q_FUNC_INFO << state;
Q_EMIT connectionState(state, type);
}
void ConnectionAgentPlugin::startTethering(const QString &type)
void DeclarativeConnectionAgent::startTethering(const QString &type)
{
connManagerInterface->startTethering(type);
}
void ConnectionAgentPlugin::onTetheringFinished(bool success)
void DeclarativeConnectionAgent::onTetheringFinished(bool success)
{
Q_EMIT tetheringFinished(success);
}
void ConnectionAgentPlugin::stopTethering(bool keepPowered)
void DeclarativeConnectionAgent::stopTethering(bool keepPowered)
{
connManagerInterface->stopTethering(keepPowered);
}
......@@ -14,22 +14,56 @@
**
****************************************************************************/
#ifndef CONNECTIONAGENTPLUGIN_H
#define CONNECTIONAGENTPLUGIN_H
#ifndef DECLARATIVECONNECTIONAGENT_H
#define DECLARATIVECONNECTIONAGENT_H
#include "connectionagentplugin.h"
#include "declarativeconnectionagent.h"
#include "connectiond_interface.h"
class NetworkManager;
class ConnectionAgentPlugin : public QObject
/*
*This class is for accessing connman's UserAgent from multiple sources.
*This is because currently, there can only be one UserAgent per system.
*
*It also makes use of a patch to connman, that allows the UserAgent
*to get signaled when a connection is needed. This is the real reason
*this daemon is needed. An InputRequest is short lived, and thus, may
*not clash with other apps that need to use UserAgent.
*
*When you are trying to intercept a connection request, you need a long
*living process to wait until such time. This will immediately clash if
*a wlan needs user Input signal from connman, and the configure will never
*get the proper signal.
*
*This qml type can be used as such:
*
*import com.jolla.connection 1.0
*
* ConnectionAgent {
* id: userAgent
* onUserInputRequested: {
* console.log(" onUserInputRequested:")
* }
*
* onConnectionRequest: {
* console.log("onConnectionRequest ")
* sendSuppress()
* }
* onErrorReported: {
* console.log("Got error from connman: " + error);
* }
* }
*
**/
class DeclarativeConnectionAgent : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(ConnectionAgentPlugin)
Q_DISABLE_COPY(DeclarativeConnectionAgent)
public:
explicit ConnectionAgentPlugin(QObject *parent = 0);
~ConnectionAgentPlugin();
explicit DeclarativeConnectionAgent(QObject *parent = 0);
~DeclarativeConnectionAgent();
public slots:
void sendUserReply(const QVariantMap &input);
......@@ -64,5 +98,5 @@ private slots:
void connectiondUnregistered(const QString = QString());
};
#endif // CONNECTIONAGENTPLUGIN_H
#endif
......@@ -14,15 +14,15 @@
**
****************************************************************************/
#ifndef CONNECTIONAGENTPLUGIN_PLUGIN_H
#define CONNECTIONAGENTPLUGIN_PLUGIN_H
#include "declarativeconnectionagent.h"
#include <QtPlugin>
#include <QtQml>
#include <QQmlEngine>
#include <QQmlExtensionPlugin>
class ConnectionagentpluginPlugin : public QQmlExtensionPlugin
class ConnectionagentPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
......@@ -32,5 +32,10 @@ public:
};
#endif // CONNECTIONAGENTPLUGIN_PLUGIN_H
void ConnectionagentPlugin::registerTypes(const char *uri)
{
// @uri com.jolla.connection
qmlRegisterType<DeclarativeConnectionAgent>(uri, 1, 0, "ConnectionAgent");
}
#include "plugin.moc"
......@@ -14,7 +14,6 @@ Requires: connman >= 1.21
BuildRequires: pkgconfig(Qt5Core)
BuildRequires: pkgconfig(Qt5DBus)
BuildRequires: pkgconfig(connman-qt5)
BuildRequires: pkgconfig(qofono-qt5)
BuildRequires: pkgconfig(Qt5Network)
BuildRequires: pkgconfig(Qt5Test)
BuildRequires: pkgconfig(Qt5Qml)
......@@ -98,7 +97,7 @@ fi
%files test
%defattr(-,root,root,-)
%{_prefix}/opt/tests/libqofono/*
%{_prefix}/opt/tests/connectionagent/*
%files tracing
%defattr(-,root,root,-)
......
......@@ -33,45 +33,23 @@ class Tst_connectionagent : public QObject
{
Q_OBJECT
public:
Tst_connectionagent();
private Q_SLOTS:
void tst_networkInstance();
void tst_restartconnman();