Add support for setting network availability
[connman] / plugins / wifi.c
index 6ab5940..92e5787 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 <errno.h>
+
 #include <dbus/dbus.h>
+#include <glib.h>
 
+#define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/plugin.h>
-#include <connman/driver.h>
+#include <connman/device.h>
 #include <connman/log.h>
 
-static DBusConnection *connection;
-
-#define SUPPLICANT_NAME  "fi.epitest.hostap.WPASupplicant"
-#define SUPPLICANT_INTF  "fi.epitest.hostap.WPASupplicant"
-#define SUPPLICANT_PATH  "/fi/epitest/hostap/WPASupplicant"
-
-enum supplicant_state {
-       STATE_INACTIVE,
-       STATE_SCANNING,
-       STATE_ASSOCIATING,
-       STATE_ASSOCIATED,
-       STATE_4WAY_HANDSHAKE,
-       STATE_GROUP_HANDSHAKE,
-       STATE_COMPLETED,
-       STATE_DISCONNECTED,
-};
+#include "inet.h"
+#include "supplicant.h"
 
-struct supplicant_task {
-       int ifindex;
-       gchar *ifname;
-       enum supplicant_state state;
-       gchar *path;
-       gboolean created;
-};
+#define CLEANUP_TIMEOUT   8    /* in seconds */
+#define INACTIVE_TIMEOUT  12   /* in seconds */
 
-static GStaticMutex task_mutex = G_STATIC_MUTEX_INIT;
-static GSList *task_list = NULL;
+struct wifi_data {
+       char *identifier;
+       connman_bool_t connected;
+};
 
-static struct supplicant_task *find_task_by_index(int index)
+static int network_probe(struct connman_network *network)
 {
-       GSList *list;
-
-       for (list = task_list; list; list = list->next) {
-               struct supplicant_task *task = list->data;
+       DBG("network %p", network);
 
-               if (task->ifindex == index)
-                       return task;
-       }
-
-       return NULL;
+       return 0;
 }
 
-static int get_interface(struct supplicant_task *task)
+static void network_remove(struct connman_network *network)
 {
-       DBusMessage *message, *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;
-
-       dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
-                                                       DBUS_TYPE_INVALID);
-
-       dbus_error_init(&error);
-
-       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("Failed to get interface");
-               dbus_message_unref(message);
-               return -EIO;
-       }
-
-       dbus_message_unref(message);
-
-       dbus_error_init(&error);
-
-       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("Wrong arguments for interface");
-               dbus_message_unref(reply);
-               return -EIO;
-       }
-
-       DBG("path %s", path);
-
-       task->path = g_strdup(path);
-       task->created = FALSE;
-
-       dbus_message_unref(reply);
-
-       return 0;
+       DBG("network %p", network);
 }
 
-static int add_interface(struct supplicant_task *task)
+static int network_connect(struct connman_network *network)
 {
-       DBusMessage *message, *reply;
-       DBusError error;
-       const char *path;
-
-       DBG("task %p", task);
-
-       message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
-                                       SUPPLICANT_INTF, "addInterface");
-       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(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("Failed to add interface");
-               dbus_message_unref(message);
-               return -EIO;
-       }
+       DBG("network %p", network);
 
-       dbus_message_unref(message);
-
-       dbus_error_init(&error);
-
-       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("Wrong arguments for interface");
-               dbus_message_unref(reply);
-               return -EIO;
-       }
-
-       DBG("path %s", path);
-
-       task->path = g_strdup(path);
-       task->created = TRUE;
-
-       dbus_message_unref(reply);
-
-       return 0;
+       return supplicant_connect(network);
 }
 
-static int remove_interface(struct supplicant_task *task)
+static int network_disconnect(struct connman_network *network)
 {
-       DBusMessage *message, *reply;
-       DBusError error;
-
-       DBG("task %p", task);
+       DBG("network %p", network);
 
-       if (task->created == FALSE)
-               return -EINVAL;
-
-       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);
-
-       dbus_error_init(&error);
-
-       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("Failed to remove interface");
-               dbus_message_unref(message);
-               return -EIO;
-       }
-
-       dbus_message_unref(message);
-
-       g_free(task->path);
-       task->path = NULL;
-
-       dbus_message_unref(reply);
-
-       return 0;
+       return supplicant_disconnect(network);
 }
 
-static int initiate_scan(struct supplicant_task *task)
+static struct connman_network_driver network_driver = {
+       .name           = "wifi",
+       .type           = CONNMAN_NETWORK_TYPE_WIFI,
+       .probe          = network_probe,
+       .remove         = network_remove,
+       .connect        = network_connect,
+       .disconnect     = network_disconnect,
+};
+
+static int wifi_probe(struct connman_device *device)
 {
-       DBusMessage *message, *reply;
-       DBusError error;
+       struct wifi_data *data;
 
-       DBG("task %p", task);
+       DBG("device %p", device);
 
-       message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
-                                       SUPPLICANT_INTF ".Interface", "scan");
-       if (message == NULL)
+       data = g_try_new0(struct wifi_data, 1);
+       if (data == NULL)
                return -ENOMEM;
 
-       dbus_error_init(&error);
-
-       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("Failed to initiate scan");
-               dbus_message_unref(message);
-               return -EIO;
-       }
-
-       dbus_message_unref(message);
+       data->connected = FALSE;
 
-       dbus_message_unref(reply);
+       connman_device_set_data(device, data);
 
        return 0;
 }
 
-static int wifi_probe(struct connman_element *element)
+static void wifi_remove(struct connman_device *device)
 {
-       struct supplicant_task *task;
-       int err;
+       struct wifi_data *data = connman_device_get_data(device);
 
-       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;
+       connman_device_set_data(device, NULL);
 
-       task->ifindex = element->netdev.index;
-       task->ifname = g_strdup(element->netdev.name);
+       g_free(data->identifier);
+       g_free(data);
+}
 
-       if (task->ifname == NULL) {
-               g_free(task);
-               return -ENOMEM;
-       }
+static int wifi_enable(struct connman_device *device)
+{
+       DBG("device %p", device);
 
-       task->created = FALSE;
-       task->state = STATE_INACTIVE;
+       return supplicant_start(device);
+}
 
-       g_static_mutex_lock(&task_mutex);
-       task_list = g_slist_append(task_list, task);
-       g_static_mutex_unlock(&task_mutex);
+static int wifi_disable(struct connman_device *device)
+{
+       struct wifi_data *data = connman_device_get_data(device);
 
-       err = get_interface(task);
-       if (err < 0) {
-               err = add_interface(task);
-               if (err < 0) {
-                       g_free(task);
-                       return err;
-               }
-       }
+       DBG("device %p", device);
 
-       initiate_scan(task);
+       data->connected = FALSE;
 
-       return 0;
+       return supplicant_stop(device);
 }
 
-static void wifi_remove(struct connman_element *element)
+static int wifi_scan(struct connman_device *device)
 {
-       struct supplicant_task *task;
+       DBG("device %p", device);
 
-       DBG("element %p name %s", element, element->name);
+       return supplicant_scan(device);
+}
 
-       g_static_mutex_lock(&task_mutex);
-       task = find_task_by_index(element->netdev.index);
-       g_static_mutex_unlock(&task_mutex);
+static struct connman_device_driver wifi_driver = {
+       .name           = "wifi",
+       .type           = CONNMAN_DEVICE_TYPE_WIFI,
+       .probe          = wifi_probe,
+       .remove         = wifi_remove,
+       .enable         = wifi_enable,
+       .disable        = wifi_disable,
+       .scan           = wifi_scan,
+};
 
-       if (task == NULL)
-               return;
+static void wifi_register(void)
+{
+       DBG("");
 
-       g_static_mutex_lock(&task_mutex);
-       task_list = g_slist_remove(task_list, task);
-       g_static_mutex_unlock(&task_mutex);
+       if (connman_device_driver_register(&wifi_driver) < 0)
+               connman_error("Failed to register WiFi driver");
+}
 
-       remove_interface(task);
+static void wifi_unregister(void)
+{
+       DBG("");
 
-       g_free(task->ifname);
-       g_free(task);
+       connman_device_driver_unregister(&wifi_driver);
 }
 
-static struct connman_driver wifi_driver = {
+static struct supplicant_driver supplicant = {
        .name           = "wifi",
-       .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
-       .subtype        = CONNMAN_ELEMENT_SUBTYPE_WIFI,
-       .probe          = wifi_probe,
-       .remove         = wifi_remove,
+       .probe          = wifi_register,
+       .remove         = wifi_unregister,
 };
 
 static int wifi_init(void)
 {
        int err;
 
-       connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
-       if (connection == NULL)
-               return -EIO;
+       err = connman_network_driver_register(&network_driver);
+       if (err < 0)
+               return err;
 
-       err = connman_driver_register(&wifi_driver);
+       err = supplicant_register(&supplicant);
        if (err < 0) {
-               dbus_connection_unref(connection);
+               connman_network_driver_unregister(&network_driver);
                return err;
        }
 
@@ -346,10 +183,10 @@ static int wifi_init(void)
 
 static void wifi_exit(void)
 {
-       connman_driver_unregister(&wifi_driver);
+       supplicant_unregister(&supplicant);
 
-       dbus_connection_unref(connection);
+       connman_network_driver_unregister(&network_driver);
 }
 
-CONNMAN_PLUGIN_DEFINE("WiFi", "WiFi interface plugin", VERSION,
+CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
                                                        wifi_init, wifi_exit)