[RFC v0 03/15] bluetooth: Parse the tree returned by ObjectManager

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Mikel Astiz <mikel.astiz@xxxxxxxxxxxx>

Parse the result of ObjectManager.GetManagedObjects(), which includes
all objects registered, their interfaces and the corresponding
properties per interface.
---
 src/modules/bluetooth/bluetooth-util.c | 124 +++++++++++++++++++++++++++++++--
 1 file changed, 119 insertions(+), 5 deletions(-)

diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
index fc05f8d..e8f8756 100644
--- a/src/modules/bluetooth/bluetooth-util.c
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -366,7 +366,7 @@ static int parse_device_property(pa_bluetooth_device *d, DBusMessageIter *i) {
 
     dbus_message_iter_recurse(i, &variant_i);
 
-/*     pa_log_debug("Parsing property org.bluez.Device.%s", key); */
+/*    pa_log_debug("Parsing device property '%s'", key); */
 
     switch (dbus_message_iter_get_arg_type(&variant_i)) {
 
@@ -448,6 +448,11 @@ static int parse_device_property(pa_bluetooth_device *d, DBusMessageIter *i) {
                     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", "GetProperties"));
@@ -852,16 +857,20 @@ 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, 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);
+}
+
 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) {
@@ -872,10 +881,98 @@ 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) {
+    DBusMessageIter element_i;
+
+    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) < 0)
+            return -1;
+
+        dbus_message_iter_next(&element_i);
+    }
+
+    d->device_info_valid = d->name && d->address;
+
+    return 0;
+}
+
+static int parse_interfaces_and_properties(pa_bluetooth_discovery *y, DBusMessageIter *dict_i) {
+    DBusMessageIter element_i;
+    const char *path;
+
+    if (dbus_message_iter_get_arg_type(dict_i) != DBUS_TYPE_OBJECT_PATH) {
+        pa_log("Expected D-Bus object path in GetManagedObjects() dictionary.");
+        return -1;
+    }
+
+    dbus_message_iter_get_basic(dict_i, &path);
+
+    if (!dbus_message_iter_next(dict_i) || dbus_message_iter_get_arg_type(dict_i) != DBUS_TYPE_ARRAY) {
+        pa_log("D-Bus interfaces missing for object %s", path);
+        return -1;
+    }
+
+    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);
+
+        if (dbus_message_iter_get_arg_type(&iface_i) != DBUS_TYPE_STRING) {
+            pa_log("Expected interface name in ObjectManager dictionary.");
+            return -1;
+        }
+
+        dbus_message_iter_get_basic(&iface_i, &interface);
+
+        if (!dbus_message_iter_next(&iface_i) || dbus_message_iter_get_arg_type(&iface_i) != DBUS_TYPE_ARRAY) {
+            pa_log("Interface properties missing for %s interface %s", path, interface);
+            return -1;
+        }
+
+        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 address 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) < 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);
@@ -896,6 +993,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) || dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) {
+        pa_log("GetManagedObjects() result is not a dictionary.");
+        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);
+
+        if (parse_interfaces_and_properties(y, &dict_i) < 0)
+            goto finish;
+
+        dbus_message_iter_next(&element_i);
+    }
+
 finish:
     dbus_message_unref(r);
 
-- 
1.7.11.7



[Index of Archives]     [Linux Audio Users]     [AMD Graphics]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux