Use global supplicant filter matching rule
[connman] / plugins / supplicant.c
index 243ec1b..a51a517 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2008  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 <stdlib.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>
 
 #define IEEE80211_CAP_IBSS      0x0002
 #define IEEE80211_CAP_PRIVACY   0x0010
 
+static GSList *driver_list = NULL;
+
 struct supplicant_task {
        int ifindex;
        gchar *ifname;
-       struct connman_element *element;
+       struct connman_device *device;
        struct supplicant_callback *callback;
        gchar *path;
        gboolean created;
@@ -475,8 +480,8 @@ static int disable_network(struct supplicant_task *task)
 }
 
 static int set_network(struct supplicant_task *task,
-                                       const unsigned char *network, int len,
-                                                       const char *passphrase)
+                               const unsigned char *network, int len,
+                               const char *security, const char *passphrase)
 {
        DBusMessage *message, *reply;
        DBusMessageIter array, dict;
@@ -502,12 +507,45 @@ static int set_network(struct supplicant_task *task,
        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";
                connman_dbus_dict_append_variant(&dict, "key_mgmt",
                                                DBUS_TYPE_STRING, &key_mgmt);
-               connman_dbus_dict_append_variant(&dict, "psk",
+
+               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";
                connman_dbus_dict_append_variant(&dict, "key_mgmt",
@@ -619,13 +657,14 @@ static void extract_rsnie(struct supplicant_network *network,
 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;
 
-       network->capabilities = capabilities;
-
-       if (capabilities & IEEE80211_CAP_PRIVACY)
+       if (network->capabilities & IEEE80211_CAP_PRIVACY)
                network->has_wep = TRUE;
 }
 
@@ -662,6 +701,19 @@ static void properties_reply(DBusPendingCall *call, void *user_data)
                //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(network, &value);
                else if (g_str_equal(key, "wpaie") == TRUE)
@@ -670,12 +722,21 @@ static void properties_reply(DBusPendingCall *call, void *user_data)
                        extract_rsnie(network, &value);
                else if (g_str_equal(key, "capabilities") == TRUE)
                        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);
        }
 
        if (task->callback && task->callback->scan_result)
-               task->callback->scan_result(task->element, network);
+               task->callback->scan_result(task->device, network);
 
        g_free(network->identifier);
        g_free(network->ssid);
@@ -737,6 +798,9 @@ static void scan_results_reply(DBusPendingCall *call, void *user_data)
                goto done;
        }
 
+       if (task->callback && task->callback->clear_results)
+                       task->callback->clear_results(task->device);
+
        for (i = 0; i < num_results; i++)
                get_network_properties(task, results[i]);
 
@@ -811,7 +875,7 @@ static void state_change(struct supplicant_task *task, DBusMessage *msg)
                task->state = STATE_DISCONNECTED;
 
        if (task->callback && task->callback->state_change)
-               task->callback->state_change(task->element, task->state);
+               task->callback->state_change(task->device, task->state);
 
        switch (task->state) {
        case STATE_COMPLETED:
@@ -857,69 +921,21 @@ static DBusHandlerResult supplicant_filter(DBusConnection *conn,
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
-static int add_filter(struct supplicant_task *task)
-{
-       DBusError error;
-       gchar *filter;
-
-       filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
-                                               SUPPLICANT_INTF, task->path);
-
-       DBG("filter %s", filter);
-
-       dbus_error_init(&error);
-
-       dbus_bus_add_match(connection, filter, &error);
-
-       g_free(filter);
-
-       if (dbus_error_is_set(&error) == TRUE) {
-               connman_error("Can't add match: %s", error.message);
-               dbus_error_free(&error);
-       }
-
-       return 0;
-}
-
-static int remove_filter(struct supplicant_task *task)
-{
-       DBusError error;
-       gchar *filter;
-
-       filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
-                                               SUPPLICANT_INTF, task->path);
-
-       DBG("filter %s", filter);
-
-       dbus_error_init(&error);
-
-       dbus_bus_remove_match(connection, filter, &error);
-
-       g_free(filter);
-
-       if (dbus_error_is_set(&error) == TRUE) {
-               connman_error("Can't add match: %s", error.message);
-               dbus_error_free(&error);
-       }
-
-       return 0;
-}
-
-int __supplicant_start(struct connman_element *element,
+int __supplicant_start(struct connman_device *device,
                                        struct supplicant_callback *callback)
 {
        struct supplicant_task *task;
        int err;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("device %p", device);
 
        task = g_try_new0(struct supplicant_task, 1);
        if (task == NULL)
                return -ENOMEM;
 
-       task->ifindex = element->index;
-       task->ifname = inet_index2name(element->index);
-       task->element = element;
+       task->ifindex = connman_device_get_index(device);
+       task->ifname = inet_index2name(task->ifindex);
+       task->device = device;
        task->callback = callback;
 
        if (task->ifname == NULL) {
@@ -941,20 +957,19 @@ int __supplicant_start(struct connman_element *element,
                }
        }
 
-       add_filter(task);
-
        set_ap_scan(task);
 
        return 0;
 }
 
-int __supplicant_stop(struct connman_element *element)
+int __supplicant_stop(struct connman_device *device)
 {
+       int index = connman_device_get_index(device);
        struct supplicant_task *task;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("device %p", device);
 
-       task = find_task_by_index(element->index);
+       task = find_task_by_index(index);
        if (task == NULL)
                return -ENODEV;
 
@@ -964,8 +979,6 @@ int __supplicant_stop(struct connman_element *element)
 
        remove_network(task);
 
-       remove_filter(task);
-
        remove_interface(task);
 
        g_free(task->ifname);
@@ -975,14 +988,15 @@ int __supplicant_stop(struct connman_element *element)
        return 0;
 }
 
-int __supplicant_scan(struct connman_element *element)
+int __supplicant_scan(struct connman_device *device)
 {
+       int index = connman_device_get_index(device);
        struct supplicant_task *task;
        int err;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("device %p", device);
 
-       task = find_task_by_index(element->index);
+       task = find_task_by_index(index);
        if (task == NULL)
                return -ENODEV;
 
@@ -1005,11 +1019,11 @@ int __supplicant_scan(struct connman_element *element)
 
 int __supplicant_connect(struct connman_element *element,
                                const unsigned char *ssid, int ssid_len,
-                                                       const char *passphrase)
+                               const char *security, const char *passphrase)
 {
        struct supplicant_task *task;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("element %p", element);
 
        task = find_task_by_index(element->index);
        if (task == NULL)
@@ -1020,7 +1034,7 @@ int __supplicant_connect(struct connman_element *element,
        select_network(task);
        disable_network(task);
 
-       set_network(task, ssid, ssid_len, passphrase);
+       set_network(task, ssid, ssid_len, security, passphrase);
 
        enable_network(task);
 
@@ -1031,7 +1045,7 @@ int __supplicant_disconnect(struct connman_element *element)
 {
        struct supplicant_task *task;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("element %p", element);
 
        task = find_task_by_index(element->index);
        if (task == NULL)
@@ -1044,20 +1058,132 @@ int __supplicant_disconnect(struct connman_element *element)
        return 0;
 }
 
-int __supplicant_init(DBusConnection *conn)
+static void supplicant_activate(DBusConnection *conn)
 {
-       connection = conn;
+       DBusMessage *message;
+
+       DBG("conn %p", conn);
+
+       message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
+                               DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
+       if (message == NULL)
+               return;
+
+       dbus_message_set_no_reply(message, TRUE);
+
+       dbus_connection_send(conn, message, NULL);
+
+       dbus_message_unref(message);
+}
+
+static void supplicant_probe(DBusConnection *conn, void *user_data)
+{
+       GSList *list;
+
+       DBG("conn %p", conn);
+
+       for (list = driver_list; list; list = list->next) {
+               struct supplicant_driver *driver = list->data;
+
+               DBG("driver %p name %s", driver, driver->name);
+
+               if (driver->probe)
+                       driver->probe();
+       }
+}
+
+static void supplicant_remove(DBusConnection *conn, void *user_data)
+{
+       GSList *list;
+
+       DBG("conn %p", conn);
+
+       for (list = driver_list; list; list = list->next) {
+               struct supplicant_driver *driver = list->data;
+
+               DBG("driver %p name %s", driver, driver->name);
+
+               if (driver->remove)
+                       driver->remove();
+       }
+}
+
+static const char *supplicant_rule = "type=signal,"
+                               "interface=" SUPPLICANT_INTF ".Interface";
+static guint watch;
+
+static int supplicant_create(void)
+{
+       if (g_slist_length(driver_list) > 0)
+               return 0;
+
+       connection = connman_dbus_get_connection();
+       if (connection == NULL)
+               return -EIO;
+
+       DBG("connection %p", connection);
 
        if (dbus_connection_add_filter(connection,
                                supplicant_filter, NULL, NULL) == FALSE) {
-               dbus_connection_unref(connection);
+               connection = connman_dbus_get_connection();
                return -EIO;
        }
 
+       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;
 }
 
-void __supplicant_exit(void)
+static void supplicant_destroy(void)
 {
+       if (g_slist_length(driver_list) > 0)
+               return;
+
+       DBG("connection %p", connection);
+
+       if (watch > 0)
+               g_dbus_remove_watch(connection, watch);
+
+       dbus_bus_remove_match(connection, supplicant_rule, NULL);
+       dbus_connection_flush(connection);
+
        dbus_connection_remove_filter(connection, supplicant_filter, NULL);
+
+       dbus_connection_unref(connection);
+       connection = NULL;
+}
+
+int supplicant_register(struct supplicant_driver *driver)
+{
+       int err;
+
+       DBG("driver %p name %s", driver, driver->name);
+
+       err = supplicant_create();
+       if (err < 0)
+               return err;
+
+       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();
 }