Use global supplicant filter matching rule
[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 <stdlib.h>
27 #include <string.h>
28
29 #include <gdbus.h>
30
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/log.h>
33 #include <connman/dbus.h>
34
35 #include "inet.h"
36 #include "supplicant.h"
37
38 #define TIMEOUT 5000
39
40 #define IEEE80211_CAP_ESS       0x0001
41 #define IEEE80211_CAP_IBSS      0x0002
42 #define IEEE80211_CAP_PRIVACY   0x0010
43
44 static GSList *driver_list = NULL;
45
46 struct supplicant_task {
47         int ifindex;
48         gchar *ifname;
49         struct connman_device *device;
50         struct supplicant_callback *callback;
51         gchar *path;
52         gboolean created;
53         gchar *network;
54         enum supplicant_state state;
55 };
56
57 static GSList *task_list = NULL;
58
59 static DBusConnection *connection;
60
61 static struct supplicant_task *find_task_by_index(int index)
62 {
63         GSList *list;
64
65         for (list = task_list; list; list = list->next) {
66                 struct supplicant_task *task = list->data;
67
68                 if (task->ifindex == index)
69                         return task;
70         }
71
72         return NULL;
73 }
74
75 static struct supplicant_task *find_task_by_path(const char *path)
76 {
77         GSList *list;
78
79         for (list = task_list; list; list = list->next) {
80                 struct supplicant_task *task = list->data;
81
82                 if (g_str_equal(task->path, path) == TRUE)
83                         return task;
84         }
85
86         return NULL;
87 }
88
89 static int get_interface(struct supplicant_task *task)
90 {
91         DBusMessage *message, *reply;
92         DBusError error;
93         const char *path;
94
95         DBG("task %p", task);
96
97         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
98                                         SUPPLICANT_INTF, "getInterface");
99         if (message == NULL)
100                 return -ENOMEM;
101
102         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
103                                                         DBUS_TYPE_INVALID);
104
105         dbus_error_init(&error);
106
107         reply = dbus_connection_send_with_reply_and_block(connection,
108                                                         message, -1, &error);
109         if (reply == NULL) {
110                 if (dbus_error_is_set(&error) == TRUE) {
111                         connman_error("%s", error.message);
112                         dbus_error_free(&error);
113                 } else
114                         connman_error("Failed to get interface");
115                 dbus_message_unref(message);
116                 return -EIO;
117         }
118
119         dbus_message_unref(message);
120
121         dbus_error_init(&error);
122
123         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
124                                                 DBUS_TYPE_INVALID) == FALSE) {
125                 if (dbus_error_is_set(&error) == TRUE) {
126                         connman_error("%s", error.message);
127                         dbus_error_free(&error);
128                 } else
129                         connman_error("Wrong arguments for interface");
130                 dbus_message_unref(reply);
131                 return -EIO;
132         }
133
134         DBG("path %s", path);
135
136         task->path = g_strdup(path);
137         task->created = FALSE;
138
139         dbus_message_unref(reply);
140
141         return 0;
142 }
143
144 static int add_interface(struct supplicant_task *task)
145 {
146         DBusMessage *message, *reply;
147         DBusError error;
148         const char *path;
149
150         DBG("task %p", task);
151
152         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
153                                         SUPPLICANT_INTF, "addInterface");
154         if (message == NULL)
155                 return -ENOMEM;
156
157         dbus_error_init(&error);
158
159         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
160                                                         DBUS_TYPE_INVALID);
161
162         reply = dbus_connection_send_with_reply_and_block(connection,
163                                                         message, -1, &error);
164         if (reply == NULL) {
165                 if (dbus_error_is_set(&error) == TRUE) {
166                         connman_error("%s", error.message);
167                         dbus_error_free(&error);
168                 } else
169                         connman_error("Failed to add interface");
170                 dbus_message_unref(message);
171                 return -EIO;
172         }
173
174         dbus_message_unref(message);
175
176         dbus_error_init(&error);
177
178         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
179                                                 DBUS_TYPE_INVALID) == FALSE) {
180                 if (dbus_error_is_set(&error) == TRUE) {
181                         connman_error("%s", error.message);
182                         dbus_error_free(&error);
183                 } else
184                         connman_error("Wrong arguments for interface");
185                 dbus_message_unref(reply);
186                 return -EIO;
187         }
188
189         DBG("path %s", path);
190
191         task->path = g_strdup(path);
192         task->created = TRUE;
193
194         dbus_message_unref(reply);
195
196         return 0;
197 }
198
199 static int remove_interface(struct supplicant_task *task)
200 {
201         DBusMessage *message, *reply;
202         DBusError error;
203
204         DBG("task %p", task);
205
206         if (task->created == FALSE)
207                 return -EINVAL;
208
209         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
210                                         SUPPLICANT_INTF, "removeInterface");
211         if (message == NULL)
212                 return -ENOMEM;
213
214         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
215                                                         DBUS_TYPE_INVALID);
216
217         dbus_error_init(&error);
218
219         reply = dbus_connection_send_with_reply_and_block(connection,
220                                                         message, -1, &error);
221         if (reply == NULL) {
222                 if (dbus_error_is_set(&error) == TRUE) {
223                         connman_error("%s", error.message);
224                         dbus_error_free(&error);
225                 } else
226                         connman_error("Failed to remove interface");
227                 dbus_message_unref(message);
228                 return -EIO;
229         }
230
231         dbus_message_unref(message);
232
233         dbus_message_unref(reply);
234
235         return 0;
236 }
237
238 static int set_ap_scan(struct supplicant_task *task)
239 {
240         DBusMessage *message, *reply;
241         DBusError error;
242         guint32 ap_scan = 1;
243
244         DBG("task %p", task);
245
246         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
247                                 SUPPLICANT_INTF ".Interface", "setAPScan");
248         if (message == NULL)
249                 return -ENOMEM;
250
251         dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
252                                                         DBUS_TYPE_INVALID);
253
254         dbus_error_init(&error);
255
256         reply = dbus_connection_send_with_reply_and_block(connection,
257                                                         message, -1, &error);
258         if (reply == NULL) {
259                 if (dbus_error_is_set(&error) == TRUE) {
260                         connman_error("%s", error.message);
261                         dbus_error_free(&error);
262                 } else
263                         connman_error("Failed to set AP scan");
264                 dbus_message_unref(message);
265                 return -EIO;
266         }
267
268         dbus_message_unref(message);
269
270         dbus_message_unref(reply);
271
272         return 0;
273 }
274
275 static int add_network(struct supplicant_task *task)
276 {
277         DBusMessage *message, *reply;
278         DBusError error;
279         const char *path;
280
281         DBG("task %p", task);
282
283         if (task->network != NULL)
284                 return -EALREADY;
285
286         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
287                                 SUPPLICANT_INTF ".Interface", "addNetwork");
288         if (message == NULL)
289                 return -ENOMEM;
290
291         dbus_error_init(&error);
292
293         reply = dbus_connection_send_with_reply_and_block(connection,
294                                                         message, -1, &error);
295         if (reply == NULL) {
296                 if (dbus_error_is_set(&error) == TRUE) {
297                         connman_error("%s", error.message);
298                         dbus_error_free(&error);
299                 } else
300                         connman_error("Failed to add network");
301                 dbus_message_unref(message);
302                 return -EIO;
303         }
304
305         dbus_message_unref(message);
306
307         dbus_error_init(&error);
308
309         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
310                                                 DBUS_TYPE_INVALID) == FALSE) {
311                 if (dbus_error_is_set(&error) == TRUE) {
312                         connman_error("%s", error.message);
313                         dbus_error_free(&error);
314                 } else
315                         connman_error("Wrong arguments for network");
316                 dbus_message_unref(reply);
317                 return -EIO;
318         }
319
320         DBG("path %s", path);
321
322         task->network = g_strdup(path);
323
324         dbus_message_unref(reply);
325
326         return 0;
327 }
328
329 static int remove_network(struct supplicant_task *task)
330 {
331         DBusMessage *message, *reply;
332         DBusError error;
333
334         DBG("task %p", task);
335
336         if (task->network == NULL)
337                 return -EINVAL;
338
339         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
340                                 SUPPLICANT_INTF ".Interface", "removeNetwork");
341         if (message == NULL)
342                 return -ENOMEM;
343
344         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->network,
345                                                         DBUS_TYPE_INVALID);
346
347         dbus_error_init(&error);
348
349         reply = dbus_connection_send_with_reply_and_block(connection,
350                                                         message, -1, &error);
351         if (reply == NULL) {
352                 if (dbus_error_is_set(&error) == TRUE) {
353                         connman_error("%s", error.message);
354                         dbus_error_free(&error);
355                 } else
356                         connman_error("Failed to remove network");
357                 dbus_message_unref(message);
358                 return -EIO;
359         }
360
361         dbus_message_unref(message);
362
363         dbus_message_unref(reply);
364
365         g_free(task->network);
366         task->network = NULL;
367
368         return 0;
369 }
370
371 static int select_network(struct supplicant_task *task)
372 {
373         DBusMessage *message, *reply;
374         DBusError error;
375
376         DBG("task %p", task);
377
378         if (task->network == NULL)
379                 return -EINVAL;
380
381         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
382                                 SUPPLICANT_INTF ".Interface", "selectNetwork");
383         if (message == NULL)
384                 return -ENOMEM;
385
386         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->network,
387                                                         DBUS_TYPE_INVALID);
388
389         dbus_error_init(&error);
390
391         reply = dbus_connection_send_with_reply_and_block(connection,
392                                                         message, -1, &error);
393         if (reply == NULL) {
394                 if (dbus_error_is_set(&error) == TRUE) {
395                         connman_error("%s", error.message);
396                         dbus_error_free(&error);
397                 } else
398                         connman_error("Failed to select network");
399                 dbus_message_unref(message);
400                 return -EIO;
401         }
402
403         dbus_message_unref(message);
404
405         dbus_message_unref(reply);
406
407         return 0;
408 }
409
410 static int enable_network(struct supplicant_task *task)
411 {
412         DBusMessage *message, *reply;
413         DBusError error;
414
415         DBG("task %p", task);
416
417         if (task->network == NULL)
418                 return -EINVAL;
419
420         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
421                                         SUPPLICANT_INTF ".Network", "enable");
422         if (message == NULL)
423                 return -ENOMEM;
424
425         dbus_error_init(&error);
426
427         reply = dbus_connection_send_with_reply_and_block(connection,
428                                                         message, -1, &error);
429         if (reply == NULL) {
430                 if (dbus_error_is_set(&error) == TRUE) {
431                         connman_error("%s", error.message);
432                         dbus_error_free(&error);
433                 } else
434                         connman_error("Failed to enable network");
435                 dbus_message_unref(message);
436                 return -EIO;
437         }
438
439         dbus_message_unref(message);
440
441         dbus_message_unref(reply);
442
443         return 0;
444 }
445
446 static int disable_network(struct supplicant_task *task)
447 {
448         DBusMessage *message, *reply;
449         DBusError error;
450
451         DBG("task %p", task);
452
453         if (task->network == NULL)
454                 return -EINVAL;
455
456         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
457                                         SUPPLICANT_INTF ".Network", "disable");
458         if (message == NULL)
459                 return -ENOMEM;
460
461         dbus_error_init(&error);
462
463         reply = dbus_connection_send_with_reply_and_block(connection,
464                                                         message, -1, &error);
465         if (reply == NULL) {
466                 if (dbus_error_is_set(&error) == TRUE) {
467                         connman_error("%s", error.message);
468                         dbus_error_free(&error);
469                 } else
470                         connman_error("Failed to disable network");
471                 dbus_message_unref(message);
472                 return -EIO;
473         }
474
475         dbus_message_unref(message);
476
477         dbus_message_unref(reply);
478
479         return 0;
480 }
481
482 static int set_network(struct supplicant_task *task,
483                                 const unsigned char *network, int len,
484                                 const char *security, const char *passphrase)
485 {
486         DBusMessage *message, *reply;
487         DBusMessageIter array, dict;
488         DBusError error;
489
490         DBG("task %p", task);
491
492         if (task->network == NULL)
493                 return -EINVAL;
494
495         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
496                                         SUPPLICANT_INTF ".Network", "set");
497         if (message == NULL)
498                 return -ENOMEM;
499
500         dbus_message_iter_init_append(message, &array);
501
502         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
503                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
504                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
505                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
506
507         connman_dbus_dict_append_array(&dict, "ssid",
508                                         DBUS_TYPE_BYTE, &network, len);
509
510         if (g_ascii_strcasecmp(security, "wpa") == 0 ||
511                                 g_ascii_strcasecmp(security, "wpa2") == 0) {
512                 const char *key_mgmt = "WPA-PSK";
513                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
514                                                 DBUS_TYPE_STRING, &key_mgmt);
515
516                 if (passphrase && strlen(passphrase) > 0)
517                         connman_dbus_dict_append_variant(&dict, "psk",
518                                                 DBUS_TYPE_STRING, &passphrase);
519         } else if (g_ascii_strcasecmp(security, "wep") == 0) {
520                 const char *key_mgmt = "NONE", *index = "0";
521                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
522                                                 DBUS_TYPE_STRING, &key_mgmt);
523
524                 if (passphrase) {
525                         int size = strlen(passphrase);
526                         if (size == 10 || size == 26) {
527                                 unsigned char *key = malloc(13);
528                                 char tmp[3];
529                                 int i;
530                                 memset(tmp, 0, sizeof(tmp));
531                                 if (key == NULL)
532                                         size = 0;
533                                 for (i = 0; i < size / 2; i++) {
534                                         memcpy(tmp, passphrase + (i * 2), 2);
535                                         key[i] = (unsigned char) strtol(tmp,
536                                                                 NULL, 16);
537                                 }
538                                 connman_dbus_dict_append_array(&dict,
539                                                 "wep_key0", DBUS_TYPE_BYTE,
540                                                         &key, size / 2);
541                                 free(key);
542                         } else
543                                 connman_dbus_dict_append_variant(&dict,
544                                                 "wep_key0", DBUS_TYPE_STRING,
545                                                                 &passphrase);
546                         connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
547                                                 DBUS_TYPE_STRING, &index);
548                 }
549         } else {
550                 const char *key_mgmt = "NONE";
551                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
552                                                 DBUS_TYPE_STRING, &key_mgmt);
553         }
554
555         dbus_message_iter_close_container(&array, &dict);
556
557         dbus_error_init(&error);
558
559         reply = dbus_connection_send_with_reply_and_block(connection,
560                                                         message, -1, &error);
561         if (reply == NULL) {
562                 if (dbus_error_is_set(&error) == TRUE) {
563                         connman_error("%s", error.message);
564                         dbus_error_free(&error);
565                 } else
566                         connman_error("Failed to set network options");
567                 dbus_message_unref(message);
568                 return -EIO;
569         }
570
571         dbus_message_unref(message);
572
573         dbus_message_unref(reply);
574
575         return 0;
576 }
577
578 static int initiate_scan(struct supplicant_task *task)
579 {
580         DBusMessage *message;
581         DBusPendingCall *call;
582
583         DBG("task %p", task);
584
585         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
586                                         SUPPLICANT_INTF ".Interface", "scan");
587         if (message == NULL)
588                 return -ENOMEM;
589
590         if (dbus_connection_send_with_reply(connection, message,
591                                                 &call, TIMEOUT) == FALSE) {
592                 connman_error("Failed to initiate scan");
593                 dbus_message_unref(message);
594                 return -EIO;
595         }
596
597         dbus_message_unref(message);
598
599         return 0;
600 }
601
602 static void extract_ssid(struct supplicant_network *network,
603                                                 DBusMessageIter *value)
604 {
605         DBusMessageIter array;
606         unsigned char *ssid;
607         int ssid_len;
608
609         dbus_message_iter_recurse(value, &array);
610         dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
611
612         if (ssid_len < 1)
613                 return;
614
615         network->ssid = g_try_malloc(ssid_len);
616         if (network->ssid == NULL)
617                 return;
618
619         memcpy(network->ssid, ssid, ssid_len);
620         network->ssid_len = ssid_len;
621
622         network->identifier = g_try_malloc0(ssid_len + 1);
623         if (network->identifier == NULL)
624                 return;
625
626         memcpy(network->identifier, ssid, ssid_len);
627 }
628
629 static void extract_wpaie(struct supplicant_network *network,
630                                                 DBusMessageIter *value)
631 {
632         DBusMessageIter array;
633         unsigned char *ie;
634         int ie_len;
635
636         dbus_message_iter_recurse(value, &array);
637         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
638
639         if (ie_len > 0)
640                 network->has_wpa = TRUE;
641 }
642
643 static void extract_rsnie(struct supplicant_network *network,
644                                                 DBusMessageIter *value)
645 {
646         DBusMessageIter array;
647         unsigned char *ie;
648         int ie_len;
649
650         dbus_message_iter_recurse(value, &array);
651         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
652
653         if (ie_len > 0)
654                 network->has_rsn = TRUE;
655 }
656
657 static void extract_capabilites(struct supplicant_network *network,
658                                                 DBusMessageIter *value)
659 {
660         dbus_message_iter_get_basic(value, &network->capabilities);
661
662         if (network->capabilities & IEEE80211_CAP_ESS)
663                 network->adhoc = FALSE;
664         else if (network->capabilities & IEEE80211_CAP_IBSS)
665                 network->adhoc = TRUE;
666
667         if (network->capabilities & IEEE80211_CAP_PRIVACY)
668                 network->has_wep = TRUE;
669 }
670
671 static void properties_reply(DBusPendingCall *call, void *user_data)
672 {
673         struct supplicant_task *task = user_data;
674         struct supplicant_network *network;
675         DBusMessage *reply;
676         DBusMessageIter array, dict;
677
678         DBG("task %p", task);
679
680         reply = dbus_pending_call_steal_reply(call);
681
682         network = g_try_new0(struct supplicant_network, 1);
683         if (network == NULL)
684                 goto done;
685
686         dbus_message_iter_init(reply, &array);
687
688         dbus_message_iter_recurse(&array, &dict);
689
690         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
691                 DBusMessageIter entry, value;
692                 const char *key;
693
694                 dbus_message_iter_recurse(&dict, &entry);
695                 dbus_message_iter_get_basic(&entry, &key);
696
697                 dbus_message_iter_next(&entry);
698
699                 dbus_message_iter_recurse(&entry, &value);
700
701                 //type = dbus_message_iter_get_arg_type(&value);
702                 //dbus_message_iter_get_basic(&value, &val);
703
704                 /* 
705                  * bssid        : a (97)
706                  * ssid         : a (97)
707                  * wpaie        : a (97)
708                  * rsnie        : a (97)
709                  * frequency    : i (105)
710                  * capabilities : q (113)
711                  * quality      : i (105)
712                  * noise        : i (105)
713                  * level        : i (105)
714                  * maxrate      : i (105)
715                  */
716
717                 if (g_str_equal(key, "ssid") == TRUE)
718                         extract_ssid(network, &value);
719                 else if (g_str_equal(key, "wpaie") == TRUE)
720                         extract_wpaie(network, &value);
721                 else if (g_str_equal(key, "rsnie") == TRUE)
722                         extract_rsnie(network, &value);
723                 else if (g_str_equal(key, "capabilities") == TRUE)
724                         extract_capabilites(network, &value);
725                 else if (g_str_equal(key, "quality") == TRUE)
726                         dbus_message_iter_get_basic(&value, &network->quality);
727                 else if (g_str_equal(key, "noise") == TRUE)
728                         dbus_message_iter_get_basic(&value, &network->noise);
729                 else if (g_str_equal(key, "level") == TRUE)
730                         dbus_message_iter_get_basic(&value, &network->level);
731                 else if (g_str_equal(key, "maxrate") == TRUE)
732                         dbus_message_iter_get_basic(&value, &network->maxrate);
733
734
735                 dbus_message_iter_next(&dict);
736         }
737
738         if (task->callback && task->callback->scan_result)
739                 task->callback->scan_result(task->device, network);
740
741         g_free(network->identifier);
742         g_free(network->ssid);
743         g_free(network);
744
745 done:
746         dbus_message_unref(reply);
747 }
748
749 static int get_network_properties(struct supplicant_task *task,
750                                                         const char *path)
751 {
752         DBusMessage *message;
753         DBusPendingCall *call;
754
755         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
756                                                 SUPPLICANT_INTF ".BSSID",
757                                                                 "properties");
758         if (message == NULL)
759                 return -ENOMEM;
760
761         if (dbus_connection_send_with_reply(connection, message,
762                                                 &call, TIMEOUT) == FALSE) {
763                 connman_error("Failed to get network properties");
764                 dbus_message_unref(message);
765                 return -EIO;
766         }
767
768         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
769
770         dbus_message_unref(message);
771
772         return 0;
773 }
774
775 static void scan_results_reply(DBusPendingCall *call, void *user_data)
776 {
777         struct supplicant_task *task = user_data;
778         DBusMessage *reply;
779         DBusError error;
780         char **results;
781         int i, num_results;
782
783         DBG("task %p", task);
784
785         reply = dbus_pending_call_steal_reply(call);
786
787         dbus_error_init(&error);
788
789         if (dbus_message_get_args(reply, &error,
790                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
791                                                 &results, &num_results,
792                                                 DBUS_TYPE_INVALID) == FALSE) {
793                 if (dbus_error_is_set(&error) == TRUE) {
794                         connman_error("%s", error.message);
795                         dbus_error_free(&error);
796                 } else
797                         connman_error("Wrong arguments for scan result");
798                 goto done;
799         }
800
801         if (task->callback && task->callback->clear_results)
802                         task->callback->clear_results(task->device);
803
804         for (i = 0; i < num_results; i++)
805                 get_network_properties(task, results[i]);
806
807         g_strfreev(results);
808
809 done:
810         dbus_message_unref(reply);
811 }
812
813 static int scan_results_available(struct supplicant_task *task)
814 {
815         DBusMessage *message;
816         DBusPendingCall *call;
817
818         DBG("task %p", task);
819
820         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
821                                                 SUPPLICANT_INTF ".Interface",
822                                                         "scanResults");
823         if (message == NULL)
824                 return -ENOMEM;
825
826         if (dbus_connection_send_with_reply(connection, message,
827                                                 &call, TIMEOUT) == FALSE) {
828                 connman_error("Failed to request scan result");
829                 dbus_message_unref(message);
830                 return -EIO;
831         }
832
833         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
834
835         dbus_message_unref(message);
836
837         return 0;
838 }
839
840 static void state_change(struct supplicant_task *task, DBusMessage *msg)
841 {
842         DBusError error;
843         const char *state, *previous;
844
845         dbus_error_init(&error);
846
847         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &state,
848                                                 DBUS_TYPE_STRING, &previous,
849                                                 DBUS_TYPE_INVALID) == FALSE) {
850                 if (dbus_error_is_set(&error) == TRUE) {
851                         connman_error("%s", error.message);
852                         dbus_error_free(&error);
853                 } else
854                         connman_error("Wrong arguments for state change");
855                 return;
856         }
857
858         DBG("state %s ==> %s", previous, state);
859
860         if (g_str_equal(state, "INACTIVE") == TRUE)
861                 task->state = STATE_INACTIVE;
862         else if (g_str_equal(state, "SCANNING") == TRUE)
863                 task->state = STATE_SCANNING;
864         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
865                 task->state = STATE_ASSOCIATING;
866         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
867                 task->state = STATE_ASSOCIATED;
868         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
869                 task->state = STATE_4WAY_HANDSHAKE;
870         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
871                 task->state = STATE_4WAY_HANDSHAKE;
872         else if (g_str_equal(state, "COMPLETED") == TRUE)
873                 task->state = STATE_COMPLETED;
874         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
875                 task->state = STATE_DISCONNECTED;
876
877         if (task->callback && task->callback->state_change)
878                 task->callback->state_change(task->device, task->state);
879
880         switch (task->state) {
881         case STATE_COMPLETED:
882                 /* carrier on */
883                 break;
884         case STATE_DISCONNECTED:
885                 /* carrier off */
886                 break;
887         default:
888                 break;
889         }
890 }
891
892 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
893                                                 DBusMessage *msg, void *data)
894 {
895         struct supplicant_task *task;
896         const char *member, *path;
897
898         if (dbus_message_has_interface(msg,
899                                 SUPPLICANT_INTF ".Interface") == FALSE)
900                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
901
902         member = dbus_message_get_member(msg);
903         if (member == NULL)
904                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
905
906         path = dbus_message_get_path(msg);
907         if (path == NULL)
908                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
909
910         task = find_task_by_path(path);
911         if (task == NULL)
912                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
913
914         DBG("task %p member %s", task, member);
915
916         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
917                 scan_results_available(task);
918         else if (g_str_equal(member, "StateChange") == TRUE)
919                 state_change(task, msg);
920
921         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
922 }
923
924 int __supplicant_start(struct connman_device *device,
925                                         struct supplicant_callback *callback)
926 {
927         struct supplicant_task *task;
928         int err;
929
930         DBG("device %p", device);
931
932         task = g_try_new0(struct supplicant_task, 1);
933         if (task == NULL)
934                 return -ENOMEM;
935
936         task->ifindex = connman_device_get_index(device);
937         task->ifname = inet_index2name(task->ifindex);
938         task->device = device;
939         task->callback = callback;
940
941         if (task->ifname == NULL) {
942                 g_free(task);
943                 return -ENOMEM;
944         }
945
946         task->created = FALSE;
947         task->state = STATE_INACTIVE;
948
949         task_list = g_slist_append(task_list, task);
950
951         err = get_interface(task);
952         if (err < 0) {
953                 err = add_interface(task);
954                 if (err < 0) {
955                         g_free(task);
956                         return err;
957                 }
958         }
959
960         set_ap_scan(task);
961
962         return 0;
963 }
964
965 int __supplicant_stop(struct connman_device *device)
966 {
967         int index = connman_device_get_index(device);
968         struct supplicant_task *task;
969
970         DBG("device %p", device);
971
972         task = find_task_by_index(index);
973         if (task == NULL)
974                 return -ENODEV;
975
976         task_list = g_slist_remove(task_list, task);
977
978         disable_network(task);
979
980         remove_network(task);
981
982         remove_interface(task);
983
984         g_free(task->ifname);
985         g_free(task->path);
986         g_free(task);
987
988         return 0;
989 }
990
991 int __supplicant_scan(struct connman_device *device)
992 {
993         int index = connman_device_get_index(device);
994         struct supplicant_task *task;
995         int err;
996
997         DBG("device %p", device);
998
999         task = find_task_by_index(index);
1000         if (task == NULL)
1001                 return -ENODEV;
1002
1003         switch (task->state) {
1004         case STATE_SCANNING:
1005                 return -EALREADY;
1006         case STATE_ASSOCIATING:
1007         case STATE_ASSOCIATED:
1008         case STATE_4WAY_HANDSHAKE:
1009         case STATE_GROUP_HANDSHAKE:
1010                 return -EBUSY;
1011         default:
1012                 break;
1013         }
1014
1015         err = initiate_scan(task);
1016
1017         return 0;
1018 }
1019
1020 int __supplicant_connect(struct connman_element *element,
1021                                 const unsigned char *ssid, int ssid_len,
1022                                 const char *security, const char *passphrase)
1023 {
1024         struct supplicant_task *task;
1025
1026         DBG("element %p", element);
1027
1028         task = find_task_by_index(element->index);
1029         if (task == NULL)
1030                 return -ENODEV;
1031
1032         add_network(task);
1033
1034         select_network(task);
1035         disable_network(task);
1036
1037         set_network(task, ssid, ssid_len, security, passphrase);
1038
1039         enable_network(task);
1040
1041         return 0;
1042 }
1043
1044 int __supplicant_disconnect(struct connman_element *element)
1045 {
1046         struct supplicant_task *task;
1047
1048         DBG("element %p", element);
1049
1050         task = find_task_by_index(element->index);
1051         if (task == NULL)
1052                 return -ENODEV;
1053
1054         disable_network(task);
1055
1056         remove_network(task);
1057
1058         return 0;
1059 }
1060
1061 static void supplicant_activate(DBusConnection *conn)
1062 {
1063         DBusMessage *message;
1064
1065         DBG("conn %p", conn);
1066
1067         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1068                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1069         if (message == NULL)
1070                 return;
1071
1072         dbus_message_set_no_reply(message, TRUE);
1073
1074         dbus_connection_send(conn, message, NULL);
1075
1076         dbus_message_unref(message);
1077 }
1078
1079 static void supplicant_probe(DBusConnection *conn, void *user_data)
1080 {
1081         GSList *list;
1082
1083         DBG("conn %p", conn);
1084
1085         for (list = driver_list; list; list = list->next) {
1086                 struct supplicant_driver *driver = list->data;
1087
1088                 DBG("driver %p name %s", driver, driver->name);
1089
1090                 if (driver->probe)
1091                         driver->probe();
1092         }
1093 }
1094
1095 static void supplicant_remove(DBusConnection *conn, void *user_data)
1096 {
1097         GSList *list;
1098
1099         DBG("conn %p", conn);
1100
1101         for (list = driver_list; list; list = list->next) {
1102                 struct supplicant_driver *driver = list->data;
1103
1104                 DBG("driver %p name %s", driver, driver->name);
1105
1106                 if (driver->remove)
1107                         driver->remove();
1108         }
1109 }
1110
1111 static const char *supplicant_rule = "type=signal,"
1112                                 "interface=" SUPPLICANT_INTF ".Interface";
1113 static guint watch;
1114
1115 static int supplicant_create(void)
1116 {
1117         if (g_slist_length(driver_list) > 0)
1118                 return 0;
1119
1120         connection = connman_dbus_get_connection();
1121         if (connection == NULL)
1122                 return -EIO;
1123
1124         DBG("connection %p", connection);
1125
1126         if (dbus_connection_add_filter(connection,
1127                                 supplicant_filter, NULL, NULL) == FALSE) {
1128                 connection = connman_dbus_get_connection();
1129                 return -EIO;
1130         }
1131
1132         dbus_bus_add_match(connection, supplicant_rule, NULL);
1133         dbus_connection_flush(connection);
1134
1135         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1136                         supplicant_probe, supplicant_remove, NULL, NULL);
1137
1138         return 0;
1139 }
1140
1141 static void supplicant_destroy(void)
1142 {
1143         if (g_slist_length(driver_list) > 0)
1144                 return;
1145
1146         DBG("connection %p", connection);
1147
1148         if (watch > 0)
1149                 g_dbus_remove_watch(connection, watch);
1150
1151         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1152         dbus_connection_flush(connection);
1153
1154         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1155
1156         dbus_connection_unref(connection);
1157         connection = NULL;
1158 }
1159
1160 int supplicant_register(struct supplicant_driver *driver)
1161 {
1162         int err;
1163
1164         DBG("driver %p name %s", driver, driver->name);
1165
1166         err = supplicant_create();
1167         if (err < 0)
1168                 return err;
1169
1170         driver_list = g_slist_append(driver_list, driver);
1171
1172         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1173                 supplicant_probe(connection, NULL);
1174         else
1175                 supplicant_activate(connection);
1176
1177         return 0;
1178 }
1179
1180 void supplicant_unregister(struct supplicant_driver *driver)
1181 {
1182         DBG("driver %p name %s", driver, driver->name);
1183
1184         supplicant_remove(connection, NULL);
1185
1186         driver_list = g_slist_remove(driver_list, driver);
1187
1188         supplicant_destroy();
1189 }