From 12be52c6bf986101af68ca5e5219f8dc71644fcb Mon Sep 17 00:00:00 2001 From: JongChern Date: Wed, 21 Oct 2026 12:52:16 +0800 Subject: [PATCH] DEV-1262 Complete the command in flight only on its own in-stream reply While streaming, the branch of processPacket() for an in-stream response (FF 8A ...) cancelled the ACK timer, cleared the wait flags, removed the head of the instruction queue and unlocked it, whatever the response was. 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 still waiting for its ACK, or a queued instruction not yet sent, and the next ACK was matched to the wrong instruction: for startSDLogging(), processAckFromSetCommand(SET_RWC) against START_LOGGING_ONLY's one-byte instruction. The branch now reads the response opcode first and completes the command in flight only if the opcode is that command's expected response (mBtGetCommandMap), as the protocol says (log-and-stream-common docs/SHIMMER3_BT_COMMUNICATION_PROTOCOL.md section 5.4, rule 2). Any other response is still processed, but leaves the queue, the wait flags and the ACK timer alone. processInstreamResponse(boolean) still reads the opcode itself and hands it to the new processInstreamResponse(byte, boolean), which holds the unchanged parsing, so overrides and other callers keep working. API_00030 (9 tests, CRC off/1/2) covers a status push while a SET command awaits its ACK, while a GET command awaits its reply, and with nothing in flight. All 9 fail without the fix. ShimmerDriver and ShimmerDriverPC test suites pass. The Android half is ShimmerResearch/ShimmerAndroidAPI#122 (DEV-1149). Co-Authored-By: Claude Opus 5.5 --- .../bluetooth/ShimmerBluetooth.java | 172 ++++++----- ...30_StreamingInstreamResponseQueueTest.java | 277 ++++++++++++++++++ 2 files changed, 378 insertions(+), 71 deletions(-) create mode 100644 ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00030_StreamingInstreamResponseQueueTest.java 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 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()