Add basic support for WiFi channel property
[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 unsigned short calculate_channel(struct supplicant_result *result)
998 {
999         return 0;
1000 }
1001
1002 static void get_properties(struct supplicant_task *task);
1003
1004 static void properties_reply(DBusPendingCall *call, void *user_data)
1005 {
1006         struct supplicant_task *task = user_data;
1007         struct supplicant_result result;
1008         struct connman_network *network;
1009         DBusMessage *reply;
1010         DBusMessageIter array, dict;
1011         unsigned char strength;
1012         unsigned short channel, frequency;
1013         const char *mode, *security;
1014         char *group;
1015
1016         DBG("task %p", task);
1017
1018         reply = dbus_pending_call_steal_reply(call);
1019         if (reply == NULL) {
1020                 get_properties(task);
1021                 return;
1022         }
1023
1024         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1025                 dbus_message_unref(reply);
1026                 get_properties(task);
1027                 return;
1028         }
1029
1030         memset(&result, 0, sizeof(result));
1031         result.frequency = -1;
1032         result.quality = -1;
1033         result.level = -1;
1034         result.noise = -1;
1035
1036         dbus_message_iter_init(reply, &array);
1037
1038         dbus_message_iter_recurse(&array, &dict);
1039
1040         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1041                 DBusMessageIter entry, value;
1042                 const char *key;
1043
1044                 dbus_message_iter_recurse(&dict, &entry);
1045                 dbus_message_iter_get_basic(&entry, &key);
1046
1047                 dbus_message_iter_next(&entry);
1048
1049                 dbus_message_iter_recurse(&entry, &value);
1050
1051                 //type = dbus_message_iter_get_arg_type(&value);
1052                 //dbus_message_iter_get_basic(&value, &val);
1053
1054                 /* 
1055                  * bssid        : a (97)
1056                  * ssid         : a (97)
1057                  * wpaie        : a (97)
1058                  * rsnie        : a (97)
1059                  * wpsie        : a (97)
1060                  * frequency    : i (105)
1061                  * capabilities : q (113)
1062                  * quality      : i (105)
1063                  * noise        : i (105)
1064                  * level        : i (105)
1065                  * maxrate      : i (105)
1066                  */
1067
1068                 if (g_str_equal(key, "bssid") == TRUE)
1069                         extract_addr(&value, &result);
1070                 else if (g_str_equal(key, "ssid") == TRUE)
1071                         extract_ssid(&value, &result);
1072                 else if (g_str_equal(key, "wpaie") == TRUE)
1073                         extract_wpaie(&value, &result);
1074                 else if (g_str_equal(key, "rsnie") == TRUE)
1075                         extract_rsnie(&value, &result);
1076                 else if (g_str_equal(key, "wpsie") == TRUE)
1077                         extract_wpsie(&value, &result);
1078                 else if (g_str_equal(key, "capabilities") == TRUE)
1079                         extract_capabilites(&value, &result);
1080                 else if (g_str_equal(key, "frequency") == TRUE)
1081                         dbus_message_iter_get_basic(&value, &result.frequency);
1082                 else if (g_str_equal(key, "quality") == TRUE)
1083                         dbus_message_iter_get_basic(&value, &result.quality);
1084                 else if (g_str_equal(key, "noise") == TRUE)
1085                         dbus_message_iter_get_basic(&value, &result.noise);
1086                 else if (g_str_equal(key, "level") == TRUE)
1087                         dbus_message_iter_get_basic(&value, &result.level);
1088                 else if (g_str_equal(key, "maxrate") == TRUE)
1089                         dbus_message_iter_get_basic(&value, &result.maxrate);
1090
1091                 dbus_message_iter_next(&dict);
1092         }
1093
1094         if (result.path == NULL)
1095                 goto done;
1096
1097         if (result.path[0] == '\0')
1098                 goto done;
1099
1100         strength = calculate_strength(&result);
1101         channel  = calculate_channel(&result);
1102
1103         frequency = (result.frequency < 0) ? 0 : result.frequency;
1104
1105         if (result.has_rsn == TRUE)
1106                 security = "rsn";
1107         else if (result.has_wpa == TRUE)
1108                 security = "wpa";
1109         else if (result.has_wep == TRUE)
1110                 security = "wep";
1111         else
1112                 security = "none";
1113
1114         mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1115
1116         group = build_group(result.path, result.name,
1117                                         result.ssid, result.ssid_len,
1118                                                         mode, security);
1119
1120         network = connman_device_get_network(task->device, result.path);
1121         if (network == NULL) {
1122                 int index;
1123
1124                 network = connman_network_create(result.path,
1125                                                 CONNMAN_NETWORK_TYPE_WIFI);
1126                 if (network == NULL)
1127                         goto done;
1128
1129                 index = connman_device_get_index(task->device);
1130                 connman_network_set_index(network, index);
1131
1132                 connman_network_set_protocol(network,
1133                                                 CONNMAN_NETWORK_PROTOCOL_IP);
1134
1135                 connman_network_set_string(network, "Address", result.addr);
1136
1137                 if (connman_device_add_network(task->device, network) < 0) {
1138                         connman_network_unref(network);
1139                         goto done;
1140                 }
1141         }
1142
1143         if (result.name != NULL && result.name[0] != '\0')
1144                 connman_network_set_string(network, "Name", result.name);
1145
1146         connman_network_set_blob(network, "WiFi.SSID",
1147                                                 result.ssid, result.ssid_len);
1148
1149         connman_network_set_string(network, "WiFi.Mode", mode);
1150
1151         DBG("%s (%s %s) strength %d (%s)",
1152                                 result.name, mode, security, strength,
1153                                 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1154
1155         connman_network_set_available(network, TRUE);
1156         connman_network_set_uint8(network, "Strength", strength);
1157         connman_network_set_uint16(network, "Frequency", frequency);
1158
1159         connman_network_set_uint16(network, "WiFi.Channel", channel);
1160         connman_network_set_string(network, "WiFi.Security", security);
1161
1162         connman_network_set_group(network, group);
1163
1164         g_free(group);
1165
1166 done:
1167         g_free(result.path);
1168         g_free(result.addr);
1169         g_free(result.name);
1170         g_free(result.ssid);
1171
1172         dbus_message_unref(reply);
1173
1174         get_properties(task);
1175 }
1176
1177 static void get_properties(struct supplicant_task *task)
1178 {
1179         DBusMessage *message;
1180         DBusPendingCall *call;
1181         char *path;
1182
1183         path = g_slist_nth_data(task->scan_results, 0);
1184         if (path == NULL)
1185                 goto noscan;
1186
1187         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1188                                                 SUPPLICANT_INTF ".BSSID",
1189                                                                 "properties");
1190
1191         task->scan_results = g_slist_remove(task->scan_results, path);
1192         g_free(path);
1193
1194         if (message == NULL)
1195                 goto noscan;
1196
1197         if (dbus_connection_send_with_reply(connection, message,
1198                                                 &call, TIMEOUT) == FALSE) {
1199                 connman_error("Failed to get network properties");
1200                 dbus_message_unref(message);
1201                 goto noscan;
1202         }
1203
1204         if (call == NULL) {
1205                 connman_error("D-Bus connection not available");
1206                 dbus_message_unref(message);
1207                 goto noscan;
1208         }
1209
1210         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1211
1212         dbus_message_unref(message);
1213
1214         return;
1215
1216 noscan:
1217         if (task->noscan == FALSE)
1218                 connman_device_set_scanning(task->device, FALSE);
1219 }
1220
1221 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1222 {
1223         struct supplicant_task *task = user_data;
1224         DBusMessage *reply;
1225         DBusError error;
1226         char **results;
1227         int i, num_results;
1228
1229         DBG("task %p", task);
1230
1231         reply = dbus_pending_call_steal_reply(call);
1232         if (reply == NULL)
1233                 goto noscan;
1234
1235         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1236                 goto done;
1237
1238         dbus_error_init(&error);
1239
1240         if (dbus_message_get_args(reply, &error,
1241                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1242                                                 &results, &num_results,
1243                                                 DBUS_TYPE_INVALID) == FALSE) {
1244                 if (dbus_error_is_set(&error) == TRUE) {
1245                         connman_error("%s", error.message);
1246                         dbus_error_free(&error);
1247                 } else
1248                         connman_error("Wrong arguments for scan result");
1249                 goto done;
1250         }
1251
1252         if (num_results == 0)
1253                 goto done;
1254
1255         for (i = 0; i < num_results; i++) {
1256                 char *path = g_strdup(results[i]);
1257                 if (path == NULL)
1258                         continue;
1259
1260                 task->scan_results = g_slist_append(task->scan_results, path);
1261         }
1262
1263         g_strfreev(results);
1264
1265         dbus_message_unref(reply);
1266
1267         get_properties(task);
1268
1269         return;
1270
1271 done:
1272         dbus_message_unref(reply);
1273
1274 noscan:
1275         if (task->noscan == FALSE)
1276                 connman_device_set_scanning(task->device, FALSE);
1277 }
1278
1279 static void scan_results_available(struct supplicant_task *task)
1280 {
1281         DBusMessage *message;
1282         DBusPendingCall *call;
1283
1284         DBG("task %p", task);
1285
1286         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1287                                                 SUPPLICANT_INTF ".Interface",
1288                                                         "scanResults");
1289         if (message == NULL)
1290                 return;
1291
1292         if (dbus_connection_send_with_reply(connection, message,
1293                                                 &call, TIMEOUT) == FALSE) {
1294                 connman_error("Failed to request scan result");
1295                 goto done;
1296         }
1297
1298         if (task->noscan == FALSE)
1299                 connman_device_set_scanning(task->device, TRUE);
1300
1301         if (call == NULL) {
1302                 connman_error("D-Bus connection not available");
1303                 goto done;
1304         }
1305
1306         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1307
1308 done:
1309         dbus_message_unref(message);
1310 }
1311
1312 static enum supplicant_state string2state(const char *state)
1313 {
1314         if (g_str_equal(state, "INACTIVE") == TRUE)
1315                 return WPA_INACTIVE;
1316         else if (g_str_equal(state, "SCANNING") == TRUE)
1317                 return WPA_SCANNING;
1318         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1319                 return WPA_ASSOCIATING;
1320         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1321                 return WPA_ASSOCIATED;
1322         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1323                 return WPA_GROUP_HANDSHAKE;
1324         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1325                 return WPA_4WAY_HANDSHAKE;
1326         else if (g_str_equal(state, "COMPLETED") == TRUE)
1327                 return WPA_COMPLETED;
1328         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1329                 return WPA_DISCONNECTED;
1330         else
1331                 return WPA_INVALID;
1332 }
1333
1334 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1335 {
1336         DBusError error;
1337         const char *newstate, *oldstate;
1338         enum supplicant_state state;
1339
1340         dbus_error_init(&error);
1341
1342         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1343                                                 DBUS_TYPE_STRING, &oldstate,
1344                                                 DBUS_TYPE_INVALID) == FALSE) {
1345                 if (dbus_error_is_set(&error) == TRUE) {
1346                         connman_error("%s", error.message);
1347                         dbus_error_free(&error);
1348                 } else
1349                         connman_error("Wrong arguments for state change");
1350                 return;
1351         }
1352
1353         DBG("state %s ==> %s", oldstate, newstate);
1354
1355         state = string2state(newstate);
1356         if (state == WPA_INVALID)
1357                 return;
1358
1359         task->state = state;
1360
1361         switch (task->state) {
1362         case WPA_SCANNING:
1363                 task->noscan = TRUE;
1364                 connman_device_set_scanning(task->device, TRUE);
1365                 break;
1366         case WPA_ASSOCIATING:
1367         case WPA_ASSOCIATED:
1368         case WPA_4WAY_HANDSHAKE:
1369         case WPA_GROUP_HANDSHAKE:
1370                 task->noscan = TRUE;
1371                 break;
1372         case WPA_COMPLETED:
1373         case WPA_DISCONNECTED:
1374                 task->noscan = FALSE;
1375                 break;
1376         case WPA_INACTIVE:
1377                 task->noscan = FALSE;
1378                 connman_device_set_scanning(task->device, FALSE);
1379                 break;
1380         case WPA_INVALID:
1381                 break;
1382         }
1383
1384         if (task->network == NULL)
1385                 return;
1386
1387         switch (task->state) {
1388         case WPA_COMPLETED:
1389                 /* carrier on */
1390                 connman_network_set_connected(task->network, TRUE);
1391                 connman_device_set_scanning(task->device, FALSE);
1392                 break;
1393         case WPA_DISCONNECTED:
1394                 /* carrier off */
1395                 connman_network_set_connected(task->network, FALSE);
1396                 connman_device_set_scanning(task->device, FALSE);
1397                 break;
1398         case WPA_ASSOCIATING:
1399                 connman_network_set_associating(task->network, TRUE);
1400                 break;
1401         default:
1402                 connman_network_set_associating(task->network, FALSE);
1403                 break;
1404         }
1405 }
1406
1407 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1408                                                 DBusMessage *msg, void *data)
1409 {
1410         struct supplicant_task *task;
1411         const char *member, *path;
1412
1413         if (dbus_message_has_interface(msg,
1414                                 SUPPLICANT_INTF ".Interface") == FALSE)
1415                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1416
1417         member = dbus_message_get_member(msg);
1418         if (member == NULL)
1419                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1420
1421         path = dbus_message_get_path(msg);
1422         if (path == NULL)
1423                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1424
1425         task = find_task_by_path(path);
1426         if (task == NULL)
1427                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1428
1429         DBG("task %p member %s", task, member);
1430
1431         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1432                 scan_results_available(task);
1433         else if (g_str_equal(member, "StateChange") == TRUE)
1434                 state_change(task, msg);
1435
1436         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1437 }
1438
1439 int supplicant_start(struct connman_device *device)
1440 {
1441         struct supplicant_task *task;
1442
1443         DBG("device %p", device);
1444
1445         task = g_try_new0(struct supplicant_task, 1);
1446         if (task == NULL)
1447                 return -ENOMEM;
1448
1449         task->ifindex = connman_device_get_index(device);
1450         task->ifname = inet_index2name(task->ifindex);
1451
1452         if (task->ifname == NULL) {
1453                 g_free(task);
1454                 return -ENOMEM;
1455         }
1456
1457         task->device = connman_device_ref(device);
1458
1459         task->created = FALSE;
1460         task->noscan = FALSE;
1461         task->state = WPA_INVALID;
1462
1463         task_list = g_slist_append(task_list, task);
1464
1465         return create_interface(task);
1466 }
1467
1468 int supplicant_stop(struct connman_device *device)
1469 {
1470         int index = connman_device_get_index(device);
1471         struct supplicant_task *task;
1472
1473         DBG("device %p", device);
1474
1475         task = find_task_by_index(index);
1476         if (task == NULL)
1477                 return -ENODEV;
1478
1479         task_list = g_slist_remove(task_list, task);
1480
1481         disable_network(task);
1482
1483         remove_network(task);
1484
1485         return remove_interface(task);
1486 }
1487
1488 int supplicant_scan(struct connman_device *device)
1489 {
1490         int index = connman_device_get_index(device);
1491         struct supplicant_task *task;
1492         int err;
1493
1494         DBG("device %p", device);
1495
1496         task = find_task_by_index(index);
1497         if (task == NULL)
1498                 return -ENODEV;
1499
1500         switch (task->state) {
1501         case WPA_SCANNING:
1502                 return -EALREADY;
1503         case WPA_ASSOCIATING:
1504         case WPA_ASSOCIATED:
1505         case WPA_4WAY_HANDSHAKE:
1506         case WPA_GROUP_HANDSHAKE:
1507                 return -EBUSY;
1508         default:
1509                 break;
1510         }
1511
1512         err = initiate_scan(task);
1513
1514         return 0;
1515 }
1516
1517 int supplicant_connect(struct connman_network *network)
1518 {
1519         struct supplicant_task *task;
1520         const char *address, *security, *passphrase;
1521         const void *ssid;
1522         unsigned int ssid_len;
1523         int index;
1524
1525         DBG("network %p", network);
1526
1527         address = connman_network_get_string(network, "Address");
1528         security = connman_network_get_string(network, "WiFi.Security");
1529         passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1530
1531         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1532
1533         DBG("address %s security %s passphrase %s",
1534                                         address, security, passphrase);
1535
1536         if (security == NULL && passphrase == NULL)
1537                 return -EINVAL;
1538
1539         if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1540                 return -EINVAL;
1541
1542         index = connman_network_get_index(network);
1543
1544         task = find_task_by_index(index);
1545         if (task == NULL)
1546                 return -ENODEV;
1547
1548         task->network = connman_network_ref(network);
1549
1550         add_network(task);
1551
1552         select_network(task);
1553         disable_network(task);
1554
1555         set_network(task, ssid, ssid_len, address, security, passphrase);
1556
1557         enable_network(task);
1558
1559         connman_network_set_associating(task->network, TRUE);
1560
1561         return 0;
1562 }
1563
1564 int supplicant_disconnect(struct connman_network *network)
1565 {
1566         struct supplicant_task *task;
1567         int index;
1568
1569         DBG("network %p", network);
1570
1571         index = connman_network_get_index(network);
1572
1573         task = find_task_by_index(index);
1574         if (task == NULL)
1575                 return -ENODEV;
1576
1577         disable_network(task);
1578
1579         remove_network(task);
1580
1581         connman_network_set_connected(task->network, FALSE);
1582
1583         connman_network_unref(task->network);
1584
1585         return 0;
1586 }
1587
1588 static void supplicant_activate(DBusConnection *conn)
1589 {
1590         DBusMessage *message;
1591
1592         DBG("conn %p", conn);
1593
1594         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1595                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1596         if (message == NULL)
1597                 return;
1598
1599         dbus_message_set_no_reply(message, TRUE);
1600
1601         dbus_connection_send(conn, message, NULL);
1602
1603         dbus_message_unref(message);
1604 }
1605
1606 static GSList *driver_list = NULL;
1607
1608 static void supplicant_probe(DBusConnection *conn, void *user_data)
1609 {
1610         GSList *list;
1611
1612         DBG("conn %p", conn);
1613
1614         for (list = driver_list; list; list = list->next) {
1615                 struct supplicant_driver *driver = list->data;
1616
1617                 DBG("driver %p name %s", driver, driver->name);
1618
1619                 if (driver->probe)
1620                         driver->probe();
1621         }
1622 }
1623
1624 static void supplicant_remove(DBusConnection *conn, void *user_data)
1625 {
1626         GSList *list;
1627
1628         DBG("conn %p", conn);
1629
1630         for (list = driver_list; list; list = list->next) {
1631                 struct supplicant_driver *driver = list->data;
1632
1633                 DBG("driver %p name %s", driver, driver->name);
1634
1635                 if (driver->remove)
1636                         driver->remove();
1637         }
1638 }
1639
1640 static const char *supplicant_rule = "type=signal,"
1641                                 "interface=" SUPPLICANT_INTF ".Interface";
1642 static guint watch;
1643
1644 static int supplicant_create(void)
1645 {
1646         if (g_slist_length(driver_list) > 0)
1647                 return 0;
1648
1649         connection = connman_dbus_get_connection();
1650         if (connection == NULL)
1651                 return -EIO;
1652
1653         DBG("connection %p", connection);
1654
1655         if (dbus_connection_add_filter(connection,
1656                                 supplicant_filter, NULL, NULL) == FALSE) {
1657                 connection = connman_dbus_get_connection();
1658                 return -EIO;
1659         }
1660
1661         dbus_bus_add_match(connection, supplicant_rule, NULL);
1662         dbus_connection_flush(connection);
1663
1664         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1665                         supplicant_probe, supplicant_remove, NULL, NULL);
1666
1667         return 0;
1668 }
1669
1670 static void supplicant_destroy(void)
1671 {
1672         if (g_slist_length(driver_list) > 0)
1673                 return;
1674
1675         DBG("connection %p", connection);
1676
1677         if (watch > 0)
1678                 g_dbus_remove_watch(connection, watch);
1679
1680         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1681         dbus_connection_flush(connection);
1682
1683         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1684
1685         dbus_connection_unref(connection);
1686         connection = NULL;
1687 }
1688
1689 int supplicant_register(struct supplicant_driver *driver)
1690 {
1691         int err;
1692
1693         DBG("driver %p name %s", driver, driver->name);
1694
1695         err = supplicant_create();
1696         if (err < 0)
1697                 return err;
1698
1699         driver_list = g_slist_append(driver_list, driver);
1700
1701         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1702                 supplicant_probe(connection, NULL);
1703         else
1704                 supplicant_activate(connection);
1705
1706         return 0;
1707 }
1708
1709 void supplicant_unregister(struct supplicant_driver *driver)
1710 {
1711         DBG("driver %p name %s", driver, driver->name);
1712
1713         supplicant_remove(connection, NULL);
1714
1715         driver_list = g_slist_remove(driver_list, driver);
1716
1717         supplicant_destroy();
1718 }