/
qconnectionagent.cpp
725 lines (625 loc) · 25.4 KB
/
qconnectionagent.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
/****************************************************************************
**
** Copyright (C) 2014-2017 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 "qconnectionagent.h"
#include "connectiond_adaptor.h"
#include <connman-qt5/useragent.h>
#include <connman-qt5/networktechnology.h>
#include <connman-qt5/networkservice.h>
#include <QtDBus/QDBusConnection>
#include <QObject>
#include <QSettings>
#define CONND_SERVICE "com.jolla.Connectiond"
#define CONND_PATH "/Connectiond"
#define CONND_SESSION_PATH = "/ConnectionSession"
Q_LOGGING_CATEGORY(connAgent, "org.sailfishos.connectionagent", QtWarningMsg)
static bool isStateOnline(NetworkManager::State state)
{
return state == NetworkManager::OnlineState || state == NetworkManager::ReadyState;
}
static bool isStateOnline(NetworkService::ServiceState state)
{
return state == NetworkService::OnlineState || state == NetworkService::ReadyState;
}
QConnectionAgent::QConnectionAgent(QObject *parent) :
QObject(parent),
ua(nullptr),
netman(NetworkManager::sharedInstance()),
isEthernet(false),
tetheringWifiTech(nullptr),
tetheringBtTech(nullptr),
tetherWifiWhenPowered(false),
tetherBtWhenPowered(false),
flightModeSuppression(false),
scanTimeoutInterval(1),
valid(true)
{
new ConnAdaptor(this);
QDBusConnection dbus = QDBusConnection::sessionBus();
if (!dbus.registerObject(CONND_PATH, this)) {
qCCritical(connAgent) << "QConnectionAgent: Could not register object to path" << CONND_PATH;
valid = false;
}
if (!dbus.registerService(CONND_SERVICE)) {
qCCritical(connAgent) << "QConnectionAgent: could not register service" << CONND_SERVICE;
valid = false;
}
connect(this, &QConnectionAgent::configurationNeeded, this, &QConnectionAgent::openConnectionDialog);
connect(netman.data(), &NetworkManager::availabilityChanged, this, &QConnectionAgent::connmanAvailabilityChanged);
connect(netman.data(), &NetworkManager::servicesListChanged, this, &QConnectionAgent::servicesListChanged);
connect(netman.data(), &NetworkManager::globalStateChanged, this, &QConnectionAgent::networkManagerStateChanged);
connect(netman.data(), &NetworkManager::offlineModeChanged, this, &QConnectionAgent::offlineModeChanged);
connect(netman.data(), &NetworkManager::servicesChanged, this, &QConnectionAgent::updateServices);
connect(netman.data(), &NetworkManager::technologiesChanged, this, &QConnectionAgent::techChanged);
QFile connmanConf("/etc/connman/main.conf");
if (connmanConf.open(QIODevice::ReadOnly | QIODevice::Text)) {
while (!connmanConf.atEnd()) {
QString line = connmanConf.readLine();
if (line.startsWith("DefaultAutoConnectTechnologies")) {
QString token = line.section(" = ", 1, 1).simplified();
techPreferenceList = token.split(",");
break;
}
}
connmanConf.close();
}
if (techPreferenceList.isEmpty()) {
//ethernet,bluetooth,cellular,wifi is default
techPreferenceList << "bluetooth" << "wifi" << "cellular" << "ethernet";
}
bool connmanAvailable = QDBusConnection::systemBus().interface()->isServiceRegistered("net.connman");
scanTimer = new QTimer(this);
connect(scanTimer, &QTimer::timeout, this, &QConnectionAgent::scanTimeout);
scanTimer->setSingleShot(true);
if (connmanAvailable && valid)
setup();
}
QConnectionAgent::~QConnectionAgent()
{
}
bool QConnectionAgent::isValid() const
{
return valid;
}
// from useragent
void QConnectionAgent::onErrorReported(const QString &servicePath, const QString &error)
{
if (shouldSuppressError(error, servicePath.contains("cellular")))
return;
if (!tetheringWifiTech && !tetheringBtTech) return;
// Suppress errors when switching to tethering mode
if ((tetherWifiWhenPowered || tetheringWifiTech->tethering()) && servicePath.contains(QStringLiteral("wifi")))
return;
qCWarning(connAgent) << "ConnectionAgent error in" << servicePath << ":" << error;
Q_EMIT errorReported(servicePath, error);
}
// from useragent
void QConnectionAgent::onConnectionRequest()
{
sendConnectReply("Suppress", 15);
qCDebug(connAgent) << flightModeSuppression;
bool okToRequest = true;
for (Service elem: orderedServicesList) {
qCDebug(connAgent) << "checking" << elem.service->name() << elem.service->autoConnect();
if (elem.service->autoConnect()) {
okToRequest = false;
break;
}
}
if (!flightModeSuppression && okToRequest) {
Q_EMIT connectionRequest();
}
}
void QConnectionAgent::onBrowserRequested(const QString &servicePath, const QString &url)
{
QString serviceName;
for (const Service &elem : orderedServicesList) {
if (elem.service->path() == servicePath) {
serviceName = elem.service->name();
break;
}
}
Q_EMIT requestBrowser(url, serviceName);
}
void QConnectionAgent::sendConnectReply(const QString &in0, int in1)
{
ua->sendConnectReply(in0, in1);
}
void QConnectionAgent::sendUserReply(const QVariantMap &input)
{
qCDebug(connAgent) << Q_FUNC_INFO;
ua->sendUserReply(input);
}
void QConnectionAgent::servicesListChanged(const QStringList &list)
{
bool changed = false;
for (const QString &path: list) {
if (orderedServicesList.indexOf(path) == -1) {
//added
qCDebug(connAgent) << Q_FUNC_INFO << "added" << path;
changed = true;
break;
}
}
if (!changed)
for (Service elem: orderedServicesList) {
if (list.indexOf(elem.path) == -1) {
//removed
qCDebug(connAgent) << Q_FUNC_INFO << "removed" << elem.path;
changed = true;
break;
}
}
if (changed)
updateServices();
}
void QConnectionAgent::serviceErrorChanged(const QString &error)
{
NetworkService *service = static_cast<NetworkService *>(sender());
if (shouldSuppressError(error, service->type() == QLatin1String("cellular")))
return;
Q_EMIT errorReported(service->path(), error);
}
void QConnectionAgent::serviceStateChanged(NetworkService::ServiceState state)
{
NetworkService *service = static_cast<NetworkService *>(sender());
if (!service)
return;
qCDebug(connAgent) << state << service->name() << service->strength();
if (state == NetworkService::ReadyState && service->type() == "wifi"
&& !tetherWifiWhenPowered
&& netman->defaultRoute()->type() == "cellular") {
netman->defaultRoute()->requestDisconnect();
}
if (state == NetworkService::DisconnectState) {
Q_EMIT connectionState(QStringLiteral("disconnect"), service->type());
}
if (!service->favorite() || !netman->getTechnology(service->type())
|| !netman->getTechnology(service->type())->powered()) {
qCDebug(connAgent) << "not fav or not powered";
return;
}
if (state == NetworkService::DisconnectState) {
ua->sendConnectReply("Clear");
}
if (state == NetworkService::FailureState) {
if (tetherWifiWhenPowered && service->type() == "cellular" && tetheringWifiTech->tethering()) {
Q_EMIT wifiTetheringFinished(false);
}
}
if (tetherWifiWhenPowered && service->type() == "wifi" && state == NetworkService::AssociationState) {
service->requestDisconnect();
}
if (state == NetworkService::OnlineState) {
Q_EMIT connectionState(QStringLiteral("online"), service->type());
if (service->type() == "wifi" && tetherWifiWhenPowered) {
netman->getTechnology(service->type())->setTethering(true);
}
if (service->type() == "cellular" && tetherWifiWhenPowered) {
if (!tetheringWifiTech->tethering()) {
tetheringWifiTech->setTethering(true);
}
}
}
// auto migrate
if (state == NetworkService::IdleState) {
if (service->type() == "wifi" && tetherWifiWhenPowered) {
netman->getTechnology(service->type())->setTethering(true);
}
} else {
updateServices();
}
}
// from plugin/qml
void QConnectionAgent::connectToType(const QString &type)
{
if (netman->technologyPathForType(type).isEmpty()) {
Q_EMIT errorReported("", "Type not valid");
return;
}
// Connman is using "cellular" and "wifi" as part of the service path
QString convType;
if (type.contains("mobile")) {
convType = "cellular";
} else if (type.contains("wlan")) {
convType = "wifi";
} else {
convType = type;
}
bool found = false;
for (Service elem : orderedServicesList) {
if (elem.path.contains(convType)) {
if (!isStateOnline(elem.service->serviceState())) {
if (elem.service->autoConnect()) {
qCDebug(connAgent) << "<<<<<<<<<<< requestConnect() >>>>>>>>>>>>";
elem.service->requestConnect();
return;
} else if (!elem.path.contains("cellular")) {
// ignore cellular that are not on autoconnect
found = true;
}
} else {
return;
}
}
}
// Can't connect to the service of a type that doesn't exist
if (!found)
return;
// Substitute "wifi" with "wlan" for lipstick
if (type.contains("wifi"))
convType = "wlan";
Q_EMIT configurationNeeded(convType);
}
void QConnectionAgent::updateServices()
{
qCDebug(connAgent) << Q_FUNC_INFO;
ServiceList oldServices = orderedServicesList;
orderedServicesList.clear();
for (const QString &tech: techPreferenceList) {
QVector<NetworkService*> services = netman->getServices(tech);
for (NetworkService *serv: services) {
const QString servicePath = serv->path();
qCDebug(connAgent) << "Known service:" << serv->name() << serv->strength();
Service elem;
elem.path = servicePath;
elem.service = serv;
orderedServicesList << elem;
if (!oldServices.contains(servicePath)) {
//new!
qCInfo(connAgent) << "New service:" << servicePath;
QObject::connect(serv, &NetworkService::serviceStateChanged,
this, &QConnectionAgent::serviceStateChanged, Qt::UniqueConnection);
QObject::connect(serv, &NetworkService::connectRequestFailed,
this, &QConnectionAgent::serviceErrorChanged, Qt::UniqueConnection);
QObject::connect(serv, &NetworkService::errorChanged,
this, &QConnectionAgent::servicesError, Qt::UniqueConnection);
QObject::connect(serv, &NetworkService::autoConnectChanged,
this, &QConnectionAgent::serviceAutoconnectChanged, Qt::UniqueConnection);
}
}
}
}
void QConnectionAgent::servicesError(const QString &errorMessage)
{
if (errorMessage.isEmpty())
return;
NetworkService *serv = static_cast<NetworkService *>(sender());
qCWarning(connAgent) << serv->name() << errorMessage;
onErrorReported(serv->path(), errorMessage);
}
void QConnectionAgent::networkManagerStateChanged(NetworkManager::State state)
{
qCInfo(connAgent) << "Network state:" << state;
if ((state == NetworkManager::OnlineState && netman->defaultRoute()->type() == "cellular")
|| (state == NetworkManager::IdleState)) {
if (tetheringWifiTech && tetheringWifiTech->powered()
&& !tetheringWifiTech->tethering())
tetheringWifiTech->scan();
// on gprs, scan wifi every scanTimeoutInterval minutes
if (scanTimeoutInterval != 0)
scanTimer->start(scanTimeoutInterval * 60 * 1000);
}
if (tetherWifiWhenPowered && state == NetworkManager::OnlineState) {
if (tetheringWifiTech->tethering()) {
if (netman->defaultRoute()->type() == "cellular") {
tetherWifiWhenPowered = false;
Q_EMIT wifiTetheringFinished(true);
}
} else {
tetheringWifiTech->setTethering(true);
}
}
}
void QConnectionAgent::connmanAvailabilityChanged(bool available)
{
if (available) {
setup();
}
}
void QConnectionAgent::setup()
{
qCDebug(connAgent) << Q_FUNC_INFO << netman->globalState();
delete ua;
ua = new UserAgent(this);
connect(ua, &UserAgent::connectionRequest, this, &QConnectionAgent::onConnectionRequest);
connect(ua, &UserAgent::errorReported, this, &QConnectionAgent::onErrorReported);
connect(ua, &UserAgent::userInputCanceled, this, &QConnectionAgent::userInputCanceled);
connect(ua, &UserAgent::userInputRequested, this, &QConnectionAgent::userInputRequested);
connect(ua, &UserAgent::browserRequested, this, &QConnectionAgent::onBrowserRequested);
updateServices();
offlineModeChanged(netman->offlineMode());
QSettings confFile;
confFile.beginGroup("Connectionagent");
scanTimeoutInterval = confFile.value("scanTimerInterval", "1").toUInt(); //in minutes
if (isStateOnline(netman->globalState())) {
qCInfo(connAgent) << "Default route type:" << netman->defaultRoute()->type();
if (netman->defaultRoute()->type() == "ethernet")
isEthernet = true;
if (netman->defaultRoute()->type() == "cellular" && scanTimeoutInterval != 0)
scanTimer->start(scanTimeoutInterval * 60 * 1000);
}
tetherBtWhenPowered = confFile.value("tetheringBtEnabled", false).toBool();
if (tetherBtWhenPowered) {
tetheringBtTech = netman->getTechnology("bluetooth");
if (tetheringBtTech) {
connect(tetheringBtTech, &NetworkTechnology::poweredChanged,
this, &QConnectionAgent::technologyPowerChanged);
connect(tetheringBtTech, &NetworkTechnology::tetheringChanged,
this, &QConnectionAgent::techTetheringChanged, Qt::UniqueConnection);
if (tetheringBtTech->powered()) {
tetheringBtTech->setTethering(true);
}
}
}
qCDebug(connAgent) << "Config file says" << confFile.value("connected", "online").toString();
}
void QConnectionAgent::technologyPowerChanged(bool powered)
{
NetworkTechnology *tech = static_cast<NetworkTechnology *>(sender());
if (tech->type() == "wifi") {
if (tetheringWifiTech)
qCInfo(connAgent) << tetheringWifiTech->name() << powered;
else
qCDebug(connAgent) << "tetheringWifiTech is null";
if (netman && powered && tetherWifiWhenPowered) {
// wifi tech might not be ready, so delay this
QTimer::singleShot(1000, this, &QConnectionAgent::enableWifiTethering);
}
} else if (tech->type() == "bluetooth") {
if (netman && powered && tetherBtWhenPowered) {
// This doesn't need to be turned off when de-powered
QTimer::singleShot(1000, this, &QConnectionAgent::enableBtTethering);
}
}
}
void QConnectionAgent::techChanged()
{
if (netman->getTechnologies().isEmpty()) {
knownTechnologies.clear();
}
if (netman->getTechnology("wifi") == nullptr) {
tetherWifiWhenPowered = false;
tetheringWifiTech = nullptr;
}
if (netman->getTechnology("bluetooth") == nullptr) {
tetheringBtTech = nullptr;
}
for (NetworkTechnology *technology: netman->getTechnologies()) {
if (!knownTechnologies.contains(technology->path())) {
knownTechnologies << technology->path();
if (technology->type() == "wifi") {
tetheringWifiTech = technology;
} else if (technology->type() == "bluetooth") {
tetheringBtTech = technology;
} else {
continue;
}
connect(technology, &NetworkTechnology::poweredChanged, this, &QConnectionAgent::technologyPowerChanged);
connect(technology, &NetworkTechnology::tetheringChanged,
this, &QConnectionAgent::techTetheringChanged, Qt::UniqueConnection);
} else {
knownTechnologies.removeOne(technology->path());
}
}
}
void QConnectionAgent::techTetheringChanged(bool on)
{
qCDebug(connAgent) << on;
NetworkTechnology *technology = static_cast<NetworkTechnology *>(sender());
if (technology && technology->type() == "bluetooth" && on) {
Q_EMIT bluetoothTetheringFinished(true);
} else if (technology && technology->type() == "wifi" && on && tetherWifiWhenPowered) {
QVector <NetworkService *> services = netman->getServices("cellular");
if (services.isEmpty())
return;
NetworkService* cellService = services.at(0);
if (cellService) {
if (cellService->serviceState() == NetworkService::IdleState
|| cellService->serviceState() == NetworkService::FailureState) {
qCInfo(connAgent) << "Requesting cell connect";
cellService->requestConnect();
} else if (cellService->connected()) {
tetherWifiWhenPowered = false;
Q_EMIT wifiTetheringFinished(true);
}
} else {
stopTethering("wifi");
}
}
}
void QConnectionAgent::offlineModeChanged(bool offline)
{
flightModeSuppression = offline;
if (offline) {
QTimer::singleShot(5 * 1000 * 60, this, &QConnectionAgent::flightModeDialogSuppressionTimeout); //5 minutes
}
}
void QConnectionAgent::flightModeDialogSuppressionTimeout()
{
flightModeSuppression = false;
}
void QConnectionAgent::serviceAutoconnectChanged(bool on)
{
NetworkService *service = qobject_cast<NetworkService *>(sender());
if (!service)
return;
qCDebug(connAgent) << service->path() << "AutoConnect is" << on;
if (!on) {
if (service->serviceState() != NetworkService::IdleState)
service->requestDisconnect();
}
}
void QConnectionAgent::scanTimeout()
{
if (!tetheringWifiTech || tetheringWifiTech->tethering())
return;
if (tetheringWifiTech->powered() && !tetheringWifiTech->connected() && netman->defaultRoute()->type() != "wifi" ) {
tetheringWifiTech->scan();
qCDebug(connAgent) << "start scanner" << scanTimeoutInterval;
if (scanTimeoutInterval != 0) {
scanTimer->start(scanTimeoutInterval * 60 * 1000);
}
}
}
void QConnectionAgent::removeAllTypes(const QString &type)
{
for (Service elem: orderedServicesList) {
if (elem.path.contains(type))
orderedServicesList.remove(elem.path);
}
}
bool QConnectionAgent::shouldSuppressError(const QString &error, bool cellular) const
{
if (error.isEmpty())
return true;
if (error == QLatin1String("Operation aborted"))
return true;
if (error == QLatin1String("Already connected"))
return true;
if (error == QLatin1String("No carrier") && cellular) // Don't report cellular carrier lost.
return true;
if (error == QLatin1String("connect-failed") && cellular && netman->offlineMode()) // Suppress errors when switching to offline mode
return true;
if (error == QLatin1String("In progress") || error.contains(QLatin1String("Method"))) // Catch dbus errors and discard
return true;
return false;
}
void QConnectionAgent::openConnectionDialog(const QString &type)
{
// open Connection Selector
QDBusInterface *connSelectorInterface = new QDBusInterface(QStringLiteral("com.jolla.lipstick.ConnectionSelector"),
QStringLiteral("/"),
QStringLiteral("com.jolla.lipstick.ConnectionSelectorIf"),
QDBusConnection::sessionBus(),
this);
connSelectorInterface->connection().connect(QStringLiteral("com.jolla.lipstick.ConnectionSelector"),
QStringLiteral("/"),
QStringLiteral("com.jolla.lipstick.ConnectionSelectorIf"),
QStringLiteral("connectionSelectorClosed"),
this,
SLOT(connectionSelectorClosed(bool)));
QList<QVariant> args;
args.append(type);
QDBusMessage reply = connSelectorInterface->callWithArgumentList(QDBus::NoBlock,
QStringLiteral("openConnection"), args);
}
void QConnectionAgent::startTethering(const QString &type)
{
if (type != "wifi" && type !="bluetooth") { // support wifi and bt
return;
}
qCDebug(connAgent) << "startTethering" << type;
NetworkTechnology *tetherTech = netman->getTechnology(type);
if (!tetherTech) {
if (type == "wifi") {
Q_EMIT wifiTetheringFinished(false);
} else {
Q_EMIT bluetoothTetheringFinished(false);
}
return;
}
bool techPowered = tetherTech->powered();
QSettings confFile;
confFile.beginGroup("Connectionagent");
if (type == "wifi") { // Only force cellular on for wifi. Bt can use either when available.
QVector <NetworkService *> services = netman->getServices("cellular");
if (services.isEmpty()) {
Q_EMIT wifiTetheringFinished(false);
return;
}
NetworkService *cellService = services.at(0);
if (!cellService || netman->offlineMode()) {
Q_EMIT wifiTetheringFinished(false);
return;
}
bool cellConnected = cellService->connected();
bool cellAutoconnect = cellService->autoConnect();
// save cellular connection state
confFile.setValue("tetheringCellularConnected", cellConnected);
confFile.setValue("tetheringCellularAutoconnect", cellAutoconnect);
// save wifi powered state
confFile.setValue("tetheringTechPowered", techPowered);
tetheringWifiTech = tetherTech;
tetherWifiWhenPowered = true;
// Only wifi tethering powers up when enabled. BT will wait until
// it's next turned on.
if (!techPowered) {
tetherTech->setPowered(true);
}
} else if (type == "bluetooth") {
// Bluetooth tethering is passive: it does not affect the network connection
// status, instead allowing devices to use the network whenever they are
// connected. It is persistent across flight mode and reboots.
tetheringBtTech = tetherTech;
tetherBtWhenPowered = true;
if (!confFile.value("tetheringBtEnabled", false).toBool()) {
confFile.setValue("tetheringBtEnabled", true);
}
}
if (techPowered) {
tetherTech->setTethering(true);
}
}
void QConnectionAgent::stopTethering(const QString &type, bool keepPowered)
{
QSettings confFile;
confFile.beginGroup("Connectionagent");
NetworkTechnology *tetherTech = netman->getTechnology(type);
if (tetherTech && tetherTech->tethering()) {
tetherTech->setTethering(false);
}
if (type == "wifi") { // restore cellular data state
tetherWifiWhenPowered = false;
bool b = confFile.value("tetheringCellularConnected").toBool();
bool ab = confFile.value("tetheringCellularAutoconnect").toBool();
for (Service elem : orderedServicesList) {
if (elem.path.contains("cellular")) {
if (isStateOnline(elem.service->serviceState())) {
qCDebug(connAgent) << "disconnect mobile data";
if (!b)
elem.service->requestDisconnect();
if (!ab)
elem.service->setAutoConnect(false);
}
}
}
b = confFile.value("tetheringTechPowered").toBool();
if (!b && tetherTech && !keepPowered) {
tetherTech->setPowered(false);
}
Q_EMIT wifiTetheringFinished(false);
} else if (type == "bluetooth") {
tetherBtWhenPowered = false;
confFile.setValue("tetheringBtEnabled", false);
if (tetherTech && !keepPowered) {
tetherTech->setPowered(false);
}
Q_EMIT bluetoothTetheringFinished(false);
}
}
void QConnectionAgent::enableWifiTethering()
{
if (tetheringWifiTech) {
qCInfo(connAgent) << "Setting Wifi tethering" << (tetherWifiWhenPowered ? "on" : "off");
tetheringWifiTech->setTethering(tetherWifiWhenPowered);
}
}
void QConnectionAgent::enableBtTethering()
{
if (tetheringBtTech) {
qCInfo(connAgent) << "Setting Bluetooth tethering" << (tetherBtWhenPowered ? "on" : "off");
tetheringBtTech->setTethering(tetherBtWhenPowered && tetheringBtTech->powered());
}
}