From: tedd.an@linux.intel.com
To: linux-bluetooth@vger.kernel.org
Cc: Tedd Ho-Jeong An <tedd.an@intel.com>
Subject: [PATCH 1/2] midi: Fix SysEx parser in MIDI plugin
Date: Thu, 18 Jun 2020 23:01:52 -0700 [thread overview]
Message-ID: <20200619060153.65114-1-tedd.an@linux.intel.com> (raw)
From: Tedd Ho-Jeong An <tedd.an@intel.com>
The SysEx end of message parser didn't consider the fact that
timestampsLow are in the stream and it might have the EOX (F7)
byte as well.
Fix that by always assuming the first system message byte is
the timestampLow.
Future work would involve support other type of system message
bytes, such as real-time.
---
profiles/midi/libmidi.c | 30 +++++++++++++++++++++++-------
1 file changed, 23 insertions(+), 7 deletions(-)
diff --git a/profiles/midi/libmidi.c b/profiles/midi/libmidi.c
index 4b4df799f..cc5f079e7 100644
--- a/profiles/midi/libmidi.c
+++ b/profiles/midi/libmidi.c
@@ -331,6 +331,24 @@ static size_t handle_end_of_sysex(struct midi_read_parser *parser,
return sysex_length + 1; /* +1 because of timestampLow */
}
+/* Searches the end of a SysEx message that contains a timestampLow
+ * before the SysEx end byte. Returns the number of bytes of valid
+ * SysEx payload in the buffer.
+ */
+static size_t sysex_eox_len(const uint8_t *data, size_t size)
+{
+ size_t i = 0;
+
+ MIDI_ASSERT(size > 0);
+
+ if (data[i] == 0xF0)
+ i++;
+
+ /* search for timestamp low */
+ while (i < size && !(data[i++] & 0x80));
+
+ return (i < size && data[i] == 0xF7) ? i : 0;
+}
size_t midi_read_raw(struct midi_read_parser *parser, const uint8_t *data,
@@ -368,14 +386,13 @@ size_t midi_read_raw(struct midi_read_parser *parser, const uint8_t *data,
/* System Common Messages */
case 0xF0: /* SysEx Start */ {
- uint8_t *pos;
+ size_t sysex_length;
/* cleanup Running Status Message */
parser->rstatus = 0;
- /* Avoid parsing if SysEx is contained in one BLE packet */
- if ((pos = memchr(data + i, 0xF7, size - i))) {
- const size_t sysex_length = pos - (data + i);
+ /* Search for End of SysEx message in one BLE message */
+ if ((sysex_length = sysex_eox_len(data + i, size - i)) > 0) {
midi_size = handle_end_of_sysex(parser, ev, data + i,
sysex_length);
} else {
@@ -424,10 +441,9 @@ size_t midi_read_raw(struct midi_read_parser *parser, const uint8_t *data,
/* Check for SysEx messages */
if (parser->sysex_stream.len > 0) {
- uint8_t *pos;
+ size_t sysex_length;
- if ((pos = memchr(data + i, 0xF7, size - i))) {
- const size_t sysex_length = pos - (data + i);
+ if ((sysex_length = sysex_eox_len(data + i, size - i)) > 0) {
midi_size = handle_end_of_sysex(parser, ev, data + i,
sysex_length);
} else {
--
2.25.4
next reply other threads:[~2020-06-19 6:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-19 6:01 tedd.an [this message]
2020-06-19 6:01 ` [PATCH 2/2] midi: Fix random empty timestamp_high value tedd.an
2020-06-19 6:09 ` [2/2] " bluez.test.bot
2020-06-19 6:09 ` [1/2] midi: Fix SysEx parser in MIDI plugin bluez.test.bot
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=20200619060153.65114-1-tedd.an@linux.intel.com \
--to=tedd.an@linux.intel.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=tedd.an@intel.com \
/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;
as well as URLs for NNTP newsgroup(s).