public inbox for linux-bluetooth@vger.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 5/5] shared/bap: Make bt_bap_pac_register to be per session
Date: Thu,  8 Dec 2022 17:03:14 -0800	[thread overview]
Message-ID: <20221209010314.707606-5-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20221209010314.707606-1-luiz.dentz@gmail.com>

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

This makes bt_bap_pac_register to be per session rather than global so
the callback don't have to match the session by itself.
---
 profiles/audio/bap.c |  6 ++--
 src/shared/bap.c     | 66 +++++++++++++++++++++++++-------------------
 src/shared/bap.h     | 11 ++++----
 3 files changed, 45 insertions(+), 38 deletions(-)

diff --git a/profiles/audio/bap.c b/profiles/audio/bap.c
index f28843ae6b38..ae944b617bb4 100644
--- a/profiles/audio/bap.c
+++ b/profiles/audio/bap.c
@@ -120,7 +120,7 @@ static void bap_data_free(struct bap_data *data)
 	queue_destroy(data->streams, NULL);
 	bt_bap_ready_unregister(data->bap, data->ready_id);
 	bt_bap_state_unregister(data->bap, data->state_id);
-	bt_bap_pac_unregister(data->pac_id);
+	bt_bap_pac_unregister(data->bap, data->pac_id);
 	bt_bap_unref(data->bap);
 	free(data);
 }
@@ -1265,8 +1265,8 @@ static int bap_probe(struct btd_service *service)
 								NULL);
 	data->state_id = bt_bap_state_register(data->bap, bap_state,
 						bap_connecting, data, NULL);
-	data->pac_id = bt_bap_pac_register(pac_added, pac_removed, service,
-								NULL);
+	data->pac_id = bt_bap_pac_register(data->bap, pac_added, pac_removed,
+						service, NULL);
 
 	bt_bap_set_user_data(data->bap, service);
 
diff --git a/src/shared/bap.c b/src/shared/bap.c
index 391838a96c55..2919f243f78a 100644
--- a/src/shared/bap.c
+++ b/src/shared/bap.c
@@ -47,6 +47,7 @@
 #define BAP_PROCESS_TIMEOUT 10
 
 struct bt_bap_pac_changed {
+	unsigned int id;
 	bt_bap_pac_func_t added;
 	bt_bap_pac_func_t removed;
 	bt_bap_destroy_func_t destroy;
@@ -165,6 +166,7 @@ struct bt_bap {
 	struct queue *notify;
 	struct queue *streams;
 
+	struct queue *pac_cbs;
 	struct queue *ready_cbs;
 	struct queue *state_cbs;
 
@@ -250,7 +252,6 @@ struct bt_pacs_context {
 
 /* Contains local bt_bap_db */
 static struct queue *bap_db;
-static struct queue *pac_cbs;
 static struct queue *bap_cbs;
 static struct queue *sessions;
 
@@ -268,24 +269,26 @@ static void *iov_append(struct iovec *iov, size_t len, const void *d)
 	return util_iov_push_mem(iov, len, d);
 }
 
-unsigned int bt_bap_pac_register(bt_bap_pac_func_t added,
+unsigned int bt_bap_pac_register(struct bt_bap *bap, bt_bap_pac_func_t added,
 				bt_bap_pac_func_t removed, void *user_data,
 				bt_bap_destroy_func_t destroy)
 {
 	struct bt_bap_pac_changed *changed;
+	static unsigned int id;
+
+	if (!bap)
+		return 0;
 
 	changed = new0(struct bt_bap_pac_changed, 1);
+	changed->id = ++id ? id : ++id;
 	changed->added = added;
 	changed->removed = removed;
 	changed->destroy = destroy;
 	changed->data = user_data;
 
-	if (!pac_cbs)
-		pac_cbs = queue_new();
+	queue_push_tail(bap->pac_cbs, changed);
 
-	queue_push_tail(pac_cbs, changed);
-
-	return queue_length(pac_cbs);
+	return changed->id;
 }
 
 static void pac_changed_free(void *data)
@@ -298,39 +301,28 @@ static void pac_changed_free(void *data)
 	free(changed);
 }
 
-struct match_pac_id {
-	unsigned int id;
-	unsigned int index;
-};
-
-static bool match_index(const void *data, const void *match_data)
+static bool match_pac_changed_id(const void *data, const void *match_data)
 {
-	struct match_pac_id *match = (void *)match_data;
+	const struct bt_bap_pac_changed *changed = data;
+	unsigned int id = PTR_TO_UINT(match_data);
 
-	match->index++;
-
-	return match->id == match->index;
+	return (changed->id == id);
 }
 
-bool bt_bap_pac_unregister(unsigned int id)
+bool bt_bap_pac_unregister(struct bt_bap *bap, unsigned int id)
 {
 	struct bt_bap_pac_changed *changed;
-	struct match_pac_id match;
 
-	memset(&match, 0, sizeof(match));
-	match.id = id;
+	if (!bap)
+		return false;
 
-	changed = queue_remove_if(pac_cbs, match_index, &match);
+	changed = queue_remove_if(bap->pac_cbs, match_pac_changed_id,
+						UINT_TO_PTR(id));
 	if (!changed)
 		return false;
 
 	pac_changed_free(changed);
 
-	if (queue_isempty(pac_cbs)) {
-		queue_destroy(pac_cbs, NULL);
-		pac_cbs = NULL;
-	}
-
 	return true;
 }
 
@@ -2366,6 +2358,13 @@ static void notify_pac_added(void *data, void *user_data)
 		changed->added(pac, changed->data);
 }
 
+static void notify_session_pac_added(void *data, void *user_data)
+{
+	struct bt_bap *bap = data;
+
+	queue_foreach(bap->pac_cbs, notify_pac_added, user_data);
+}
+
 struct bt_bap_pac *bt_bap_add_vendor_pac(struct gatt_db *db,
 					const char *name, uint8_t type,
 					uint8_t id, uint16_t cid, uint16_t vid,
@@ -2402,7 +2401,7 @@ struct bt_bap_pac *bt_bap_add_vendor_pac(struct gatt_db *db,
 		return NULL;
 	}
 
-	queue_foreach(pac_cbs, notify_pac_added, pac);
+	queue_foreach(sessions, notify_session_pac_added, pac);
 
 	return pac;
 }
@@ -2434,6 +2433,13 @@ static void notify_pac_removed(void *data, void *user_data)
 		changed->removed(pac, changed->data);
 }
 
