Conversation
The protocol has had a NACK (0xFE) reply for a long time; this API never learned about it. A 0xFE fell through to the switch default in both branches of ReadData(): while streaming that logged "Misaligned ByteStream Detected" and discarded the packet in progress, and otherwise it was dropped silently. Either way the reply the caller was waiting for never arrived, ReadByte() carried on raising TimeoutException, and once StreamTimeOutCount passed 10 the API raised "Connection lost" and disconnected - about ten seconds after a refusal the device had already reported cleanly. A refused command was indistinguishable from a dead link, and the log actively misdirected. This is the same defect as Shimmer-Java-Android-API#293, found by checking the sibling implementations of the wire format after that one. The timing and the mechanism differ - Java tore the link down after two seconds via its ACK timer - but the cause is identical: only ACK was tested for. The firmware refuses more than it used to. Every command except SET_SD_SYNC_COMMAND and ACK is refused while SD sync is enabled (ShimBt_isCmdAllowedWhileSdSyncing), which includes the whole connect sequence, so a sync-enabled Shimmer cannot be connected to at all. Also any SET while sensing, a sync-mode mismatch, an out-of-range InfoMem or calibration write, and several commands the dispatcher accepts but that were never implemented. ProcessNackFromCommand() is protected virtual so an application can surface the refusal; by default it logs and raises a notification event. It clears mWaitingForStartStreamingACK, which a refused START_STREAMING would otherwise leave set for the rest of the session, and consumes the CRC bytes the firmware appends to the NACK packet when CRC mode is on - those would otherwise be read as the next packet header. None of the three SDBT_switch overrides looked at 0xFE, so adding an explicit case ahead of the default changes nothing else. ShimmerCaptureXamarin carries a second, diverged copy of ShimmerBluetooth.cs with the same defect. It is deliberately left alone: nothing has touched it since July 2017, neither CI workflow builds it, and Xamarin is end of life, so a change there could not be verified by anything. Worth fixing if that sample is ever revived. ShimmerAPI builds clean; the ten warnings are pre-existing and in files this does not touch. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
marknolan
force-pushed
the
DEV-982_nack_handling
branch
from
September 21, 2026 09:06
e5bf48a to
69442ca
Compare
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.
The protocol has had a
NACK_COMMAND_PROCESSED(0xFE) reply for a long time. ThisAPI never learned about it, so
0xFEfell through to theswitchdefault in bothbranches of
ReadData().What was happening
"Misaligned ByteStream Detected"and nullsthe packet in progress. A clean refusal was reported as stream corruption.
Either way the reply the caller was waiting for never arrived,
ReadByte()carried onraising
TimeoutException, and onceStreamTimeOutCountpassed 10 the API raised"Connection lost"and disconnected — roughly ten seconds after the device hadalready told us, cleanly, that it would not do the thing.
A refused command was indistinguishable from a dead link, and the log actively
misdirected.
Why this is being raised now
This is the same defect as
Shimmer-Java-Android-API#293,
found by checking the sibling implementations of the wire format afterwards. The
mechanism and the timing differ — Java tore the link down after two seconds via its ACK
timer, this API times out and disconnects after about ten — but the cause is identical:
only ACK was ever tested for.
SwiftAPIhas the same gap and is being fixed alongside;the TypeScript SDK already handles it.
Where it bites
Of the ~14
sendNacksites in the firmware'sshimmer_bt_uart.c, only one is theblocked-while-sensing gate, and sending commands mid-stream is not a supported
operation anyway. The rest are reachable while idle:
SET_SD_SYNC_COMMANDand ACK —ShimBt_isCmdAllowedWhileSdSyncingargs[]SET_SAMPLING_RATE_COMMANDwith a zero clock dividerSET_INFOMEM_COMMANDoffset out of rangeSET_DAUGHTER_CARD_MEMEEPROM write failureSET_CHARGE_STATUS_LED_COMMAND,SET_BT_COMMS_BAUD_RATEThe SD sync one is the sharpest: with sync enabled the firmware refuses the whole
connect sequence, so a sync-enabled Shimmer cannot be connected to at all.
What this changes
ProcessNackFromCommand()isprotected virtual, so an application can override it tosurface the refusal; by default it logs and raises a notification event on the existing
MSG_IDENTIFIER_NOTIFICATION_MESSAGEchannel. It also:mWaitingForStartStreamingACK— a refusedSTART_STREAMINGwouldotherwise leave that flag set for the rest of the session, so the next stray ACK
would flip the state machine to
STREAMING.on. The firmware writes the refusal as a complete response packet and appends CRC to
it exactly as it would to a response, so without this those bytes would be read as
the next packet header.
None of the three
SDBT_switchoverrides looked at0xFE, so adding an explicit caseahead of the default changes nothing else.
The Xamarin duplicate is deliberately left alone
ShimmerCaptureXamarincarries a second, diverged copy ofShimmerBluetooth.cswiththe same defect. I patched it, then took it back out: nothing has touched that tree
since July 2017, neither CI workflow builds it, and Xamarin is end of life, so
nothing could verify the change — not CI, not a local build. I would rather flag it here
than ship unverified edits to dead code. Worth doing if that sample is ever revived.
Verification
ShimmerAPIbuilds clean — 0 errors. The ten warnings are pre-existing and in filesthis does not touch (
TestRadio.cs,SerialPortRadio.cs,ShimmerSDLog.cs,ShimmerLogAndStream.cs) plusNU1701package-restore noise.No hardware test. There is no fake transport for the receive path in this repo, so
exercising a NACK end-to-end would mean building one; that is worth its own piece of
work rather than being half-built inside this change. The equivalent change in
pyshimmerdoes carry tests, because that repo already has the fixtures for it.Worth a bench check alongside the Java one: enable SD sync on a Shimmer, connect, and
confirm the connection now survives and the refusal is reported, where previously it
dropped with
"Connection lost".🤖 Generated with Claude Code