Assign supplicant filter only once
[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
31 #include "inet.h"
32 #include "supplicant.h"
33
34 #define TIMEOUT 5000
35
36 #define IEEE80211_CAP_ESS       0x0001
37 #define IEEE80211_CAP_IBSS      0x0002
38 #define IEEE80211_CAP_PRIVACY   0x0010
39
40 struct supplicant_task {
41         int ifindex;
42         gchar *ifname;
43         struct connman_element *element;
44         struct supplicant_callback *callback;
45         gchar *path;
46         gboolean created;
47         gchar *network;
48         enum supplicant_state state;
49 };
50
51 static GStaticMutex task_mutex = G_STATIC_MUTEX_INIT;
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 void append_entry(DBusMessageIter *dict,
478                                 const char *key, int type, void *val)
479 {
480         DBusMessageIter entry, value;
481         const char *signature;
482
483         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
484                                                                 NULL, &entry);
485
486         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
487
488         switch (type) {
489         case DBUS_TYPE_STRING:
490                 signature = DBUS_TYPE_STRING_AS_STRING;
491                 break;
492         case DBUS_TYPE_UINT16:
493                 signature = DBUS_TYPE_UINT16_AS_STRING;
494                 break;
495         default:
496                 signature = DBUS_TYPE_VARIANT_AS_STRING;
497                 break;
498         }
499
500         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
501                                                         signature, &value);
502         dbus_message_iter_append_basic(&value, type, val);
503         dbus_message_iter_close_container(&entry, &value);
504
505         dbus_message_iter_close_container(dict, &entry);
506 }
507
508 static int set_network(struct supplicant_task *task, const char *network,
509                                                 const char *passphrase)
510 {
511         DBusMessage *message, *reply;
512         DBusMessageIter array, dict;
513         DBusError error;
514
515         DBG("task %p", task);
516
517         if (task->network == NULL)
518                 return -EINVAL;
519
520         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
521                                         SUPPLICANT_INTF ".Network", "set");
522         if (message == NULL)
523                 return -ENOMEM;
524
525         dbus_message_iter_init_append(message, &array);
526
527         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
528                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
529                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
530                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
531
532         append_entry(&dict, "ssid", DBUS_TYPE_STRING, &network);
533
534         if (passphrase && strlen(passphrase) > 0) {
535                 const char *key_mgmt = "WPA-PSK";
536                 append_entry(&dict, "key_mgmt", DBUS_TYPE_STRING, &key_mgmt);
537                 append_entry(&dict, "psk", DBUS_TYPE_STRING, &passphrase);
538         } else {
539                 const char *key_mgmt = "NONE";
540                 append_entry(&dict, "key_mgmt", DBUS_TYPE_STRING, &key_mgmt);
541         }
542
543         dbus_message_iter_close_container(&array, &dict);
544
545         dbus_error_init(&error);
546
547         reply = dbus_connection_send_with_reply_and_block(connection,
548                                                         message, -1, &error);
549         if (reply == NULL) {
550                 if (dbus_error_is_set(&error) == TRUE) {
551                         connman_error("%s", error.message);
552                         dbus_error_free(&error);
553                 } else
554                         connman_error("Failed to set network options");
555                 dbus_message_unref(message);
556                 return -EIO;
557         }
558
559         dbus_message_unref(message);
560
561         dbus_message_unref(reply);
562
563         return 0;
564 }
565
566 static int initiate_scan(struct supplicant_task *task)
567 {
568         DBusMessage *message;
569         DBusPendingCall *call;
570
571         DBG("task %p", task);
572
573         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
574                                         SUPPLICANT_INTF ".Interface", "scan");
575         if (message == NULL)
576                 return -ENOMEM;
577
578         if (dbus_connection_send_with_reply(connection, message,
579                                                 &call, TIMEOUT) == FALSE) {
580                 connman_error("Failed to initiate scan");
581                 dbus_message_unref(message);
582                 return -EIO;
583         }
584
585         dbus_message_unref(message);
586
587         return 0;
588 }
589
590 static void extract_ssid(struct supplicant_network *network,
591                                                 DBusMessageIter *value)
592 {
593         DBusMessageIter array;
594         unsigned char *ssid;
595         int ssid_len;
596
597         dbus_message_iter_recurse(value, &array);
598         dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
599
600         network->identifier = g_strdup((char *) ssid);
601 }
602
603 static void extract_wpaie(struct supplicant_network *network,
604                                                 DBusMessageIter *value)
605 {
606         DBusMessageIter array;
607         unsigned char *ie;
608         int ie_len;
609
610         dbus_message_iter_recurse(value, &array);
611         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
612
613         if (ie_len > 0)
614                 network->has_wpa = TRUE;
615 }
616
617 static void extract_rsnie(struct supplicant_network *network,
618                                                 DBusMessageIter *value)
619 {
620         DBusMessageIter array;
621         unsigned char *ie;
622         int ie_len;
623
624         dbus_message_iter_recurse(value, &array);
625         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
626
627         if (ie_len > 0)
628                 network->has_rsn = TRUE;
629 }
630
631 static void extract_capabilites(struct supplicant_network *network,
632                                                 DBusMessageIter *value)
633 {
634         guint capabilities;
635
636         dbus_message_iter_get_basic(value, &capabilities);
637
638         network->capabilities = capabilities;
639
640         if (capabilities & IEEE80211_CAP_PRIVACY)
641                 network->has_wep = TRUE;
642 }
643
644 static void properties_reply(DBusPendingCall *call, void *user_data)
645 {
646         struct supplicant_task *task = user_data;
647         struct supplicant_network *network;
648         DBusMessage *reply;
649         DBusMessageIter array, dict;
650
651         DBG("task %p", task);
652
653         reply = dbus_pending_call_steal_reply(call);
654
655         network = g_try_new0(struct supplicant_network, 1);
656         if (network == NULL)
657                 goto done;
658
659         dbus_message_iter_init(reply, &array);
660
661         dbus_message_iter_recurse(&array, &dict);
662
663         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
664                 DBusMessageIter entry, value;
665                 const char *key;
666
667                 dbus_message_iter_recurse(&dict, &entry);
668                 dbus_message_iter_get_basic(&entry, &key);
669
670                 dbus_message_iter_next(&entry);
671
672                 dbus_message_iter_recurse(&entry, &value);
673
674                 //type = dbus_message_iter_get_arg_type(&value);
675                 //dbus_message_iter_get_basic(&value, &val);
676
677                 if (g_str_equal(key, "ssid") == TRUE)
678                         extract_ssid(network, &value);
679                 else if (g_str_equal(key, "wpaie") == TRUE)
680                         extract_wpaie(network, &value);
681                 else if (g_str_equal(key, "rsnie") == TRUE)
682                         extract_rsnie(network, &value);
683                 else if (g_str_equal(key, "capabilities") == TRUE)
684                         extract_capabilites(network, &value);
685
686                 dbus_message_iter_next(&dict);
687         }
688
689         if (task->callback && task->callback->scan_result)
690                 task->callback->scan_result(task->element, network);
691
692         g_free(network);
693
694 done:
695         dbus_message_unref(reply);
696 }
697
698 static int get_network_properties(struct supplicant_task *task,
699                                                         const char *path)
700 {
701         DBusMessage *message;
702         DBusPendingCall *call;
703
704         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
705                                                 SUPPLICANT_INTF ".BSSID",
706                                                                 "properties");
707         if (message == NULL)
708                 return -ENOMEM;
709
710         if (dbus_connection_send_with_reply(connection, message,
711                                                 &call, TIMEOUT) == FALSE) {
712                 connman_error("Failed to get network properties");
713                 dbus_message_unref(message);
714                 return -EIO;
715         }
716
717         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
718
719         dbus_message_unref(message);
720
721         return 0;
722 }
723
724 static void scan_results_reply(DBusPendingCall *call, void *user_data)
725 {
726         struct supplicant_task *task = user_data;
727         DBusMessage *reply;
728         DBusError error;
729         char **results;
730         int i, num_results;
731
732         DBG("task %p", task);
733
734         reply = dbus_pending_call_steal_reply(call);
735
736         dbus_error_init(&error);
737
738         if (dbus_message_get_args(reply, &error,
739                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
740                                                 &results, &num_results,
741                                                 DBUS_TYPE_INVALID) == FALSE) {
742                 if (dbus_error_is_set(&error) == TRUE) {
743                         connman_error("%s", error.message);
744                         dbus_error_free(&error);
745                 } else
746                         connman_error("Wrong arguments for scan result");
747                 goto done;
748         }
749
750         for (i = 0; i < num_results; i++)
751                 get_network_properties(task, results[i]);
752
753         g_strfreev(results);
754
755 done:
756         dbus_message_unref(reply);
757 }
758
759 static int scan_results_available(struct supplicant_task *task)
760 {
761         DBusMessage *message;
762         DBusPendingCall *call;
763
764         DBG("task %p", task);
765
766         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
767                                                 SUPPLICANT_INTF ".Interface",
768                                                         "scanResults");
769         if (message == NULL)
770                 return -ENOMEM;
771
772         if (dbus_connection_send_with_reply(connection, message,
773                                                 &call, TIMEOUT) == FALSE) {
774                 connman_error("Failed to request scan result");
775                 dbus_message_unref(message);
776                 return -EIO;
777         }
778
779         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
780
781         dbus_message_unref(message);
782
783         return 0;
784 }
785
786 static void state_change(struct supplicant_task *task, DBusMessage *msg)
787 {
788         DBusError error;
789         const char *state, *previous;
790
791         dbus_error_init(&error);
792
793         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &state,
794                                                 DBUS_TYPE_STRING, &previous,
795                                                 DBUS_TYPE_INVALID) == FALSE) {
796                 if (dbus_error_is_set(&error) == TRUE) {
797                         connman_error("%s", error.message);
798                         dbus_error_free(&error);
799                 } else
800                         connman_error("Wrong arguments for state change");
801                 return;
802         }
803
804         DBG("state %s ==> %s", previous, state);
805
806         if (g_str_equal(state, "INACTIVE") == TRUE)
807                 task->state = STATE_INACTIVE;
808         else if (g_str_equal(state, "SCANNING") == TRUE)
809                 task->state = STATE_SCANNING;
810         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
811                 task->state = STATE_ASSOCIATING;
812         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
813                 task->state = STATE_ASSOCIATED;
814         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
815                 task->state = STATE_4WAY_HANDSHAKE;
816         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
817                 task->state = STATE_4WAY_HANDSHAKE;
818         else if (g_str_equal(state, "COMPLETED") == TRUE)
819                 task->state = STATE_COMPLETED;
820         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
821                 task->state = STATE_DISCONNECTED;
822
823         if (task->callback && task->callback->state_change)
824                 task->callback->state_change(task->element, task->state);
825
826         switch (task->state) {
827         case STATE_COMPLETED:
828                 /* carrier on */
829                 break;
830         case STATE_DISCONNECTED:
831                 /* carrier off */
832                 break;
833         default:
834                 break;
835         }
836 }
837
838 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
839                                                 DBusMessage *msg, void *data)
840 {
841         struct supplicant_task *task;
842         const char *member, *path;
843
844         if (dbus_message_has_interface(msg,
845                                 SUPPLICANT_INTF ".Interface") == FALSE)
846                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
847
848         member = dbus_message_get_member(msg);
849         if (member == NULL)
850                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
851
852         path = dbus_message_get_path(msg);
853         if (path == NULL)
854                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
855
856         task = find_task_by_path(path);
857         if (task == NULL)
858                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
859
860         DBG("task %p member %s", task, member);
861
862         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
863                 scan_results_available(task);
864         else if (g_str_equal(member, "StateChange") == TRUE)
865                 state_change(task, msg);
866
867         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
868 }
869
870 static int add_filter(struct supplicant_task *task)
871 {
872         DBusError error;
873         gchar *filter;
874
875         filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
876                                                 SUPPLICANT_INTF, task->path);
877
878         DBG("filter %s", filter);
879
880         dbus_error_init(&error);
881
882         dbus_bus_add_match(connection, filter, &error);
883
884         g_free(filter);
885
886         if (dbus_error_is_set(&error) == TRUE) {
887                 connman_error("Can't add match: %s", error.message);
888                 dbus_error_free(&error);
889         }
890
891         return 0;
892 }
893
894 static int remove_filter(struct supplicant_task *task)
895 {
896         DBusError error;
897         gchar *filter;
898
899         filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
900                                                 SUPPLICANT_INTF, task->path);
901
902         DBG("filter %s", filter);
903
904         dbus_error_init(&error);
905
906         dbus_bus_remove_match(connection, filter, &error);
907
908         g_free(filter);
909
910         if (dbus_error_is_set(&error) == TRUE) {
911                 connman_error("Can't add match: %s", error.message);
912                 dbus_error_free(&error);
913         }
914
915         return 0;
916 }
917
918 int __supplicant_start(struct connman_element *element,
919                                         struct supplicant_callback *callback)
920 {
921         struct supplicant_task *task;
922         int err;
923
924         DBG("element %p name %s", element, element->name);
925
926         task = g_try_new0(struct supplicant_task, 1);
927         if (task == NULL)
928                 return -ENOMEM;
929
930         task->ifindex = element->index;
931         task->ifname = inet_index2name(element->index);
932         task->element = element;
933         task->callback = callback;
934
935         if (task->ifname == NULL) {
936                 g_free(task);
937                 return -ENOMEM;
938         }
939
940         task->created = FALSE;
941         task->state = STATE_INACTIVE;
942
943         g_static_mutex_lock(&task_mutex);
944         task_list = g_slist_append(task_list, task);
945         g_static_mutex_unlock(&task_mutex);
946
947         err = get_interface(task);
948         if (err < 0) {
949                 err = add_interface(task);
950                 if (err < 0) {
951                         g_free(task);
952                         return err;
953                 }
954         }
955
956         add_filter(task);
957
958         set_ap_scan(task);
959
960         return 0;
961 }
962
963 int __supplicant_stop(struct connman_element *element)
964 {
965         struct supplicant_task *task;
966
967         DBG("element %p name %s", element, element->name);
968
969         task = find_task_by_index(element->index);
970         if (task == NULL)
971                 return -ENODEV;
972
973         g_static_mutex_lock(&task_mutex);
974         task_list = g_slist_remove(task_list, task);
975         g_static_mutex_unlock(&task_mutex);
976
977         disable_network(task);
978
979         remove_network(task);
980
981         remove_filter(task);
982
983         remove_interface(task);
984
985         g_free(task->ifname);
986         g_free(task->path);
987         g_free(task);
988
989         return 0;
990 }
991
992 int __supplicant_scan(struct connman_element *element)
993 {
994         struct supplicant_task *task;
995         int err;
996
997         DBG("element %p name %s", element, element->name);
998
999         task = find_task_by_index(element->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, const char *ssid)
1021 {
1022         struct supplicant_task *task;
1023         const char *passphrase = NULL;
1024
1025         DBG("element %p name %s", element, element->name);
1026
1027         task = find_task_by_index(element->index);
1028         if (task == NULL)
1029                 return -ENODEV;
1030
1031         add_network(task);
1032
1033         select_network(task);
1034         disable_network(task);
1035
1036         set_network(task, ssid, passphrase);
1037
1038         enable_network(task);
1039
1040         return 0;
1041 }
1042
1043 int __supplicant_disconnect(struct connman_element *element)
1044 {
1045         struct supplicant_task *task;
1046
1047         DBG("element %p name %s", element, element->name);
1048
1049         task = find_task_by_index(element->index);
1050         if (task == NULL)
1051                 return -ENODEV;
1052
1053         disable_network(task);
1054
1055         remove_network(task);
1056
1057         return 0;
1058 }
1059
1060 int __supplicant_init(void)
1061 {
1062         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1063         if (connection == NULL)
1064                 return -EIO;
1065
1066         if (dbus_connection_add_filter(connection,
1067                                 supplicant_filter, NULL, NULL) == FALSE) {
1068                 dbus_connection_unref(connection);
1069                 return -EIO;
1070         }
1071
1072         return 0;
1073 }
1074
1075 void __supplicant_exit(void)
1076 {
1077         if (connection == NULL)
1078                 return;
1079
1080         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1081
1082         dbus_connection_unref(connection);
1083         connection = NULL;
1084 }