Update copyright information
[connman] / plugins / supplicant.c
index b7d01cf..0d7d293 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 <glib.h>
+
+#define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/log.h>
 #include <connman/dbus.h>
 
@@ -41,7 +45,7 @@
 struct supplicant_task {
        int ifindex;
        gchar *ifname;
-       struct connman_element *element;
+       struct connman_device *device;
        struct supplicant_callback *callback;
        gchar *path;
        gboolean created;
@@ -502,12 +506,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",
@@ -621,6 +658,11 @@ static void extract_capabilites(struct supplicant_network *network,
 {
        dbus_message_iter_get_basic(value, &network->capabilities);
 
+       if (network->capabilities & IEEE80211_CAP_ESS)
+               network->adhoc = FALSE;
+       else if (network->capabilities & IEEE80211_CAP_IBSS)
+               network->adhoc = TRUE;
+
        if (network->capabilities & IEEE80211_CAP_PRIVACY)
                network->has_wep = TRUE;
 }
@@ -693,7 +735,7 @@ static void properties_reply(DBusPendingCall *call, void *user_data)
        }
 
        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);
@@ -755,6 +797,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]);
 
@@ -829,7 +874,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:
@@ -923,21 +968,21 @@ static int remove_filter(struct supplicant_task *task)
        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) {
@@ -966,13 +1011,14 @@ int __supplicant_start(struct connman_element *element,
        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;
 
@@ -993,14 +1039,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;
 
@@ -1027,7 +1074,7 @@ int __supplicant_connect(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)
@@ -1049,7 +1096,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)
@@ -1062,8 +1109,28 @@ int __supplicant_disconnect(struct connman_element *element)
        return 0;
 }
 
+void __supplicant_activate(DBusConnection *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);
+}
+
 int __supplicant_init(DBusConnection *conn)
 {
+       DBG("conn %p", conn);
+
        connection = conn;
 
        if (dbus_connection_add_filter(connection,
@@ -1077,5 +1144,7 @@ int __supplicant_init(DBusConnection *conn)
 
 void __supplicant_exit(void)
 {
+       DBG("conn %p", connection);
+
        dbus_connection_remove_filter(connection, supplicant_filter, NULL);
 }