Fix detection of Bluetooth devices
authorMarcel Holtmann <marcel@holtmann.org>
Fri, 5 Dec 2008 12:59:25 +0000 (13:59 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Fri, 5 Dec 2008 12:59:25 +0000 (13:59 +0100)
plugins/bluetooth.c

index 6b0b69a..978f257 100644 (file)
@@ -33,6 +33,8 @@
 
 #define BLUEZ_SERVICE "org.bluez"
 
+#define TIMEOUT 5000
+
 static int bluetooth_probe(struct connman_device *device)
 {
        DBG("device %p", device);
@@ -52,14 +54,91 @@ static struct connman_device_driver bluetooth_driver = {
        .remove = bluetooth_remove,
 };
 
+static GSList *device_list = NULL;
+
+static void 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_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++) {
+               struct connman_element *device;
+
+               device = connman_element_create(NULL);
+               device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
+               device->subtype = CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH;
+
+               device->name = g_path_get_basename(adapters[i]);
+
+               connman_element_register(device, NULL);
+               device_list = g_slist_append(device_list, device);
+       }
+
+       g_strfreev(adapters);
+
+done:
+       dbus_message_unref(reply);
+}
+
 static void bluetooth_connect(DBusConnection *connection, void *user_data)
 {
+       DBusMessage *message;
+       DBusPendingCall *call;
+
        DBG("connection %p", connection);
+
+       message = dbus_message_new_method_call(BLUEZ_SERVICE, "/",
+                                       "org.bluez.Manager", "ListAdapters");
+       if (message == NULL)
+               return;
+
+       if (dbus_connection_send_with_reply(connection, message,
+                                               &call, TIMEOUT) == FALSE) {
+               connman_error("Failed to get Bluetooth adapters");
+               dbus_message_unref(message);
+               return;
+       }
+
+       dbus_pending_call_set_notify(call, adapters_reply, NULL, NULL);
+
+       dbus_message_unref(message);
 }
 
 static void bluetooth_disconnect(DBusConnection *connection, void *user_data)
 {
+       GSList *list;
+
        DBG("connection %p", connection);
+
+       for (list = device_list; list; list = list->next) {
+               struct connman_element *device = list->data;
+
+               connman_element_unregister(device);
+               connman_element_unref(device);
+       }
+
+       g_slist_free(device_list);
+       device_list = NULL;
 }
 
 static DBusConnection *connection;