Skip to content

DEV-971: fix five event-channel parser defects found in #348's review - #349

Merged
JongChern merged 2 commits into
DEV-971_event_channelfrom
DEV-971_java_event_markers
Nov 13, 2026
Merged

JongChern merged 2 commits into
DEV-971_event_channelfrom
DEV-971_java_event_markers

Conversation

@JongChern

Copy link
Copy Markdown
Contributor

DEV-971. Follow-up to #348, which was merged before these landed: four parser defects in the Java event channel, found in its review. Each was first reproduced by a test that failed on #348's code (API_00044_EventStreamReviewReproTest), then fixed. All changes are in ShimmerBluetooth.java.

# Defect on #348's code Repro result before the fix Fix
1 Idle: records behind an in-stream reply were lost. After an unsolicited status push, [ACK][0x8A][0x71] (sent on a dock, an undock, or a press that starts logging), clearSerialBuffer() wiped the records queued behind it 0 of 2 records after a status push; 1 of 2 after a stray ACK With the event channel on, the bytes after the reply are left for the next pass. A frame after a stray ACK no longer has link-CRC bytes cleared after it, since it carries its own CRC
2 Streaming: an ACK nobody waited for, before a record frame, re-ran the ACK handling of the SET that went last processAckFromSetCommand(0x05) ran for a sampling-rate write long finished The ACK is taken only while one is awaited
3 Streaming with no link CRC: the packet before a 0x8A was taken as a packet even when the 0x8A was a data byte after a lost byte The misframed bytes 20 8A 01 07 05 08 3F 09 00 reached the app as a sample Without a CRC, the packet is taken only once the frame after it passes its own CRC; otherwise the bytes are discarded, as before #348
4 readEventChannelStatus() could be queued behind a start of streaming not yet acknowledged. Its reply, [ACK][0xBA][9], has no 0x8A wrapper, was not taken between data packets, and the command timed out Queued after the start; reply not taken Refused while a start of streaming is queued; a reply that still arrives between packets is taken

Tests

Suite Result
API_00044_EventStreamReviewReproTest 6/6. All 6 failed before the fix
API_00043_EventStreamTest 9/9
ShimmerDriver 219/219
ShimmerDriverPC (build, as CI runs it) 154/154
Java 8 MATLAB jar (shadowJar -PmatlabJava8) Builds

Not run on a unit since the fix. The bench run before it, posted on #348, was clean, and none of these four conditions came up in it.

Not changed

These review findings were not reproduced by a test, so they are left as they were:

  • Records go to a listener, not the MSG_IDENTIFIER_* handler messages. A design choice for review.
  • A buffer copy per byte once a connection has opted in.
  • Frame detection lives in five read paths.
  • The test window's duplicate of EventFile.getCompanionFile().
  • The test window's fixed 1.5 s read-back delay.

🤖 Generated with Claude Code

Each was first reproduced by a test that failed on the unchanged code
(API_00044_EventStreamReviewReproTest), then fixed:

1. Idle: event records right behind an in-stream reply were lost. After an
   unsolicited status push ([ACK][0x8A][0x71]), sent on a dock, an undock or a
   press that starts logging, clearSerialBuffer() wiped the records queued
   behind it (0 of 2 arrived). With the event channel on, the bytes after the
   reply are now left for the next pass. A record frame after a stray ACK no
   longer has link-CRC bytes cleared after it either: it carries its own CRC.
2. Streaming: an ACK nobody waited for, before a record frame, re-ran the ACK
   handling of the SET that went last. It is now taken only while an ACK is
   awaited.
3. Streaming with no link CRC: the packet before a 0x8A was taken as a packet
   even when the 0x8A was a data byte after a lost byte, so misframed bytes
   reached the app as a sample. It is now taken only once the frame after it
   passes its own CRC; otherwise the bytes are discarded as before.
4. readEventChannelStatus() could be queued behind a start of streaming not yet
   acknowledged, and its unwrapped reply, [ACK][0xBA][9], was then not taken
   between data packets and the command timed out. It is now refused while a
   start of streaming is queued, and a reply that still arrives between packets
   is taken.

