/
networkmanager.cpp
1335 lines (1145 loc) · 40.2 KB
/
networkmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2010 Intel Corporation.
* Copyright © 2012-2018 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is licensed under the terms and conditions of the
* Apache License, version 2.0. The full text of the Apache License is at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include "networkmanager.h"
#include "libconnman_p.h"
#include <QRegExp>
// ==========================================================================
// NetworkManagerFactory
// ==========================================================================
static NetworkManager* staticInstance = NULL;
NetworkManager* NetworkManagerFactory::createInstance()
{
if (!staticInstance)
staticInstance = new NetworkManager;
return staticInstance;
}
NetworkManager* NetworkManagerFactory::instance()
{
return createInstance();
}
// ==========================================================================
// NetworkManager::Private
// ==========================================================================
class NetworkManager::Private : public QObject
{
Q_OBJECT
public:
class ListUpdate;
static const QString InputRequestTimeout;
static const uint DefaultInputRequestTimeout;
static const QString WifiType;
static const QString CellularType;
bool m_registered;
bool m_servicesAvailable;
bool m_technologiesAvailable;
bool m_connecting;
bool m_connected;
QStringList m_availableServicesOrder;
QStringList m_wifiServicesOrder;
QStringList m_cellularServicesOrder;
NetworkService* m_connectedWifi;
public:
static bool selectSaved(NetworkService *service);
static bool selectAvailable(NetworkService *service);
bool updateWifiConnected(NetworkService *service);
void setServicesAvailable(bool servicesAvailable);
void setTechnologiesAvailable(bool technologiesAvailable);
void updateState(const QString &newState);
public:
Private(NetworkManager *parent)
: QObject(parent)
, m_registered(false)
, m_servicesAvailable(false)
, m_technologiesAvailable(false)
, m_connecting(false)
, m_connected(false)
, m_connectedWifi(NULL) {}
NetworkManager* manager()
{ return (NetworkManager*)parent(); }
void maybeCreateInterfaceProxyLater()
{ QMetaObject::invokeMethod(this, "maybeCreateInterfaceProxy"); }
public Q_SLOTS:
void maybeCreateInterfaceProxy();
void onWifiConnectedChanged();
};
const QString NetworkManager::Private::InputRequestTimeout("InputRequestTimeout");
const uint NetworkManager::Private::DefaultInputRequestTimeout(300000);
const QString NetworkManager::Private::WifiType("wifi");
const QString NetworkManager::Private::CellularType("cellular");
bool NetworkManager::Private::selectSaved(NetworkService *service)
{
return service && service->saved();
}
bool NetworkManager::Private::selectAvailable(NetworkService *service)
{
return service && service->available();
}
void NetworkManager::Private::maybeCreateInterfaceProxy()
{
// Theoretically, connman may have become unregistered while this call
// was sitting in the queue. We need to re-check the m_registered flag.
if (m_registered) {
NetworkManager* mgr = manager();
if (!mgr->m_available) {
mgr->setConnmanAvailable(true);
}
}
}
bool NetworkManager::Private::updateWifiConnected(NetworkService *service)
{
if (service->connected()) {
if (!m_connectedWifi) {
m_connectedWifi = service;
return true;
}
} else if (m_connectedWifi == service) {
QVector<NetworkService*> availableWifi = manager()->getAvailableServices(WifiType);
m_connectedWifi = NULL;
for (NetworkService *wifi: availableWifi) {
if (wifi->connected()) {
m_connectedWifi = wifi;
break;
}
}
return true;
}
return false;
}
void NetworkManager::Private::onWifiConnectedChanged()
{
NetworkService *service = qobject_cast<NetworkService*>(sender());
if (service && updateWifiConnected(service)) {
Q_EMIT manager()->connectedWifiChanged();
}
}
// ==========================================================================
// NetworkManager::InterfaceProxy
//
// qdbusxml2cpp doesn't really do much, it's easier to write these proxies
// by hand. Basically, this is what we have here:
//
// <interface name="net.connman.Manager">
// <method name="GetProperties">
// <arg name="properties" type="a{sv}" direction="out"/>
// </method>
// <method name="SetProperty">
// <arg name="name" type="s"/>
// <arg name="value" type="v"/>
// </method>
// <method name="GetTechnologies">
// <arg name="technologies" type="a(oa{sv})" direction="out"/>
// </method>
// <method name="GetServices">
// <arg name="services" type="a(oa{sv})" direction="out"/>
// </method>
// <method name="RegisterAgent">
// <arg name="path" type="o"/>
// </method>
// <method name="UnregisterAgent">
// <arg name="path" type="o"/>
// </method>
// <method name="RegisterCounter">
// <arg name="path" type="o"/>
// <arg name="accuracy" type="u"/>
// <arg name="period" type="u"/>
// </method>
// <method name="UnregisterCounter">
// <arg name="path" type="o"/>
// </method>
// <method name="ResetCounters">
// <arg name="type" type="s"/>
// </method>
// <method name="CreateSession">
// <arg name="settings" type="a{sv}"/>
// <arg name="notifier" type="o"/>
// <arg name="session" type="o" direction="out"/>
// </method>
// <method name="DestroySession">
// <arg name="session" type="o"/>
// </method>
// <method name="CreateService">
// <arg name="service_type" type="s"/>
// <arg name="device_ident" type="s"/>
// <arg name="network_ident" type="s"/>
// <arg name="settings" type="a(ss)"/>
// <arg name="service" type="o"/>
// </method>
// <signal name="TechnologyAdded">
// <arg name="technology" type="o"/>
// <arg name="properties" type="a{sv}"/>
// </signal>
// <signal name="TechnologyRemoved">
// <arg name="technology" type="o"/>
// </signal>
// <signal name="ServicesChanged">
// <arg name="changed" type="a(oa{sv})"/>
// <arg name="removed" type="ao"/>
// </signal>
// <signal name="PropertyChanged">
// <arg name="name" type="s"/>
// <arg name="value" type="v"/>
// </signal>
// </interface>
//
// ==========================================================================
class NetworkManager::InterfaceProxy: public QDBusAbstractInterface
{
Q_OBJECT
public:
InterfaceProxy(NetworkManager *parent) :
QDBusAbstractInterface(CONNMAN_SERVICE, "/", "net.connman.Manager",
CONNMAN_BUS, parent) {}
public:
QDBusPendingCall GetProperties()
{ return asyncCall("GetProperties"); }
QDBusPendingCall SetProperty(QString name, QVariant value)
{ return asyncCall("SetProperty", name, qVariantFromValue(QDBusVariant(value))); }
QDBusPendingCall GetTechnologies()
{ return asyncCall("GetTechnologies"); }
QDBusPendingCall GetServices()
{ return asyncCall("GetServices"); }
QDBusPendingCall RegisterAgent(const QString &path)
{ return asyncCall("RegisterAgent", qVariantFromValue(QDBusObjectPath(path))); }
QDBusPendingCall UnregisterAgent(const QString &path)
{ return asyncCall("UnregisterAgent", qVariantFromValue(QDBusObjectPath(path))); }
QDBusPendingCall RegisterCounter(const QString &path, uint accuracy, uint period)
{ return asyncCall("RegisterCounter", qVariantFromValue(QDBusObjectPath(path)), accuracy, period); }
QDBusPendingCall ResetCounters(const QString &type)
{ return asyncCall("ResetCounters", type); }
QDBusPendingCall UnregisterCounter(const QString &path)
{ return asyncCall("UnregisterCounter", qVariantFromValue(QDBusObjectPath(path))); }
QDBusPendingReply<QDBusObjectPath> CreateSession(const QVariantMap &settings, const QString &path)
{ return asyncCall("CreateSession", settings, qVariantFromValue(QDBusObjectPath(path))); }
QDBusPendingCall DestroySession(const QString &path)
{ return asyncCall("DestroySession", qVariantFromValue(QDBusObjectPath(path))); }
QDBusPendingReply<QDBusObjectPath> CreateService(const QString &type, const QString &device, const QString &network, const StringPairArray &settings)
{ return asyncCall("CreateService", type, device, network, qVariantFromValue(settings)); }
Q_SIGNALS:
void PropertyChanged(const QString &name, const QDBusVariant &value);
void ServicesChanged(ConnmanObjectList changed, const QList<QDBusObjectPath> &removed);
void TechnologyAdded(const QDBusObjectPath &technology, const QVariantMap &properties);
void TechnologyRemoved(const QDBusObjectPath &technology);
};
// ==========================================================================
// NetworkManager
// ==========================================================================
const QString NetworkManager::State("State");
const QString NetworkManager::OfflineMode("OfflineMode");
const QString NetworkManager::SessionMode("SessionMode");
const QString NetworkManager::WifiTechnologyPath("/net/connman/technology/wifi");
const QString NetworkManager::CellularTechnologyPath("/net/connman/technology/cellular");
const QString NetworkManager::BluetoothTechnologyPath("/net/connman/technology/bluetooth");
const QString NetworkManager::GpsTechnologyPath("/net/connman/technology/gps");
NetworkManager::NetworkManager(QObject* parent)
: QObject(parent),
m_proxy(NULL),
m_defaultRoute(NULL),
m_invalidDefaultRoute(new NetworkService("/", QVariantMap(), this)),
m_priv(new Private(this)),
m_available(false),
m_servicesEnabled(true),
m_technologiesEnabled(true)
{
registerCommonDataTypes();
QDBusServiceWatcher* watcher = new QDBusServiceWatcher(CONNMAN_SERVICE, CONNMAN_BUS,
QDBusServiceWatcher::WatchForRegistration |
QDBusServiceWatcher::WatchForUnregistration, this);
connect(watcher, SIGNAL(serviceRegistered(QString)), SLOT(onConnmanRegistered()));
connect(watcher, SIGNAL(serviceUnregistered(QString)), SLOT(onConnmanUnregistered()));
setConnmanAvailable(CONNMAN_BUS.interface()->isServiceRegistered(CONNMAN_SERVICE));
}
NetworkManager::~NetworkManager()
{
}
NetworkManager* NetworkManager::instance()
{
return NetworkManagerFactory::createInstance();
}
void NetworkManager::onConnmanRegistered()
{
// Store the current connman registration state so that
// Private::maybeCreateInterfaceProxy() could check it.
// m_proxy is not a reliable indicator because proxy creation
// can (and sometimes does) fail.
m_priv->m_registered = true;
setConnmanAvailable(true);
}
void NetworkManager::onConnmanUnregistered()
{
m_priv->m_registered = false;
setConnmanAvailable(false);
}
void NetworkManager::setConnmanAvailable(bool available)
{
if (m_available != available) {
if (available) {
if (connectToConnman()) {
Q_EMIT availabilityChanged(m_available = true);
} else {
//
// There is a race condition (at least in Qt 5.6) between
// this thread and "QDBusConnection" thread updating the
// service owner map. If we get there first (before the
// owner map is updated) we would fetch the old (empty)
// owner from there which makes QDBusAbstractInterface
// invalid, even though the name is actually owned.
//
// The easy workaround is to try again a bit later.
// This is a very narrow race condition, no delay is
// necessary.
//
m_priv->maybeCreateInterfaceProxyLater();
}
} else {
DBG_("connman not AVAILABLE");
Q_EMIT availabilityChanged(m_available = false);
disconnectFromConnman();
}
}
}
bool NetworkManager::connectToConnman()
{
disconnectFromConnman();
m_proxy = new InterfaceProxy(this);
if (!m_proxy->isValid()) {
WARN_(m_proxy->lastError());
delete m_proxy;
m_proxy = NULL;
return false;
} else {
connect(m_proxy,
SIGNAL(PropertyChanged(QString,QDBusVariant)),
SLOT(propertyChanged(QString,QDBusVariant)));
connect(new QDBusPendingCallWatcher(m_proxy->GetProperties(), m_proxy),
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(getPropertiesFinished(QDBusPendingCallWatcher*)));
return true;
}
}
void NetworkManager::disconnectFromConnman()
{
delete m_proxy;
m_proxy = NULL;
disconnectTechnologies();
disconnectServices();
}
void NetworkManager::disconnectTechnologies()
{
// Update availability and check whether validity changed
bool wasValid = isValid();
m_priv->setTechnologiesAvailable(false);
if (m_proxy) {
disconnect(m_proxy, SIGNAL(TechnologyAdded(QDBusObjectPath,QVariantMap)),
this, SLOT(technologyAdded(QDBusObjectPath,QVariantMap)));
disconnect(m_proxy, SIGNAL(TechnologyRemoved(QDBusObjectPath)),
this, SLOT(technologyRemoved(QDBusObjectPath)));
}
for (NetworkTechnology *tech : m_technologiesCache) {
tech->deleteLater();
}
if (!m_technologiesCache.isEmpty()) {
m_technologiesCache.clear();
Q_EMIT technologiesChanged();
}
if (wasValid != isValid()) {
Q_EMIT validChanged();
}
}
void NetworkManager::disconnectServices()
{
// Update availability and check whether validity changed
bool wasValid = isValid();
m_priv->setServicesAvailable(false);
if (m_defaultRoute != m_invalidDefaultRoute) {
m_defaultRoute = m_invalidDefaultRoute;
Q_EMIT defaultRouteChanged(m_defaultRoute);
}
if (m_proxy) {
disconnect(m_proxy, SIGNAL(ServicesChanged(ConnmanObjectList,QList<QDBusObjectPath>)),
this, SLOT(updateServices(ConnmanObjectList,QList<QDBusObjectPath>)));
}
for (NetworkService *service : m_servicesCache) {
service->deleteLater();
}
m_servicesCache.clear();
// Clear all lists before emitting the signals
bool emitSavedServicesChanged = false;
if (!m_savedServicesOrder.isEmpty()) {
m_savedServicesOrder.clear();
emitSavedServicesChanged = true;
}
bool emitAvailableServicesChanged = false;
if (!m_priv->m_availableServicesOrder.isEmpty()) {
m_priv->m_availableServicesOrder.clear();
emitAvailableServicesChanged = true;
}
bool emitWifiServicesChanged = false;
if (!m_priv->m_wifiServicesOrder.isEmpty()) {
m_priv->m_wifiServicesOrder.clear();
emitWifiServicesChanged = true;
}
bool emitCellularServicesChanged = false;
if (!m_priv->m_cellularServicesOrder.isEmpty()) {
m_priv->m_cellularServicesOrder.clear();
emitCellularServicesChanged = true;
}
if (!m_servicesOrder.isEmpty()) {
m_servicesOrder.clear();
Q_EMIT servicesChanged();
}
if (emitSavedServicesChanged) {
Q_EMIT savedServicesChanged();
}
if (emitSavedServicesChanged) {
Q_EMIT savedServicesChanged();
}
if (emitAvailableServicesChanged) {
Q_EMIT availableServicesChanged();
}
if (emitWifiServicesChanged) {
Q_EMIT wifiServicesChanged();
}
if (emitCellularServicesChanged) {
Q_EMIT cellularServicesChanged();
}
if (wasValid != isValid()) {
Q_EMIT validChanged();
}
}
void NetworkManager::setupTechnologies()
{
if (m_proxy) {
connect(m_proxy,
SIGNAL(TechnologyAdded(QDBusObjectPath,QVariantMap)),
SLOT(technologyAdded(QDBusObjectPath,QVariantMap)));
connect(m_proxy,
SIGNAL(TechnologyRemoved(QDBusObjectPath)),
SLOT(technologyRemoved(QDBusObjectPath)));
connect(new QDBusPendingCallWatcher(m_proxy->GetTechnologies(), m_proxy),
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(getTechnologiesFinished(QDBusPendingCallWatcher*)));
}
}
void NetworkManager::setupServices()
{
if (m_proxy) {
connect(m_proxy,
SIGNAL(ServicesChanged(ConnmanObjectList,QList<QDBusObjectPath>)),
SLOT(updateServices(ConnmanObjectList,QList<QDBusObjectPath>)));
connect(new QDBusPendingCallWatcher(m_proxy->GetServices(), m_proxy),
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(getServicesFinished(QDBusPendingCallWatcher*)));
}
}
class NetworkManager::Private::ListUpdate {
public:
ListUpdate(QStringList* list) : storage(list), changed(false), count(0) {}
void add(const QString& str) {
if (storage->count() == count) {
storage->append(str);
changed = true;
} else if (storage->at(count) != str) {
while (storage->count() > count) {
storage->removeLast();
}
storage->append(str);
changed = true;
}
count++;
}
void done() {
while (storage->count() > count) {
storage->removeLast();
changed = true;
}
}
public:
QStringList* storage;
bool changed;
int count;
};
void NetworkManager::updateServices(const ConnmanObjectList &changed, const QList<QDBusObjectPath> &removed)
{
Private::ListUpdate services(&m_servicesOrder);
Private::ListUpdate savedServices(&m_savedServicesOrder);
Private::ListUpdate availableServices(&m_priv->m_availableServicesOrder);
Private::ListUpdate wifiServices(&m_priv->m_wifiServicesOrder);
Private::ListUpdate cellularServices(&m_priv->m_cellularServicesOrder);
QStringList addedServices;
QStringList removedServices;
NetworkService* prevConnectedWifi = m_priv->m_connectedWifi;
for (const ConnmanObject &obj : changed) {
const QString path(obj.objpath.path());
NetworkService *service = m_servicesCache.value(path);
if (service) {
// We don't want to emit signals at this point. Those will
// be emitted later, after internal state is fully updated
disconnect(service, SIGNAL(connectedChanged(bool)),
this, SLOT(updateDefaultRoute()));
disconnect(service, SIGNAL(connectedChanged(bool)),
m_priv, SLOT(onWifiConnectedChanged()));
service->updateProperties(obj.properties);
} else {
service = new NetworkService(path, obj.properties, this);
m_servicesCache.insert(path, service);
addedServices.append(path);
}
// Re-connect updateDefaultRoute slot
connect(service, SIGNAL(connectedChanged(bool)),
this, SLOT(updateDefaultRoute()));
// Full list
services.add(path);
// Saved services
if (Private::selectSaved(service)) {
savedServices.add(path);
}
// Available services
if (Private::selectAvailable(service)) {
availableServices.add(path);
}
// Per-technology lists
const QString type(service->type());
if (type == Private::WifiType) {
wifiServices.add(path);
// Some special treatment for WiFi services
m_priv->updateWifiConnected(service);
connect(service, SIGNAL(connectedChanged(bool)),
m_priv, SLOT(onWifiConnectedChanged()));
} else if (type == Private::CellularType) {
cellularServices.add(path);
}
}
// Cut the tails
services.done();
savedServices.done();
availableServices.done();
wifiServices.done();
cellularServices.done();
// Removed services
for (const QDBusObjectPath &obj : removed) {
const QString path(obj.path());
NetworkService *service = m_servicesCache.value(path);
if (service) {
if (service == m_priv->m_connectedWifi) {
m_priv->m_connectedWifi = NULL;
}
service->deleteLater();
m_servicesCache.remove(path);
removedServices.append(path);
} else {
// connman maintains a virtual "hidden" wifi network and removes it upon init
DBG_("attempted to remove non-existing service" << path);
}
}
// Make sure that m_servicesCache doesn't contain stale elements.
// Hopefully that won't happen too often (if at all)
if (m_servicesCache.count() > m_servicesOrder.count()) {
QStringList keys = m_servicesCache.keys();
for (const QString &path: keys) {
if (!m_servicesOrder.contains(path)) {
m_servicesCache.value(path)->deleteLater();
m_servicesCache.remove(path);
removedServices.append(path);
if (m_servicesCache.count() == m_servicesOrder.count()) {
break;
}
}
}
}
// Update availability and check whether validity changed
bool wasValid = isValid();
m_priv->setServicesAvailable(true);
// Emit signals
if (m_priv->m_connectedWifi != prevConnectedWifi) {
Q_EMIT connectedWifiChanged();
}
// Added services
for (const QString &path: addedServices) {
Q_EMIT serviceAdded(path);
}
// Removed services
for (const QString &path: removedServices) {
Q_EMIT serviceRemoved(path);
}
updateDefaultRoute();
if (services.changed) {
Q_EMIT servicesChanged();
// This one is probably unnecessary:
Q_EMIT servicesListChanged(m_servicesOrder);
}
if (savedServices.changed) {
Q_EMIT savedServicesChanged();
}
if (availableServices.changed) {
Q_EMIT availableServicesChanged();
}
if (wifiServices.changed) {
Q_EMIT wifiServicesChanged();
}
if (cellularServices.changed) {
Q_EMIT cellularServicesChanged();
}
if (wasValid != isValid()) {
Q_EMIT validChanged();
}
}
void NetworkManager::propertyChanged(const QString &name, const QDBusVariant &value)
{
propertyChanged(name, value.variant());
}
void NetworkManager::updateDefaultRoute()
{
QString defaultNetDev;
QFile routeFile("/proc/net/route");
if (routeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&routeFile);
QString line = in.readLine();
while (!line.isNull()) {
QStringList lineList = line.split('\t');
if (lineList.size() >= 11) {
if ((lineList.at(1) == "00000000" && lineList.at(3) == "0003") ||
(lineList.at(0).startsWith("ppp") && lineList.at(3) == "0001")) {
defaultNetDev = lineList.at(0);
break;
}
}
line = in.readLine();
}
routeFile.close();
}
if (defaultNetDev.isNull()) {
QFile ipv6routeFile("/proc/net/ipv6_route");
if (ipv6routeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream ipv6in(&ipv6routeFile);
QString ipv6line = ipv6in.readLine();
while (!ipv6line.isNull()) {
QStringList ipv6lineList = ipv6line.split(QRegExp("\\s+"));
if (ipv6lineList.size() >= 10) {
if (ipv6lineList.at(0) == "00000000000000000000000000000000" &&
(ipv6lineList.at(8).endsWith("3") || (ipv6lineList.at(8).endsWith("1")))) {
defaultNetDev = ipv6lineList.at(9).trimmed();
break;
}
ipv6line = ipv6in.readLine();
}
}
ipv6routeFile.close();
}
}
for (NetworkService *service : m_servicesCache) {
if (service->connected()) {
if (defaultNetDev == service->ethernet().value("Interface")) {
if (m_defaultRoute != service) {
m_defaultRoute = service;
Q_EMIT defaultRouteChanged(m_defaultRoute);
}
return;
}
}
}
if (m_defaultRoute != m_invalidDefaultRoute) {
m_defaultRoute = m_invalidDefaultRoute;
Q_EMIT defaultRouteChanged(m_defaultRoute);
}
}
void NetworkManager::technologyAdded(const QDBusObjectPath &technology,
const QVariantMap &properties)
{
NetworkTechnology *tech = new NetworkTechnology(technology.path(),
properties, this);
m_technologiesCache.insert(tech->type(), tech);
Q_EMIT technologiesChanged();
}
void NetworkManager::technologyRemoved(const QDBusObjectPath &technology)
{
// if we weren't storing by type() this loop would be unecessary
// but since this function will be triggered rarely that's fine
for (NetworkTechnology *net : m_technologiesCache) {
if (net->objPath() == technology.path()) {
m_technologiesCache.remove(net->type());
net->deleteLater();
break;
}
}
Q_EMIT technologiesChanged();
}
void NetworkManager::getPropertiesFinished(QDBusPendingCallWatcher *watcher)
{
QDBusPendingReply<QVariantMap> reply = *watcher;
watcher->deleteLater();
if (reply.isError()) {
DBG_(reply.error().message());
return;
}
QVariantMap props = reply.value();
for (QVariantMap::ConstIterator i = props.constBegin(); i != props.constEnd(); ++i)
propertyChanged(i.key(), i.value());
if (m_technologiesEnabled)
setupTechnologies();
if (m_servicesEnabled)
setupServices();
}
void NetworkManager::getTechnologiesFinished(QDBusPendingCallWatcher *watcher)
{
QDBusPendingReply<ConnmanObjectList> reply = *watcher;
watcher->deleteLater();
if (reply.isError())
return;
for (const ConnmanObject &object : reply.value()) {
NetworkTechnology *tech = new NetworkTechnology(object.objpath.path(),
object.properties, this);
m_technologiesCache.insert(tech->type(), tech);
}
// Update availability and check whether validity changed
bool wasValid = isValid();
m_priv->setTechnologiesAvailable(true);
Q_EMIT technologiesChanged();
if (wasValid != isValid()) {
Q_EMIT validChanged();
}
}
void NetworkManager::getServicesFinished(QDBusPendingCallWatcher *watcher)
{
QDBusPendingReply<ConnmanObjectList> reply = *watcher;
ConnmanObjectList services;
watcher->deleteLater();
if (reply.isError()) {
WARN_(reply.error());
} else {
services = reply.value();
}
updateServices(services, QList<QDBusObjectPath>());
}
// Public API /////////////
// Getters
bool NetworkManager::isAvailable() const
{
return m_available;
}
QString NetworkManager::state() const
{
return m_propertiesCache[State].toString();
}
bool NetworkManager::offlineMode() const
{
return m_propertiesCache[OfflineMode].toBool();
}
NetworkService* NetworkManager::defaultRoute() const
{
return m_defaultRoute;
}
NetworkService* NetworkManager::connectedWifi() const
{
return m_priv->m_connectedWifi;
}
NetworkTechnology* NetworkManager::getTechnology(const QString &type) const
{
return m_technologiesCache.value(type);
}
QVector<NetworkTechnology *> NetworkManager::getTechnologies() const
{
QVector<NetworkTechnology *> techs;
for (NetworkTechnology *tech : m_technologiesCache) {
techs.push_back(tech);
}
return techs;
}
QVector<NetworkService*> NetworkManager::selectServices(const QStringList &list,
ServiceSelector selector) const
{
QVector<NetworkService *> services;
for (const QString &path : list) {
NetworkService *service = m_servicesCache.value(path);
if (selector(service)) {
services.append(m_servicesCache.value(path));
}
}
return services;
}
QVector<NetworkService*> NetworkManager::selectServices(const QStringList &list,
const QString &tech) const
{
QVector<NetworkService*> services;
if (tech.isEmpty()) {
for (const QString &path : list) {
services.append(m_servicesCache.value(path));
}
} else {
for (const QString &path : list) {
NetworkService *service = m_servicesCache.value(path);
if (service->type() == tech) {
services.append(service);
}
}
}
return services;
}
QStringList NetworkManager::selectServiceList(const QStringList &list,
ServiceSelector selector) const
{
QStringList services;
for (const QString &path : list) {
if (selector(m_servicesCache.value(path))) {
services.append(path);
}
}
return services;
}
QStringList NetworkManager::selectServiceList(const QStringList &list,
const QString &tech) const
{
if (tech.isEmpty()) {
return list;
} else {
QStringList services;
for (const QString &path : list) {
NetworkService *service = m_servicesCache.value(path);
if (service->type() == tech) {
services.append(path);
}
}
return services;
}
}
QVector<NetworkService*> NetworkManager::getServices(const QString &tech) const
{
if (tech == Private::WifiType) {
return selectServices(m_priv->m_wifiServicesOrder, QString());
} else if (tech == Private::CellularType) {
return selectServices(m_priv->m_cellularServicesOrder, QString());
} else {
return selectServices(m_servicesOrder, tech);
}
}
QVector<NetworkService*> NetworkManager::getSavedServices(const QString &tech) const
{
// Choose smaller list to scan
if (tech == Private::WifiType) {
if (m_priv->m_wifiServicesOrder.count() < m_savedServicesOrder.count()) {
return selectServices(m_priv->m_wifiServicesOrder, Private::selectSaved);
}
} else if (tech == Private::CellularType) {
if (m_priv->m_cellularServicesOrder.count() < m_savedServicesOrder.count()) {
return selectServices(m_priv->m_cellularServicesOrder, Private::selectSaved);
}
}
return selectServices(m_savedServicesOrder, tech);
}
QVector<NetworkService*> NetworkManager::getAvailableServices(const QString &tech) const
{
// Choose smaller list to scan
if (tech == Private::WifiType) {
if (m_priv->m_wifiServicesOrder.count() < m_priv->m_availableServicesOrder.count()) {
return selectServices(m_priv->m_wifiServicesOrder, Private::selectAvailable);
}
} else if (tech == Private::CellularType) {
if (m_priv->m_cellularServicesOrder.count() < m_priv->m_availableServicesOrder.count()) {
return selectServices(m_priv->m_cellularServicesOrder, Private::selectAvailable);
}
}
return selectServices(m_priv->m_availableServicesOrder, tech);
}
QStringList NetworkManager::servicesList(const QString &tech)
{
if (tech == Private::WifiType) {
return m_priv->m_wifiServicesOrder;
} else if (tech == Private::CellularType) {
return m_priv->m_cellularServicesOrder;
} else {
return selectServiceList(m_servicesOrder, tech);
}
}
QStringList NetworkManager::savedServicesList(const QString &tech)
{
// Choose smaller list to scan
if (tech == Private::WifiType) {
if (m_priv->m_wifiServicesOrder.count() < m_savedServicesOrder.count()) {
return selectServiceList(m_priv->m_wifiServicesOrder, Private::selectSaved);
}
} else if (tech == Private::CellularType) {
if (m_priv->m_cellularServicesOrder.count() < m_savedServicesOrder.count()) {
return selectServiceList(m_priv->m_cellularServicesOrder, Private::selectSaved);
}
}
return selectServiceList(m_savedServicesOrder, tech);