Use async creation and removal for supplicant interfaces
[connman] / plugins / supplicant.c
index ad97f00..88e8123 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
 #include <config.h>
 #endif
 
-#include <stdio.h>
-#include <errno.h>
-#include <unistd.h>
 #include <stdlib.h>
-#include <signal.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <net/if.h>
+#include <string.h>
 
-#include <dbus/dbus.h>
 #include <gdbus.h>
 
+#define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/log.h>
+#include <connman/dbus.h>
 
+#include "inet.h"
 #include "supplicant.h"
 
-enum supplicant_state {
-       STATE_INACTIVE,
-       STATE_SCANNING,
-       STATE_ASSOCIATING,
-       STATE_ASSOCIATED,
-       STATE_4WAY_HANDSHAKE,
-       STATE_GROUP_HANDSHAKE,
-       STATE_COMPLETED,
-       STATE_DISCONNECTED,
-};
+#define TIMEOUT 5000
+
+#define IEEE80211_CAP_ESS       0x0001
+#define IEEE80211_CAP_IBSS      0x0002
+#define IEEE80211_CAP_PRIVACY   0x0010
+
+static GSList *driver_list = NULL;
+
+static void process_state_change(struct connman_device *device,
+                                               enum supplicant_state state)
+{
+       GSList *list;
+
+       for (list = driver_list; list; list = list->next) {
+               struct supplicant_driver *driver = list->data;
+
+               if (driver->state_change)
+                       driver->state_change(device, state);
+       }
+}
+
+static void process_clear_results(struct connman_device *device)
+{
+       GSList *list;
+
+       for (list = driver_list; list; list = list->next) {
+               struct supplicant_driver *driver = list->data;
+
+               if (driver->clear_results)
+                       driver->clear_results(device);
+       }
+}
+
+static void process_scan_result(struct connman_device *device,
+                                       struct supplicant_network *network)
+{
+       GSList *list;
+
+       for (list = driver_list; list; list = list->next) {
+               struct supplicant_driver *driver = list->data;
+
+               if (driver->scan_result)
+                       driver->scan_result(device, network);
+       }
+}
 
 struct supplicant_task {
-       DBusConnection *conn;
        int ifindex;
        gchar *ifname;
-       struct connman_iface *iface;
+       struct connman_device *device;
        gchar *path;
        gboolean created;
        gchar *network;
        enum supplicant_state state;
 };
 
-static GSList *tasks = NULL;
+static GSList *task_list = NULL;
 
-struct supplicant_ap {
-       gchar *identifier;
-       GByteArray *ssid;
-       guint capabilities;
-       gboolean has_wep;
-       gboolean has_wpa;
-       gboolean has_rsn;
-};
+static DBusConnection *connection;
 
-#define IEEE80211_CAP_ESS       0x0001
-#define IEEE80211_CAP_IBSS      0x0002
-#define IEEE80211_CAP_PRIVACY   0x0010
+static void free_task(struct supplicant_task *task)
+{
+       DBG("task %p", task);
+
+       g_free(task->ifname);
+       g_free(task->path);
+       g_free(task);
+}
 
-static struct supplicant_task *find_task(int ifindex)
+static struct supplicant_task *find_task_by_index(int index)
 {
        GSList *list;
 
-       for (list = tasks; list; list = list->next) {
+       for (list = task_list; list; list = list->next) {
                struct supplicant_task *task = list->data;
 
-               if (task->ifindex == ifindex) 
+               if (task->ifindex == index)
                        return task;
        }
 
        return NULL;
 }
 
-static int get_interface(struct supplicant_task *task)
+static struct supplicant_task *find_task_by_path(const char *path)
 {
-       DBusMessage *message, *reply;
+       GSList *list;
+
+       for (list = task_list; list; list = list->next) {
+               struct supplicant_task *task = list->data;
+
+               if (g_str_equal(task->path, path) == TRUE)
+                       return task;
+       }
+
+       return NULL;
+}
+
+static void add_interface_reply(DBusPendingCall *call, void *user_data)
+{
+       struct supplicant_task *task = user_data;
+       DBusMessage *reply;
        DBusError error;
        const char *path;
 
        DBG("task %p", task);
 
-       message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
-                                       SUPPLICANT_INTF, "getInterface");
-       if (message == NULL)
-               return -ENOMEM;
+       reply = dbus_pending_call_steal_reply(call);
+       if (reply == NULL)
+               return;
 
-       dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
-                                                       DBUS_TYPE_INVALID);
+       if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
+               goto done;
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
-                                                       message, -1, &error);
-       if (reply == NULL) {
+       if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
+                                               DBUS_TYPE_INVALID) == FALSE) {
                if (dbus_error_is_set(&error) == TRUE) {
                        connman_error("%s", error.message);
                        dbus_error_free(&error);
                } else
-                       connman_error("Failed to get interface");
+                       connman_error("Wrong arguments for interface");
+               goto done;
+       }
+
+       DBG("path %s", path);
+
+       task->path = g_strdup(path);
+       task->created = TRUE;
+
+       connman_device_set_powered(task->device, TRUE);
+
+done:
+       dbus_message_unref(reply);
+}
+
+static int add_interface(struct supplicant_task *task)
+{
+       DBusMessage *message;
+       DBusPendingCall *call;
+
+       DBG("task %p", task);
+
+       message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
+                                       SUPPLICANT_INTF, "addInterface");
+       if (message == NULL)
+               return -ENOMEM;
+
+       dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
+                                                       DBUS_TYPE_INVALID);
+
+       if (dbus_connection_send_with_reply(connection, message,
+                                               &call, TIMEOUT) == FALSE) {
+               connman_error("Failed to add interface");
                dbus_message_unref(message);
                return -EIO;
        }
 
