Extract all properties from scan result
[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         dbus_message_iter_get_basic(value, &network->capabilities);
623
624         if (network->capabilities & IEEE80211_CAP_PRIVACY)
625                 network->has_wep = TRUE;
626 }
627
628 static void properties_reply(DBusPendingCall *call, void *user_data)
629 {
630         struct supplicant_task *task = user_data;
631         struct supplicant_network *network;
632         DBusMessage *reply;
633         DBusMessageIter array, dict;
634
635         DBG("task %p", task);
636
637         reply = dbus_pending_call_steal_reply(call);
638
639         network = g_try_new0(struct supplicant_network, 1);
640         if (network == NULL)
641                 goto done;
642
643         dbus_message_iter_init(reply, &array);
644
645         dbus_message_iter_recurse(&array, &dict);
646
647         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
648                 DBusMessageIter entry, value;
649                 const char *key;
650
651                 dbus_message_iter_recurse(&dict, &entry);
652                 dbus_message_iter_get_basic(&entry, &key);
653
654                 dbus_message_iter_next(&entry);
655
656                 dbus_message_iter_recurse(&entry, &value);
657
658                 //type = dbus_message_iter_get_arg_type(&value);
659                 //dbus_message_iter_get_basic(&value, &val);
660
661                 /* 
662                  * bssid        : a (97)
663                  * ssid         : a (97)
664                  * wpaie        : a (97)
665                  * rsnie        : a (97)
666                  * frequency    : i (105)
667                  * capabilities : q (113)
668                  * quality      : i (105)
669                  * noise        : i (105)
670                  * level        : i (105)
671                  * maxrate      : i (105)
672                  */
673
674                 if (g_str_equal(key, "ssid") == TRUE)
675                         extract_ssid(network, &value);
676                 else if (g_str_equal(key, "wpaie") == TRUE)
677                         extract_wpaie(network, &value);
678                 else if (g_str_equal(key, "rsnie") == TRUE)
679                         extract_rsnie(network, &value);
680                 else if (g_str_equal(key, "capabilities") == TRUE)
681                         extract_capabilites(network, &value);
682                 else if (g_str_equal(key, "quality") == TRUE)
683                         dbus_message_iter_get_basic(&value, &network->quality);
684                 else if (g_str_equal(key, "noise") == TRUE)
685                         dbus_message_iter_get_basic(&value, &network->noise);
686                 else if (g_str_equal(key, "level") == TRUE)
687                         dbus_message_iter_get_basic(&value, &network->level);
688                 else if (g_str_equal(key, "maxrate") == TRUE)
689                         dbus_message_iter_get_basic(&value, &network->maxrate);
690
691
692                 dbus_message_iter_next(&dict);
693         }
694
695         if (task->callback && task->callback->scan_result)
696                 task->callback->scan_result(task->element, network);
697
698         g_free(network->identifier);
699         g_free(network->ssid);
700         g_free(network);
701
702 done:
703         dbus_message_unref(reply);
704 }
705
706 static int get_network_properties(struct supplicant_task *task,
707                                                         const char *path)
708 {
709         DBusMessage *message;
710         DBusPendingCall *call;
711
712         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
713                                                 SUPPLICANT_INTF ".BSSID",
714                                                                 "properties");
715         if (message == NULL)
716                 return -ENOMEM;
717
718         if (dbus_connection_send_with_reply(connection, message,
719                                                 &call, TIMEOUT) == FALSE) {
720                 connman_error("Failed to get network properties");
721                 dbus_message_unref(message);
722                 return -EIO;
723         }
724
725         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
726
727         dbus_message_unref(message);
728
729         return 0;
730 }
731
732 static void scan_results_reply(DBusPendingCall *call, void *user_data)
733 {
734         struct supplicant_task *task = user_data;
735         DBusMessage *reply;
736         DBusError error;
737         char **results;
738         int i, num_results;
739
740         DBG("task %p", task);
741
742         reply = dbus_pending_call_steal_reply(call);
743
744         dbus_error_init(&error);
745
746         if (dbus_message_get_args(reply, &error,
747                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
748                                                 &results, &num_results,
749                                                 DBUS_TYPE_INVALID) == FALSE) {
750                 if (dbus_error_is_set(&error) == TRUE) {
751                         connman_error("%s", error.message);
752                         dbus_error_free(&error);
753                 } else
754                         connman_error("Wrong arguments for scan result");
755                 goto done;
756         }
757
758         for (i = 0; i < num_results; i++)
759                 get_network_properties(task, results[i]);
760
761         g_strfreev(results);
762
763 done:
764         dbus_message_unref(reply);
765 }
766
767 static int scan_results_available(struct supplicant_task *task)
768 {
769         DBusMessage *message;
770         DBusPendingCall *call;
771
772         DBG("task %p", task);
773
774         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
775                                                 SUPPLICANT_INTF ".Interface",
776                                                         "scanResults");
777         if (message == NULL)
778                 return -ENOMEM;
779
780         if (dbus_connection_send_with_reply(connection, message,
781                                                 &call, TIMEOUT) == FALSE) {
782                 connman_error("Failed to request scan result");
783                 dbus_message_unref(message);
784                 return -EIO;
785         }
786
787         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
788
789         dbus_message_unref(message);
790
791         return 0;
792 }
793
794 static void state_change(struct supplicant_task *task, DBusMessage *msg)
795 {
796         DBusError error;
797         const char *state, *previous;
798
799         dbus_error_init(&error);
800
801         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &state,
802                                                 DBUS_TYPE_STRING, &previous,
803                                                 DBUS_TYPE_INVALID) == FALSE) {
804                 if (dbus_error_is_set(&error) == TRUE) {
805                         connman_error("%s", error.message);
806                         dbus_error_free(&error);
807                 } else
808                         connman_error("Wrong arguments for state change");
809                 return;
810         }
811
812         DBG("state %s ==> %s", previous, state);
813
814         if (g_str_equal(state, "INACTIVE") == TRUE)
815                 task->state = STATE_INACTIVE;
816         else if (g_str_equal(state, "SCANNING") == TRUE)
817                 task->state = STATE_SCANNING;
818         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
819                 task->state = STATE_ASSOCIATING;
820         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
821                 task->state = STATE_ASSOCIATED;
822         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
823                 task->state = STATE_4WAY_HANDSHAKE;
824         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
825                 task->state = STATE_4WAY_HANDSHAKE;
826         else if (g_str_equal(state, "COMPLETED") == TRUE)
827                 task->state = STATE_COMPLETED;
828         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
829                 task->state = STATE_DISCONNECTED;
830
831         if (task->callback && task->callback->state_change)
832                 task->callback->state_change(task->element, task->state);
833
834         switch (task->state) {
835         case STATE_COMPLETED:
836                 /* carrier on */
837                 break;
838         case STATE_DISCONNECTED:
839                 /* carrier off */
840                 break;
841         default:
842                 break;
843         }
844 }
845
846 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
847                                                 DBusMessage *msg, void *data)
848 {
849         struct supplicant_task *task;
850         const char *member, *path;
851
852         if (dbus_message_has_interface(msg,
853                                 SUPPLICANT_INTF ".Interface") == FALSE)
854                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
855
856         member = dbus_message_get_member(msg);
857         if (member == NULL)
858                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
859
860         path = dbus_message_get_path(msg);
861         if (path == NULL)
862                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
863
864         task = find_task_by_path(path);
865         if (task == NULL)
866                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
867
868         DBG("task %p member %s", task, member);
869
870         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
871                 scan_results_available(task);
872         else if (g_str_equal(member, "StateChange") == TRUE)
873                 state_change(task, msg);
874
875         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
876 }
877
878 static int add_filter(struct supplicant_task *task)
879 {
880         DBusError error;
881         gchar *filter;
882
883         filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
884                                                 SUPPLICANT_INTF, task->path);
885
886         DBG("filter %s", filter);
887
888         dbus_error_init(&error);
889
890         dbus_bus_add_match(connection, filter, &error);
891
892         g_free(filter);
893
894         if (dbus_error_is_set(&error) == TRUE) {
895                 connman_error("Can't add match: %s", error.message);
896                 dbus_error_free(&error);
897         }
898
899         return 0;
900 }
901
902 static int remove_filter(struct supplicant_task *task)
903 {
904         DBusError error;
905         gchar *filter;
906
907         filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
908                                                 SUPPLICANT_INTF, task->path);
909
910         DBG("filter %s", filter);
911
912         dbus_error_init(&error);
913
914         dbus_bus_remove_match(connection, filter, &error);
915
916         g_free(filter);
917
918         if (dbus_error_is_set(&error) == TRUE) {
919                 connman_error("Can't add match: %s", error.message);
920                 dbus_error_free(&error);
921         }
922
923         return 0;
924 }
925
926 int __supplicant_start(struct connman_element *element,
927                                         struct supplicant_callback *callback)
928 {
929         struct supplicant_task *task;
930         int err;
931
932         DBG("element %p name %s", element, element->name);
933
934         task = g_try_new0(struct supplicant_task, 1);
935         if (task == NULL)
936                 return -ENOMEM;
937
938         task->ifindex = element->index;
939         task->ifname = inet_index2name(element->index);
940         task->element = element;
941         task->callback = callback;
942
943         if (task->ifname == NULL) {
944                 g_free(task);
945                 return -ENOMEM;
946         }
947
948         task->created = FALSE;
949         task->state = STATE_INACTIVE;
950
951         task_list = g_slist_append(task_list, task);
952
953         err = get_interface(task);
954         if (err < 0) {
955                 err = add_interface(task);
956                 if (err < 0) {
957                         g_free(task);
958                         return err;
959                 }
960         }
961
962         add_filter(task);
963
964         set_ap_scan(task);
965
966         return 0;
967 }
968
969 int __supplicant_stop(struct connman_element *element)
970 {
971         struct supplicant_task *task;
972
973         DBG("element %p name %s", element, element->name);
974
975         task = find_task_by_index(element->index);
976         if (task == NULL)
977                 return -ENODEV;
978
979         task_list = g_slist_remove(task_list, task);
980
981         disable_network(task);
982
983         remove_network(task);
984
985         remove_filter(task);
986
987         remove_interface(task);
988
989         g_free(task->ifname);
990         g_free(task->path);
991         g_free(task);
992
993         return 0;
994 }
995
996 int __supplicant_scan(struct connman_element *element)
997 {
998         struct supplicant_task *task;
999         int err;
1000
1001         DBG("element %p name %s", element, element->name);
1002
1003         task = find_task_by_index(element->index);
1004         if (task == NULL)
1005                 return -ENODEV;
1006
1007         switch (task->state) {
1008         case STATE_SCANNING:
1009                 return -EALREADY;
1010         case STATE_ASSOCIATING:
1011         case STATE_ASSOCIATED:
1012         case STATE_4WAY_HANDSHAKE:
1013         case STATE_GROUP_HANDSHAKE:
1014                 return -EBUSY;
1015         default:
1016                 break;
1017         }
1018
1019         err = initiate_scan(task);
1020
1021         return 0;
1022 }
1023
1024 int __supplicant_connect(struct connman_element *element,
1025                                 const unsigned char *ssid, int ssid_len,
1026                                                         const char *passphrase)
1027 {
1028         struct supplicant_task *task;
1029
1030         DBG("element %p name %s", element, element->name);
1031
1032         task = find_task_by_index(element->index);
1033         if (task == NULL)
1034                 return -ENODEV;
1035
1036         add_network(task);
1037
1038         select_network(task);
1039         disable_network(task);
1040
1041         set_network(task, ssid, ssid_len, passphrase);
1042
1043         enable_network(task);
1044
1045         return 0;
1046 }
1047
1048 int __supplicant_disconnect(struct connman_element *element)
1049 {
1050         struct supplicant_task *task;
1051
1052         DBG("element %p name %s", element, element->name);
1053
1054         task = find_task_by_index(element->index);
1055         if (task == NULL)
1056                 return -ENODEV;
1057
1058         disable_network(task);
1059
1060         remove_network(task);
1061
1062         return 0;
1063 }
1064
1065 int __supplicant_init(DBusConnection *conn)
1066 {
1067         connection = conn;
1068
1069         if (dbus_connection_add_filter(connection,
1070                                 supplicant_filter, NULL, NULL) == FALSE) {
1071                 dbus_connection_unref(connection);
1072                 return -EIO;
1073         }
1074
1075         return 0;
1076 }
1077
1078 void __supplicant_exit(void)
1079 {
1080         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1081 }