diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index abdbf73ed..df8801f60 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -1052,22 +1052,34 @@ else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTR printLogDataForDebugging("COMMAND TXed and ACK RECEIVED IN STREAM"); printLogDataForDebugging("INS CMD RESP"); - //Firstly handle the in-stream response - stopTimerCheckForAckOrResp(); //cancel the ack timer - mWaitForResponse=false; - mWaitForAck=false; + // The firmware also sends a status response unprompted (on a dock or + // undock, the user button, a low-battery stop), so this completes the + // command in flight only if the response is that command's reply + byte[] inStreamResponseCommand = readBytes(1, INSTREAM_CMD_RESPONSE); + boolean isReplyToCommandInFlight = inStreamResponseCommand!=null + && isReplyToCommandInFlight(inStreamResponseCommand[0]); + if(isReplyToCommandInFlight){ + stopTimerCheckForAckOrResp(); //cancel the ack timer + mWaitForResponse=false; + mWaitForAck=false; + } - processInstreamResponse(true); + //Firstly handle the in-stream response + if(inStreamResponseCommand!=null){ + processInstreamResponse(inStreamResponseCommand[0], true); + } - // Need to remove here because it is an - // in-stream response while streaming so not - // handled elsewhere - if(getListofInstructions().size()>0){ - removeInstruction(0); + if(isReplyToCommandInFlight){ + // Need to remove here because it is an + // in-stream response while streaming so not + // handled elsewhere + if(getListofInstructions().size()>0){ + removeInstruction(0); + } + + mTransactionCompleted=true; + setInstructionStackLock(false); } - - mTransactionCompleted=true; - setInstructionStackLock(false); //Then process the Data packet processDataPacket(bufferTemp); @@ -1203,76 +1215,94 @@ else if(currentCommand==GET_INFOMEM_COMMAND || currentCommand==GET_CALIB_DUMP_CO } } + /** + * Whether an in-stream response is the reply to the command in flight. The + * protocol matches a reply to its command by the response opcode, never by the + * ACK (log-and-stream-common docs/SHIMMER3_BT_COMMUNICATION_PROTOCOL.md + * section 5.4, rule 2). + */ + private boolean isReplyToCommandInFlight(byte inStreamResponseCommand) { + if(!mWaitForAck && !mWaitForResponse){ + return false; + } + BtCommandDetails commandDetails = mBtGetCommandMap.get(mCurrentCommand); + return commandDetails!=null && commandDetails.expectedResponse==inStreamResponseCommand; + } + /** process responses to in-stream response */ protected void processInstreamResponse(boolean shouldClearCrcFromBuffer) { - boolean responseWasParsed = true; - byte[] inStreamResponseCommandBuffer = readBytes(1, INSTREAM_CMD_RESPONSE); if(inStreamResponseCommandBuffer!=null){ - byte inStreamResponseCommand = inStreamResponseCommandBuffer[0]; - consolePrintLn("In-stream received = " + btCommandToString(inStreamResponseCommand)); + processInstreamResponse(inStreamResponseCommandBuffer[0], shouldClearCrcFromBuffer); + } + } - if(inStreamResponseCommand==DIR_RESPONSE){ - byte[] responseData = readBytes(1, inStreamResponseCommand); - if(responseData!=null){ - int directoryNameLength = responseData[0]; - byte[] bufferDirectoryName = new byte[directoryNameLength]; - bufferDirectoryName = readBytes(directoryNameLength, inStreamResponseCommand); - if(bufferDirectoryName!=null){ - String tempDirectory = new String(bufferDirectoryName); - mDirectoryName = tempDirectory; - printLogDataForDebugging("Directory Name = " + mDirectoryName); - } + /** process an in-stream response whose response command has already been read */ + protected void processInstreamResponse(byte inStreamResponseCommand, boolean shouldClearCrcFromBuffer) { + boolean responseWasParsed = true; + + consolePrintLn("In-stream received = " + btCommandToString(inStreamResponseCommand)); + + if(inStreamResponseCommand==DIR_RESPONSE){ + byte[] responseData = readBytes(1, inStreamResponseCommand); + if(responseData!=null){ + int directoryNameLength = responseData[0]; + byte[] bufferDirectoryName = new byte[directoryNameLength]; + bufferDirectoryName = readBytes(directoryNameLength, inStreamResponseCommand); + if(bufferDirectoryName!=null){ + String tempDirectory = new String(bufferDirectoryName); + mDirectoryName = tempDirectory; + printLogDataForDebugging("Directory Name = " + mDirectoryName); } } - else if(inStreamResponseCommand==STATUS_RESPONSE){ - int statusBytesToRead = 0; - if(isSupportedUSBPluggedInStatus()) { - statusBytesToRead = 2; - }else { - statusBytesToRead = 1; - } - byte[] responseData = readBytes(statusBytesToRead, inStreamResponseCommand); - if(responseData!=null){ - parseStatusByte(responseData); - - if(!isSupportedRtcStateInStatus()){ - if(!mIsSensing && !isInitialised()){ - writeRealTimeClock(); - } - } else { - //New case to make sure RTC is set if it hasn't been already - if(!isSDLogging() && (!isInitialised() || !mIsRtcSet)){ - writeRealTimeClock(); - } + } + else if(inStreamResponseCommand==STATUS_RESPONSE){ + int statusBytesToRead = 0; + if(isSupportedUSBPluggedInStatus()) { + statusBytesToRead = 2; + }else { + statusBytesToRead = 1; + } + byte[] responseData = readBytes(statusBytesToRead, inStreamResponseCommand); + if(responseData!=null){ + parseStatusByte(responseData); + + if(!isSupportedRtcStateInStatus()){ + if(!mIsSensing && !isInitialised()){ + writeRealTimeClock(); + } + } else { + //New case to make sure RTC is set if it hasn't been already + if(!isSDLogging() && (!isInitialised() || !mIsRtcSet)){ + writeRealTimeClock(); } - - eventLogAndStreamStatusChanged(mCurrentCommand); - } - } - else if(inStreamResponseCommand==VBATT_RESPONSE) { - byte[] responseData = readBytes(3, inStreamResponseCommand); - if(responseData!=null){ - ShimmerBattStatusDetails battStatusDetails = new ShimmerBattStatusDetails(((responseData[1]&0xFF)<<8)+(responseData[0]&0xFF),responseData[2]); - setBattStatusDetails(battStatusDetails); - printLogDataForDebugging("Battery Status:" - + "\tVoltage=" + battStatusDetails.getBattVoltageParsed() - + "\tCharging status=" + battStatusDetails.getChargingStatusParsed() - + "\tBatt %=" + battStatusDetails.getEstimatedChargePercentageParsed()); } + + eventLogAndStreamStatusChanged(mCurrentCommand); } - else - { - responseWasParsed = false; - } - - if (shouldClearCrcFromBuffer && mBtCommsCrcModeCurrent != BT_CRC_MODE.OFF && responseWasParsed) { - // The driver doesn't currently support checking of CRC bytes on any response - // other then data packets but we still need to clear them from the buffer if - // CRC is enabled. - clearCrcBytesFromBuffer(inStreamResponseCommand); + } + else if(inStreamResponseCommand==VBATT_RESPONSE) { + byte[] responseData = readBytes(3, inStreamResponseCommand); + if(responseData!=null){ + ShimmerBattStatusDetails battStatusDetails = new ShimmerBattStatusDetails(((responseData[1]&0xFF)<<8)+(responseData[0]&0xFF),responseData[2]); + setBattStatusDetails(battStatusDetails); + printLogDataForDebugging("Battery Status:" + + "\tVoltage=" + battStatusDetails.getBattVoltageParsed() + + "\tCharging status=" + battStatusDetails.getChargingStatusParsed() + + "\tBatt %=" + battStatusDetails.getEstimatedChargePercentageParsed()); } } + else + { + responseWasParsed = false; + } + + if (shouldClearCrcFromBuffer && mBtCommsCrcModeCurrent != BT_CRC_MODE.OFF && responseWasParsed) { + // The driver doesn't currently support checking of CRC bytes on any response + // other then data packets but we still need to clear them from the buffer if + // CRC is enabled. + clearCrcBytesFromBuffer(inStreamResponseCommand); + } } 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 new file mode 100644 index 000000000..1aba75222 --- /dev/null +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00030_StreamingInstreamResponseQueueTest.java @@ -0,0 +1,277 @@ +package com.shimmerresearch.bluetooth; + +import static org.junit.Assert.*; + +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Deque; + +import org.junit.After; +import org.junit.Test; + +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_CRC_MODE; +import com.shimmerresearch.comms.wiredProtocol.ShimmerCrc; +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-1262: while streaming, the branch of processPacket() for an in-stream + * response (FF 8A ...) completed the command in flight whatever the response + * was: it cancelled the ACK timer, cleared the wait flags, removed the head of + * the instruction queue and unlocked it. The firmware also sends a status + * response unprompted (dock, undock, the user button, a low-battery stop), so + * such a push removed a SET command that was still waiting for its ACK, or a + * queued instruction not yet sent, and the next ACK was matched to the wrong + * instruction. The protocol matches a response to the command in flight by + * its opcode (log-and-stream-common docs/SHIMMER3_BT_COMMUNICATION_PROTOCOL.md + * section 5.4, rule 2). + *
+ * 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.
+ */
+public class API_00030_StreamingInstreamResponseQueueTest {
+
+ /** 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