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

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