5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <net/ethernet.h>
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/device.h>
36 #include <connman/option.h>
37 #include <connman/inet.h>
38 #include <connman/dbus.h>
39 #include <connman/log.h>
41 #include "supplicant.h"
45 #define IEEE80211_CAP_ESS 0x0001
46 #define IEEE80211_CAP_IBSS 0x0002
47 #define IEEE80211_CAP_PRIVACY 0x0010
49 #define SUPPLICANT_NAME "fi.epitest.hostap.WPASupplicant"
50 #define SUPPLICANT_INTF "fi.epitest.hostap.WPASupplicant"
51 #define SUPPLICANT_PATH "/fi/epitest/hostap/WPASupplicant"
53 /* Taken from "WPA Supplicant - Common definitions" */
54 enum supplicant_state {
56 * WPA_DISCONNECTED - Disconnected state
58 * This state indicates that client is not associated, but is likely to
59 * start looking for an access point. This state is entered when a
65 * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
67 * This state is entered if there are no enabled networks in the
68 * configuration. wpa_supplicant is not trying to associate with a new
69 * network and external interaction (e.g., ctrl_iface call to add or
70 * enable a network) is needed to start association.
75 * WPA_SCANNING - Scanning for a network
77 * This state is entered when wpa_supplicant starts scanning for a
83 * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
85 * This state is entered when wpa_supplicant has found a suitable BSS
86 * to associate with and the driver is configured to try to associate
87 * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
88 * state is entered when the driver is configured to try to associate
89 * with a network using the configured SSID and security policy.
94 * WPA_ASSOCIATED - Association completed
96 * This state is entered when the driver reports that association has
97 * been successfully completed with an AP. If IEEE 802.1X is used
98 * (with or without WPA/WPA2), wpa_supplicant remains in this state
99 * until the IEEE 802.1X/EAPOL authentication has been completed.
104 * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
106 * This state is entered when WPA/WPA2 4-Way Handshake is started. In
107 * case of WPA-PSK, this happens when receiving the first EAPOL-Key
108 * frame after association. In case of WPA-EAP, this state is entered
109 * when the IEEE 802.1X/EAPOL authentication has been completed.
114 * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
116 * This state is entered when 4-Way Key Handshake has been completed
117 * (i.e., when the supplicant sends out message 4/4) and when Group
118 * Key rekeying is started by the AP (i.e., when supplicant receives
124 * WPA_COMPLETED - All authentication completed
126 * This state is entered when the full authentication process is
127 * completed. In case of WPA2, this happens when the 4-Way Handshake is
128 * successfully completed. With WPA, this state is entered after the
129 * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
130 * completed after dynamic keys are received (or if not used, after
131 * the EAP authentication has been completed). With static WEP keys and
132 * plaintext connections, this state is entered when an association
133 * has been completed.
135 * This state indicates that the supplicant has completed its
136 * processing for the association phase and that data connection is
142 * WPA_INVALID - Invalid state (parsing error)
144 * This state is returned if the string input is invalid. It is not
145 * an official wpa_supplicant state.
150 struct supplicant_result {
154 unsigned int addr_len;
156 unsigned int ssid_len;
157 dbus_uint16_t capabilities;
163 dbus_int32_t frequency;
164 dbus_int32_t quality;
167 dbus_int32_t maxrate;
170 struct supplicant_task {
173 struct connman_device *device;
174 struct connman_network *network;
178 enum supplicant_state state;
180 GSList *scan_results;
183 static GSList *task_list = NULL;
185 static DBusConnection *connection;
187 static void free_task(struct supplicant_task *task)
189 DBG("task %p", task);
191 g_free(task->ifname);
196 static struct supplicant_task *find_task_by_index(int index)
200 for (list = task_list; list; list = list->next) {
201 struct supplicant_task *task = list->data;
203 if (task->ifindex == index)
210 static struct supplicant_task *find_task_by_path(const char *path)
214 for (list = task_list; list; list = list->next) {
215 struct supplicant_task *task = list->data;
217 if (g_strcmp0(task->path, path) == 0)
224 static void add_interface_reply(DBusPendingCall *call, void *user_data)
226 struct supplicant_task *task = user_data;
231 DBG("task %p", task);
233 reply = dbus_pending_call_steal_reply(call);
237 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
240 dbus_error_init(&error);
242 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
243 DBUS_TYPE_INVALID) == FALSE) {
244 if (dbus_error_is_set(&error) == TRUE) {
245 connman_error("%s", error.message);
246 dbus_error_free(&error);
248 connman_error("Wrong arguments for add interface");
252 DBG("path %s", path);
254 task->path = g_strdup(path);
255 task->created = TRUE;
257 connman_device_set_powered(task->device, TRUE);
259 dbus_message_unref(reply);
264 task_list = g_slist_remove(task_list, task);
266 connman_device_unref(task->device);
271 static int add_interface(struct supplicant_task *task)
273 const char *driver = connman_option_get_string("wifi");
274 DBusMessage *message;
275 DBusMessageIter array, dict;
276 DBusPendingCall *call;
278 DBG("task %p", task);
280 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
281 SUPPLICANT_INTF, "addInterface");
285 dbus_message_iter_init_append(message, &array);
287 dbus_message_iter_append_basic(&array,
288 DBUS_TYPE_STRING, &task->ifname);
290 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
291 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
292 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
293 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
295 connman_dbus_dict_append_variant(&dict, "driver",
296 DBUS_TYPE_STRING, &driver);
298 dbus_message_iter_close_container(&array, &dict);
300 if (dbus_connection_send_with_reply(connection, message,
301 &call, TIMEOUT) == FALSE) {
302 connman_error("Failed to add interface");
303 dbus_message_unref(message);
308 connman_error("D-Bus connection not available");
309 dbus_message_unref(message);
313 dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
315 dbus_message_unref(message);
320 static void get_interface_reply(DBusPendingCall *call, void *user_data)
322 struct supplicant_task *task = user_data;
327 DBG("task %p", task);
329 reply = dbus_pending_call_steal_reply(call);
333 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
338 dbus_error_init(&error);
340 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
341 DBUS_TYPE_INVALID) == FALSE) {
342 if (dbus_error_is_set(&error) == TRUE) {
343 connman_error("%s", error.message);
344 dbus_error_free(&error);
346 connman_error("Wrong arguments for get interface");
350 DBG("path %s", path);
352 task->path = g_strdup(path);
353 task->created = FALSE;
355 connman_device_set_powered(task->device, TRUE);
358 dbus_message_unref(reply);
361 static int create_interface(struct supplicant_task *task)
363 DBusMessage *message;
364 DBusPendingCall *call;
366 DBG("task %p", task);
368 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
369 SUPPLICANT_INTF, "getInterface");
373 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
376 if (dbus_connection_send_with_reply(connection, message,
377 &call, TIMEOUT) == FALSE) {
378 connman_error("Failed to get interface");
379 dbus_message_unref(message);
384 connman_error("D-Bus connection not available");
385 dbus_message_unref(message);
389 dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
391 dbus_message_unref(message);
396 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
398 struct supplicant_task *task = user_data;
401 DBG("task %p", task);
403 reply = dbus_pending_call_steal_reply(call);
405 connman_device_set_powered(task->device, FALSE);
407 connman_device_unref(task->device);
409 connman_inet_ifdown(task->ifindex);
413 dbus_message_unref(reply);
416 static int remove_interface(struct supplicant_task *task)
418 DBusMessage *message;
419 DBusPendingCall *call;
421 DBG("task %p", task);
423 if (task->created == FALSE) {
424 connman_device_set_powered(task->device, FALSE);
428 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
429 SUPPLICANT_INTF, "removeInterface");
433 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
436 if (dbus_connection_send_with_reply(connection, message,
437 &call, TIMEOUT) == FALSE) {
438 connman_error("Failed to remove interface");
439 dbus_message_unref(message);
444 connman_error("D-Bus connection not available");
445 dbus_message_unref(message);
449 dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
451 dbus_message_unref(message);
457 static int set_ap_scan(struct supplicant_task *task)
459 DBusMessage *message, *reply;
463 DBG("task %p", task);
465 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
466 SUPPLICANT_INTF ".Interface", "setAPScan");
470 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
473 dbus_error_init(&error);
475 reply = dbus_connection_send_with_reply_and_block(connection,
476 message, -1, &error);
478 if (dbus_error_is_set(&error) == TRUE) {
479 connman_error("%s", error.message);
480 dbus_error_free(&error);
482 connman_error("Failed to set AP scan");
483 dbus_message_unref(message);
487 dbus_message_unref(message);
489 dbus_message_unref(reply);
495 static int add_network(struct supplicant_task *task)
497 DBusMessage *message, *reply;
501 DBG("task %p", task);
503 if (task->netpath != NULL)
506 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
507 SUPPLICANT_INTF ".Interface", "addNetwork");
511 dbus_error_init(&error);
513 reply = dbus_connection_send_with_reply_and_block(connection,
514 message, -1, &error);
516 if (dbus_error_is_set(&error) == TRUE) {
517 connman_error("%s", error.message);
518 dbus_error_free(&error);
520 connman_error("Failed to add network");
521 dbus_message_unref(message);
525 dbus_message_unref(message);
527 dbus_error_init(&error);
529 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
530 DBUS_TYPE_INVALID) == FALSE) {
531 if (dbus_error_is_set(&error) == TRUE) {
532 connman_error("%s", error.message);
533 dbus_error_free(&error);
535 connman_error("Wrong arguments for network");
536 dbus_message_unref(reply);
540 DBG("path %s", path);
542 task->netpath = g_strdup(path);
544 dbus_message_unref(reply);
549 static int remove_network(struct supplicant_task *task)
551 DBusMessage *message, *reply;
554 DBG("task %p", task);
556 if (task->netpath == NULL)
559 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
560 SUPPLICANT_INTF ".Interface", "removeNetwork");
564 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
567 dbus_error_init(&error);
569 reply = dbus_connection_send_with_reply_and_block(connection,
570 message, -1, &error);
572 if (dbus_error_is_set(&error) == TRUE) {
573 connman_error("%s", error.message);
574 dbus_error_free(&error);
576 connman_error("Failed to remove network");
577 dbus_message_unref(message);
581 dbus_message_unref(message);
583 dbus_message_unref(reply);
585 g_free(task->netpath);
586 task->netpath = NULL;
591 static int select_network(struct supplicant_task *task)
593 DBusMessage *message, *reply;
596 DBG("task %p", task);
598 if (task->netpath == NULL)
601 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
602 SUPPLICANT_INTF ".Interface", "selectNetwork");
606 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
609 dbus_error_init(&error);
611 reply = dbus_connection_send_with_reply_and_block(connection,
612 message, -1, &error);
614 if (dbus_error_is_set(&error) == TRUE) {
615 connman_error("%s", error.message);
616 dbus_error_free(&error);
618 connman_error("Failed to select network");
619 dbus_message_unref(message);
623 dbus_message_unref(message);
625 dbus_message_unref(reply);
630 static int enable_network(struct supplicant_task *task)
632 DBusMessage *message, *reply;
635 DBG("task %p", task);
637 if (task->netpath == NULL)
640 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
641 SUPPLICANT_INTF ".Network", "enable");
645 dbus_error_init(&error);
647 reply = dbus_connection_send_with_reply_and_block(connection,
648 message, -1, &error);
650 if (dbus_error_is_set(&error) == TRUE) {
651 connman_error("%s", error.message);
652 dbus_error_free(&error);
654 connman_error("Failed to enable network");
655 dbus_message_unref(message);
659 dbus_message_unref(message);
661 dbus_message_unref(reply);
666 static int disable_network(struct supplicant_task *task)
668 DBusMessage *message, *reply;
671 DBG("task %p", task);
673 if (task->netpath == NULL)
676 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
677 SUPPLICANT_INTF ".Network", "disable");
681 dbus_error_init(&error);
683 reply = dbus_connection_send_with_reply_and_block(connection,
684 message, -1, &error);
686 if (dbus_error_is_set(&error) == TRUE) {
687 connman_error("%s", error.message);
688 dbus_error_free(&error);
690 connman_error("Failed to disable network");
691 dbus_message_unref(message);
695 dbus_message_unref(message);
697 dbus_message_unref(reply);
702 static int set_network(struct supplicant_task *task,
703 const unsigned char *network, int len,
704 const char *address, const char *security,
705 const char *passphrase)
707 DBusMessage *message, *reply;
708 DBusMessageIter array, dict;
711 DBG("task %p", task);
713 if (task->netpath == NULL)
716 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
717 SUPPLICANT_INTF ".Network", "set");
721 dbus_message_iter_init_append(message, &array);
723 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
724 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
725 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
726 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
728 if (address == NULL) {
729 dbus_uint32_t scan_ssid = 1;
730 connman_dbus_dict_append_variant(&dict, "scan_ssid",
731 DBUS_TYPE_UINT32, &scan_ssid);
733 connman_dbus_dict_append_variant(&dict, "bssid",
734 DBUS_TYPE_STRING, &address);
736 connman_dbus_dict_append_array(&dict, "ssid",
737 DBUS_TYPE_BYTE, &network, len);
739 if (g_ascii_strcasecmp(security, "wpa") == 0 ||
740 g_ascii_strcasecmp(security, "rsn") == 0) {
741 const char *key_mgmt = "WPA-PSK";
742 connman_dbus_dict_append_variant(&dict, "key_mgmt",
743 DBUS_TYPE_STRING, &key_mgmt);
745 if (passphrase && strlen(passphrase) > 0)
746 connman_dbus_dict_append_variant(&dict, "psk",
747 DBUS_TYPE_STRING, &passphrase);
748 } else if (g_ascii_strcasecmp(security, "wep") == 0) {
749 const char *key_mgmt = "NONE", *index = "0";
750 const char *auth_alg = "OPEN SHARED";
751 connman_dbus_dict_append_variant(&dict, "key_mgmt",
752 DBUS_TYPE_STRING, &key_mgmt);
754 connman_dbus_dict_append_variant(&dict, "auth_alg",
755 DBUS_TYPE_STRING, &auth_alg);
758 int size = strlen(passphrase);
759 if (size == 10 || size == 26) {
760 unsigned char *key = malloc(13);
763 memset(tmp, 0, sizeof(tmp));
766 for (i = 0; i < size / 2; i++) {
767 memcpy(tmp, passphrase + (i * 2), 2);
768 key[i] = (unsigned char) strtol(tmp,
771 connman_dbus_dict_append_array(&dict,
772 "wep_key0", DBUS_TYPE_BYTE,
776 connman_dbus_dict_append_variant(&dict,
777 "wep_key0", DBUS_TYPE_STRING,
779 connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
780 DBUS_TYPE_STRING, &index);
783 const char *key_mgmt = "NONE";
784 connman_dbus_dict_append_variant(&dict, "key_mgmt",
785 DBUS_TYPE_STRING, &key_mgmt);
788 dbus_message_iter_close_container(&array, &dict);
790 dbus_error_init(&error);
792 reply = dbus_connection_send_with_reply_and_block(connection,
793 message, -1, &error);
795 if (dbus_error_is_set(&error) == TRUE) {
796 connman_error("%s", error.message);
797 dbus_error_free(&error);
799 connman_error("Failed to set network options");
800 dbus_message_unref(message);
804 dbus_message_unref(message);
806 dbus_message_unref(reply);
811 static int initiate_scan(struct supplicant_task *task)
813 DBusMessage *message;
814 DBusPendingCall *call;
816 DBG("task %p", task);
818 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
819 SUPPLICANT_INTF ".Interface", "scan");
823 if (dbus_connection_send_with_reply(connection, message,
824 &call, TIMEOUT) == FALSE) {
825 connman_error("Failed to initiate scan");
826 dbus_message_unref(message);
830 dbus_message_unref(message);
839 { "<hidden>", "hidden" },
840 { "default", "linksys" },
848 { "comcomcom", "3com" },
850 { "Symbol", "symbol" },
851 { "Motorola", "motorola" },
852 { "Wireless" , "wireless" },
857 static char *build_group(const char *addr, const char *name,
858 const unsigned char *ssid, unsigned int ssid_len,
859 const char *mode, const char *security)
867 str = g_string_sized_new((ssid_len * 2) + 24);
871 for (i = 0; special_ssid[i].name; i++) {
872 if (g_strcmp0(special_ssid[i].name, name) == 0) {
873 if (special_ssid[i].value == NULL)
874 g_string_append_printf(str, "%s_%s",
877 g_string_append_printf(str, "%s_%s",
878 special_ssid[i].value, addr);
883 if (ssid_len > 0 && ssid[0] != '\0') {
884 for (i = 0; i < ssid_len; i++)
885 g_string_append_printf(str, "%02x", ssid[i]);
887 g_string_append_printf(str, "hidden_%s", addr);
890 g_string_append_printf(str, "_%s_%s", mode, security);
892 return g_string_free(str, FALSE);
895 static void extract_addr(DBusMessageIter *value,
896 struct supplicant_result *result)
898 DBusMessageIter array;
899 struct ether_addr *eth;
903 dbus_message_iter_recurse(value, &array);
904 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
909 result->addr = g_try_malloc(addr_len);
910 if (result->addr == NULL)
913 memcpy(result->addr, addr, addr_len);
914 result->addr_len = addr_len;
916 result->path = g_try_malloc0(13);
917 if (result->path == NULL)
922 snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
923 eth->ether_addr_octet[0],
924 eth->ether_addr_octet[1],
925 eth->ether_addr_octet[2],
926 eth->ether_addr_octet[3],
927 eth->ether_addr_octet[4],
928 eth->ether_addr_octet[5]);
931 static void extract_ssid(DBusMessageIter *value,
932 struct supplicant_result *result)
934 DBusMessageIter array;
939 dbus_message_iter_recurse(value, &array);
940 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
945 result->ssid = g_try_malloc(ssid_len);
946 if (result->ssid == NULL)
949 memcpy(result->ssid, ssid, ssid_len);
950 result->ssid_len = ssid_len;
952 result->name = g_try_malloc0(ssid_len + 1);
953 if (result->name == NULL)
957 for (i = 0; i < ssid_len; i++)
958 if (g_ascii_isprint(ssid[i]))
964 static void extract_wpaie(DBusMessageIter *value,
965 struct supplicant_result *result)
967 DBusMessageIter array;
971 dbus_message_iter_recurse(value, &array);
972 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
975 result->has_wpa = TRUE;
978 static void extract_rsnie(DBusMessageIter *value,
979 struct supplicant_result *result)
981 DBusMessageIter array;
985 dbus_message_iter_recurse(value, &array);
986 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
989 result->has_rsn = TRUE;
992 static void extract_wpsie(DBusMessageIter *value,
993 struct supplicant_result *result)
995 DBusMessageIter array;
999 dbus_message_iter_recurse(value, &array);
1000 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1003 result->has_wps = TRUE;
1006 static void extract_capabilites(DBusMessageIter *value,
1007 struct supplicant_result *result)
1009 dbus_message_iter_get_basic(value, &result->capabilities);
1011 if (result->capabilities & IEEE80211_CAP_ESS)
1012 result->adhoc = FALSE;
1013 else if (result->capabilities & IEEE80211_CAP_IBSS)
1014 result->adhoc = TRUE;
1016 if (result->capabilities & IEEE80211_CAP_PRIVACY)
1017 result->has_wep = TRUE;
1020 static unsigned char calculate_strength(struct supplicant_result *result)
1022 if (result->quality < 0) {
1023 unsigned char strength;
1025 if (result->level > 0)
1026 strength = 100 - result->level;
1028 strength = 120 + result->level;
1036 return result->quality;
1039 static unsigned short calculate_channel(struct supplicant_result *result)
1041 if (result->frequency < 0)
1044 return (result->frequency - 2407) / 5;
1047 static void get_properties(struct supplicant_task *task);
1049 static void properties_reply(DBusPendingCall *call, void *user_data)
1051 struct supplicant_task *task = user_data;
1052 struct supplicant_result result;
1053 struct connman_network *network;
1055 DBusMessageIter array, dict;
1056 unsigned char strength;
1057 unsigned short channel, frequency;
1058 const char *mode, *security;
1061 DBG("task %p", task);
1063 reply = dbus_pending_call_steal_reply(call);
1064 if (reply == NULL) {
1065 get_properties(task);
1069 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1070 dbus_message_unref(reply);
1071 get_properties(task);
1075 memset(&result, 0, sizeof(result));
1076 result.frequency = -1;
1077 result.quality = -1;
1081 dbus_message_iter_init(reply, &array);
1083 dbus_message_iter_recurse(&array, &dict);
1085 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1086 DBusMessageIter entry, value;
1089 dbus_message_iter_recurse(&dict, &entry);
1090 dbus_message_iter_get_basic(&entry, &key);
1092 dbus_message_iter_next(&entry);
1094 dbus_message_iter_recurse(&entry, &value);
1096 //type = dbus_message_iter_get_arg_type(&value);
1097 //dbus_message_iter_get_basic(&value, &val);
1105 * frequency : i (105)
1106 * capabilities : q (113)
1113 if (g_str_equal(key, "bssid") == TRUE)
1114 extract_addr(&value, &result);
1115 else if (g_str_equal(key, "ssid") == TRUE)
1116 extract_ssid(&value, &result);
1117 else if (g_str_equal(key, "wpaie") == TRUE)
1118 extract_wpaie(&value, &result);
1119 else if (g_str_equal(key, "rsnie") == TRUE)
1120 extract_rsnie(&value, &result);
1121 else if (g_str_equal(key, "wpsie") == TRUE)
1122 extract_wpsie(&value, &result);
1123 else if (g_str_equal(key, "capabilities") == TRUE)
1124 extract_capabilites(&value, &result);
1125 else if (g_str_equal(key, "frequency") == TRUE)
1126 dbus_message_iter_get_basic(&value, &result.frequency);
1127 else if (g_str_equal(key, "quality") == TRUE)
1128 dbus_message_iter_get_basic(&value, &result.quality);
1129 else if (g_str_equal(key, "noise") == TRUE)
1130 dbus_message_iter_get_basic(&value, &result.noise);
1131 else if (g_str_equal(key, "level") == TRUE)
1132 dbus_message_iter_get_basic(&value, &result.level);
1133 else if (g_str_equal(key, "maxrate") == TRUE)
1134 dbus_message_iter_get_basic(&value, &result.maxrate);
1136 dbus_message_iter_next(&dict);
1139 if (result.path == NULL)
1142 if (result.path[0] == '\0')
1145 if (result.frequency > 0 && result.frequency < 14)
1146 result.frequency = 2407 + (5 * result.frequency);
1147 else if (result.frequency == 14)
1148 result.frequency = 2484;
1150 strength = calculate_strength(&result);
1151 channel = calculate_channel(&result);
1153 frequency = (result.frequency < 0) ? 0 : result.frequency;
1155 if (result.has_rsn == TRUE)
1157 else if (result.has_wpa == TRUE)
1159 else if (result.has_wep == TRUE)
1164 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1166 group = build_group(result.path, result.name,
1167 result.ssid, result.ssid_len,
1170 network = connman_device_get_network(task->device, result.path);
1171 if (network == NULL) {
1174 network = connman_network_create(result.path,
1175 CONNMAN_NETWORK_TYPE_WIFI);
1176 if (network == NULL)
1179 index = connman_device_get_index(task->device);
1180 connman_network_set_index(network, index);
1182 connman_network_set_protocol(network,
1183 CONNMAN_NETWORK_PROTOCOL_IP);
1185 connman_network_set_address(network, result.addr,
1188 if (connman_device_add_network(task->device, network) < 0) {
1189 connman_network_unref(network);
1194 if (result.name != NULL && result.name[0] != '\0')
1195 connman_network_set_name(network, result.name);
1197 connman_network_set_blob(network, "WiFi.SSID",
1198 result.ssid, result.ssid_len);
1200 connman_network_set_string(network, "WiFi.Mode", mode);
1202 DBG("%s (%s %s) strength %d (%s)",
1203 result.name, mode, security, strength,
1204 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1206 connman_network_set_available(network, TRUE);
1207 connman_network_set_strength(network, strength);
1209 connman_network_set_uint16(network, "Frequency", frequency);
1210 connman_network_set_uint16(network, "WiFi.Channel", channel);
1211 connman_network_set_string(network, "WiFi.Security", security);
1213 connman_network_set_group(network, group);
1218 g_free(result.path);
1219 g_free(result.addr);
1220 g_free(result.name);
1221 g_free(result.ssid);
1223 dbus_message_unref(reply);
1225 get_properties(task);
1228 static void get_properties(struct supplicant_task *task)
1230 DBusMessage *message;
1231 DBusPendingCall *call;
1234 path = g_slist_nth_data(task->scan_results, 0);
1238 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1239 SUPPLICANT_INTF ".BSSID",
1242 task->scan_results = g_slist_remove(task->scan_results, path);
1245 if (message == NULL)
1248 if (dbus_connection_send_with_reply(connection, message,
1249 &call, TIMEOUT) == FALSE) {
1250 connman_error("Failed to get network properties");
1251 dbus_message_unref(message);
1256 connman_error("D-Bus connection not available");
1257 dbus_message_unref(message);
1261 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1263 dbus_message_unref(message);
1268 if (task->noscan == FALSE)
1269 connman_device_set_scanning(task->device, FALSE);
1272 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1274 struct supplicant_task *task = user_data;
1280 DBG("task %p", task);
1282 reply = dbus_pending_call_steal_reply(call);
1286 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1289 dbus_error_init(&error);
1291 if (dbus_message_get_args(reply, &error,
1292 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1293 &results, &num_results,
1294 DBUS_TYPE_INVALID) == FALSE) {
1295 if (dbus_error_is_set(&error) == TRUE) {
1296 connman_error("%s", error.message);
1297 dbus_error_free(&error);
1299 connman_error("Wrong arguments for scan result");
1303 if (num_results == 0)
1306 for (i = 0; i < num_results; i++) {
1307 char *path = g_strdup(results[i]);
1311 task->scan_results = g_slist_append(task->scan_results, path);
1314 g_strfreev(results);
1316 dbus_message_unref(reply);
1318 get_properties(task);
1323 dbus_message_unref(reply);
1326 if (task->noscan == FALSE)
1327 connman_device_set_scanning(task->device, FALSE);
1330 static void scan_results_available(struct supplicant_task *task)
1332 DBusMessage *message;
1333 DBusPendingCall *call;
1335 DBG("task %p", task);
1337 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1338 SUPPLICANT_INTF ".Interface",
1340 if (message == NULL)
1343 if (dbus_connection_send_with_reply(connection, message,
1344 &call, TIMEOUT) == FALSE) {
1345 connman_error("Failed to request scan result");
1349 if (task->noscan == FALSE)
1350 connman_device_set_scanning(task->device, TRUE);
1353 connman_error("D-Bus connection not available");
1357 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1360 dbus_message_unref(message);
1363 static enum supplicant_state string2state(const char *state)
1365 if (g_str_equal(state, "INACTIVE") == TRUE)
1366 return WPA_INACTIVE;
1367 else if (g_str_equal(state, "SCANNING") == TRUE)
1368 return WPA_SCANNING;
1369 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1370 return WPA_ASSOCIATING;
1371 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1372 return WPA_ASSOCIATED;
1373 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1374 return WPA_GROUP_HANDSHAKE;
1375 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1376 return WPA_4WAY_HANDSHAKE;
1377 else if (g_str_equal(state, "COMPLETED") == TRUE)
1378 return WPA_COMPLETED;
1379 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1380 return WPA_DISCONNECTED;
1385 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1388 const char *newstate, *oldstate;
1389 enum supplicant_state state;
1391 dbus_error_init(&error);
1393 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1394 DBUS_TYPE_STRING, &oldstate,
1395 DBUS_TYPE_INVALID) == FALSE) {
1396 if (dbus_error_is_set(&error) == TRUE) {
1397 connman_error("%s", error.message);
1398 dbus_error_free(&error);
1400 connman_error("Wrong arguments for state change");
1404 DBG("state %s ==> %s", oldstate, newstate);
1406 state = string2state(newstate);
1407 if (state == WPA_INVALID)
1410 task->state = state;
1412 switch (task->state) {
1414 task->noscan = TRUE;
1415 connman_device_set_scanning(task->device, TRUE);
1417 case WPA_ASSOCIATING:
1418 case WPA_ASSOCIATED:
1419 case WPA_4WAY_HANDSHAKE:
1420 case WPA_GROUP_HANDSHAKE:
1421 task->noscan = TRUE;
1424 case WPA_DISCONNECTED:
1425 task->noscan = FALSE;
1428 task->noscan = FALSE;
1429 connman_device_set_scanning(task->device, FALSE);
1435 if (task->network == NULL)
1438 switch (task->state) {
1441 connman_network_set_connected(task->network, TRUE);
1442 connman_device_set_scanning(task->device, FALSE);
1444 case WPA_DISCONNECTED:
1446 connman_network_set_connected(task->network, FALSE);
1447 connman_device_set_scanning(task->device, FALSE);
1449 case WPA_ASSOCIATING:
1450 connman_network_set_associating(task->network, TRUE);
1453 connman_network_set_associating(task->network, FALSE);
1458 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1459 DBusMessage *msg, void *data)
1461 struct supplicant_task *task;
1462 const char *member, *path;
1464 if (dbus_message_has_interface(msg,
1465 SUPPLICANT_INTF ".Interface") == FALSE)
1466 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1468 member = dbus_message_get_member(msg);
1470 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1472 path = dbus_message_get_path(msg);
1474 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1476 task = find_task_by_path(path);
1478 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1480 DBG("task %p member %s", task, member);
1482 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1483 scan_results_available(task);
1484 else if (g_str_equal(member, "StateChange") == TRUE)
1485 state_change(task, msg);
1487 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1490 int supplicant_start(struct connman_device *device)
1492 struct supplicant_task *task;
1494 DBG("device %p", device);
1496 task = g_try_new0(struct supplicant_task, 1);
1500 task->ifindex = connman_device_get_index(device);
1501 task->ifname = connman_inet_ifname(task->ifindex);
1503 if (task->ifname == NULL) {
1508 task->device = connman_device_ref(device);
1510 task->created = FALSE;
1511 task->noscan = FALSE;
1512 task->state = WPA_INVALID;
1514 task_list = g_slist_append(task_list, task);
1516 return create_interface(task);
1519 int supplicant_stop(struct connman_device *device)
1521 int index = connman_device_get_index(device);
1522 struct supplicant_task *task;
1524 DBG("device %p", device);
1526 task = find_task_by_index(index);
1530 task_list = g_slist_remove(task_list, task);
1532 disable_network(task);
1534 remove_network(task);
1536 return remove_interface(task);
1539 int supplicant_scan(struct connman_device *device)
1541 int index = connman_device_get_index(device);
1542 struct supplicant_task *task;
1545 DBG("device %p", device);
1547 task = find_task_by_index(index);
1551 switch (task->state) {
1554 case WPA_ASSOCIATING:
1555 case WPA_ASSOCIATED:
1556 case WPA_4WAY_HANDSHAKE:
1557 case WPA_GROUP_HANDSHAKE:
1563 err = initiate_scan(task);
1568 int supplicant_connect(struct connman_network *network)
1570 struct supplicant_task *task;
1571 const char *address, *security, *passphrase;
1573 unsigned int ssid_len;
1576 DBG("network %p", network);
1578 address = connman_network_get_string(network, "Address");
1579 security = connman_network_get_string(network, "WiFi.Security");
1580 passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1582 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1584 DBG("address %s security %s passphrase %s",
1585 address, security, passphrase);
1587 if (security == NULL && passphrase == NULL)
1590 if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1593 index = connman_network_get_index(network);
1595 task = find_task_by_index(index);
1599 task->network = connman_network_ref(network);
1603 select_network(task);
1604 disable_network(task);
1606 set_network(task, ssid, ssid_len, address, security, passphrase);
1608 enable_network(task);
1610 connman_network_set_associating(task->network, TRUE);
1615 int supplicant_disconnect(struct connman_network *network)
1617 struct supplicant_task *task;
1620 DBG("network %p", network);
1622 index = connman_network_get_index(network);
1624 task = find_task_by_index(index);
1628 disable_network(task);
1630 remove_network(task);
1632 connman_network_set_connected(task->network, FALSE);
1634 connman_network_unref(task->network);
1639 static void supplicant_activate(DBusConnection *conn)
1641 DBusMessage *message;
1643 DBG("conn %p", conn);
1645 message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1646 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1647 if (message == NULL)
1650 dbus_message_set_no_reply(message, TRUE);
1652 dbus_connection_send(conn, message, NULL);
1654 dbus_message_unref(message);
1657 static GSList *driver_list = NULL;
1659 static void supplicant_probe(DBusConnection *conn, void *user_data)
1663 DBG("conn %p", conn);
1665 for (list = driver_list; list; list = list->next) {
1666 struct supplicant_driver *driver = list->data;
1668 DBG("driver %p name %s", driver, driver->name);
1675 static void supplicant_remove(DBusConnection *conn, void *user_data)
1679 DBG("conn %p", conn);
1681 for (list = driver_list; list; list = list->next) {
1682 struct supplicant_driver *driver = list->data;
1684 DBG("driver %p name %s", driver, driver->name);
1691 static const char *supplicant_rule = "type=signal,"
1692 "interface=" SUPPLICANT_INTF ".Interface";
1695 static int supplicant_create(void)
1697 if (g_slist_length(driver_list) > 0)
1700 connection = connman_dbus_get_connection();
1701 if (connection == NULL)
1704 DBG("connection %p", connection);
1706 if (dbus_connection_add_filter(connection,
1707 supplicant_filter, NULL, NULL) == FALSE) {
1708 connection = connman_dbus_get_connection();
1712 dbus_bus_add_match(connection, supplicant_rule, NULL);
1713 dbus_connection_flush(connection);
1715 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1716 supplicant_probe, supplicant_remove, NULL, NULL);
1721 static void supplicant_destroy(void)
1723 if (g_slist_length(driver_list) > 0)
1726 DBG("connection %p", connection);
1729 g_dbus_remove_watch(connection, watch);
1731 dbus_bus_remove_match(connection, supplicant_rule, NULL);
1732 dbus_connection_flush(connection);
1734 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1736 dbus_connection_unref(connection);
1740 int supplicant_register(struct supplicant_driver *driver)
1744 DBG("driver %p name %s", driver, driver->name);
1746 err = supplicant_create();
1750 driver_list = g_slist_append(driver_list, driver);
1752 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1753 supplicant_probe(connection, NULL);
1755 supplicant_activate(connection);
1760 void supplicant_unregister(struct supplicant_driver *driver)
1762 DBG("driver %p name %s", driver, driver->name);
1764 supplicant_remove(connection, NULL);
1766 driver_list = g_slist_remove(driver_list, driver);
1768 supplicant_destroy();