+       dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
+
        dbus_message_unref(message);
 
+       return -EINPROGRESS;
+}
+
+static void get_interface_reply(DBusPendingCall *call, void *user_data)
+{
+       struct supplicant_task *task = user_data;
+       DBusMessage *reply;
+       DBusError error;
+       const char *path;
+
+       DBG("task %p", task);
+
+       reply = dbus_pending_call_steal_reply(call);
+       if (reply == NULL)
+               return;
+
+       if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
+               add_interface(task);
+               goto done;
+       }
+
        dbus_error_init(&error);
 
        if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
@@ -132,8 +227,7 @@ static int get_interface(struct supplicant_task *task)
                        dbus_error_free(&error);
                } else
                        connman_error("Wrong arguments for interface");
-               dbus_message_unref(reply);
-               return -EIO;
+               goto done;
        }
 
        DBG("path %s", path);
@@ -141,65 +235,129 @@ static int get_interface(struct supplicant_task *task)
        task->path = g_strdup(path);
        task->created = FALSE;
 
-       dbus_message_unref(reply);
+       connman_device_set_powered(task->device, TRUE);
 
-       return 0;
+done:
+       dbus_message_unref(reply);
 }
 
-static int add_interface(struct supplicant_task *task)
+static int create_interface(struct supplicant_task *task)
 {
-       DBusMessage *message, *reply;
-       DBusError error;
-       const char *path;
+       DBusMessage *message;
+       DBusPendingCall *call;
 
        DBG("task %p", task);
 
        message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
-                                       SUPPLICANT_INTF, "addInterface");
+                                       SUPPLICANT_INTF, "getInterface");
        if (message == NULL)
                return -ENOMEM;
 
-       dbus_error_init(&error);
-
        dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
                                                        DBUS_TYPE_INVALID);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
-                                                       message, -1, &error);
-       if (reply == NULL) {
-               if (dbus_error_is_set(&error) == TRUE) {
-                       connman_error("%s", error.message);
-                       dbus_error_free(&error);
-               } else
-                       connman_error("Failed to add interface");
+       if (dbus_connection_send_with_reply(connection, message,
+                                               &call, TIMEOUT) == FALSE) {
+               connman_error("Failed to get interface");
+               dbus_message_unref(message);
+               return -EIO;
+       }
+
+       dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
+
+       dbus_message_unref(message);
+
+       return -EINPROGRESS;
+}
+
+static void remove_interface_reply(DBusPendingCall *call, void *user_data)
+{
+       struct supplicant_task *task = user_data;
+       DBusMessage *reply;
+
+       DBG("task %p", task);
+
+       reply = dbus_pending_call_steal_reply(call);
+
+       connman_device_set_powered(task->device, FALSE);
+
+       free_task(task);
+
+       dbus_message_unref(reply);
+}
+
+static int remove_interface(struct supplicant_task *task)
+{
+       DBusMessage *message;
+       DBusPendingCall *call;
+
+       DBG("task %p", task);
+
+       if (task->created == FALSE) {
+               connman_device_set_powered(task->device, FALSE);
+               return 0;
+       }
+
+       message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
+                                       SUPPLICANT_INTF, "removeInterface");
+       if (message == NULL)
+               return -ENOMEM;
+
+       dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
+                                                       DBUS_TYPE_INVALID);
+
+       if (dbus_connection_send_with_reply(connection, message,
+                                               &call, TIMEOUT) == FALSE) {
+               connman_error("Failed to remove interface");
                dbus_message_unref(message);
                return -EIO;
        }
 
+       dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
+
        dbus_message_unref(message);
 
+       return -EINPROGRESS;
+}
+
+#if 0
+static int set_ap_scan(struct supplicant_task *task)
+{
+       DBusMessage *message, *reply;
+       DBusError error;
+       guint32 ap_scan = 1;
+
+       DBG("task %p", task);
+
+       message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
+                               SUPPLICANT_INTF ".Interface", "setAPScan");
+       if (message == NULL)
+               return -ENOMEM;
+
+       dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
+                                                       DBUS_TYPE_INVALID);
+
        dbus_error_init(&error);
 
-       if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
-                                               DBUS_TYPE_INVALID) == FALSE) {
+       reply = dbus_connection_send_with_reply_and_block(connection,
+                                                       message, -1, &error);
+       if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
                        connman_error("%s", error.message);
                        dbus_error_free(&error);
                } else
-                       connman_error("Wrong arguments for interface");
-               dbus_message_unref(reply);
+                       connman_error("Failed to set AP scan");
+               dbus_message_unref(message);
                return -EIO;
        }
 
-       DBG("path %s", path);
-
-       task->path = g_strdup(path);
-       task->created = TRUE;
+       dbus_message_unref(message);
 
        dbus_message_unref(reply);
 
        return 0;
 }
+#endif
 
 static int add_network(struct supplicant_task *task)
 {
@@ -209,6 +367,9 @@ static int add_network(struct supplicant_task *task)
 
        DBG("task %p", task);
 
+       if (task->network != NULL)
+               return -EALREADY;
+
        message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
                                SUPPLICANT_INTF ".Interface", "addNetwork");
        if (message == NULL)
@@ -216,7 +377,7 @@ static int add_network(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -259,6 +420,9 @@ static int remove_network(struct supplicant_task *task)
 
        DBG("task %p", task);
 
+       if (task->network == NULL)
+               return -EINVAL;
+
        message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
                                SUPPLICANT_INTF ".Interface", "removeNetwork");
        if (message == NULL)
@@ -269,7 +433,7 @@ static int remove_network(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -285,6 +449,9 @@ static int remove_network(struct supplicant_task *task)
 
        dbus_message_unref(reply);
 
+       g_free(task->network);
+       task->network = NULL;
+
        return 0;
 }
 
@@ -295,6 +462,9 @@ static int select_network(struct supplicant_task *task)
 
        DBG("task %p", task);
 
+       if (task->network == NULL)
+               return -EINVAL;
+
        message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
                                SUPPLICANT_INTF ".Interface", "selectNetwork");
        if (message == NULL)
@@ -305,7 +475,7 @@ static int select_network(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -331,6 +501,9 @@ static int enable_network(struct supplicant_task *task)
 
        DBG("task %p", task);
 
+       if (task->network == NULL)
+               return -EINVAL;
+
        message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
                                        SUPPLICANT_INTF ".Network", "enable");
        if (message == NULL)
@@ -338,7 +511,7 @@ static int enable_network(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -364,6 +537,9 @@ static int disable_network(struct supplicant_task *task)
 
        DBG("task %p", task);
 
+       if (task->network == NULL)
+               return -EINVAL;
+
        message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
                                        SUPPLICANT_INTF ".Network", "disable");
        if (message == NULL)
@@ -371,7 +547,7 @@ static int disable_network(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -390,39 +566,9 @@ static int disable_network(struct supplicant_task *task)
        return 0;
 }
 
