Skip to contentSkip to conversation

mcp: support multiple MCP and implement local GMCS

PATCH Superseded Pauli Virtanen 2 participants 11 messages Dec 12, 2025

Subsystems (2)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover mcp: support multiple MCP and implement local GMCS

v6:
- use only rewritten bt_uinput to be able to change license
- make all patches buildable, combining commits
- in that, split out mcp: add support for Repeat & Shuffle
- fixup: re-read of values on track change if remote doesn't notify
- fixup: make sure other values are read only after CCID

v5:
- fix -D_FORTIFY_SOURCE false positive vs. strncpy in the old avctp code in testbot environment

v4:
- move uinput-util.h to src/shared + adapt accordingly
- improve debug logs in profile

v3:
- add missing #include to fix build in the test environment

v2:
- fix compilation issues
- remove unnecessary code in get_media_cp_op_supported

***

For Media Control Client, add support for multiple GMCS / MCS services on the server. Revise the API accordingly.

For Media Control Server, make it a non-stub implementation (OTS still missing), and add an API the profile can use.

Add tests.

This changes the design of the previous MCP code, so it's mostly a rewrite.

The shared/mcp: commit doesn't fix the API in profiles/audio, that's done in the later mcp: commit, as it's a rewrite of the API.

TODO (for later): OTP/OTS parts -- requires design for OTP/OTS implementation first

TODO (for later): MPRIS integration to GMCS server -- this needs some redesign in media.c as it's hardcoded to AVRCP there.

Tested devices (MCS):
- Creative Zen Hybrid Pro (doesn't even connect without GMCS)
- Samsung Galaxy Buds Pro2
- Creative Aurvana Ace 2

Tested devices (MCP):
- Samsung Galaxy A56

Pauli Virtanen (9):
shared/mcp, mcp: support multiple MCP, and add non-stub MCS server
test-mcp: add tests for MCP / MCS
mcp: add support for Repeat & Shuffle for remote player shared/uinput: add uinput utility functions
avctp: use uinput utilities from src/shared
mcp: add local GMCS service that emits uinput media keys shared/mcp: on track changed, re-read values if notify not available
test-mcp: check attributes are reread on track change if no notify shared/mcp: complete CCID read before considering other attributes

 .gitignore             |    1 +
 Makefile.am            |    9 +-
 lib/bluetooth/uuid.h   |   27 +-
 profiles/audio/avctp.c |  166 +--
 profiles/audio/mcp.c   |  808 +++++++---
 src/shared/mcp.c       | 3216 +++++++++++++++++++++++-----------------
 src/shared/mcp.h       |  186 ++-
 src/shared/mcs.h       |   51 +-
 src/shared/uinput.c    |  204 +++
 src/shared/uinput.h    |   32 +
 unit/test-mcp.c        | 1859 +++++++++++++++++++++++
 11 files changed, 4802 insertions(+), 1757 deletions(-)
 create mode 100644 src/shared/uinput.c
 create mode 100644 src/shared/uinput.h
 create mode 100644 unit/test-mcp.c
Signature · 1 line
2.51.1
#1 lore raw

PATCH v6 1/9 shared/mcp, mcp: support multiple MCP, and add non-stub MCS server

For Media Control Client, add support for multiple GMCS / MCS services on the server. Revise the API accordingly.

For Media Control Server, make it a complete implementation (OTS still missing), and add an API the profile can use.

This is mostly a complete rewrite.

Adapt also profile/mcp.c to use the new API, adding support for multiple MCS services on the remote side.

---
 lib/bluetooth/uuid.h |   27 +-
 profiles/audio/mcp.c |  518 +++----
 src/shared/mcp.c     | 3214 ++++++++++++++++++++++++------------------
 src/shared/mcp.h     |  186 ++-
 src/shared/mcs.h     |   51 +-
 5 files changed, 2362 insertions(+), 1634 deletions(-)
lib/bluetooth/uuid.h +14 -13
index 805366c3d..74bd83742 100644
@@ -198,20 +198,21 @@ extern "C" {
 #define	AICS_AUDIO_INPUT_CP_CHRC_UUID		0X2B7B
 #define	AICS_INPUT_DESCR_CHAR_UUID		0X2B7C
 
+#define MCS_UUID                                0x1848
 #define GMCS_UUID                               0x1849
-#define MEDIA_PLAYER_NAME_CHRC_UUID             0x2b93
-#define MEDIA_TRACK_CHNGD_CHRC_UUID             0x2b96
-#define MEDIA_TRACK_TITLE_CHRC_UUID             0x2b97
-#define MEDIA_TRACK_DURATION_CHRC_UUID          0x2b98
-#define MEDIA_TRACK_POSTION_CHRC_UUID           0x2b99
-#define MEDIA_PLAYBACK_SPEED_CHRC_UUID          0x2b9a
-#define MEDIA_SEEKING_SPEED_CHRC_UUID           0x2b9b
-#define MEDIA_PLAYING_ORDER_CHRC_UUID           0x2ba1
-#define MEDIA_PLAY_ORDER_SUPPRTD_CHRC_UUID      0x2ba2
-#define MEDIA_STATE_CHRC_UUID                   0x2ba3
-#define MEDIA_CP_CHRC_UUID                      0x2ba4
-#define MEDIA_CP_OP_SUPPORTED_CHRC_UUID         0x2ba5
-#define MEDIA_CONTENT_CONTROL_ID_CHRC_UUID      0x2bba
+#define MCS_MEDIA_PLAYER_NAME_CHRC_UUID         0x2b93
+#define MCS_TRACK_CHANGED_CHRC_UUID             0x2b96
+#define MCS_TRACK_TITLE_CHRC_UUID               0x2b97
+#define MCS_TRACK_DURATION_CHRC_UUID            0x2b98
+#define MCS_TRACK_POSITION_CHRC_UUID            0x2b99
+#define MCS_PLAYBACK_SPEED_CHRC_UUID            0x2b9a
+#define MCS_SEEKING_SPEED_CHRC_UUID             0x2b9b
+#define MCS_PLAYING_ORDER_CHRC_UUID             0x2ba1
+#define MCS_PLAYING_ORDER_SUPPORTED_CHRC_UUID   0x2ba2
+#define MCS_MEDIA_STATE_CHRC_UUID               0x2ba3
+#define MCS_MEDIA_CP_CHRC_UUID                  0x2ba4
+#define MCS_MEDIA_CP_OP_SUPPORTED_CHRC_UUID     0x2ba5
+#define MCS_CCID_CHRC_UUID                      0x2bba
 
 /* Telephony and Media Audio Service */
 #define TMAS_UUID_STR		"00001855-0000-1000-8000-00805f9b34fb"
profiles/audio/mcp.c +277 -241

634 lines not on this page · raw message

src/shared/mcp.c +1878 -1336

2,332 lines not on this page · raw message

src/shared/mcp.h +146 -40

186 lines not on this page · raw message

src/shared/mcs.h +47 -4

51 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.51.1

Part of the diff is not shown here. View the raw message.

#2 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1032730

---Test result---

Test Summary:

CheckPatch                    PENDING   0.37 seconds
GitLint                       PENDING   0.43 seconds
BuildEll                      PASS      20.11 seconds
BluezMake                     FAIL      19.07 seconds
MakeCheck                     FAIL      41.80 seconds
MakeDistcheck                 PASS      244.36 seconds
CheckValgrind                 FAIL      15.57 seconds
CheckSmatch                   FAIL      21.33 seconds
bluezmakeextell               FAIL      13.43 seconds
IncrementalBuild              PENDING   0.36 seconds
ScanBuild                     FAIL      34.85 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: BluezMake - FAIL
Desc: Build BlueZ
Output:

src/shared/uinput.c: In function ‘uinput_emit’:
src/shared/uinput.c:67:2: error: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Werror=unused-result]

   67 |  write(uinput->fd, &ie, sizeof(ie));
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
make[1]: *** [Makefile:7984: src/shared/libshared_mainloop_la-uinput.lo] Error 1 make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:4239: all] Error 2
##############################
Test: MakeCheck - FAIL
Desc: Run Bluez Make Check
Output:

