diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index 7a7a47e10..3129b2721 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -2626,6 +2626,9 @@ private void initializeShimmer3or3R(int hardwareVersion){ if (isBtCrcModeSupported()) { writeBtCommsCrcMode(DEFAULT_BT_CRC_MODE_IF_SUPPORTED); + } else if (DEFAULT_BT_CRC_MODE_IF_SUPPORTED!=BT_CRC_MODE.OFF && isBtCrcClearedWhenSensingStops()) { + printLogDataForDebugging("Link CRC left off: " + getFirmwareVersionParsed() + + " on a Shimmer3R drops it whenever sensing stops; v1.0.11 and later keep it"); } if (RN4678_ERROR_DETECTION_ENABLED && isSupportedRn4678ErrorDetection()) { @@ -5023,8 +5026,17 @@ public boolean enableBtCommsTwoByteCrc() { return writeBtCommsCrcMode(BT_CRC_MODE.TWO_BYTE_CRC); } + /** + * Queues SET_CRC_COMMAND. Turning a CRC on is refused wherever + * {@link #isBtCrcModeSupported()} is false. Turning it off is refused only by + * firmware without the command. + * + * @param btCrcMode + * @return false if nothing was queued + */ public boolean writeBtCommsCrcMode(BT_CRC_MODE btCrcMode) { - if (getFirmwareVersionCode() >= 8) { + boolean isAllowed = btCrcMode==BT_CRC_MODE.OFF ? getFirmwareVersionCode() >= 8 : isBtCrcModeSupported(); + if (isAllowed) { writeInstruction(new byte[] { SET_CRC_COMMAND, (byte) (btCrcMode.ordinal()) }); return true; } @@ -5034,6 +5046,8 @@ public boolean writeBtCommsCrcMode(BT_CRC_MODE btCrcMode) { /** * Sets the default mode that should be used when establishing a connection to * the Shimmer if the current firmware version supports the CRC feature. + * Shimmer3R LogAndStream before v1.00.011 counts as not supporting it; see + * {@link #isBtCrcModeSupported()}. * * @param btCommsCrcMode */ @@ -5080,11 +5094,42 @@ private void resetCurrentCrcMode() { /** * Response only valid if a connection has been established and the firmware * version has been read from the sensor. + *
+ * False on Shimmer3R LogAndStream before v1.00.011 although it has + * SET_CRC_COMMAND, because it drops the CRC whenever sensing stops; see + * {@link #isBtCrcClearedWhenSensingStops()}. * * @return */ public boolean isBtCrcModeSupported() { - return getFirmwareVersionCode() >= 8; + return getFirmwareVersionCode() >= 8 && !isBtCrcClearedWhenSensingStops(); + } + + /** + * True for Shimmer3R LogAndStream before v1.00.011. Those releases turn the + * Bluetooth link CRC off by themselves whenever sensing stops + * (S4Sens_stopSensing, s4_sensing.c:354 at v1.00.010): after STOP_STREAMING, + * STOP_SDBT or STOP_LOGGING, and when the user button or docking ends SD + * logging, without telling the host. The stop's own ACK still carries the CRC + * and every reply after it is bare, so a host still expecting the CRC loses + * sync after every stop, including stops it did not ask for. c8016de3 removed + * the clear: v1.00.011 and later clear it only at startup and on disconnect, + * and no Shimmer3 release clears it at a stop. + *
+ * Shimmer3 and Shimmer3R LogAndStream version numbers overlap, so this follows + * the hardware the device reports. The Shimmer3R v1.00.008 side build for + * older Consensys reports a Shimmer3 and is let through, as it is by the web + * SDK's keepsLinkCrcWhenSensingStops(). + *
+ * Derived from the firmware source; not yet run against a Shimmer3R on + * v1.00.010 or earlier. + * + * @return + */ + public boolean isBtCrcClearedWhenSensingStops() { + return getHardwareVersion()==HW_ID.SHIMMER_3R + && getFirmwareIdentifier()==FW_ID.LOGANDSTREAM + && !isThisVerCompatibleWith(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 0, 11); } /**** DISABLE FUNCTIONS *****/ diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00025_Shimmer3rBtCrcGateTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00025_Shimmer3rBtCrcGateTest.java new file mode 100644 index 000000000..816ed0cdf --- /dev/null +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00025_Shimmer3rBtCrcGateTest.java @@ -0,0 +1,110 @@ +package com.shimmerresearch.bluetooth; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_CRC_MODE; +import com.shimmerresearch.driver.ShimmerObject; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerObject; +import com.shimmerresearch.pcDriver.ShimmerPC; + +/** + * DEV-976: Shimmer3R LogAndStream v0.00.002 to v1.00.010 turn the Bluetooth + * link CRC off by themselves whenever sensing stops, and nothing tells the + * host. The stop's own ACK still carries the CRC; every reply after it is bare, + * so a driver still expecting the CRC loses sync after every stop. v1.00.011 is + * the first release that keeps it. The driver must not turn a CRC on for the + * earlier releases, at connect or when asked to. + *
+ * Shimmer3 and Shimmer3R LogAndStream version numbers overlap, and no Shimmer3 + * firmware clears the CRC at a stop, so the gate has to check the hardware too. + *
+ * No hardware: the connect path writes the default CRC mode only when
+ * isBtCrcModeSupported() is true, so the gate and the instruction stack are
+ * what these check. It lives in this package because the instruction stack is a
+ * protected member of ShimmerBluetooth.
+ *
+ * @author Mark Nolan
+ */
+public class API_00025_Shimmer3rBtCrcGateTest {
+
+ private static ShimmerPC device(int hardwareVersion, int fwMajor, int fwMinor, int fwInternal) {
+ ShimmerPC device = new ShimmerPC("COM99");
+ device.setShimmerVersionObjectAndCreateSensorMap(new ShimmerVerObject(hardwareVersion, FW_ID.LOGANDSTREAM, fwMajor, fwMinor, fwInternal));
+ return device;
+ }
+
+ private static String describe(ShimmerPC device) {
+ return device.getHardwareVersionParsed() + " " + device.getFirmwareVersionParsed();
+ }
+
+ /** The argument of every SET_CRC_COMMAND on the instruction stack, in order */
+ private static List