/
qofonoconnectioncontext.cpp
557 lines (473 loc) · 16 KB
/
qofonoconnectioncontext.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
/****************************************************************************
**
** Copyright (C) 2013-2020 Jolla Ltd.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
****************************************************************************/
#ifdef QOFONO_PROVISIONING
#include <QtXmlPatterns/QXmlQuery>
#endif
#include "dbustypes_p.h"
#include "qofonoconnectioncontext.h"
#include "qofonoconnectionmanager.h"
#include "qofononetworkregistration.h"
#include "ofono_connection_context_interface.h"
#define SUPER QOfonoObject
class QOfonoConnectionContext::Private : public SUPER::ExtData
{
public:
bool provisioning;
bool mgrValid;
QSharedPointer<QOfonoConnectionManager> mgr;
Private() : provisioning(false), mgrValid(false) {}
~Private() {}
};
QOfonoConnectionContext::QOfonoConnectionContext(QObject *parent)
: SUPER(new Private, parent)
{
}
QOfonoConnectionContext::~QOfonoConnectionContext()
{
}
QDBusAbstractInterface *QOfonoConnectionContext::createDbusInterface(const QString &path)
{
return new OfonoConnectionContext(OFONO_SERVICE, path, OFONO_BUS, this);
}
void QOfonoConnectionContext::objectPathChanged(const QString &path, const QVariantMap *properties)
{
SUPER::objectPathChanged(path, properties);
Q_EMIT contextPathChanged(path);
}
QString QOfonoConnectionContext::contextPath() const
{
return objectPath();
}
QString QOfonoConnectionContext::modemPath() const
{
QString path = objectPath();
int lastSlash = path.lastIndexOf('/');
return (lastSlash > 0) ? path.left(lastSlash) : QString();
}
void QOfonoConnectionContext::setContextPath(const QString &path)
{
if (path != objectPath()) {
ValidTracker valid(this);
QString oldModemPath(modemPath());
setObjectPath(path);
QString newModemPath(modemPath());
if (oldModemPath != newModemPath) {
Private *priv = privateData();
if (!priv->mgr.isNull()) {
priv->mgr->disconnect(this);
priv->mgr.reset();
priv->mgrValid = false;
}
if (!newModemPath.isEmpty()) {
priv->mgr = QOfonoConnectionManager::instance(newModemPath);
priv->mgrValid = priv->mgr.data()->isValid();
connect(priv->mgr.data(), SIGNAL(validChanged(bool)),
this, SLOT(onManagerValidChanged(bool)));
}
Q_EMIT modemPathChanged(newModemPath);
}
}
}
void QOfonoConnectionContext::onManagerValidChanged(bool valid)
{
ValidTracker track(this);
privateData()->mgrValid = valid;
if (valid) {
resetDbusInterface();
} else {
setDbusInterface(NULL);
}
}
QVariant QOfonoConnectionContext::convertProperty(const QString &key, const QVariant &value)
{
if (key == QLatin1String("Settings") ||
key == QLatin1String("IPv6.Settings")) {
QVariantMap map;
value.value<QDBusArgument>() >> map;
return map;
} else {
return SUPER::convertProperty(key, value);
}
}
void QOfonoConnectionContext::propertyChanged(const QString &property, const QVariant &value)
{
SUPER::propertyChanged(property, value);
if (property == QLatin1String("Active")) {
Q_EMIT activeChanged(value.value<bool>());
} else if (property == QLatin1String("Name")) {
Q_EMIT nameChanged(value.value<QString>());
} else if (property == QLatin1String("AccessPointName")) {
Q_EMIT accessPointNameChanged(value.value<QString>());
} else if (property == QLatin1String("Type")) {
Q_EMIT typeChanged(value.value<QString>());
} else if (property == QLatin1String("AuthenticationMethod")) {
Q_EMIT authMethodChanged(value.value<QString>());
} else if (property == QLatin1String("Username")) {
Q_EMIT usernameChanged(value.value<QString>());
} else if (property == QLatin1String("Password")) {
Q_EMIT passwordChanged(value.value<QString>());
} else if (property == QLatin1String("Protocol")) {
Q_EMIT protocolChanged(value.value<QString>());
} else if (property == QLatin1String("MessageProxy")) {
Q_EMIT messageProxyChanged(value.value<QString>());
} else if (property == QLatin1String("MessageCenter")) {
Q_EMIT messageCenterChanged(value.value<QString>());
} else if (property == QLatin1String("Settings")) {
Q_EMIT settingsChanged(getVariantMap("Settings"));
} else if (property == QLatin1String("IPv6.Settings")) {
Q_EMIT IPv6SettingsChanged(getVariantMap("IPv6.Settings"));
}
}
bool QOfonoConnectionContext::active() const
{
return getBool("Active");
}
QString QOfonoConnectionContext::accessPointName() const
{
return getString("AccessPointName");
}
QString QOfonoConnectionContext::type() const
{
return getString("Type");
}
QString QOfonoConnectionContext::authMethod() const
{
return getString("AuthenticationMethod");
}
QString QOfonoConnectionContext::username() const
{
return getString("Username");
}
QString QOfonoConnectionContext::password() const
{
return getString("Password");
}
QString QOfonoConnectionContext::protocol() const
{
return getString("Protocol");
}
QString QOfonoConnectionContext::name() const
{
return getString("Name");
}
QString QOfonoConnectionContext::messageProxy() const
{
return getString("MessageProxy");
}
QString QOfonoConnectionContext::messageCenter() const
{
return getString("MessageCenter");
}
QVariantMap QOfonoConnectionContext::settings() const
{
return getVariantMap("Settings");
}
QVariantMap QOfonoConnectionContext::IPv6Settings() const
{
return getVariantMap("IPv6.Settings");
}
void QOfonoConnectionContext::setActive(const bool value)
{
// need someway to tell ui that someone wants to disconnect
if (!value) {
Q_EMIT disconnectRequested();
}
setProperty("Active", value);
}
void QOfonoConnectionContext::setAccessPointName(const QString &value)
{
setProperty("AccessPointName", value);
}
void QOfonoConnectionContext::setType(const QString &value)
{
setProperty("Type", value);
}
void QOfonoConnectionContext::setAuthMethod(const QString &value)
{
setProperty("AuthenticationMethod", value);
}
void QOfonoConnectionContext::setUsername(const QString &value)
{
setProperty("Username", value);
}
void QOfonoConnectionContext::setPassword(const QString &value)
{
setProperty("Password", value);
}
void QOfonoConnectionContext::setProtocol(const QString &value)
{
setProperty("Protocol", value);
}
void QOfonoConnectionContext::setName(const QString &value)
{
setProperty("Name", value);
}
void QOfonoConnectionContext::setMessageProxy(const QString &value)
{
setProperty("MessageProxy", value);
}
void QOfonoConnectionContext::setMessageCenter(const QString &value)
{
setProperty("MessageCenter", value);
}
void QOfonoConnectionContext::disconnect()
{
Q_EMIT disconnectRequested();
setPropertySync("Active", false);
}
bool QOfonoConnectionContext::isValid() const
{
return privateData()->mgrValid && SUPER::isValid();
}
bool QOfonoConnectionContext::provisioning() const
{
return privateData()->provisioning;
}
QOfonoConnectionContext::Private *QOfonoConnectionContext::privateData() const
{
return (QOfonoConnectionContext::Private*)SUPER::extData();
}
bool QOfonoConnectionContext::provision()
{
Private *priv = privateData();
if (!priv->provisioning) {
OfonoConnectionContext *iface = (OfonoConnectionContext*)dbusInterface();
if (iface) {
priv->provisioning = true;
Q_EMIT provisioningChanged(true);
connect(new QDBusPendingCallWatcher(iface->ProvisionContext(), iface),
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(onProvisionContextFinished(QDBusPendingCallWatcher*)));
return true;
}
}
return false;
}
void QOfonoConnectionContext::onProvisionContextFinished(QDBusPendingCallWatcher *watch)
{
watch->deleteLater();
QDBusPendingReply<> reply(*watch);
QString error;
if (reply.isError()) {
qWarning() << "Provisioning failed:" << reply.error();
error = reply.error().name();
}
privateData()->provisioning = false;
Q_EMIT provisioningChanged(false);
Q_EMIT provisioningFinished(error);
}
/*
* These provisioning functions use the mobile broadband provider info database available from this url:
* https://git.gnome.org/browse/mobile-broadband-provider-info/
*
**/
/*
* Tries to validate the context against the current registered network
**/
bool QOfonoConnectionContext::validateProvisioning()
{
#ifdef QOFONO_PROVISIONING
QString modem(modemPath());
qDebug() << modem;
if (modem.isEmpty())
return false;
QOfonoNetworkRegistration netReg;
netReg.setModemPath(modem);
// This won't work because ofono queries are asynchronous:
if (netReg.status() == "registered")
return validateProvisioning(netReg.networkOperators().at(0),netReg.mcc(),netReg.mnc());
#endif // QOFONO_PROVISIONING
return false;
}
/*
* Tries to validate the context using the provider, mcc and mnc arguments
**/
bool QOfonoConnectionContext::validateProvisioning(const QString &providerString, const QString &mcc, const QString &mnc)
{
#ifdef QOFONO_PROVISIONING
qDebug() << providerString;
QXmlQuery query;
QString provider = providerString;
query.setFocus(QUrl("/usr/share/mobile-broadband-provider-info/serviceproviders.xml"));
if (provider.contains("\'")) {
provider = provider.replace("\'", "'");
}
//provider
query.setQuery("/serviceproviders/country/provider[ name = '"+provider+"']/string()");
QString providerName;
query.evaluateTo(&providerName);
providerName = providerName.simplified();
if (providerName.isEmpty() && provider.at(1).isLower() ) {
//try with uppercase
provider[0] = provider.at(0).toUpper();
query.setQuery("/serviceproviders/country/provider[ name = '"+provider+"']/string()");
query.evaluateTo(&providerName);
if (providerName.isEmpty()) {
qDebug() << "provider not found";
return false;
}
}
// apn
query.setQuery("/serviceproviders/country/provider[ name = '"+provider+"']/gsm[ network-id [ @mcc = '"+mcc+"' and @mnc = '"+mnc+"' ] ]/apn/@value/string()");
QStringList accessPointNameList;
query.evaluateTo(&accessPointNameList);
if (accessPointNameList.isEmpty()) {
qDebug() << "APN not found";
return false;
}
QString apn = accessPointName();
if (!accessPointNameList.contains(apn)) {
qDebug() << "APN not valid"<< accessPointName();
return false;
}
QString queryString("/serviceproviders/country/provider[ name = '"+provider+"']//gsm[ network-id[@mcc = '"+mcc+"' and @mnc = '"+mnc+"']]/apn [ @value = '"+apn+ "']/");
//type
query.setQuery(queryString+"usage/@type/string()");
QString typeStr;
query.evaluateTo(&typeStr);
typeStr = typeStr.simplified();
if (!typeStr.isEmpty() && !typeStr.contains(type())) {
qDebug() << "type is not the same"
<< typeStr << type();
return false;
}
//dns
// ofono dns properties are read only
//username
query.setQuery(queryString+"username/string()");
QString usernameStr;
query.evaluateTo(&usernameStr);
if (!usernameStr.contains(username())) {
qDebug() << "username is not the same";
return false;
}
//password
query.setQuery(queryString+"password/string()");
QString passwordStr;
query.evaluateTo(&passwordStr);
if (!passwordStr.contains(password())) {
qDebug() << "password is not the same";
return false;
}
//we got here, must be ok
return true;
#else // QOFONO_PROVISIONING
return false;
#endif
}
void QOfonoConnectionContext::provisionForCurrentNetwork(const QString &type)
{
#ifdef QOFONO_PROVISIONING
QString modem(modemPath());
if (modem.isEmpty())
return;
QOfonoNetworkRegistration netReg;
netReg.setModemPath(modem);
if (netReg.status() == "registered")
provision(netReg.name(), netReg.mcc(),netReg.mnc(), type);
#endif // QOFONO_PROVISIONING
}
/*
* Tries to provision the context using the provider, mcc and mnc given.
*
* In the case of multiple apn's for given provider and type, this will provision the context
* using the first available apn.
*
* The only way to see if this is a working context is to try to activate the context.
*
**/
void QOfonoConnectionContext::provision(const QString &provider, const QString &mcc, const QString &mnc, const QString &type)
{
#ifdef QOFONO_PROVISIONING
QXmlQuery query;
query.setFocus(QUrl("/usr/share/mobile-broadband-provider-info/serviceproviders.xml"));
QString providerStr = provider;
if (providerStr.contains("\'")) {
providerStr.replace("\'", "'");
}
// provider
query.setQuery("/serviceproviders/country/provider[ name = '"+providerStr+"']/string()");
QString providerName;
query.evaluateTo(&providerName);
providerName = providerName.simplified();
if (providerName.isEmpty() && providerStr.at(0).isLower() ) {
//try with uppercase first letter
providerStr[0] = providerStr.at(0).toUpper();
query.setQuery("/serviceproviders/country/provider [ name = '"+providerStr+"']/string()");
query.evaluateTo(&providerName);
if (providerName.isEmpty()) {
Q_EMIT reportError("Provider not found");
return;
}
}
// apn
query.setQuery("/serviceproviders/country/provider[ name = '"+providerStr+"']/gsm[ network-id [ @mcc = '"+mcc+"' and @mnc = '"+mnc+"' ] ]/apn/@value/string()");
QStringList accessPointNameList;
query.evaluateTo(&accessPointNameList);
if (accessPointNameList.isEmpty()) {
Q_EMIT reportError("APN not found");
return;
}
Q_FOREACH( const QString &apn, accessPointNameList) {
QString queryString("/serviceproviders/country/provider[ name = '"+providerStr+"']//gsm[ network-id[@mcc = '"+mcc+"' and @mnc = '"+mnc+"']]/apn [ @value = '"+apn+ "']/");
// type
query.setQuery(queryString+"usage/@type/string()");
QString typeStr;
query.evaluateTo(&typeStr);
typeStr = typeStr.simplified();
if (typeStr.isEmpty()) {
typeStr = "internet";
}
if (typeStr != type) {
qDebug() << typeStr << "continue";
continue;
}
// name
query.setQuery(queryString+"name/string()");
QString nameStr;
query.evaluateTo(&nameStr);
nameStr = nameStr.simplified();
if (nameStr.isEmpty())
nameStr = providerStr;
// username
query.setQuery(queryString+"username/string()");
QString usernameStr;
query.evaluateTo(&usernameStr);
usernameStr = usernameStr.simplified();
// password
query.setQuery(queryString+"password/string()");
QString passwordStr;
query.evaluateTo(&passwordStr);
passwordStr = passwordStr.simplified();
if (!nameStr.isEmpty()) {
setPropertySync("Name", nameStr);
}
if (!type.isEmpty()) {
setPropertySync("Type", type);
}
if (!apn.isEmpty()) {
setPropertySync("AccessPointName", apn);
}
if (!passwordStr.isEmpty()) {
setPropertySync("Password", passwordStr);
}
if (!usernameStr.isEmpty()) {
setPropertySync("Username", usernameStr);
}
break;
}
Q_EMIT setPropertyFinished();
Q_EMIT provisioningFinished();
#endif // QOFONO_PROVISIONING
}