Repository navigation
DEV-1149: handle in-stream responses once, whole, and by opcode - #122
Merged
Merged
Conversation
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>
JongChern
changed the base branch from
DEV-1148_streaming_bare_ack_crc
to
master
October 23, 2026 12:18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DEV-1149. Stacked on #121 (DEV-1148), so this PR targets
DEV-1148_streaming_bare_ack_crcand 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 ofprocessInstreamResponse(). 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: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.readBattery()andreadStatusLogAndStream()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.startSDLogging(), that isprocessAckFromSetCommand(SET_RWC_COMMAND)reading the clock bytes from START_LOGGING_ONLY's one-byte instruction: ArrayIndexOutOfBoundsException, uncaught, on the IO thread.The fix
processPacket()calls a newprocessInstreamResponseWhileStreaming()in any streaming state. It:mWaitForAckis set and the driver'smBtGetCommandMapgives the response opcode asmCurrentCommand's expected response. This is the protocol's rule: log-and-stream-commondocs/SHIMMER3_BT_COMMUNICATION_PROTOCOL.mdsection 5.4, rule 2, which says to match by response opcode, never by the ACK.The
processInstreamResponse()override is removed, and with itgetDataFromArrayOutputStream(), which only that override used. The driver's version still serves the not-streaming path. These wereprotected; 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_StreamingInstreamResponseTestuses 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.inStreamResponseSplitAcrossReadsreplyToTheQueuedCommandInFlightCompletesItOnceresponsesThatAnswerNoQueuedCommandLeaveTheQueueAloneinStreamResponseWhileStreamingAndSdLoggingAll 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
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 queuesreadBattery(), so 1 and 3 do not apply. Details are in DEV-1149.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