Skip to content

DEV-1144: choose the link CRC from the firmware version, after reading it - #23

Merged
JongChern merged 1 commit into
mainfrom
DEV-1144_crc_mode_by_firmware
Nov 4, 2026
Merged

JongChern merged 1 commit into
mainfrom
DEV-1144_crc_mode_by_firmware

Conversation

@marknolan

Copy link
Copy Markdown
Member

Summary

connect() sent SET_CRC 2 on every connect, before it had read any version. So it could not know that two Shimmer3R LogAndStream ranges cannot carry a 2-byte CRC. It now reads the versions first, then asks for the strongest CRC that firmware carries safely.

Firmware What a 2-byte CRC does there CRC now
Shimmer3R LogAndStream v1.00.024 to v1.00.049 The status push overruns its buffer and the sensor hardfaults (DEV-621). 1 byte
Shimmer3R LogAndStream v1.00.010 and earlier The CRC is dropped at every stop, and the next reply is misread. From the code, Swift then traps in removeFirst. none
Everything else, including every Shimmer3 Nothing. 2 bytes, as before

The two ranges

  • v1.00.024 to v1.00.049. The firmware builds the unsolicited status push in uint8_t selfcmd[6] (ShimBt_instreamStatusRespSend, log-and-stream-common Comms/shimmer_bt_uart.c:2262 at f39be8c1f, which v1.00.049 pins).
    • The push is the ACK prefix (on by default), 0x8A 0x71, two status bytes and the CRC. A 2-byte CRC makes 7 bytes, and the 7th overruns the buffer. Fixed in v1.00.050.
    • A 1-byte CRC makes 6 bytes, which fits.
    • The push fires on dock and undock, the user button, trial-duration expiry and a low-battery stop. The host's own starts and stops do not push.
  • v1.00.010 and earlier. S4Sens_stopSensing calls setBtCrcMode(CRC_OFF) (shimmer3r-firmware S3R_Production/S4_App/s4_sensing.c:354 at v1.00.010; gone at v1.00.011).

What changed

  • connect() now runs SET_CRC off, then the FW version, then the device version, then SET_CRC with strongestSafeCRCMode. The last step is skipped when that mode is off.
    • SET_CRC off goes first so that both ends read the versions without a CRC. This class's CRCMode survives a disconnect, and the firmware's survives a link drop it never saw. The Java driver also resets the CRC first, and the protocol doc (§8.2) puts SET_CRC after the version reads.
    • If either version read fails, no CRC is turned on.
  • strongestSafeCRCMode(...) is a new static function that makes the decision from the hardware, the firmware identifier and the version.
    • It checks the hardware first, because Shimmer3 and Shimmer3R LogAndStream version numbers overlap.
    • Unread versions (-1) get no CRC.
  • sendCRCCommand refuses any mode stronger than that, and returns false. That covers every later caller, notably add crc and PRR UI #22's public setCRCMode.
  • sendCRCCommand now sets CRCMode before writing the command, not after. The firmware applies the new mode to the command's own ACK. The first command now switches down from a possibly stale mode, so an ACK handled in that gap would removeFirst past the end of the buffer.

Why not the web SDK's fix

The web SDK (DEV-1141, ShimmerResearch/shimmer-web-sdk#183) keeps the 2-byte CRC and turns the push's ACK prefix off with 0xA3. Swift cannot take that route, because it parses no status pushes at all.

An unprefixed push starts with 0x8A, and the idle receive loop's 0xFF check never consumes it. Every later reply would queue behind it, and since there are no command timeouts, every later call would hang.

Testing: unbuilt and untested

  • This was written without Xcode. This PR's CI run is the first time the code is compiled or run.
  • New testStrongestSafeCRCMode in ShimmerBluetoothTests, 14 assertions:
    • each range boundary: v1.00.010/011, 023/024 and 049/050;
    • major- and minor-number cases that a check on the internal number alone would get wrong;
    • Shimmer3 at overlapping version numbers;
    • non-LogAndStream firmware;
    • unread versions.
  • Not exercised by CI:
    • connect() and sendCRCCommand. Shimmer3Protocol cannot be constructed in a test, because init needs a live CBPeripheral.
    • The receive loop's off and 1-byte paths. On main, every reply Swift has ever parsed was under a 2-byte CRC, so this change runs the off path for the first time, on every connect and every device. It has been read, not run.
  • No hardware: no Shimmer3R in either range has been run against SwiftAPI.

Open PRs

Not covered

🤖 Generated with Claude Code

…g it

connect() turned a 2-byte CRC on before reading any version, so it could
not know that two Shimmer3R LogAndStream ranges cannot carry one:

- v1.00.024 to v1.00.049 build the unsolicited status push in a 6-byte
  buffer. Behind the default ACK prefix a 2-byte CRC makes the push 7
  bytes, and the sensor hardfaults (DEV-621, fixed in v1.00.050). One
  CRC byte fits.
- v1.00.010 and earlier turn the CRC off whenever sensing stops, so every
  reply after a stop arrives without the trailer this class strips.

connect() now sends SET_CRC off, reads the firmware and device versions,
then asks for strongestSafeCRCMode: 1 byte on the first range, none on
the second, 2 bytes everywhere else as before. sendCRCCommand refuses
any stronger mode, which also covers the public setCRCMode in #22. It
also now sets CRCMode before the write, since the firmware applies the
new mode to the command's own ACK.

Unbuilt and untested: written without Xcode, so CI is the first compile.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JongChern
JongChern merged commit f9f054a into main Nov 4, 2026
1 check 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