shared/gatt-client: Introduce bt_gatt_client_ref_safe
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Mon, 13 Mar 2023 22:51:49 +0000 (15:51 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 5 Jan 2024 10:21:48 +0000 (15:51 +0530)
This introduces bt_gatt_client_ref_save which ensures the instaces
which are being destroyed, e.g. ref_count = 0, do not attempt to reach
callbacks.

src/shared/gatt-client.c

index 1bb1890..8e1fdbc 100644 (file)
@@ -202,9 +202,20 @@ static bool idle_notify(const void *data, const void *user_data)
        return true;
 }
 
+static struct bt_gatt_client *
+bt_gatt_client_ref_safe(struct bt_gatt_client *client)
+{
+       if (!client || !client->ref_count)
+               return NULL;
+
+       return bt_gatt_client_ref(client);
+}
+
 static void notify_client_idle(struct bt_gatt_client *client)
 {
-       bt_gatt_client_ref(client);
+       client = bt_gatt_client_ref_safe(client);
+       if (!client)
+               return;
 
        queue_remove_all(client->idle_cbs, idle_notify, NULL, idle_destroy);
 
@@ -1497,10 +1508,13 @@ static void notify_client_ready(struct bt_gatt_client *client, bool success,
 {
        const struct queue_entry *entry;
 
-       if (client->ready)
+       client = bt_gatt_client_ref_safe(client);
+       if (!client)
                return;
 
-       bt_gatt_client_ref(client);
+       if (client->ready)
+               goto done;
+
        client->ready = success;
 
        if (client->parent)
@@ -1523,6 +1537,7 @@ static void notify_client_ready(struct bt_gatt_client *client, bool success,
                notify_client_ready(clone, success, att_ecode);
        }
 
+done:
        bt_gatt_client_unref(client);
 }