adapter: Track discovery client which initiate an action
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Thu, 11 Jun 2020 20:14:07 +0000 (13:14 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 12 Apr 2021 09:00:49 +0000 (14:30 +0530)
This adds tracking for active clients so it can be properly
removed/freed on the callbacks.

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/adapter.c

index 27172a94a6cddd262bf8349c2fb0b9721d8b501b..8ac3c512e1d886d56debccafe4545263e11180ec 100644 (file)
@@ -317,6 +317,10 @@ struct btd_adapter {
        /* current discovery filter, if any */
        struct mgmt_cp_start_service_discovery *current_discovery_filter;
 
+#ifndef TIZEN_FEATURE_BLUEZ_MODIFY
+       struct watch_client *client;    /* active discovery client */
+#endif
+
        GSList *discovery_found;        /* list of found devices */
        guint discovery_idle_timeout;   /* timeout between discovery runs */
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
@@ -2088,6 +2092,9 @@ static void discovery_free(void *user_data)
 {
        struct watch_client *client = user_data;
 
+#ifndef TIZEN_FEATURE_BLUEZ_MODIFY
+       DBG("%p", client);
+#endif
        if (client->watch)
                g_dbus_remove_watch(dbus_conn, client->watch);
 
@@ -2103,7 +2110,12 @@ static void discovery_free(void *user_data)
        g_free(client);
 }
 
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
 static void discovery_remove(struct watch_client *client, bool exit)
+#else
+static void discovery_remove(struct watch_client *client)
+#endif
+
 {
        struct btd_adapter *adapter = client->adapter;
 
@@ -2115,7 +2127,14 @@ static void discovery_remove(struct watch_client *client, bool exit)
        adapter->discovery_list = g_slist_remove(adapter->discovery_list,
                                                                client);
 
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
        if (!exit && client->discovery_filter)
+#else
+       if (adapter->client == client)
+               adapter->client = NULL;
+
+       if (client->watch && client->discovery_filter)
+#endif
                adapter->set_filter_list = g_slist_prepend(
                                        adapter->set_filter_list, client);
        else
@@ -2133,13 +2152,31 @@ static void discovery_remove(struct watch_client *client, bool exit)
 
 static void trigger_start_discovery(struct btd_adapter *adapter, guint delay);
 
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
 static void discovery_reply(struct watch_client *client, uint8_t status)
+#else
+static struct watch_client *discovery_complete(struct btd_adapter *adapter,
+                                               uint8_t status)
+#endif
 {
+#ifndef TIZEN_FEATURE_BLUEZ_MODIFY
+       struct watch_client *client = adapter->client;
+#endif
        DBusMessage *reply;
+#ifndef TIZEN_FEATURE_BLUEZ_MODIFY
+
+       if (!client)
+               return NULL;
+
+       adapter->client = NULL;
+#endif
 
        if (!client->msg)
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
                return;
-
+#else
+               return client;
+#endif
        if (!status) {
                g_dbus_send_reply(dbus_conn, client->msg, DBUS_TYPE_INVALID);
        } else  {
@@ -2149,6 +2186,9 @@ static void discovery_reply(struct watch_client *client, uint8_t status)
 
        dbus_message_unref(client->msg);
        client->msg = NULL;
+#ifndef TIZEN_FEATURE_BLUEZ_MODIFY
+       return client;
+#endif
 }
 
 static void start_discovery_complete(uint8_t status, uint16_t length,
@@ -2201,8 +2241,11 @@ static void start_discovery_complete(uint8_t status, uint16_t length,
                else
                        adapter->filtered_discovery = false;
 
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
                discovery_reply(client, status);
-
+#else
+               discovery_complete(adapter, status);
+#endif
                if (adapter->discovering)
                        return;
 
@@ -2218,11 +2261,17 @@ static void start_discovery_complete(uint8_t status, uint16_t length,
 #endif
        }
 
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
 fail:
        /* Reply with an error if the first discovery has failed */
        if (client->msg) {
                discovery_reply(client, status);
                discovery_remove(client, false);
+#else
+       client = discovery_complete(adapter, status);
+       if (client) {
+               discovery_remove(client);
+#endif
                return;
        }
 
@@ -2351,8 +2400,10 @@ static gboolean start_discovery_timeout(gpointer user_data)
        struct mgmt_cp_start_discovery cp;
        cp.type = new_type;
        mgmt_send(adapter->mgmt, MGMT_OP_START_DISCOVERY,
-                               adapter->dev_id, sizeof(cp), &cp,
-                               start_discovery_complete, adapter, NULL);
+                                       adapter->dev_id, sizeof(cp), &cp,
+                                       start_discovery_complete, adapter,
+                                       NULL);
+
 
 #else
        /* If we're doing filtered discovery, it must be quickly restarted */
@@ -2411,8 +2462,10 @@ static gboolean start_discovery_timeout(gpointer user_data)
 
                cp.type = new_type;
                mgmt_send(adapter->mgmt, MGMT_OP_START_DISCOVERY,
-                               adapter->dev_id, sizeof(cp), &cp,
-                               start_discovery_complete, adapter, NULL);
+                                       adapter->dev_id, sizeof(cp), &cp,
+                                       start_discovery_complete, adapter,
+                                       NULL);
+
                return FALSE;
        }
 
@@ -2777,18 +2830,9 @@ static void stop_discovery_complete(uint8_t status, uint16_t length,
 
        DBG("status 0x%02x", status);
 
-       /* Is there are no clients the discovery must have been stopped while
-        * discovery command was pending.
-        */
-       if (!adapter->discovery_list)
-               return;
-
-       client = adapter->discovery_list->data;
-
-       discovery_reply(client, status);
-
-       if (status != MGMT_STATUS_SUCCESS)
-               goto done;
+       client = discovery_complete(adapter, status);
+       if (client)
+               discovery_remove(client);
 
        adapter->discovery_type = 0x00;
        adapter->discovery_enable = 0x00;
@@ -2799,9 +2843,6 @@ static void stop_discovery_complete(uint8_t status, uint16_t length,
                                        ADAPTER_INTERFACE, "Discovering");
 
        trigger_passive_scanning(adapter);
-
-done:
-       discovery_remove(client);
 }
 #endif
 
@@ -3037,14 +3078,22 @@ static int update_discovery_filter(struct btd_adapter *adapter)
        return -EINPROGRESS;
 }
 
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
 static int discovery_stop(struct watch_client *client, bool exit)
+#else
+static int discovery_stop(struct watch_client *client)
+#endif
 {
        struct btd_adapter *adapter = client->adapter;
        struct mgmt_cp_stop_discovery cp;
 
        /* Check if there are more client discovering */
        if (g_slist_next(adapter->discovery_list)) {
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
                discovery_remove(client, exit);
+#else
+               discovery_remove(client);
+#endif
                update_discovery_filter(adapter);
                return 0;
        }
@@ -3057,7 +3106,11 @@ static int discovery_stop(struct watch_client *client, bool exit)
         * and so it is enough to send out the signal and just return.
         */
        if (adapter->discovery_enable == 0x00) {
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
                discovery_remove(client, exit);
+#else
+               discovery_remove(client);
+#endif
                adapter->discovering = false;
                g_dbus_emit_property_changed(dbus_conn, adapter->path,
                                ADAPTER_INTERFACE, "Discovering");
@@ -3070,7 +3123,9 @@ static int discovery_stop(struct watch_client *client, bool exit)
         adapter->disc_type = BT_DISC_TYPE_BREDR_ONLY;
 #endif
        cp.type = adapter->discovery_type;
-
+#ifndef TIZEN_FEATURE_BLUEZ_MODIFY
+       adapter->client = client;
+#endif
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
         cp.type = 0x01;
 #endif
@@ -3140,8 +3195,13 @@ static void discovery_disconnect(DBusConnection *conn, void *user_data)
        struct watch_client *client = user_data;
 
        DBG("owner %s", client->owner);
-
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
        discovery_stop(client, true);
+#else
+       client->watch = 0;
+
+       discovery_stop(client);
+#endif
 }
 
 /*
@@ -6351,7 +6411,11 @@ done:
        /* If the discovery has to be started wait it complete to reply */
        if (err == -EINPROGRESS) {
                client->msg = dbus_message_ref(msg);
+#ifndef TIZEN_FEATURE_BLUEZ_MODIFY
+               adapter->client = client;
+#endif
                return NULL;
+
        }
 
        return btd_error_failed(msg, strerror(-err));
@@ -6668,12 +6732,19 @@ static DBusMessage *stop_discovery(DBusConnection *conn,
        if (client->msg)
                return btd_error_busy(msg);
 
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
        err = discovery_stop(client, false);
+#else
+       err = discovery_stop(client);
+#endif
        switch (err) {
                case 0:
                        return dbus_message_new_method_return(msg);
                case -EINPROGRESS:
                        client->msg = dbus_message_ref(msg);
+#ifndef TIZEN_FEATURE_BLUEZ_MODIFY
+                       adapter->client = client;
+#endif
                        return NULL;
                default:
                        return btd_error_failed(msg, strerror(-err));