From: Simon Mikuda <simon.mikuda@streamunlimited.com>
To: linux-bluetooth@vger.kernel.org
Cc: Simon Mikuda <simon.mikuda@streamunlimited.com>
Subject: [PATCH BlueZ v4 08/10] avrcp: Fix NumberOfItems set from a reply for another folder
Date: Tue, 24 Nov 2026 13:19:02 +0100 [thread overview]
Message-ID: <20261124121904.3267700-9-simon.mikuda@streamunlimited.com> (raw)
In-Reply-To: <20261124121904.3267700-1-simon.mikuda@streamunlimited.com>
The GetTotalNumberOfItems reply does not say which folder it counts, and
the client may change folder or player before it arrives. The reply then
sets NumberOfItems of whichever folder is current by then.
To reproduce, change folder to /Filesystem, to another folder and back
to /Filesystem before the replies arrive: the reply for the other folder
sets NumberOfItems of /Filesystem.
Keep the player and directory path each request was sent for, and drop
the reply when that directory is no longer the player's current folder.
Fixes: f17d3a2c3b0d ("audio/avrcp: Add support for GetTotalNumberOfItems")
Assisted-by: Claude:claude-opus-5-5
---
profiles/audio/avrcp.c | 65 +++++++++++++++++++++++++++++++---------
profiles/audio/player.c | 6 ++--
profiles/audio/player.h | 1 +
unit/test-media-player.c | 9 ++++--
4 files changed, 62 insertions(+), 19 deletions(-)
diff --git a/profiles/audio/avrcp.c b/profiles/audio/avrcp.c
index f65d394e8..796ab917e 100644
--- a/profiles/audio/avrcp.c
+++ b/profiles/audio/avrcp.c
@@ -252,6 +252,13 @@ struct avrcp {
bool pos_changed_supported;
uint8_t transaction;
struct pending_pdu *pending_pdu;
+ GSList *total_items_reqs;
+};
+
+struct total_items_req {
+ struct avrcp *session;
+ struct avrcp_player *player;
+ char *dir_path;
};
struct passthrough_handler {
@@ -3554,50 +3561,66 @@ static int ct_add_to_nowplaying(struct media_player *mp, const char *name,
return 0;
}
+static void total_items_req_free(void *data)
+{
+ struct total_items_req *req = data;
+
+ g_free(req->dir_path);
+ g_free(req);
+}
+
static gboolean avrcp_get_total_numberofitems_rsp(struct avctp *conn,
uint8_t *operands, size_t operand_count,
void *user_data)
{
struct iovec iov = { operands, operand_count };
- struct avrcp *session = user_data;
- struct avrcp_player *player = session->controller->player;
- struct media_player *mp = player->user_data;
+ struct total_items_req *req = user_data;
+ struct avrcp *session = req->session;
+ struct avrcp_player *player = req->player;
uint32_t num_of_items = 0;
uint16_t uid_counter;
uint8_t status;
- if (operands == NULL)
- return FALSE;
+ session->total_items_reqs = g_slist_remove(session->total_items_reqs,
+ req);
+
+ if (!operands || !g_slist_find(session->controller->players, player))
+ goto free;
if (!avrcp_pull_browsing_header(&iov) ||
!util_iov_pull_u8(&iov, &status))
- return FALSE;
+ goto free;
if (status == AVRCP_STATUS_OUT_OF_BOUNDS)
goto done;
if (status != AVRCP_STATUS_SUCCESS)
- return FALSE;
+ goto free;
if (!util_iov_pull_be16(&iov, &uid_counter) ||
!util_iov_pull_be32(&iov, &num_of_items))
- return FALSE;
+ goto free;
player->uid_counter = uid_counter;
if (!num_of_items)
- return FALSE;
+ goto free;
done:
- media_player_total_items_complete(mp, num_of_items);
+ media_player_total_items_complete(player->user_data, req->dir_path,
+ num_of_items);
+free:
+ total_items_req_free(req);
return FALSE;
}
-static void avrcp_get_total_numberofitems(struct avrcp *session)
+static void avrcp_get_total_numberofitems(struct avrcp *session,
+ const char *dir_path)
{
uint8_t buf[AVRCP_BROWSING_HEADER_LENGTH + 1];
struct avrcp_player *player = session->controller->player;
struct avrcp_browsing_header *pdu = (void *) buf;
+ struct total_items_req *req;
memset(buf, 0, sizeof(buf));
@@ -3606,8 +3629,20 @@ static void avrcp_get_total_numberofitems(struct avrcp *session)
pdu->params[0] = player->scope;
- avctp_send_browsing_req(session->conn, buf, sizeof(buf),
- avrcp_get_total_numberofitems_rsp, session);
+ /* The player and its folder may change before the reply */
+ req = g_new0(struct total_items_req, 1);
+ req->session = session;
+ req->player = player;
+ req->dir_path = g_strdup(dir_path);
+
+ if (avctp_send_browsing_req(session->conn, buf, sizeof(buf),
+ avrcp_get_total_numberofitems_rsp, req) < 0) {
+ total_items_req_free(req);
+ return;
+ }
+
+ session->total_items_reqs = g_slist_prepend(session->total_items_reqs,
+ req);
}
static int ct_get_total_numberofitems(struct media_player *mp, const char *name,
@@ -3631,7 +3666,7 @@ static int ct_get_total_numberofitems(struct media_player *mp, const char *name,
else
player->scope = 0x01;
- avrcp_get_total_numberofitems(session);
+ avrcp_get_total_numberofitems(session, name);
return 0;
}
@@ -4552,6 +4587,8 @@ static void session_destroy(struct avrcp *session, int err)
if (session->position_timer > 0)
timeout_remove(session->position_timer);
+ g_slist_free_full(session->total_items_reqs, total_items_req_free);
+
if (session->controller != NULL)
controller_destroy(session);
diff --git a/profiles/audio/player.c b/profiles/audio/player.c
index 07f4d4f78..9802e52bb 100644
--- a/profiles/audio/player.c
+++ b/profiles/audio/player.c
@@ -758,11 +758,13 @@ done:
}
void media_player_total_items_complete(struct media_player *mp,
- uint32_t num_of_items)
+ const char *dir_path,
+ uint32_t num_of_items)
{
struct media_folder *folder = mp->scope;
- if (folder == NULL)
+ /* The scope may have changed while the request was in flight */
+ if (folder == NULL || g_strcmp0(folder->item->name, dir_path) != 0)
return;
if (folder->number_of_items != num_of_items) {
diff --git a/profiles/audio/player.h b/profiles/audio/player.h
index dcd7df286..ee8c40a67 100644
--- a/profiles/audio/player.h
+++ b/profiles/audio/player.h
@@ -108,6 +108,7 @@ void media_player_change_folder_complete(struct media_player *player,
int ret);
void media_player_search_complete(struct media_player *mp, int ret);
void media_player_total_items_complete(struct media_player *mp,
+ const char *dir_path,
uint32_t num_of_items);
void media_player_set_callbacks(struct media_player *mp,
diff --git a/unit/test-media-player.c b/unit/test-media-player.c
index ef2d5dff3..ba10e5175 100644
--- a/unit/test-media-player.c
+++ b/unit/test-media-player.c
@@ -142,10 +142,12 @@ static void number_of_items_reply(DBusPendingCall *call, void *user_data);
static gboolean complete_total_items(gpointer user_data)
{
+ const char *dir_path = user_data;
+
if (context == NULL)
return FALSE;
- media_player_total_items_complete(context->mp, TOTAL_ITEMS);
+ media_player_total_items_complete(context->mp, dir_path, TOTAL_ITEMS);
call_get_number_of_items(&context->pending2, number_of_items_reply);
@@ -158,7 +160,7 @@ static int test_search(struct media_player *mp, const char *string,
return 0;
}
-static int test_total_items(struct media_player *mp, const char *name,
+static int test_total_items(struct media_player *mp, const char *dir_path,
void *user_data)
{
struct context *ctx = user_data;
@@ -166,7 +168,8 @@ static int test_total_items(struct media_player *mp, const char *name,
ctx->total_items_calls++;
if (ctx->check_total_items)
- g_idle_add(complete_total_items, NULL);
+ g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, complete_total_items,
+ g_strdup(dir_path), g_free);
return 0;
}
--
2.43.0
next prev parent reply other threads:[~2026-11-24 12:19 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 12:18 [PATCH BlueZ v4 00/10] AVRCP browsing fixes Simon Mikuda
2026-11-24 12:18 ` [PATCH BlueZ v4 01/10] avrcp: Fix not checking browsing PDU parameter length Simon Mikuda
2026-11-24 21:03 ` AVRCP browsing fixes bluez.test.bot
2026-11-24 12:18 ` [PATCH BlueZ v4 02/10] avrcp: Fix listing media players up to item 0xffffffff Simon Mikuda
2026-11-24 12:18 ` [PATCH BlueZ v4 03/10] avrcp: Fix byte order of ADDRESSED_PLAYER_CHANGED notification Simon Mikuda
2026-11-24 12:18 ` [PATCH BlueZ v4 04/10] avrcp: Fix byte order of TRACK_CHANGED identifier Simon Mikuda
2026-11-24 12:18 ` [PATCH BlueZ v4 05/10] avrcp: Fix not browsing a player addressed before it was listed Simon Mikuda
2026-11-24 12:19 ` [PATCH BlueZ v4 06/10] player: Fix crash on Search in a folder whose name has a space Simon Mikuda
2026-11-24 12:19 ` [PATCH BlueZ v4 07/10] player: Fix missing Name of listed Now Playing items Simon Mikuda
2026-11-24 12:19 ` Simon Mikuda [this message]
2026-11-24 12:19 ` [PATCH BlueZ v4 09/10] unit/test-media-player: Add test for stale NumberOfItems reply Simon Mikuda
2026-11-24 12:19 ` [PATCH BlueZ v4 10/10] player: Fix Now Playing NumberOfItems when its content changes Simon Mikuda
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=20261124121904.3267700-9-simon.mikuda@streamunlimited.com \
--to=simon.mikuda@streamunlimited.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