Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 68 additions & 4 deletions ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,22 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol {
var result = await radio?.connect()
if (result!){
startProcessing()
let res = await sendCRCCommand(crcMode: BTCRCMode.TWO_BYTE)
// Versions first, because which CRC is safe depends on the firmware (strongestSafeCRCMode).
// Off before them, so both ends read them without one: this class's CRCMode outlives a
// disconnect, and the firmware's outlives a link drop it never saw.
var res = await sendCRCCommand(crcMode: BTCRCMode.OFF)
if (res!){
let fwVersionRead = await sendReadFWVersionCommand()
let shimmerVersionRead = await sendReadShimmerVersionCommand()
var crcMode = BTCRCMode.OFF
if (fwVersionRead == true && shimmerVersionRead == true){
crcMode = strongestSafeCRCModeForDevice()
}
if (crcMode != BTCRCMode.OFF){
res = await sendCRCCommand(crcMode: crcMode)
}
}
if (res!){
await sendReadFWVersionCommand()
await sendReadShimmerVersionCommand()
await sendReadExpBoardVersionCommand()
createSensors()
//[0x8E 0x80 0x00 0x00]
Expand Down Expand Up @@ -1453,10 +1465,17 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol {
}

private func sendCRCCommand(crcMode:BTCRCMode) async ->Bool?{
// Refused rather than sent when this firmware cannot carry it (strongestSafeCRCMode).
let strongestSafe = strongestSafeCRCModeForDevice()
if (crcMode.rawValue > strongestSafe.rawValue){
print("Refusing CRC mode \(crcMode): firmware \(REV_FW_IDENTIFIER).\(REV_FW_MAJOR).\(REV_FW_MINOR).\(REV_FW_INTERNAL) on hardware \(REV_HW_MAJOR) carries \(strongestSafe) at most")
return false
}
let bytes:[UInt8] = [PacketTypeShimmer.setCRCCommand.rawValue,crcMode.rawValue]
commandSent = PacketTypeShimmer.setCRCCommand
radio!.writeBytes(bytes:bytes)
// Before the write, because the firmware applies the new mode to this command's own ACK.
self.CRCMode = crcMode
radio!.writeBytes(bytes:bytes)
return await withCheckedContinuation { continuation in
if self.continuation == nil {
// 2
Expand All @@ -1465,6 +1484,51 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol {
}
}

/// LogAndStream's identifier in the GET_FW_VERSION_COMMAND reply (`REV_FW_IDENTIFIER`).
static let FW_IDENTIFIER_LOGANDSTREAM = 3

/// The strongest link CRC this firmware carries safely, and so the one connect() turns on.
///
/// Everything gets two bytes except two Shimmer3R LogAndStream ranges:
///
/// - **v1.00.024 to v1.00.049 carry one byte at most.** They build the unsolicited status push in
/// `uint8_t selfcmd[6]` (`ShimBt_instreamStatusRespSend`, log-and-stream-common
/// `Comms/shimmer_bt_uart.c`). The push is the ACK prefix, 0x8A 0x71, two status bytes and the
/// CRC, so a 2-byte CRC makes seven bytes and the seventh overruns the buffer: the sensor
/// hardfaults (DEV-621, fixed in v1.00.050). A 1-byte CRC makes six, which fits. The firmware
/// pushes on dock and undock, the user button, a trial-duration expiry and a low-battery stop,
/// so the overrun can come at any point in a session.
/// - **v1.00.010 and earlier carry none.** They turn the CRC off by themselves whenever sensing
/// stops, without telling the host (`S4Sens_stopSensing`, shimmer3r-firmware
/// `S3R_Production/S4_App/s4_sensing.c` at v1.00.010). Every reply after a stop is then bare,
/// while this class still expects a CRC on it.
///
/// The hardware is checked first because Shimmer3 and Shimmer3R LogAndStream version numbers
/// overlap, and no Shimmer3 release has either problem. No CRC is safe until both versions have
/// been read (they are -1 until then); the protocol document likewise puts SET_CRC_COMMAND after
/// the version reads (log-and-stream-common `docs/SHIMMER3_BT_COMMUNICATION_PROTOCOL.md` §8.2).
static func strongestSafeCRCMode(hardwareVersion: Int, firmwareIdentifier: Int, firmwareMajor: Int, firmwareMinor: Int, firmwareInternal: Int) -> BTCRCMode {
if (hardwareVersion < 0 || firmwareIdentifier < 0){
return BTCRCMode.OFF
}
if (hardwareVersion != HardwareType.Shimmer3R.rawValue || firmwareIdentifier != FW_IDENTIFIER_LOGANDSTREAM){
return BTCRCMode.TWO_BYTE
}
let firmware = (firmwareMajor, firmwareMinor, firmwareInternal)
if (firmware < (1, 0, 11)){
return BTCRCMode.OFF
}
if (firmware >= (1, 0, 24) && firmware < (1, 0, 50)){
return BTCRCMode.ONE_BYTE
}
return BTCRCMode.TWO_BYTE
}

/// strongestSafeCRCMode for the versions this link has read.
private func strongestSafeCRCModeForDevice() -> BTCRCMode {
return Shimmer3Protocol.strongestSafeCRCMode(hardwareVersion: REV_HW_MAJOR, firmwareIdentifier: REV_FW_IDENTIFIER, firmwareMajor: REV_FW_MAJOR, firmwareMinor: REV_FW_MINOR, firmwareInternal: REV_FW_INTERNAL)
}

public func sendReadShimmerVersionCommand() async -> Bool?{
let bytes:[UInt8] = [PacketTypeShimmer.getShimmerVersionCommand.rawValue]
commandSent = PacketTypeShimmer.getShimmerVersionCommand
Expand Down
31 changes: 31 additions & 0 deletions ShimmerBluetooth/ShimmerBluetoothTests/ShimmerBluetoothTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,37 @@ class ShimmerBluetoothTests: XCTestCase {
// Put the code you want to measure the time of here.
}
}

func testStrongestSafeCRCMode() {
func mode(_ hardware: Int, _ firmwareIdentifier: Int, _ major: Int, _ minor: Int, _ fwInternal: Int) -> Shimmer3Protocol.BTCRCMode {
return Shimmer3Protocol.strongestSafeCRCMode(hardwareVersion: hardware, firmwareIdentifier: firmwareIdentifier, firmwareMajor: major, firmwareMinor: minor, firmwareInternal: fwInternal)
}
let shimmer3 = Shimmer3Protocol.HardwareType.Shimmer3.rawValue
let shimmer3R = Shimmer3Protocol.HardwareType.Shimmer3R.rawValue
let logAndStream = Shimmer3Protocol.FW_IDENTIFIER_LOGANDSTREAM

// Shimmer3R LogAndStream before v1.00.011 turns the CRC off whenever sensing stops.
XCTAssertEqual(mode(shimmer3R, logAndStream, 0, 0, 2), .OFF)
XCTAssertEqual(mode(shimmer3R, logAndStream, 0, 0, 30), .OFF, "major 0 is before v1.00.011, whatever the internal number")
XCTAssertEqual(mode(shimmer3R, logAndStream, 1, 0, 10), .OFF)
// From v1.00.011 the CRC survives a stop, and until v1.00.024 the push has one status byte, so two CRC bytes fit.
XCTAssertEqual(mode(shimmer3R, logAndStream, 1, 0, 11), .TWO_BYTE)
XCTAssertEqual(mode(shimmer3R, logAndStream, 1, 0, 23), .TWO_BYTE)
// v1.00.024 to v1.00.049: two status bytes in a six-byte push leave room for one CRC byte (DEV-621).
XCTAssertEqual(mode(shimmer3R, logAndStream, 1, 0, 24), .ONE_BYTE)
XCTAssertEqual(mode(shimmer3R, logAndStream, 1, 0, 49), .ONE_BYTE)
// v1.00.050 made room for two.
XCTAssertEqual(mode(shimmer3R, logAndStream, 1, 0, 50), .TWO_BYTE)
XCTAssertEqual(mode(shimmer3R, logAndStream, 1, 1, 30), .TWO_BYTE, "minor 1 is after v1.00.049, whatever the internal number")
// Shimmer3 version numbers overlap both ranges, and no Shimmer3 release has either problem.
XCTAssertEqual(mode(shimmer3, logAndStream, 1, 0, 10), .TWO_BYTE)
XCTAssertEqual(mode(shimmer3, logAndStream, 1, 0, 30), .TWO_BYTE)
// Firmware other than LogAndStream (1 is BtStream) keeps the 2-byte CRC it always had.
XCTAssertEqual(mode(shimmer3R, 1, 1, 0, 30), .TWO_BYTE)
// No CRC until both versions have been read.
XCTAssertEqual(mode(-1, logAndStream, 1, 0, 30), .OFF)
XCTAssertEqual(mode(shimmer3R, -1, -1, -1, -1), .OFF)
}



Expand Down
Loading