Move calculation of signal strengh into a function
[connman] / plugins / supplicant.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 <stdio.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <net/ethernet.h>
31
32 #include <gdbus.h>
33
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/device.h>
36 #include <connman/dbus.h>
37 #include <connman/log.h>
38
39 #include "inet.h"
40 #include "supplicant.h"
41
42 #define TIMEOUT 5000
43
44 #define IEEE80211_CAP_ESS       0x0001
45 #define IEEE80211_CAP_IBSS      0x0002
46 #define IEEE80211_CAP_PRIVACY   0x0010
47
48 #define SUPPLICANT_NAME  "fi.epitest.hostap.WPASupplicant"
49 #define SUPPLICANT_INTF  "fi.epitest.hostap.WPASupplicant"
50 #define SUPPLICANT_PATH  "/fi/epitest/hostap/WPASupplicant"
51
52 /* Taken from "WPA Supplicant - Common definitions" */
53 enum supplicant_state {
54         /**
55          * WPA_DISCONNECTED - Disconnected state
56          *
57          * This state indicates that client is not associated, but is likely to
58          * start looking for an access point. This state is entered when a
59          * connection is lost.
60          */
61         WPA_DISCONNECTED,
62
63         /**
64          * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
65          *
66          * This state is entered if there are no enabled networks in the
67          * configuration. wpa_supplicant is not trying to associate with a new
68          * network and external interaction (e.g., ctrl_iface call to add or
69          * enable a network) is needed to start association.
70          */
71         WPA_INACTIVE,
72
73         /**
74          * WPA_SCANNING - Scanning for a network
75          *
76          * This state is entered when wpa_supplicant starts scanning for a
77          * network.
78          */
79         WPA_SCANNING,
80
81         /**
82          * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
83          *
84          * This state is entered when wpa_supplicant has found a suitable BSS
85          * to associate with and the driver is configured to try to associate
86          * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
87          * state is entered when the driver is configured to try to associate
88          * with a network using the configured SSID and security policy.
89          */
90         WPA_ASSOCIATING,
91
92         /**
93          * WPA_ASSOCIATED - Association completed
94          *
95          * This state is entered when the driver reports that association has
96          * been successfully completed with an AP. If IEEE 802.1X is used
97          * (with or without WPA/WPA2), wpa_supplicant remains in this state
98          * until the IEEE 802.1X/EAPOL authentication has been completed.
99          */
100         WPA_ASSOCIATED,
101
102         /**
103          * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
104          *
105          * This state is entered when WPA/WPA2 4-Way Handshake is started. In
106          * case of WPA-PSK, this happens when receiving the first EAPOL-Key
107          * frame after association. In case of WPA-EAP, this state is entered
108          * when the IEEE 802.1X/EAPOL authentication has been completed.
109          */
110         WPA_4WAY_HANDSHAKE,
111
112         /**
113          * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
114          *
115          * This state is entered when 4-Way Key Handshake has been completed
116          * (i.e., when the supplicant sends out message 4/4) and when Group
117          * Key rekeying is started by the AP (i.e., when supplicant receives
118          * message 1/2).
119          */
120         WPA_GROUP_HANDSHAKE,
121
122         /**
123          * WPA_COMPLETED - All authentication completed
124          *
125          * This state is entered when the full authentication process is
126          * completed. In case of WPA2, this happens when the 4-Way Handshake is
127          * successfully completed. With WPA, this state is entered after the
128          * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
129          * completed after dynamic keys are received (or if not used, after
130          * the EAP authentication has been completed). With static WEP keys and
131          * plaintext connections, this state is entered when an association
132          * has been completed.
133          *
134          * This state indicates that the supplicant has completed its
135          * processing for the association phase and that data connection is
136          * fully configured.
137          */
138         WPA_COMPLETED,
139
140         /**
141          * WPA_INVALID - Invalid state (parsing error)
142          *
143          * This state is returned if the string input is invalid. It is not
144          * an official wpa_supplicant state.
145          */
146         WPA_INVALID,
147 };
148
149 struct supplicant_result {
150         char *path;
151         char *name;
152         char *addr;
153         unsigned char *ssid;
154         unsigned int ssid_len;
155         dbus_uint16_t capabilities;
156         gboolean adhoc;
157         gboolean has_wep;
158         gboolean has_wpa;
159         gboolean has_rsn;
160         gboolean has_wps;
161         dbus_int32_t frequency;
162         dbus_int32_t quality;
163         dbus_int32_t noise;
164         dbus_int32_t level;
165         dbus_int32_t maxrate;
166 };
167
168 struct supplicant_task {
169         int ifindex;
170         char *ifname;
171         struct connman_device *device;
172         struct connman_network *network;
173         char *path;
174         char *netpath;
175         gboolean created;
176         enum supplicant_state state;
177         gboolean noscan;
178         GSList *scan_results;
179 };
180
181 static GSList *task_list = NULL;
182
183 static DBusConnection *connection;
184
185 static void free_task(struct supplicant_task *task)
186 {
187         DBG("task %p", task);
188
189         g_free(task->ifname);
190         g_free(task->path);
191         g_free(task);
192 }
193
194 static struct supplicant_task *find_task_by_index(int index)
195 {
196         GSList *list;
197
198         for (list = task_list; list; list = list->next) {
199                 struct supplicant_task *task = list->data;
200
201                 if (task->ifindex == index)
202                         return task;
203         }
204
205         return NULL;
206 }
207
208 static struct supplicant_task *find_task_by_path(const char *path)
209 {
210         GSList *list;
211
212         for (list = task_list; list; list = list->next) {
213                 struct supplicant_task *task = list->data;
214
215                 if (g_str_equal(task->path, path) == TRUE)
216                         return task;
217         }
218
219         return NULL;
220 }
221
222 static void add_interface_reply(DBusPendingCall *call, void *user_data)
223 {
224         struct supplicant_task *task = user_data;
225         DBusMessage *reply;
226         DBusError error;
227         const char *path;
228
229         DBG("task %p", task);
230
231         reply = dbus_pending_call_steal_reply(call);
232         if (reply == NULL)
233                 return;
234
235         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
236                 goto done;
237
238         dbus_error_init(&error);
239
240         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
241                                                 DBUS_TYPE_INVALID) == FALSE) {
242                 if (dbus_error_is_set(&error) == TRUE) {
243                         connman_error("%s", error.message);
244                         dbus_error_free(&error);
245                 } else
246                         connman_error("Wrong arguments for add interface");
247                 goto done;
248         }
249
250         DBG("path %s", path);
251
252         task->path = g_strdup(path);
253         task->created = TRUE;
254
255         connman_device_set_powered(task->device, TRUE);
256
257 done:
258         dbus_message_unref(reply);
259 }
260
261 static int add_interface(struct supplicant_task *task)
262 {
263         DBusMessage *message;
264         DBusPendingCall *call;
265
266         DBG("task %p", task);
267
268         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
269                                         SUPPLICANT_INTF, "addInterface");
270         if (message == NULL)
271                 return -ENOMEM;
272
273         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
274                                                         DBUS_TYPE_INVALID);
275
276         if (dbus_connection_send_with_reply(connection, message,
277                                                 &call, TIMEOUT) == FALSE) {
278                 connman_error("Failed to add interface");
279                 dbus_message_unref(message);
280                 return -EIO;
281         }
282
283         if (call == NULL) {
284                 connman_error("D-Bus connection not available");
285                 dbus_message_unref(message);
286                 return -EIO;
287         }
288
289         dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
290
291         dbus_message_unref(message);
292
293         return -EINPROGRESS;
294 }
295
296 static void get_interface_reply(DBusPendingCall *call, void *user_data)
297 {
298         struct supplicant_task *task = user_data;
299         DBusMessage *reply;
300         DBusError error;
301         const char *path;
302
303         DBG("task %p", task);
304
305         reply = dbus_pending_call_steal_reply(call);
306         if (reply == NULL)
307                 return;
308
309         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
310                 add_interface(task);
311                 goto done;
312         }
313
314         dbus_error_init(&error);
315
316         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
317                                                 DBUS_TYPE_INVALID) == FALSE) {
318                 if (dbus_error_is_set(&error) == TRUE) {
319                         connman_error("%s", error.message);
320                         dbus_error_free(&error);
321                 } else
322                         connman_error("Wrong arguments for get interface");
323                 goto done;
324         }
325
326         DBG("path %s", path);
327
328         task->path = g_strdup(path);
329         task->created = FALSE;
330
331         connman_device_set_powered(task->device, TRUE);
332
333 done:
334         dbus_message_unref(reply);
335 }
336
337 static int create_interface(struct supplicant_task *task)
338 {
339         DBusMessage *message;
340         DBusPendingCall *call;
341
342         DBG("task %p", task);
343
344         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
345                                         SUPPLICANT_INTF, "getInterface");
346         if (message == NULL)
347                 return -ENOMEM;
348
349         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
350                                                         DBUS_TYPE_INVALID);
351
352         if (dbus_connection_send_with_reply(connection, message,
353                                                 &call, TIMEOUT) == FALSE) {
354                 connman_error("Failed to get interface");
355                 dbus_message_unref(message);
356                 return -EIO;
357         }
358
359         if (call == NULL) {
360                 connman_error("D-Bus connection not available");
361                 dbus_message_unref(message);
362                 return -EIO;
363         }
364
365         dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
366
367         dbus_message_unref(message);
368
369         return -EINPROGRESS;
370 }
371
372 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
373 {
374         struct supplicant_task *task = user_data;
375         DBusMessage *reply;
376
377         DBG("task %p", task);
378
379         reply = dbus_pending_call_steal_reply(call);
380
381         connman_device_set_powered(task->device, FALSE);
382
383         connman_device_unref(task->device);
384
385         inet_ifdown(task->ifindex);
386
387         free_task(task);
388
389         dbus_message_unref(reply);
390 }
391
392 static int remove_interface(struct supplicant_task *task)
393 {
394         DBusMessage *message;
395         DBusPendingCall *call;
396
397         DBG("task %p", task);
398
399         if (task->created == FALSE) {
400                 connman_device_set_powered(task->device, FALSE);
401                 return 0;
402         }
403
404         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
405                                         SUPPLICANT_INTF, "removeInterface");
406         if (message == NULL)
407                 return -ENOMEM;
408
409         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
410                                                         DBUS_TYPE_INVALID);
411
412         if (dbus_connection_send_with_reply(connection, message,
413                                                 &call, TIMEOUT) == FALSE) {
414                 connman_error("Failed to remove interface");
415                 dbus_message_unref(message);
416                 return -EIO;
417         }
418
419         if (call == NULL) {
420                 connman_error("D-Bus connection not available");
421                 dbus_message_unref(message);
422                 return -EIO;
423         }
424
425         dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
426
427         dbus_message_unref(message);
428
429         return -EINPROGRESS;
430 }
431
432 #if 0
433 static int set_ap_scan(struct supplicant_task *task)
434 {
435         DBusMessage *message, *reply;
436         DBusError error;
437         guint32 ap_scan = 1;
438
439         DBG("task %p", task);
440
441         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
442                                 SUPPLICANT_INTF ".Interface", "setAPScan");
443         if (message == NULL)
444                 return -ENOMEM;
445
446         dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
447                                                         DBUS_TYPE_INVALID);
448
449         dbus_error_init(&error);
450
451         reply = dbus_connection_send_with_reply_and_block(connection,
452                                                         message, -1, &error);
453         if (reply == NULL) {
454                 if (dbus_error_is_set(&error) == TRUE) {
455                         connman_error("%s", error.message);
456                         dbus_error_free(&error);
457                 } else
458                         connman_error("Failed to set AP scan");
459                 dbus_message_unref(message);
460                 return -EIO;
461         }
462
463         dbus_message_unref(message);
464
465         dbus_message_unref(reply);
466
467         return 0;
468 }
469 #endif
470
471 static int add_network(struct supplicant_task *task)
472 {
473         DBusMessage *message, *reply;
474         DBusError error;
475         const char *path;
476
477         DBG("task %p", task);
478
479         if (task->netpath != NULL)
480                 return -EALREADY;
481
482         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
483                                 SUPPLICANT_INTF ".Interface", "addNetwork");
484         if (message == NULL)
485                 return -ENOMEM;
486
487         dbus_error_init(&error);
488
489         reply = dbus_connection_send_with_reply_and_block(connection,
490                                                         message, -1, &error);
491         if (reply == NULL) {
492                 if (dbus_error_is_set(&error) == TRUE) {
493                         connman_error("%s", error.message);
494                         dbus_error_free(&error);
495                 } else
496                         connman_error("Failed to add network");
497                 dbus_message_unref(message);
498                 return -EIO;
499         }
500
501         dbus_message_unref(message);
502
503         dbus_error_init(&error);
504
505         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
506                                                 DBUS_TYPE_INVALID) == FALSE) {
507                 if (dbus_error_is_set(&error) == TRUE) {
508                         connman_error("%s", error.message);
509                         dbus_error_free(&error);
510                 } else
511                         connman_error("Wrong arguments for network");
512                 dbus_message_unref(reply);
513                 return -EIO;
514         }
515
516         DBG("path %s", path);
517
518         task->netpath = g_strdup(path);
519
520         dbus_message_unref(reply);
521
522         return 0;
523 }
524
525 static int remove_network(struct supplicant_task *task)
526 {
527         DBusMessage *message, *reply;
528         DBusError error;
529
530         DBG("task %p", task);
531
532         if (task->netpath == NULL)
533                 return -EINVAL;
534
535         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
536                                 SUPPLICANT_INTF ".Interface", "removeNetwork");
537         if (message == NULL)
538                 return -ENOMEM;
539
540         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
541                                                         DBUS_TYPE_INVALID);
542
543         dbus_error_init(&error);
544
545         reply = dbus_connection_send_with_reply_and_block(connection,
546                                                         message, -1, &error);
547         if (reply == NULL) {
548                 if (dbus_error_is_set(&error) == TRUE) {
549                         connman_error("%s", error.message);
550                         dbus_error_free(&error);
551                 } else
552                         connman_error("Failed to remove network");
553                 dbus_message_unref(message);
554                 return -EIO;
555         }
556
557         dbus_message_unref(message);
558
559         dbus_message_unref(reply);
560
561         g_free(task->netpath);
562         task->netpath = NULL;
563
564         return 0;
565 }
566
567 static int select_network(struct supplicant_task *task)
568 {
569         DBusMessage *message, *reply;
570         DBusError error;
571
572         DBG("task %p", task);
573
574         if (task->netpath == NULL)
575                 return -EINVAL;
576
577         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
578                                 SUPPLICANT_INTF ".Interface", "selectNetwork");
579         if (message == NULL)
580                 return -ENOMEM;
581
582         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
583                                                         DBUS_TYPE_INVALID);
584
585         dbus_error_init(&error);
586
587         reply = dbus_connection_send_with_reply_and_block(connection,
588                                                         message, -1, &error);
589         if (reply == NULL) {
590                 if (dbus_error_is_set(&error) == TRUE) {
591                         connman_error("%s", error.message);
592                         dbus_error_free(&error);
593                 } else
594                         connman_error("Failed to select network");
595                 dbus_message_unref(message);
596                 return -EIO;
597         }
598
599         dbus_message_unref(message);
600
601         dbus_message_unref(reply);
602
603         return 0;
604 }
605
606 static int enable_network(struct supplicant_task *task)
607 {
608         DBusMessage *message, *reply;
609         DBusError error;
610
611         DBG("task %p", task);
612
613         if (task->netpath == NULL)
614                 return -EINVAL;
615
616         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
617                                         SUPPLICANT_INTF ".Network", "enable");
618         if (message == NULL)
619                 return -ENOMEM;
620
621         dbus_error_init(&error);
622
623         reply = dbus_connection_send_with_reply_and_block(connection,
624                                                         message, -1, &error);
625         if (reply == NULL) {
626                 if (dbus_error_is_set(&error) == TRUE) {
627                         connman_error("%s", error.message);
628                         dbus_error_free(&error);
629                 } else
630                         connman_error("Failed to enable network");
631                 dbus_message_unref(message);
632                 return -EIO;
633         }
634
635         dbus_message_unref(message);
636
637         dbus_message_unref(reply);
638
639         return 0;
640 }
641
642 static int disable_network(struct supplicant_task *task)
643 {
644         DBusMessage *message, *reply;
645         DBusError error;
646
647         DBG("task %p", task);
648
649         if (task->netpath == NULL)
650                 return -EINVAL;
651
652         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
653                                         SUPPLICANT_INTF ".Network", "disable");
654         if (message == NULL)
655                 return -ENOMEM;
656
657         dbus_error_init(&error);
658
659         reply = dbus_connection_send_with_reply_and_block(connection,
660                                                         message, -1, &error);
661         if (reply == NULL) {
662                 if (dbus_error_is_set(&error) == TRUE) {
663                         connman_error("%s", error.message);
664                         dbus_error_free(&error);
665                 } else
666                         connman_error("Failed to disable network");
667                 dbus_message_unref(message);
668                 return -EIO;
669         }
670
671         dbus_message_unref(message);
672
673         dbus_message_unref(reply);
674
675         return 0;
676 }
677
678 static int set_network(struct supplicant_task *task,
679                                 const unsigned char *network, int len,
680                                 const char *address, const char *security,
681                                                         const char *passphrase)
682 {
683         DBusMessage *message, *reply;
684         DBusMessageIter array, dict;
685         DBusError error;
686
687         DBG("task %p", task);
688
689         if (task->netpath == NULL)
690                 return -EINVAL;
691
692         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
693                                         SUPPLICANT_INTF ".Network", "set");
694         if (message == NULL)
695                 return -ENOMEM;
696
697         dbus_message_iter_init_append(message, &array);
698
699         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
700                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
701                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
702                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
703
704         if (address == NULL) {
705                 dbus_uint32_t scan_ssid = 1;
706                 connman_dbus_dict_append_variant(&dict, "scan_ssid",
707                                                 DBUS_TYPE_UINT32, &scan_ssid);
708         } else
709                 connman_dbus_dict_append_variant(&dict, "bssid",
710                                                 DBUS_TYPE_STRING, &address);
711
712         connman_dbus_dict_append_array(&dict, "ssid",
713                                         DBUS_TYPE_BYTE, &network, len);
714
715         if (g_ascii_strcasecmp(security, "wpa") == 0 ||
716                                 g_ascii_strcasecmp(security, "rsn") == 0) {
717                 const char *key_mgmt = "WPA-PSK";
718                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
719                                                 DBUS_TYPE_STRING, &key_mgmt);
720
721                 if (passphrase && strlen(passphrase) > 0)
722                         connman_dbus_dict_append_variant(&dict, "psk",
723                                                 DBUS_TYPE_STRING, &passphrase);
724         } else if (g_ascii_strcasecmp(security, "wep") == 0) {
725                 const char *key_mgmt = "NONE", *index = "0";
726                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
727                                                 DBUS_TYPE_STRING, &key_mgmt);
728
729                 if (passphrase) {
730                         int size = strlen(passphrase);
731                         if (size == 10 || size == 26) {
732                                 unsigned char *key = malloc(13);
733                                 char tmp[3];
734                                 int i;
735                                 memset(tmp, 0, sizeof(tmp));
736                                 if (key == NULL)
737                                         size = 0;
738                                 for (i = 0; i < size / 2; i++) {
739                                         memcpy(tmp, passphrase + (i * 2), 2);
740                                         key[i] = (unsigned char) strtol(tmp,
741                                                                 NULL, 16);
742                                 }
743                                 connman_dbus_dict_append_array(&dict,
744                                                 "wep_key0", DBUS_TYPE_BYTE,
745                                                         &key, size / 2);
746                                 free(key);
747                         } else
748                                 connman_dbus_dict_append_variant(&dict,
749                                                 "wep_key0", DBUS_TYPE_STRING,
750                                                                 &passphrase);
751                         connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
752                                                 DBUS_TYPE_STRING, &index);
753                 }
754         } else {
755                 const char *key_mgmt = "NONE";
756                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
757                                                 DBUS_TYPE_STRING, &key_mgmt);
758         }
759
760         dbus_message_iter_close_container(&array, &dict);
761
762         dbus_error_init(&error);
763
764         reply = dbus_connection_send_with_reply_and_block(connection,
765                                                         message, -1, &error);
766         if (reply == NULL) {
767                 if (dbus_error_is_set(&error) == TRUE) {
768                         connman_error("%s", error.message);
769                         dbus_error_free(&error);
770                 } else
771                         connman_error("Failed to set network options");
772                 dbus_message_unref(message);
773                 return -EIO;
774         }
775
776         dbus_message_unref(message);
777
778         dbus_message_unref(reply);
779
780         return 0;
781 }
782
783 static int initiate_scan(struct supplicant_task *task)
784 {
785         DBusMessage *message;
786         DBusPendingCall *call;
787
788         DBG("task %p", task);
789
790         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
791                                         SUPPLICANT_INTF ".Interface", "scan");
792         if (message == NULL)
793                 return -ENOMEM;
794
795         if (dbus_connection_send_with_reply(connection, message,
796                                                 &call, TIMEOUT) == FALSE) {
797                 connman_error("Failed to initiate scan");
798                 dbus_message_unref(message);
799                 return -EIO;
800         }
801
802         dbus_message_unref(message);
803
804         return 0;
805 }
806
807 static struct {
808         char *name;
809         char *value;
810 } special_ssid[] = {
811         { "<hidden>", "hidden"  },
812         { "default",  "linksys" },
813         { "wireless"  },
814         { "linksys"   },
815         { "netgear"   },
816         { "dlink"     },
817         { "2wire"     },
818         { "compaq"    },
819         { "tsunami"   },
820         { "comcomcom" },
821         { "Symbol",    "symbol"   },
822         { "Wireless" , "wireless" },
823         { "WLAN",      "wlan"     },
824         { }
825 };
826
827 static char *build_group(const char *addr, const char *name,
828                         const unsigned char *ssid, unsigned int ssid_len,
829                                         const char *mode, const char *security)
830 {
831         GString *str;
832         unsigned int i;
833
834         if (addr == NULL)
835                 return NULL;
836
837         str = g_string_sized_new((ssid_len * 2) + 24);
838         if (str == NULL)
839                 return NULL;
840
841         for (i = 0; special_ssid[i].name; i++) {
842                 if (g_strcmp0(special_ssid[i].name, name) == 0) {
843                         if (special_ssid[i].value == NULL)
844                                 g_string_append_printf(str, "%s_%s",
845                                                                 name, addr);
846                         else
847                                 g_string_append_printf(str, "%s_%s",
848                                                 special_ssid[i].value, addr);
849                         goto done;
850                 }
851         }
852
853         if (ssid_len > 0 && ssid[0] != '\0') {
854                 for (i = 0; i < ssid_len; i++)
855                         g_string_append_printf(str, "%02x", ssid[i]);
856         } else
857                 g_string_append_printf(str, "hidden_%s", addr);
858
859 done:
860         g_string_append_printf(str, "_%s_%s", mode, security);
861
862         return g_string_free(str, FALSE);
863 }
864
865 static void extract_addr(DBusMessageIter *value,
866                                         struct supplicant_result *result)
867 {
868         DBusMessageIter array;
869         struct ether_addr *eth;
870         unsigned char *addr;
871         int addr_len;
872
873         dbus_message_iter_recurse(value, &array);
874         dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
875
876         if (addr_len != 6)
877                 return;
878
879         eth = (void *) addr;
880
881         result->addr = g_try_malloc0(18);
882         if (result->addr == NULL)
883                 return;
884
885         snprintf(result->addr, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
886                                                 eth->ether_addr_octet[0],
887                                                 eth->ether_addr_octet[1],
888                                                 eth->ether_addr_octet[2],
889                                                 eth->ether_addr_octet[3],
890                                                 eth->ether_addr_octet[4],
891                                                 eth->ether_addr_octet[5]);
892
893         result->path = g_try_malloc0(18);
894         if (result->path == NULL)
895                 return;
896
897         snprintf(result->path, 18, "%02x%02x%02x%02x%02x%02x",
898                                                 eth->ether_addr_octet[0],
899                                                 eth->ether_addr_octet[1],
900                                                 eth->ether_addr_octet[2],
901                                                 eth->ether_addr_octet[3],
902                                                 eth->ether_addr_octet[4],
903                                                 eth->ether_addr_octet[5]);
904 }
905
906 static void extract_ssid(DBusMessageIter *value,
907                                         struct supplicant_result *result)
908 {
909         DBusMessageIter array;
910         unsigned char *ssid;
911         int ssid_len;
912
913         dbus_message_iter_recurse(value, &array);
914         dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
915
916         if (ssid_len < 1)
917                 return;
918
919         result->ssid = g_try_malloc(ssid_len);
920         if (result->ssid == NULL)
921                 return;
922
923         memcpy(result->ssid, ssid, ssid_len);
924         result->ssid_len = ssid_len;
925
926         result->name = g_try_malloc0(ssid_len + 1);
927         if (result->name == NULL)
928                 return;
929
930         memcpy(result->name, ssid, ssid_len);
931 }
932
933 static void extract_wpaie(DBusMessageIter *value,
934                                         struct supplicant_result *result)
935 {
936         DBusMessageIter array;
937         unsigned char *ie;
938         int ie_len;
939
940         dbus_message_iter_recurse(value, &array);
941         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
942
943         if (ie_len > 0)
944                 result->has_wpa = TRUE;
945 }
946
947 static void extract_rsnie(DBusMessageIter *value,
948                                         struct supplicant_result *result)
949 {
950         DBusMessageIter array;
951         unsigned char *ie;
952         int ie_len;
953
954         dbus_message_iter_recurse(value, &array);
955         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
956
957         if (ie_len > 0)
958                 result->has_rsn = TRUE;
959 }
960
961 static void extract_wpsie(DBusMessageIter *value,
962                                         struct supplicant_result *result)
963 {
964         DBusMessageIter array;
965         unsigned char *ie;
966         int ie_len;
967
968         dbus_message_iter_recurse(value, &array);
969         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
970
971         if (ie_len > 0)
972                 result->has_wps = TRUE;
973 }
974
975 static void extract_capabilites(DBusMessageIter *value,
976                                         struct supplicant_result *result)
977 {
978         dbus_message_iter_get_basic(value, &result->capabilities);
979
980         if (result->capabilities & IEEE80211_CAP_ESS)
981                 result->adhoc = FALSE;
982         else if (result->capabilities & IEEE80211_CAP_IBSS)
983                 result->adhoc = TRUE;
984
985         if (result->capabilities & IEEE80211_CAP_PRIVACY)
986                 result->has_wep = TRUE;
987 }
988
989 static unsigned char calculate_strength(struct supplicant_result *result)
990 {
991         if (result->quality < 0)
992                 return 0;
993
994         return result->quality;
995 }
996
997 static void get_properties(struct supplicant_task *task);
998
999 static void properties_reply(DBusPendingCall *call, void *user_data)
1000 {
1001         struct supplicant_task *task = user_data;
1002         struct supplicant_result result;
1003         struct connman_network *network;
1004         DBusMessage *reply;
1005         DBusMessageIter array, dict;
1006         unsigned char strength;
1007         unsigned short frequency;
1008         const char *mode, *security;
1009         char *group;
1010
1011         DBG("task %p", task);
1012
1013         reply = dbus_pending_call_steal_reply(call);
1014         if (reply == NULL) {
1015                 get_properties(task);
1016                 return;
1017         }
1018
1019         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1020                 dbus_message_unref(reply);
1021                 get_properties(task);
1022                 return;
1023         }
1024
1025         memset(&result, 0, sizeof(result));
1026         result.quality = -1;
1027         result.level = -1;
1028         result.noise = -1;
1029
1030         dbus_message_iter_init(reply, &array);
1031
1032         dbus_message_iter_recurse(&array, &dict);
1033
1034         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1035                 DBusMessageIter entry, value;
1036                 const char *key;
1037
1038                 dbus_message_iter_recurse(&dict, &entry);
1039                 dbus_message_iter_get_basic(&entry, &key);
1040
1041                 dbus_message_iter_next(&entry);
1042
1043                 dbus_message_iter_recurse(&entry, &value);
1044
1045                 //type = dbus_message_iter_get_arg_type(&value);
1046                 //dbus_message_iter_get_basic(&value, &val);
1047
1048                 /* 
1049                  * bssid        : a (97)
1050                  * ssid         : a (97)
1051                  * wpaie        : a (97)
1052                  * rsnie        : a (97)
1053                  * wpsie        : a (97)
1054                  * frequency    : i (105)
1055                  * capabilities : q (113)
1056                  * quality      : i (105)
1057                  * noise        : i (105)
1058                  * level        : i (105)
1059                  * maxrate      : i (105)
1060                  */
1061
1062                 if (g_str_equal(key, "bssid") == TRUE)
1063                         extract_addr(&value, &result);
1064                 else if (g_str_equal(key, "ssid") == TRUE)
1065                         extract_ssid(&value, &result);
1066                 else if (g_str_equal(key, "wpaie") == TRUE)
1067                         extract_wpaie(&value, &result);
1068                 else if (g_str_equal(key, "rsnie") == TRUE)
1069                         extract_rsnie(&value, &result);
1070                 else if (g_str_equal(key, "wpsie") == TRUE)
1071                         extract_wpsie(&value, &result);
1072                 else if (g_str_equal(key, "capabilities") == TRUE)
1073                         extract_capabilites(&value, &result);
1074                 else if (g_str_equal(key, "frequency") == TRUE)
1075                         dbus_message_iter_get_basic(&value, &result.frequency);
1076                 else if (g_str_equal(key, "quality") == TRUE)
1077                         dbus_message_iter_get_basic(&value, &result.quality);
1078                 else if (g_str_equal(key, "noise") == TRUE)
1079                         dbus_message_iter_get_basic(&value, &result.noise);
1080                 else if (g_str_equal(key, "level") == TRUE)
1081                         dbus_message_iter_get_basic(&value, &result.level);
1082                 else if (g_str_equal(key, "maxrate") == TRUE)
1083                         dbus_message_iter_get_basic(&value, &result.maxrate);
1084
1085                 dbus_message_iter_next(&dict);
1086         }
1087
1088         if (result.path == NULL)
1089                 goto done;
1090
1091         if (result.path[0] == '\0')
1092                 goto done;
1093
1094         strength  = calculate_strength(&result);
1095         frequency = result.frequency;
1096
1097         if (result.has_rsn == TRUE)
1098                 security = "rsn";
1099         else if (result.has_wpa == TRUE)
1100                 security = "wpa";
1101         else if (result.has_wep == TRUE)
1102                 security = "wep";
1103         else
1104                 security = "none";
1105
1106         mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1107
1108         group = build_group(result.path, result.name,
1109                                         result.ssid, result.ssid_len,
1110                                                         mode, security);
1111
1112         network = connman_device_get_network(task->device, result.path);
1113         if (network == NULL) {
1114                 int index;
1115
1116                 network = connman_network_create(result.path,
1117                                                 CONNMAN_NETWORK_TYPE_WIFI);
1118                 if (network == NULL)
1119                         goto done;
1120
1121                 index = connman_device_get_index(task->device);
1122                 connman_network_set_index(network, index);
1123
1124                 connman_network_set_protocol(network,
1125                                                 CONNMAN_NETWORK_PROTOCOL_IP);
1126
1127                 connman_network_set_string(network, "Address", result.addr);
1128
1129                 if (connman_device_add_network(task->device, network) < 0) {
1130                         connman_network_unref(network);
1131                         goto done;
1132                 }
1133         }
1134
1135         if (result.name != NULL && result.name[0] != '\0')
1136                 connman_network_set_string(network, "Name", result.name);
1137
1138         connman_network_set_blob(network, "WiFi.SSID",
1139                                                 result.ssid, result.ssid_len);
1140
1141         connman_network_set_string(network, "WiFi.Mode", mode);
1142
1143         DBG("%s (%s %s) strength %d (%s)",
1144                                 result.name, mode, security, strength,
1145                                 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1146
1147         connman_network_set_available(network, TRUE);
1148         connman_network_set_uint8(network, "Strength", strength);
1149         connman_network_set_uint16(network, "Frequency", frequency);
1150
1151         connman_network_set_string(network, "WiFi.Security", security);
1152
1153         connman_network_set_group(network, group);
1154
1155         g_free(group);
1156
1157 done:
1158         g_free(result.path);
1159         g_free(result.addr);
1160         g_free(result.name);
1161         g_free(result.ssid);
1162
1163         dbus_message_unref(reply);
1164
1165         get_properties(task);
1166 }
1167
1168 static void get_properties(struct supplicant_task *task)
1169 {
1170         DBusMessage *message;
1171         DBusPendingCall *call;
1172         char *path;
1173
1174         path = g_slist_nth_data(task->scan_results, 0);
1175         if (path == NULL)
1176                 goto noscan;
1177
1178         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1179                                                 SUPPLICANT_INTF ".BSSID",
1180                                                                 "properties");
1181
1182         task->scan_results = g_slist_remove(task->scan_results, path);
1183         g_free(path);
1184
1185         if (message == NULL)
1186                 goto noscan;
1187
1188         if (dbus_connection_send_with_reply(connection, message,
1189                                                 &call, TIMEOUT) == FALSE) {
1190                 connman_error("Failed to get network properties");
1191                 dbus_message_unref(message);
1192                 goto noscan;
1193         }
1194
1195         if (call == NULL) {
1196                 connman_error("D-Bus connection not available");
1197                 dbus_message_unref(message);
1198                 goto noscan;
1199         }
1200
1201         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1202
1203         dbus_message_unref(message);
1204
1205         return;
1206
1207 noscan:
1208         if (task->noscan == FALSE)
1209                 connman_device_set_scanning(task->device, FALSE);
1210 }
1211
1212 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1213 {
1214         struct supplicant_task *task = user_data;
1215         DBusMessage *reply;
1216         DBusError error;
1217         char **results;
1218         int i, num_results;
1219
1220         DBG("task %p", task);
1221
1222         reply = dbus_pending_call_steal_reply(call);
1223         if (reply == NULL)
1224                 goto noscan;
1225
1226         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1227                 goto done;
1228
1229         dbus_error_init(&error);
1230
1231         if (dbus_message_get_args(reply, &error,
1232                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1233                                                 &results, &num_results,
1234                                                 DBUS_TYPE_INVALID) == FALSE) {
1235                 if (dbus_error_is_set(&error) == TRUE) {
1236                         connman_error("%s", error.message);
1237                         dbus_error_free(&error);
1238                 } else
1239                         connman_error("Wrong arguments for scan result");
1240                 goto done;
1241         }
1242
1243         if (num_results == 0)
1244                 goto done;
1245
1246         for (i = 0; i < num_results; i++) {
1247                 char *path = g_strdup(results[i]);
1248                 if (path == NULL)
1249                         continue;
1250
1251                 task->scan_results = g_slist_append(task->scan_results, path);
1252         }
1253
1254         g_strfreev(results);
1255
1256         dbus_message_unref(reply);
1257
1258         get_properties(task);
1259
1260         return;
1261
1262 done:
1263         dbus_message_unref(reply);
1264
1265 noscan:
1266         if (task->noscan == FALSE)
1267                 connman_device_set_scanning(task->device, FALSE);
1268 }
1269
1270 static void scan_results_available(struct supplicant_task *task)
1271 {
1272         DBusMessage *message;
1273         DBusPendingCall *call;
1274
1275         DBG("task %p", task);
1276
1277         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1278                                                 SUPPLICANT_INTF ".Interface",
1279                                                         "scanResults");
1280         if (message == NULL)
1281                 return;
1282
1283         if (dbus_connection_send_with_reply(connection, message,
1284                                                 &call, TIMEOUT) == FALSE) {
1285                 connman_error("Failed to request scan result");
1286                 goto done;
1287         }
1288
1289         if (task->noscan == FALSE)
1290                 connman_device_set_scanning(task->device, TRUE);
1291
1292         if (call == NULL) {
1293                 connman_error("D-Bus connection not available");
1294                 goto done;
1295         }
1296
1297         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1298
1299 done:
1300         dbus_message_unref(message);
1301 }
1302
1303 static enum supplicant_state string2state(const char *state)
1304 {
1305         if (g_str_equal(state, "INACTIVE") == TRUE)
1306                 return WPA_INACTIVE;
1307         else if (g_str_equal(state, "SCANNING") == TRUE)
1308                 return WPA_SCANNING;
1309         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1310                 return WPA_ASSOCIATING;
1311         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1312                 return WPA_ASSOCIATED;
1313         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1314                 return WPA_GROUP_HANDSHAKE;
1315         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1316                 return WPA_4WAY_HANDSHAKE;
1317         else if (g_str_equal(state, "COMPLETED") == TRUE)
1318                 return WPA_COMPLETED;
1319         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1320                 return WPA_DISCONNECTED;
1321         else
1322                 return WPA_INVALID;
1323 }
1324
1325 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1326 {
1327         DBusError error;
1328         const char *newstate, *oldstate;
1329         enum supplicant_state state;
1330
1331         dbus_error_init(&error);
1332
1333         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1334                                                 DBUS_TYPE_STRING, &oldstate,
1335                                                 DBUS_TYPE_INVALID) == FALSE) {
1336                 if (dbus_error_is_set(&error) == TRUE) {
1337                         connman_error("%s", error.message);
1338                         dbus_error_free(&error);
1339                 } else
1340                         connman_error("Wrong arguments for state change");
1341                 return;
1342         }
1343
1344         DBG("state %s ==> %s", oldstate, newstate);
1345
1346         state = string2state(newstate);
1347         if (state == WPA_INVALID)
1348                 return;
1349
1350         task->state = state;
1351
1352         switch (task->state) {
1353         case WPA_SCANNING:
1354                 task->noscan = TRUE;
1355                 connman_device_set_scanning(task->device, TRUE);
1356                 break;
1357         case WPA_ASSOCIATING:
1358         case WPA_ASSOCIATED:
1359         case WPA_4WAY_HANDSHAKE:
1360         case WPA_GROUP_HANDSHAKE:
1361                 task->noscan = TRUE;
1362                 break;
1363         case WPA_COMPLETED:
1364         case WPA_DISCONNECTED:
1365                 task->noscan = FALSE;
1366                 break;
1367         case WPA_INACTIVE:
1368                 task->noscan = FALSE;
1369                 connman_device_set_scanning(task->device, FALSE);
1370                 break;
1371         case WPA_INVALID:
1372                 break;
1373         }
1374
1375         if (task->network == NULL)
1376                 return;
1377
1378         switch (task->state) {
1379         case WPA_COMPLETED:
1380                 /* carrier on */
1381                 connman_network_set_connected(task->network, TRUE);
1382                 connman_device_set_scanning(task->device, FALSE);
1383                 break;
1384         case WPA_DISCONNECTED:
1385                 /* carrier off */
1386                 connman_network_set_connected(task->network, FALSE);
1387                 connman_device_set_scanning(task->device, FALSE);
1388                 break;
1389         case WPA_ASSOCIATING:
1390                 connman_network_set_associating(task->network, TRUE);
1391                 break;
1392         default:
1393                 connman_network_set_associating(task->network, FALSE);
1394                 break;
1395         }
1396 }
1397
1398 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1399                                                 DBusMessage *msg, void *data)
1400 {
1401         struct supplicant_task *task;
1402         const char *member, *path;
1403
1404         if (dbus_message_has_interface(msg,
1405                                 SUPPLICANT_INTF ".Interface") == FALSE)
1406                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1407
1408         member = dbus_message_get_member(msg);
1409         if (member == NULL)
1410                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1411
1412         path = dbus_message_get_path(msg);
1413         if (path == NULL)
1414                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1415
1416         task = find_task_by_path(path);
1417         if (task == NULL)
1418                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1419
1420         DBG("task %p member %s", task, member);
1421
1422         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1423                 scan_results_available(task);
1424         else if (g_str_equal(member, "StateChange") == TRUE)
1425                 state_change(task, msg);
1426
1427         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1428 }
1429
1430 int supplicant_start(struct connman_device *device)
1431 {
1432         struct supplicant_task *task;
1433
1434         DBG("device %p", device);
1435
1436         task = g_try_new0(struct supplicant_task, 1);
1437         if (task == NULL)
1438                 return -ENOMEM;
1439
1440         task->ifindex = connman_device_get_index(device);
1441         task->ifname = inet_index2name(task->ifindex);
1442
1443         if (task->ifname == NULL) {
1444                 g_free(task);
1445                 return -ENOMEM;
1446         }
1447
1448         task->device = connman_device_ref(device);
1449
1450         task->created = FALSE;
1451         task->noscan = FALSE;
1452         task->state = WPA_INVALID;
1453
1454         task_list = g_slist_append(task_list, task);
1455
1456         return create_interface(task);
1457 }
1458
1459 int supplicant_stop(struct connman_device *device)
1460 {
1461         int index = connman_device_get_index(device);
1462         struct supplicant_task *task;
1463
1464         DBG("device %p", device);
1465
1466         task = find_task_by_index(index);
1467         if (task == NULL)
1468                 return -ENODEV;
1469
1470         task_list = g_slist_remove(task_list, task);
1471
1472         disable_network(task);
1473
1474         remove_network(task);
1475
1476         return remove_interface(task);
1477 }
1478
1479 int supplicant_scan(struct connman_device *device)
1480 {
1481         int index = connman_device_get_index(device);
1482         struct supplicant_task *task;
1483         int err;
1484
1485         DBG("device %p", device);
1486
1487         task = find_task_by_index(index);
1488         if (task == NULL)
1489                 return -ENODEV;
1490
1491         switch (task->state) {
1492         case WPA_SCANNING:
1493                 return -EALREADY;
1494         case WPA_ASSOCIATING:
1495         case WPA_ASSOCIATED:
1496         case WPA_4WAY_HANDSHAKE:
1497         case WPA_GROUP_HANDSHAKE:
1498                 return -EBUSY;
1499         default:
1500                 break;
1501         }
1502
1503         err = initiate_scan(task);
1504
1505         return 0;
1506 }
1507
1508 int supplicant_connect(struct connman_network *network)
1509 {
1510         struct supplicant_task *task;
1511         const char *address, *security, *passphrase;
1512         const void *ssid;
1513         unsigned int ssid_len;
1514         int index;
1515
1516         DBG("network %p", network);
1517
1518         address = connman_network_get_string(network, "Address");
1519         security = connman_network_get_string(network, "WiFi.Security");
1520         passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1521
1522         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1523
1524         DBG("address %s security %s passphrase %s",
1525                                         address, security, passphrase);
1526
1527         if (security == NULL && passphrase == NULL)
1528                 return -EINVAL;
1529
1530         if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1531                 return -EINVAL;
1532
1533         index = connman_network_get_index(network);
1534
1535         task = find_task_by_index(index);
1536         if (task == NULL)
1537                 return -ENODEV;
1538
1539         task->network = connman_network_ref(network);
1540
1541         add_network(task);
1542
1543         select_network(task);
1544         disable_network(task);
1545
1546         set_network(task, ssid, ssid_len, address, security, passphrase);
1547
1548         enable_network(task);
1549
1550         connman_network_set_associating(task->network, TRUE);
1551
1552         return 0;
1553 }
1554
1555 int supplicant_disconnect(struct connman_network *network)
1556 {
1557         struct supplicant_task *task;
1558         int index;
1559
1560         DBG("network %p", network);
1561
1562         index = connman_network_get_index(network);
1563
1564         task = find_task_by_index(index);
1565         if (task == NULL)
1566                 return -ENODEV;
1567
1568         disable_network(task);
1569
1570         remove_network(task);
1571
1572         connman_network_set_connected(task->network, FALSE);
1573
1574         connman_network_unref(task->network);
1575
1576         return 0;
1577 }
1578
1579 static void supplicant_activate(DBusConnection *conn)
1580 {
1581         DBusMessage *message;
1582
1583         DBG("conn %p", conn);
1584
1585         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1586                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1587         if (message == NULL)
1588                 return;
1589
1590         dbus_message_set_no_reply(message, TRUE);
1591
1592         dbus_connection_send(conn, message, NULL);
1593
1594         dbus_message_unref(message);
1595 }
1596
1597 static GSList *driver_list = NULL;
1598
1599 static void supplicant_probe(DBusConnection *conn, void *user_data)
1600 {
1601         GSList *list;
1602
1603         DBG("conn %p", conn);
1604
1605         for (list = driver_list; list; list = list->next) {
1606                 struct supplicant_driver *driver = list->data;
1607
1608                 DBG("driver %p name %s", driver, driver->name);
1609
1610                 if (driver->probe)
1611                         driver->probe();
1612         }
1613 }
1614
1615 static void supplicant_remove(DBusConnection *conn, void *user_data)
1616 {
1617         GSList *list;
1618
1619         DBG("conn %p", conn);
1620
1621         for (list = driver_list; list; list = list->next) {
1622                 struct supplicant_driver *driver = list->data;
1623
1624                 DBG("driver %p name %s", driver, driver->name);
1625
1626                 if (driver->remove)
1627                         driver->remove();
1628         }
1629 }
1630
1631 static const char *supplicant_rule = "type=signal,"
1632                                 "interface=" SUPPLICANT_INTF ".Interface";
1633 static guint watch;
1634
1635 static int supplicant_create(void)
1636 {
1637         if (g_slist_length(driver_list) > 0)
1638                 return 0;
1639
1640         connection = connman_dbus_get_connection();
1641         if (connection == NULL)
1642                 return -EIO;
1643
1644         DBG("connection %p", connection);
1645
1646         if (dbus_connection_add_filter(connection,
1647                                 supplicant_filter, NULL, NULL) == FALSE) {
1648                 connection = connman_dbus_get_connection();
1649                 return -EIO;
1650         }
1651
1652         dbus_bus_add_match(connection, supplicant_rule, NULL);
1653         dbus_connection_flush(connection);
1654
1655         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1656                         supplicant_probe, supplicant_remove, NULL, NULL);
1657
1658         return 0;
1659 }
1660
1661 static void supplicant_destroy(void)
1662 {
1663         if (g_slist_length(driver_list) > 0)
1664                 return;
1665
1666         DBG("connection %p", connection);
1667
1668         if (watch > 0)
1669                 g_dbus_remove_watch(connection, watch);
1670
1671         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1672         dbus_connection_flush(connection);
1673
1674         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1675
1676         dbus_connection_unref(connection);
1677         connection = NULL;
1678 }
1679
1680 int supplicant_register(struct supplicant_driver *driver)
1681 {
1682         int err;
1683
1684         DBG("driver %p name %s", driver, driver->name);
1685
1686         err = supplicant_create();
1687         if (err < 0)
1688                 return err;
1689
1690         driver_list = g_slist_append(driver_list, driver);
1691
1692         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1693                 supplicant_probe(connection, NULL);
1694         else
1695                 supplicant_activate(connection);
1696
1697         return 0;
1698 }
1699
1700 void supplicant_unregister(struct supplicant_driver *driver)
1701 {
1702         DBG("driver %p name %s", driver, driver->name);
1703
1704         supplicant_remove(connection, NULL);
1705
1706         driver_list = g_slist_remove(driver_list, driver);
1707
1708         supplicant_destroy();
1709 }