summaryrefslogtreecommitdiffstats
path: root/src/plugins/bearer/android/src/qandroidbearerengine.cpp
blob: ad9895e0cf2718aa917d60b9c8b2dcad378be5c4 (plain)
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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qandroidbearerengine.h"
#include <private/qnetworksession_impl_p.h>
#include "wrappers/androidconnectivitymanager.h"

#ifndef QT_NO_BEARERMANAGEMENT

QT_BEGIN_NAMESPACE

static QString networkConfType(const AndroidNetworkInfo &networkInfo)
{
    switch (networkInfo.getType()) {
    case AndroidNetworkInfo::Mobile:
        return QStringLiteral("Mobile");
    case AndroidNetworkInfo::Wifi:
        return QStringLiteral("WiFi");
    case AndroidNetworkInfo::Wimax:
        return QStringLiteral("WiMax");
    case AndroidNetworkInfo::Ethernet:
        return QStringLiteral("Ethernet");
    case AndroidNetworkInfo::Bluetooth:
        return QStringLiteral("Bluetooth");
    default:
        break;
    }

    return QString();
}

static inline bool isMobile(QNetworkConfiguration::BearerType type)
{
    if (type == QNetworkConfiguration::BearerWLAN
        || type == QNetworkConfiguration::BearerWiMAX
        || type == QNetworkConfiguration::BearerBluetooth
        || type == QNetworkConfiguration::BearerEthernet
        || type == QNetworkConfiguration::BearerUnknown) {
        return false;
    }

    return true;
}

static QNetworkConfiguration::BearerType getBearerType(const AndroidNetworkInfo &networkInfo)
{
    switch (networkInfo.getType()) {
    case AndroidNetworkInfo::Mobile:
    {
        switch (networkInfo.getSubtype()) {
        case AndroidNetworkInfo::Gprs:
        case AndroidNetworkInfo::Edge:
        case AndroidNetworkInfo::Iden:    // 2G
            return QNetworkConfiguration::Bearer2G;
        case AndroidNetworkInfo::Umts:    // BearerWCDMA (3 .5 .75 G)
        case AndroidNetworkInfo::Hsdpa:   // 3G (?) UMTS
        case AndroidNetworkInfo::Hsupa:   // 3G (?) UMTS
            return QNetworkConfiguration::BearerWCDMA;
        case AndroidNetworkInfo::Cdma:    // CDMA ISA95[AB]
        case AndroidNetworkInfo::Cdma1xRTT:   // BearerCDMA2000 (3G)
        case AndroidNetworkInfo::Ehrpd:   // CDMA Bridge thing?!?
            return QNetworkConfiguration::BearerCDMA2000;
        case AndroidNetworkInfo::Evdo0:   // BearerEVDO
        case AndroidNetworkInfo::EvdoA:   // BearerEVDO
        case AndroidNetworkInfo::EvdoB:   // BearerEVDO
            return QNetworkConfiguration::BearerEVDO;
        case AndroidNetworkInfo::Hspa:
        case AndroidNetworkInfo::Hspap:   // HSPA+
            return QNetworkConfiguration::BearerHSPA;
        case AndroidNetworkInfo::Lte:     // BearerLTE (4G)
            return QNetworkConfiguration::BearerLTE;
        default:
            break;
        }
    }
    case AndroidNetworkInfo::Wifi:
        return QNetworkConfiguration::BearerWLAN;
    case AndroidNetworkInfo::Wimax:
        return QNetworkConfiguration::BearerWiMAX;
    case AndroidNetworkInfo::Bluetooth:
    case AndroidNetworkInfo::MobileDun:
        return QNetworkConfiguration::BearerBluetooth;
    case AndroidNetworkInfo::Ethernet:
        return QNetworkConfiguration::BearerEthernet;
    case AndroidNetworkInfo::MobileMms:
    case AndroidNetworkInfo::MobileSupl:
    case AndroidNetworkInfo::MobileHipri:
    case AndroidNetworkInfo::Dummy:
    case AndroidNetworkInfo::UnknownType:
        break;
    }

    return QNetworkConfiguration::BearerUnknown;
}

