Repository navigation
DEV-976: switch the link CRC at the SET_CRC ACK, which comes in the new mode - #212
Merged
Merged
Conversation
…ew mode The firmware switches its Bluetooth CRC while it processes SET_CRC_COMMAND, before it builds the ACK, so the ACK is framed in the new mode (shimmer3r-firmware shimmer_bt_comms.c:1262 and :2344 at LogAndStream_Shimmer3R_v1.00.010, log-and-stream-common Comms/shimmer_bt_uart.c:944 and :2422 at v1.01.017, and the same in every tagged Shimmer3 release with the command). WriteCRCMode recorded the new mode only after its 300 ms sleep, so ReadData parsed that ACK in the old one. Off to two bytes broke. The ACK is FF F4 65, where F4 65 is the CRC of a bare ACK, and the connected-state loop never drops a bare ACK's CRC. F4 is no response code, but 0x65 is DETECT_EXPANSION_BOARD_RESPONSE. It took the next reply's ACK for a length of 255 and swallowed everything after it until a read timed out, after 1 s of silence: - A reply to a command sent after WriteCRCMode(TWO_BYTE) was lost. - A stream started straight after never reached SHIMMER_STATE_STREAMING. The start command's ACK went into the swallow, no packet was delivered, and the expansion board read at connect became "Unknown". - ShimmerConsoleAppExample connects this way. There the swallow ends in the 1.5 s waitTilTimeOut leaves when the status reply it swallowed never comes. The other switches worked only by luck. Off to one byte: F4 is no response code. One byte to two: F4's one-byte drop takes the 65. Two bytes to one: the drop loop rereads the mode for each byte, and WriteCRCMode has set one byte before the next reply arrives. To off: a bare FF needs nothing dropped. WriteCRCMode now sets mWaitingForCRCModeACK and mCRCModeAfterACK before it writes, and ReadData's connected-state ACK handler switches to that mode and drops its CRC. The flag is cleared when the wait ends, however it ends, so a later reply's ACK is never taken for it. If no ACK arrived within the wait, the mode is applied at its end, as before. A switch made while streaming is unchanged. ShimmerCrcSwitchTest runs ReadData against CrcLinkDevice, a CrcFirmwareDevice (ShimmerCrcFirmwareTest) that answers as the firmware does: it applies SET_CRC before it ACKs, frames each reply in its own mode with ShimmerUartCrcCalc, streams after START_STREAMING, and times reads out after ReadTimeout. Every switch is followed by two sampling-rate reads. Without the fix 3 of its 11 tests fail: off to two bytes, every switch in turn (at its off-to-two step), and the stream. Removing the drop of the ACK's CRC fails the stream test alone, and leaving the flag set fails the never-arriving ACK test alone. All 46 ShimmerBluetoothTests pass (Visual Studio 2022 MSBuild, vstest.console). Derived from the firmware source and a test double. Not run on hardware. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 4, 2026
Merged
JongChern
approved these changes
Oct 20, 2026
JongChern
left a comment
Contributor
There was a problem hiding this comment.
Verified against shimmer3r-firmware LogAndStream_Shimmer3R_v1.00.010: the SET_CRC case calls setBtCrcMode(args[0]) before the reply is built, and BtUart_sendRsp frames every reply in the current mode (shimmer_bt_comms.c:2344), so the ACK comes in the new mode. A bare ACK's CRC is F4 65 with this API's ShimmerUartCrcCalc (odd-length zero pad), and 0x65 is DETECT_EXPANSION_BOARD_RESPONSE, which explains the 255-byte swallow after off -> two-byte. The fix switches at the ACK, clears the flag in a finally, and leaves mid-stream switches alone. CI re-run against today's master: all green (unit tests 58/58, PublishNuGet pack).
JongChern
added a commit
that referenced
this pull request
Oct 20, 2026
Resolves the conflict with #212 (DEV-976, switch the link CRC at the SET_CRC ACK) as this PR's description sets out: - WriteCRCMode: the 2-byte refusal runs before mCRCModeAfterACK and the ACK wait flag are set, so a refused call never leaves the flag set. - Its docblock keeps both PRs' additions. - ShimmerUnitTests.csproj compiles both ShimmerCrcStatusPushTest.cs and ShimmerCrcSwitchTest.cs. Built ShimmerAPITest.sln with VS 18 MSBuild and ran ShimmerBluetoothTests under vstest.console, as CI does: 65 of 65 pass (master's 58 plus this PR's 7). ShimmerConsoleAppExample, which CI does not build, also builds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-976. The C# side of ShimmerResearch/shimmer-web-sdk#176 ("turning the link CRC off no longer loses its ACK"). In C# the switch that breaks is the opposite one: turning a two-byte CRC on.
The bug
SET_CRC_COMMAND, so the ACK is framed in the new mode. In shimmer3r-firmware atLogAndStream_Shimmer3R_v1.00.010,BtUart_processCmdcallssetBtCrcMode(shimmer_bt_comms.c:1262), andBtUart_sendRspreads the mode when it builds the reply (:2344). From v1.00.019 the code is in log-and-stream-common (Comms/shimmer_bt_uart.c:944and:2422at v1.01.017). I checked every tagged Shimmer3R and Shimmer3 LogAndStream release that has the command, and all of them do this.WriteCRCModerecorded the new mode only after its 300 ms sleep, soReadDataparsed that ACK in the old mode.0xFF. The CRC of a bare ACK is a constant:F4 65by the API's ownShimmerUartCrcCalc.Off → two bytes broke. The ACK is
FF F4 65.F4is no response code, but0x65isDETECT_EXPANSION_BOARD_RESPONSE. It took the next reply's ACK (0xFF) for a length and swallowed the 255 bytes after it, ending early only when a read timed out after 1 s of silence.WriteCRCMode(TWO_BYTE)is lost.0xF4). With the test double: 98 packets sent, 0 delivered, state Connected after 3 s.ShimmerConsoleAppExampleconnects this way (TWO_BYTEin its constructor). Reading the code, at connect the swallow takes the configuration writes' ACKs and the status reply. It ends during the 1.5 swaitTilTimeOutthat follows, because the status reply never arrives. So that connect loses its status read. This was not run.The other switches only worked by luck:
FF F4F4is no response codeFF F4 650x65read as an expansion board responseFFFF F4 65F4's one-byte drop takes the65FF F4F4's drop loop rereads the mode for each byte.WriteCRCModehas set one byte before the next reply arrives, so the loop drops only that reply's ACKShimmerCapture's CRC checkbox only switches between off and one byte, so it was never affected.
The fix
WriteCRCModesetsmWaitingForCRCModeACKandmCRCModeAfterACKbefore it writes. When the ACK arrives, the connected-state ACK handler switches to that mode and drops that many CRC bytes, as the firmware framed it.finally), so a later reply's ACK is never taken for this one.Testing
ShimmerUnitTests/ShimmerCrcSwitchTestruns the realReadDataagainstCrcLinkDevice, aCrcFirmwareDevicethat also answers the way the firmware does:SET_CRCbefore it ACKs,ShimmerUartCrcCalc,START_STREAMING,ReadTimeout, as a serial port's do.The tests cover:
F4 65, and0x65isDETECT_EXPANSION_BOARD_RESPONSE.Built with Visual Studio 2022's MSBuild (a full rebuild of
ShimmerAPITest.sln) and run undervstest.console, as CI does.Hardware: not run. This rests on the firmware source and the test double. To check it on a Shimmer3R on LogAndStream 1.0.11 or later, or a Shimmer3, call
WriteCRCMode(TWO_BYTE)and thenStartStreaming(): data should arrive. Before this PR, none did.Found, not fixed here
With a CRC on, the connected-state loop drops no bare ACK's CRC in steady state either. Its
F4then costs the next reply its first byte. That byte is usually the reply's own ACK, which does no harm. The exception is the START_STREAMING ACK:StartStreaming()leaves the API Connected with no data.WriteAccelRangethenStartStreaming(): one byte delivered 0 of 66 packets, two bytes 0 of 65, and off streamed.StartStreamingandLogis not affected, because it reads the status first.This is older than DEV-976 and has nothing to do with switching, so it is left for a separate change. Fixing it changes how every ACK is parsed while a CRC is on.
Other copies in this repo
ShimmerCaptureXamarin/ShimmerBluetooth.cshas its ownReadDatabut no CRC support, so it cannot turn one on.ShimmerAPI/TestSerialPort/Program.cshas no CRC code.ShimmerBluetooth.ReadData, so they get the fix:ShimmerLogAndStreamSystemSerialPort, the 32feet classes, andShimmerLogAndStreamBLE.Sibling APIs
Neither Java nor Swift needs this. Both were traced on paper; neither was run.
0x65.ShimmerBluetooth.java:2287-2289), so the next reply is read in the new mode.:687-689), and drops non-FFbytes while it waits for an ACK. So a strayF4 65never reaches the response parser, and the steady-state problem above does not arise.sendCRCCommandsetsCRCModeright after the write and before it awaits the reply (Shimmer3Protocol.swift:1458-1460). The SET_CRC handler then reads the ACK at that width (:695-713).connect(). add crc and PRR UI SwiftAPI#22 makes every switch reachable, and all of them are read at the new width.1 + CRCModebytes at the new width and keeps the new mode. That fix belongs in SwiftAPI's NACK PR: read a NACK to SET_CRC at the old width, and put the old mode back.Wrappers and consumers
ShimmerLogAndStreamSystemSerialPortconstructor. It never callsWriteCRCMode, so it never turns a CRC on and is not exposed. It picks this up only when it is rebuilt against a later release.Overlap with open PRs
Checked with
git merge-treeagainst this branch:#204 (NACK handling) composes with this one. A NACKed
SET_CRCskips the firmware's switch and arrives in the old mode. This PR switches only on an ACK, so #204 drops the NACK's CRC in the right mode. Two things are left for after both merge:WriteCRCModeapplies the new mode at the end of the wait even after a NACK, as master always has. DEV-982: handle NACK from a Shimmer3/3R instead of timing out the connection #204'sProcessNackFromCommandcould clearmWaitingForCRCModeACKso that the old mode is kept.ProcessNackFromCommanddrops it, and then the loop's trailingb != 0xffdrop takes the same number of bytes again. That is in DEV-982: handle NACK from a Shimmer3/3R instead of timing out the connection #204, not here.🤖 Generated with Claude Code