Skip to content

DEV-1145: drop a bare ACK's link CRC while connected, so a stream started after a SET is seen - #213

Merged
JongChern merged 1 commit into
masterfrom
DEV-1145_drop_bare_ack_crc_while_connected
Oct 20, 2026
Merged

JongChern merged 1 commit into
masterfrom
DEV-1145_drop_bare_ack_crc_while_connected

Conversation

@marknolan

@marknolan marknolan commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

DEV-1145.

Stacked on #212, which is stacked on #211. This branch is based on DEV-976_crc_switch_ack_in_new_mode. The fix sits in the same case as #212's SET_CRC ACK handling, and the tests use #212's CrcLinkDevice. It targets master so that CI runs. Until #211 and #212 merge, the diff also shows their three commits; only 8f8bef0 is this PR's. Merge #211, then #212, then this.

The bug

With a Bluetooth CRC on, the firmware frames every reply as the ACK, then any response, then one CRC over the whole frame. See ShimBt_sendRsp, log-and-stream-common Comms/shimmer_bt_uart.c:1827; the CRC is added at :2422-2426 @ 599bd41.

Reply to On the wire, two-byte CRC
a GET FF 04 lo hi <crc> <crc>
a SET (bare ACK) FF F4 65

The CRC of a bare ACK is a constant: F4 65, or F4 in one-byte mode. #212 hit this for the SET_CRC switch; this PR handles the general case.

ReadData's connected-state loop drops a reply's CRC after the reply is handled, but only when its first byte was not 0xFF. That is right for a GET, whose CRC follows its response. It never drops a bare ACK's CRC. What happens instead:

  1. F4 is read as a response code and falls through to SDBT_switch's default.
  2. The CRC drop after it reads N bytes: 65, then the first byte of whatever arrives within ReadTimeout (1 s).
  3. That byte is usually the next reply's leading ACK, which is harmless. When it is the ACK to START_STREAMING_COMMAND, mWaitingForStartStreamingACK never fires. The API stays Connected and raises no data events while the device streams. The stream is then parsed as connected-state replies.

The bug is on master already. Measured with CrcLinkDevice, WriteAccelRange(1) then StartStreaming():

CRC State after Packets delivered
one byte Connected 0 of 66
two bytes Connected 0 of 65
off Streaming 64 of 65

Who hits it: any caller that turns a CRC on, calls a Write… setter, then calls StartStreaming(). The setters sleep 200–300 ms after sending, well inside the 1 s window.

Who doesn't:

  • StartStreamingandLog, which sends GET_STATUS and sleeps 500 ms first.
  • ShimmerConsoleAppExample, whose WriteSensors ends with a calibration read and 1 s sleeps.

The fix

With a CRC on, the connected-state ACK case now reads the byte after the ACK. It does this only when neither #212's CRC switch nor START_STREAMING is waiting for that ACK.

  • F4 is a bare ACK's CRC, and the rest of it is dropped.
  • Anything else is the response code of a GET reply. The loop handles it next, and the existing drop takes that reply's CRC after the response, as before.

This works because no response code is 0xF4. I checked:

  • every opcode in log-and-stream-common's Comms/shimmer_bt_uart.h on main, and its whole history, for a _COMMAND or _RESPONSE define of 0xF4;
  • every tag of shimmer3r-firmware, and every LogAndStream* tag of shimmer3-firmware.

The only 0xF4 defines are BMP180/BMP280 chip registers (CTRL_MEAS), and this API has no packet type of 0xF4. The web SDK's Shimmer3RClient relies on the same fact: its framer recognises a bare ACK by checking a CRC over the ACK alone.

The read never waits on anything but the link. The firmware hands each frame to the transmit path in one call (ShimBt_writeToTxBufAndSend), so the byte after an ACK is always that same frame's next byte.

Why a local variable and not UnalignedBytesReceived. The loop does read that list first. But StartStreaming, StartStreamingandLog and StartStreamingEXGSawtoothTestSignal replace it from the caller's thread, which causes two problems:

  • a response code pushed into the list could be lost;
  • the loop could index the emptied list between its Count() check and [0]. That throws an ArgumentOutOfRangeException, which the loop does not catch.

A local in ReadData is touched only by the read thread.

Unchanged:

Testing

ShimmerBareAckCrcTest runs the API's own read loop against #212's CrcLinkDevice, which frames every reply as the firmware does.

