+
+ dbus_message_iter_get_basic(&value, &path);
+
+ add_network(device, path);
+
+ dbus_message_iter_next(&value);
+ }
+}
+
+static gboolean adapter_changed(DBusConnection *connection,
+ DBusMessage *message, void *user_data)
+{
+ const char *path = dbus_message_get_path(message);
+ struct connman_device *device;
+ DBusMessageIter iter, value;
+ const char *key;
+
+ DBG("path %s", path);
+
+ device = g_hash_table_lookup(bluetooth_devices, path);
+ if (device == NULL)
+ return TRUE;
+
+ 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);
+
+ if (g_str_equal(key, "Powered") == TRUE) {
+ dbus_bool_t val;
+
+ dbus_message_iter_get_basic(&value, &val);
+ connman_device_set_powered(device, val);
+ } else if (g_str_equal(key, "Discovering") == TRUE) {
+ dbus_bool_t val;
+
+ dbus_message_iter_get_basic(&value, &val);
+ connman_device_set_scanning(device, val);
+ } else if (g_str_equal(key, "Devices") == TRUE) {
+ check_networks(device, &value);
+ }
+
+ return TRUE;
+}
+
+static gboolean device_removed(DBusConnection *connection,
+ DBusMessage *message, void *user_data)
+{
+ const char *network_path;
+ struct connman_network *network;
+ struct connman_device *device;
+ DBusMessageIter iter;
+
+ DBG("");
+
+ if (dbus_message_iter_init(message, &iter) == FALSE)
+ return TRUE;
+
+ dbus_message_iter_get_basic(&iter, &network_path);
+
+ network = g_hash_table_lookup(bluetooth_networks, network_path);
+ if (network == NULL)
+ return TRUE;
+
+ device = connman_network_get_device(network);
+ if (device == NULL)
+ return TRUE;
+
+ g_hash_table_remove(bluetooth_networks, network_path);
+
+ return TRUE;
+}
+
+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;
+
+ 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_reply(DBusPendingCall *call, void *user_data)
+{
+ 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);
+
+ reply = dbus_pending_call_steal_reply(call);
+
+ if (path == NULL)
+ goto done;
+
+ extract_properties(reply, NULL, &address, &name, NULL,
+ &powered, &scanning, NULL, &networks);
+
+ if (address == NULL)
+ goto done;
+
+ if (g_strcmp0(address, "00:00:00:00:00:00") == 0)
+ goto done;
+
+ device = g_hash_table_lookup(bluetooth_devices, path);
+ if (device != NULL)
+ goto update;
+
+ ether_aton_r(address, &addr);
+
+ 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;
+ }
+
+ 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:
+ 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);
+
+ 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 gboolean adapter_added(DBusConnection *connection, DBusMessage *message,
+ void *user_data)
+{
+ 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);
+
+ g_hash_table_remove(bluetooth_devices, path);
+}
+
+static gboolean adapter_removed(DBusConnection *connection, DBusMessage *message,
+ void *user_data)
+{
+ const char *path;
+
+ 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)
+{
+ DBusMessage *reply;
+ DBusError error;
+ char **adapters;
+ int i, num_adapters;
+
+ 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);
+ goto done;
+ }
+
+ if (dbus_message_get_args(reply, &error,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
+ &adapters, &num_adapters,
+ 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 adapter list");
+ goto done;
+ }
+
+ for (i = 0; i < num_adapters; i++)
+ 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)
+{
+ DBusMessage *message;
+ DBusPendingCall *call;
+
+ 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");
+ goto done;
+ }
+
+ 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);
+}
+
+static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
+{
+ DBG("connection %p", connection);
+
+ if (bluetooth_devices == NULL)
+ return;
+
+ g_hash_table_destroy(bluetooth_networks);
+ bluetooth_networks = NULL;
+ g_hash_table_destroy(bluetooth_devices);
+ bluetooth_devices = NULL;
+}
+
+static int bluetooth_probe(struct connman_device *device)
+{
+ GHashTableIter iter;
+ gpointer key, value;
+
+ DBG("device %p", device);
+
+ if (bluetooth_devices == NULL)
+ return -ENOTSUP;
+
+ g_hash_table_iter_init(&iter, bluetooth_devices);
+
+ while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
+ struct connman_device *device_pan = value;
+
+ if (device == device_pan)
+ return 0;
+ }
+
+ return -ENOTSUP;
+}
+
+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,