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