Test Before the fix After
stream started after WriteAccelRange, one-byte CRC fails: stays Connected passes
the same, two-byte CRC fails: stays Connected passes
the same, CRC off (control) passes passes
stream started after three SET commands, both CRC modes fails: stays Connected passes
sampling-rate reply read after a SET command, three rounds, both CRC modes passes (the byte it lost was harmless) passes

The last test guards the new read-ahead, since every GET reply now goes through it. So do all of #212's switch tests, which read the sampling rate after each switch.

I ran the full ShimmerAPITest.sln suite in a fresh worktree, built with VS 2022 MSBuild and run under vstest.console as CI does: 51/51 pass. The build shows only the four pre-existing warnings, none in a changed file. No hardware test.

Sibling implementations

None of them needs this fix:

Implementation Why not
Java (Shimmer-Java-Android-API) It tracks the command it sent (mCurrentCommand), so it knows whether a response follows an ACK. It also clears the serial buffer before every write while not streaming.
Swift (SwiftAPI) It tracks commandSent, and each bare-ACK branch removes the ACK and its CRC together.
Web SDK Shimmer3RClient frames by CRC, recognising a bare ACK from the CRC over the ACK alone. Shimmer3Client has no CRC support.
pyshimmer No link CRC.
This repo's Xamarin copy and TestSerialPort No CRC support.

Wrapper: Shimmer-Labview-API pins the ShimmerAPI NuGet package at 1.3.10, and this repo is at 1.3.11. It gets this fix only once a package that includes it is published and that pin is bumped.

Related, not fixed here

  • DEV-982: handle NACK from a Shimmer3/3R instead of timing out the connection #204 drops a NACK's CRC twice while connected. It is dropped once in ProcessNackFromCommand, and again in this same trailing drop, because 0xFE is not 0xFF. I've commented there.
  • Java, while streaming. The sibling check found that a bare ACK with a CRC on ends in "Unknown parsing error while streaming", because processPacket expects 0x00 or 0x8A after an ACK. Not yet reproduced.

🤖 Generated with Claude Code

…rted after a SET is seen

With a Bluetooth CRC on, the firmware frames every reply as the ACK, any
response, then one CRC over both (ShimBt_sendRsp). So the reply to a SET
command is a bare ACK followed at once by its CRC, which is always F4 65,
or F4 in one-byte mode. While connected, ReadData drops a reply's CRC only
after a first byte other than 0xFF, which is right for a response and
never takes a bare ACK's CRC. F4 was read as a response code, and the CRC
drop after it took the first byte of whatever arrived within ReadTimeout.
When that was the ACK to START_STREAMING_COMMAND, mWaitingForStartStreamingACK
never fired: the API stayed connected with no data while the device
streamed. WriteAccelRange then StartStreaming delivered 0 of 66 packets
with a one-byte CRC and 0 of 65 with two, against 64 of 65 with none.

The connected ACK case now reads the byte after an ACK that neither the CRC
switch nor START_STREAMING is waiting for. F4 is a bare ACK's CRC, and the
rest of it is dropped. No response code is 0xF4 in any Shimmer3 or
Shimmer3R firmware: every LogAndStream tag of shimmer3-firmware and
shimmer3r-firmware, and log-and-stream-common's history, define 0xF4 only
as a BMP180/280 register. Anything else is a response code, and the loop
handles it next. It is held in a local rather than in UnalignedBytesReceived,
which StartStreaming replaces from the caller's thread.

ShimmerBareAckCrcTest drives the read loop against CrcLinkDevice: a stream
started after one SET command and after three, in each CRC mode, and a
reply read after a SET command. The stream cases fail without the fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@JongChern JongChern left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the premise the fix rests on: no response code is 0xF4, neither in log-and-stream-common's Comms/shimmer_bt_uart.h nor in any of this API's packet types (the only 0xF4 in the library is a trailing CRC in the S3 simulator's GET_STATUS reply). A bare ACK's CRC is F4 65 with ShimmerUartCrcCalc, so reading one byte after an ACK tells a bare ACK from a GET reply. The read-ahead runs only when neither the START_STREAMING nor #212's SET_CRC ACK is awaited, and the byte is kept in a read-thread local rather than UnalignedBytesReceived. CI on the merge with today's master (38ee5d0, which includes #214): unit tests 70/70, BLE tests and PublishNuGet pack green.

@JongChern
JongChern merged commit 290e170 into master Oct 20, 2026
10 checks passed
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