-static void append_entry(DBusMessageIter *dict,
-                               const char *key, int type, void *val)
-{
-       DBusMessageIter entry, value;
-       const char *signature;
-
-       dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
-                                                               NULL, &entry);
-
-       dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
-
-       switch (type) {
-       case DBUS_TYPE_STRING:
-               signature = DBUS_TYPE_STRING_AS_STRING;
-               break;
-       case DBUS_TYPE_UINT16:
-               signature = DBUS_TYPE_UINT16_AS_STRING;
-               break;
-       default:
-               signature = DBUS_TYPE_VARIANT_AS_STRING;
-               break;
-       }
-
-       dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
-                                                       signature, &value);
-       dbus_message_iter_append_basic(&value, type, val);
-       dbus_message_iter_close_container(&entry, &value);
-
-       dbus_message_iter_close_container(dict, &entry);
-}
-
-static int set_network(struct supplicant_task *task, const char *network,
-                                               const char *passphrase)
+static int set_network(struct supplicant_task *task,
+                               const unsigned char *network, int len,
+                               const char *security, const char *passphrase)
 {
        DBusMessage *message, *reply;
        DBusMessageIter array, dict;
@@ -430,6 +576,9 @@ static int set_network(struct supplicant_task *task, const char *network,
 
        DBG("task %p", task);
 
+       if (task->network == NULL)
+               return -EINVAL;
+
        message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
                                        SUPPLICANT_INTF ".Network", "set");
        if (message == NULL)
@@ -442,22 +591,59 @@ static int set_network(struct supplicant_task *task, const char *network,
                        DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
                        DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
 
-       append_entry(&dict, "ssid", DBUS_TYPE_STRING, &network);
+       connman_dbus_dict_append_array(&dict, "ssid",
+                                       DBUS_TYPE_BYTE, &network, len);
 
-       if (passphrase && strlen(passphrase) > 0) {
+       if (g_ascii_strcasecmp(security, "wpa") == 0 ||
+                               g_ascii_strcasecmp(security, "wpa2") == 0) {
                const char *key_mgmt = "WPA-PSK";
-               append_entry(&dict, "key_mgmt", DBUS_TYPE_STRING, &key_mgmt);
-               append_entry(&dict, "psk", DBUS_TYPE_STRING, &passphrase);
+               connman_dbus_dict_append_variant(&dict, "key_mgmt",
+                                               DBUS_TYPE_STRING, &key_mgmt);
+
+               if (passphrase && strlen(passphrase) > 0)
+                       connman_dbus_dict_append_variant(&dict, "psk",
+                                               DBUS_TYPE_STRING, &passphrase);
+       } else if (g_ascii_strcasecmp(security, "wep") == 0) {
+               const char *key_mgmt = "NONE", *index = "0";
+               connman_dbus_dict_append_variant(&dict, "key_mgmt",
+                                               DBUS_TYPE_STRING, &key_mgmt);
+
+               if (passphrase) {
+                       int size = strlen(passphrase);
+                       if (size == 10 || size == 26) {
+                               unsigned char *key = malloc(13);
+                               char tmp[3];
+                               int i;
+                               memset(tmp, 0, sizeof(tmp));
+                               if (key == NULL)
+                                       size = 0;
+                               for (i = 0; i < size / 2; i++) {
+                                       memcpy(tmp, passphrase + (i * 2), 2);
+                                       key[i] = (unsigned char) strtol(tmp,
+                                                               NULL, 16);
+                               }
+                               connman_dbus_dict_append_array(&dict,
+                                               "wep_key0", DBUS_TYPE_BYTE,
+                                                       &key, size / 2);
+                               free(key);
+                       } else
+                               connman_dbus_dict_append_variant(&dict,
+                                               "wep_key0", DBUS_TYPE_STRING,
+                                                               &passphrase);
+                       connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
+                                               DBUS_TYPE_STRING, &index);
+               }
        } else {
                const char *key_mgmt = "NONE";
-               append_entry(&dict, "key_mgmt", DBUS_TYPE_STRING, &key_mgmt);
+               connman_dbus_dict_append_variant(&dict, "key_mgmt",
+                                               DBUS_TYPE_STRING, &key_mgmt);
        }
 
        dbus_message_iter_close_container(&array, &dict);
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -478,8 +664,8 @@ static int set_network(struct supplicant_task *task, const char *network,
 
 static int initiate_scan(struct supplicant_task *task)
 {
-       DBusMessage *message, *reply;
-       DBusError error;
+       DBusMessage *message;
+       DBusPendingCall *call;
 
        DBG("task %p", task);
 
@@ -488,28 +674,20 @@ static int initiate_scan(struct supplicant_task *task)
        if (message == NULL)
                return -ENOMEM;
 
-       dbus_error_init(&error);
-
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
-                                                       message, -1, &error);
-       if (reply == NULL) {
-               if (dbus_error_is_set(&error) == TRUE) {
-                       connman_error("%s", error.message);
-                       dbus_error_free(&error);
-               } else
-                       connman_error("Failed to initiate scan");
+       if (dbus_connection_send_with_reply(connection, message,
+                                               &call, TIMEOUT) == FALSE) {
+               connman_error("Failed to initiate scan");
                dbus_message_unref(message);
                return -EIO;
        }
 
        dbus_message_unref(message);
 
-       dbus_message_unref(reply);
-
        return 0;
 }
 
-static void extract_ssid(struct supplicant_ap *ap, DBusMessageIter *value)
+static void extract_ssid(struct supplicant_network *network,
+                                               DBusMessageIter *value)
 {
        DBusMessageIter array;
        unsigned char *ssid;
@@ -518,10 +696,25 @@ static void extract_ssid(struct supplicant_ap *ap, DBusMessageIter *value)
        dbus_message_iter_recurse(value, &array);
        dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
 
-       ap->identifier = g_strdup((char *) ssid);
+       if (ssid_len < 1)
+               return;
+
+       network->ssid = g_try_malloc(ssid_len);
+       if (network->ssid == NULL)
+               return;
+
+       memcpy(network->ssid, ssid, ssid_len);
+       network->ssid_len = ssid_len;
+
+       network->identifier = g_try_malloc0(ssid_len + 1);
+       if (network->identifier == NULL)
+               return;
+
+       memcpy(network->identifier, ssid, ssid_len);
 }
 
-static void extract_wpaie(struct supplicant_ap *ap, DBusMessageIter *value)
+static void extract_wpaie(struct supplicant_network *network,
+                                               DBusMessageIter *value)
 {
        DBusMessageIter array;
        unsigned char *ie;
@@ -531,10 +724,11 @@ static void extract_wpaie(struct supplicant_ap *ap, DBusMessageIter *value)
        dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
 
        if (ie_len > 0)
-               ap->has_wpa = TRUE;
+               network->has_wpa = TRUE;
 }
 
-static void extract_rsnie(struct supplicant_ap *ap, DBusMessageIter *value)
+static void extract_rsnie(struct supplicant_network *network,
+                                               DBusMessageIter *value)
 {
        DBusMessageIter array;
        unsigned char *ie;
@@ -544,36 +738,39 @@ static void extract_rsnie(struct supplicant_ap *ap, DBusMessageIter *value)
        dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
 
        if (ie_len > 0)
-               ap->has_rsn = TRUE;
+               network->has_rsn = TRUE;
 }
 
-static void extract_capabilites(struct supplicant_ap *ap,
+static void extract_capabilites(struct supplicant_network *network,
                                                DBusMessageIter *value)
 {
-       guint capabilities;
+       dbus_message_iter_get_basic(value, &network->capabilities);
 
-       dbus_message_iter_get_basic(value, &capabilities);
+       if (network->capabilities & IEEE80211_CAP_ESS)
+               network->adhoc = FALSE;
+       else if (network->capabilities & IEEE80211_CAP_IBSS)
+               network->adhoc = TRUE;
 
-       ap->capabilities = capabilities;
-
-       if (capabilities & IEEE80211_CAP_PRIVACY)
-               ap->has_wep = TRUE;
+       if (network->capabilities & IEEE80211_CAP_PRIVACY)
+               network->has_wep = TRUE;
 }
 
-static int parse_network_properties(struct supplicant_task *task,
-                                                       DBusMessage *message)
+static void properties_reply(DBusPendingCall *call, void *user_data)
 {
+       struct supplicant_task *task = user_data;
+       struct supplicant_network *network;
+       DBusMessage *reply;
        DBusMessageIter array, dict;
-       struct supplicant_ap *ap;
-       int security = 0;
 
        DBG("task %p", task);
 
-       ap = g_try_new0(struct supplicant_ap, 1);
-       if (ap == NULL)
-               return -ENOMEM;
+       reply = dbus_pending_call_steal_reply(call);
 
-       dbus_message_iter_init(message, &array);
+       network = g_try_new0(struct supplicant_network, 1);
+       if (network == NULL)
+               goto done;
+
+       dbus_message_iter_init(reply, &array);
 
        dbus_message_iter_recurse(&array, &dict);
 
@@ -591,42 +788,55 @@ static int parse_network_properties(struct supplicant_task *task,
                //type = dbus_message_iter_get_arg_type(&value);
                //dbus_message_iter_get_basic(&value, &val);
 
+               /* 
+                * bssid        : a (97)
+                * ssid         : a (97)
+                * wpaie        : a (97)
+                * rsnie        : a (97)
+                * frequency    : i (105)
+                * capabilities : q (113)
+                * quality      : i (105)
+                * noise        : i (105)
+                * level        : i (105)
+                * maxrate      : i (105)
+                */
+
                if (g_str_equal(key, "ssid") == TRUE)
-                       extract_ssid(ap, &value);
+                       extract_ssid(network, &value);
                else if (g_str_equal(key, "wpaie") == TRUE)
-                       extract_wpaie(ap, &value);
+                       extract_wpaie(network, &value);
                else if (g_str_equal(key, "rsnie") == TRUE)
-                       extract_rsnie(ap, &value);
+                       extract_rsnie(network, &value);
                else if (g_str_equal(key, "capabilities") == TRUE)
-                       extract_capabilites(ap, &value);
+                       extract_capabilites(network, &value);
+               else if (g_str_equal(key, "quality") == TRUE)
+                       dbus_message_iter_get_basic(&value, &network->quality);
+               else if (g_str_equal(key, "noise") == TRUE)
+                       dbus_message_iter_get_basic(&value, &network->noise);
+               else if (g_str_equal(key, "level") == TRUE)
+                       dbus_message_iter_get_basic(&value, &network->level);
+               else if (g_str_equal(key, "maxrate") == TRUE)
+                       dbus_message_iter_get_basic(&value, &network->maxrate);
+
 
                dbus_message_iter_next(&dict);
        }
 
-       DBG("SSID %s", ap->identifier);
-
-       if (ap->has_wep)
-               security |= 0x01;
-       if (ap->has_wpa)
-               security |= 0x02;
-       if (ap->has_rsn)
-               security |= 0x04;
-
-       connman_iface_indicate_station(task->iface,
-                                       ap->identifier, 25, security);
+       process_scan_result(task->device, network);
 
-       g_free(ap);
+       g_free(network->identifier);
+       g_free(network->ssid);
+       g_free(network);
 
-       return 0;
+done:
+       dbus_message_unref(reply);
 }
 
 static int get_network_properties(struct supplicant_task *task,
                                                        const char *path)
 {
-       DBusMessage *message, *reply;
-       DBusError error;
-
-       DBG("task %p", task);
+       DBusMessage *message;
+       DBusPendingCall *call;
 
        message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
                                                SUPPLICANT_INTF ".BSSID",
@@ -634,59 +844,31 @@ static int get_network_properties(struct supplicant_task *task,
        if (message == NULL)
                return -ENOMEM;
 
-       dbus_error_init(&error);
-
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
-                                                       message, -1, &error);
-       if (reply == NULL) {
-               if (dbus_error_is_set(&error) == TRUE) {
-                       connman_error("%s", error.message);
-                       dbus_error_free(&error);
-               } else
-                       connman_error("Failed to get network properties");
+       if (dbus_connection_send_with_reply(connection, message,
+                                               &call, TIMEOUT) == FALSE) {
+               connman_error("Failed to get network properties");
                dbus_message_unref(message);
                return -EIO;
        }
 
-       dbus_message_unref(message);
-
-       parse_network_properties(task, reply);
+       dbus_pending_call_set_notify(call, properties_reply, task, NULL);
 
-       dbus_message_unref(reply);
+       dbus_message_unref(message);
 
        return 0;
 }
 
-static int scan_results_available(struct supplicant_task *task)
+static void scan_results_reply(DBusPendingCall *call, void *user_data)
 {
-       DBusMessage *message, *reply;
+       struct supplicant_task *task = user_data;
+       DBusMessage *reply;
        DBusError error;
        char **results;
        int i, num_results;
 
        DBG("task %p", task);
 
-       message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
-                                               SUPPLICANT_INTF ".Interface",
-                                                       "scanResults");
-       if (message == NULL)
-               return -ENOMEM;
-
-       dbus_error_init(&error);
-
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
-                                                       message, -1, &error);
-       if (reply == NULL) {
-               if (dbus_error_is_set(&error) == TRUE) {
-                       connman_error("%s", error.message);
-                       dbus_error_free(&error);
-               } else
-                       connman_error("Failed to request scan result");
-               dbus_message_unref(message);
-               return -EIO;
-       }
-
-       dbus_message_unref(message);
+       reply = dbus_pending_call_steal_reply(call);
 
        dbus_error_init(&error);
 
@@ -699,16 +881,43 @@ static int scan_results_available(struct supplicant_task *task)
                        dbus_error_free(&error);
                } else
                        connman_error("Wrong arguments for scan result");
-               dbus_message_unref(reply);
-               return -EIO;
+               goto done;
        }
 
+       process_clear_results(task->device);
+
        for (i = 0; i < num_results; i++)
                get_network_properties(task, results[i]);
 
        g_strfreev(results);
 
+done:
        dbus_message_unref(reply);
+}
+
+static int scan_results_available(struct supplicant_task *task)
+{
+       DBusMessage *message;
+       DBusPendingCall *call;
+
+       DBG("task %p", task);
+
+       message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
+                                               SUPPLICANT_INTF ".Interface",
+                                                       "scanResults");
+       if (message == NULL)
+               return -ENOMEM;
+
+       if (dbus_connection_send_with_reply(connection, message,
+                                               &call, TIMEOUT) == FALSE) {
+               connman_error("Failed to request scan result");
+               dbus_message_unref(message);
+               return -EIO;
+       }
+
+       dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
+
+       dbus_message_unref(message);
 
        return 0;
 }
