*
* Connection Manager
*
- * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2010 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 <stdlib.h>
+#include <string.h>
+#include <netinet/ether.h>
#include <gdbus.h>
#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
+#include <connman/technology.h>
#include <connman/device.h>
+#include <connman/inet.h>
#include <connman/dbus.h>
#include <connman/log.h>
-#include "inet.h"
-
#define BLUEZ_SERVICE "org.bluez"
#define BLUEZ_MANAGER_INTERFACE BLUEZ_SERVICE ".Manager"
#define BLUEZ_ADAPTER_INTERFACE BLUEZ_SERVICE ".Adapter"
#define BLUEZ_DEVICE_INTERFACE BLUEZ_SERVICE ".Device"
#define BLUEZ_NETWORK_INTERFACE BLUEZ_SERVICE ".Network"
+#define BLUEZ_NETWORK_SERVER BLUEZ_SERVICE ".NetworkServer"
#define LIST_ADAPTERS "ListAdapters"
#define ADAPTER_ADDED "AdapterAdded"
#define ADAPTER_REMOVED "AdapterRemoved"
+#define DEVICE_REMOVED "DeviceRemoved"
#define PROPERTY_CHANGED "PropertyChanged"
#define GET_PROPERTIES "GetProperties"
#define CONNECT "Connect"
#define DISCONNECT "Disconnect"
-#define TIMEOUT 5000
-
-typedef void (* properties_callback_t) (DBusConnection *connection,
- const char *path,
- DBusMessage *message,
- void *user_data);
-
-struct properties_data {
- DBusConnection *connection;
- DBusMessage *message;
- properties_callback_t callback;
- void *user_data;
-};
-
-static void get_properties_reply(DBusPendingCall *call, void *user_data)
-{
- struct properties_data *data = user_data;
- DBusMessage *reply;
- const char *path;
-
- reply = dbus_pending_call_steal_reply(call);
- if (reply == NULL)
- goto done;
-
- path = dbus_message_get_path(data->message);
-
- data->callback(data->connection, path, reply, data->user_data);
-
- dbus_message_unref(reply);
-
-done:
- dbus_message_unref(data->message);
- g_free(data);
-}
-
-static void get_properties(DBusConnection *connection,
- const char *path, const char *interface,
- properties_callback_t callback, void *user_data)
-{
- struct properties_data *data;
- DBusMessage *message;
- DBusPendingCall *call;
-
- DBG("path %s interface %s", path, interface);
-
- data = g_try_new0(struct properties_data, 1);
- if (data == NULL)
- return;
-
- message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
- interface, GET_PROPERTIES);
- if (message == NULL) {
- g_free(data);
- return;
- }
-
- if (dbus_connection_send_with_reply(connection, message,
- &call, TIMEOUT) == FALSE) {
- connman_error("Failed to get properties for %s", interface);
- dbus_message_unref(message);
- g_free(data);
- return;
- }
+#define REGISTER "Register"
+#define UNREGISTER "Unregister"
- data->connection = connection;
- data->message = message;
- data->callback = callback;
- data->user_data = user_data;
+#define UUID_NAP "00001116-0000-1000-8000-00805f9b34fb"
- dbus_pending_call_set_notify(call, get_properties_reply, data, NULL);
-}
+#define TIMEOUT 5000
-struct adapter_data {
- DBusConnection *connection;
-};
+static DBusConnection *connection;
-struct network_data {
- DBusConnection *connection;
- char *interface;
-};
+static GHashTable *bluetooth_devices = NULL;
+static GHashTable *bluetooth_networks = NULL;
static int pan_probe(struct connman_network *network)
{
- struct connman_device *device = connman_network_get_device(network);
- struct adapter_data *adapter;
- struct network_data *data;
-
DBG("network %p", network);
- if (device == NULL)
- return -EINVAL;
-
- adapter = connman_device_get_data(device);
- if (adapter == NULL)
- return -EINVAL;
-
- data = g_try_new0(struct network_data, 1);
- if (data == NULL)
- return -ENOMEM;
-
- data->connection = adapter->connection;
-
- connman_network_set_data(network, data);
-
return 0;
}
static void pan_remove(struct connman_network *network)
{
- struct network_data *data = connman_network_get_data(network);
-
DBG("network %p", network);
-
- connman_network_set_data(network, NULL);
-
- g_free(data);
}
static void connect_reply(DBusPendingCall *call, void *user_data)
{
struct connman_network *network = user_data;
- struct network_data *data = connman_network_get_data(network);
DBusMessage *reply;
DBusError error;
const char *interface = NULL;
DBG("network %p", network);
reply = dbus_pending_call_steal_reply(call);
- if (reply == NULL)
- return;
dbus_error_init(&error);
+ if (dbus_set_error_from_message(&error, reply) == TRUE) {
+ connman_error("%s", error.message);
+ dbus_error_free(&error);
+
+ goto err;
+ }
+
if (dbus_message_get_args(reply, &error,
DBUS_TYPE_STRING, &interface,
DBUS_TYPE_INVALID) == FALSE) {
dbus_error_free(&error);
} else
connman_error("Wrong arguments for connect");
- goto done;
+ goto err;
}
if (interface == NULL)
- goto done;
+ goto err;
DBG("interface %s", interface);
- data->interface = g_strdup(interface);
-
- index = inet_name2index(interface);
+ index = connman_inet_ifindex(interface);
connman_network_set_index(network, index);
+
connman_network_set_connected(network, TRUE);
-done:
dbus_message_unref(reply);
+
+ dbus_pending_call_unref(call);
+
+ return;
+err:
+
+ connman_network_set_connected(network, FALSE);
+
+ dbus_message_unref(reply);
+
+ dbus_pending_call_unref(call);
}
static int pan_connect(struct connman_network *network)
{
- struct network_data *data = connman_network_get_data(network);
- const char *path = connman_network_get_string(network, "Node");
+ const char *path = connman_network_get_string(network, "Path");
const char *uuid = "nap";
DBusMessage *message;
DBusPendingCall *call;
DBG("network %p", network);
+ if (path == NULL)
+ return -EINVAL;
+
message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
BLUEZ_NETWORK_INTERFACE, CONNECT);
if (message == NULL)
return -ENOMEM;
+ dbus_message_set_auto_start(message, FALSE);
+
dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
DBUS_TYPE_INVALID);
- if (dbus_connection_send_with_reply(data->connection, message,
+ if (dbus_connection_send_with_reply(connection, message,
&call, TIMEOUT * 10) == FALSE) {
connman_error("Failed to connect service");
dbus_message_unref(message);
return -EINVAL;
}
+ if (call == NULL) {
+ connman_error("D-Bus connection not available");
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
dbus_pending_call_set_notify(call, connect_reply, network, NULL);
dbus_message_unref(message);
static void disconnect_reply(DBusPendingCall *call, void *user_data)
{
struct connman_network *network = user_data;
- struct network_data *data = connman_network_get_data(network);
DBusMessage *reply;
DBusError error;
DBG("network %p", network);
reply = dbus_pending_call_steal_reply(call);
- if (reply == NULL)
- return;
dbus_error_init(&error);
+ if (dbus_set_error_from_message(&error, reply) == TRUE) {
+ connman_error("%s", error.message);
+ dbus_error_free(&error);
+ goto done;
+ }
+
if (dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) == FALSE) {
if (dbus_error_is_set(&error) == TRUE) {
connman_error("%s", error.message);
goto done;
}
- g_free(data->interface);
- data->interface = NULL;
-
connman_network_set_connected(network, FALSE);
- connman_network_set_index(network, -1);
done:
dbus_message_unref(reply);
+
+ dbus_pending_call_unref(call);
+
+ connman_network_unref(network);
}
static int pan_disconnect(struct connman_network *network)
{
- struct network_data *data = connman_network_get_data(network);
- const char *path = connman_network_get_string(network, "Node");
+ const char *path = connman_network_get_string(network, "Path");
DBusMessage *message;
DBusPendingCall *call;
DBG("network %p", network);
- if (data->interface == NULL)
+ if (path == NULL)
return -EINVAL;
message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
if (message == NULL)
return -ENOMEM;
- dbus_message_append_args(message, DBUS_TYPE_STRING, &data->interface,
- DBUS_TYPE_INVALID);
+ dbus_message_set_auto_start(message, FALSE);
+
+ dbus_message_append_args(message, DBUS_TYPE_INVALID);
- if (dbus_connection_send_with_reply(data->connection, message,
+ if (dbus_connection_send_with_reply(connection, message,
&call, TIMEOUT) == FALSE) {
connman_error("Failed to disconnect service");
dbus_message_unref(message);
return -EINVAL;
}
+ if (call == NULL) {
+ connman_error("D-Bus connection not available");
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
+ connman_network_ref(network);
+
+ connman_network_set_associating(network, FALSE);
+
dbus_pending_call_set_notify(call, disconnect_reply, network, NULL);
dbus_message_unref(message);
- return -EINPROGRESS;
+ return 0;
}
static struct connman_network_driver pan_driver = {
.disconnect = pan_disconnect,
};
-static int bluetooth_probe(struct connman_device *adapter)
+static gboolean network_changed(DBusConnection *connection,
+ DBusMessage *message, void *user_data)
{
- struct adapter_data *data;
+ const char *path = dbus_message_get_path(message);
+ struct connman_network *network;
+ DBusMessageIter iter, value;
+ const char *key;
- DBG("adapter %p", adapter);
+ DBG("path %s", path);
- data = g_try_new0(struct adapter_data, 1);
- if (data == NULL)
- return -ENOMEM;
+ network = g_hash_table_lookup(bluetooth_networks, path);
+ if (network == NULL)
+ return TRUE;
- data->connection = connman_dbus_get_connection();
- if (data->connection == NULL) {
- g_free(data);
- return -EIO;
- }
+ if (dbus_message_iter_init(message, &iter) == FALSE)
+ return TRUE;
- connman_device_set_data(adapter, data);
+ dbus_message_iter_get_basic(&iter, &key);
- return 0;
-}
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &value);
-static void bluetooth_remove(struct connman_device *adapter)
-{
- struct adapter_data *data = connman_device_get_data(adapter);
+ if (g_str_equal(key, "Connected") == TRUE) {
+ dbus_bool_t connected;
- DBG("adapter %p", adapter);
+ dbus_message_iter_get_basic(&value, &connected);
- connman_device_set_data(adapter, NULL);
+ if (connected == TRUE)
+ return TRUE;
- dbus_connection_unref(data->connection);
+ connman_network_set_associating(network, FALSE);
+ connman_network_set_connected(network, FALSE);
+ }
- g_free(data);
+ return TRUE;
}
-static void powered_reply(DBusPendingCall *call, void *user_data)
+static void extract_properties(DBusMessage *reply, const char **parent,
+ const char **address,
+ const char **name,
+ const char **alias,
+ dbus_bool_t *powered,
+ dbus_bool_t *scanning,
+ DBusMessageIter *uuids,
+ DBusMessageIter *networks)
{
- DBusMessage *reply;
+ DBusMessageIter array, dict;
- DBG("");
+ if (dbus_message_iter_init(reply, &array) == FALSE)
+ return;
- reply = dbus_pending_call_steal_reply(call);
+ if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
+ return;
- dbus_message_unref(reply);
-}
+ dbus_message_iter_recurse(&array, &dict);
-static int change_powered(DBusConnection *connection, const char *path,
- dbus_bool_t powered)
-{
- DBusMessage *message;
- DBusMessageIter iter;
- DBusPendingCall *call;
+ while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
+ DBusMessageIter entry, value;
+ const char *key;
- DBG("");
+ dbus_message_iter_recurse(&dict, &entry);
+ dbus_message_iter_get_basic(&entry, &key);
- message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
- BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
- if (message == NULL)
- return -ENOMEM;
+ dbus_message_iter_next(&entry);
+ dbus_message_iter_recurse(&entry, &value);
- dbus_message_iter_init_append(message, &iter);
- connman_dbus_property_append_variant(&iter, "Powered",
- DBUS_TYPE_BOOLEAN, &powered);
+ if (g_str_equal(key, "Adapter") == TRUE) {
+ if (parent != NULL)
+ dbus_message_iter_get_basic(&value, parent);
+ } else if (g_str_equal(key, "Address") == TRUE) {
+ if (address != NULL)
+ dbus_message_iter_get_basic(&value, address);
+ } else if (g_str_equal(key, "Name") == TRUE) {
+ if (name != NULL)
+ dbus_message_iter_get_basic(&value, name);
+ } else if (g_str_equal(key, "Alias") == TRUE) {
+ if (alias != NULL)
+ dbus_message_iter_get_basic(&value, alias);
+ } else if (g_str_equal(key, "Powered") == TRUE) {
+ if (powered != NULL)
+ dbus_message_iter_get_basic(&value, powered);
+ } else if (g_str_equal(key, "Discovering") == TRUE) {
+ if (scanning != NULL)
+ dbus_message_iter_get_basic(&value, scanning);
+ } else if (g_str_equal(key, "Devices") == TRUE) {
+ if (networks != NULL)
+ memcpy(networks, &value, sizeof(value));
+ } else if (g_str_equal(key, "UUIDs") == TRUE) {
+ if (uuids != NULL)
+ memcpy(uuids, &value, sizeof(value));
+ }
- if (dbus_connection_send_with_reply(connection, message,
- &call, TIMEOUT) == FALSE) {
- connman_error("Failed to change Powered property");
- dbus_message_unref(message);
- return -EINVAL;
+ dbus_message_iter_next(&dict);
}
+}
- dbus_pending_call_set_notify(call, powered_reply, NULL, NULL);
+static dbus_bool_t has_pan(DBusMessageIter *array)
+{
+ DBusMessageIter value;
- dbus_message_unref(message);
+ if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
+ return FALSE;
- return -EINPROGRESS;
-}
+ dbus_message_iter_recurse(array, &value);
-static int bluetooth_enable(struct connman_device *adapter)
-{
- struct adapter_data *data = connman_device_get_data(adapter);
- const char *path = connman_device_get_string(adapter, "Node");
+ while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
+ const char *uuid;
+
+ dbus_message_iter_get_basic(&value, &uuid);
- DBG("adapter %p", adapter);
+ if (g_strcmp0(uuid, UUID_NAP) == 0)
+ return TRUE;
+
+ dbus_message_iter_next(&value);
+ }
- return change_powered(data->connection, path, TRUE);
+ return FALSE;
}
-static int bluetooth_disable(struct connman_device *adapter)
+static void network_properties_reply(DBusPendingCall *call, void *user_data)
{
- struct adapter_data *data = connman_device_get_data(adapter);
- const char *path = connman_device_get_string(adapter, "Node");
+ char *path = user_data;
+ struct connman_device *device;
+ struct connman_network *network;
+ DBusMessage *reply;
+ DBusMessageIter uuids;
+ const char *parent = NULL, *address = NULL, *name = NULL;
+ struct ether_addr addr;
+ char ident[13];
- DBG("adapter %p", adapter);
+ reply = dbus_pending_call_steal_reply(call);
- return change_powered(data->connection, path, FALSE);
-}
+ extract_properties(reply, &parent, &address, NULL, &name,
+ NULL, NULL, &uuids, NULL);
-static int bluetooth_scan(struct connman_device *adapter)
-{
- DBG("adapter %p", adapter);
+ if (parent == NULL)
+ goto done;
- return -EIO;
-}
+ device = g_hash_table_lookup(bluetooth_devices, parent);
+ if (device == NULL)
+ goto done;
-static struct connman_device_driver bluetooth_driver = {
- .name = "bluetooth",
- .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
- .probe = bluetooth_probe,
- .remove = bluetooth_remove,
- .enable = bluetooth_enable,
- .disable = bluetooth_disable,
- .scan = bluetooth_scan,
-};
+ if (address == NULL)
+ goto done;
-static GSList *adapter_list = NULL;
+ ether_aton_r(address, &addr);
-static void free_adapters(void)
-{
- GSList *list;
+ snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
+ addr.ether_addr_octet[0],
+ addr.ether_addr_octet[1],
+ addr.ether_addr_octet[2],
+ addr.ether_addr_octet[3],
+ addr.ether_addr_octet[4],
+ addr.ether_addr_octet[5]);
- DBG("");
+ if (has_pan(&uuids) == FALSE)
+ goto done;
- for (list = adapter_list; list; list = list->next) {
- struct connman_device *adapter = list->data;
+ network = connman_device_get_network(device, ident);
+ if (network != NULL)
+ goto done;
- connman_device_unregister(adapter);
- connman_device_unref(adapter);
- }
+ network = connman_network_create(ident,
+ CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
+ if (network == NULL)
+ goto done;
- g_slist_free(adapter_list);
- adapter_list = NULL;
-}
+ connman_network_set_string(network, "Path", path);
-static struct connman_device *find_adapter(const char *path)
-{
- GSList *list;
+ connman_network_set_name(network, name);
- DBG("path %s", path);
+ connman_device_add_network(device, network);
- for (list = adapter_list; list; list = list->next) {
- struct connman_device *adapter = list->data;
- const char *adapter_path = connman_device_get_string(adapter,
- "Node");
+ connman_network_set_group(network, ident);
- if (adapter_path == NULL)
- continue;
+ g_hash_table_replace(bluetooth_networks, g_strdup(path), network);
- if (g_str_equal(adapter_path, path) == TRUE)
- return adapter;
- }
+done:
+ dbus_message_unref(reply);
- return NULL;
+ dbus_pending_call_unref(call);
}
-static void device_properties(DBusConnection *connection, const char *path,
- DBusMessage *message, void *user_data)
+static void add_network(struct connman_device *device, const char *path)
{
- struct connman_device *device = user_data;
- const char *node = g_basename(path);
- struct connman_network *network;
+ DBusMessage *message;
+ DBusPendingCall *call;
DBG("path %s", path);
- network = connman_device_get_network(device, node);
- if (network != NULL)
+ message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
+ BLUEZ_DEVICE_INTERFACE, GET_PROPERTIES);
+ if (message == NULL)
return;
- network = connman_network_create(node,
- CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN);
- if (network == NULL)
- return;
+ dbus_message_set_auto_start(message, FALSE);
- connman_network_set_string(network, "Node", path);
+ if (dbus_connection_send_with_reply(connection, message,
+ &call, TIMEOUT) == FALSE) {
+ connman_error("Failed to get network properties for %s", path);
+ goto done;
+ }
- connman_device_add_network(device, network);
+ if (call == NULL) {
+ connman_error("D-Bus connection not available");
+ goto done;
+ }
+
+ dbus_pending_call_set_notify(call, network_properties_reply,
+ g_strdup(path), g_free);
+
+done:
+ dbus_message_unref(message);
}
-static void check_devices(struct connman_device *adapter,
- DBusConnection *connection, DBusMessageIter *array)
+static void check_networks(struct connman_device *device,
+ DBusMessageIter *array)
{
DBusMessageIter value;
dbus_message_iter_get_basic(&value, &path);
- get_properties(connection, path, BLUEZ_DEVICE_INTERFACE,
- device_properties, adapter);
+ add_network(device, path);
dbus_message_iter_next(&value);
}
}
-static void property_changed(DBusConnection *connection, DBusMessage *message)
+static gboolean adapter_changed(DBusConnection *connection,
+ DBusMessage *message, void *user_data)
{
const char *path = dbus_message_get_path(message);
- struct connman_device *adapter;
+ struct connman_device *device;
DBusMessageIter iter, value;
const char *key;
DBG("path %s", path);
- adapter = find_adapter(path);
- if (adapter == NULL)
- return;
+ device = g_hash_table_lookup(bluetooth_devices, path);
+ if (device == NULL)
+ return TRUE;
if (dbus_message_iter_init(message, &iter) == FALSE)
- return;
+ return TRUE;
dbus_message_iter_get_basic(&iter, &key);
dbus_message_iter_recurse(&iter, &value);
if (g_str_equal(key, "Powered") == TRUE) {
- gboolean val;
+ dbus_bool_t val;
dbus_message_iter_get_basic(&value, &val);
- connman_device_set_powered(adapter, val);
+ connman_device_set_powered(device, val);
} else if (g_str_equal(key, "Discovering") == TRUE) {
- gboolean val;
+ dbus_bool_t val;
dbus_message_iter_get_basic(&value, &val);
- connman_device_set_scanning(adapter, val);
+ connman_device_set_scanning(device, val);
+ } else if (g_str_equal(key, "Devices") == TRUE) {
+ check_networks(device, &value);
}
+
+ return TRUE;
}
-static void parse_adapter_properties(struct connman_device *adapter,
- DBusConnection *connection,
- DBusMessage *reply)
+static gboolean device_removed(DBusConnection *connection,
+ DBusMessage *message, void *user_data)
{
- DBusMessageIter array, dict;
-
- if (dbus_message_iter_init(reply, &array) == FALSE)
- return;
+ const char *network_path;
+ struct connman_network *network;
+ struct connman_device *device;
+ DBusMessageIter iter;
- if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
- return;
+ DBG("");
- dbus_message_iter_recurse(&array, &dict);
+ if (dbus_message_iter_init(message, &iter) == FALSE)
+ return TRUE;
- while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter entry, value;
- const char *key;
+ dbus_message_iter_get_basic(&iter, &network_path);
- dbus_message_iter_recurse(&dict, &entry);
- dbus_message_iter_get_basic(&entry, &key);
+ network = g_hash_table_lookup(bluetooth_networks, network_path);
+ if (network == NULL)
+ return TRUE;
- dbus_message_iter_next(&entry);
- dbus_message_iter_recurse(&entry, &value);
+ device = connman_network_get_device(network);
+ if (device == NULL)
+ return TRUE;
- if (g_str_equal(key, "Powered") == TRUE) {
- gboolean val;
+ g_hash_table_remove(bluetooth_networks, network_path);
- dbus_message_iter_get_basic(&value, &val);
- connman_device_set_powered(adapter, val);
- } else if (g_str_equal(key, "Discovering") == TRUE) {
- gboolean val;
+ return TRUE;
+}
- dbus_message_iter_get_basic(&value, &val);
- connman_device_set_scanning(adapter, val);
- } else if (g_str_equal(key, "Devices") == TRUE) {
- check_devices(adapter, connection, &value);
- }
+static gboolean device_changed(DBusConnection *connection,
+ DBusMessage *message, void *user_data)
+{
+ const char *path = dbus_message_get_path(message);
+ DBusMessageIter iter, value;
+ const char *key;
- dbus_message_iter_next(&dict);
+ DBG("path %s", path);
+
+ if (dbus_message_iter_init(message, &iter) == FALSE)
+ return TRUE;
+
+ dbus_message_iter_get_basic(&iter, &key);
+
+ dbus_message_iter_next(&iter);
+ dbus_message_iter_recurse(&iter, &value);
+
+ DBG("key %s", key);
+
+ if (g_str_equal(key, "UUIDs") == TRUE)
+ add_network(NULL, path);
+
+ return TRUE;
+}
+
+static void remove_device_networks(struct connman_device *device)
+{
+ GHashTableIter iter;
+ gpointer key, value;
+ GSList *key_list = NULL;
+ GSList *list;
+
+ if (bluetooth_networks == NULL)
+ return;
+
+ g_hash_table_iter_init(&iter, bluetooth_networks);
+
+ while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
+ struct connman_network *network = value;
+
+ if (connman_network_get_device(network) != device)
+ continue;
+
+ key_list = g_slist_append(key_list, key);
}
+
+ for (list = key_list; list != NULL; list = list->next) {
+ const char *network_path = list->data;
+
+ g_hash_table_remove(bluetooth_networks, network_path);
+ }
+
+ g_slist_free(key_list);
}
-static void adapter_properties(DBusConnection *connection, const char *path,
- DBusMessage *message, void *user_data)
+static void adapter_properties_reply(DBusPendingCall *call, void *user_data)
{
- const char *node = g_basename(path);
- struct connman_device *adapter;
+ char *path = user_data;
+ struct connman_device *device;
+ DBusMessage *reply;
+ DBusMessageIter networks;
+ const char *address = NULL, *name = NULL;
+ dbus_bool_t powered = FALSE, scanning = FALSE;
+ struct ether_addr addr;
+ char ident[13];
DBG("path %s", path);
- adapter = find_adapter(path);
- if (adapter != NULL)
+ reply = dbus_pending_call_steal_reply(call);
+
+ if (path == NULL)
goto done;
- adapter = connman_device_create(node, CONNMAN_DEVICE_TYPE_BLUETOOTH);
- if (adapter == NULL)
- return;
+ extract_properties(reply, NULL, &address, &name, NULL,
+ &powered, &scanning, NULL, &networks);
- connman_device_set_string(adapter, "Node", path);
+ if (address == NULL)
+ goto done;
- if (node != NULL && g_str_has_prefix(node, "hci") == TRUE) {
- int index;
- errno = 0;
- index = atoi(node + 3);
- if (errno == 0)
- connman_device_set_index(adapter, index);
- }
+ if (g_strcmp0(address, "00:00:00:00:00:00") == 0)
+ goto done;
- connman_device_set_interface(adapter, node);
+ device = g_hash_table_lookup(bluetooth_devices, path);
+ if (device != NULL)
+ goto update;
- connman_device_set_policy(adapter, CONNMAN_DEVICE_POLICY_MANUAL);
- connman_device_set_mode(adapter, CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE);
+ ether_aton_r(address, &addr);
- if (connman_device_register(adapter) < 0) {
- connman_device_unref(adapter);
- return;
+ snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
+ addr.ether_addr_octet[0],
+ addr.ether_addr_octet[1],
+ addr.ether_addr_octet[2],
+ addr.ether_addr_octet[3],
+ addr.ether_addr_octet[4],
+ addr.ether_addr_octet[5]);
+
+ device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_BLUETOOTH);
+ if (device == NULL)
+ goto done;
+
+ connman_device_set_ident(device, ident);
+
+ connman_device_set_string(device, "Path", path);
+
+ if (connman_device_register(device) < 0) {
+ connman_device_unref(device);
+ goto done;
}
- adapter_list = g_slist_append(adapter_list, adapter);
+ g_hash_table_insert(bluetooth_devices, g_strdup(path), device);
+
+update:
+ connman_device_set_string(device, "Address", address);
+ connman_device_set_string(device, "Name", name);
+ connman_device_set_string(device, "Path", path);
+
+ connman_device_set_powered(device, powered);
+ connman_device_set_scanning(device, scanning);
+
+ if (powered == TRUE)
+ check_networks(device, &networks);
+ else
+ remove_device_networks(device);
done:
- parse_adapter_properties(adapter, connection, message);
+ dbus_message_unref(reply);
+
+ dbus_pending_call_unref(call);
}
static void add_adapter(DBusConnection *connection, const char *path)
{
+ DBusMessage *message;
+ DBusPendingCall *call;
+
DBG("path %s", path);
- get_properties(connection, path, BLUEZ_ADAPTER_INTERFACE,
- adapter_properties, NULL);
+ message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
+ BLUEZ_ADAPTER_INTERFACE, GET_PROPERTIES);
+ if (message == NULL)
+ return;
+
+ dbus_message_set_auto_start(message, FALSE);
+
+ if (dbus_connection_send_with_reply(connection, message,
+ &call, TIMEOUT) == FALSE) {
+ connman_error("Failed to get adapter properties for %s", path);
+ goto done;
+ }
+
+ if (call == NULL) {
+ connman_error("D-Bus connection not available");
+ goto done;
+ }
+
+ dbus_pending_call_set_notify(call, adapter_properties_reply,
+ g_strdup(path), g_free);
+
+done:
+ dbus_message_unref(message);
}
-static void remove_adapter(DBusConnection *connection, const char *path)
+static gboolean adapter_added(DBusConnection *connection, DBusMessage *message,
+ void *user_data)
{
- struct connman_device *adapter;
+ const char *path;
+
+ dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
+ DBUS_TYPE_INVALID);
+ add_adapter(connection, path);
+ return TRUE;
+}
+static void remove_adapter(DBusConnection *connection, const char *path)
+{
DBG("path %s", path);
- adapter = find_adapter(path);
- if (adapter == NULL)
- return;
+ g_hash_table_remove(bluetooth_devices, path);
+}
- adapter_list = g_slist_remove(adapter_list, adapter);
+static gboolean adapter_removed(DBusConnection *connection, DBusMessage *message,
+ void *user_data)
+{
+ const char *path;
- connman_device_unregister(adapter);
- connman_device_unref(adapter);
+ dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
+ DBUS_TYPE_INVALID);
+ remove_adapter(connection, path);
+ return TRUE;
}
static void list_adapters_reply(DBusPendingCall *call, void *user_data)
{
- DBusConnection *connection = user_data;
DBusMessage *reply;
DBusError error;
char **adapters;
dbus_error_init(&error);
+ if (dbus_set_error_from_message(&error, reply) == TRUE) {
+ connman_error("%s", error.message);
+ dbus_error_free(&error);
+ goto done;
+ }
+
if (dbus_message_get_args(reply, &error,
DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
&adapters, &num_adapters,
}
for (i = 0; i < num_adapters; i++)
- get_properties(connection, adapters[i],
- BLUEZ_ADAPTER_INTERFACE,
- adapter_properties, NULL);
+ add_adapter(connection, adapters[i]);
g_strfreev(adapters);
done:
dbus_message_unref(reply);
+
+ dbus_pending_call_unref(call);
+}
+
+static void unregister_device(gpointer data)
+{
+ struct connman_device *device = data;
+
+ DBG("");
+
+ remove_device_networks(device);
+
+ connman_device_unregister(device);
+ connman_device_unref(device);
+}
+
+static void remove_network(gpointer data)
+{
+ struct connman_network *network = data;
+ struct connman_device *device;
+
+ DBG("network %p", network);
+
+ device = connman_network_get_device(network);
+ if (device != NULL)
+ connman_device_remove_network(device, network);
+
+ connman_network_unref(network);
}
static void bluetooth_connect(DBusConnection *connection, void *user_data)
DBG("connection %p", connection);
+ bluetooth_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, unregister_device);
+
+ bluetooth_networks = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, remove_network);
+
message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
BLUEZ_MANAGER_INTERFACE, LIST_ADAPTERS);
if (message == NULL)
return;
+ dbus_message_set_auto_start(message, FALSE);
+
if (dbus_connection_send_with_reply(connection, message,
&call, TIMEOUT) == FALSE) {
connman_error("Failed to get Bluetooth adapters");
- dbus_message_unref(message);
- return;
+ goto done;
}
- dbus_pending_call_set_notify(call, list_adapters_reply,
- connection, NULL);
+ if (call == NULL) {
+ connman_error("D-Bus connection not available");
+ goto done;
+ }
+ dbus_pending_call_set_notify(call, list_adapters_reply, NULL, NULL);
+
+done:
dbus_message_unref(message);
}
{
DBG("connection %p", connection);
- free_adapters();
+ if (bluetooth_devices == NULL)
+ return;
+
+ g_hash_table_destroy(bluetooth_networks);
+ bluetooth_networks = NULL;
+ g_hash_table_destroy(bluetooth_devices);
+ bluetooth_devices = NULL;
}
-static DBusHandlerResult bluetooth_signal(DBusConnection *connection,
- DBusMessage *message, void *user_data)
+static int bluetooth_probe(struct connman_device *device)
{
- if (dbus_message_has_interface(message,
- BLUEZ_MANAGER_INTERFACE) == FALSE &&
- dbus_message_has_interface(message,
- BLUEZ_ADAPTER_INTERFACE) == FALSE)
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ DBG("device %p", device);
- DBG("connection %p", connection);
+ return 0;
+}
- if (dbus_message_is_signal(message, BLUEZ_ADAPTER_INTERFACE,
- PROPERTY_CHANGED) == TRUE) {
- property_changed(connection, message);
- } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
- ADAPTER_ADDED) == TRUE) {
- const char *path;
- dbus_message_get_args(message, NULL,
- DBUS_TYPE_OBJECT_PATH, &path,
+static void bluetooth_remove(struct connman_device *device)
+{
+ DBG("device %p", device);
+}
+
+static void powered_reply(DBusPendingCall *call, void *user_data)
+{
+ DBusError error;
+ DBusMessage *reply;
+
+ DBG("");
+
+ reply = dbus_pending_call_steal_reply(call);
+
+ dbus_error_init(&error);
+
+ if (dbus_set_error_from_message(&error, reply) == TRUE) {
+ connman_error("%s", error.message);
+ dbus_error_free(&error);
+ dbus_message_unref(reply);
+ dbus_pending_call_unref(call);
+ return;
+ }
+
+ dbus_message_unref(reply);
+ dbus_pending_call_unref(call);
+
+ add_adapter(connection, user_data);
+}
+
+static int change_powered(DBusConnection *connection, const char *path,
+ dbus_bool_t powered)
+{
+ DBusMessage *message;
+ DBusMessageIter iter;
+ DBusPendingCall *call;
+
+ DBG("");
+
+ if (path == NULL)
+ return -EINVAL;
+
+ message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
+ BLUEZ_ADAPTER_INTERFACE, SET_PROPERTY);
+ if (message == NULL)
+ return -ENOMEM;
+
+ dbus_message_set_auto_start(message, FALSE);
+
+ dbus_message_iter_init_append(message, &iter);
+ connman_dbus_property_append_basic(&iter, "Powered",
+ DBUS_TYPE_BOOLEAN, &powered);
+
+ if (dbus_connection_send_with_reply(connection, message,
+ &call, TIMEOUT) == FALSE) {
+ connman_error("Failed to change Powered property");
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
+ if (call == NULL) {
+ connman_error("D-Bus connection not available");
+ dbus_message_unref(message);
+ return -EINVAL;
+ }
+
+ dbus_pending_call_set_notify(call, powered_reply,
+ g_strdup(path), g_free);
+
+ dbus_message_unref(message);
+
+ return -EINPROGRESS;
+}
+
+static int bluetooth_enable(struct connman_device *device)
+{
+ const char *path = connman_device_get_string(device, "Path");
+
+ DBG("device %p", device);
+
+ return change_powered(connection, path, TRUE);
+}
+
+static int bluetooth_disable(struct connman_device *device)
+{
+ const char *path = connman_device_get_string(device, "Path");
+
+ DBG("device %p", device);
+
+ return change_powered(connection, path, FALSE);
+}
+
+static struct connman_device_driver bluetooth_driver = {
+ .name = "bluetooth",
+ .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
+ .probe = bluetooth_probe,
+ .remove = bluetooth_remove,
+ .enable = bluetooth_enable,
+ .disable = bluetooth_disable,
+};
+
+static int tech_probe(struct connman_technology *technology)
+{
+ return 0;
+}
+
+static void tech_remove(struct connman_technology *technology)
+{
+}
+
+static void server_register_reply(DBusPendingCall *call, void *user_data)
+{
+ struct connman_technology *technology = user_data;
+ DBusError error;
+ DBusMessage *reply;
+
+ DBG("");
+
+ reply = dbus_pending_call_steal_reply(call);
+
+ dbus_error_init(&error);
+
+ if (dbus_set_error_from_message(&error, reply) == TRUE) {
+ connman_error("%s", error.message);
+ dbus_error_free(&error);
+ dbus_message_unref(reply);
+ dbus_pending_call_unref(call);
+ return;
+ }
+
+ dbus_message_unref(reply);
+ dbus_pending_call_unref(call);
+
+ connman_technology_tethering_notify(technology, TRUE);
+}
+
+static void server_unregister_reply(DBusPendingCall *call, void *user_data)
+{
+ struct connman_technology *technology = user_data;
+ DBusError error;
+ DBusMessage *reply;
+
+ DBG("");
+
+ reply = dbus_pending_call_steal_reply(call);
+
+ dbus_error_init(&error);
+
+ if (dbus_set_error_from_message(&error, reply) == TRUE) {
+ connman_error("%s", error.message);
+ dbus_error_free(&error);
+ dbus_message_unref(reply);
+ dbus_pending_call_unref(call);
+ return;
+ }
+
+ dbus_message_unref(reply);
+ dbus_pending_call_unref(call);
+
+ connman_technology_tethering_notify(technology, FALSE);
+}
+
+
+static void server_register(const char *path, const char *uuid,
+ struct connman_technology *technology,
+ const char *bridge, connman_bool_t enabled)
+{
+ DBusMessage *message;
+ DBusPendingCall *call;
+ char *command;
+
+ DBG("path %s enabled %d", path, enabled);
+
+ command = enabled ? REGISTER : UNREGISTER;
+
+ message = dbus_message_new_method_call(BLUEZ_SERVICE, path,
+ BLUEZ_NETWORK_SERVER, command);
+ if (message == NULL)
+ return;
+
+ dbus_message_set_auto_start(message, FALSE);
+
+ dbus_message_append_args(message, DBUS_TYPE_STRING, &uuid,
DBUS_TYPE_INVALID);
- add_adapter(connection, path);
- } else if (dbus_message_is_signal(message, BLUEZ_MANAGER_INTERFACE,
- ADAPTER_REMOVED) == TRUE) {
- const char *path;
- dbus_message_get_args(message, NULL,
- DBUS_TYPE_OBJECT_PATH, &path,
+
+ if (enabled == TRUE)
+ dbus_message_append_args(message, DBUS_TYPE_STRING, &bridge,
DBUS_TYPE_INVALID);
- remove_adapter(connection, path);
+
+ if (dbus_connection_send_with_reply(connection, message,
+ &call, TIMEOUT) == FALSE) {
+ connman_error("Failed to enable PAN server");
+ dbus_message_unref(message);
+ return;
+ }
+
+ if (call == NULL) {
+ connman_error("D-Bus connection not available");
+ dbus_message_unref(message);
+ return;
}
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ if (enabled == TRUE)
+ dbus_pending_call_set_notify(call, server_register_reply,
+ technology, NULL);
+ else
+ dbus_pending_call_set_notify(call, server_unregister_reply,
+ technology, NULL);
+
+ dbus_message_unref(message);
}
-static DBusConnection *connection;
-static guint watch;
+struct tethering_info {
+ struct connman_technology *technology;
+ const char *bridge;
+};
+
+static void enable_nap(gpointer key, gpointer value, gpointer user_data)
+{
+ struct tethering_info *info = user_data;
+ struct connman_device *device = value;
+ const char *path;
+
+ DBG("");
+
+ path = connman_device_get_string(device, "Path");
+
+ server_register(path, "nap", info->technology, info->bridge, TRUE);
+}
+
+static void disable_nap(gpointer key, gpointer value, gpointer user_data)
+{
+ struct tethering_info *info = user_data;
+ struct connman_device *device = value;
+ const char *path;
+
+ DBG("");
+
+ path = connman_device_get_string(device, "Path");
+
+ server_register(path, "nap", info->technology, info->bridge, FALSE);
+}
+
+static int tech_set_tethering(struct connman_technology *technology,
+ const char *identifier, const char *passphrase,
+ const char *bridge, connman_bool_t enabled)
+{
+ struct tethering_info info = {
+ .technology = technology,
+ .bridge = bridge,
+ };
+
+ DBG("bridge %s", bridge);
+
+ if (enabled)
+ g_hash_table_foreach(bluetooth_devices, enable_nap, &info);
+ else
+ g_hash_table_foreach(bluetooth_devices, disable_nap, &info);
+
+ return 0;
+}
-static const char *added_rule = "type=signal,member=" ADAPTER_ADDED
- ",interface=" BLUEZ_MANAGER_INTERFACE;
-static const char *removed_rule = "type=signal,member=" ADAPTER_REMOVED
- ",interface=" BLUEZ_MANAGER_INTERFACE;
+static struct connman_technology_driver tech_driver = {
+ .name = "bluetooth",
+ .type = CONNMAN_SERVICE_TYPE_BLUETOOTH,
+ .probe = tech_probe,
+ .remove = tech_remove,
+ .set_tethering = tech_set_tethering,
+};
-static const char *adapter_rule = "type=signal,member=" PROPERTY_CHANGED
- ",interface=" BLUEZ_ADAPTER_INTERFACE;
+static guint watch;
+static guint added_watch;
+static guint removed_watch;
+static guint adapter_watch;
+static guint device_watch;
+static guint device_removed_watch;
+static guint network_watch;
static int bluetooth_init(void)
{
- int err = -EIO;
+ int err;
connection = connman_dbus_get_connection();
if (connection == NULL)
return -EIO;
- if (dbus_connection_add_filter(connection, bluetooth_signal,
- NULL, NULL) == FALSE)
- goto unref;
+ watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
+ bluetooth_connect, bluetooth_disconnect, NULL, NULL);
+
+ added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ BLUEZ_MANAGER_INTERFACE,
+ ADAPTER_ADDED, adapter_added,
+ NULL, NULL);
+
+ removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ BLUEZ_MANAGER_INTERFACE,
+ ADAPTER_REMOVED, adapter_removed,
+ NULL, NULL);
+
+ adapter_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ BLUEZ_ADAPTER_INTERFACE,
+ PROPERTY_CHANGED, adapter_changed,
+ NULL, NULL);
+
+ device_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ BLUEZ_ADAPTER_INTERFACE,
+ DEVICE_REMOVED, device_removed,
+ NULL, NULL);
+
+ device_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ BLUEZ_DEVICE_INTERFACE,
+ PROPERTY_CHANGED, device_changed,
+ NULL, NULL);
+
+ network_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+ BLUEZ_NETWORK_INTERFACE,
+ PROPERTY_CHANGED, network_changed,
+ NULL, NULL);
+
+ if (watch == 0 || added_watch == 0 || removed_watch == 0
+ || adapter_watch == 0 || network_watch == 0
+ || device_watch == 0
+ || device_removed_watch == 0) {
+ err = -EIO;
+ goto remove;
+ }
err = connman_network_driver_register(&pan_driver);
if (err < 0)
goto remove;
}
- watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
- bluetooth_connect, bluetooth_disconnect, NULL, NULL);
- if (watch == 0) {
+ err = connman_technology_driver_register(&tech_driver);
+ if (err < 0) {
connman_device_driver_unregister(&bluetooth_driver);
connman_network_driver_unregister(&pan_driver);
- err = -EIO;
goto remove;
}
- if (g_dbus_check_service(connection, BLUEZ_SERVICE) == TRUE)
- bluetooth_connect(connection, NULL);
-
- dbus_bus_add_match(connection, added_rule, NULL);
- dbus_bus_add_match(connection, removed_rule, NULL);
- dbus_bus_add_match(connection, adapter_rule, NULL);
- dbus_connection_flush(connection);
-
return 0;
remove:
- dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
+ g_dbus_remove_watch(connection, watch);
+ g_dbus_remove_watch(connection, added_watch);
+ g_dbus_remove_watch(connection, removed_watch);
+ g_dbus_remove_watch(connection, adapter_watch);
+ g_dbus_remove_watch(connection, device_removed_watch);
+ g_dbus_remove_watch(connection, device_watch);
+ g_dbus_remove_watch(connection, network_watch);
-unref:
dbus_connection_unref(connection);
return err;
static void bluetooth_exit(void)
{
- dbus_bus_remove_match(connection, adapter_rule, NULL);
- dbus_bus_remove_match(connection, removed_rule, NULL);
- dbus_bus_remove_match(connection, added_rule, NULL);
- dbus_connection_flush(connection);
-
g_dbus_remove_watch(connection, watch);
+ g_dbus_remove_watch(connection, added_watch);
+ g_dbus_remove_watch(connection, removed_watch);
+ g_dbus_remove_watch(connection, adapter_watch);
+ g_dbus_remove_watch(connection, device_removed_watch);
+ g_dbus_remove_watch(connection, device_watch);
+ g_dbus_remove_watch(connection, network_watch);
- free_adapters();
+ bluetooth_disconnect(connection, NULL);
+
+ connman_technology_driver_unregister(&tech_driver);
connman_device_driver_unregister(&bluetooth_driver);
connman_network_driver_unregister(&pan_driver);
- dbus_connection_remove_filter(connection, bluetooth_signal, NULL);
-
dbus_connection_unref(connection);
}
CONNMAN_PLUGIN_DEFINE(bluetooth, "Bluetooth technology plugin", VERSION,
- bluetooth_init, bluetooth_exit)
+ CONNMAN_PLUGIN_PRIORITY_DEFAULT, bluetooth_init, bluetooth_exit)