#include <stdlib.h>
#include <string.h>
-#include <dbus/dbus.h>
-#include <glib.h>
+#include <gdbus.h>
#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/log.h>
#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 {
int ifindex;
gchar *ifname;
struct connman_device *device;
- struct supplicant_callback *callback;
gchar *path;
gboolean created;
gchar *network;
static DBusConnection *connection;
+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_by_index(int index)
{
GSList *list;
return NULL;
}
-static int get_interface(struct supplicant_task *task)
+static void add_interface_reply(DBusPendingCall *call, void *user_data)
{
- DBusMessage *message, *reply;
+ 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;
-
- 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;
- }
+ reply = dbus_pending_call_steal_reply(call);
+ if (reply == NULL)
+ return;
- dbus_message_unref(message);
+ if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
+ goto done;
dbus_error_init(&error);
dbus_error_free(&error);
} else
connman_error("Wrong arguments for interface");
- dbus_message_unref(reply);
- return -EIO;
+ goto done;
}
DBG("path %s", path);
task->path = g_strdup(path);
- task->created = FALSE;
+ task->created = TRUE;
- 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)
{
- DBusMessage *message, *reply;
- DBusError error;
- const char *path;
+ DBusMessage *message;
+ DBusPendingCall *call;
DBG("task %p", task);
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");
+ 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,
dbus_error_free(&error);
} else
connman_error("Wrong arguments for interface");
- dbus_message_unref(reply);
- return -EIO;
+ goto done;
}
DBG("path %s", path);
task->path = g_strdup(path);
- task->created = TRUE;
+ task->created = FALSE;
+ connman_device_set_powered(task->device, TRUE);
+
+done:
dbus_message_unref(reply);
+}
- return 0;
+static int create_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, "getInterface");
+ 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 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, *reply;
- DBusError error;
+ DBusMessage *message;
+ DBusPendingCall *call;
DBG("task %p", task);
- if (task->created == FALSE)
- return -EINVAL;
+ 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");
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");
+ if (dbus_connection_send_with_reply(connection, message,
+ &call, TIMEOUT) == FALSE) {
+ connman_error("Failed to remove interface");
dbus_message_unref(message);
return -EIO;
}
- dbus_message_unref(message);
+ dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
- dbus_message_unref(reply);
+ dbus_message_unref(message);
- return 0;
+ return -EINPROGRESS;
}
+#if 0
static int set_ap_scan(struct supplicant_task *task)
{
DBusMessage *message, *reply;
return 0;
}
+#endif
static int add_network(struct supplicant_task *task)
{
dbus_message_iter_next(&dict);
}
- if (task->callback && task->callback->scan_result)
- task->callback->scan_result(task->device, network);
+ process_scan_result(task->device, network);
g_free(network->identifier);
g_free(network->ssid);
goto done;
}
- if (task->callback && task->callback->clear_results)
- task->callback->clear_results(task->device);
+ process_clear_results(task->device);
for (i = 0; i < num_results; i++)
get_network_properties(task, results[i]);
else if (g_str_equal(state, "DISCONNECTED") == TRUE)
task->state = STATE_DISCONNECTED;
- if (task->callback && task->callback->state_change)
- task->callback->state_change(task->device, task->state);
+ process_state_change(task->device, task->state);
switch (task->state) {
case STATE_COMPLETED:
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_device *device,
- struct supplicant_callback *callback)
+int supplicant_start(struct connman_device *device)
{
struct supplicant_task *task;
- int err;
DBG("device %p", device);
task->ifindex = connman_device_get_index(device);
task->ifname = inet_index2name(task->ifindex);
task->device = device;
- task->callback = callback;
if (task->ifname == NULL) {
g_free(task);
task_list = g_slist_append(task_list, task);
- err = get_interface(task);
- if (err < 0) {
- err = add_interface(task);
- if (err < 0) {
- g_free(task);
- return err;
- }
- }
-
- add_filter(task);
-
- set_ap_scan(task);
-
- return 0;
+ return create_interface(task);
}
-int __supplicant_stop(struct connman_device *device)
+int supplicant_stop(struct connman_device *device)
{
int index = connman_device_get_index(device);
struct supplicant_task *task;
remove_network(task);
- remove_filter(task);
-
- remove_interface(task);
-
- g_free(task->ifname);
- g_free(task->path);
- g_free(task);
-
- return 0;
+ return remove_interface(task);
}
-int __supplicant_scan(struct connman_device *device)
+int supplicant_scan(struct connman_device *device)
{
int index = connman_device_get_index(device);
struct supplicant_task *task;
return 0;
}
-void __supplicant_activate(DBusConnection *conn)
+static void supplicant_activate(DBusConnection *conn)
{
DBusMessage *message;
dbus_message_unref(message);
}
-int __supplicant_init(DBusConnection *conn)
+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);
- connection = 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)
{
- DBG("conn %p", connection);
+ 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();
}