wifi: Add support for default background scanning parameters
[framework/connectivity/connman.git] / plugins / wifi.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
36
37 #ifndef IFF_LOWER_UP
38 #define IFF_LOWER_UP    0x10000
39 #endif
40
41 #include <dbus/dbus.h>
42 #include <glib.h>
43
44 #define CONNMAN_API_SUBJECT_TO_CHANGE
45 #include <connman/plugin.h>
46 #include <connman/inet.h>
47 #include <connman/device.h>
48 #include <connman/rtnl.h>
49 #include <connman/technology.h>
50 #include <connman/log.h>
51 #include <connman/option.h>
52 #include <connman/storage.h>
53
54 #include <gsupplicant/gsupplicant.h>
55
56 #define CLEANUP_TIMEOUT   8     /* in seconds */
57 #define INACTIVE_TIMEOUT  12    /* in seconds */
58 #define MAXIMUM_RETRIES   4
59
60 #define BGSCAN_DEFAULT "simple:30:-45:300"
61
62 struct connman_technology *wifi_technology = NULL;
63
64 struct hidden_params {
65         char ssid[32];
66         unsigned int ssid_len;
67         char *identity;
68         char *passphrase;
69 };
70
71 struct wifi_data {
72         char *identifier;
73         struct connman_device *device;
74         struct connman_network *network;
75         struct connman_network *pending_network;
76         GSList *networks;
77         GSupplicantInterface *interface;
78         GSupplicantState state;
79         connman_bool_t connected;
80         connman_bool_t disconnecting;
81         connman_bool_t tethering;
82         connman_bool_t bridged;
83         const char *bridge;
84         int index;
85         unsigned flags;
86         unsigned int watch;
87         int retries;
88         struct hidden_params *hidden;
89 };
90
91 static GList *iface_list = NULL;
92
93 static void handle_tethering(struct wifi_data *wifi)
94 {
95         if (wifi->tethering == FALSE)
96                 return;
97
98         if (wifi->bridge == NULL)
99                 return;
100
101         if (wifi->bridged == TRUE)
102                 return;
103
104         DBG("index %d bridge %s", wifi->index, wifi->bridge);
105
106         if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
107                 return;
108
109         wifi->bridged = TRUE;
110 }
111
112 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
113 {
114         struct connman_device *device = user_data;
115         struct wifi_data *wifi = connman_device_get_data(device);
116
117         DBG("index %d flags %d change %d", wifi->index, flags, change);
118
119         if (!change)
120                 return;
121
122         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
123                 if (flags & IFF_UP)
124                         DBG("interface up");
125                 else
126                         DBG("interface down");
127         }
128
129         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
130                 if (flags & IFF_LOWER_UP) {
131                         DBG("carrier on");
132
133                         handle_tethering(wifi);
134                 } else
135                         DBG("carrier off");
136         }
137
138         wifi->flags = flags;
139 }
140
141 static int wifi_probe(struct connman_device *device)
142 {
143         struct wifi_data *wifi;
144
145         DBG("device %p", device);
146
147         wifi = g_try_new0(struct wifi_data, 1);
148         if (wifi == NULL)
149                 return -ENOMEM;
150
151         wifi->connected = FALSE;
152         wifi->disconnecting = FALSE;
153         wifi->tethering = FALSE;
154         wifi->bridged = FALSE;
155         wifi->bridge = NULL;
156         wifi->state = G_SUPPLICANT_STATE_INACTIVE;
157
158         connman_device_set_data(device, wifi);
159         wifi->device = connman_device_ref(device);
160
161         wifi->index = connman_device_get_index(device);
162         wifi->flags = 0;
163
164         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
165                                                         wifi_newlink, device);
166
167         iface_list = g_list_append(iface_list, wifi);
168
169         return 0;
170 }
171
172 static void remove_networks(struct connman_device *device,
173                                 struct wifi_data *wifi)
174 {
175         GSList *list;
176
177         for (list = wifi->networks; list != NULL; list = list->next) {
178                 struct connman_network *network = list->data;
179
180                 connman_device_remove_network(device, network);
181                 connman_network_unref(network);
182         }
183
184         g_slist_free(wifi->networks);
185         wifi->networks = NULL;
186 }
187
188 static void wifi_remove(struct connman_device *device)
189 {
190         struct wifi_data *wifi = connman_device_get_data(device);
191
192         DBG("device %p wifi %p", device, wifi);
193
194         if (wifi == NULL)
195                 return;
196
197         iface_list = g_list_remove(iface_list, wifi);
198
199         remove_networks(device, wifi);
200
201         connman_device_set_powered(device, FALSE);
202         connman_device_set_data(device, NULL);
203         connman_device_unref(wifi->device);
204         connman_rtnl_remove_watch(wifi->watch);
205
206         g_supplicant_interface_set_data(wifi->interface, NULL);
207
208         g_free(wifi->identifier);
209         g_free(wifi);
210 }
211
212 static void interface_create_callback(int result,
213                                         GSupplicantInterface *interface,
214                                                         void *user_data)
215 {
216         struct wifi_data *wifi = user_data;
217
218         DBG("result %d ifname %s, wifi %p", result,
219                                 g_supplicant_interface_get_ifname(interface),
220                                 wifi);
221
222         if (result < 0 || wifi == NULL)
223                 return;
224
225         wifi->interface = interface;
226         g_supplicant_interface_set_data(interface, wifi);
227
228         if (g_supplicant_interface_get_ready(interface) == FALSE)
229                 return;
230
231         DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
232
233         if (wifi->device == NULL) {
234                 connman_error("WiFi device not set");
235                 return;
236         }
237
238         connman_device_set_powered(wifi->device, TRUE);
239 }
240
241 static int wifi_enable(struct connman_device *device)
242 {
243         struct wifi_data *wifi = connman_device_get_data(device);
244         const char *interface = connman_device_get_string(device, "Interface");
245         const char *driver = connman_option_get_string("wifi");
246         int ret;
247
248         DBG("device %p %p", device, wifi);
249
250         ret = g_supplicant_interface_create(interface, driver, NULL,
251                                                 interface_create_callback,
252                                                         wifi);
253         if (ret < 0)
254                 return ret;
255
256         return -EINPROGRESS;
257 }
258
259 static int wifi_disable(struct connman_device *device)
260 {
261         struct wifi_data *wifi = connman_device_get_data(device);
262         int ret;
263
264         DBG("device %p", device);
265
266         wifi->connected = FALSE;
267         wifi->disconnecting = FALSE;
268
269         if (wifi->pending_network != NULL)
270                 wifi->pending_network = NULL;
271
272         remove_networks(device, wifi);
273
274         ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
275         if (ret < 0)
276                 return ret;
277
278         return -EINPROGRESS;
279 }
280
281 static void hidden_free(struct hidden_params *hidden)
282 {
283         if (hidden == NULL)
284                 return;
285
286         g_free(hidden->identity);
287         g_free(hidden->passphrase);
288         g_free(hidden);
289 }
290
291 static void scan_callback(int result, GSupplicantInterface *interface,
292                                                 void *user_data)
293 {
294         struct connman_device *device = user_data;
295         struct wifi_data *wifi = connman_device_get_data(device);
296
297         DBG("result %d", result);
298
299         if (wifi != NULL && wifi->hidden != NULL) {
300                 hidden_free(wifi->hidden);
301                 wifi->hidden = NULL;
302         }
303
304         if (result < 0)
305                 connman_device_reset_scanning(device);
306
307         connman_device_set_scanning(device, FALSE);
308         connman_device_unref(device);
309 }
310
311 static int add_scan_param(gchar *hex_ssid, int freq,
312                         GSupplicantScanParams *scan_data,
313                         int driver_max_scan_ssids)
314 {
315         unsigned int i;
316
317         if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
318                 gchar *ssid;
319                 unsigned int j = 0, hex;
320                 size_t hex_ssid_len = strlen(hex_ssid);
321
322                 ssid = g_try_malloc0(hex_ssid_len / 2);
323                 if (ssid == NULL)
324                         return -ENOMEM;
325
326                 for (i = 0; i < hex_ssid_len; i += 2) {
327                         sscanf(hex_ssid + i, "%02x", &hex);
328                         ssid[j++] = hex;
329                 }
330
331                 memcpy(scan_data->ssids[scan_data->num_ssids].ssid, ssid, j);
332                 scan_data->ssids[scan_data->num_ssids].ssid_len = j;
333                 scan_data->num_ssids++;
334
335                 g_free(ssid);
336         }
337
338         /* Don't add duplicate entries */
339         for (i = 0; i < G_SUPPLICANT_MAX_FAST_SCAN; i++) {
340                 if (scan_data->freqs[i] == 0) {
341                         scan_data->freqs[i] = freq;
342                         break;
343                 } else if (scan_data->freqs[i] == freq)
344                         break;
345         }
346
347         return 0;
348 }
349
350 struct last_connected {
351         GTimeVal modified;
352         gchar *ssid;
353         int freq;
354 };
355
356 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
357 {
358         GTimeVal *aval = (GTimeVal *)a;
359         GTimeVal *bval = (GTimeVal *)b;
360
361         /* Note that the sort order is descending */
362         if (aval->tv_sec < bval->tv_sec)
363                 return 1;
364
365         if (aval->tv_sec > bval->tv_sec)
366                 return -1;
367
368         return 0;
369 }
370
371 static void free_entry(gpointer data)
372 {
373         struct last_connected *entry = data;
374
375         g_free(entry->ssid);
376         g_free(entry);
377 }
378
379 static int get_latest_connections(int max_ssids,
380                                 GSupplicantScanParams *scan_data)
381 {
382         GSequenceIter *iter;
383         GSequence *latest_list;
384         struct last_connected *entry;
385         GKeyFile *keyfile;
386         GTimeVal modified;
387         gchar **services;
388         gchar *str;
389         char *ssid;
390         int i, freq;
391         int num_ssids = 0;
392
393         latest_list = g_sequence_new(free_entry);
394         if (latest_list == NULL)
395                 return -ENOMEM;
396
397         services = connman_storage_get_services();
398         for (i = 0; services && services[i]; i++) {
399                 if (strncmp(services[i], "wifi_", 5) != 0)
400                         continue;
401
402                 keyfile = connman_storage_load_service(services[i]);
403
404                 str = g_key_file_get_string(keyfile,
405                                         services[i], "Favorite", NULL);
406                 if (str == NULL || g_strcmp0(str, "true")) {
407                         if (str)
408                                 g_free(str);
409                         g_key_file_free(keyfile);
410                         continue;
411                 }
412                 g_free(str);
413
414                 str = g_key_file_get_string(keyfile,
415                                         services[i], "AutoConnect", NULL);
416                 if (str == NULL || g_strcmp0(str, "true")) {
417                         if (str)
418                                 g_free(str);
419                         g_key_file_free(keyfile);
420                         continue;
421                 }
422                 g_free(str);
423
424                 str = g_key_file_get_string(keyfile,
425                                         services[i], "Modified", NULL);
426                 if (str != NULL) {
427                         g_time_val_from_iso8601(str, &modified);
428                         g_free(str);
429                 }
430
431                 ssid = g_key_file_get_string(keyfile,
432                                         services[i], "SSID", NULL);
433
434                 freq = g_key_file_get_integer(keyfile, services[i],
435                                         "Frequency", NULL);
436                 if (freq) {
437                         entry = g_try_new(struct last_connected, 1);
438                         if (entry == NULL) {
439                                 g_sequence_free(latest_list);
440                                 g_key_file_free(keyfile);
441                                 g_free(ssid);
442                                 return -ENOMEM;
443                         }
444
445                         entry->ssid = ssid;
446                         entry->modified = modified;
447                         entry->freq = freq;
448
449                         g_sequence_insert_sorted(latest_list, entry,
450                                                 sort_entry, NULL);
451                         num_ssids++;
452                 } else
453                         g_free(ssid);
454
455                 g_key_file_free(keyfile);
456         }
457
458         g_strfreev(services);
459
460         num_ssids = num_ssids > G_SUPPLICANT_MAX_FAST_SCAN ?
461                 G_SUPPLICANT_MAX_FAST_SCAN : num_ssids;
462
463         iter = g_sequence_get_begin_iter(latest_list);
464
465         for (i = 0; i < num_ssids; i++) {
466                 entry = g_sequence_get(iter);
467
468                 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
469                                                 entry->modified.tv_sec);
470
471                 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
472
473                 iter = g_sequence_iter_next(iter);
474         }
475
476         g_sequence_free(latest_list);
477         return num_ssids;
478 }
479
480 static int wifi_scan(struct connman_device *device)
481 {
482         struct wifi_data *wifi = connman_device_get_data(device);
483         int ret;
484
485         DBG("device %p %p", device, wifi->interface);
486
487         if (wifi->tethering == TRUE)
488                 return 0;
489
490         connman_device_ref(device);
491         ret = g_supplicant_interface_scan(wifi->interface, NULL,
492                                         scan_callback, device);
493         if (ret == 0)
494                 connman_device_set_scanning(device, TRUE);
495         else
496                 connman_device_unref(device);
497
498         return ret;
499 }
500
501 static int wifi_scan_fast(struct connman_device *device)
502 {
503         struct wifi_data *wifi = connman_device_get_data(device);
504         GSupplicantScanParams *scan_params = NULL;
505         int ret;
506         int driver_max_ssids = 0;
507
508         DBG("device %p %p", device, wifi->interface);
509
510         if (wifi->tethering == TRUE)
511                 return 0;
512
513         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
514                                                         wifi->interface);
515         DBG("max ssids %d", driver_max_ssids);
516         if (driver_max_ssids == 0)
517                 return wifi_scan(device);
518
519         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
520         if (scan_params == NULL)
521                 return -ENOMEM;
522
523         ret = get_latest_connections(driver_max_ssids, scan_params);
524         if (ret <= 0) {
525                 g_free(scan_params);
526                 return wifi_scan(device);
527         }
528
529         connman_device_ref(device);
530         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
531                                                 scan_callback, device);
532         if (ret == 0)
533                 connman_device_set_scanning(device, TRUE);
534         else {
535                 g_free(scan_params);
536                 connman_device_unref(device);
537         }
538
539         return ret;
540 }
541
542 static int wifi_scan_hidden(struct connman_device *device,
543                 const char *ssid, unsigned int ssid_len,
544                 const char *identity, const char* passphrase)
545 {
546         struct wifi_data *wifi = connman_device_get_data(device);
547         GSupplicantScanParams *scan_params = NULL;
548         struct hidden_params *hidden;
549         int ret;
550
551         DBG("hidden SSID %s", ssid);
552
553         if (wifi->tethering == TRUE || wifi->hidden != NULL)
554                 return -EBUSY;
555
556         if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
557                 return -EINVAL;
558
559         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
560         if (scan_params == NULL)
561                 return -ENOMEM;
562         memcpy(scan_params->ssids[0].ssid, ssid, ssid_len);
563         scan_params->ssids[0].ssid_len = ssid_len;
564         scan_params->num_ssids = 1;
565
566         hidden = g_try_new0(struct hidden_params, 1);
567         if (hidden == NULL) {
568                 g_free(scan_params);
569                 return -ENOMEM;
570         }
571         memcpy(hidden->ssid, ssid, ssid_len);
572         hidden->ssid_len = ssid_len;
573         hidden->identity = g_strdup(identity);
574         hidden->passphrase = g_strdup(passphrase);
575         wifi->hidden = hidden;
576
577         connman_device_ref(device);
578         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
579                         scan_callback, device);
580         if (ret == 0)
581                 connman_device_set_scanning(device, TRUE);
582         else {
583                 connman_device_unref(device);
584                 g_free(scan_params);
585                 hidden_free(wifi->hidden);
586                 wifi->hidden = NULL;
587         }
588
589         return ret;
590 }
591
592 static struct connman_device_driver wifi_ng_driver = {
593         .name           = "wifi",
594         .type           = CONNMAN_DEVICE_TYPE_WIFI,
595         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
596         .probe          = wifi_probe,
597         .remove         = wifi_remove,
598         .enable         = wifi_enable,
599         .disable        = wifi_disable,
600         .scan           = wifi_scan,
601         .scan_fast      = wifi_scan_fast,
602         .scan_hidden    = wifi_scan_hidden,
603 };
604
605 static void system_ready(void)
606 {
607         DBG("");
608
609         if (connman_device_driver_register(&wifi_ng_driver) < 0)
610                 connman_error("Failed to register WiFi driver");
611 }
612
613 static void system_killed(void)
614 {
615         DBG("");
616
617         connman_device_driver_unregister(&wifi_ng_driver);
618 }
619
620 static int network_probe(struct connman_network *network)
621 {
622         DBG("network %p", network);
623
624         return 0;
625 }
626
627 static void network_remove(struct connman_network *network)
628 {
629         struct connman_device *device = connman_network_get_device(network);
630         struct wifi_data *wifi;
631
632         DBG("network %p", network);
633
634         wifi = connman_device_get_data(device);
635         if (wifi == NULL)
636                 return;
637
638         if (wifi->network != network)
639                 return;
640
641         wifi->network = NULL;
642 }
643
644 static void connect_callback(int result, GSupplicantInterface *interface,
645                                                         void *user_data)
646 {
647         struct connman_network *network = user_data;
648
649         DBG("network %p result %d", network, result);
650
651         if (result == -ENOKEY) {
652                 connman_network_set_error(network,
653                                         CONNMAN_NETWORK_ERROR_INVALID_KEY);
654         } else if (result < 0) {
655                 connman_network_set_error(network,
656                                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
657         }
658 }
659
660 static GSupplicantSecurity network_security(const char *security)
661 {
662         if (g_str_equal(security, "none") == TRUE)
663                 return G_SUPPLICANT_SECURITY_NONE;
664         else if (g_str_equal(security, "wep") == TRUE)
665                 return G_SUPPLICANT_SECURITY_WEP;
666         else if (g_str_equal(security, "psk") == TRUE)
667                 return G_SUPPLICANT_SECURITY_PSK;
668         else if (g_str_equal(security, "wpa") == TRUE)
669                 return G_SUPPLICANT_SECURITY_PSK;
670         else if (g_str_equal(security, "rsn") == TRUE)
671                 return G_SUPPLICANT_SECURITY_PSK;
672         else if (g_str_equal(security, "ieee8021x") == TRUE)
673                 return G_SUPPLICANT_SECURITY_IEEE8021X;
674
675         return G_SUPPLICANT_SECURITY_UNKNOWN;
676 }
677
678 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
679 {
680         const char *security, *passphrase, *agent_passphrase;
681
682         memset(ssid, 0, sizeof(*ssid));
683         ssid->mode = G_SUPPLICANT_MODE_INFRA;
684         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
685                                                 &ssid->ssid_len);
686         ssid->scan_ssid = 1;
687         security = connman_network_get_string(network, "WiFi.Security");
688         ssid->security = network_security(security);
689         passphrase = connman_network_get_string(network,
690                                                 "WiFi.Passphrase");
691         if (passphrase == NULL || strlen(passphrase) == 0) {
692
693                 /* Use agent provided passphrase as a fallback */
694                 agent_passphrase = connman_network_get_string(network,
695                                                 "WiFi.AgentPassphrase");
696
697                 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
698                         ssid->passphrase = NULL;
699                 else
700                         ssid->passphrase = agent_passphrase;
701         } else
702                 ssid->passphrase = passphrase;
703
704         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
705
706         /*
707          * If our private key password is unset,
708          * we use the supplied passphrase. That is needed
709          * for PEAP where 2 passphrases (identity and client
710          * cert may have to be provided.
711          */
712         if (connman_network_get_string(network,
713                                         "WiFi.PrivateKeyPassphrase") == NULL)
714                 connman_network_set_string(network,
715                                                 "WiFi.PrivateKeyPassphrase",
716                                                 ssid->passphrase);
717         /* We must have an identity for both PEAP and TLS */
718         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
719
720         /* Use agent provided identity as a fallback */
721         if (ssid->identity == NULL || strlen(ssid->identity) == 0)
722                 ssid->identity = connman_network_get_string(network,
723                                                         "WiFi.AgentIdentity");
724
725         ssid->ca_cert_path = connman_network_get_string(network,
726                                                         "WiFi.CACertFile");
727         ssid->client_cert_path = connman_network_get_string(network,
728                                                         "WiFi.ClientCertFile");
729         ssid->private_key_path = connman_network_get_string(network,
730                                                         "WiFi.PrivateKeyFile");
731         ssid->private_key_passphrase = connman_network_get_string(network,
732                                                 "WiFi.PrivateKeyPassphrase");
733         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
734
735         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
736         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
737
738         ssid->bgscan = BGSCAN_DEFAULT;
739 }
740
741 static int network_connect(struct connman_network *network)
742 {
743         struct connman_device *device = connman_network_get_device(network);
744         struct wifi_data *wifi;
745         GSupplicantInterface *interface;
746         GSupplicantSSID *ssid;
747
748         DBG("network %p", network);
749
750         if (device == NULL)
751                 return -ENODEV;
752
753         wifi = connman_device_get_data(device);
754         if (wifi == NULL)
755                 return -ENODEV;
756
757         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
758         if (ssid == NULL)
759                 return -ENOMEM;
760
761         interface = wifi->interface;
762
763         ssid_init(ssid, network);
764
765         if (wifi->disconnecting == TRUE)
766                 wifi->pending_network = network;
767         else {
768                 wifi->network = network;
769                 wifi->retries = 0;
770
771                 return g_supplicant_interface_connect(interface, ssid,
772                                                 connect_callback, network);
773         }
774
775         return -EINPROGRESS;
776 }
777
778 static void disconnect_callback(int result, GSupplicantInterface *interface,
779                                                                 void *user_data)
780 {
781         struct wifi_data *wifi = user_data;
782
783         if (wifi->network != NULL) {
784                 /*
785                  * if result < 0 supplican return an error because
786                  * the network is not current.
787                  * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
788                  * failed, call connman_network_set_connected to report
789                  * disconnect is completed.
790                  */
791                 if (result < 0)
792                         connman_network_set_connected(wifi->network, FALSE);
793         }
794
795         wifi->network = NULL;
796
797         wifi->disconnecting = FALSE;
798
799         if (wifi->pending_network != NULL) {
800                 network_connect(wifi->pending_network);
801                 wifi->pending_network = NULL;
802         }
803
804 }
805
806 static int network_disconnect(struct connman_network *network)
807 {
808         struct connman_device *device = connman_network_get_device(network);
809         struct wifi_data *wifi;
810         int err;
811
812         DBG("network %p", network);
813
814         wifi = connman_device_get_data(device);
815         if (wifi == NULL || wifi->interface == NULL)
816                 return -ENODEV;
817
818         connman_network_set_associating(network, FALSE);
819
820         if (wifi->disconnecting == TRUE)
821                 return -EALREADY;
822
823         wifi->disconnecting = TRUE;
824
825         err = g_supplicant_interface_disconnect(wifi->interface,
826                                                 disconnect_callback, wifi);
827         if (err < 0)
828                 wifi->disconnecting = FALSE;
829
830         return err;
831 }
832
833 static struct connman_network_driver network_driver = {
834         .name           = "wifi",
835         .type           = CONNMAN_NETWORK_TYPE_WIFI,
836         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
837         .probe          = network_probe,
838         .remove         = network_remove,
839         .connect        = network_connect,
840         .disconnect     = network_disconnect,
841 };
842
843 static void interface_added(GSupplicantInterface *interface)
844 {
845         const char *ifname = g_supplicant_interface_get_ifname(interface);
846         const char *driver = g_supplicant_interface_get_driver(interface);
847         struct wifi_data *wifi;
848
849         wifi = g_supplicant_interface_get_data(interface);
850
851         /*
852          * We can get here with a NULL wifi pointer when
853          * the interface added signal is sent before the
854          * interface creation callback is called.
855          */
856         if (wifi == NULL)
857                 return;
858
859         DBG("ifname %s driver %s wifi %p tethering %d",
860                         ifname, driver, wifi, wifi->tethering);
861
862         if (wifi->device == NULL) {
863                 connman_error("WiFi device not set");
864                 return;
865         }
866
867         connman_device_set_powered(wifi->device, TRUE);
868
869         if (wifi->tethering == TRUE)
870                 return;
871 }
872
873 static connman_bool_t is_idle(struct wifi_data *wifi)
874 {
875         DBG("state %d", wifi->state);
876
877         switch (wifi->state) {
878         case G_SUPPLICANT_STATE_UNKNOWN:
879         case G_SUPPLICANT_STATE_DISCONNECTED:
880         case G_SUPPLICANT_STATE_INACTIVE:
881         case G_SUPPLICANT_STATE_SCANNING:
882                 return TRUE;
883
884         case G_SUPPLICANT_STATE_AUTHENTICATING:
885         case G_SUPPLICANT_STATE_ASSOCIATING:
886         case G_SUPPLICANT_STATE_ASSOCIATED:
887         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
888         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
889         case G_SUPPLICANT_STATE_COMPLETED:
890                 return FALSE;
891         }
892
893         return FALSE;
894 }
895
896 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
897                                                 struct wifi_data *wifi)
898 {
899         /* First, let's check if WPS processing did not went wrong */
900         if (g_supplicant_interface_get_wps_state(interface) ==
901                 G_SUPPLICANT_WPS_STATE_FAIL)
902                 return FALSE;
903
904         /* Unlike normal connection, being associated while processing wps
905          * actually means that we are idling. */
906         switch (wifi->state) {
907         case G_SUPPLICANT_STATE_UNKNOWN:
908         case G_SUPPLICANT_STATE_DISCONNECTED:
909         case G_SUPPLICANT_STATE_INACTIVE:
910         case G_SUPPLICANT_STATE_SCANNING:
911         case G_SUPPLICANT_STATE_ASSOCIATED:
912                 return TRUE;
913         case G_SUPPLICANT_STATE_AUTHENTICATING:
914         case G_SUPPLICANT_STATE_ASSOCIATING:
915         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
916         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
917         case G_SUPPLICANT_STATE_COMPLETED:
918                 return FALSE;
919         }
920
921         return FALSE;
922 }
923
924 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
925                                         struct connman_network *network,
926                                         struct connman_device *device,
927                                         struct wifi_data *wifi)
928 {
929         connman_bool_t wps;
930
931         wps = connman_network_get_bool(network, "WiFi.UseWPS");
932         if (wps == TRUE) {
933                 const unsigned char *ssid, *wps_ssid;
934                 unsigned int ssid_len, wps_ssid_len;
935                 const char *wps_key;
936
937                 /* Checking if we got associated with requested
938                  * network */
939                 ssid = connman_network_get_blob(network, "WiFi.SSID",
940                                                 &ssid_len);
941
942                 wps_ssid = g_supplicant_interface_get_wps_ssid(
943                         interface, &wps_ssid_len);
944
945                 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
946                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
947                         connman_network_set_associating(network, FALSE);
948                         g_supplicant_interface_disconnect(wifi->interface,
949                                                 disconnect_callback, wifi);
950                         return FALSE;
951                 }
952
953                 wps_key = g_supplicant_interface_get_wps_key(interface);
954                 connman_network_set_string(network, "WiFi.Passphrase",
955                                         wps_key);
956
957                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
958         }
959
960         return TRUE;
961 }
962
963 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
964                                         struct connman_network *network,
965                                         struct wifi_data *wifi)
966 {
967         if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
968                 return FALSE;
969
970         wifi->retries++;
971
972         if (wifi->retries < MAXIMUM_RETRIES)
973                 return TRUE;
974
975         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
976
977         return FALSE;
978 }
979
980 static void interface_state(GSupplicantInterface *interface)
981 {
982         struct connman_network *network;
983         struct connman_device *device;
984         struct wifi_data *wifi;
985         GSupplicantState state = g_supplicant_interface_get_state(interface);
986         connman_bool_t wps;
987
988         wifi = g_supplicant_interface_get_data(interface);
989
990         DBG("wifi %p interface state %d", wifi, state);
991
992         if (wifi == NULL)
993                 return;
994
995         network = wifi->network;
996         device = wifi->device;
997
998         if (device == NULL || network == NULL)
999                 return;
1000
1001         switch (state) {
1002         case G_SUPPLICANT_STATE_SCANNING:
1003                 break;
1004
1005         case G_SUPPLICANT_STATE_AUTHENTICATING:
1006         case G_SUPPLICANT_STATE_ASSOCIATING:
1007                 connman_network_set_associating(network, TRUE);
1008                 break;
1009
1010         case G_SUPPLICANT_STATE_COMPLETED:
1011                 if (handle_wps_completion(interface, network, device, wifi) ==
1012                                                                         FALSE)
1013                         break;
1014
1015                 /* reset scan trigger and schedule background scan */
1016                 connman_device_schedule_scan(device);
1017
1018                 connman_network_set_connected(network, TRUE);
1019                 break;
1020
1021         case G_SUPPLICANT_STATE_DISCONNECTED:
1022                 /*
1023                  * If we're in one of the idle modes, we have
1024                  * not started association yet and thus setting
1025                  * those ones to FALSE could cancel an association
1026                  * in progress.
1027                  */
1028                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1029                 if (wps == TRUE)
1030                         if (is_idle_wps(interface, wifi) == TRUE)
1031                                 break;
1032
1033                 if (is_idle(wifi))
1034                         break;
1035
1036                 /* If previous state was 4way-handshake, then
1037                  * it's either: psk was incorrect and thus we retry
1038                  * or if we reach the maximum retries we declare the
1039                  * psk as wrong */
1040                 if (handle_4way_handshake_failure(interface,
1041                                                 network, wifi) == TRUE)
1042                         break;
1043
1044                 /* We disable the selected network, if not then
1045                  * wpa_supplicant will loop retrying */
1046                 if (g_supplicant_interface_enable_selected_network(interface,
1047                                                 FALSE) != 0)
1048                         DBG("Could not disables selected network");
1049
1050                 connman_network_set_associating(network, FALSE);
1051                 connman_network_set_connected(network, FALSE);
1052                 break;
1053
1054         case G_SUPPLICANT_STATE_INACTIVE:
1055                 connman_network_set_associating(network, FALSE);
1056                 break;
1057
1058         case G_SUPPLICANT_STATE_UNKNOWN:
1059         case G_SUPPLICANT_STATE_ASSOCIATED:
1060         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1061         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1062                 break;
1063         }
1064
1065         wifi->state = state;
1066
1067         DBG("DONE");
1068 }
1069
1070 static void interface_removed(GSupplicantInterface *interface)
1071 {
1072         const char *ifname = g_supplicant_interface_get_ifname(interface);
1073         struct wifi_data *wifi;
1074
1075         DBG("ifname %s", ifname);
1076
1077         wifi = g_supplicant_interface_get_data(interface);
1078
1079         if (wifi != NULL && wifi->tethering == TRUE)
1080                 return;
1081
1082         if (wifi == NULL || wifi->device == NULL) {
1083                 connman_error("Wrong wifi pointer");
1084                 return;
1085         }
1086
1087         wifi->interface = NULL;
1088         connman_device_set_powered(wifi->device, FALSE);
1089 }
1090
1091 static void scan_started(GSupplicantInterface *interface)
1092 {
1093         DBG("");
1094 }
1095
1096 static void scan_finished(GSupplicantInterface *interface)
1097 {
1098         DBG("");
1099 }
1100
1101 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1102 {
1103         unsigned char strength;
1104
1105         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1106         if (strength > 100)
1107                 strength = 100;
1108
1109         return strength;
1110 }
1111
1112 static void network_added(GSupplicantNetwork *supplicant_network)
1113 {
1114         struct connman_network *network;
1115         GSupplicantInterface *interface;
1116         struct wifi_data *wifi;
1117         const char *name, *identifier, *security, *group, *mode;
1118         const unsigned char *ssid;
1119         unsigned int ssid_len;
1120         connman_bool_t wps;
1121
1122         DBG("");
1123
1124         interface = g_supplicant_network_get_interface(supplicant_network);
1125         wifi = g_supplicant_interface_get_data(interface);
1126         name = g_supplicant_network_get_name(supplicant_network);
1127         identifier = g_supplicant_network_get_identifier(supplicant_network);
1128         security = g_supplicant_network_get_security(supplicant_network);
1129         group = g_supplicant_network_get_identifier(supplicant_network);
1130         wps = g_supplicant_network_get_wps(supplicant_network);
1131         mode = g_supplicant_network_get_mode(supplicant_network);
1132
1133         if (wifi == NULL)
1134                 return;
1135
1136         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1137
1138         network = connman_device_get_network(wifi->device, identifier);
1139
1140         if (network == NULL) {
1141                 network = connman_network_create(identifier,
1142                                                 CONNMAN_NETWORK_TYPE_WIFI);
1143                 if (network == NULL)
1144                         return;
1145
1146                 connman_network_set_index(network, wifi->index);
1147
1148                 if (connman_device_add_network(wifi->device, network) < 0) {
1149                         connman_network_unref(network);
1150                         return;
1151                 }
1152
1153                 wifi->networks = g_slist_append(wifi->networks, network);
1154         }
1155
1156         if (name != NULL && name[0] != '\0')
1157                 connman_network_set_name(network, name);
1158
1159         connman_network_set_blob(network, "WiFi.SSID",
1160                                                 ssid, ssid_len);
1161         connman_network_set_string(network, "WiFi.Security", security);
1162         connman_network_set_strength(network,
1163                                 calculate_strength(supplicant_network));
1164         connman_network_set_bool(network, "WiFi.WPS", wps);
1165
1166         connman_network_set_frequency(network,
1167                         g_supplicant_network_get_frequency(supplicant_network));
1168
1169         connman_network_set_available(network, TRUE);
1170         connman_network_set_string(network, "WiFi.Mode", mode);
1171
1172         if (ssid != NULL)
1173                 connman_network_set_group(network, group);
1174
1175         if (wifi->hidden != NULL) {
1176                 if (wifi->hidden->ssid_len == ssid_len &&
1177                                 memcmp(wifi->hidden->ssid, ssid,
1178                                                 ssid_len) == 0) {
1179                         connman_network_connect_hidden(network,
1180                                         wifi->hidden->identity,
1181                                         wifi->hidden->passphrase);
1182                         hidden_free(wifi->hidden);
1183                         wifi->hidden = NULL;
1184                 }
1185         }
1186 }
1187
1188 static void network_removed(GSupplicantNetwork *network)
1189 {
1190         GSupplicantInterface *interface;
1191         struct wifi_data *wifi;
1192         const char *name, *identifier;
1193         struct connman_network *connman_network;
1194
1195         interface = g_supplicant_network_get_interface(network);
1196         wifi = g_supplicant_interface_get_data(interface);
1197         identifier = g_supplicant_network_get_identifier(network);
1198         name = g_supplicant_network_get_name(network);
1199
1200         DBG("name %s", name);
1201
1202         if (wifi == NULL)
1203                 return;
1204
1205         connman_network = connman_device_get_network(wifi->device, identifier);
1206         if (connman_network == NULL)
1207                 return;
1208
1209         wifi->networks = g_slist_remove(wifi->networks, connman_network);
1210
1211         connman_device_remove_network(wifi->device, connman_network);
1212         connman_network_unref(connman_network);
1213 }
1214
1215 static void network_changed(GSupplicantNetwork *network, const char *property)
1216 {
1217         GSupplicantInterface *interface;
1218         struct wifi_data *wifi;
1219         const char *name, *identifier;
1220         struct connman_network *connman_network;
1221
1222         interface = g_supplicant_network_get_interface(network);
1223         wifi = g_supplicant_interface_get_data(interface);
1224         identifier = g_supplicant_network_get_identifier(network);
1225         name = g_supplicant_network_get_name(network);
1226
1227         DBG("name %s", name);
1228
1229         if (wifi == NULL)
1230                 return;
1231
1232         connman_network = connman_device_get_network(wifi->device, identifier);
1233         if (connman_network == NULL)
1234                 return;
1235
1236         if (g_str_equal(property, "Signal") == TRUE) {
1237                connman_network_set_strength(connman_network,
1238                                         calculate_strength(network));
1239                connman_network_update(connman_network);
1240         }
1241 }
1242
1243 static void debug(const char *str)
1244 {
1245         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1246                 connman_debug("%s", str);
1247 }
1248
1249 static const GSupplicantCallbacks callbacks = {
1250         .system_ready           = system_ready,
1251         .system_killed          = system_killed,
1252         .interface_added        = interface_added,
1253         .interface_state        = interface_state,
1254         .interface_removed      = interface_removed,
1255         .scan_started           = scan_started,
1256         .scan_finished          = scan_finished,
1257         .network_added          = network_added,
1258         .network_removed        = network_removed,
1259         .network_changed        = network_changed,
1260         .debug                  = debug,
1261 };
1262
1263
1264 static int tech_probe(struct connman_technology *technology)
1265 {
1266         wifi_technology = technology;
1267
1268         return 0;
1269 }
1270
1271 static void tech_remove(struct connman_technology *technology)
1272 {
1273         wifi_technology = NULL;
1274 }
1275
1276 struct wifi_tethering_info {
1277         struct wifi_data *wifi;
1278         struct connman_technology *technology;
1279         char *ifname;
1280         GSupplicantSSID *ssid;
1281 };
1282
1283 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1284 {
1285         GSupplicantSSID *ap;
1286
1287         ap = g_try_malloc0(sizeof(GSupplicantSSID));
1288         if (ap == NULL)
1289                 return NULL;
1290
1291         ap->mode = G_SUPPLICANT_MODE_MASTER;
1292         ap->ssid = ssid;
1293         ap->ssid_len = strlen(ssid);
1294         ap->scan_ssid = 0;
1295         ap->freq = 2412;
1296
1297         if (passphrase == NULL || strlen(passphrase) == 0) {
1298                 ap->security = G_SUPPLICANT_SECURITY_NONE;
1299                 ap->passphrase = NULL;
1300         } else {
1301                ap->security = G_SUPPLICANT_SECURITY_PSK;
1302                ap->protocol = G_SUPPLICANT_PROTO_RSN;
1303                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1304                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1305                ap->passphrase = passphrase;
1306         }
1307
1308         return ap;
1309 }
1310
1311 static void ap_start_callback(int result, GSupplicantInterface *interface,
1312                                                         void *user_data)
1313 {
1314         struct wifi_tethering_info *info = user_data;
1315
1316         DBG("result %d index %d bridge %s",
1317                 result, info->wifi->index, info->wifi->bridge);
1318
1319         if (result < 0) {
1320                 connman_inet_remove_from_bridge(info->wifi->index,
1321                                                         info->wifi->bridge);
1322                 connman_technology_tethering_notify(info->technology, FALSE);
1323         }
1324
1325         g_free(info->ifname);
1326         g_free(info);
1327 }
1328
1329 static void ap_create_callback(int result,
1330                                 GSupplicantInterface *interface,
1331                                         void *user_data)
1332 {
1333         struct wifi_tethering_info *info = user_data;
1334
1335         DBG("result %d ifname %s", result,
1336                                 g_supplicant_interface_get_ifname(interface));
1337
1338         if (result < 0) {
1339                 connman_inet_remove_from_bridge(info->wifi->index,
1340                                                         info->wifi->bridge);
1341                 connman_technology_tethering_notify(info->technology, FALSE);
1342
1343                 g_free(info->ifname);
1344                 g_free(info);
1345                 return;
1346         }
1347
1348         info->wifi->interface = interface;
1349         g_supplicant_interface_set_data(interface, info->wifi);
1350
1351         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1352                 connman_error("Failed to set interface ap_scan property");
1353
1354         g_supplicant_interface_connect(interface, info->ssid,
1355                                                 ap_start_callback, info);
1356 }
1357
1358 static void sta_remove_callback(int result,
1359                                 GSupplicantInterface *interface,
1360                                         void *user_data)
1361 {
1362         struct wifi_tethering_info *info = user_data;
1363         const char *driver = connman_option_get_string("wifi");
1364
1365         DBG("ifname %s result %d ", info->ifname, result);
1366
1367         if (result < 0) {
1368                 info->wifi->tethering = TRUE;
1369
1370                 g_free(info->ifname);
1371                 g_free(info);
1372                 return;
1373         }
1374
1375         info->wifi->interface = NULL;
1376
1377         connman_technology_tethering_notify(info->technology, TRUE);
1378
1379         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1380                                                 ap_create_callback,
1381                                                         info);
1382 }
1383
1384 static int tech_set_tethering(struct connman_technology *technology,
1385                                 const char *identifier, const char *passphrase,
1386                                 const char *bridge, connman_bool_t enabled)
1387 {
1388         GList *list;
1389         GSupplicantInterface *interface;
1390         struct wifi_data *wifi;
1391         struct wifi_tethering_info *info;
1392         const char *ifname;
1393         unsigned int mode;
1394         int err;
1395
1396         DBG("");
1397
1398         if (enabled == FALSE) {
1399                 for (list = iface_list; list; list = list->next) {
1400                         wifi = list->data;
1401
1402                         if (wifi->tethering == TRUE) {
1403                                 wifi->tethering = FALSE;
1404
1405                                 connman_inet_remove_from_bridge(wifi->index,
1406                                                                         bridge);
1407                                 wifi->bridged = FALSE;
1408                         }
1409                 }
1410
1411                 connman_technology_tethering_notify(technology, FALSE);
1412
1413                 return 0;
1414         }
1415
1416         for (list = iface_list; list; list = list->next) {
1417                 wifi = list->data;
1418
1419                 interface = wifi->interface;
1420
1421                 if (interface == NULL)
1422                         continue;
1423
1424                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1425
1426                 mode = g_supplicant_interface_get_mode(interface);
1427                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1428                         DBG("%s does not support AP mode", ifname);
1429                         continue;
1430                 }
1431
1432                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1433                 if (info == NULL)
1434                         return -ENOMEM;
1435
1436                 info->wifi = wifi;
1437                 info->technology = technology;
1438                 info->wifi->bridge = bridge;
1439                 info->ssid = ssid_ap_init(identifier, passphrase);
1440                 if (info->ssid == NULL) {
1441                         g_free(info);
1442                         continue;
1443                 }
1444                 info->ifname = g_strdup(ifname);
1445                 if (info->ifname == NULL) {
1446                         g_free(info);
1447                         continue;
1448                 }
1449
1450                 info->wifi->tethering = TRUE;
1451
1452                 err = g_supplicant_interface_remove(interface,
1453                                                 sta_remove_callback,
1454                                                         info);
1455                 if (err == 0)
1456                         return err;
1457         }
1458
1459         return -EOPNOTSUPP;
1460 }
1461
1462 static void regdom_callback(void *user_data)
1463 {
1464         char *alpha2 = user_data;
1465
1466         DBG("");
1467
1468         if (wifi_technology == NULL)
1469                 return;
1470
1471         connman_technology_regdom_notify(wifi_technology, alpha2);
1472 }
1473
1474 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1475 {
1476         return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1477 }
1478
1479 static struct connman_technology_driver tech_driver = {
1480         .name           = "wifi",
1481         .type           = CONNMAN_SERVICE_TYPE_WIFI,
1482         .probe          = tech_probe,
1483         .remove         = tech_remove,
1484         .set_tethering  = tech_set_tethering,
1485         .set_regdom     = tech_set_regdom,
1486 };
1487
1488 static int wifi_init(void)
1489 {
1490         int err;
1491
1492         err = connman_network_driver_register(&network_driver);
1493         if (err < 0)
1494                 return err;
1495
1496         err = g_supplicant_register(&callbacks);
1497         if (err < 0) {
1498                 connman_network_driver_unregister(&network_driver);
1499                 return err;
1500         }
1501
1502         err = connman_technology_driver_register(&tech_driver);
1503         if (err < 0) {
1504                 g_supplicant_unregister(&callbacks);
1505                 connman_network_driver_unregister(&network_driver);
1506                 return err;
1507         }
1508
1509         return 0;
1510 }
1511
1512 static void wifi_exit(void)
1513 {
1514         DBG();
1515
1516         connman_technology_driver_unregister(&tech_driver);
1517
1518         g_supplicant_unregister(&callbacks);
1519
1520         connman_network_driver_unregister(&network_driver);
1521 }
1522
1523 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1524                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)