Skip to content

DEV-1149: handle in-stream responses once, whole, and by opcode - #122

Merged
JongChern merged 1 commit into
masterfrom
DEV-1149_instream_response_while_streaming
Oct 24, 2026
Merged

JongChern merged 1 commit into
masterfrom
DEV-1149_instream_response_while_streaming

Conversation

@marknolan

@marknolan marknolan commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

DEV-1149. Stacked on #121 (DEV-1148), so this PR targets DEV-1148_streaming_bare_ack_crc and its diff is only DEV-1149's commit.

What was wrong

While streaming, Shimmer.processPacket() hands an in-stream response, FF 8A <opcode> <payload> [link CRC], to the override of processInstreamResponse(). That covers the replies to GET_STATUS, GET_VBATT and GET_DIR, and the firmware's unsolicited status push. All three defects predate DEV-1148:

  1. A response whose CRC arrives in a later read is processed twice. The override sized each response without its link CRC. clearSingleDataPacketFromBuffers() then returned silently when the CRC was not there yet, so the next read parsed the response again and passed the data packet before it on twice. A GET_DIR reply whose name had not all arrived threw NullPointerException.
  2. Any in-stream response removed the head of the instruction queue. While streaming, readBattery() and readStatusLogAndStream() write straight to the socket, and the firmware pushes its status unprompted: on a dock, an undock, the user button, a trial-duration expiry or a low-battery stop. Either can arrive while a queued SET command awaits its ACK.
    • The SET command was removed, and its ACK was then processed against the next instruction.
    • For startSDLogging(), that is processAckFromSetCommand(SET_RWC_COMMAND) reading the clock bytes from START_LOGGING_ONLY's one-byte instruction: ArrayIndexOutOfBoundsException, uncaught, on the IO thread.
    • Split across reads, the branch also removed an instruction on every read.
  3. In STREAMING_AND_SDLOGGING the response was read from the socket. The override parsed from the buffer only in STREAMING; otherwise it called the driver's version. That version reads the rest of the response from the socket with a blocking read, although Shimmer has already buffered it. The parser stalled there until the buffer passed 10,000 bytes and was discarded.

The fix

processPacket() calls a new processInstreamResponseWhileStreaming() in any streaming state. It:

  • waits until the whole response, link CRC included, is in the buffer;
  • parses it from the buffer, then passes the data packet on;
  • consumes the packet, the response and its CRC exactly once, keeping later bytes and their timestamps;
  • completes the queued command in flight only if the response is its reply. That means mWaitForAck is set and the driver's mBtGetCommandMap gives the response opcode as mCurrentCommand's expected response. This is the protocol's rule: log-and-stream-common docs/SHIMMER3_BT_COMMUNICATION_PROTOCOL.md section 5.4, rule 2, which says to match by response opcode, never by the ACK.

The processInstreamResponse() override is removed, and with it getDataFromArrayOutputStream(), which only that override used. The driver's version still serves the not-streaming path. These were protected; nothing else in this repo used them.

Parsing per opcode (status bytes, RTC write, battery, directory name) is unchanged, apart from the directory name's length byte, now read unsigned.

Tests

AAPI_00003_StreamingInstreamResponseTest uses the same in-memory harness as AAPI_00002. It replays each scenario with every link CRC mode (off, 1, 2), and with the bytes arriving in reads of every size from 1 to 48. A self-check confirms the largest read takes everything at once.

Test Shows On the parent commit
inStreamResponseSplitAcrossReads 1 packet before the reply parsed twice (1-byte CRC, 1-byte reads)
replyToTheQueuedCommandInFlightCompletesItOnce 1, 2 a status push completed the queued GET_DIR
responsesThatAnswerNoQueuedCommandLeaveTheQueueAlone 2 the battery reply completed SET_RWC while it awaited its ACK
inStreamResponseWhileStreamingAndSdLogging 3 read the response from the socket

All four failed on the parent commit (378b5c9). With the fix, all 14 tests (AAPI_00002's 10 and AAPI_00003's 4) pass in both the debug and release unit-test variants, run on the JVM with :ShimmerAndroidInstrumentDriver:test. Not tested on hardware.

AAPI_00002's comment that split in-stream responses were left alone is updated to point here.

Not in this PR

  • The Java driver (ShimmerBluetooth, the same at 0.11.7_beta and on master) has defect 2 for the status push: its in-stream branch also completes the head of the queue for any in-stream response. It reads one byte at a time and queues readBattery(), so 1 and 3 do not apply. Details are in DEV-1149.
  • Neither this parser nor the driver checks the CRC of the data packet before an in-stream response, unlike the packet before a bare ACK (DEV-1148).

Merging

Do not merge until Mark has reviewed. If #121 is squash-merged, rebase this onto master first (git rebase --onto origin/master 378b5c9), or the old DEV-1148 commits come along.

🤖 Generated with Claude Code

While streaming, Shimmer.processPacket() handed an in-stream response
(FF 8A <opcode> <payload> [link CRC]) to processInstreamResponse(). That
had three defects:

- It sized each response without its link CRC, and consumed nothing when
  the CRC had not arrived. The next read parsed the response again and
  passed the data packet before it on twice. A GET_DIR reply whose name
  had not all arrived threw NullPointerException.
- It took every in-stream response for the reply to the queued command in
  flight. But while streaming, readBattery() and readStatusLogAndStream()
  write their commands straight to the socket, and the firmware pushes
  its status unprompted. So a queued SET command was removed while it
  awaited its ACK. For startSDLogging(), that ACK was then processed
  against START_LOGGING_ONLY_COMMAND's instruction and threw
  ArrayIndexOutOfBoundsException on the IO thread. A response split
  across reads also removed an instruction on every read.
- It parsed from the buffer only in STREAMING. In STREAMING_AND_SDLOGGING
  it called the driver's version, which reads the response from the
  socket although it is already in the buffer.

Replace it with processInstreamResponseWhileStreaming(), which
processPacket() calls in any streaming state. It waits for the whole
response, CRC included, parses it from the buffer, and consumes the
packet, the response and its CRC exactly once. It completes the queued
command in flight only if the response is that command's reply, matched
by response opcode through the driver's mBtGetCommandMap. The protocol
requires this: log-and-stream-common
docs/SHIMMER3_BT_COMMUNICATION_PROTOCOL.md section 5.4, rule 2.

The processInstreamResponse() override goes, and so does
getDataFromArrayOutputStream(), which only it used. The driver's version
still serves the not-streaming path.

AAPI_00003_StreamingInstreamResponseTest replays four scenarios with
every link CRC mode, and the bytes arriving in reads of every size from
1 to 48. All four fail on the parent commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants