summaryrefslogtreecommitdiffstats
path: root/src/imports/wifi/qwifimanager.cpp
blob: 1ba3d640168c33f9fb4702f6235cd32197d65356 (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
#include "qwifimanager.h"

#include <QtCore>

#include <hardware_legacy/wifi.h>
#include <cutils/properties.h>
#include <unistd.h>

#define WLAN_INTERFACE "wlan0"

static bool QT_WIFI_DEBUG = !qgetenv("QT_WIFI_DEBUG").isEmpty();

const QEvent::Type WIFI_SCAN_RESULTS = (QEvent::Type) (QEvent::User + 2001);
const QEvent::Type WIFI_CONNECTED = (QEvent::Type) (QEvent::User + 2002);

static int q_wifi_start_supplicant()
{
#if Q_ANDROID_VERSION_MAJOR > 4 || (Q_ANDROID_VERSION_MAJOR == 4 && Q_ANDROID_VERSION_MINOR >= 1)
    return wifi_start_supplicant(0);
#else
    return wifi_start_supplicant();
#endif
}

static int q_wifi_connect_to_supplicant()
{
#if Q_ANDROID_VERSION_MAJOR > 4 || (Q_ANDROID_VERSION_MAJOR == 4 && Q_ANDROID_VERSION_MINOR >= 1)
    return wifi_connect_to_supplicant(WLAN_INTERFACE);
#else
    return wifi_connect_to_supplicant();
#endif
}

static int q_wifi_command(const char *command, char *reply, size_t *reply_len)
{
#if Q_ANDROID_VERSION_MAJOR > 4 || (Q_ANDROID_VERSION_MAJOR == 4 && Q_ANDROID_VERSION_MINOR >= 1)
    return wifi_command(WLAN_INTERFACE, command, reply, reply_len);
#else
    return wifi_command(command, reply, reply_len);
#endif

}

static int q_wifi_wait_for_event(char *buf, size_t len)
{
#if Q_ANDROID_VERSION_MAJOR > 4 || (Q_ANDROID_VERSION_MAJOR == 4 && Q_ANDROID_VERSION_MINOR >= 1)
    return wifi_wait_for_event(WLAN_INTERFACE, buf, len);
#else
    return wifi_wait_for_event(buf, len);
#endif
}


class QWifiManagerEvent : public QEvent
{
public:
    QWifiManagerEvent(QEvent::Type type, const QByteArray &data = QByteArray())
        : QEvent(type)
        , m_data(data)
    {
    }

    QByteArray data() const { return m_data; }

private:
    QByteArray m_data;
};



class QWifiManagerEventThread : public QThread
{
public:
    QWifiManagerEventThread(QWifiManager *manager)
        : m_manager(manager)
    {

    }

    void run() {
        if (QT_WIFI_DEBUG) qDebug("EventReceiver thread is running");
        char buffer[2048];
        while (1) {
            int size = q_wifi_wait_for_event(buffer, sizeof(buffer) - 1);
            if (size > 0) {
                buffer[size] = 0;

                if (QT_WIFI_DEBUG) qDebug("EVENT: %s", buffer);

                char *event = &buffer[11];
                if (strstr(event, "SCAN-RESULTS")) {
                    QWifiManagerEvent *e = new QWifiManagerEvent(WIFI_SCAN_RESULTS);
                    QCoreApplication::postEvent(m_manager, e);
                } else if (strstr(event, "CONNECTED")) {
                    QWifiManagerEvent *e = new QWifiManagerEvent(WIFI_CONNECTED);
                    QCoreApplication::postEvent(m_manager, e);
                }
            }
        }
    }

    QWifiManager *m_manager;
};



QWifiManager::QWifiManager()
    : m_networks(this)
    , m_eventThread(0)
    , m_scanTimer(0)
    , m_internalState(IS_Uninitialized)
    , m_scanning(false)
{
}



void QWifiManager::start()
{
    if (QT_WIFI_DEBUG) qDebug("QWifiManager: start");
    connectToBackend();
}



void QWifiManager::setScanning(bool scanning)
{
    if (m_scanning == scanning)
        return;

    m_scanning = scanning;
    emit scanningChanged(scanning);

    if (m_scanning) {
        if (QT_WIFI_DEBUG) qDebug("QWifiManager: scanning");
        call("SCAN");
        m_scanTimer = startTimer(5000);
    } else {
        if (QT_WIFI_DEBUG) qDebug("QWifiManager: stop scanning");
        killTimer(m_scanTimer);
    }
}



QByteArray int_to_ip(int i) {
    QByteArray ip;

    ip.append(QByteArray::number(i & 0x000000ff));
    ip.append('.');
    ip.append(QByteArray::number((i & 0x0000ff00) >> 8));
    ip.append('.');
    ip.append(QByteArray::number((i & 0x00ff0000) >> 16));
    ip.append('.');
    ip.append(QByteArray::number(i >> 24));

    return ip;
}


void QWifiManager::connectToBackend()
{
    if (m_internalState == IS_Uninitialized)
        m_internalState = IS_LoadDriver;

    if (m_internalState == IS_LoadDriver && (is_wifi_driver_loaded() || wifi_load_driver() >= 0))
        m_internalState = IS_StartBackend;

    if (m_internalState == IS_StartBackend && q_wifi_start_supplicant() >= 0) {
        m_internalState = IS_ConnectToBackend;
        sleep(3); //###
    }

    if (m_internalState == IS_ConnectToBackend && q_wifi_connect_to_supplicant() >= 0)
        m_internalState = IS_UpAndRunning;

    if (m_internalState == IS_UpAndRunning) {
        qDebug("QWifiManager: started successfully");

        emit readyChanged(true);
        m_eventThread = new QWifiManagerEventThread(this);
        m_eventThread->start();

        handleConnected();
    } else {
        qWarning("QWifiManager: stuck at internal state level: %d", m_internalState);
    }
}


QByteArray QWifiManager::call(const char *command)
{
    char data[2048];
    size_t len = sizeof(data) - 1;  // -1: room to add a 0-terminator
    if (q_wifi_command(command, data, &len) < 0) {
        qWarning("QWifiManager: call failed: %s", command);
        return QByteArray();
    }
    if (len < sizeof(data))
        data[len] = 0;
    QByteArray result = QByteArray::fromRawData(data, len);
    if (QT_WIFI_DEBUG) qDebug("QWifiManager::call: %s ==>\n%s", command, result.constData());
    return result;
}


bool QWifiManager::checkedCall(const char *command)
{
    return call(command).trimmed().toUpper() == "OK";
}


bool QWifiManager::event(QEvent *e)
{
    switch ((int) e->type()) {
    case WIFI_SCAN_RESULTS:
        m_networks.parseScanResults(call("SCAN_RESULTS"));
        return true;
    case WIFI_CONNECTED:
        handleConnected();
        break;
    case QEvent::Timer: {
        int tid = static_cast<QTimerEvent *>(e)->timerId();
        if (tid == m_scanTimer) {
            call("SCAN");
            return true;
        }
        break;
    }
    }

    return QObject::event(e);
}

void QWifiManager::connect(QWifiNetwork *network, const QString &passphrase)
{
    if (network->ssid() == m_connectedSSID) {
        if (QT_WIFI_DEBUG) qDebug("QWifiManager::connect(), already connected to %s", network->ssid().constData());
        return;
    }

    call("DISABLE_NETWORK all");
    if (!m_connectedSSID.isEmpty()) {
        m_connectedSSID.clear();
        emit connectedSSIDChanged(m_connectedSSID);
        //also possibly change online state
    }

    bool networkKnown = false;
    QByteArray id;
    QByteArray listResult = call("LIST_NETWORKS");
    QList<QByteArray> lines = listResult.split('\n');
    foreach (const QByteArray &line, lines) {
        if (line.contains(network->ssid())) {
            int networkId = line.toInt();
            id = QByteArray::number(networkId);
            networkKnown = true;
            break;
        }
    }

    if (!networkKnown) {
        bool ok;
        QByteArray id = call("ADD_NETWORK").trimmed();
        id.toInt(&ok);
        if (!ok) {
            qWarning("QWifiManager::connect(), failed to add network");
            return;
        }
    }
    QByteArray setNetworkCommand = QByteArray("SET_NETWORK ") + id;

    bool ok = true;
    if (!networkKnown)
        ok = ok && checkedCall(setNetworkCommand + QByteArray(" ssid ") + '"' + network->ssid() + '"');

    QByteArray key_mgmt;
    if (network->supportsWPA() || network->supportsWPA2()) {
        ok = ok && checkedCall(setNetworkCommand + QByteArray(" psk ") + '"' + passphrase.toLatin1() + '"');
        key_mgmt = "WPA_PSK";
    } else if (network->supportsWEP()) {
        ok = ok && checkedCall(setNetworkCommand + QByteArray(" wep_key0 ") + '"' + passphrase.toLatin1() + '"');
        ok = ok && checkedCall(setNetworkCommand + QByteArray(" auth_alg OPEN SHARED"));
        key_mgmt = "NONE";
    } else if (!network->supportsWPS() && passphrase.length() == 0) {
        // open network
        key_mgmt = "NONE";
    }
    ok = ok && checkedCall(setNetworkCommand + QByteArray(" key_mgmt ") + key_mgmt);

    if (!ok) {
        if (!networkKnown)
            call("REMOVE_NETWORK " + id);
        qWarning("QWifiManager::connect(), failed to set properties on network '%s'", id.constData());
        return;
    }

    call(QByteArray("SELECT_NETWORK ") + id);
    call("RECONNECT");
}


class ProcessRunner : public QThread {
public:
    void run() {
        QStringList args;
        args << QStringLiteral("-A")
         << QStringLiteral("-h") << QStringLiteral("KAON")  //### hardcoded hostname, for testing
         << QStringLiteral("wlan0");
        QProcess::execute(QStringLiteral("dhcpcd"), args);
        deleteLater();
    }
};


void QWifiManager::handleConnected()
{
    QList<QByteArray> lists = call("LIST_NETWORKS").split('\n');
    QByteArray connectedNetwork;
    for (int i=1; i<lists.size(); ++i) {
        if (lists.at(i).toUpper().contains("[CURRENT]")) {
            connectedNetwork = lists.at(i);
            break;
        }
    }

    if (connectedNetwork.isEmpty()) {
        if (QT_WIFI_DEBUG) qDebug("QWifiManager::handleConnected: not connected to a network...");
        m_online = false;
        onlineChanged(m_online);
        return;
    } else {
        if (QT_WIFI_DEBUG) qDebug("QWifiManager::handleConnected: current is %s", connectedNetwork.constData());
    }

    QList<QByteArray> info = connectedNetwork.split('\t');
    m_connectedSSID = info.at(1);
    emit connectedSSIDChanged(m_connectedSSID);

    if (QT_WIFI_DEBUG) qDebug("QWifiManager::handleConnected: starting dhcpcd...");
    QThread *t = new ProcessRunner();
    t->start();

    int ipaddr, gateway, mask, dns1, dns2, server, lease;
    if (do_dhcp_request(&ipaddr, &gateway, &mask, &dns1, &dns2, &server, &lease) == 0) {
        if (QT_WIFI_DEBUG) {
            printf("ip ........: %s\n"
                   "gateway ...: %s\n"
                   "mask ......: %s\n"
                   "dns1 ......: %s\n"
                   "dns2 ......: %s\n"
                   "lease .....: %d\n",
                   int_to_ip(ipaddr).constData(),
                   int_to_ip(gateway).constData(),
                   int_to_ip(mask).constData(),
                   int_to_ip(dns1).constData(),
                   int_to_ip(dns2).constData(),
                   lease);
        }

        // Updating dns values..
        property_set("net.dns1", int_to_ip(dns1).constData());
        property_set("net.dns2", int_to_ip(dns2).constData());

        // Store (possibly updated) settings
        call("SAVE_CONFIG");

        m_online = true;
        onlineChanged(m_online);

     } else {
        if (QT_WIFI_DEBUG) qDebug("QWifiManager::handleConnected: dhcp request failed...");
     }
}