Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH BlueZ 1/2] avrcp: Defer init until service discovery completes
@ 2026-11-24 13:39 Simon Mikuda
  2026-11-24 13:39 ` [PATCH BlueZ 2/2] avrcp: Check what the remote uses instead of its AVRCP version Simon Mikuda
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Simon Mikuda @ 2026-11-24 13:39 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Simon Mikuda

When the remote connects AVCTP before SDP has found its AVRCP records,
as iOS does on the first connection after pairing, neither controller
nor target is initialized and the session stays without them. Wait for
the service discovery with device_wait_for_svc_complete() instead.

The remote uses the session meanwhile: iOS asks for the supported
events and registers for EVENT_VOLUME_CHANGED right away, and would
not get absolute volume otherwise. Report every supported event from
the start, the remote registers only those it uses, and create the
target when the control channel connects so these registrations are
kept; target_init() completes it from the SDP record. A volume
registration with no volume set yet starts at max volume, which
target_init() set only for 1.4 controllers with category 2.

Assisted-by: Claude:claude-opus-5-5
---
 profiles/audio/avrcp.c | 102 ++++++++++++++++++++++-------------------
 1 file changed, 55 insertions(+), 47 deletions(-)

diff --git a/profiles/audio/avrcp.c b/profiles/audio/avrcp.c
index 2f214e982..f7c988ec0 100644
--- a/profiles/audio/avrcp.c
+++ b/profiles/audio/avrcp.c
@@ -252,6 +252,7 @@ struct avrcp {
 	bool pos_changed_supported;
 	uint8_t transaction;
 	struct pending_pdu *pending_pdu;
+	unsigned int svc_complete_id;
 };
 
 struct passthrough_handler {
@@ -1614,12 +1615,6 @@ static uint8_t avrcp_handle_register_notification(struct avrcp *session,
 	if (!(session->supported_events & (1 << pdu->params[0])))
 		goto err;
 
-	if (!session->target) {
-		pdu->params_len = cpu_to_be16(1);
-		pdu->params[0] = AVRCP_STATUS_INVALID_COMMAND;
-		return AVC_CTYPE_REJECTED;
-	}
-
 	switch (pdu->params[0]) {
 	case AVRCP_EVENT_STATUS_CHANGED:
 		len = 2;
@@ -1648,6 +1643,10 @@ static uint8_t avrcp_handle_register_notification(struct avrcp *session,
 		len = 1;
 		break;
 	case AVRCP_EVENT_VOLUME_CHANGED:
+		/* Start at max volume if none has been set yet */
+		if (media_transport_get_a2dp_volume(dev) < 0)
+			media_transport_set_a2dp_volume(dev, 127);
+
 		volume = media_transport_get_a2dp_volume(dev);
 		if (volume < 0)
 			goto err;
@@ -4263,16 +4262,15 @@ static void session_init_browsing(struct avrcp *session)
 							destroy_browsing);
 }
 
-static struct avrcp_data *data_init(struct avrcp *session, const char *uuid)
+static struct avrcp_data *data_init(struct avrcp *session,
+					struct avrcp_data *data,
+					const char *uuid)
 {
-	struct avrcp_data *data;
 	const sdp_record_t *rec;
 	sdp_list_t *list, *protos;
 	sdp_profile_desc_t *desc;
 	int port = 0;
 
-	data = g_new0(struct avrcp_data, 1);
-
 	rec = btd_device_get_record(session->dev, uuid);
 	if (rec == NULL)
 		return data;
@@ -4348,14 +4346,9 @@ static void avrcp_connect_browsing(struct avrcp *session)
 
 static void target_init(struct avrcp *session)
 {
-	struct avrcp_server *server = session->server;
 	struct avrcp_data *target;
-	struct avrcp_player *player;
 	struct btd_service *service;
 
-	if (session->target != NULL)
-		return;
-
 	service = btd_device_get_service(session->dev, AVRCP_REMOTE_UUID);
 
 	if (service == NULL) {
@@ -4363,47 +4356,22 @@ static void target_init(struct avrcp *session)
 		return;
 	}
 
-	target = data_init(session, AVRCP_REMOTE_UUID);
-	session->target = target;
+	target = data_init(session, session->target, AVRCP_REMOTE_UUID);
 
 	DBG("%p version 0x%04x", target, target->version);
 
 	btd_service_connecting_complete(service, 0);
 
-	player = g_slist_nth_data(server->players, 0);
-	if (player != NULL) {
+	if (target->player != NULL) {
 		int8_t init_volume;
-		target->player = player;
-		player->sessions = g_slist_prepend(player->sessions, session);
 
 		init_volume = btd_device_get_volume(session->dev);
 		media_transport_set_a2dp_volume(session->dev, init_volume);
 	}
 
-	session->supported_events |= (1 << AVRCP_EVENT_STATUS_CHANGED) |
-				(1 << AVRCP_EVENT_TRACK_CHANGED) |
-				(1 << AVRCP_EVENT_TRACK_REACHED_START) |
-				(1 << AVRCP_EVENT_TRACK_REACHED_END) |
-				(1 << AVRCP_EVENT_SETTINGS_CHANGED);
-
 	if (target->version < 0x0104)
 		return;
 
-	if (avrcp_volume_supported(target)) {
-		session->supported_events |=
-				(1 << AVRCP_EVENT_VOLUME_CHANGED);
-		/* Check if transport volume hasn't been initialized then set it
-		 * to max so it works properly if the controller attempts to
-		 * subscribe to AVRCP_EVENT_VOLUME_CHANGED.
-		 */
-		if (media_transport_get_a2dp_volume(session->dev) < 0)
-			media_transport_set_a2dp_volume(session->dev, 127);
-	}
-
-	session->supported_events |=
-				(1 << AVRCP_EVENT_ADDRESSED_PLAYER_CHANGED) |
-				(1 << AVRCP_EVENT_AVAILABLE_PLAYERS_CHANGED);
-
 	/* Only check capabilities if controller is not supported */
 	if (session->controller == NULL)
 		avrcp_get_capabilities(session);
@@ -4430,7 +4398,8 @@ static void controller_init(struct avrcp *session)
 		return;
 	}
 
-	controller = data_init(session, AVRCP_TARGET_UUID);
+	controller = data_init(session, g_new0(struct avrcp_data, 1),
+							AVRCP_TARGET_UUID);
 	session->controller = controller;
 
 	DBG("%p version 0x%04x", controller, controller->version);
@@ -4460,8 +4429,35 @@ static void controller_init(struct avrcp *session)
 	avrcp_connect_browsing(session);
 }
 
+static void session_svc_complete(struct btd_device *dev, int err,
+							void *user_data)
+{
+	struct avrcp *session = user_data;
+
+	session->svc_complete_id = 0;
+
+	if (btd_device_get_service(session->dev, AVRCP_TARGET_UUID) != NULL)
+		controller_init(session);
+
+	if (btd_device_get_service(session->dev, AVRCP_REMOTE_UUID) != NULL)
+		target_init(session);
+}
+
 static void session_init_control(struct avrcp *session)
 {
+	struct avrcp_player *player;
+
+	/* The remote may register for events before its records are known,
+	 * target_init() completes the target from them.
+	 */
+	session->target = g_new0(struct avrcp_data, 1);
+
+	player = g_slist_nth_data(session->server->players, 0);
+	if (player != NULL) {
+		session->target->player = player;
+		player->sessions = g_slist_prepend(player->sessions, session);
+	}
+
 	session->passthrough_id = avctp_register_passthrough_handler(
 							session->conn,
 							handle_passthrough,
@@ -4473,11 +4469,19 @@ static void session_init_control(struct avrcp *session)
 							session);
 	session->control_handlers = control_handlers;
 
-	if (btd_device_get_service(session->dev, AVRCP_TARGET_UUID) != NULL)
-		controller_init(session);
+	/* Report every event supported: the remote registers those it uses */
+	session->supported_events |= (1 << AVRCP_EVENT_STATUS_CHANGED) |
+				(1 << AVRCP_EVENT_TRACK_CHANGED) |
+				(1 << AVRCP_EVENT_TRACK_REACHED_START) |
+				(1 << AVRCP_EVENT_TRACK_REACHED_END) |
+				(1 << AVRCP_EVENT_SETTINGS_CHANGED) |
+				(1 << AVRCP_EVENT_ADDRESSED_PLAYER_CHANGED) |
+				(1 << AVRCP_EVENT_AVAILABLE_PLAYERS_CHANGED) |
+				(1 << AVRCP_EVENT_VOLUME_CHANGED);
 
-	if (btd_device_get_service(session->dev, AVRCP_REMOTE_UUID) != NULL)
-		target_init(session);
+	session->svc_complete_id = device_wait_for_svc_complete(session->dev,
+							session_svc_complete,
+							session);
 }
 
 static void controller_destroy(struct avrcp *session)
@@ -4513,6 +4517,10 @@ static void session_destroy(struct avrcp *session, int err)
 
 	session_abort_pending_pdu(session);
 
+	if (session->svc_complete_id)
+		device_remove_svc_complete_callback(session->dev,
+						session->svc_complete_id);
+
 	service = btd_device_get_service(session->dev, AVRCP_TARGET_UUID);
 	if (service != NULL) {
 		if (session->control_id == 0)

base-commit: 1608f836188610aabeff5a3164169ebe0360e443
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-24 21:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 13:39 [PATCH BlueZ 1/2] avrcp: Defer init until service discovery completes Simon Mikuda
2026-11-24 13:39 ` [PATCH BlueZ 2/2] avrcp: Check what the remote uses instead of its AVRCP version Simon Mikuda
2026-11-24 16:15 ` [PATCH BlueZ 1/2] avrcp: Defer init until service discovery completes Luiz Augusto von Dentz
2026-11-24 21:01 ` [BlueZ,1/2] " bluez.test.bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox