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