Skip to content

DEV-1291: Take an in-stream ACK that comes with no data packet before it - #332

Merged
JongChern merged 1 commit into
masterfrom
DEV-1291_instream_ack_after_pause
Oct 22, 2026
Merged

JongChern merged 1 commit into
masterfrom
DEV-1291_instream_ack_after_pause

Conversation

@JongChern

@JongChern JongChern commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

The bug

After STOP_LOGGING_ONLY_COMMAND, the unit stops sending data packets for about 100 ms while it closes its SD file. A command sent in that pause is answered at once, so its ACK comes straight after the previous ACK's CRC, with no data packet before it:

... data, FF F4 (the STOP_LOGGING_ONLY ACK) | pause | FF 8A 71 36 00 BF (the GET_STATUS reply), data ...

processPacket() only looks for an ACK after a data packet, so it threw the reply away:

Unexpected packet header bytes ... Packet 1 header: [0xFF]
Throw Bytes [0xFF 0x8A 0x71 0x36]

The command then timed out after 2 s. While streaming, the timeout calls clearAllInstructions(), so whatever was queued behind it was silently dropped. That includes a Stop Streaming, which goes through the same queue.

The Java HIL (#331) found it on a Shimmer3 (LogAndStream v1.1.7) and a Shimmer3R (v1.1.21). In each overlap, the HIL stops logging while streaming, then asks for status and battery at once. Up to 4 of 5 overlaps per run lost their replies.

The fix

Before processPacket(), processWhileStreaming() now looks for an ACK at the very start of the buffer (processAckAtStartOfBuffer()). It takes one only while a command waits for its ACK, and only when the bytes after it are what that command expects:

  • for a GET command: its own in-stream reply, FF 8A then its response opcode, matched as in DEV-1262. The rest of the reply and its CRC are read from the port as in the existing in-stream branch.
  • for a SET command: a bare ACK whose link CRC checks out.

So a 0xFF data byte that the parser resyncs onto is not taken for one. It never looks at more than 3 bytes, fewer than processPacket() needs, so nothing else changes.

Without a link CRC the case can't arise. The previous ACK isn't processed until the stream resumes, so the next command never goes out during the pause.

Tests

  • API_00032_StreamingAckAfterPauseTest (new, 8 tests) feeds the parser the bytes a logging stop produces, one at a time as the IO thread does. That is, a data packet and the STOP_LOGGING_ONLY ACK, then in the pause:
    • a status reply, with GET_VBATT still queued behind it and not lost;
    • a battery reply;
    • a SET command's bare ACK;
      each with a 1-byte and a 2-byte CRC. All 6 fail without the fix.
  • Two more tests check that an ACK at the start of the buffer is not taken for a different command: a status push does not complete GET_VBATT.
  • ShimmerDriverPC: 74 of 74 pass. ShimmerDriver: 177 of 177 pass.

On hardware

These runs used the Java HIL from #331, not yet merged, built locally together with this fix and the DEV-1275 fix (#333). The units were a Shimmer3 and a Shimmer3R over Classic, with nothing else on the bench.

Before With the fix
Shimmer3R inStream: overlaps whose replies were lost 2–4 of 5 per run (3 runs) 0 of 15 (3 runs)
Replies the new path took after a logging stop 12, each one the old parser would have thrown away; 0 command timeouts
Shimmer3 inStream 1 of 5 overlaps lost 0 of 5
Full HIL run, both units inStream failed on both 8 of 8 scenarios pass

Other APIs

  • Android: its own Classic parser in Shimmer.java has the same gap (TODO: ACK in bufferTemp[0] not handled). It overrides the driver's parser, so this fix won't reach it. The port is a separate PR in ShimmerAndroidAPI.
  • Handled already: C# (a byte-at-a-time switch that recognises an ACK wherever it lands), pyshimmer (looks at the byte at every message boundary; no link CRC), and shimmer-logandstream (frames by the byte at each boundary).
  • Not applicable, no logging-only commands: the web SDK and Swift. Their stream parsers have weaker spots of their own, unrelated to this fix.

🤖 Generated with Claude Code

After STOP_LOGGING_ONLY_COMMAND the unit stops sending data packets for about
100 ms while it closes its SD file. A command sent in that pause is answered
at once, so its ACK comes straight after the previous ACK's CRC, with no data
packet before it.

processPacket() only looks for an ACK after a data packet, so it threw the
reply away. The command then timed out, and while streaming the timeout
clears every queued instruction, so whatever the user asked for next
(including Stop Streaming) was silently dropped. The Java HIL found it on a
Shimmer3 and a Shimmer3R: up to 4 of 5 status and battery requests sent
after a logging stop were lost.

processWhileStreaming() now looks for an ACK at the very start of the buffer
first. It takes one only while a command waits for its ACK, and only when the
bytes after it are what that command expects:
- for a GET command, its own in-stream reply (FF 8A, then its response opcode);
- for a SET command, a bare ACK whose link CRC checks out.
So a 0xFF data byte the parser resyncs onto is not taken for one. It looks at
no more than 3 bytes, fewer than processPacket() needs, so nothing else
changes. Without a link CRC the case can't arise: the previous ACK isn't
processed until the stream resumes, so the next command never goes out during
the pause.

API_00032 feeds the parser the bytes a logging stop produces, then a status
reply, a battery reply, or a SET command's bare ACK in the pause, with 1- and
2-byte CRCs. Without the fix, all six of those cases fail. Two more check that
an ACK at the start of the buffer is not taken for a different command.

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

Copy link
Copy Markdown
Contributor Author

Combined hardware verification. This used the Java HIL from #331 on a local merge of #331, #332, #333 and #334. The units were a Shimmer3 (LogAndStream v1.1.7) and a Shimmer3R (v1.1.21) over Bluetooth Classic, with nothing else on the bench.

  • Default 1-byte CRC: all 10 scenario runs passed: streamRest, midStreamSet, reconnectLoop, inStream and queueStress, on both units. There were 0 parser errors and 0 command timeouts.
  • Unit tests on the same merge: ShimmerDriverPC 85 of 85, ShimmerDriver 177 of 177.
  • This fix (DEV-1291): 9 replies arrived in the pause after a logging stop, and all were taken: 5 with the 1-byte CRC, 4 with the 2-byte. There were 0 timeouts. inStream also passes with the CRC off on both units.

@JongChern
JongChern merged commit 8059433 into master Oct 22, 2026
2 checks passed
@JongChern
JongChern deleted the DEV-1291_instream_ack_after_pause branch October 22, 2026 07:33
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