diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index 0b7b49c01..d23354449 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -3097,6 +3097,11 @@ private void initialiseStreaming(){ mSync=true; // a backup sync done every time you start streaming } + /** + * Starts SD logging, writing the PC's time to the clock first. On a Shimmer3R that is streaming + * the clock isn't written (see isRealTimeClockWriteBlocked()), and logging starts on the clock + * the connect sequence set. + */ @Override public void startSDLogging(){ //Shouldn't be logging anyway but just incase. @@ -4213,11 +4218,17 @@ public void readRealTimeClock(){ } } - /**Gets pc time and writes the 8 byte value to shimmer device - * + /** + * Queues a write of the PC's time to the Shimmer's real-world clock; the time is read just before + * the command is sent. Nothing is queued for a Shimmer3R that is streaming (see + * isRealTimeClockWriteBlocked()): the connect sequence sets the clock instead. */ public void writeRealTimeClock(){ if(getFirmwareIdentifier()==FW_ID.LOGANDSTREAM){ + if(isRealTimeClockWriteBlocked()){ + printLogDataForDebugging("SET_RWC_COMMAND not queued: the Shimmer3R is streaming (DEV-1337)"); + return; + } //Just fill empty bytes here for RTC, set them just before writing to Shimmer byte[] bytearraycommand= new byte[9]; bytearraycommand[0]=SET_RWC_COMMAND; @@ -4225,6 +4236,20 @@ public void writeRealTimeClock(){ } } + /** + * Whether a clock write must not be queued now. On a Shimmer3R the counter that stamps each + * streamed packet is the real-world clock, so setting the clock mid-stream steps it: packets come + * out off the sample grid and sometimes behind the one before, and a step back beyond the + * timestamp unwrap's reorder window reads as a counter wrap, stamping the rest of the session + * 512 s late (DEV-1245, firmware). So the clock is not written to a streaming Shimmer3R + * (DEV-1337). The connect sequence sets it, and if a status reply shows it unset, it is written at + * the next reply while the unit isn't streaming. A Shimmer3 keeps its timestamps in order across + * a clock write, so it is still written there. + */ + protected boolean isRealTimeClockWriteBlocked() { + return mIsStreaming && getHardwareVersion()==HW_ID.SHIMMER_3R; + } + public void readGyroRange() { writeInstruction(GET_MPU9150_GYRO_RANGE_COMMAND); } diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00027_StreamingBareAckCrcTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00027_StreamingBareAckCrcTest.java index 12c9dc5fc..912125651 100644 --- a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00027_StreamingBareAckCrcTest.java +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00027_StreamingBareAckCrcTest.java @@ -183,30 +183,25 @@ private void assertBuffered(byte... expected) { } /** - * startSDLogging() while streaming sends SET_RWC_COMMAND and then - * START_LOGGING_ONLY_COMMAND, and the firmware answers each with a bare ACK + * startSDLogging() while a Shimmer3R streams sends START_LOGGING_ONLY_COMMAND, + * with no clock write before it (DEV-1337), and the firmware answers with a + * bare ACK */ private void startSdLoggingWhileStreaming(BT_CRC_MODE crcMode) { streamingShimmer3r(crcMode); mDevice.startSDLogging(); - - sendNextInstruction(); - assertEquals(ShimmerObject.SET_RWC_COMMAND, mDevice.mCurrentCommand); - receive(dataPacket(payload(1), crcMode), bareAck(crcMode), - dataPacket(payload(2), crcMode), dataPacket(payload(3), crcMode)); - assertAckProcessed(); - assertEquals("START_LOGGING_ONLY_COMMAND is still queued", 1, mDevice.getListofInstructions().size()); + assertEquals("only START_LOGGING_ONLY_COMMAND is queued", 1, mDevice.getListofInstructions().size()); sendNextInstruction(); assertEquals(ShimmerObject.START_LOGGING_ONLY_COMMAND, mDevice.mCurrentCommand); - receive(dataPacket(payload(4), crcMode), bareAck(crcMode), - dataPacket(payload(5), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + receive(dataPacket(payload(1), crcMode), bareAck(crcMode), + dataPacket(payload(2), crcMode), dataPacket(payload(3), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); assertAckProcessed(); assertTrue(mDevice.getListofInstructions().isEmpty()); assertTrue(mDevice.isSDLogging()); assertEquals(BT_STATE.STREAMING_AND_SDLOGGING, mDevice.getBluetoothRadioState()); - assertParsed(payload(1), payload(2), payload(3), payload(4), payload(5)); + assertParsed(payload(1), payload(2), payload(3)); assertBuffered(ShimmerObject.DATA_PACKET); } diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00030_StreamingInstreamResponseQueueTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00030_StreamingInstreamResponseQueueTest.java index 1aba75222..66db792ee 100644 --- a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00030_StreamingInstreamResponseQueueTest.java +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00030_StreamingInstreamResponseQueueTest.java @@ -181,25 +181,28 @@ private void assertCompleted() { } /** - * startSDLogging() queues SET_RWC_COMMAND and START_LOGGING_ONLY_COMMAND. A - * status push between SET_RWC_COMMAND and its ACK must leave both queued, and - * the ACK must then complete SET_RWC_COMMAND, not START_LOGGING_ONLY_COMMAND. + * startSDLogging() queues START_LOGGING_ONLY_COMMAND (no clock write to a + * streaming Shimmer3R, DEV-1337), and readBattery() queues GET_VBATT_COMMAND + * behind it. A status push between START_LOGGING_ONLY_COMMAND and its ACK must + * leave both queued, and the ACK must then complete START_LOGGING_ONLY_COMMAND, + * not GET_VBATT_COMMAND. */ private void statusPushWhileASetCommandAwaitsItsAck(BT_CRC_MODE crcMode) { streamingShimmer3r(crcMode); mDevice.startSDLogging(); + mDevice.readBattery(); assertEquals(2, mDevice.getListofInstructions().size()); sendNextInstruction(); - assertEquals(ShimmerObject.SET_RWC_COMMAND, mDevice.mCurrentCommand); + assertEquals(ShimmerObject.START_LOGGING_ONLY_COMMAND, mDevice.mCurrentCommand); receive(dataPacket(1, crcMode), statusPush(crcMode), dataPacket(2, crcMode)); assertTrue("the status push is still processed", mDevice.isDocked()); - assertStillWaitingFor(ShimmerObject.SET_RWC_COMMAND, 2); + assertStillWaitingFor(ShimmerObject.START_LOGGING_ONLY_COMMAND, 2); receive(dataPacket(3, crcMode), bareAck(crcMode), dataPacket(4, crcMode), dataPacket(5, crcMode)); assertCompleted(); - assertEquals("START_LOGGING_ONLY_COMMAND is still queued", 1, mDevice.getListofInstructions().size()); - assertEquals(ShimmerObject.START_LOGGING_ONLY_COMMAND, mDevice.getListofInstructions().get(0)[0]); + assertEquals("GET_VBATT_COMMAND is still queued", 1, mDevice.getListofInstructions().size()); + assertEquals(ShimmerObject.GET_VBATT_COMMAND, mDevice.getListofInstructions().get(0)[0]); } @Test diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00032_StreamingAckAfterPauseTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00032_StreamingAckAfterPauseTest.java index 69f5e4e5d..3c20f522d 100644 --- a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00032_StreamingAckAfterPauseTest.java +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00032_StreamingAckAfterPauseTest.java @@ -251,16 +251,17 @@ public void batteryReplyInThePauseWithTwoByteCrc() { private void bareAckInThePause(BT_CRC_MODE crcMode) { streamingShimmer3r(crcMode); mDevice.stopSDLogging(); - mDevice.startSDLogging(); // SET_RWC_COMMAND, then START_LOGGING_ONLY_COMMAND + mDevice.startSDLogging(); // START_LOGGING_ONLY_COMMAND: no clock write to a streaming Shimmer3R (DEV-1337) + mDevice.readBattery(); assertEquals(3, mDevice.getListofInstructions().size()); stopLoggingAndReceiveItsAck(crcMode); sendNextInstruction(); - assertEquals(ShimmerObject.SET_RWC_COMMAND, mDevice.mCurrentCommand); + assertEquals(ShimmerObject.START_LOGGING_ONLY_COMMAND, mDevice.mCurrentCommand); receive(bareAck(crcMode), dataPacket(2, crcMode), new byte[] {ShimmerObject.DATA_PACKET}); assertCompleted(); - assertEquals("START_LOGGING_ONLY_COMMAND is still queued", 1, mDevice.getListofInstructions().size()); - assertEquals(ShimmerObject.START_LOGGING_ONLY_COMMAND, mDevice.getListofInstructions().get(0)[0]); + assertEquals("GET_VBATT_COMMAND is still queued", 1, mDevice.getListofInstructions().size()); + assertEquals(ShimmerObject.GET_VBATT_COMMAND, mDevice.getListofInstructions().get(0)[0]); assertParsed(payload(1), payload(2)); } diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00035_NoClockWriteWhileStreamingTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00035_NoClockWriteWhileStreamingTest.java new file mode 100644 index 000000000..900517dbc --- /dev/null +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00035_NoClockWriteWhileStreamingTest.java @@ -0,0 +1,218 @@ +package com.shimmerresearch.bluetooth; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.ConcurrentLinkedDeque; + +import org.junit.After; +import org.junit.Test; + +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; +import com.shimmerresearch.shimmer3.communication.ByteCommunication; +import com.shimmerresearch.verisense.communication.ByteCommunicationListener; + +import jssc.SerialPortTimeoutException; + +/** + * DEV-1337: on a Shimmer3R the counter that stamps each streamed packet is the real-world clock, so + * a clock write (SET_RWC) while it streams steps that counter. Packets come out off the sample grid + * and sometimes behind the one before, and a step back beyond the timestamp unwrap's reorder window + * reads as a counter wrap, stamping the rest of the session 512 s late (DEV-1245, firmware). + * startSDLogging() wrote the clock before every logging start, so starting logging mid-stream + * triggered it every time; the Android HIL's midStreamSet and inStream failed on a Shimmer3R for it. + *
+ * writeRealTimeClock() now queues nothing while a Shimmer3R streams. The connect sequence still + * writes the clock, and a Shimmer3, whose timestamps stay in order across a clock write, still gets + * it mid-stream. + *
+ * No hardware: the IOThread runs as connect() starts it, over an in-memory radio that acknowledges
+ * every command, and records what reaches the unit.
+ */
+public class API_00035_NoClockWriteWhileStreamingTest {
+
+ private static final long WAIT_MS = 5000;
+
+ /** Not docked, sensing or logging; RTC set */
+ private static final byte STATUS_IDLE = 0x04;
+
+ /** Sensing and SD logging; RTC set */
+ private static final byte STATUS_SD_LOGGING = 0x0E;
+
+ /** A Shimmer3R's second status byte: USB not plugged in */
+ private static final byte STATUS_USB_UNPLUGGED = 0x00;
+
+ private static class AnsweringRadio implements ByteCommunication {
+ final ConcurrentLinkedDeque