diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index df8801f60..012d39546 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -1119,10 +1119,89 @@ protected void processWhileStreaming() { printLogDataForDebugging("readbyte null"); } + if(processAckAtStartOfBuffer()){ + return; + } + //If there is a full packet and the subsequent sequence number of following packet if(mByteArrayOutputStream.size()>=getPacketSizeWithCrc()+2){ // +2 because there are two acks processPacket(); - } + } + } + + /** + * Handles an ACK at the very start of the buffer, with no data packet before + * it. The unit sends one when it answers while its data packets are paused, as + * they are for about 100 ms while it closes its SD file after + * STOP_LOGGING_ONLY_COMMAND (DEV-1291). processPacket() only looks for an ACK + * after a data packet, so it threw such a reply away; the command then timed + * out, and the timeout dropped every queued instruction. + *

+ * It is taken only while a command waits for its ACK, and only when the bytes + * after it are what that command expects: its own in-stream reply (FF 8A and + * then its response opcode), or for a SET command a bare ACK whose link CRC + * checks out. A 0xFF data byte the parser resyncs onto is therefore not taken + * for one. Without a link CRC a bare ACK can't be told from a data byte, but + * then the previous ACK isn't processed until the stream resumes either, so the + * next command never goes out during the pause. + * + * @return true while the bytes so far are taken, or may still turn out to be + * such an ACK. The buffer is then at most 3 bytes, too short for + * processPacket() anyway. + */ + private boolean processAckAtStartOfBuffer() { + if(!mWaitForAck || mByteArrayOutputStream.size()==0){ + return false; + } + byte[] bufferTemp = mByteArrayOutputStream.toByteArray(); + if(bufferTemp[0]!=ACK_COMMAND_PROCESSED){ + return false; + } + if(bufferTemp.length==1){ + return true; //wait for what follows the ACK + } + + //The in-stream reply to the GET command in flight + if(isSupportedInStreamCmds() && bufferTemp[1]==INSTREAM_CMD_RESPONSE){ + if(bufferTemp.length==2){ + return true; //wait for the response opcode + } + if(bufferTemp.length>3 || !isReplyToCommandInFlight(bufferTemp[2])){ + return false; + } + printLogDataForDebugging("COMMAND TXed and ACK RECEIVED IN STREAM, with no data packet before it"); + stopTimerCheckForAckOrResp(); //cancel the ack timer + mWaitForResponse=false; + mWaitForAck=false; + + //Reads the rest of the response, and its CRC, from the port + processInstreamResponse(bufferTemp[2], true); + + if(getListofInstructions().size()>0){ + removeInstruction(0); + } + mTransactionCompleted=true; + setInstructionStackLock(false); + clearBuffers(); + return true; + } + + //A bare ACK to the SET command in flight + if(isKnownSetCommand(mCurrentCommand) && mBtCommsCrcModeCurrent!=BT_CRC_MODE.OFF){ + int numCrcBytes = mBtCommsCrcModeCurrent.getNumCrcBytes(); + for(int i=1;i + * Only with a link CRC: without one the previous ACK isn't processed until the + * stream resumes, so the next command never goes out during the pause. + *

+ * No hardware: the parser reads from an in-memory byte queue, one byte at a + * time as the IO thread does, as in API_00027 and API_00030. + */ +public class API_00032_StreamingAckAfterPauseTest { + + /** Bytes after the DATA_PACKET byte: a 3-byte timestamp and three 2-byte channels */ + private static final int PACKET_SIZE = 9; + + /** Docked, sensing, RTC set and streaming; not SD logging */ + private static final byte STATUS_DOCKED_AND_STREAMING = 0x17; + + private static class QueueRadio implements ByteCommunication { + final Deque rx = new ArrayDeque(); + + void queue(byte... bytes) { + for(byte b:bytes){ + rx.add(b); + } + } + + @Override + public byte[] readBytes(int byteCount, int timeout) throws SerialPortTimeoutException { + if(rx.size() expected = new ArrayList(); + for(byte[] payload:expectedPayloads){ + expected.add(UtilShimmer.bytesToHexStringWithSpacesFormatted(payload)); + } + List parsed = new ArrayList(); + // Package-private, so ShimmerPC does not inherit it + ShimmerBluetooth device = mDevice; + for(RawBytePacketWithPCTimeStamp packet:device.mABQPacketByeArray){ + parsed.add(UtilShimmer.bytesToHexStringWithSpacesFormatted(packet.mDataArray)); + } + assertEquals("every data packet is parsed, once and in order", expected, parsed); + } + + private void assertCompleted() { + assertFalse("the ACK is taken", mDevice.mWaitForAck); + assertFalse("the next instruction can be sent", mDevice.isInstructionStackLock()); + assertNull("the ACK timer is cancelled", mDevice.mTimerCheckForAckOrResp); + } + + /** GET_STATUS_COMMAND sent in the pause: its reply must complete it, and nothing queued behind it may be lost */ + private void statusReplyInThePause(BT_CRC_MODE crcMode) { + streamingShimmer3r(crcMode); + mDevice.stopSDLogging(); + mDevice.readStatusLogAndStream(); + mDevice.readBattery(); + stopLoggingAndReceiveItsAck(crcMode); + + sendNextInstruction(); + assertEquals(ShimmerObject.GET_STATUS_COMMAND, mDevice.mCurrentCommand); + receive(statusReply(crcMode), dataPacket(2, crcMode), dataPacket(3, crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertCompleted(); + assertTrue("the status reply is decoded", mDevice.isDocked()); + 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), payload(3)); + } + + @Test + public void statusReplyInThePauseWithOneByteCrc() { + statusReplyInThePause(BT_CRC_MODE.ONE_BYTE_CRC); + } + + @Test + public void statusReplyInThePauseWithTwoByteCrc() { + statusReplyInThePause(BT_CRC_MODE.TWO_BYTE_CRC); + } + + /** GET_VBATT_COMMAND sent in the pause */ + private void batteryReplyInThePause(BT_CRC_MODE crcMode) { + streamingShimmer3r(crcMode); + mDevice.stopSDLogging(); + mDevice.readBattery(); + stopLoggingAndReceiveItsAck(crcMode); + + sendNextInstruction(); + receive(vbattReply(crcMode), dataPacket(2, crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertCompleted(); + assertTrue(mDevice.getListofInstructions().isEmpty()); + assertEquals(0x0B24, mDevice.getBattStatusDetails().getBattAdcValue(), 0); + assertParsed(payload(1), payload(2)); + } + + @Test + public void batteryReplyInThePauseWithOneByteCrc() { + batteryReplyInThePause(BT_CRC_MODE.ONE_BYTE_CRC); + } + + @Test + public void batteryReplyInThePauseWithTwoByteCrc() { + batteryReplyInThePause(BT_CRC_MODE.TWO_BYTE_CRC); + } + + /** A SET command sent in the pause: its bare ACK must complete it */ + private void bareAckInThePause(BT_CRC_MODE crcMode) { + streamingShimmer3r(crcMode); + mDevice.stopSDLogging(); + mDevice.startSDLogging(); // SET_RWC_COMMAND, then START_LOGGING_ONLY_COMMAND + assertEquals(3, mDevice.getListofInstructions().size()); + stopLoggingAndReceiveItsAck(crcMode); + + sendNextInstruction(); + assertEquals(ShimmerObject.SET_RWC_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]); + assertParsed(payload(1), payload(2)); + } + + @Test + public void bareAckInThePauseWithOneByteCrc() { + bareAckInThePause(BT_CRC_MODE.ONE_BYTE_CRC); + } + + @Test + public void bareAckInThePauseWithTwoByteCrc() { + bareAckInThePause(BT_CRC_MODE.TWO_BYTE_CRC); + } + + /** + * An ACK at the start of the buffer is taken only for the reply the command in + * flight expects: a status push there does not complete GET_VBATT_COMMAND. + */ + private void anotherReplyAtTheStartIsNotTaken(BT_CRC_MODE crcMode) { + streamingShimmer3r(crcMode); + mDevice.readBattery(); + sendNextInstruction(); + + receive(statusReply(crcMode), dataPacket(1, crcMode), dataPacket(2, crcMode)); + assertEquals("GET_VBATT_COMMAND is still in flight", ShimmerObject.GET_VBATT_COMMAND, mDevice.mCurrentCommand); + assertTrue("it is still waiting for its ACK", mDevice.mWaitForAck); + assertTrue("the next instruction is not sent before it completes", mDevice.isInstructionStackLock()); + assertEquals(1, mDevice.getListofInstructions().size()); + } + + @Test + public void anotherReplyAtTheStartIsNotTakenWithOneByteCrc() { + anotherReplyAtTheStartIsNotTaken(BT_CRC_MODE.ONE_BYTE_CRC); + } + + @Test + public void anotherReplyAtTheStartIsNotTakenWithTwoByteCrc() { + anotherReplyAtTheStartIsNotTaken(BT_CRC_MODE.TWO_BYTE_CRC); + } +}