client/player: Fix attempting to acquire already acquired transport
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Wed, 28 Sep 2022 20:54:47 +0000 (13:54 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:55 +0000 (14:55 +0530)
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.

Signed-off-by: Manika Shrivastava <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
client/player.c

index 70a2d11..996da26 100644 (file)
@@ -64,6 +64,7 @@ struct endpoint {
        uint8_t codec;
        struct iovec *caps;
        bool auto_accept;
+       bool acquiring;
        uint8_t cig;
        uint8_t cis;
        char *transport;
@@ -2687,6 +2688,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)
@@ -2768,10 +2793,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) {
@@ -2802,11 +2836,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;
        }
 }
 
@@ -2814,7 +2859,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);
@@ -2832,14 +2877,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;
        }