Qt 4.8
qconnmanengine.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qconnmanengine.h"
44 #include "qofonoservice_linux_p.h"
45 #include "../qnetworksession_impl.h"
46 
47 #include <QtNetwork/private/qnetworkconfiguration_p.h>
48 
49 #include <QtNetwork/qnetworksession.h>
50 
51 #include <QtCore/qdebug.h>
52 
53 #include <QtDBus/QtDBus>
54 #include <QtDBus/QDBusConnection>
55 #include <QtDBus/QDBusInterface>
56 #include <QtDBus/QDBusMessage>
57 #include <QtDBus/QDBusReply>
58 
59 #ifndef QT_NO_BEARERMANAGEMENT
60 #ifndef QT_NO_DBUS
61 
63 
65 : QBearerEngineImpl(parent),
66  connmanManager(new QConnmanManagerInterface(this))
67 {
68 }
69 
71 {
72 }
73 
75 {
76  QMutexLocker locker(&mutex);
77  return connmanManager->isValid();
78 }
79 
81 {
84 
85  foreach(const QString techPath, connmanManager->getTechnologies()) {
87  tech = new QConnmanTechnologyInterface(techPath, this);
88 
91  }
92 
93  foreach(const QString servPath, connmanManager->getServices()) {
94  addServiceConfiguration(servPath);
95  }
96 
97  // Get current list of access points.
99 }
100 
102 {
103  QMutexLocker locker(&mutex);
104  QList<QNetworkConfigurationPrivate *> fetchedConfigurations;
105  QNetworkConfigurationPrivate* cpPriv = 0;
106 
107  for (int i = 0; i < foundConfigurations.count(); ++i) {
109  cpPriv = foundConfigurations.at(i);
110 
111  config->name = cpPriv->name;
112  config->isValid = cpPriv->isValid;
113  config->id = cpPriv->id;
114  config->state = cpPriv->state;
115  config->type = cpPriv->type;
116  config->roamingSupported = cpPriv->roamingSupported;
117  config->purpose = cpPriv->purpose;
118  config->bearerType = cpPriv->bearerType;
119 
120  fetchedConfigurations.append(config);
121  delete config;
122  }
123  return fetchedConfigurations;
124 }
125 
127 {
131 }
132 
134 {
135  QMutexLocker locker(&mutex);
136  return configInterfaces.value(id);
137 }
138 
140 {
141  QMutexLocker locker(&mutex);
142  return accessPointConfigurations.contains(id);
143 }
144 
146 {
147  QMutexLocker locker(&mutex);
148  QString servicePath = serviceFromId(id);
149  QConnmanServiceInterface serv(servicePath);
150  if(!serv.isValid()) {
152  } else {
153  if(serv.getType() != "cellular") {
154 
155  serv.connect();
156  } else {
157  QOfonoManagerInterface ofonoManager(0);
158  QString modemPath = ofonoManager.currentModem().path();
159  QOfonoDataConnectionManagerInterface dc(modemPath,0);
160  foreach(const QDBusObjectPath dcPath,dc.getPrimaryContexts()) {
161  if(dcPath.path().contains(servicePath.section("_",-1))) {
162  QOfonoPrimaryDataContextInterface primaryContext(dcPath.path(),0);
163  primaryContext.setActive(true);
164  }
165  }
166  }
167  }
168 }
169 
171 {
172  QMutexLocker locker(&mutex);
173  QString servicePath = serviceFromId(id);
174  QConnmanServiceInterface serv(servicePath);
175  if(!serv.isValid()) {
177  } else {
178  if(serv.getType() != "cellular") {
179  serv.disconnect();
180  } else {
181  QOfonoManagerInterface ofonoManager(0);
182  QString modemPath = ofonoManager.currentModem().path();
183  QOfonoDataConnectionManagerInterface dc(modemPath,0);
184  foreach(const QDBusObjectPath dcPath,dc.getPrimaryContexts()) {
185  if(dcPath.path().contains(servicePath.section("_",-1))) {
186  QOfonoPrimaryDataContextInterface primaryContext(dcPath.path(),0);
187  primaryContext.setActive(false);
188  }
189  }
190  }
191  }
192 }
193 
195 {
196  QMutexLocker locker(&mutex);
198 }
199 
201 {
202  QMutexLocker locker(&mutex);
203  foreach(const QString service, serviceNetworks) {
204  if (id == QString::number(qHash(service)))
205  return service;
206  }
207 
208  return QString();
209 }
210 
212 {
213  QMutexLocker locker(&mutex);
214 
216 
217  if (!ptr)
219 
220  if (!ptr->isValid) {
222 
223  }
224  QString service = serviceFromId(id);
225  QConnmanServiceInterface serv(service);
226  QString servState = serv.getState();
227 
228  if(serv.isFavorite() && (servState == "idle" || servState == "failure")) {
230  }
231 
232  if(servState == "association" || servState == "configuration" || servState == "login") {
234  }
235  if(servState == "ready" || servState == "online") {
237  }
238 
244  } else if ((ptr->state & QNetworkConfiguration::Undefined) ==
247  }
248 
250 }
251 
253 {//TODO use connman counter API
254  QMutexLocker locker(&mutex);
255  quint64 result = 0;
256  QString devFile = getInterfaceFromId(id);
257  QFile tx("/sys/class/net/"+devFile+"/statistics/tx_bytes");
258  if(tx.exists() && tx.open(QIODevice::ReadOnly | QIODevice::Text)) {
259  QTextStream in(&tx);
260  in >> result;
261  tx.close();
262  }
263 
264  return result;
265 }
266 
268 {//TODO use connman counter API
269  QMutexLocker locker(&mutex);
270  quint64 result = 0;
271  QString devFile = getInterfaceFromId(id);
272  QFile rx("/sys/class/net/"+devFile+"/statistics/rx_bytes");
273  if(rx.exists() && rx.open(QIODevice::ReadOnly | QIODevice::Text)) {
274  QTextStream in(&rx);
275  in >> result;
276  rx.close();
277  }
278  return result;
279 }
280 
282 {
283  // TODO
284  QMutexLocker locker(&mutex);
285  if (activeTime.isNull()) {
286  return 0;
287  }
289 }
290 
291 QNetworkConfigurationManager::Capabilities QConnmanEngine::capabilities() const
292 {
296 }
297 
299 {
300  return new QNetworkSessionPrivateImpl;
301 }
302 
304 {
306 }
307 
308 void QConnmanEngine::propertyChangedContext(const QString &path,const QString &item, const QDBusVariant &value)
309 {
310  Q_UNUSED(path);
311 
312  QMutexLocker locker(&mutex);
313  if(item == "Services") {
315  QStringList list = qdbus_cast<QStringList>(arg);
316 
317  if(list.count() > accessPointConfigurations.count()) {
318  foreach(const QString service, list) {
319  addServiceConfiguration(service);
320  }
321  }
322  }
323 
324  if(item == "Technologies") {
326  QStringList newlist = qdbus_cast<QStringList>(arg);
327  if(newlist.count() > 0) {
329 
330  foreach(const QString listPath, newlist) {
331  if(!oldtech.contains(listPath)) {
333  tech = new QConnmanTechnologyInterface(listPath,this);
336  technologies.insert(listPath, tech);
337  }
338  }
339  }
340  }
341  if(item == "State") {
342 // qDebug() << value.variant();
343  }
344 }
345 
347 {
348  QMutexLocker locker(&mutex);
349  if(item == "State") {
351 
352  if(value.variant().toString() == "failure") {
353  QConnmanServiceInterface serv(path);
355  }
356  }
357 }
358 
360 {
361  if(item == "State") {
362  if(value.variant().toString() == "offline") {
363  QConnmanTechnologyInterface tech(path);
366 
367  technologies.remove(path);
368  }
369  }
370 }
371 
373 {
374  QMutexLocker locker(&mutex);
375 
376  if (accessPointConfigurations.contains(id)) {
377 
379 
380  QString servicePath = serviceFromId(id);
382  serv = new QConnmanServiceInterface(servicePath);
383  QString networkName = serv->getName();
384 
385  QNetworkConfiguration::StateFlags curState = getStateForService(servicePath);
386 
387  ptr->mutex.lock();
388 
389  if (!ptr->isValid) {
390  ptr->isValid = true;
391  }
392 
393  if (ptr->name != networkName) {
394  ptr->name = networkName;
395  }
396 
397  if (ptr->state != curState) {
398  ptr->state = curState;
399  }
400 
401  ptr->mutex.unlock();
402 
403  locker.unlock();
405  locker.relock();
406  }
407 
408  locker.unlock();
410 }
411 
412 QNetworkConfiguration::StateFlags QConnmanEngine::getStateForService(const QString &service)
413 {
414  QMutexLocker locker(&mutex);
415  QConnmanServiceInterface serv(service);
416  QNetworkConfiguration::StateFlags flag = QNetworkConfiguration::Defined;
417  if( serv.getType() == "cellular") {
418  if(serv.isSetupRequired()) {
419  flag = ( flag | QNetworkConfiguration::Defined);
420  } else {
421  flag = ( flag | QNetworkConfiguration::Discovered);
422  }
423  } else {
424  if(serv.isFavorite()) {
425  flag = ( flag | QNetworkConfiguration::Discovered);
426  } else {
428  }
429  }
430 
431  if(serv.getState() == "ready" || serv.getState() == "online") {
432  flag = ( flag | QNetworkConfiguration::Active);
433  }
434 
435  return flag;
436 }
437 
439 {
440  if (type == "wifi")
442  if (type == "ethernet")
444  if (type == "bluetooth")
446  if (type == "cellular") {
447  return ofonoTechToBearerType(type);
448  }
449  if (type == "wimax")
451 
452 // if(type == "gps")
453 // if(type == "vpn")
454 
456 }
457 
459 {
460  QOfonoManagerInterface ofonoManager(this);
461  QOfonoNetworkRegistrationInterface ofonoNetwork(ofonoManager.currentModem().path(),this);
462 
463  if(ofonoNetwork.isValid()) {
464  foreach(const QDBusObjectPath op,ofonoNetwork.getOperators() ) {
465  QOfonoNetworkOperatorInterface opIface(op.path(),this);
466 
467  foreach(const QString opTech, opIface.getTechnologies()) {
468 
469  if(opTech == "gsm") {
471  }
472  if(opTech == "edge"){
473  return QNetworkConfiguration::BearerCDMA2000; //wrong, I know
474  }
475  if(opTech == "umts"){
477  }
478  if(opTech == "hspa"){
480  }
481  if(opTech == "lte"){
482  return QNetworkConfiguration::BearerWiMAX; //not exact
483  }
484  }
485  }
486  }
488 }
489 
491 {
492  QOfonoManagerInterface ofonoManager(this);
493  QString modemPath = ofonoManager.currentModem().path();
494  QOfonoDataConnectionManagerInterface dc(modemPath,this);
495  foreach(const QDBusObjectPath dcPath,dc.getPrimaryContexts()) {
496  if(dcPath.path().contains(context.section("_",-1))) {
497  return dc.isRoamingAllowed();
498  }
499  }
500  return false;
501 }
502 
504 {
505  QMutexLocker locker(&mutex);
506 
507  if (accessPointConfigurations.contains(id)) {
508 
509  QString service = serviceFromId(id);
510  QConnmanServiceInterface serv(service);
511 
514 
515  serviceNetworks.removeOne(service);
516 
518  locker.unlock();
520  locker.relock();
521  }
522 }
523 
525 {
526  QMutexLocker locker(&mutex);
528  serv = new QConnmanServiceInterface(servicePath);
529 
530  const QString id = QString::number(qHash(servicePath));
531 
532  if (!accessPointConfigurations.contains(id)) {
533  serviceNetworks.append(servicePath);
534 
538 
539  QString networkName = serv->getName();
540 
541  const QString connectionType = serv->getType();
542  if (connectionType == "ethernet") {
544  } else if (connectionType == "wifi") {
546  } else if (connectionType == "cellular") {
547  cpPriv->bearerType = ofonoTechToBearerType("cellular");
548  if(servicePath.isEmpty()) {
549  networkName = serv->getAPN();
550  if(networkName.isEmpty()) {
551  networkName = serv->getName();
552  }
553  }
554  cpPriv->roamingSupported = isRoamingAllowed(servicePath);
555  } else if (connectionType == "wimax") {
557  } else {
559  }
560 
561  cpPriv->name = networkName;
562  cpPriv->isValid = true;
563  cpPriv->id = id;
565 
566  if(serv->getSecurity() == "none") {
568  } else {
570  }
571 
572  cpPriv->state = getStateForService(servicePath);
573 
575  accessPointConfigurations.insert(ptr->id, ptr);
576  foundConfigurations.append(cpPriv);
577  configInterfaces[cpPriv->id] = serv->getInterface();
578 
579  locker.unlock();
581  locker.relock();
583  }
584 }
585 
587 {
588  return false;
589 }
590 
592 
593 #endif // QT_NO_DBUS
594 #endif // QT_NO_BEARERMANAGEMENT
BearerType
Specifies the type of bearer used by a configuration.
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
QString section(QChar sep, int start, int end=-1, SectionFlags flags=SectionDefault) const
This function returns a section of the string.
Definition: qstring.h:781
uint qHash(const QProcEnvKey &key)
Definition: qprocess_p.h:96
QList< QString > serviceNetworks
virtual quint64 bytesWritten(const QString &id)
int type
Definition: qmetatype.cpp:239
bool isNull() const
Returns true if both the date and the time are null; otherwise returns false.
Definition: qdatetime.cpp:2334
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void lock()
Locks the mutex.
Definition: qmutex.cpp:151
bool isRoamingAllowed(const QString &context)
T qdbus_cast(const QDBusArgument &arg)
virtual void disconnectFromId(const QString &id)
void addServiceConfiguration(const QString &servicePath)
QNetworkConfiguration::StateFlags state
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
The QDBusArgument class is used to marshall and demarshall D-Bus arguments.
Definition: qdbusargument.h:69
bool isValid() const
Returns true if this is a valid reference to a remote object.
QDBusObjectPath currentModem()
QNetworkConfiguration::StateFlags getStateForService(const QString &service)
void configurationChanged(QNetworkConfigurationPrivatePointer config)
#define SLOT(a)
Definition: qobjectdefs.h:226
void unlock()
Unlocks this mutex locker.
Definition: qmutex.h:117
virtual quint64 startTime(const QString &id)
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
void updateCompleted()
State
This enum describes the connectivity state of the session.
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool requiresPolling() const
QMap< QString, QString > configInterfaces
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QNetworkConfiguration::BearerType bearerType
QDateTime activeTime
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
void relock()
Relocks an unlocked mutex locker.
Definition: qmutex.h:125
QConnmanManagerInterface * connmanManager
bool removeOne(const T &t)
Removes the first occurrence of value in the list and returns true on success; otherwise returns fals...
Definition: qlist.h:796
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QList< QNetworkConfigurationPrivate * > getConfigurations()
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void technologyPropertyChangedContext(const QString &, const QString &, const QDBusVariant &)
QHash< QString, QNetworkConfigurationPrivatePointer > accessPointConfigurations
unsigned __int64 quint64
Definition: qglobal.h:943
virtual QString getInterfaceFromId(const QString &id)
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QMap< QString, QConnmanTechnologyInterface * > technologies
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
int secsTo(const QDateTime &) const
Returns the number of seconds from this datetime to the other datetime.
Definition: qdatetime.cpp:2914
void configurationAdded(QNetworkConfigurationPrivatePointer config)
QConnmanEngine(QObject *parent=0)
QList< QNetworkConfigurationPrivate * > foundConfigurations
void configurationChange(const QString &id)
QNetworkSessionPrivate * createSessionBackend()
QString path() const
Returns this object path.
const T * ptr(const T &t)
virtual void connectToId(const QString &id)
void unlock()
Unlocks the mutex.
Definition: qmutex.cpp:296
bool connmanAvailable() const
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
int remove(const Key &key)
Removes all the items that have the key key from the map.
Definition: qmap.h:662
void servicePropertyChangedContext(const QString &, const QString &, const QDBusVariant &)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
QNetworkConfiguration::Purpose purpose
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
QExplicitlySharedDataPointer< QNetworkConfigurationPrivate > QNetworkConfigurationPrivatePointer
virtual QNetworkConfigurationPrivatePointer defaultConfiguration()
virtual quint64 bytesReceived(const QString &id)
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
bool requestScan(const QString &type)
QNetworkConfiguration::Type type
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
Q_INVOKABLE void requestUpdate()
bool singleShot
This static function calls a slot after a given time interval.
Definition: qtimer.h:59
QNetworkSession::State sessionStateForId(const QString &id)
The QDBusObjectPath class enables the programmer to identify the OBJECT_PATH type provided by the D-B...
void configurationRemoved(QNetworkConfigurationPrivatePointer config)
QVariant variant() const
Returns this D-Bus variant as a QVariant object.
void propertyChangedContext(const QString &, const QString &, const QDBusVariant &)
static QDateTime currentDateTime()
Returns the current datetime, as reported by the system clock, in the local time zone.
Definition: qdatetime.cpp:3138
QList< QDBusObjectPath > getPrimaryContexts()
bool contains(const Key &key) const
Returns true if the map contains an item with key key; otherwise returns false.
Definition: qmap.h:553
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
QNetworkConfiguration::BearerType ofonoTechToBearerType(const QString &type)
void connectionError(const QString &id, QBearerEngineImpl::ConnectionError error)
QString serviceFromId(const QString &id)
void removeConfiguration(const QString &servicePath)
virtual QNetworkConfigurationManager::Capabilities capabilities() const
Q_INVOKABLE void initialize()
virtual void close()
Calls QFile::flush() and closes the file.
Definition: qfile.cpp:1680
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
bool hasIdentifier(const QString &id)
The QDBusVariant class enables the programmer to identify the variant type provided by the D-Bus type...
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
QNetworkConfiguration::BearerType typeToBearer(const QString &type)