src/shared/uinput.c: In function ‘uinput_emit’:
src/shared/uinput.c:67:2: error: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Werror=unused-result]

   67 |  write(uinput->fd, &ie, sizeof(ie));
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
make[1]: *** [Makefile:7711: src/shared/libshared_glib_la-uinput.lo] Error 1
make: *** [Makefile:11000: check] Error 2
##############################
Test: CheckValgrind - FAIL
Desc: Run Bluez Make Check with Valgrind
Output:

src/shared/uinput.c: In function ‘uinput_emit’:
src/shared/uinput.c:67:2: error: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Werror=unused-result]

   67 |  write(uinput->fd, &ie, sizeof(ie));
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
make[1]: *** [Makefile:7984: src/shared/libshared_mainloop_la-uinput.lo] Error 1 make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:11000: check] Error 2
##############################
Test: CheckSmatch - FAIL
Desc: Run smatch tool with source
Output:

src/shared/crypto.c:271:21: warning: Variable length array is used.
src/shared/crypto.c:272:23: warning: Variable length array is used.
src/shared/gatt-helpers.c:768:31: warning: Variable length array is used.
src/shared/gatt-helpers.c:830:31: warning: Variable length array is used.
src/shared/gatt-helpers.c:1323:31: warning: Variable length array is used.
src/shared/gatt-helpers.c:1354:23: warning: Variable length array is used.
src/shared/gatt-server.c:278:25: warning: Variable length array is used.
src/shared/gatt-server.c:618:25: warning: Variable length array is used.
src/shared/gatt-server.c:716:25: warning: Variable length array is used.
src/shared/bap.c:312:25: warning: array of flexible structures
src/shared/bap.c: note: in included file:
./src/shared/ascs.h:88:25: warning: array of flexible structures
src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):
/usr/include/readline/rltypedefs.h:35:23: warning: non-ANSI function declaration of function 'Function' /usr/include/readline/rltypedefs.h:36:25: warning: non-ANSI function declaration of function 'VFunction' /usr/include/readline/rltypedefs.h:37:27: warning: non-ANSI function declaration of function 'CPFunction' /usr/include/readline/rltypedefs.h:38:29: warning: non-ANSI function declaration of function 'CPPFunction' src/shared/uinput.c: In function ‘uinput_emit’:
src/shared/uinput.c:67:2: error: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Werror=unused-result]

   67 |  write(uinput->fd, &ie, sizeof(ie));
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
make[1]: *** [Makefile:7984: src/shared/libshared_mainloop_la-uinput.lo] Error 1 make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:4239: all] Error 2
##############################
Test: bluezmakeextell - FAIL
Desc: Build Bluez with External ELL
Output:

src/shared/uinput.c: In function ‘uinput_emit’:
src/shared/uinput.c:67:2: error: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Werror=unused-result]

   67 |  write(uinput->fd, &ie, sizeof(ie));
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
make[1]: *** [Makefile:7984: src/shared/libshared_mainloop_la-uinput.lo] Error 1 make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:4239: all] Error 2
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series
Output:

##############################
Test: ScanBuild - FAIL
Desc: Run Scan Build
Output:

src/shared/gatt-client.c:451:21: warning: Use of memory after it is freed

        gatt_db_unregister(op->client->db, op->db_id);
                           ^~~~~~~~~~
src/shared/gatt-client.c:696:2: warning: Use of memory after it is freed
        discovery_op_complete(op, false, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:996:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1102:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1296:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1361:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1636:6: warning: Use of memory after it is freed
        if (read_db_hash(op)) {
            ^~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1641:2: warning: Use of memory after it is freed
        discover_all(op);
        ^~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1697:56: warning: Use of memory after it is freed
        notify_data->chrc->ccc_write_id = notify_data->att_id = att_id;
                                          ~~~~~~~~~~~~~~~~~~~ ^
src/shared/gatt-client.c:2150:6: warning: Use of memory after it is freed
        if (read_db_hash(op)) {
            ^~~~~~~~~~~~~~~~
src/shared/gatt-client.c:2158:8: warning: Use of memory after it is freed
                                                        discovery_op_ref(op),
                                                        ^~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:3183:2: warning: Use of memory after it is freed
        complete_write_long_op(req, success, 0, false);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:3205:2: warning: Use of memory after it is freed
        request_unref(req);
        ^~~~~~~~~~~~~~~~~~
13 warnings generated.
src/shared/uinput.c: In function ‘uinput_emit’:
src/shared/uinput.c:67:2: error: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Werror=unused-result]
   67 |  write(uinput->fd, &ie, sizeof(ie));
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
make[1]: *** [Makefile:7984: src/shared/libshared_mainloop_la-uinput.lo] Error 1 make[1]: *** Waiting for unfinished jobs.... src/shared/bap.c:1529:8: warning: Use of memory after it is freed

        bap = bt_bap_ref_safe(bap);
              ^~~~~~~~~~~~~~~~~~~~
src/shared/bap.c:2323:20: warning: Use of memory after it is freed
        return queue_find(stream->bap->streams, NULL, stream);
                          ^~~~~~~~~~~~~~~~~~~~

2 warnings generated.
make: *** [Makefile:4239: all] Error 2

---
Regards,
Linux Bluetooth

#3 lore raw

PATCH v6 2/9 test-mcp: add tests for MCP / MCS

In reply to Pauli Virtanen

Add tests for the Media Control Client / Server implementation.

This contains basic GGIT and state transition tests. Not all state transition tests are here, as they'd largely test the upper layer of the profile which is not tested now.

---
 .gitignore      |    1 +
 Makefile.am     |    6 +
 unit/test-mcp.c | 1807 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1814 insertions(+)
 create mode 100644 unit/test-mcp.c
.gitignore +1 -0
index 80427e1dd..f8e4ad686 100644
@@ -120,6 +120,7 @@ unit/test-bass
 unit/test-battery
 unit/test-tmap
 unit/test-gmap
+unit/test-mcp
 unit/test-profile
 tools/mgmt-tester
 tools/smp-tester
Makefile.am +6 -0
index 060a90cfc..795d0d403 100644
@@ -731,6 +731,12 @@ unit_test_gmap_SOURCES = unit/test-gmap.c $(btio_sources)
 unit_test_gmap_LDADD = src/libshared-glib.la \
 				lib/libbluetooth-internal.la $(GLIB_LIBS)
 
+unit_tests += unit/test-mcp
+
+unit_test_mcp_SOURCES = unit/test-mcp.c $(btio_sources)
+unit_test_mcp_LDADD = src/libshared-glib.la \
+				lib/libbluetooth-internal.la $(GLIB_LIBS)
+
 unit_tests += unit/test-battery
 
 unit_test_battery_SOURCES = unit/test-battery.c
unit/test-mcp.c new file +1807 -0

1,807 lines not on this page · raw message

Signature · 1 line
2.51.1
#4 lore raw

PATCH v6 3/9 mcp: add support for Repeat & Shuffle for remote player

In reply to Pauli Virtanen

Add support for toggling remote player playing order settings.

These do not fully map to current org.bluez.MediaPlayer Repeat/Shuffle, but try best effort mapping.

---
 profiles/audio/mcp.c | 118 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 118 insertions(+)
profiles/audio/mcp.c +118 -0
index a4cb9e9d8..48e65d497 100644
@@ -185,6 +185,47 @@ static void remote_track_position(void *data, int32_t position_centisec)
 	media_player_set_position(remote->mp, position_centisec * 10);
 }
 
+static const struct {
+	uint16_t basic;
+	uint16_t repeat;
+	bool shuffle;
+	bool single;
+} playing_orders[] = {
+	{ BT_MCS_ORDER_SINGLE_ONCE, BT_MCS_ORDER_SINGLE_REPEAT,
+	  .single = true },
+	{ BT_MCS_ORDER_IN_ORDER_ONCE, BT_MCS_ORDER_IN_ORDER_REPEAT },
+	{ BT_MCS_ORDER_OLDEST_ONCE, BT_MCS_ORDER_OLDEST_REPEAT },
+	{ BT_MCS_ORDER_NEWEST_ONCE, BT_MCS_ORDER_NEWEST_REPEAT },
+	{ BT_MCS_ORDER_SHUFFLE_ONCE, BT_MCS_ORDER_SHUFFLE_REPEAT,
+	  .shuffle = true },
+};
+
+static void remote_playing_order(void *data, uint8_t order)
+{
+	struct remote_player *remote = data;
+	const char *repeat = "off";
+	unsigned int i;
+	bool shuffle;
+
+	remote->playing_order = order;
+
+	for (i = 0; i < ARRAY_SIZE(playing_orders); ++i) {
+		shuffle = playing_orders[i].shuffle;
+		if (order == playing_orders[i].basic) {
+			break;
+		} else if (order == playing_orders[i].repeat) {
+			repeat = playing_orders[i].single ? "singletrack" :
+				"alltracks";
+			break;
+		}
+	}
+	if (i == ARRAY_SIZE(playing_orders))
+		return;
+
+	media_player_set_setting(remote->mp, "Repeat", repeat);
+	media_player_set_setting(remote->mp, "Shuffle", shuffle ? "on" : "off");
+}
+
 static void remote_media_state(void *data, uint8_t status)
 {
 	struct remote_player *remote = data;
@@ -206,6 +247,7 @@ static const struct bt_mcp_listener_callback remote_cb = {
 	.track_title = remote_track_title,
 	.track_duration = remote_track_duration,
 	.track_position = remote_track_position,
+	.playing_order = remote_playing_order,
 	.media_state = remote_media_state,
 	.destroy = remote_destroy,
 };
@@ -245,12 +287,88 @@ static int remote_mp_previous(struct media_player *mp, void *user_data)
 	return bt_mcp_previous_track(remote->mcp, remote->ccid);
 }
 
+static bool remote_mp_set_setting(struct media_player *mp, const char *key,
+					const char *value, void *user_data)
+{
+	struct remote_player *remote = user_data;
+	unsigned int i;
+
+	if (strcmp(key, "Repeat") == 0) {
+		bool repeat = (strcmp(value, "alltracks") == 0);
+		uint8_t order = remote->playing_order;
+
+		/* Some sensible mapping, 1-to-1 not possible */
+		for (i = 0; i < ARRAY_SIZE(playing_orders); ++i) {
+			if (order == playing_orders[i].basic) {
+				if (repeat)
+					order = playing_orders[i].repeat;
+				break;
+			} else if (order == playing_orders[i].repeat) {
+				if (!repeat)
+					order = playing_orders[i].basic;
+				break;
+			}
+		}
+
+		if (strcmp(value, "singletrack") == 0)
+			order = BT_MCS_ORDER_SINGLE_REPEAT;
+
+		DBG("Set Repeat %s -> 0x%02x", value, order);
+
+		if (order == remote->playing_order)
+			return true;
+		return bt_mcp_set_playing_order(remote->mcp, remote->ccid,
+									order);
+	}
+
+	if (strcmp(key, "Shuffle") == 0) {
+		bool shuffle = (strcmp(value, "off") != 0);
+		uint8_t order = remote->playing_order;
+
+		/* Some sensible mapping, 1-to-1 not possible */
+		switch (order) {
+		case BT_MCS_ORDER_SHUFFLE_ONCE:
+			if (!shuffle)
+				order = BT_MCS_ORDER_IN_ORDER_ONCE;
+			break;
+		case BT_MCS_ORDER_SHUFFLE_REPEAT:
+			if (!shuffle)
+				order = BT_MCS_ORDER_IN_ORDER_REPEAT;
+			break;
+		case BT_MCS_ORDER_SINGLE_ONCE:
+		case BT_MCS_ORDER_IN_ORDER_ONCE:
+		case BT_MCS_ORDER_OLDEST_ONCE:
+		case BT_MCS_ORDER_NEWEST_ONCE:
+			if (shuffle)
+				order = BT_MCS_ORDER_SHUFFLE_ONCE;
+			break;
+		case BT_MCS_ORDER_SINGLE_REPEAT:
+		case BT_MCS_ORDER_IN_ORDER_REPEAT:
+		case BT_MCS_ORDER_OLDEST_REPEAT:
+		case BT_MCS_ORDER_NEWEST_REPEAT:
+			if (shuffle)
+				order = BT_MCS_ORDER_SHUFFLE_REPEAT;
+			break;
+		}
+
+		DBG("Set Shuffle %s -> 0x%02x", value, order);
+
+		if (order == remote->playing_order)
+			return true;
+		return bt_mcp_set_playing_order(remote->mcp, remote->ccid,
+									order);
+	}
+
+	return false;
+}
+
 static const struct media_player_callback remote_mp_cb = {
 	.play		= remote_mp_play,
 	.pause		= remote_mp_pause,
 	.stop		= remote_mp_stop,
 	.next		= remote_mp_next,
 	.previous	= remote_mp_previous,
+	.set_setting	= remote_mp_set_setting,
 };
 
 static void mcp_ccid(void *data, uint8_t ccid, bool gmcs)
Signature · 1 line
2.51.1
#5 lore raw

PATCH v6 4/9 shared/uinput: add uinput utility functions

In reply to Pauli Virtanen

Add uinput utility function that can be used both for AVCTP and MCS.

---
 Makefile.am         |   3 +-
 src/shared/uinput.c | 204 ++++++++++++++++++++++++++++++++++++++++++++
 src/shared/uinput.h |  32 +++++++
 3 files changed, 238 insertions(+), 1 deletion(-)
 create mode 100644 src/shared/uinput.c
 create mode 100644 src/shared/uinput.h
Makefile.am +2 -1
index 795d0d403..3adfa6cd5 100644
@@ -247,7 +247,8 @@ shared_sources = src/shared/io.h src/shared/timeout.h \
 			src/shared/lc3.h src/shared/tty.h \
 			src/shared/bap-defs.h \
 			src/shared/asha.h src/shared/asha.c \
-			src/shared/battery.h src/shared/battery.c
+			src/shared/battery.h src/shared/battery.c \
+			src/shared/uinput.h src/shared/uinput.c
 
 if READLINE
 shared_sources += src/shared/shell.c src/shared/shell.h
src/shared/uinput.c new file +204 -0

204 lines not on this page · raw message

src/shared/uinput.h new file +32 -0
new file mode 100644
index 000000000..a15e4b3b4
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2025  Pauli Virtanen
+ */
+
+struct bt_uinput;
+
+struct bt_uinput_key_map {
+	const char *name;
+	unsigned int code;
+	uint16_t uinput;
+};
+
+typedef void (*bt_uinput_debug_func_t)(const char *str, void *user_data);
+
+struct bt_uinput *bt_uinput_new(const char *name, const char *suffix,
+				const bdaddr_t *addr,
+				const struct input_id *dev_id);
+
+void bt_uinput_set_debug(struct bt_uinput *uinput,
+					bt_uinput_debug_func_t debug_func,
+					void *user_data);
+
+int bt_uinput_create(struct bt_uinput *uinput,
+				const struct bt_uinput_key_map *key_map);
+
+void bt_uinput_destroy(struct bt_uinput *uinput);
+
+void bt_uinput_send_key(struct bt_uinput *uinput, uint16_t key, bool pressed);
Signature · 1 line
2.51.1
#6 lore raw

PATCH v6 5/9 avctp: use uinput utilities from src/shared

In reply to Pauli Virtanen

Make use of the src/shared version of uinput_create() / send_key

---
 profiles/audio/avctp.c | 166 ++++++++++-------------------------------
 1 file changed, 40 insertions(+), 126 deletions(-)
profiles/audio/avctp.c +40 -126
index 65eec6f6c..f20e08c00 100644
@@ -41,6 +41,7 @@
 #include "src/error.h"
 #include "src/shared/timeout.h"
 #include "src/shared/util.h"
+#include "src/shared/uinput.h"
 
 #include "avctp.h"
 #include "avrcp.h"
@@ -191,7 +192,7 @@ struct avctp {
 
 	avctp_state_t state;
 
-	int uinput;
+	struct bt_uinput *uinput;
 
 	guint auth_id;
 	unsigned int passthrough_id;
@@ -228,11 +229,7 @@ struct avctp_browsing_pdu_handler {
 	GDestroyNotify destroy;
 };
 
-static const struct {
-	const char *name;
-	uint8_t avc;
-	uint16_t uinput;
-} key_map[] = {
+static const struct bt_uinput_key_map key_map[] = {
 	{ "SELECT",		AVC_SELECT,		KEY_SELECT },
 	{ "UP",			AVC_UP,			KEY_UP },
 	{ "DOWN",		AVC_DOWN,		KEY_DOWN },
@@ -301,27 +298,6 @@ static gboolean avctp_passthrough_rsp(struct avctp *session, uint8_t code,
 					uint8_t *operands, size_t operand_count,
 					void *user_data);
 
-static int send_event(int fd, uint16_t type, uint16_t code, int32_t value)
-{
-	struct input_event event;
-
-	memset(&event, 0, sizeof(event));
-	event.type	= type;
-	event.code	= code;
-	event.value	= value;
-
-	return write(fd, &event, sizeof(event));
-}
-
-static void send_key(int fd, uint16_t key, int pressed)
-{
-	if (fd < 0)
-		return;
-
-	send_event(fd, EV_KEY, key, pressed);
-	send_event(fd, EV_SYN, SYN_REPORT, 0);
-}
-
 static bool auto_release(gpointer user_data)
 {
 	struct avctp *session = user_data;
@@ -330,7 +306,7 @@ static bool auto_release(gpointer user_data)
 
 	DBG("AV/C: key press timeout");
 
-	send_key(session->uinput, session->key.op, 0);
+	bt_uinput_send_key(session->uinput, session->key.op, 0);
 
 	return FALSE;
 }
@@ -344,12 +320,12 @@ static void handle_press(struct avctp *session, uint16_t op)
 		if (session->key.op == op)
 			goto done;
 
-		send_key(session->uinput, session->key.op, 0);
+		bt_uinput_send_key(session->uinput, session->key.op, 0);
 	}
 
 	session->key.op = op;
 
-	send_key(session->uinput, op, 1);
+	bt_uinput_send_key(session->uinput, op, 1);
 
 done:
 	session->key.timer = timeout_add_seconds(AVC_PRESS_TIMEOUT,
@@ -364,7 +340,7 @@ static void handle_release(struct avctp *session, uint16_t op)
 		session->key.timer = 0;
 	}
 
-	send_key(session->uinput, op, 0);
+	bt_uinput_send_key(session->uinput, op, 0);
 }
 
 static size_t handle_panel_passthrough(struct avctp *session,
@@ -401,12 +377,12 @@ static size_t handle_panel_passthrough(struct avctp *session,
 	for (i = 0; key_map[i].name != NULL; i++) {
 		uint8_t key_quirks;
 
-		if ((operands[0] & 0x7F) != key_map[i].avc)
+		if ((operands[0] & 0x7F) != key_map[i].code)
 			continue;
 
 		DBG("AV/C: %s %s", key_map[i].name, status);
 
-		key_quirks = session->key_quirks[key_map[i].avc];
+		key_quirks = session->key_quirks[key_map[i].code];
 
 		if (key_quirks & QUIRK_NO_RELEASE) {
 			if (!pressed) {
@@ -415,8 +391,10 @@ static size_t handle_panel_passthrough(struct avctp *session,
 			}
 
 			DBG("AV/C: treating key press as press + release");
-			send_key(session->uinput, key_map[i].uinput, 1);
-			send_key(session->uinput, key_map[i].uinput, 0);
+			bt_uinput_send_key(session->uinput, key_map[i].uinput,
+									1);
+			bt_uinput_send_key(session->uinput, key_map[i].uinput,
+									0);
 			break;
 		}
 
@@ -570,15 +548,14 @@ static void avctp_disconnected(struct avctp *session)
 	if (session->key.timer > 0)
 		timeout_remove(session->key.timer);
 
-	if (session->uinput >= 0) {
+	if (session->uinput) {
 		char address[18];
 
 		ba2str(device_get_address(session->device), address);
 		DBG("AVCTP: closing uinput for %s", address);
 
-		ioctl(session->uinput, UI_DEV_DESTROY);
-		close(session->uinput);
-		session->uinput = -1;
+		bt_uinput_destroy(session->uinput);
+		session->uinput = NULL;
 	}
 
 	server = session->server;
@@ -1154,92 +1131,17 @@ failed:
 	return FALSE;
 }
 
-static int uinput_create(struct btd_device *device, const char *name,
-			 const char *suffix)
+static void uinput_debug(const char *str, void *data)
 {
-	struct uinput_user_dev dev;
-	int fd, err, i;
-	char src[18];
-
-	fd = open("/dev/uinput", O_RDWR);
-	if (fd < 0) {
-		fd = open("/dev/input/uinput", O_RDWR);
-		if (fd < 0) {
-			fd = open("/dev/misc/uinput", O_RDWR);
-			if (fd < 0) {
-				err = -errno;
-				error("Can't open input device: %s (%d)",
-							strerror(-err), -err);
-				return err;
-			}
-		}
-	}
-
-	memset(&dev, 0, sizeof(dev));
-
-	if (name) {
-		strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE - 1);
-		dev.name[UINPUT_MAX_NAME_SIZE - 1] = '\0';
-	}
-
-	if (suffix) {
-		int len, slen;
-
-		len = strlen(dev.name);
-		slen = strlen(suffix);
-
-		/* If name + suffix don't fit, truncate the name, then add the
-		 * suffix.
-		 */
-		if (len + slen < UINPUT_MAX_NAME_SIZE - 1) {
-			strcpy(dev.name + len, suffix);
-		} else {
-			len = UINPUT_MAX_NAME_SIZE - slen - 1;
-			strncpy(dev.name + len, suffix, slen);
-			dev.name[UINPUT_MAX_NAME_SIZE - 1] = '\0';
-		}
-	}
-
-	dev.id.bustype = BUS_BLUETOOTH;
-	dev.id.vendor  = btd_device_get_vendor(device);
-	dev.id.product = btd_device_get_product(device);
-	dev.id.version = btd_device_get_version(device);
-
-	if (write(fd, &dev, sizeof(dev)) < 0) {
-		err = -errno;
-		error("Can't write device information: %s (%d)",
-						strerror(-err), -err);
-		close(fd);
-		return err;
-	}
-
-	ioctl(fd, UI_SET_EVBIT, EV_KEY);
-	ioctl(fd, UI_SET_EVBIT, EV_REL);
-	ioctl(fd, UI_SET_EVBIT, EV_REP);
-	ioctl(fd, UI_SET_EVBIT, EV_SYN);
-
-	ba2strlc(btd_adapter_get_address(device_get_adapter(device)), src);
-	ioctl(fd, UI_SET_PHYS, src);
-
-	for (i = 0; key_map[i].name != NULL; i++)
-		ioctl(fd, UI_SET_KEYBIT, key_map[i].uinput);
-
-	if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
-		err = -errno;
-		error("Can't create uinput device: %s (%d)",
-						strerror(-err), -err);
-		close(fd);
-		return err;
-	}
-
-	send_event(fd, EV_REP, REP_DELAY, 300);
-
-	return fd;
+	DBG_IDX(0xffff, "%s", str);
 }
 
 static void init_uinput(struct avctp *session)
 {
+	struct btd_adapter *adapter = device_get_adapter(session->device);
 	char name[UINPUT_MAX_NAME_SIZE];
+	struct input_id id;
+	int err;
 
 	device_get_name(session->device, name, sizeof(name));
 	if (g_str_equal(name, "Nokia CK-20W")) {
@@ -1249,11 +1151,24 @@ static void init_uinput(struct avctp *session)
 		session->key_quirks[AVC_PAUSE] |= QUIRK_NO_RELEASE;
 	}
 
-	session->uinput = uinput_create(session->device, name, " (AVRCP)");
-	if (session->uinput < 0)
-		error("AVRCP: failed to init uinput for %s", name);
-	else
+	id.bustype = BUS_BLUETOOTH;
+	id.vendor  = btd_device_get_vendor(session->device);
+	id.product = btd_device_get_product(session->device);
+	id.version = btd_device_get_version(session->device);
+
+	session->uinput = bt_uinput_new(name, " (AVRCP)",
+				btd_adapter_get_address(adapter), &id);
+	bt_uinput_set_debug(session->uinput, uinput_debug, NULL);
+
+	err = bt_uinput_create(session->uinput, key_map);
+	if (err < 0) {
+		error("AVRCP: failed to create uinput for %s: %s", name,
+								strerror(-err));
+		bt_uinput_destroy(session->uinput);
+		session->uinput = NULL;
+	} else {
 		DBG("AVRCP: uinput initialized for %s", name);
+	}
 }
 
 static struct avctp_queue *avctp_queue_create(struct avctp_channel *chan)
@@ -1492,7 +1407,6 @@ static struct avctp *avctp_get_internal(struct btd_device *device)
 	session->server = server;
 	session->device = btd_device_ref(device);
 	session->state = AVCTP_STATE_DISCONNECTED;
-	session->uinput = -1;
 	session->key.op = AVC_INVALID;
 
 	server->sessions = g_slist_append(server->sessions, session);
@@ -1793,7 +1707,7 @@ static const char *op2str(uint8_t op)
 	int i;
 
 	for (i = 0; key_map[i].name != NULL; i++) {
-		if ((op & 0x7F) == key_map[i].avc)
+		if ((op & 0x7F) == key_map[i].code)
 			return key_map[i].name;
 	}
 
@@ -2232,7 +2146,7 @@ bool avctp_supports_avc(uint8_t avc)
 	int i;
 
 	for (i = 0; key_map[i].name != NULL; i++) {
-		if (key_map[i].avc == avc)
+		if (key_map[i].code == avc)
 			return true;
 	}
 	return false;
Signature · 1 line
2.51.1
#7 lore raw

PATCH v6 6/9 mcp: add local GMCS service that emits uinput media keys

In reply to Pauli Virtanen

Implement simple GMCS service that is always inactive, and emits media key presses via uinput for Play/Pause/Stop/Next/Prev MCS commands.

In practice, this seems to be enough to support media control keys on headsets. Some headsets (Creative Zen Hybrid Pro) also refuse to connect if there is no GMCS service.
---

Notes:
Tested production devices:

- Creative Zen Hybrid Pro (doesn't even connect without GMCS)
- Samsung Galaxy Buds Pro2

 profiles/audio/mcp.c | 180 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 180 insertions(+)
profiles/audio/mcp.c +180 -0
index 48e65d497..fc0587ce3 100644
@@ -22,6 +22,8 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <unistd.h>
+#include <linux/uinput.h>
 
 #include <glib.h>
 
@@ -41,6 +43,7 @@
 #include "src/shared/gatt-server.h"
 #include "src/shared/mcp.h"
 #include "src/shared/mcs.h"
+#include "src/shared/uinput.h"
 
 #include "btio/btio.h"
 #include "src/plugin.h"
@@ -420,6 +423,158 @@ static const struct bt_mcp_callback mcp_cb = {
 };
 
 
+/*
+ * Local player
+ */
+
+struct gmcs;
+
+struct local_player {
+	struct bt_mcs *mcs;
+	struct media_player *mp;
+	struct gmcs *gmcs;
+};
+
+struct gmcs {
+	struct bt_uinput *uinput;
+	struct btd_adapter *adapter;
+	struct bt_mcs *mcs;
+	struct queue *players;
+};
+
+static const struct bt_uinput_key_map key_map[] = {
+	{ "Play",	BT_MCS_CMD_PLAY,		KEY_PLAYCD },
+	{ "Stop",	BT_MCS_CMD_STOP,		KEY_STOPCD },
+	{ "Pause",	BT_MCS_CMD_PAUSE,		KEY_PAUSECD },
+	{ "Next Track",	BT_MCS_CMD_NEXT_TRACK,		KEY_NEXTSONG },
+	{ "Prev Track",	BT_MCS_CMD_PREV_TRACK,		KEY_PREVIOUSSONG },
+	{ NULL }
+};
+
+static struct queue *servers;
+
+static bool gmcs_command(struct gmcs *gmcs, uint8_t cmd)
+{
+	unsigned int i;
+
+	/* Emulate media key press */
+	if (!gmcs->uinput)
+		return false;
+
+	for (i = 0; i < ARRAY_SIZE(key_map); ++i) {
+		if (key_map[i].code == cmd) {
+			DBG("GMCS press %s", key_map[i].name);
+			bt_uinput_send_key(gmcs->uinput, key_map[i].uinput, 1);
+			bt_uinput_send_key(gmcs->uinput, key_map[i].uinput, 0);
+			break;
+		}
+	}
+
+	/* We are always inactive, so command does not cause state changes and
+	 * does not succeed, even though we do generate the key presses.
+	 * This should be OK vs. MCP v1.0.1 p. 26
+	 */
+	return false;
+}
+
+static bool gmcs_play(void *data)
+{
+	return gmcs_command(data, BT_MCS_CMD_PLAY);
+}
+
+static bool gmcs_pause(void *data)
+{
+	return gmcs_command(data, BT_MCS_CMD_PAUSE);
+}
+
+static bool gmcs_stop(void *data)
+{
+	return gmcs_command(data, BT_MCS_CMD_STOP);
+}
+
+static bool gmcs_next_track(void *data)
+{
+	return gmcs_command(data, BT_MCS_CMD_NEXT_TRACK);
+}
+
+static bool gmcs_previous_track(void *data)
+{
+	return gmcs_command(data, BT_MCS_CMD_PREV_TRACK);
+}
+
+static void gmcs_media_player_name(void *data, struct iovec *buf, size_t size)
+{
+	struct gmcs *gmcs = data;
+	int len;
+
+	len = snprintf((void *)buf->iov_base, size, "%s",
+					btd_adapter_get_name(gmcs->adapter));
+	if (len < 0)
+		len = 0;
+	else if ((size_t)len > size)
+		len = size;
+	util_iov_push(buf, len);
+}
+
+static void gmcs_destroy(void *data)
+{
+	struct gmcs *gmcs = data;
+
+	DBG("destroy %p", data);
+
+	queue_remove(servers, gmcs);
+
+	bt_uinput_destroy(gmcs->uinput);
+
+	free(gmcs);
+}
+
+static void gmcs_debug(void *data, const char *str)
+{
+	DBG_IDX(0xffff, "%s", str);
+}
+
+static const struct bt_mcs_callback gmcs_cb = {
+	.media_player_name = gmcs_media_player_name,
+	.play = gmcs_play,
+	.pause = gmcs_pause,
+	.stop = gmcs_stop,
+	.next_track = gmcs_next_track,
+	.previous_track = gmcs_previous_track,
+	.debug = gmcs_debug,
+	.destroy = gmcs_destroy,
+};
+
+static void uinput_debug(const char *str, void *data)
+{
+	DBG_IDX(0xffff, "%s", str);
+}
+
+static struct gmcs *gmcs_new(struct btd_adapter *adapter)
+{
+	struct gmcs *gmcs;
+	const char *name = btd_adapter_get_name(adapter);
+	int err;
+
+	gmcs = new0(struct gmcs, 1);
+	gmcs->adapter = adapter;
+
+	gmcs->uinput = bt_uinput_new(name, " (MCS)",
+			btd_adapter_get_address(adapter), NULL);
+	bt_uinput_set_debug(gmcs->uinput, uinput_debug, gmcs);
+
+	err = bt_uinput_create(gmcs->uinput, key_map);
+	if (err < 0) {
+		error("MCS: failed to init uinput for %s: %s", name,
+								strerror(-err));
+		bt_uinput_destroy(gmcs->uinput);
+		gmcs->uinput = NULL;
+	}
+
+	DBG("new %p", gmcs);
+	return gmcs;
+}
+
 /*
  * Profile
  */
@@ -497,11 +652,36 @@ static void mcp_remove(struct btd_service *service)
 
 static int gmcs_probe(struct btd_profile *p, struct btd_adapter *adapter)
 {
+	struct btd_gatt_database *database = btd_adapter_get_database(adapter);
+	struct gatt_db *db = btd_gatt_database_get_db(database);
+	struct gmcs *gmcs;
+
+	DBG("Add GMCS server %s", adapter_get_path(adapter));
+
+	gmcs = gmcs_new(adapter);
+	if (!gmcs)
+		return -EINVAL;
+
+	gmcs->mcs = bt_mcs_register(db, true, &gmcs_cb, gmcs);
+	if (!gmcs->mcs) {
+		gmcs_destroy(gmcs);
+		return -EINVAL;
+	}
+
+	if (!servers)
+		servers = queue_new();
+	queue_push_tail(servers, gmcs);
+
 	return 0;
 }
 
 static void gmcs_remove(struct btd_profile *p, struct btd_adapter *adapter)
 {
+	struct btd_gatt_database *database = btd_adapter_get_database(adapter);
+	struct gatt_db *db = btd_gatt_database_get_db(database);
+
+	DBG("Remove GMCS server %s", adapter_get_path(adapter));
+	bt_mcs_unregister_all(db);
 }
 
 static struct btd_profile mcp_gmcs_profile = {
Signature · 1 line
2.51.1
#8 lore raw

PATCH v6 7/9 shared/mcp: on track changed, re-read values if notify not available

In reply to Pauli Virtanen

Notify is optional for some MCS attributes. To get new track titles etc. re-read values on track change, if remote does not have notify on them.

---
 src/shared/mcp.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
src/shared/mcp.c +14 -14
index 97970c495..4916289e1 100644
@@ -979,7 +979,8 @@ void bt_mcs_unregister_all(struct gatt_db *db)
  */
 
 static void mcp_service_reread(struct bt_mcp_service *service,
-					struct gatt_db_attribute *attrib);
+					struct gatt_db_attribute *attrib,
+					bool skip_notify);
 
 static void mcp_debug_func(const char *str, void *user_data)
 {
@@ -1074,7 +1075,6 @@ static void mcp_pending_write_cb(bool success, uint8_t att_ecode,
 								void *user_data)
 {
 	struct bt_mcp_pending *pending = user_data;
-	uint8_t props;
 
 	if (!success) {
 		pending->write.result = BT_MCS_RESULT_COMMAND_CANNOT_COMPLETE;
@@ -1083,14 +1083,8 @@ static void mcp_pending_write_cb(bool success, uint8_t att_ecode,
 
 	pending->write.result = BT_MCS_RESULT_SUCCESS;
 
-	if (!gatt_db_attribute_get_char_data(pending->write.attrib, NULL,
-						NULL, &props, NULL, NULL))
-		return;
-	if (props & BT_GATT_CHRC_PROP_NOTIFY)
-		return;
-
 	/* If the attribute doesn't have notify, reread to get the new value */
-	mcp_service_reread(pending->service, pending->write.attrib);
+	mcp_service_reread(pending->service, pending->write.attrib, true);
 }
 
 static void mcp_pending_write_done(void *user_data)
@@ -1389,6 +1383,8 @@ static void update_track_changed(bool success, uint8_t att_ecode,
 		return;
 	}
 
+	mcp_service_reread(service, NULL, true);
+
 	DBG_SVC(service, "Track Changed");
 
 	LISTENER_CB(service, track_changed);
@@ -1626,7 +1622,7 @@ static void update_ccid(bool success, uint8_t att_ecode,
 	uint8_t v;
 
 	if (!success || !util_iov_pull_u8(&iov, &v)) {
-		DBG_SVC(service, "Unable to read Media State: error 0x%02x",
+		DBG_SVC(service, "Unable to read CCID: error 0x%02x",
 								att_ecode);
 		return;
 	}
@@ -1639,7 +1635,8 @@ static void update_ccid(bool success, uint8_t att_ecode,
 }
 
 static void mcp_service_reread(struct bt_mcp_service *service,
-					struct gatt_db_attribute *attrib)
+					struct gatt_db_attribute *attrib,
+					bool skip_notify)
 {
 	const struct {
 		struct gatt_db_attribute *attr;
@@ -1659,6 +1656,7 @@ static void mcp_service_reread(struct bt_mcp_service *service,
 	};
 	struct bt_gatt_client *client = service->mcp->client;
 	uint16_t value_handle;
+	uint8_t props;
 	unsigned int i;
 
 	for (i = 0; i < ARRAY_SIZE(attrs); ++i) {
@@ -1668,8 +1666,10 @@ static void mcp_service_reread(struct bt_mcp_service *service,
 			continue;
 
 		if (!gatt_db_attribute_get_char_data(attrs[i].attr, NULL,
-					&value_handle, NULL, NULL, NULL))
-			return;
+					&value_handle, &props, NULL, NULL))
+			continue;
+		if (skip_notify && (props & BT_GATT_CHRC_PROP_NOTIFY))
+			continue;
 
 		DBG_SVC(service, "re-read handle 0x%04x", value_handle);
 
@@ -1681,7 +1681,7 @@ static void mcp_service_reread(struct bt_mcp_service *service,
 static void notify_media_player_name(struct bt_mcp_service *service)
 {
 	/* On player name change, re-read all attributes */
-	mcp_service_reread(service, NULL);
+	mcp_service_reread(service, NULL, false);
 }
 
 static void mcp_idle(void *data)
Signature · 1 line
2.51.1
#9 lore raw

PATCH v6 8/9 test-mcp: check attributes are reread on track change if no notify

In reply to Pauli Virtanen

Add test we reread attributes on track change if remote does not have optional notify features.

---
 unit/test-mcp.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
unit/test-mcp.c +52 -0
index 0100df1ab..b05630185 100644
@@ -1325,6 +1325,57 @@ static void testgroup_cl_mccp(void)
 		test_setup, test_client, &cfg_mccp_bv_21_c, MCCP_BV_21_C);
 }
 
+#define CL_BLUEZ_1_REREAD \
+	NOTIFY_CHRC(TRACK_CHG), \
+	READ_CHRC(TRACK_TITLE, 'N', 'e', 'w'), \
+	READ_CHRC(TRACK_DUR, 0xff, 0xff, 0xff, 0xff), \
+	READ_CHRC(TRACK_POS, 0xff, 0xff, 0xff, 0xff), \
+	READ_CHRC(PLAY_SPEED, 0x00), \
+	READ_CHRC(SEEK_SPEED, 0x00), \
+	READ_CHRC(PLAY_ORDER, 0x04), \
+	READ_CHRC(PLAY_ORDER_SUPP, 0x18, 0x00), \
+	READ_CHRC(CP_SUPP, SPLIT_INT32(0x01))
+
+static void cl_reread_complete_cb(const void *user_data)
+{
+	struct test_data *data = (void *)user_data;
+
+	if (data->step == 2)
+		tester_test_passed();
+}
+
+static void cl_reread_track_title(void *user_data, const uint8_t *value,
+								uint16_t length)
+{
+	struct test_data *data = user_data;
+
+	if (strncmp((void *)value, "Title", length) == 0 && data->step == 0) {
+		data->step++;
+	} else if (strncmp((void *)value, "New", length) == 0 &&
+							data->step == 1) {
+		data->step++;
+		tester_io_set_complete_func(cl_reread_complete_cb);
+	} else {
+		FAIL_TEST();
+	}
+}
+
+const struct test_config cfg_cl_bluez_1_reread = {
+	.listener_cb = &(struct bt_mcp_listener_callback) {
+		.track_title = cl_reread_track_title,
+	},
+	.setup_data = setup_data_mcs,
+	.setup_data_len = ARRAY_SIZE(setup_data_mcs),
+	.gmcs = false,
+};
+
+static void testgroup_cl_extra(void)
+{
+	define_test("MCP/CL/BLUEZ-1 [Reread On Track Change, No Notify]",
+		test_setup, test_client,
+		&cfg_cl_bluez_1_reread, CL_BLUEZ_1_REREAD);
+}
+
 /*
  * Server tests
  */
@@ -1800,6 +1851,7 @@ int main(int argc, char *argv[])
 	tester_init(&argc, &argv);
 	testgroup_cl_cggit();
 	testgroup_cl_mccp();
+	testgroup_cl_extra();
 	testgroup_sr_sggit();
 	testgroup_sr_mcp();
 
Signature · 1 line
2.51.1
#10 lore raw

PATCH v6 9/9 shared/mcp: complete CCID read before considering other attributes

In reply to Pauli Virtanen

Service is shown ready to upper level when CCID is read, after which we should notify upper level about current attribute values.

Make sure the values reach upper level, by reading them only after the service is ready. Otherwise, the reads may complete in unspecified order and upper level misses some events.

---
 src/shared/mcp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
src/shared/mcp.c +4 -2
index 4916289e1..910089f18 100644
@@ -981,6 +981,7 @@ void bt_mcs_unregister_all(struct gatt_db *db)
 static void mcp_service_reread(struct bt_mcp_service *service,
 					struct gatt_db_attribute *attrib,
 					bool skip_notify);
+static void foreach_mcs_char(struct gatt_db_attribute *attr, void *user_data);
 
 static void mcp_debug_func(const char *str, void *user_data)
 {
@@ -1631,6 +1632,9 @@ static void update_ccid(bool success, uint8_t att_ecode,
 
 	service->rdb.ccid_value = v;
 
+	gatt_db_service_foreach_char(service->rdb.service, foreach_mcs_char,
+								service);
+
 	update_add_service(service, service->mcp);
 }
 
@@ -1917,8 +1921,6 @@ static void foreach_mcs_service(struct gatt_db_attribute *attr, void *user_data)
 	/* Find CCID first */
 	gatt_db_service_foreach_char(attr, foreach_mcs_ccid, service);
 
-	gatt_db_service_foreach_char(attr, foreach_mcs_char, service);
-
 	queue_push_tail(mcp->services, service);
 }
 
Signature · 1 line
2.51.1