Linux bluetooth development
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v1 01/11] shared/bass: add a ready callback
Date: Tue, 15 Sep 2026 17:15:18 -0400	[thread overview]
Message-ID: <20260915211528.1223784-2-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20260915211528.1223784-1-luiz.dentz@gmail.com>

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

A BASS client has no way to tell when the session is usable, i.e. when
the service discovery, the reads and the subscriptions triggered by
bt_bass_attach() are complete.

Without it an upper layer may write to the Broadcast Audio Scan Control
Point before the Broadcast Receive State notifications are enabled, and
miss the state changes the write causes, as the notifications are only
sent to clients that have enabled them.

Register for the GATT client idle event and notify the registered
callbacks once it is reached, as done for BAP.

Assisted-by: opencode:claude-opus-5
---
 src/shared/bass.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/bass.h |   6 +++
 2 files changed, 113 insertions(+)

diff --git a/src/shared/bass.c b/src/shared/bass.c
index 6566b89ae368..aad1edad48d8 100644
--- a/src/shared/bass.c
+++ b/src/shared/bass.c
@@ -30,6 +30,13 @@
 
 struct bt_bass_db;
 
+struct bt_bass_ready {
+	unsigned int id;
+	bt_bass_ready_func_t func;
+	bt_bass_destroy_func_t destroy;
+	void *data;
+};
+
 struct bt_bass_cb {
 	unsigned int id;
 	bt_bass_func_t attached;
@@ -67,8 +74,10 @@ struct bt_bass {
 
 	struct queue *src_cbs;
 	struct queue *cp_handlers;
+	struct queue *ready_cbs;
 
 	unsigned int disconn_id;
+	unsigned int idle_id;
 
 	void *user_data;
 };
@@ -1520,6 +1529,8 @@ static void bass_attach_att(struct bt_bass *bass, struct bt_att *att)
 							bass, NULL);
 }
 
+static void bass_idle(void *data);
+
 bool bt_bass_attach(struct bt_bass *bass, struct bt_gatt_client *client)
 {
 	bt_uuid_t uuid;
@@ -1546,6 +1557,14 @@ bool bt_bass_attach(struct bt_bass *bass, struct bt_gatt_client *client)
 
 	bass_attach_att(bass, bt_gatt_client_get_att(client));
 
+	/* Notify the upper layer once the discovery, reads and
+	 * subscriptions are complete, so it does not attempt any
+	 * operation before it is able to receive the notifications it
+	 * would generate.
+	 */
+	bass->idle_id = bt_gatt_client_idle_register(bass->client, bass_idle,
+								bass, NULL);
+
 	bt_uuid16_create(&uuid, BASS_UUID);
 	gatt_db_foreach_service(bass->rdb->db, &uuid, foreach_bass_service,
 				bass);
@@ -1584,6 +1603,11 @@ void bt_bass_detach(struct bt_bass *bass)
 
 	bt_att_unregister_disconnect(att, bass->disconn_id);
 
+	if (bass->idle_id) {
+		bt_gatt_client_idle_unregister(bass->client, bass->idle_id);
+		bass->idle_id = 0;
+	}
+
 	bt_gatt_client_unref(bass->client);
 	bass->client = NULL;
 
@@ -1605,6 +1629,87 @@ static void bass_db_free(void *data)
 	free(bdb);
 }
 
+static void bass_ready_free(void *data)
+{
+	struct bt_bass_ready *ready = data;
+
+	if (ready->destroy)
+		ready->destroy(ready->data);
+
+	free(ready);
+}
+
+static void bass_notify_ready(struct bt_bass *bass)
+{
+	const struct queue_entry *entry;
+
+	bt_bass_ref(bass);
+
+	for (entry = queue_get_entries(bass->ready_cbs); entry;
+							entry = entry->next) {
+		struct bt_bass_ready *ready = entry->data;
+
+		ready->func(bass, ready->data);
+	}
+
+	bt_bass_unref(bass);
+}
+
+static void bass_idle(void *data)
+{
+	struct bt_bass *bass = data;
+
+	bass->idle_id = 0;
+
+	bass_notify_ready(bass);
+}
+
+unsigned int bt_bass_ready_register(struct bt_bass *bass,
+				bt_bass_ready_func_t func, void *user_data,
+				bt_bass_destroy_func_t destroy)
+{
+	struct bt_bass_ready *ready;
+	static unsigned int id;
+
+	if (!bass)
+		return 0;
+
+	ready = new0(struct bt_bass_ready, 1);
+	ready->id = ++id ? id : ++id;
+	ready->func = func;
+	ready->destroy = destroy;
+	ready->data = user_data;
+
+	queue_push_tail(bass->ready_cbs, ready);
+
+	return ready->id;
+}
+
+static bool match_ready_id(const void *data, const void *match_data)
+{
+	const struct bt_bass_ready *ready = data;
+	unsigned int id = PTR_TO_UINT(match_data);
+
+	return (ready->id == id);
+}
+
+bool bt_bass_ready_unregister(struct bt_bass *bass, unsigned int id)
+{
+	struct bt_bass_ready *ready;
+
+	if (!bass)
+		return false;
+
+	ready = queue_remove_if(bass->ready_cbs, match_ready_id,
+						UINT_TO_PTR(id));
+	if (!ready)
+		return false;
+
+	bass_ready_free(ready);
+
+	return true;
+}
+
 static void bass_free(void *data)
 {
 	struct bt_bass *bass = data;
@@ -1614,6 +1719,7 @@ static void bass_free(void *data)
 	queue_destroy(bass->notify, NULL);
 	queue_destroy(bass->src_cbs, bass_src_changed_free);
 	queue_destroy(bass->cp_handlers, bass_cp_handler_free);
+	queue_destroy(bass->ready_cbs, bass_ready_free);
 
 	free(bass);
 }
