Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH BlueZ v2 1/2] avrcp: Defer init until service discovery completes
@ 2026-11-25 17:02 Simon Mikuda
  2026-11-25 17:02 ` [PATCH BlueZ v2 2/2] avrcp: Check what the remote uses instead of its AVRCP version Simon Mikuda
  2026-11-25 23:53 ` [BlueZ,v2,1/2] avrcp: Defer init until service discovery completes bluez.test.bot
  0 siblings, 2 replies; 3+ messages in thread
From: Simon Mikuda @ 2026-11-25 17:02 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. Until its records are known, report
the events of the AVRCP version implemented here; target_init() then
drops the 1.4 events for a controller older than 1.4, for which they
are RFU. 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
---
Changes in v2:
- 1/2: report the AVRCP 1.4 events only to controllers of 1.4 or later;
  until the remote's record is known, those of the version implemented
  here (Luiz)
- 2/2: accept a registration for EVENT_VOLUME_CHANGED from a remote whose
  version leaves it out of the reported events

 profiles/audio/avrcp.c | 114 ++++++++++++++++++++++++-----------------
 1 file changed, 67 insertions(+), 47 deletions(-)

diff --git a/profiles/audio/avrcp.c b/profiles/audio/avrcp.c
index 2f214e982..0a28b0339 100644
--- a/profiles/audio/avrcp.c
+++ b/profiles/audio/avrcp.c
@@ -141,6 +141,18 @@
 #define AVRCP_CT_VERSION		0x0106
 #define AVRCP_TG_VERSION		0x0106
 
+/* The events a target reports since AVRCP 1.3, and those 1.4 added */
+#define AVRCP_EVENTS_1_3 \
+	((1 << AVRCP_EVENT_STATUS_CHANGED) | \
+	(1 << AVRCP_EVENT_TRACK_CHANGED) | \
+	(1 << AVRCP_EVENT_TRACK_REACHED_END) | \
+	(1 << AVRCP_EVENT_TRACK_REACHED_START) | \
+	(1 << AVRCP_EVENT_SETTINGS_CHANGED))
+#define AVRCP_EVENTS_1_4 \
+	((1 << AVRCP_EVENT_AVAILABLE_PLAYERS_CHANGED) | \
+	(1 << AVRCP_EVENT_ADDRESSED_PLAYER_CHANGED) | \
+	(1 << AVRCP_EVENT_VOLUME_CHANGED))
+
 #define AVRCP_SCOPE_MEDIA_PLAYER_LIST			0x00
 #define AVRCP_SCOPE_MEDIA_PLAYER_VFS			0x01
 #define AVRCP_SCOPE_SEARCH				0x02
@@ -252,6 +264,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 +1627,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 +1655,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 +4274,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 +4358,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 +4368,26 @@ 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);
 
+	/* The events of AVRCP 1.4 are RFU for an older controller */
+	if (target->version < 0x0104)
+		session->supported_events &= ~AVRCP_EVENTS_1_4;
+
 	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 +4414,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 +4445,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 +4485,15 @@ 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);
+	/* Until the remote's records are known, report the events of the
+	 * version implemented here, target_init() limits them to the
+	 * remote's version.
+	 */
+	session->supported_events = AVRCP_EVENTS_1_3 | AVRCP_EVENTS_1_4;
 
-	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 +4529,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] 3+ messages in thread

end of thread, other threads:[~2026-11-25 23:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 17:02 [PATCH BlueZ v2 1/2] avrcp: Defer init until service discovery completes Simon Mikuda
2026-11-25 17:02 ` [PATCH BlueZ v2 2/2] avrcp: Check what the remote uses instead of its AVRCP version Simon Mikuda
2026-11-25 23:53 ` [BlueZ,v2,1/2] avrcp: Defer init until service discovery completes 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