Skip to content

DEV-982: handle NACK from a Shimmer3/3R instead of timing out the connection - #204

Open
marknolan wants to merge 1 commit into
masterfrom
DEV-982_nack_handling
Open

marknolan wants to merge 1 commit into
masterfrom
DEV-982_nack_handling

Conversation

@marknolan

@marknolan marknolan commented Sep 21, 2026

Copy link
Copy Markdown
Member

The protocol has had a NACK_COMMAND_PROCESSED (0xFE) reply for a long time. This
API never learned about it, so 0xFE fell through to the switch default in both
branches of ReadData().

What was happening

  • While streaming — the default logs "Misaligned ByteStream Detected" and nulls
    the packet in progress. A clean refusal was reported as stream corruption.
  • Otherwise — the byte was consumed and 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 — roughly ten seconds after the device had
already 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. SwiftAPI has the same gap and is being fixed alongside;
the TypeScript SDK already handles it.

Where it bites

Of the ~14 sendNack sites in the firmware's shimmer_bt_uart.c, only one is the
blocked-while-sensing gate, and sending commands mid-stream is not a supported
operation anyway. The rest are reachable while idle:

Condition
SD sync enabled refuses everything except SET_SD_SYNC_COMMAND and ACK — ShimBt_isCmdAllowedWhileSdSyncing
a sync command while sync is disabled same XOR gate
payload truncated past args[]
SET_SAMPLING_RATE_COMMAND with a zero clock divider
calibration RAM write out of range
SET_INFOMEM_COMMAND offset out of range
SET_DAUGHTER_CARD_MEM EEPROM write failure
SET_CHARGE_STATUS_LED_COMMAND, SET_BT_COMMS_BAUD_RATE dispatched but never implemented

The 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() is protected virtual, so an application can override it to
surface the refusal; by default it logs and raises a notification event on the existing
MSG_IDENTIFIER_NOTIFICATION_MESSAGE channel. It also:

  • clears mWaitingForStartStreamingACK — a refused START_STREAMING would
    otherwise leave that flag set for the rest of the session, so the next stray ACK
    would flip the state machine to STREAMING.
  • consumes the CRC bytes the firmware appends to the NACK packet when CRC mode is
    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_switch overrides looked at 0xFE, so adding an explicit case
ahead of the default changes nothing else.

The Xamarin duplicate is deliberately left alone

ShimmerCaptureXamarin carries a second, diverged copy of ShimmerBluetooth.cs with
the 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

ShimmerAPI builds clean — 0 errors. The ten warnings are pre-existing and in files
this does not touch (TestRadio.cs, SerialPortRadio.cs, ShimmerSDLog.cs,
ShimmerLogAndStream.cs) plus NU1701 package-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
pyshimmer does 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

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
marknolan force-pushed the DEV-982_nack_handling branch from e5bf48a to 69442ca Compare September 21, 2026 09:06
@marknolan marknolan self-assigned this Sep 21, 2026
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