ShimmerDriver 219/219 and ShimmerDriverPC 154/154 pass; the Java 8 MATLAB jar
builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JongChern

Copy link
Copy Markdown
Contributor Author

Reviewed by reading the code at 3de20a7; I did not run the suites. The four fixes look correct. The gaps are in the tests.

The fixes

  • 1, idle: correct. After an unsolicited status push the parser returns without clearing the serial buffer, only when the event channel is on; a frame after a stray ACK no longer has link-CRC bytes cleared after it.
  • 2, stray ACK before a frame: correct for this branch. The same unguarded processAckWhileStreaming() still runs for a stray ACK followed by a data packet, and for a bare ACK with a link CRC. That is existing behaviour and independent of the event channel, so worth recording, not fixing here.
  • 3, link CRC off: correct; it waits on NEED_MORE. One cost: a good packet followed by a corrupt frame is discarded with it.
  • 4, status read: correct. isStreamStartQueued() uses the same lock as the list's writers, and if a start slips in between the check and the queueing, the new streaming branch takes the reply. It only fires while that command waits for its ACK.

Unchanged: behaviour with the event channel off; status, VBATT and DIR replies through the reworked idle handler. Android's Shimmer.java doesn't override processInstreamResponse, so nothing is bypassed. Nothing needs more than Java 8.

Test gaps

  • Every test runs with the link CRC off. Untested: the "CRC bytes not cleared after a frame" half of fix 1 (only visible with a CRC on), clearCrcBytesFromBuffer in fix 4's new branch, and fix 2 with a 1-byte or 2-byte CRC.
  • The two idle tests can race the radio. AnsweringRadio.readBytes throws at once when bytes are short, and the test queues bytes one at a time while the IOThread reads. The IOThread can finish the status push before the records are queued (the test then passes on unfixed code), or a multi-byte read can land mid-queue. I have not reproduced either. Queueing each burst in one step, or letting reads wait for their timeout as API_00036's radio does, removes it.
  • Fix 3's test doesn't check that the good packets after the misframed one are still parsed. Fix 4's test doesn't check that the instruction is removed, the lock released, or the surrounding packets parsed.

Before it leaves draft: a bench run, since it hasn't run on hardware since the fix and CI doesn't run on this base. I also didn't confirm against the firmware that 0xB9 is answered, not NACKed, while streaming; a NACK would go through the existing path.

…ehind a bare ACK's CRC

From the review of #349:

- The idle tests' radios now take each burst whole and let reads wait for
  their bytes, as a serial port does (WaitingTestRadio), so the IOThread can
  no longer race the test's writes. API_00043's idle tests use it too.
- The status-push, frame-after-ACK and status-reply tests now run with no
  link CRC, a 1-byte and a 2-byte one.
- The misframing test checks that the good packets after the damaged one
  still arrive; the status-reply test that the command is retired, the lock
  released, the timer cancelled and the packets either side parsed.

The CRC runs reproduced one more defect, which is fixed here: with a link CRC
on (the driver turns it on at connect where the firmware has it), the
records sent in the same burst as the opt-in's ACK were lost, 0 of 2. The
bare ACK's link CRC is left after the ACK is taken; the idle path read it as
an unknown byte and cleared the whole serial buffer, records included. With
the event channel on, the idle path now drops only the bytes it read.

All 7 tests in API_00044 fail on #348's code and pass here. ShimmerDriver
219/219, ShimmerDriverPC 155/155; the Java 8 MATLAB jar builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JongChern

Copy link
Copy Markdown
Contributor Author

Thanks. Every point checked out against the code, and the test gaps are closed in be9a917.

Test gaps

Gap Change
Every test ran with the link CRC off The status-push, frame-after-ACK and status-reply tests now run with no link CRC, a 1-byte and a 2-byte CRC
The idle tests could race the radio New WaitingTestRadio: each burst is queued whole, and reads wait for their bytes up to the timeout. API_00043's idle tests had the same radio and use it too
Fix 3's test Now checks the exact packets that arrive: the good ones either side, not the damaged one
Fix 4's test Now checks the command is retired, the lock released, the ACK timer cancelled, and the packets either side parsed

