All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ] client/player: Fix attempting to acquire already acquired transport
Date: Wed, 28 Sep 2022 14:26:49 -0700	[thread overview]
Message-ID: <20220928212649.3824261-1-luiz.dentz@gmail.com> (raw)

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

If the transport has links check if the link is acquiring before
attempting to call Acquire otherwise it may cause an error to be
printed.
---
 client/player.c | 70 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 65 insertions(+), 5 deletions(-)

diff --git a/client/player.c b/client/player.c
index 30ae263c8e41..4324089345c1 100644
--- a/client/player.c
+++ b/client/player.c
@@ -65,6 +65,7 @@ struct endpoint {
 	uint8_t codec;
 	struct iovec *caps;
 	bool auto_accept;
+	bool acquiring;
 	uint8_t cig;
 	uint8_t cis;
 	char *transport;
@@ -2688,6 +2689,30 @@ static struct endpoint *find_ep_by_transport(const char *path)
 	return NULL;
 }
 
+static struct endpoint *find_link_by_proxy(GDBusProxy *proxy)
+{
+	DBusMessageIter iter, array;
+
+	if (!g_dbus_proxy_get_property(proxy, "Links", &iter))
+		return NULL;
+
+	dbus_message_iter_recurse(&iter, &array);
+
+	while (dbus_message_iter_get_arg_type(&array) ==
+				DBUS_TYPE_OBJECT_PATH) {
+		const char *transport;
+		struct endpoint *link;
+
+		dbus_message_iter_get_basic(&array, &transport);
+
+		link = find_ep_by_transport(transport);
+		if (link)
+			return link;
+	}
+
+	return NULL;
+}
+
 static void transport_close(struct transport *transport)
 {
 	if (transport->fd < 0)
@@ -2769,10 +2794,19 @@ static void transport_new(GDBusProxy *proxy, int sk, uint16_t mtu[2])
 static void acquire_reply(DBusMessage *message, void *user_data)
 {
 	GDBusProxy *proxy = user_data;
+	struct endpoint *ep, *link;
 	DBusError error;
 	int sk;
 	uint16_t mtu[2];
 
+	ep = find_ep_by_transport(g_dbus_proxy_get_path(proxy));
+	if (ep) {
+		ep->acquiring = false;
+		link = find_link_by_proxy(proxy);
+		if (link)
+			link->acquiring = false;
+	}
+
 	dbus_error_init(&error);
 
 	if (dbus_set_error_from_message(&error, message) == TRUE) {
@@ -2803,11 +2837,22 @@ static void acquire_reply(DBusMessage *message, void *user_data)
 static void transport_acquire(const char *input, void *user_data)
 {
 	GDBusProxy *proxy = user_data;
+	struct endpoint *ep, *link;
 
 	if (!strcasecmp(input, "y") || !strcasecmp(input, "yes")) {
-		if (!g_dbus_proxy_method_call(proxy, "Acquire", NULL,
+		if (g_dbus_proxy_method_call(proxy, "Acquire", NULL,
 						acquire_reply, proxy, NULL))
-			bt_shell_printf("Failed acquire transport\n");
+			return;
+		bt_shell_printf("Failed acquire transport\n");
+	}
+
+	/* Reset acquiring */
+	ep = find_ep_by_transport(g_dbus_proxy_get_path(proxy));
+	if (ep) {
+		ep->acquiring = false;
+		link = find_link_by_proxy(proxy);
+		if (link)
+			link->acquiring = false;
 	}
 }
 
@@ -2815,7 +2860,7 @@ static void transport_property_changed(GDBusProxy *proxy, const char *name,
 						DBusMessageIter *iter)
 {
 	char *str;
-	struct endpoint *ep;
+	struct endpoint *ep, *link;
 
 	str = proxy_description(proxy, "Transport", COLORED_CHG);
 	print_iter(str, name, iter);
@@ -2833,14 +2878,29 @@ static void transport_property_changed(GDBusProxy *proxy, const char *name,
 	 * endpoint.
 	 */
 	ep = find_ep_by_transport(g_dbus_proxy_get_path(proxy));
-	if (!ep)
+	if (!ep || ep->acquiring)
 		return;
 
+	ep->acquiring = true;
+
+	link = find_link_by_proxy(proxy);
+	if (link) {
+		bt_shell_printf("Link %s found\n", link->transport);
+		/* If link already acquiring wait it to be complete */
+		if (link->acquiring)
+			return;
+		link->acquiring = true;
+	}
+
 	if (ep->auto_accept) {
 		bt_shell_printf("Auto Acquiring...\n");
 		if (!g_dbus_proxy_method_call(proxy, "Acquire", NULL,
-						acquire_reply, proxy, NULL))
+						acquire_reply, proxy, NULL)) {
 			bt_shell_printf("Failed acquire transport\n");
+			ep->acquiring = false;
+			if (link)
+				link->acquiring = false;
+		}
 		return;
 	}
 
-- 
2.37.3


             reply	other threads:[~2022-09-28 21:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-28 21:26 Luiz Augusto von Dentz [this message]
2022-09-28 22:28 ` [BlueZ] client/player: Fix attempting to acquire already acquired transport bluez.test.bot
2022-09-29 21:00 ` [PATCH BlueZ] " patchwork-bot+bluetooth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220928212649.3824261-1-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.