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 queuedCrcModes(ShimmerPC device) { + List modes = new ArrayList(); + for(byte[] instruction:device.getListofInstructions()){ + if(instruction!=null && instruction[0]==ShimmerObject.SET_CRC_COMMAND){ + modes.add((int) instruction[1]); + } + } + return modes; + } + + private static void assertCrcRefused(ShimmerPC device) { + String label = describe(device); + assertFalse(label, device.isBtCrcModeSupported()); + assertFalse(label, device.writeBtCommsCrcMode(BT_CRC_MODE.ONE_BYTE_CRC)); + assertFalse(label, device.enableBtCommsOneByteCrc()); + assertFalse(label, device.enableBtCommsTwoByteCrc()); + assertEquals(label + ": nothing is sent", Arrays.asList(), queuedCrcModes(device)); + } + + private static void assertCrcAllowed(ShimmerPC device) { + String label = describe(device); + assertTrue(label, device.isBtCrcModeSupported()); + assertTrue(label, device.writeBtCommsCrcMode(BT_CRC_MODE.ONE_BYTE_CRC)); + assertEquals(label, Arrays.asList(BT_CRC_MODE.ONE_BYTE_CRC.ordinal()), queuedCrcModes(device)); + } + + @Test + public void shimmer3rBeforeV1_00_011IsRefused() { + assertCrcRefused(device(HW_ID.SHIMMER_3R, 1, 0, 10)); // the last release that clears it at a stop + assertCrcRefused(device(HW_ID.SHIMMER_3R, 1, 0, 7)); + assertCrcRefused(device(HW_ID.SHIMMER_3R, 0, 0, 2)); // the first release + } + + @Test + public void shimmer3rFromV1_00_011IsAllowed() { + assertCrcAllowed(device(HW_ID.SHIMMER_3R, 1, 0, 11)); + // v1.01.000: the internal number restarts at each minor release, so it + // cannot decide on its own + assertCrcAllowed(device(HW_ID.SHIMMER_3R, 1, 1, 0)); + assertCrcAllowed(device(HW_ID.SHIMMER_3R, 2, 0, 0)); + } + + @Test + public void shimmer3IsUnchanged() { + // The same version numbers on a Shimmer3, whose firmware clears the CRC + // only at startup and on disconnect. v1.00.008 is also what the Shimmer3R + // v1.00.008 side build reports, as it claims to be a Shimmer3. + assertCrcAllowed(device(HW_ID.SHIMMER_3, 1, 0, 8)); + assertCrcAllowed(device(HW_ID.SHIMMER_3, 1, 0, 10)); + // The Shimmer3 floor for SET_CRC_COMMAND has not moved + assertCrcAllowed(device(HW_ID.SHIMMER_3, 0, 13, 7)); + assertFalse(device(HW_ID.SHIMMER_3, 0, 13, 6).isBtCrcModeSupported()); + } + + @Test + public void turningTheCrcOffIsNeverRefused() { + ShimmerPC device = device(HW_ID.SHIMMER_3R, 1, 0, 10); + assertTrue(device.writeBtCommsCrcMode(BT_CRC_MODE.OFF)); + assertTrue(device.disableBtCommsCrc()); + assertEquals(Arrays.asList(BT_CRC_MODE.OFF.ordinal(), BT_CRC_MODE.OFF.ordinal()), queuedCrcModes(device)); + } +} diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_0000X_ByteCommunicationShimmer3.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_0000X_ByteCommunicationShimmer3.java index c9332a3f3..3a4de3a82 100644 --- a/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_0000X_ByteCommunicationShimmer3.java +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_0000X_ByteCommunicationShimmer3.java @@ -5,6 +5,7 @@ import org.junit.Test; import org.junit.runners.MethodSorters; +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_CRC_MODE; import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE; import com.shimmerresearch.driver.BasicProcessWithCallBack; import com.shimmerresearch.driver.CallbackObject; @@ -64,6 +65,9 @@ public void test001_testConnectandDisconnect() { if (!(mDevice.getHardwareVersion()==HW_ID.SHIMMER_3)) { assert(false); } + // A Shimmer3 keeps the link CRC when sensing stops, so connecting turns + // on the driver default (DEV-976) + assertEquals(BT_CRC_MODE.ONE_BYTE_CRC, mDevice.getCurrentBtCommsCrcMode()); System.out.println(mDevice.getHardwareVersionParsed()); if (!mDevice.getHardwareVersionParsed().equals("Shimmer3")) { diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_0000X_ByteCommunicationShimmer3R.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_0000X_ByteCommunicationShimmer3R.java index 8458e9341..f04fa4111 100644 --- a/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_0000X_ByteCommunicationShimmer3R.java +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_0000X_ByteCommunicationShimmer3R.java @@ -5,6 +5,7 @@ import org.junit.Test; import org.junit.runners.MethodSorters; +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_CRC_MODE; import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE; import com.shimmerresearch.driver.BasicProcessWithCallBack; import com.shimmerresearch.driver.CallbackObject; @@ -80,6 +81,9 @@ public void test001_testConnectandDisconnect() { if (!(mDevice.getHardwareVersion()==HW_ID.SHIMMER_3R)) { assert(false); } + // LogAndStream v0.0.1 is before v1.00.011, so this Shimmer3R would drop + // a link CRC whenever sensing stops: connecting must not turn one on (DEV-976) + assertEquals(BT_CRC_MODE.OFF, mDevice.getCurrentBtCommsCrcMode()); System.out.println(mDevice.getHardwareVersionParsed()); if (!mDevice.getHardwareVersionParsed().equals(ShimmerVerDetails.mMapOfShimmerRevisions.get(HW_ID.SHIMMER_3R))) {