All 7 tests in API_00044 fail on #348's code and pass here.

A fifth defect, found by the CRC runs and fixed here. With a link CRC on, the records sent in the same burst as the opt-in's ACK were lost: 0 of 2. The driver turns the link CRC on at connect where the firmware has it, and the bench run used a 1-byte CRC.

  • Cause: the ACK path takes the bare ACK but leaves its link CRC (F4, or F4 65). The idle path read that as an unknown byte and cleared the whole serial buffer, records included.
  • Fix: with the event channel on, the idle path now drops only the bytes it read. This replaces the narrower "return after an in-stream reply" from 3de20a7, and covers it.
  • Why the bench run didn't show it: the initial-state record arrived 118 ms after the ACK, after the clear.

The other points

  • Fix 2 with a link CRC: with a CRC on, a stray ACK before a frame goes through the older bare-ACK branch, which acknowledges without checking that an ACK is awaited. That branch does the same for a stray ACK before a data packet. It predates the event channel, so it is unchanged here, and the fix 2 test says so.
  • Fix 3's cost, a good packet dropped with a corrupt frame after it, applies only with no link CRC, where nothing can confirm that packet's end.
  • 0xB9 while streaming: answered, not NACKed, by log-and-stream-common's source. ShimBt_isCmdBlockedWhileSensing does not list it, and the reply is built as [0xBA][9] (Comms/shimmer_bt_uart.c). This is from reading the source, not from hardware.

Results: ShimmerDriver 219/219, ShimmerDriverPC 155/155, and the Java 8 MATLAB jar builds. Still not run on a unit since the fixes, so it stays a draft until a bench run.

🤖 Generated with Claude Code

@JongChern JongChern changed the title DEV-971: fix four event-channel parser defects found in #348's review DEV-971: fix five event-channel parser defects found in #348's review Nov 13, 2026
@JongChern

Copy link
Copy Markdown
Contributor Author

Bench, 13 Nov 2026: a Shimmer3R on LogAndStream v1.1.26 over Bluetooth Classic, with the 1-byte link CRC the driver sets at connect.

Code: be9a917. Run in two ways: a headless check with no one at the bench (changing nothing on the unit except the connection's opt-in), and the DEV-1473 Java Shimmer Capture for the button presses.

Headless check

Check Result
Event channel probe on connect Present
Fix 5: the opt-in's first record, sent straight after its ACK with the link CRC on 5 of 5 opt-ins got the initial-state record (seq 0), logged 7 ms after the ACK, right behind its CRC byte
Status read while idle Answered, saying the stream is on
Fix 4: status read straight after startStreaming() Refused
20 s stream at 51.2 Hz, opted in 1,010 samples, 0 missing, 100 % reception, worst delay 91 ms
Status read after the stream Answered
Frames 6 taken, 0 with a bad CRC

Button presses in Capture

Run Result
Byte 232 Button mode 4 written, read back as 0x04
On USB power 3 double presses, 13 records, seq 0-12, no gaps, 0 dropped. Logging did not start (the firmware does not log on USB), so the unit sent no status push. One press arrived after a GET_STATUS was sent and before its ACK: the record was taken and the poll completed
On battery A double press started logging and another stopped it, with two single presses while logging: 13 records, seq 0-12, no gaps. The unit sent its own status push each time (IsSDLogging true, then false), the driver went Ready, SD Logging, Ready, and nothing after either push was cleared

The two Capture runs logged no unexpected headers, resyncs, bad CRCs, NACKs, exceptions or lost links.

Not covered on hardware

  • Fix 1's exact case, records already queued behind a status push, did not occur. The firmware sent each double press's records before its push: about 400 ms before when logging started, 50 ms before when it stopped. The case is covered by its tests, with each link CRC.
  • Fixes 2 and 3 need a stray ACK or a lost byte, which the bench does not produce on demand. They rely on their tests.
  • TTL edges and an event file read from the card were not tried.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant