Blame SOURCES/0001-bluez5-do-NameHasOwner-before-using-org.bluez.patch

7335ea
From e50ec0deb7c20d1daa26cc7eab5a1ff75b9f7bf8 Mon Sep 17 00:00:00 2001
7335ea
From: Wim Taymans <wtaymans@redhat.com>
7335ea
Date: Wed, 17 Nov 2021 12:28:23 +0100
7335ea
Subject: [PATCH] bluez5: do NameHasOwner before using org.bluez
7335ea
7335ea
We should not be using org.bluez when the bluetooth service is not
7335ea
running or else we might try to activate it. The activation of the
7335ea
bluetooth service should be done at boot time.
7335ea
---
7335ea
 src/modules/bluetooth/bluez5-util.c | 61 ++++++++++++++++++++++++++++-
7335ea
 1 file changed, 60 insertions(+), 1 deletion(-)
7335ea
7335ea
diff --git a/src/modules/bluetooth/bluez5-util.c b/src/modules/bluetooth/bluez5-util.c
7335ea
index a21896ede..282886e45 100644
7335ea
--- a/src/modules/bluetooth/bluez5-util.c
7335ea
+++ b/src/modules/bluetooth/bluez5-util.c
7335ea
@@ -1095,6 +1095,65 @@ static void get_managed_objects(pa_bluetooth_discovery *y) {
7335ea
     send_and_add_to_pending(y, m, get_managed_objects_reply, NULL);
7335ea
 }
7335ea
 
7335ea
+static void check_name_owner_reply(DBusPendingCall *pending, void *userdata) {
7335ea
+    pa_dbus_pending *p;
7335ea
+    pa_bluetooth_discovery *y;
7335ea
+    DBusMessage *r;
7335ea
+    DBusError err;
7335ea
+    bool running;
7335ea
+
7335ea
+    pa_assert_se(p = userdata);
7335ea
+    pa_assert_se(y = p->context_data);
7335ea
+    pa_assert_se(r = dbus_pending_call_steal_reply(pending));
7335ea
+
7335ea
+    if (dbus_message_is_error(r, DBUS_ERROR_UNKNOWN_METHOD)) {
7335ea
+        pa_log_warn("BlueZ D-Bus ObjectManager not available");
7335ea
+        goto finish;
7335ea
+    }
7335ea
+
7335ea
+    if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
7335ea
+        pa_log_error("NameHasOwner() failed: %s: %s", dbus_message_get_error_name(r), pa_dbus_get_error_message(r));
7335ea
+        goto finish;
7335ea
+    }
7335ea
+
7335ea
+    if (!pa_streq(dbus_message_get_signature(r), "b")) {
7335ea
+        pa_log_error("Invalid reply signature for NameHasOwner()");
7335ea
+        goto finish;
7335ea
+    }
7335ea
+
7335ea
+    dbus_error_init(&err;;
7335ea
+    if (!dbus_message_get_args(r, &err, DBUS_TYPE_BOOLEAN, &running, DBUS_TYPE_INVALID)) {
7335ea
+        pa_log_error("Could not check bluetooth service: %s", err.message);
7335ea
+        dbus_error_free(&err;;
7335ea
+        goto finish;
7335ea
+   }
7335ea
+
7335ea
+   pa_log_info("bluetooth service running: %s", running ? "yes" : "no");
7335ea
+   if (running)
7335ea
+       get_managed_objects(y);
7335ea
+
7335ea
+finish:
7335ea
+    dbus_message_unref(r);
7335ea
+
7335ea
+    PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
7335ea
+    pa_dbus_pending_free(p);
7335ea
+}
7335ea
+
7335ea
+static void check_name_owner(pa_bluetooth_discovery *y) {
7335ea
+    DBusMessage *m;
7335ea
+    const char *service = BLUEZ_SERVICE;
7335ea
+
7335ea
+    pa_assert(y);
7335ea
+
7335ea
+    pa_assert_se(m = dbus_message_new_method_call("org.freedesktop.DBus",
7335ea
+                            "/org/freedesktop/DBus",
7335ea
+                            "org.freedesktop.DBus",
7335ea
+                            "NameHasOwner"));
7335ea
+    dbus_message_append_args(m, DBUS_TYPE_STRING, &service, DBUS_TYPE_INVALID);
7335ea
+
7335ea
+    send_and_add_to_pending(y, m, check_name_owner_reply, NULL);
7335ea
+}
7335ea
+
7335ea
 pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook) {
7335ea
     pa_assert(y);
7335ea
     pa_assert(PA_REFCNT_VALUE(y) > 0);
7335ea
@@ -1653,7 +1712,7 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c, int headset_backe
7335ea
         pa_xfree(endpoint);
7335ea
     }
7335ea
 
7335ea
-    get_managed_objects(y);
7335ea
+    check_name_owner(y);
7335ea
 
7335ea
     return y;
7335ea
 
7335ea
-- 
7335ea
2.36.1
7335ea