@@ -749,13 +958,26 @@ static void state_change(struct supplicant_task *task, DBusMessage *msg)
                task->state = STATE_COMPLETED;
        else if (g_str_equal(state, "DISCONNECTED") == TRUE)
                task->state = STATE_DISCONNECTED;
+
+       process_state_change(task->device, task->state);
+
+       switch (task->state) {
+       case STATE_COMPLETED:
+               /* carrier on */
+               break;
+       case STATE_DISCONNECTED:
+               /* carrier off */
+               break;
+       default:
+               break;
+       }
 }
 
 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
                                                DBusMessage *msg, void *data)
 {
-       struct supplicant_task *task = data;
-       const char *member;
+       struct supplicant_task *task;
+       const char *member, *path;
 
        if (dbus_message_has_interface(msg,
                                SUPPLICANT_INTF ".Interface") == FALSE)
@@ -765,6 +987,14 @@ static DBusHandlerResult supplicant_filter(DBusConnection *conn,
        if (member == NULL)
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
+       path = dbus_message_get_path(msg);
+       if (path == NULL)
+               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+       task = find_task_by_path(path);
+       if (task == NULL)
+               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
        DBG("task %p member %s", task, member);
 
        if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
@@ -775,209 +1005,249 @@ static DBusHandlerResult supplicant_filter(DBusConnection *conn,
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
-static int add_filter(struct supplicant_task *task)
+int supplicant_start(struct connman_device *device)
 {
-       DBusError error;
-       gchar *filter;
-
-       if (dbus_connection_add_filter(task->conn,
-                               supplicant_filter, task, NULL) == FALSE)
-               return -EIO;
+       struct supplicant_task *task;
 
-       filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
-                                               SUPPLICANT_INTF, task->path);
+       DBG("device %p", device);
 
-       DBG("filter %s", filter);
+       task = g_try_new0(struct supplicant_task, 1);
+       if (task == NULL)
+               return -ENOMEM;
 
-       dbus_error_init(&error);
+       task->ifindex = connman_device_get_index(device);
+       task->ifname = inet_index2name(task->ifindex);
+       task->device = device;
 
-       dbus_bus_add_match(task->conn, filter, &error);
+       if (task->ifname == NULL) {
+               g_free(task);
+               return -ENOMEM;
+       }
 
-       g_free(filter);
+       task->created = FALSE;
+       task->state = STATE_INACTIVE;
 
-       if (dbus_error_is_set(&error) == TRUE) {
-               connman_error("Can't add match: %s", error.message);
-               dbus_error_free(&error);
-       }
+       task_list = g_slist_append(task_list, task);
 
-       return 0;
+       return create_interface(task);
 }
 
-static int remove_filter(struct supplicant_task *task)
+int supplicant_stop(struct connman_device *device)
 {
-       DBusError error;
-       gchar *filter;
+       int index = connman_device_get_index(device);
+       struct supplicant_task *task;
 
-       filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
-                                               SUPPLICANT_INTF, task->path);
+       DBG("device %p", device);
 
-       DBG("filter %s", filter);
+       task = find_task_by_index(index);
+       if (task == NULL)
+               return -ENODEV;
 
-       dbus_error_init(&error);
+       task_list = g_slist_remove(task_list, task);
+
+       disable_network(task);
+
+       remove_network(task);
+
+       return remove_interface(task);
+}
+
+int supplicant_scan(struct connman_device *device)
+{
+       int index = connman_device_get_index(device);
+       struct supplicant_task *task;
+       int err;
 
-       dbus_bus_add_match(task->conn, filter, &error);
+       DBG("device %p", device);
 
-       g_free(filter);
+       task = find_task_by_index(index);
+       if (task == NULL)
+               return -ENODEV;
 
-       if (dbus_error_is_set(&error) == TRUE) {
-               connman_error("Can't add match: %s", error.message);
-               dbus_error_free(&error);
+       switch (task->state) {
+       case STATE_SCANNING:
+               return -EALREADY;
+       case STATE_ASSOCIATING:
+       case STATE_ASSOCIATED:
+       case STATE_4WAY_HANDSHAKE:
+       case STATE_GROUP_HANDSHAKE:
+               return -EBUSY;
+       default:
+               break;
        }
 
-       dbus_connection_remove_filter(task->conn, supplicant_filter, task);
+       err = initiate_scan(task);
 
        return 0;
 }
 
-int __supplicant_start(struct connman_iface *iface)
+int __supplicant_connect(struct connman_element *element,
+                               const unsigned char *ssid, int ssid_len,
+                               const char *security, const char *passphrase)
 {
-       struct ifreq ifr;
        struct supplicant_task *task;
-       int sk, err;
 
-       sk = socket(PF_INET, SOCK_DGRAM, 0);
-       if (sk < 0)
-               return -EIO;
+       DBG("element %p", element);
 
-       memset(&ifr, 0, sizeof(ifr));
-       ifr.ifr_ifindex = iface->index;
+       task = find_task_by_index(element->index);
+       if (task == NULL)
+               return -ENODEV;
 
-       err = ioctl(sk, SIOCGIFNAME, &ifr);
+       add_network(task);
 
-       close(sk);
+       select_network(task);
+       disable_network(task);
 
-       if (err < 0)
-               return -EIO;
+       set_network(task, ssid, ssid_len, security, passphrase);
 
-       DBG("interface %s", ifr.ifr_name);
+       enable_network(task);
 
-       task = g_try_new0(struct supplicant_task, 1);
-       if (task == NULL)
-               return -ENOMEM;
+       return 0;
+}
 
-       task->ifindex = iface->index;
-       task->ifname = g_strdup(ifr.ifr_name);
-       task->iface = iface;
+int __supplicant_disconnect(struct connman_element *element)
+{
+       struct supplicant_task *task;
 
-       if (task->ifname == NULL) {
-               g_free(task);
-               return -ENOMEM;
-       }
+       DBG("element %p", element);
 
-       task->conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
-       if (task->conn == NULL) {
-               g_free(task);
-               return -EIO;
-       }
+       task = find_task_by_index(element->index);
+       if (task == NULL)
+               return -ENODEV;
 
-       task->created = FALSE;
+       disable_network(task);
 
-       err = get_interface(task);
-       if (err < 0) {
-               err = add_interface(task);
-               if (err < 0) {
-                       g_free(task);
-                       return err;
-               }
-       }
+       remove_network(task);
 
-       task->state = STATE_INACTIVE;
+       return 0;
+}
+
+static void supplicant_activate(DBusConnection *conn)
+{
+       DBusMessage *message;
 
-       tasks = g_slist_append(tasks, task);
+       DBG("conn %p", conn);
 
-       add_filter(task);
+       message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
+                               DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
+       if (message == NULL)
+               return;
 
-       add_network(task);
+       dbus_message_set_no_reply(message, TRUE);
 
-       select_network(task);
-       disable_network(task);
+       dbus_connection_send(conn, message, NULL);
 
-       return 0;
+       dbus_message_unref(message);
 }
 
-int __supplicant_stop(struct connman_iface *iface)
+static void supplicant_probe(DBusConnection *conn, void *user_data)
 {
-       struct supplicant_task *task;
+       GSList *list;
 
-       task = find_task(iface->index);
-       if (task == NULL)
-               return -ENODEV;
+       DBG("conn %p", conn);
 
-       DBG("interface %s", task->ifname);
+       for (list = driver_list; list; list = list->next) {
+               struct supplicant_driver *driver = list->data;
 
-       tasks = g_slist_remove(tasks, task);
+               DBG("driver %p name %s", driver, driver->name);
 
-       remove_filter(task);
+               if (driver->probe)
+                       driver->probe();
+       }
+}
 
-       remove_network(task);
+static void supplicant_remove(DBusConnection *conn, void *user_data)
+{
+       GSList *list;
 
-       dbus_connection_unref(task->conn);
+       DBG("conn %p", conn);
 
-       g_free(task->ifname);
-       g_free(task->network);
-       g_free(task->path);
-       g_free(task);
+       for (list = driver_list; list; list = list->next) {
+               struct supplicant_driver *driver = list->data;
 
-       return 0;
+               DBG("driver %p name %s", driver, driver->name);
+
+               if (driver->remove)
+                       driver->remove();
+       }
 }
 
-int __supplicant_scan(struct connman_iface *iface)
+static const char *supplicant_rule = "type=signal,"
+                               "interface=" SUPPLICANT_INTF ".Interface";
+static guint watch;
+
+static int supplicant_create(void)
 {
-       struct supplicant_task *task;
-       int err;
+       if (g_slist_length(driver_list) > 0)
+               return 0;
 
-       task = find_task(iface->index);
-       if (task == NULL)
-               return -ENODEV;
+       connection = connman_dbus_get_connection();
+       if (connection == NULL)
+               return -EIO;
 
-       DBG("interface %s", task->ifname);
+       DBG("connection %p", connection);
 
-       switch (task->state) {
-       case STATE_SCANNING:
-               return -EALREADY;
-       case STATE_ASSOCIATING:
-       case STATE_ASSOCIATED:
-       case STATE_4WAY_HANDSHAKE:
-       case STATE_GROUP_HANDSHAKE:
-               return -EBUSY;
-       default:
-               break;
+       if (dbus_connection_add_filter(connection,
+                               supplicant_filter, NULL, NULL) == FALSE) {
+               connection = connman_dbus_get_connection();
+               return -EIO;
        }
 
-       err = initiate_scan(task);
+       dbus_bus_add_match(connection, supplicant_rule, NULL);
+       dbus_connection_flush(connection);
+
+       watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
+                       supplicant_probe, supplicant_remove, NULL, NULL);
 
        return 0;
 }
 
-int __supplicant_connect(struct connman_iface *iface,
-                               const char *network, const char *passphrase)
+static void supplicant_destroy(void)
 {
-       struct supplicant_task *task;
+       if (g_slist_length(driver_list) > 0)
+               return;
 
-       task = find_task(iface->index);
-       if (task == NULL)
-               return -ENODEV;
+       DBG("connection %p", connection);
 
-       DBG("interface %s", task->ifname);
+       if (watch > 0)
+               g_dbus_remove_watch(connection, watch);
 
-       set_network(task, network, passphrase);
+       dbus_bus_remove_match(connection, supplicant_rule, NULL);
+       dbus_connection_flush(connection);
 
-       enable_network(task);
+       dbus_connection_remove_filter(connection, supplicant_filter, NULL);
 
-       return 0;
+       dbus_connection_unref(connection);
+       connection = NULL;
 }
 
-int __supplicant_disconnect(struct connman_iface *iface)
+int supplicant_register(struct supplicant_driver *driver)
 {
-       struct supplicant_task *task;
+       int err;
 
-       task = find_task(iface->index);
-       if (task == NULL)
-               return -ENODEV;
+       DBG("driver %p name %s", driver, driver->name);
 
-       DBG("interface %s", task->ifname);
+       err = supplicant_create();
+       if (err < 0)
+               return err;
 
-       disable_network(task);
+       driver_list = g_slist_append(driver_list, driver);
+
+       if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
+               supplicant_probe(connection, NULL);
+       else
+               supplicant_activate(connection);
 
        return 0;
 }
+
+void supplicant_unregister(struct supplicant_driver *driver)
+{
+       DBG("driver %p name %s", driver, driver->name);
+
+       supplicant_remove(connection, NULL);
+
+       driver_list = g_slist_remove(driver_list, driver);
+
+       supplicant_destroy();
+}