QAndroidBearerEngine::QAndroidBearerEngine(QObject *parent)
    : QBearerEngineImpl(parent),
      m_connectivityManager(0)
{
}

QAndroidBearerEngine::~QAndroidBearerEngine()
{
}

QString QAndroidBearerEngine::getInterfaceFromId(const QString &id)
{
    const QMutexLocker locker(&mutex);
    return m_configurationInterface.value(id);
}

bool QAndroidBearerEngine::hasIdentifier(const QString &id)
{
    const QMutexLocker locker(&mutex);
    return m_configurationInterface.contains(id);
}

void QAndroidBearerEngine::connectToId(const QString &id)
{
    Q_EMIT connectionError(id, OperationNotSupported);
}

void QAndroidBearerEngine::disconnectFromId(const QString &id)
{
    Q_EMIT connectionError(id, OperationNotSupported);
}

QNetworkSession::State QAndroidBearerEngine::sessionStateForId(const QString &id)
{
    const QMutexLocker locker(&mutex);
    QNetworkConfigurationPrivatePointer ptr = accessPointConfigurations.value(id);

    if ((!ptr || !ptr->isValid) || m_connectivityManager == 0)
        return QNetworkSession::Invalid;

    const QMutexLocker configLocker(&ptr->mutex);
    // Don't re-order...
    if ((ptr->state & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) {
        return QNetworkSession::Connected;
    } else if ((ptr->state & QNetworkConfiguration::Discovered) == QNetworkConfiguration::Discovered) {
        return QNetworkSession::Disconnected;
    } else if ((ptr->state & QNetworkConfiguration::Defined) == QNetworkConfiguration::Defined) {
        return QNetworkSession::NotAvailable;
    } else if ((ptr->state & QNetworkConfiguration::Undefined) == QNetworkConfiguration::Undefined) {
        return QNetworkSession::NotAvailable;
    }

    return QNetworkSession::Invalid;
}

QNetworkConfigurationManager::Capabilities QAndroidBearerEngine::capabilities() const
{

    return AndroidTrafficStats::isTrafficStatsSupported()
            ? QNetworkConfigurationManager::ForcedRoaming
              | QNetworkConfigurationManager::DataStatistics
            : QNetworkConfigurationManager::ForcedRoaming;

}

QNetworkSessionPrivate *QAndroidBearerEngine::createSessionBackend()
{
    return new QNetworkSessionPrivateImpl();
}

QNetworkConfigurationPrivatePointer QAndroidBearerEngine::defaultConfiguration()
{
    return QNetworkConfigurationPrivatePointer();
}

bool QAndroidBearerEngine::requiresPolling() const
{
    return false;
}

quint64 QAndroidBearerEngine::bytesWritten(const QString &id)
{
    QMutexLocker lock(&mutex);
    QNetworkConfigurationPrivatePointer ptr = accessPointConfigurations.value(id);
    if (!ptr || !ptr->isValid)
        return 0;

    return isMobile(ptr->bearerType)
            ? AndroidTrafficStats::getMobileTxBytes()
            : AndroidTrafficStats::getTotalTxBytes() - AndroidTrafficStats::getMobileTxBytes();
}

quint64 QAndroidBearerEngine::bytesReceived(const QString &id)
{
    QMutexLocker lock(&mutex);
    QNetworkConfigurationPrivatePointer ptr = accessPointConfigurations.value(id);
    if (!ptr || !ptr->isValid)
        return 0;

    return isMobile(ptr->bearerType)
            ? AndroidTrafficStats::getMobileRxBytes()
            : AndroidTrafficStats::getTotalRxBytes() - AndroidTrafficStats::getMobileRxBytes();
}

quint64 QAndroidBearerEngine::startTime(const QString &id)
{
    Q_UNUSED(id);
    return Q_UINT64_C(0);
}

void QAndroidBearerEngine::initialize()
{
    if (m_connectivityManager != 0)
        return;

    m_connectivityManager = AndroidConnectivityManager::getInstance();
    if (m_connectivityManager == 0)
        return;

    updateConfigurations();

    connect(m_connectivityManager, &AndroidConnectivityManager::activeNetworkChanged,
            this, &QAndroidBearerEngine::updateConfigurations);

}

void QAndroidBearerEngine::requestUpdate()
{
    updateConfigurations();
}

void QAndroidBearerEngine::updateConfigurations()
{
#ifndef QT_NO_NETWORKINTERFACE
    if (m_connectivityManager == 0)
        return;

    {
        QMutexLocker locker(&mutex);
        QStringList oldKeys = accessPointConfigurations.keys();

        QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
        if (interfaces.isEmpty())
            interfaces = QNetworkInterface::allInterfaces();

        // Create a configuration for each of the main types (WiFi, Mobile, Bluetooth, WiMax, Ethernet)
        const auto netInfos = m_connectivityManager->getAllNetworkInfo();
        for (const AndroidNetworkInfo &netInfo : netInfos) {

            if (!netInfo.isValid())
                continue;

            const QString name = networkConfType(netInfo);
            if (name.isEmpty())
                continue;

            QNetworkConfiguration::BearerType bearerType = getBearerType(netInfo);

            QString interfaceName;
            QNetworkConfiguration::StateFlag state = QNetworkConfiguration::Defined;
            if (netInfo.isAvailable()) {
                if (netInfo.isConnected()) {
                    // Attempt to map an interface to this configuration
                    while (!interfaces.isEmpty()) {
                        QNetworkInterface interface = interfaces.takeFirst();
                        // ignore loopback interface
                        if (!interface.isValid())
                            continue;

                        if (interface.flags() & QNetworkInterface::IsLoopBack)
                            continue;
                        // There is no way to get the interface from the NetworkInfo, so
                        // look for an active interface...
                        if (interface.flags() & QNetworkInterface::IsRunning
                                && !interface.addressEntries().isEmpty()) {
                            state = QNetworkConfiguration::Active;
                            interfaceName = interface.name();
                            break;
                        }
                    }
                }
            }

            const QString key = QString(QLatin1String("android:%1:%2")).arg(name).arg(interfaceName);
            const QString id = QString::number(qHash(key));
            m_configurationInterface[id] = interfaceName;

            oldKeys.removeAll(id);
            if (accessPointConfigurations.contains(id)) {
                QNetworkConfigurationPrivatePointer ptr = accessPointConfigurations.value(id);
                bool changed = false;
                {
                    const QMutexLocker confLocker(&ptr->mutex);

                    if (!ptr->isValid) {
                        ptr->isValid = true;
                        changed = true;
                    }

                    // Don't reset the bearer type to 'Unknown'
                    if (ptr->bearerType != QNetworkConfiguration::BearerUnknown
                            && ptr->bearerType != bearerType) {
                        ptr->bearerType = bearerType;
                        changed = true;
                    }

                    if (ptr->name != name) {
                        ptr->name = name;
                        changed = true;
                    }

                    if (ptr->id != id) {
                        ptr->id = id;
                        changed = true;
                    }

                    if (ptr->state != state) {
                        ptr->state = state;
                        changed = true;
                    }
                } // Unlock configuration

                if (changed) {
                    locker.unlock();
                    Q_EMIT configurationChanged(ptr);
                    locker.relock();
                }
            } else {
                QNetworkConfigurationPrivatePointer ptr(new QNetworkConfigurationPrivate);
                ptr->name = name;
                ptr->isValid = true;
                ptr->id = id;
                ptr->state = state;
                ptr->type = QNetworkConfiguration::InternetAccessPoint;
                ptr->bearerType = bearerType;
                accessPointConfigurations.insert(id, ptr);
                locker.unlock();
                Q_EMIT configurationAdded(ptr);
                locker.relock();
            }
        }

        while (!oldKeys.isEmpty()) {
            QNetworkConfigurationPrivatePointer ptr = accessPointConfigurations.take(oldKeys.takeFirst());
            m_configurationInterface.remove(ptr->id);
            locker.unlock();
            Q_EMIT configurationRemoved(ptr);
            locker.relock();
        }

    } // Unlock engine

#endif // QT_NO_NETWORKINTERFACE

    Q_EMIT updateCompleted();
}

QT_END_NAMESPACE

#endif // QT_NO_BEARERMANAGEMENT