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
|
/*
*
* Connection Manager
*
* Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if_arp.h>
#include <linux/wireless.h>
#include <dbus/dbus.h>
#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
#include <connman/device.h>
#include <connman/driver.h>
#include <connman/log.h>
#include "inet.h"
#include "supplicant.h"
#define CLEANUP_TIMEOUT 8 /* in seconds */
#define INACTIVE_TIMEOUT 12 /* in seconds */
struct wifi_data {
GSList *current;
GSList *pending;
guint cleanup_timer;
guint inactive_timer;
gchar *identifier;
gboolean connected;
};
static int network_probe(struct connman_element *element)
{
DBG("element %p name %s", element, element->name);
return 0;
}
static void network_remove(struct connman_element *element)
{
DBG("element %p name %s", element, element->name);
}
static int network_enable(struct connman_element *element)
{
struct connman_device *device = (struct connman_device *) element->parent;
char *name, *security = NULL, *passphrase = NULL;
unsigned char *ssid;
int ssid_len;
DBG("element %p name %s", element, element->name);
if (connman_element_get_static_property(element,
"Name", &name) == FALSE)
return -EIO;
if (connman_element_get_static_array_property(element,
"WiFi.SSID", &ssid, &ssid_len) == FALSE)
return -EIO;
if (device != NULL) {
struct wifi_data *data = connman_device_get_data(device);
if (data != NULL) {
if (data->connected == TRUE)
return -EBUSY;
g_free(data->identifier);
data->identifier = g_strdup(name);
}
}
connman_element_get_value(element,
CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
connman_element_get_value(element,
CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
DBG("name %s security %s passhprase %s",
name, security, passphrase);
if (__supplicant_connect(element, ssid, ssid_len,
security, passphrase) < 0)
connman_error("Failed to initiate connect");
return 0;
}
static int network_disable(struct connman_element *element)
{
DBG("element %p name %s", element, element->name);
connman_element_unregister_children(element);
__supplicant_disconnect(element);
return 0;
}
static struct connman_driver network_driver = {
.name = "wifi-network",
.type = CONNMAN_ELEMENT_TYPE_NETWORK,
.subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
.probe = network_probe,
.remove = network_remove,
.enable = network_enable,
.disable = network_disable,
};
static struct connman_element *find_current_element(struct wifi_data *data,
const char *identifier)
{
GSList *list;
for (list = data->current; list; list = list->next) {
struct connman_element *element = list->data;
if (connman_element_match_static_property(element,
"Name", &identifier) == TRUE)
return element;
}
return NULL;
}
static struct connman_element *find_pending_element(struct wifi_data *data,
const char *identifier)
{
GSList *list;
for (list = data->pending; list; list = list->next) {
struct connman_element *element = list->data;
if (connman_element_match_static_property(element,
"Name", &identifier) == TRUE)
return element;
}
return NULL;
}
static gboolean inactive_scan(gpointer user_data)
{
struct connman_device *device = user_data;
struct wifi_data *data = connman_device_get_data(device);
DBG("device %p", device);
__supplicant_scan(device);
data->inactive_timer = 0;
return FALSE;
}
static void connect_known_networks(struct connman_device *device)
{
struct wifi_data *data = connman_device_get_data(device);
GSList *list;
DBG("device %p", device);
if (data->inactive_timer > 0) {
g_source_remove(data->inactive_timer);
data->inactive_timer = 0;
}
for (list = data->current; list; list = list->next) {
struct connman_element *element = list->data;
if (element->policy == CONNMAN_ELEMENT_POLICY_AUTO &&
element->remember == TRUE &&
element->available == TRUE) {
if (network_enable(element) == 0)
return;
}
}
data->inactive_timer = g_timeout_add_seconds(INACTIVE_TIMEOUT,
inactive_scan, device);
}
static void state_change(struct connman_device *device,
enum supplicant_state state)
{
struct wifi_data *data = connman_device_get_data(device);
struct connman_element *element;
DBG("device %p state %d", device, state);
if (state == STATE_SCANNING)
connman_device_set_scanning(device, TRUE);
else
connman_device_set_scanning(device, FALSE);
if (data == NULL)
return;
DBG("identifier %s", data->identifier);
if (data->identifier == NULL)
goto reconnect;
element = find_current_element(data, data->identifier);
if (element == NULL)
goto reconnect;
if (state == STATE_COMPLETED && data->connected == FALSE) {
struct connman_element *dhcp;
data->connected = TRUE;
connman_element_set_enabled(element, TRUE);
dhcp = connman_element_create(NULL);
dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
dhcp->index = element->index;
if (connman_element_register(dhcp, element) < 0)
connman_element_unref(dhcp);
} else if (state == STATE_INACTIVE || state == STATE_DISCONNECTED) {
data->connected = FALSE;
connman_element_set_enabled(element, FALSE);
connman_element_unregister_children(element);
}
reconnect:
if (state == STATE_INACTIVE) {
data->connected = FALSE;
connect_known_networks(device);
}
}
static gboolean cleanup_pending(gpointer user_data)
{
struct wifi_data *data = user_data;
GSList *list;
DBG("");
for (list = data->pending; list; list = list->next) {
struct connman_element *element = list->data;
DBG("element %p name %s", element, element->name);
connman_element_unregister(element);
connman_element_unref(element);
}
g_slist_free(data->pending);
data->pending = NULL;
data->cleanup_timer = 0;
return FALSE;
}
static void clear_results(struct connman_device *device)
{
struct wifi_data *data = connman_device_get_data(device);
DBG("pending %d", g_slist_length(data->pending));
DBG("current %d", g_slist_length(data->current));
if (data->cleanup_timer > 0) {
g_source_remove(data->cleanup_timer);
cleanup_pending(data);
}
data->pending = data->current;
data->current = NULL;
data->cleanup_timer = g_timeout_add_seconds(CLEANUP_TIMEOUT,
cleanup_pending, data);
}
static void scan_result(struct connman_device *device,
struct supplicant_network *network)
{
struct wifi_data *data = connman_device_get_data(device);
struct connman_element *element;
gchar *temp;
unsigned int i;
DBG("device %p identifier %s", device, network->identifier);
if (data == NULL)
return;
if (network->identifier == NULL)
return;
if (network->identifier[0] == '\0')
return;
temp = g_strdup(network->identifier);
for (i = 0; i < strlen(temp); i++) {
char tmp = temp[i];
if ((tmp < '0' || tmp > '9') && (tmp < 'A' || tmp > 'Z') &&
(tmp < 'a' || tmp > 'z'))
temp[i] = '_';
}
element = find_pending_element(data, network->identifier);
if (element == NULL) {
const char *mode;
element = connman_element_create(temp);
element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
element->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
element->index = connman_device_get_index(device);
connman_element_add_static_property(element, "Name",
DBUS_TYPE_STRING, &network->identifier);
connman_element_add_static_array_property(element, "WiFi.SSID",
DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
mode = (network->adhoc == TRUE) ? "adhoc" : "managed";
connman_element_add_static_property(element, "WiFi.Mode",
DBUS_TYPE_STRING, &mode);
if (element->wifi.security == NULL) {
const char *security;
if (network->has_rsn == TRUE)
security = "wpa2";
else if (network->has_wpa == TRUE)
security = "wpa";
else if (network->has_wep == TRUE)
security = "wep";
else
security = "none";
element->wifi.security = g_strdup(security);
}
element->strength = network->quality;
connman_element_add_static_property(element, "Strength",
DBUS_TYPE_BYTE, &element->strength);
DBG("%s (%s %s) strength %d", network->identifier, mode,
element->wifi.security, element->strength);
if (connman_element_register(element,
(struct connman_element *) device) < 0) {
connman_element_unref(element);
goto done;
}
} else {
data->pending = g_slist_remove(data->pending, element);
if (element->strength != network->quality) {
element->strength = network->quality;
connman_element_set_static_property(element, "Strength",
DBUS_TYPE_BYTE, &element->strength);
connman_element_update(element);
}
}
data->current = g_slist_append(data->current, element);
element->available = TRUE;
done:
g_free(temp);
}
static struct supplicant_callback wifi_callback = {
.state_change = state_change,
.clear_results = clear_results,
.scan_result = scan_result,
};
static int wifi_probe(struct connman_device *device)
{
struct wifi_data *data;
DBG("device %p", device);
data = g_try_new0(struct wifi_data, 1);
if (data == NULL)
return -ENOMEM;
data->connected = FALSE;
connman_device_set_data(device, data);
return 0;
}
static void wifi_remove(struct connman_device *device)
{
struct wifi_data *data = connman_device_get_data(device);
DBG("device %p", device);
connman_device_set_data(device, NULL);
g_free(data->identifier);
g_free(data);
}
static int wifi_enable(struct connman_device *device)
{
int err;
DBG("device %p", device);
err = __supplicant_start(device, &wifi_callback);
if (err < 0)
return err;
connman_device_set_powered(device, TRUE);
__supplicant_scan(device);
return 0;
}
static int wifi_disable(struct connman_device *device)
{
struct wifi_data *data = connman_device_get_data(device);
GSList *list;
DBG("device %p", device);
if (data->cleanup_timer > 0) {
g_source_remove(data->cleanup_timer);
cleanup_pending(data);
}
if (data->inactive_timer > 0) {
g_source_remove(data->inactive_timer);
data->inactive_timer = 0;
}
for (list = data->current; list; list = list->next) {
struct connman_element *network = list->data;
if (network->enabled == TRUE)
__supplicant_disconnect(network);
connman_element_unref(network);
}
g_slist_free(data->current);
data->current = NULL;
connman_element_unregister_children((struct connman_element *) device);
__supplicant_stop(device);
connman_device_set_powered(device, FALSE);
return 0;
}
static int wifi_scan(struct connman_device *device)
{
DBG("device %p", device);
__supplicant_scan(device);
return 0;
}
static struct connman_device_driver wifi_driver = {
.name = "wifi",
.type = CONNMAN_DEVICE_TYPE_WIFI,
.probe = wifi_probe,
.remove = wifi_remove,
.enable = wifi_enable,
.disable = wifi_disable,
.scan = wifi_scan,
};
static void wifi_register(void)
{
DBG("");
connman_device_driver_register(&wifi_driver);
}
static void wifi_unregister(void)
{
DBG("");
connman_device_driver_unregister(&wifi_driver);
}
static struct supplicant_driver supplicant = {
.name = "wifi",
.probe = wifi_register,
.remove = wifi_unregister,
};
static int wifi_init(void)
{
int err;
err = connman_driver_register(&network_driver);
if (err < 0)
return err;
err = supplicant_register(&supplicant);
if (err < 0) {
connman_driver_unregister(&network_driver);
return err;
}
return 0;
}
static void wifi_exit(void)
{
supplicant_unregister(&supplicant);
connman_driver_unregister(&network_driver);
}
CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
wifi_init, wifi_exit)
|