Linux bluetooth development
 help / color / mirror / Atom feed
From: aparilli <nauta@google.com>
To: linux-bluetooth@vger.kernel.org
Cc: Alessandro Parilli <nauta@google.com>
Subject: [PATCH BlueZ] midi: Fix stall when timestampHigh is zero
Date: Wed,  2 Dec 2026 18:01:40 +0000	[thread overview]
Message-ID: <20261202180142.3189154-1-nauta@google.com> (raw)

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


                 reply	other threads:[~2026-12-02 18:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261202180142.3189154-1-nauta@google.com \
    --to=nauta@google.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