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

append_timestamp_high_maybe() busy-waits until the high 6 bits of the
13-bit millisecond timestamp become non-zero. This was added by commit
c29241fe129f ("midi: Fix random empty timestamp_high value") to avoid
sending a header byte of 0x80, at the time the parser in this file
treated a timestampHigh of 0 as "header not read yet". Commit
220f102c8db9 ("profiles/midi: Fix zero timestampHigh") fixed the parser,
and the BLE MIDI specification allows a timestampHigh of 0 (the header
is 0b10hhhhhh), so the workaround is no longer needed.

It is also costly: the high bits are 0 for the first 128 ms of every
8.192 s timestamp period, so 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.

Fixes: c29241fe129f ("midi: Fix random empty timestamp_high value")
---
v2: explain why the loop was added (c29241fe129f) and why it is no
    longer needed since 220f102c8db9; add a Fixes: tag. No code change.

 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-03 16:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 16:42 [PATCH BlueZ v2] midi: Fix stall when timestampHigh is zero Alessandro Parilli

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