Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH BlueZ] midi: Fix stall when timestampHigh is zero
@ 2026-12-02 18:01 aparilli
  0 siblings, 0 replies; only message in thread
From: aparilli @ 2026-12-02 18:01 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Alessandro Parilli

From: Alessandro Parilli <nauta@google.com>

append_timestamp_high_maybe() busy-waits until the high 6 bits of the
13-bit millisecond timestamp become non-zero, treating a header byte of
0x80 as invalid. The BLE MIDI specification allows a timestampHigh of 0
(the header is 0b10hhhhhh), and the high bits are 0 for the first 128 ms
of every 8.192 s timestamp period. Any packet started in that window
makes bluetoothd spin in its main loop for up to 128 ms, sending and
reading nothing.

With MIDI clock (24 ppqn) sent to a synth over BLE this shows up every
8.192 s: a btmon capture at 100 BPM (one clock message every 25 ms)
shows the outgoing ATT writes stopping for 135-152 ms each period,
followed by a single packet carrying the 3 clock messages that queued
up. Clock-following devices drift behind each time.

Write the header from the current time once instead. The read side
already handles a timestampHigh of 0.
---
 profiles/midi/libmidi.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/profiles/midi/libmidi.c b/profiles/midi/libmidi.c
index 349f5f7..d90f522 100644
--- a/profiles/midi/libmidi.c
+++ b/profiles/midi/libmidi.c
@@ -58,20 +58,18 @@ inline static uint8_t sysex_get(const snd_seq_event_t *ev, size_t i)
 
 inline static void append_timestamp_high_maybe(struct midi_write_parser *parser)
 {
-	uint8_t timestamp_high = 0x80;
-
 	if (midi_write_has_data(parser))
 		return;
 
-	/* Make sure timesampt_high is assigned a non-zero value */
-	do {
-		/* convert µs to ms */
-		parser->rtime = g_get_monotonic_time() / 1000;
-		timestamp_high |= (parser->rtime & 0x1F80) >> 7;
-	} while (timestamp_high == 0x80);
+	/* convert µs to ms */
+	parser->rtime = g_get_monotonic_time() / 1000;
 
-	/* set timestampHigh */
-	buffer_append_byte(&parser->midi_stream, timestamp_high);
+	/*
+	 * set timestampHigh: the header byte is 10hhhhhh and a timestampHigh
+	 * of 0 (header 0x80) is valid.
+	 */
+	buffer_append_byte(&parser->midi_stream,
+				0x80 | ((parser->rtime & 0x1F80) >> 7));
 }
 
 inline static void append_timestamp_low(struct midi_write_parser *parser)
-- 
2.56.1.911.g883d8bcb20-goog


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-02 18:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 18:01 [PATCH BlueZ] midi: Fix stall when timestampHigh is zero aparilli

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