Skip to contentSkip to conversation

avrcp: Defer init until service discovery completes

PATCH New Simon Mikuda 1 participant 2 messages Nov 25 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 avrcp: Defer init until service discovery completes

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(-)
profiles/audio/avrcp.c +67 -47
index 2f214e982..0a28b0339 100644
@@ -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

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 2/2 avrcp: Check what the remote uses instead of its AVRCP version

The version in the remote's SDP record does not tell which commands it uses. Android phones such as the Nexus 6P show AVRCP 1.3 in their controller record but set the absolute volume and register for EVENT_VOLUME_CHANGED, and the Huawei Nova 7 registers for it without a controller record that passes avrcp_volume_supported(). Both lose absolute volume control.

Accept SetAbsoluteVolume and a registration for EVENT_VOLUME_CHANGED whenever the remote uses them, even when the supported events listed for its version leave the event out: the registration already tells whether to notify. Ask any remote for its capabilities and register the events it reports, connect browsing on the feature bit alone, and drop the version checks before SetAddressedPlayer and player settings, which exist only for players and settings the remote reported. Sending SetAbsoluteVolume still requires category 2 when VolumeCategory is set.

Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/avrcp.c | 35 +++++++++--------------------------
 1 file changed, 9 insertions(+), 26 deletions(-)
profiles/audio/avrcp.c +9 -26
index 0a28b0339..8e60ca670 100644
@@ -1623,8 +1623,11 @@ static uint8_t avrcp_handle_register_notification(struct avrcp *session,
 	if (len != 5)
 		goto err;
 
-	/* Check if event is supported otherwise reject */
-	if (!(session->supported_events & (1 << pdu->params[0])))
+	/* Check if event is supported otherwise reject, but a remote that
+	 * registers for the volume uses it whatever version it reports
+	 */
+	if (!(session->supported_events & (1 << pdu->params[0])) &&
+			pdu->params[0] != AVRCP_EVENT_VOLUME_CHANGED)
 		goto err;
 
 	switch (pdu->params[0]) {
@@ -1752,10 +1755,10 @@ err:
 	return AVC_CTYPE_REJECTED;
 }
 
-/* SetAbsoluteVolume requires at least version 1.4 and a category-2 */
+/* SetAbsoluteVolume requires a category-2 */
 static bool avrcp_volume_supported(struct avrcp_data *data)
 {
-	if (!data || data->version < 0x0104)
+	if (!data)
 		return false;
 
 	if (btd_opts.avrcp.volume_category &&
@@ -1775,11 +1778,6 @@ static uint8_t avrcp_handle_set_absolute_volume(struct avrcp *session,
 	if (len != 1)
 		goto err;
 
-	if (!avrcp_volume_supported(session->target)) {
-		error("SetAbsoluteVolume not supported");
-		goto err;
-	}
-
 	volume = pdu->params[0] = pdu->params[0] & 0x7F;
 
 	media_transport_set_a2dp_volume(session->dev, volume);
@@ -3096,8 +3094,7 @@ static void avrcp_set_addressed_player(struct avrcp *session,
 static void set_addressed_player(struct avrcp *session,
 					struct avrcp_player *player)
 {
-	if (!player || !player->id || player->addressed ||
-				session->controller->version < 0x0104)
+	if (!player || !player->id || player->addressed)
 		return;
 
 	/* Set player as addressed */
@@ -3149,9 +3146,6 @@ static bool ct_set_setting(struct media_player *mp, const char *key,
 	if (session == NULL)
 		return false;
 
-	if (session->controller->version < 0x0103)
-		return false;
-
 	set_ct_player(session, player);
 
 	attr = attr_to_val(key);
@@ -4385,9 +4379,6 @@ static void target_init(struct avrcp *session)
 		media_transport_set_a2dp_volume(session->dev, init_volume);
 	}
 
-	if (target->version < 0x0104)
-		return;
-
 	/* Only check capabilities if controller is not supported */
 	if (session->controller == NULL)
 		avrcp_get_capabilities(session);
@@ -4431,14 +4422,8 @@ static void controller_init(struct avrcp *session)
 			return;
 	}
 
-	if (controller->version < 0x0103)
-		return;
-
 	avrcp_get_capabilities(session);
 
-	if (controller->version < 0x0104)
-		return;
-
 	if (!(controller->features & AVRCP_FEATURE_BROWSING))
 		return;
 
@@ -4849,10 +4834,8 @@ int avrcp_set_volume(struct btd_device *dev, int8_t volume, bool notify)
 		return -ENOTCONN;
 
 	if (notify) {
-		if (!avrcp_volume_supported(session->target)) {
-			error("EVENT_VOLUME_CHANGED not supported");
+		if (!session->target)
 			return -ENOTSUP;
-		}
 		return avrcp_event(session, AVRCP_EVENT_VOLUME_CHANGED,
 								&volume);
 	}
Signature · 1 line
2.43.0