@@ -1710,6 +1816,7 @@ struct bt_bass *bt_bass_new(struct gatt_db *ldb, struct gatt_db *rdb,
 	bass->notify = queue_new();
 	bass->src_cbs = queue_new();
 	bass->cp_handlers = queue_new();
+	bass->ready_cbs = queue_new();
 
 	if (!rdb)
 		goto done;
diff --git a/src/shared/bass.h b/src/shared/bass.h
index a7b7741db3b7..b617f85396c6 100644
--- a/src/shared/bass.h
+++ b/src/shared/bass.h
@@ -95,6 +95,7 @@ struct bt_bass_remove_src_params {
 
 typedef void (*bt_bass_func_t)(struct bt_bass *bass, void *user_data);
 typedef void (*bt_bass_destroy_func_t)(void *user_data);
+typedef void (*bt_bass_ready_func_t)(struct bt_bass *bass, void *user_data);
 typedef void (*bt_bass_debug_func_t)(const char *str, void *user_data);
 typedef void (*bt_bass_src_func_t)(uint8_t id, uint32_t bid, uint8_t enc,
 				   uint8_t state, uint32_t bis_sync,
@@ -116,6 +117,11 @@ bool bt_bass_set_user_data(struct bt_bass *bass, void *user_data);
 struct bt_bass *bt_bass_ref(struct bt_bass *bass);
 void bt_bass_unref(struct bt_bass *bass);
 bool bt_bass_attach(struct bt_bass *bass, struct bt_gatt_client *client);
+
+unsigned int bt_bass_ready_register(struct bt_bass *bass,
+				bt_bass_ready_func_t func, void *user_data,
+				bt_bass_destroy_func_t destroy);
+bool bt_bass_ready_unregister(struct bt_bass *bass, unsigned int id);
 bool bt_bass_set_att(struct bt_bass *bass, struct bt_att *att);
 void bt_bass_detach(struct bt_bass *bass);
 void bt_bass_add_db(struct gatt_db *db, const bdaddr_t *adapter_bdaddr);
-- 
2.55.0


  reply	other threads:[~2026-09-15 21:15 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-15 21:15 [PATCH BlueZ v1 00/11] Cover a BAP stream to a coordinated set Luiz Augusto von Dentz
2026-09-15 21:15 ` Luiz Augusto von Dentz [this message]
2026-09-15 21:58   ` bluez.test.bot
2026-09-16  0:55   ` bluez.test.bot
2026-09-15 21:15 ` [PATCH BlueZ v1 02/11] bass: wait for the session to be ready before pushing a source Luiz Augusto von Dentz
2026-09-15 21:15 ` [PATCH BlueZ v1 03/11] test: functional: report what a test is waiting for Luiz Augusto von Dentz
2026-09-15 21:15 ` [PATCH BlueZ v1 04/11] test: functional: answer the notification registered by the victim Luiz Augusto von Dentz
2026-09-15 21:15 ` [PATCH BlueZ v1 05/11] doc: describe the BAP coordinated set test cases Luiz Augusto von Dentz
2026-09-15 21:15 ` [PATCH BlueZ v1 06/11] emulator: use the handle of the receiving side Luiz Augusto von Dentz
2026-09-15 21:15 ` [PATCH BlueZ v1 07/11] emulator: allow the maximum number of CIS of a CIG Luiz Augusto von Dentz
2026-09-15 21:15 ` [PATCH BlueZ v1 08/11] device: keep the long term key of both roles Luiz Augusto von Dentz
2026-09-15 21:15 ` [PATCH BlueZ v1 09/11] client: allow a capability with the auto agent Luiz Augusto von Dentz
2026-09-15 21:15 ` [PATCH BlueZ v1 10/11] test: functional: fail as soon as an operation fails Luiz Augusto von Dentz
2026-09-15 21:15 ` [PATCH BlueZ v1 11/11] test: functional: add the BAP coordinated set tests Luiz Augusto von Dentz
2026-09-16 18:30 ` [PATCH BlueZ v1 00/11] Cover a BAP stream to a coordinated set 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=20260915211528.1223784-2-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox