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 rx = new ConcurrentLinkedDeque(); + final List written = Collections.synchronizedList(new ArrayList()); + + void queue(byte... bytes) { + for(byte b:bytes){ + rx.add(b); + } + } + + List commandsWritten() { + List commands = new ArrayList(); + synchronized(written){ + for(byte[] w:written){ + commands.add(w[0]); + } + } + return commands; + } + + @Override + public boolean writeBytes(byte[] buffer) { + written.add(buffer.clone()); + queue(ShimmerObject.ACK_COMMAND_PROCESSED); + return true; + } + + @Override + public byte[] readBytes(int byteCount, int timeout) throws SerialPortTimeoutException { + if(rx.size() sent = radio.commandsWritten(); + assertFalse("no SET_RWC sent to a streaming Shimmer3R: " + sent, sent.contains(ShimmerObject.SET_RWC_COMMAND)); + assertTrue("logging still starts: " + sent, sent.contains(ShimmerObject.START_LOGGING_ONLY_COMMAND)); + } + + /** An app's own call is held back the same way */ + @Test + public void writeRealTimeClockOnAStreamingShimmer3rSendsNothing() throws Exception { + AnsweringRadio radio = connected(HW_ID.SHIMMER_3R, true); + mDevice.writeRealTimeClock(); + waitForQueueToDrain(); + assertEquals("nothing sent", new ArrayList(), radio.commandsWritten()); + } + + @Test + public void startSdLoggingOnAnIdleShimmer3rStillWritesTheClockFirst() throws Exception { + AnsweringRadio radio = connected(HW_ID.SHIMMER_3R, false); + mDevice.startSDLogging(); + waitForQueueToDrain(); + List sent = radio.commandsWritten(); + assertEquals("SET_RWC, then START_LOGGING_ONLY: " + sent, 2, sent.size()); + assertEquals(ShimmerObject.SET_RWC_COMMAND, (byte) sent.get(0)); + assertEquals(ShimmerObject.START_LOGGING_ONLY_COMMAND, (byte) sent.get(1)); + } + + /** + * A Shimmer3 keeps its timestamps in order across a clock write, so it is unchanged. Only the + * first command is checked: while streaming the driver takes an ACK only among stream data, which + * this radio doesn't send, so the clock write's ACK timeout clears the logging start behind it. + */ + @Test + public void startSdLoggingOnAStreamingShimmer3StillWritesTheClock() throws Exception { + AnsweringRadio radio = connected(HW_ID.SHIMMER_3, true); + mDevice.startSDLogging(); + waitForQueueToDrain(); + List sent = radio.commandsWritten(); + assertFalse("something was sent", sent.isEmpty()); + assertEquals("SET_RWC sent first to a streaming Shimmer3: " + sent, ShimmerObject.SET_RWC_COMMAND, (byte) sent.get(0)); + } + + /** + * The clock is set at connect instead: the connect sequence reads the status, and while the unit + * isn't initialised or logging, that reply queues a clock write. No IOThread here: the reply is + * handed to the driver's parser as the IO thread does, and the queue is inspected. + */ + private ShimmerPC connectingShimmer3r(byte status) { + AnsweringRadio radio = new AnsweringRadio(); + mDevice = new ShimmerPC("COM99"); + mDevice.setTestRadio(radio); + mDevice.setShimmerVersionObjectAndCreateSensorMap(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 22)); + mDevice.mCurrentCommand = ShimmerObject.GET_STATUS_COMMAND; + radio.queue(ShimmerObject.STATUS_RESPONSE, status, STATUS_USB_UNPLUGGED); + mDevice.processInstreamResponse(false); + assertTrue("the whole reply is read", radio.rx.isEmpty()); + return mDevice; + } + + private static boolean queued(ShimmerPC device, byte command) { + for(byte[] instruction:device.getListofInstructions()){ + if(instruction!=null && instruction[0]==command){ + return true; + } + } + return false; + } + + @Test + public void aConnectToAUnitNotLoggingWritesTheClock() { + ShimmerPC device = connectingShimmer3r(STATUS_IDLE); + assertTrue("SET_RWC queued during connect", queued(device, ShimmerObject.SET_RWC_COMMAND)); + } + + /** A logging unit's clock is left alone, so its SD log isn't stepped */ + @Test + public void aConnectToALoggingUnitLeavesTheClock() { + ShimmerPC device = connectingShimmer3r(STATUS_SD_LOGGING); + assertFalse("no SET_RWC queued for a logging unit", queued(device, ShimmerObject.SET_RWC_COMMAND)); + } +} diff --git a/tools/hil/README.md b/tools/hil/README.md index d73eb49a5..324f763b7 100644 --- a/tools/hil/README.md +++ b/tools/hil/README.md @@ -65,7 +65,7 @@ Each report counts what the session covered: connects, in-stream replies by type battery readings decoded, the status timer's requests, and command timeouts. Its events also say which driver timers were running after connect, after configure and while streaming. -Starting SD logging mid-stream (`startSDLogging()` while streaming) writes the RTC mid-stream, which a Shimmer3R answers with out-of-order timestamps (DEV-1245). Consensys never does that, and neither do these scenarios. +A Shimmer3R answers a clock write (`SET_RWC`) while it streams with out-of-order timestamps (DEV-1245, firmware). Up to 0.11.11_beta, `startSDLogging()` while streaming wrote the clock first. Since DEV-1337 the driver doesn't send a clock write to a streaming Shimmer3R; the connect sequence sets the clock. These scenarios start logging the Consensys way regardless. The stream checks are the Android HIL's (`StreamAnalysis`, the same class in ShimmerAndroidAPI's `shimmerHilTest`), so the two judge a stream by the same rules. `StreamAnalysisTest` runs in CI, as