+static void notify_session_pac_removed(void *data, void *user_data)
+{
+	struct bt_bap *bap = data;
+
+	queue_foreach(bap->pac_cbs, notify_pac_removed, user_data);
+}
+
 bool bt_bap_pac_set_ops(struct bt_bap_pac *pac, struct bt_bap_pac_ops *ops,
 					void *user_data)
 {
@@ -2480,7 +2486,7 @@ bool bt_bap_remove_pac(struct bt_bap_pac *pac)
 
 found:
 	queue_foreach(sessions, remove_streams, pac);
-	queue_foreach(pac_cbs, notify_pac_removed, pac);
+	queue_foreach(sessions, notify_session_pac_removed, pac);
 	bap_pac_free(pac);
 	return true;
 }
@@ -2552,6 +2558,7 @@ static void bap_free(void *data)
 
 	bap_db_free(bap->rdb);
 
+	queue_destroy(bap->pac_cbs, pac_changed_free);
 	queue_destroy(bap->ready_cbs, bap_ready_free);
 	queue_destroy(bap->state_cbs, bap_state_free);
 
@@ -2632,6 +2639,7 @@ struct bt_bap *bt_bap_new(struct gatt_db *ldb, struct gatt_db *rdb)
 	bap->reqs = queue_new();
 	bap->pending = queue_new();
 	bap->notify = queue_new();
+	bap->pac_cbs = queue_new();
 	bap->ready_cbs = queue_new();
 	bap->streams = queue_new();
 	bap->state_cbs = queue_new();
diff --git a/src/shared/bap.h b/src/shared/bap.h
index 7b9f88c8320c..3558d0445486 100644
--- a/src/shared/bap.h
+++ b/src/shared/bap.h
@@ -87,12 +87,6 @@ typedef void (*bt_bap_stream_func_t)(struct bt_bap_stream *stream,
 typedef void (*bt_bap_func_t)(struct bt_bap *bap, void *user_data);
 
 /* Local PAC related functions */
-
-unsigned int bt_bap_pac_register(bt_bap_pac_func_t added,
-				bt_bap_pac_func_t removed, void *user_data,
-				bt_bap_destroy_func_t destroy);
-bool bt_bap_pac_unregister(unsigned int id);
-
 struct bt_bap_pac_qos {
 	uint8_t  framing;
 	uint8_t  phy;
@@ -161,6 +155,11 @@ bool bt_bap_set_debug(struct bt_bap *bap, bt_bap_debug_func_t cb,
 bool bap_print_cc(void *data, size_t len, util_debug_func_t func,
 						void *user_data);
 
+unsigned int bt_bap_pac_register(struct bt_bap *bap, bt_bap_pac_func_t added,
+				bt_bap_pac_func_t removed, void *user_data,
+				bt_bap_destroy_func_t destroy);
+bool bt_bap_pac_unregister(struct bt_bap *bap, unsigned int id);
+
 unsigned int bt_bap_ready_register(struct bt_bap *bap,
 				bt_bap_ready_func_t func, void *user_data,
 				bt_bap_destroy_func_t destroy);
-- 
2.37.3


  parent reply	other threads:[~2022-12-09  1:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-09  1:03 [PATCH BlueZ 1/5] shared/att: Fix not requeueing in the same channel Luiz Augusto von Dentz
2022-12-09  1:03 ` [PATCH BlueZ 2/5] shared/bap: Log error message if request cannot be sent Luiz Augusto von Dentz
2022-12-09  1:03 ` [PATCH BlueZ 3/5] shared/bap: Read PAC Sink/Source if respective location is found Luiz Augusto von Dentz
2022-12-09  1:03 ` [PATCH BlueZ 4/5] shared/gatt-db: Allow passing NULL to gatt_db_attribute_write Luiz Augusto von Dentz
2022-12-09  1:03 ` Luiz Augusto von Dentz [this message]
2022-12-09  4:33 ` [BlueZ,1/5] shared/att: Fix not requeueing in the same channel bluez.test.bot
2022-12-09 21:30 ` [PATCH BlueZ 1/5] " 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=20221209010314.707606-5-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