[pulseaudio-commits] Branch 'next' - 2 commits - src/modules

Tanu Kaskinen tanuk at kemper.freedesktop.org
Wed May 1 03:38:24 PDT 2013


 src/modules/bluetooth/bluetooth-util.c |  177 +++++++++++++++++++++++++++++++--
 1 file changed, 169 insertions(+), 8 deletions(-)

New commits:
commit 81b3b10eb121d88720898c56b004f5516fb0d328
Author: Mikel Astiz <mikel.astiz at bmw-carit.de>
Date:   Mon Apr 29 18:27:59 2013 +0200

    bluetooth: Parse the tree returned by ObjectManager
    
    Parse the result of ObjectManager.GetManagedObjects(), which includes
    all objects registered, their interfaces and the corresponding
    properties per interface.

diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
index 1883c8a..cee283e 100644
--- a/src/modules/bluetooth/bluetooth-util.c
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -362,8 +362,6 @@ static int parse_device_property(pa_bluetooth_device *d, DBusMessageIter *i, boo
 
     dbus_message_iter_recurse(i, &variant_i);
 
-/*     pa_log_debug("Parsing property org.bluez.Device.%s", key); */
-
     switch (dbus_message_iter_get_arg_type(&variant_i)) {
 
         case DBUS_TYPE_STRING: {
@@ -452,6 +450,11 @@ static int parse_device_property(pa_bluetooth_device *d, DBusMessageIter *i, boo
                     uuiddata.uuid = value;
                     pa_hook_fire(&d->discovery->hooks[PA_BLUETOOTH_HOOK_DEVICE_UUID_ADDED], &uuiddata);
 
+                    if (d->discovery->version >= BLUEZ_VERSION_5) {
+                        dbus_message_iter_next(&ai);
+                        continue;
+                    }
+
                     /* Vudentz said the interfaces are here when the UUIDs are announced */
                     if (strcasecmp(HSP_AG_UUID, value) == 0 || strcasecmp(HFP_AG_UUID, value) == 0) {
                         pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.HandsfreeGateway",
@@ -867,16 +870,25 @@ static void register_endpoint(pa_bluetooth_discovery *y, const char *path, const
     send_and_add_to_pending(y, m, register_endpoint_reply, pa_xstrdup(endpoint));
 }
 
+static void register_adapter_endpoints(pa_bluetooth_discovery *y, const char *path) {
+    register_endpoint(y, path, A2DP_SOURCE_ENDPOINT, A2DP_SOURCE_UUID);
+    register_endpoint(y, path, A2DP_SINK_ENDPOINT, A2DP_SINK_UUID);
+
+    /* For BlueZ 5, only A2DP is registered in the Media API */
+    if (y->version >= BLUEZ_VERSION_5)
+        return;
+
+    register_endpoint(y, path, HFP_AG_ENDPOINT, HFP_AG_UUID);
+    register_endpoint(y, path, HFP_HS_ENDPOINT, HFP_HS_UUID);
+}
+
 static void found_adapter(pa_bluetooth_discovery *y, const char *path) {
     DBusMessage *m;
 
     pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Adapter", "GetProperties"));
     send_and_add_to_pending(y, m, get_properties_reply, NULL);
 
-    register_endpoint(y, path, HFP_AG_ENDPOINT, HFP_AG_UUID);
-    register_endpoint(y, path, HFP_HS_ENDPOINT, HFP_HS_UUID);
-    register_endpoint(y, path, A2DP_SOURCE_ENDPOINT, A2DP_SOURCE_UUID);
-    register_endpoint(y, path, A2DP_SINK_ENDPOINT, A2DP_SINK_UUID);
+    register_adapter_endpoints(y, path);
 }
 
 static void list_adapters(pa_bluetooth_discovery *y) {
@@ -887,10 +899,94 @@ static void list_adapters(pa_bluetooth_discovery *y) {
     send_and_add_to_pending(y, m, get_properties_reply, NULL);
 }
 
+static int parse_device_properties(pa_bluetooth_device *d, DBusMessageIter *i, bool is_property_change) {
+    DBusMessageIter element_i;
+    int ret = 0;
+
+    dbus_message_iter_recurse(i, &element_i);
+
+    while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
+        DBusMessageIter dict_i;
+
+        dbus_message_iter_recurse(&element_i, &dict_i);
+
+        if (parse_device_property(d, &dict_i, is_property_change))
+            ret = -1;
+
+        dbus_message_iter_next(&element_i);
+    }
+
+    if (!d->address || !d->alias || d->paired < 0 || d->trusted < 0) {
+        pa_log_error("Non-optional information missing for device %s", d->path);
+        d->device_info_valid = -1;
+        return -1;
+    }
+
+    d->device_info_valid = 1;
+    return ret;
+}
+
+static int parse_interfaces_and_properties(pa_bluetooth_discovery *y, DBusMessageIter *dict_i) {
+    DBusMessageIter element_i;
+    const char *path;
+
+    pa_assert(dbus_message_iter_get_arg_type(dict_i) == DBUS_TYPE_OBJECT_PATH);
+    dbus_message_iter_get_basic(dict_i, &path);
+
+    pa_assert_se(dbus_message_iter_next(dict_i));
+    pa_assert(dbus_message_iter_get_arg_type(dict_i) == DBUS_TYPE_ARRAY);
+
+    dbus_message_iter_recurse(dict_i, &element_i);
+
+    while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
+        DBusMessageIter iface_i;
+        const char *interface;
+
+        dbus_message_iter_recurse(&element_i, &iface_i);
+
+        pa_assert(dbus_message_iter_get_arg_type(&iface_i) == DBUS_TYPE_STRING);
+        dbus_message_iter_get_basic(&iface_i, &interface);
+
+        pa_assert_se(dbus_message_iter_next(&iface_i));
+        pa_assert(dbus_message_iter_get_arg_type(&iface_i) == DBUS_TYPE_ARRAY);
+
+        if (pa_streq(interface, "org.bluez.Adapter1")) {
+            pa_log_debug("Adapter %s found", path);
+            register_adapter_endpoints(y, path);
+        } else if (pa_streq(interface, "org.bluez.Device1")) {
+            pa_bluetooth_device *d;
+
+            if (pa_hashmap_get(y->devices, path)) {
+                pa_log("Found duplicated D-Bus path for device %s", path);
+                return -1;
+            }
+
+            pa_log_debug("Device %s found", path);
+
+            d = device_new(y, path);
+            pa_hashmap_put(y->devices, d->path, d);
+
+            /* FIXME: BlueZ 5 doesn't support the old Audio interface, and thus
+               it's not possible to know if any audio profile is about to be
+               connected. This can introduce regressions with modules such as
+               module-card-restore */
+            d->audio_state = PA_BT_AUDIO_STATE_DISCONNECTED;
+
+            if (parse_device_properties(d, &iface_i, false) < 0)
+                return -1;
+        }
+
+        dbus_message_iter_next(&element_i);
+    }
+
+    return 0;
+}
+
 static void get_managed_objects_reply(DBusPendingCall *pending, void *userdata) {
     DBusMessage *r;
     pa_dbus_pending *p;
     pa_bluetooth_discovery *y;
+    DBusMessageIter arg_i, element_i;
 
     pa_assert_se(p = userdata);
     pa_assert_se(y = p->context_data);
@@ -911,6 +1007,23 @@ static void get_managed_objects_reply(DBusPendingCall *pending, void *userdata)
     pa_log_info("D-Bus ObjectManager detected so assuming BlueZ version 5.");
     y->version = BLUEZ_VERSION_5;
 
+    if (!dbus_message_iter_init(r, &arg_i) || !pa_streq(dbus_message_get_signature(r), "a{oa{sa{sv}}}")) {
+        pa_log("Invalid reply signature for GetManagedObjects().");
+        goto finish;
+    }
+
+    dbus_message_iter_recurse(&arg_i, &element_i);
+    while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
+        DBusMessageIter dict_i;
+
+        dbus_message_iter_recurse(&element_i, &dict_i);
+
+        /* Ignore errors here and proceed with next object */
+        parse_interfaces_and_properties(y, &dict_i);
+
+        dbus_message_iter_next(&element_i);
+    }
+
 finish:
     dbus_message_unref(r);
 

commit 73959d63702736ad3063bd041d0b1913b9352af0
Author: Mikel Astiz <mikel.astiz at bmw-carit.de>
Date:   Mon Apr 29 18:27:58 2013 +0200

    bluetooth: Detect BlueZ 5
    
    Check the existence of ObjectManager to detect the version of the
    running daemon. If the interface exists, it should be BlueZ 5.

diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
index c15ecd1..1883c8a 100644
--- a/src/modules/bluetooth/bluetooth-util.c
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -61,12 +61,19 @@
     " </interface>"                                                     \
     "</node>"
 
+typedef enum pa_bluez_version {
+    BLUEZ_VERSION_UNKNOWN,
+    BLUEZ_VERSION_4,
+    BLUEZ_VERSION_5,
+} pa_bluez_version_t;
+
 struct pa_bluetooth_discovery {
     PA_REFCNT_DECLARE;
 
     pa_core *core;
     pa_dbus_connection *connection;
     PA_LLIST_HEAD(pa_dbus_pending, pending);
+    pa_bluez_version_t version;
     bool adapters_listed;
     pa_hashmap *devices;
     pa_hashmap *transports;
@@ -880,6 +887,46 @@ static void list_adapters(pa_bluetooth_discovery *y) {
     send_and_add_to_pending(y, m, get_properties_reply, NULL);
 }
 
+static void get_managed_objects_reply(DBusPendingCall *pending, void *userdata) {
+    DBusMessage *r;
+    pa_dbus_pending *p;
+    pa_bluetooth_discovery *y;
+
+    pa_assert_se(p = userdata);
+    pa_assert_se(y = p->context_data);
+    pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+    if (dbus_message_is_error(r, DBUS_ERROR_UNKNOWN_METHOD)) {
+        pa_log_info("D-Bus ObjectManager not detected so falling back to BlueZ version 4 API.");
+        y->version = BLUEZ_VERSION_4;
+        list_adapters(y);
+        goto finish;
+    }
+
+    if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+        pa_log("GetManagedObjects() failed: %s: %s", dbus_message_get_error_name(r), pa_dbus_get_error_message(r));
+        goto finish;
+    }
+
+    pa_log_info("D-Bus ObjectManager detected so assuming BlueZ version 5.");
+    y->version = BLUEZ_VERSION_5;
+
+finish:
+    dbus_message_unref(r);
+
+    PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
+    pa_dbus_pending_free(p);
+}
+
+static void init_bluez(pa_bluetooth_discovery *y) {
+    DBusMessage *m;
+    pa_assert(y);
+
+    pa_assert_se(m = dbus_message_new_method_call("org.bluez", "/", "org.freedesktop.DBus.ObjectManager",
+                                                  "GetManagedObjects"));
+    send_and_add_to_pending(y, m, get_managed_objects_reply, NULL);
+}
+
 static int transport_parse_property(pa_bluetooth_transport *t, DBusMessageIter *i) {
     const char *key;
     DBusMessageIter variant_i;
@@ -1023,11 +1070,12 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us
                 pa_log_debug("Bluetooth daemon disappeared.");
                 remove_all_devices(y);
                 y->adapters_listed = false;
+                y->version = BLUEZ_VERSION_UNKNOWN;
             }
 
             if (new_owner && *new_owner) {
                 pa_log_debug("Bluetooth daemon appeared.");
-                list_adapters(y);
+                init_bluez(y);
             }
         }
 
@@ -1710,7 +1758,7 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
     pa_assert_se(dbus_connection_register_object_path(conn, A2DP_SOURCE_ENDPOINT, &vtable_endpoint, y));
     pa_assert_se(dbus_connection_register_object_path(conn, A2DP_SINK_ENDPOINT, &vtable_endpoint, y));
 
-    list_adapters(y);
+    init_bluez(y);
 
     return y;
 



More information about the pulseaudio-commits mailing list