From c7e9547827ae66aa03de7f7141c164dc75a320e1 Mon Sep 17 00:00:00 2001 From: JongChern Date: Thu, 22 Oct 2026 13:38:43 +0800 Subject: [PATCH 1/2] DEV-1291 Take an in-stream ACK that comes with no data packet before it After STOP_LOGGING_ONLY_COMMAND the unit stops sending data packets for about 100 ms while it closes its SD file. A command sent in that pause is answered at once, so its ACK comes straight after the previous ACK, with no data packet before it. processPacket() only looks for an ACK after a data packet, so it threw the reply away a byte at a time. A queued command then timed out, and while streaming the timeout clears every queued instruction. The Java HIL found it on a Shimmer3 and a Shimmer3R (DEV-1269). This ports the driver's DEV-1291 fix. Shimmer overrides the streaming parser, so the driver's fix doesn't reach it. processWhileStreaming() now looks for an ACK at the start of the buffer first. It takes one only while a reply is due, and only when the bytes after it are that reply: - an in-stream reply (FF 8A, then the response opcode) to the queued GET command in flight, or to a read that readBattery() or readStatusLogAndStream() wrote straight to the socket. Those two are not queued, so Shimmer now keeps the response opcodes they await; - for the queued SET command in flight, a bare ACK whose link CRC checks out. So a 0xFF data byte the parser resyncs onto is not taken for one. An in-stream reply is parsed whole from the buffer by DEV-1149's parser, which now takes the ACK's position. Unlike the driver, Shimmer can send a read in the pause without a link CRC, because it doesn't queue the read. The ACK before the reply is then followed by another ACK rather than a packet, which processPacket() took for a parse error. It now treats it as the next frame. AAPI_00005 replays a logging stop, then a status reply, a battery reply, a queued GET's reply or a queued SET's bare ACK in the pause, in each link CRC mode that can send it and in reads of every size. Without the fix, all four fail. A fifth checks that a reply that is not due is not taken. Stacked on DEV-1149 (#122), whose in-stream parser this builds on. Co-Authored-By: Claude Opus 5.5 --- .../com/shimmerresearch/android/Shimmer.java | 118 ++++- ...AAPI_00005_StreamingAckAfterPauseTest.java | 448 ++++++++++++++++++ 2 files changed, 553 insertions(+), 13 deletions(-) create mode 100644 ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00005_StreamingAckAfterPauseTest.java diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java index 38a12235..473c9284 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java @@ -177,6 +177,7 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Set; import java.util.UUID; @@ -758,6 +759,14 @@ protected void clearSingleDataPacketFromBuffers(byte[] bufferTemp, int packetSiz */ private static final byte[] BARE_ACK_CRC = ShimmerCrc.shimmerUartCrcCalc(new byte[] {ACK_COMMAND_PROCESSED}, 1); + /** + * The response opcodes of the reads that readBattery() and + * readStatusLogAndStream() write straight to the socket while streaming, whose + * replies have not arrived yet. They are not queued, so nothing else says that + * a reply is due. + */ + private final List mUnqueuedRepliesAwaited = Collections.synchronizedList(new ArrayList()); + @Override protected void processPacket() { setIamAlive(true); @@ -816,7 +825,11 @@ else if(bufferTemp[0]==DATA_PACKET allBytes = mByteArrayOutputStream.toByteArray(); bufferTemp = new byte[getPacketSizeWithCrc()+3]; //check if the next byte is data packet System.arraycopy(allBytes,0,bufferTemp,0,bufferTemp.length); - if(bufferTemp[getPacketSizeWithCrc()+2]==DATA_PACKET){ + //With no link CRC, a bare ACK is followed by the next packet, or by the + //next ACK when the unit answers while its data packets are paused + //(DEV-1291). That ACK is left at the start of the buffer. + if(bufferTemp[getPacketSizeWithCrc()+2]==DATA_PACKET + || (bufferTemp[getPacketSizeWithCrc()+2]==ACK_COMMAND_PROCESSED && mBtCommsCrcModeCurrent==BT_CRC_MODE.OFF)){ //Firstly handle the data packet processDataPacket(bufferTemp); clearSingleDataPacketFromBuffers(bufferTemp, getPacketSizeWithCrc()+2); //clear an extra byte which is the ack @@ -828,7 +841,7 @@ else if(bufferTemp[0]==DATA_PACKET //this is for LogAndStream support: an in-stream response, the reply to a //GET command or the firmware's unsolicited status push else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTREAM_CMD_RESPONSE){ - processInstreamResponseWhileStreaming(); + processInstreamResponseWhileStreaming(getPacketSizeWithCrc()+1); } else { printLogDataForDebugging("Unknown parsing error while streaming"); @@ -928,11 +941,12 @@ protected void discardFirstBufferByte(){ } /** - * Handles an in-stream response that follows a data packet while streaming: + * Handles an in-stream response while streaming: * [data packet][ACK][INSTREAM_CMD_RESPONSE][response opcode][payload], then * the link CRC if one is on. It is the reply to GET_STATUS_COMMAND, * GET_VBATT_COMMAND or GET_DIR_COMMAND, or the status push the firmware - * sends unprompted. + * sends unprompted. While the unit's data packets are paused, a reply comes + * with no data packet before it (see processAckAtStartOfBuffer()). *

* processWhileStreaming() reads whatever has arrived, so the response can be * split across reads anywhere. Nothing is done until all of it, its CRC @@ -941,10 +955,13 @@ protected void discardFirstBufferByte(){ * exactly once. This holds in every streaming state: the driver's * processInstreamResponse() reads the response from the socket, which only * suits its own byte-at-a-time reads. + * + * @param ackIndex where the response's ACK is in the buffer: after the data + * packet, or 0 if there is none */ - private void processInstreamResponseWhileStreaming() { + private void processInstreamResponseWhileStreaming(int ackIndex) { byte[] allBytes = mByteArrayOutputStream.toByteArray(); - int responseCommandIndex = getPacketSizeWithCrc()+3; + int responseCommandIndex = ackIndex+2; if(allBytes.length<=responseCommandIndex){ return; //wait for the response opcode } @@ -1009,8 +1026,11 @@ else if(inStreamResponseCommand==STATUS_RESPONSE){ } //Then handle the data packet, and clear the packet, the response and its CRC - processDataPacket(allBytes); + if(ackIndex>0){ + processDataPacket(allBytes); + } clearSingleDataPacketFromBuffers(allBytes, frameLength); + mUnqueuedRepliesAwaited.remove(Byte.valueOf(inStreamResponseCommand)); //Last, complete the queued command in flight if this is its reply. While //streaming, an in-stream response is not handled elsewhere. @@ -1041,6 +1061,72 @@ private boolean isReplyToCommandInFlight(byte inStreamResponseCommand) { return mWaitForAck && commandDetails!=null && commandDetails.expectedResponse==inStreamResponseCommand; } + /** + * 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 a byte at a time. The + * reply to an unqueued read was lost; a queued command timed out, and the + * timeout dropped every queued instruction. + *

+ * It is taken only while a reply is due, and only when the bytes after it are + * that reply: the in-stream reply to the queued GET command in flight, or to a + * read that readBattery() or readStatusLogAndStream() wrote straight to the + * socket (FF 8A, then the response opcode), or for the queued SET command in + * flight, 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 + * here can't be told from a data byte, but then the previous ACK isn't + * processed until the stream resumes either, so no queued command goes out + * during the pause. An unqueued read can, and processPacket() leaves its + * reply here. + * + * @return true while the bytes at the start of the buffer are taken, or may + * still turn out to be such an ACK + */ + private boolean processAckAtStartOfBuffer() { + if(!mWaitForAck && mUnqueuedRepliesAwaited.isEmpty()){ + return false; + } + byte[] allBytes = mByteArrayOutputStream.toByteArray(); + if(allBytes.length==0 || allBytes[0]!=ACK_COMMAND_PROCESSED){ + return false; + } + if(allBytes.length==1){ + return true; //wait for what follows the ACK + } + + //An in-stream reply that is due + if(isSupportedInStreamCmds() && allBytes[1]==INSTREAM_CMD_RESPONSE){ + if(allBytes.length==2){ + return true; //wait for the response opcode + } + if(!isReplyToCommandInFlight(allBytes[2]) && !mUnqueuedRepliesAwaited.contains(allBytes[2])){ + return false; + } + printLogDataForDebugging("ACK RECEIVED IN STREAM, with no data packet before it"); + processInstreamResponseWhileStreaming(0); + return true; + } + + //A bare ACK to the SET command in flight + if(mWaitForAck && isKnownSetCommand(mCurrentCommand) && mBtCommsCrcModeCurrent!=BT_CRC_MODE.OFF){ + int numCrcBytes = mBtCommsCrcModeCurrent.getNumCrcBytes(); + for(int i=1;i=getPacketSizeWithCrc()+2){ // +2 because there are two acks + while(true){ int size = mByteArrayOutputStream.size(); - processPacket(); - int newSize = mByteArrayOutputStream.size(); - if (size == newSize){ + if(!processAckAtStartOfBuffer()){ + //If there is a full packet and the subsequent sequence number of following packet + if(size getSensorIdsSet() { @Override public void readBattery() { if (isStreaming()) { + mUnqueuedRepliesAwaited.add(VBATT_RESPONSE); writeBytes(new byte[]{GET_VBATT_COMMAND}); } else { super.readBattery(); @@ -1913,6 +2004,7 @@ public void readBattery() { public void readStatusLogAndStream() { if (this.isSupportedInStreamCmds()) { if (isStreaming()) { + mUnqueuedRepliesAwaited.add(STATUS_RESPONSE); writeBytes(new byte[]{GET_STATUS_COMMAND}); } else { super.readStatusLogAndStream(); diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00005_StreamingAckAfterPauseTest.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00005_StreamingAckAfterPauseTest.java new file mode 100644 index 00000000..05be642c --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00005_StreamingAckAfterPauseTest.java @@ -0,0 +1,448 @@ +package com.shimmerresearch.bluetooth; + +import static org.junit.Assert.*; + +import android.content.Context; +import android.os.Handler; + +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Deque; +import java.util.List; + +import org.junit.After; +import org.junit.Test; + +import com.shimmerresearch.android.Shimmer; +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_CRC_MODE; +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE; +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.driverUtilities.UtilShimmer; + +/** + * DEV-1291: after STOP_LOGGING_ONLY_COMMAND the unit stops sending data packets + * for about 100 ms while it closes its SD file. A command sent in that pause is + * answered at once, so its ACK comes straight after the previous ACK, with no + * data packet before it. Shimmer.processPacket() only looks for an ACK after a + * data packet, so it threw the reply away a byte at a time. For a queued + * command, the ACK timer then cleared the instruction queue. Both a Shimmer3 + * and a Shimmer3R do it (found by the Java HIL, DEV-1269). + *

+ * The driver fixed this in ShimmerBluetooth (whose + * API_00032_StreamingAckAfterPauseTest this ports), but Shimmer overrides that + * parser with its own. Shimmer also writes readBattery() and + * readStatusLogAndStream() straight to the socket while streaming rather than + * queue them, so they are sent at once, in the pause too, and with or without a + * link CRC. + *

+ * Each scenario is replayed with the bytes arriving in reads of every size, from + * one byte to all of them at once. No hardware: the parser reads from an + * in-memory byte queue. It lives in this package because the parser's state is + * protected or package-private in ShimmerBluetooth. + */ +public class AAPI_00005_StreamingAckAfterPauseTest { + + /** Bytes after the DATA_PACKET byte: a 3-byte timestamp and three 2-byte channels */ + private static final int PACKET_SIZE = 9; + + /** As many bytes as any scenario here sends in one go, so the largest read takes everything at once */ + private static final int MAX_READ_SIZE = 48; + + /** The battery ADC value in vbattReply() */ + private static final int VBATT_ADC = 0x0B24; + + /** Status byte 0: docked, an SD card in, and streaming with the clock set */ + private static final byte STATUS_DOCKED_AND_STREAMING = 0x20|0x10|0x04|0x02|0x01; + + /** The GET_DIR_COMMAND reply: the SD file path without its file number */ + private static final String DIRECTORY_NAME = "Trial/Shim_A1B2-"; + + /** The link CRC modes on which a queued command can be sent in the pause */ + private static final BT_CRC_MODE[] CRC_ON = {BT_CRC_MODE.ONE_BYTE_CRC, BT_CRC_MODE.TWO_BYTE_CRC}; + + /** A Shimmer whose Bluetooth link is in memory */ + private static class QueueShimmer extends Shimmer { + final Deque rx = new ArrayDeque(); + final ByteArrayOutputStream tx = new ByteArrayOutputStream(); + int bytesSkipped = 0; + + QueueShimmer() { + super((Handler) null, (Context) null); + // A device has been initialised by the time it streams. Before then, a + // status response queues SET_RWC_COMMAND to set the clock. + mIsInitialised = true; + } + + void queue(byte[] bytes) { + for(byte b:bytes){ + rx.add(b); + } + } + + /** What the IO thread does on each pass while streaming */ + void ioThreadPass() { + processWhileStreaming(); + } + + @Override + protected int availableBytes() { + return rx.size(); + } + + @Override + protected byte[] readBytes(int numberofBytes) { + // Shimmer's would block for bytes that have not arrived, and none will + assertTrue("read " + numberofBytes + " bytes but only " + rx.size() + " had arrived", numberofBytes<=rx.size()); + byte[] bytes = new byte[numberofBytes]; + for(int i=0;i mDevices = new ArrayList(); + + @After + public void stopAckTimers() { + for(QueueShimmer device:mDevices){ + device.stopTimerCheckForAckOrResp(); + } + } + + /** + * Shimmer reads whatever has arrived, so a frame can be split across reads + * anywhere. Runs the scenario with each of the link CRC modes, and the bytes + * arriving one at a time, two at a time and so on, up to all of them at once. + */ + private static void forEachCrcModeAndReadSize(BT_CRC_MODE[] crcModes, Scenario scenario) throws Exception { + for(BT_CRC_MODE crcMode:crcModes){ + for(int readSize=1;readSize<=MAX_READ_SIZE;readSize++){ + String run = crcMode + ", bytes arriving " + readSize + " at a time: "; + try { + scenario.run(crcMode, readSize); + } + catch(AssertionError e){ + throw new AssertionError(run + e.getMessage(), e); + } + catch(RuntimeException e){ + throw new AssertionError(run + e, e); + } + } + } + } + + /** A Shimmer3R part way through streaming PACKET_SIZE-byte packets */ + private QueueShimmer streamingShimmer3r(BT_CRC_MODE crcMode) { + QueueShimmer device = new QueueShimmer(); + mDevices.add(device); + device.setShimmerVersionObjectAndCreateSensorMap(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 7)); + device.setPacketSize(PACKET_SIZE); + device.setCurrentBtCommsCrcMode(crcMode); + device.setBluetoothRadioState(BT_STATE.STREAMING); + // Queue each parsed packet rather than build an ObjectCluster from it + device.mUseProcessingThread = true; + return device; + } + + /** What the IO thread does when it sends the next queued instruction while streaming */ + private static void sendNextInstruction(QueueShimmer device) { + device.mCurrentCommand = device.getListofInstructions().get(0)[0]; + device.setInstructionStackLock(true); + device.mWaitForAck = true; + device.startTimerCheckForAckOrResp(2); + } + + /** Hands the frames to the streaming parser readSize bytes at a time, one IO thread pass per read */ + private static void receive(QueueShimmer device, int readSize, byte[]... frames) { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + for(byte[] frame:frames){ + bytes.write(frame, 0, frame.length); + } + byte[] all = bytes.toByteArray(); + assertTrue("the largest read takes all " + all.length + " bytes at once", all.length<=MAX_READ_SIZE); + for(int start=0;start expected = new ArrayList(); + for(byte[] payload:expectedPayloads){ + expected.add(hex(payload)); + } + List parsed = new ArrayList(); + // Package-private, so Shimmer does not inherit it + ShimmerBluetooth parser = device; + for(RawBytePacketWithPCTimeStamp packet:parser.mABQPacketByeArray){ + parsed.add(hex(packet.mDataArray)); + } + assertEquals("every good data packet is parsed, once and in order", expected, parsed); + } + + /** The commands in the instruction queue, in order */ + private static void assertQueued(QueueShimmer device, byte... expectedCommands) { + List expected = new ArrayList(); + for(byte command:expectedCommands){ + expected.add(ShimmerBluetooth.btCommandToString(command)); + } + List queued = new ArrayList(); + for(byte[] instruction:device.getListofInstructions()){ + queued.add(ShimmerBluetooth.btCommandToString(instruction[0])); + } + assertEquals("the instruction queue", expected, queued); + } + + private static void assertQueuedFirst(QueueShimmer device, byte command) { + assertEquals(ShimmerBluetooth.btCommandToString(command), + ShimmerBluetooth.btCommandToString(device.getListofInstructions().get(0)[0])); + } + + /** The command was sent and nothing has answered it yet */ + private static void assertAwaitingReply(QueueShimmer device, byte command) { + assertEquals("the command in flight", ShimmerBluetooth.btCommandToString(command), + ShimmerBluetooth.btCommandToString(device.mCurrentCommand)); + assertTrue("it still awaits its reply", device.mWaitForAck); + assertTrue("so the next instruction cannot be sent", device.isInstructionStackLock()); + assertNotNull("and its ACK timer still runs", device.mTimerCheckForAckOrResp); + } + + /** The command in flight was answered, and the next can be sent */ + private static void assertAnswered(QueueShimmer device) { + assertFalse("the reply is taken", device.mWaitForAck); + assertFalse("the next instruction can be sent", device.isInstructionStackLock()); + assertNull("the ACK timer is cancelled, so it cannot clear the instruction queue", device.mTimerCheckForAckOrResp); + } + + /** What is left in the parser's buffer, each byte with its PC timestamp */ + private static void assertBuffered(QueueShimmer device, byte... expected) { + assertEquals(hex(expected), hex(device.mByteArrayOutputStream.toByteArray())); + assertEquals("one PC timestamp per buffered byte", expected.length, device.mListofPCTimeStamps.size()); + } + + /** The parser never lost its place, so every frame was consumed whole and once */ + private static void assertNoResync(QueueShimmer device) { + assertEquals("bytes skipped to find the next frame", 0, device.bytesSkipped); + } + + /** + * readStatusLogAndStream() in the pause. Its reply was thrown away, and with + * no link CRC the ACK of STOP_LOGGING_ONLY_COMMAND before it too. + */ + @Test + public void statusReplyInThePause() throws Exception { + forEachCrcModeAndReadSize(BT_CRC_MODE.values(), (crcMode, readSize) -> { + QueueShimmer device = streamingShimmer3r(crcMode); + device.stopSDLogging(); + stopLoggingAndReceiveItsAck(device, crcMode, readSize); + + device.readStatusLogAndStream(); + assertEquals("while streaming, Shimmer writes the command straight out rather than queue it", + hex(new byte[] {ShimmerObject.GET_STATUS_COMMAND}), hex(device.tx.toByteArray())); + receive(device, readSize, statusReply(crcMode), dataPacket(payload(2), crcMode), + dataPacket(payload(3), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertAnswered(device); + assertQueued(device); + assertTrue("the status reply is decoded", device.isDocked()); + + assertParsed(device, payload(1), payload(2), payload(3)); + assertBuffered(device, ShimmerObject.DATA_PACKET); + assertNoResync(device); + }); + } + + /** readBattery() in the pause */ + @Test + public void batteryReplyInThePause() throws Exception { + forEachCrcModeAndReadSize(BT_CRC_MODE.values(), (crcMode, readSize) -> { + QueueShimmer device = streamingShimmer3r(crcMode); + device.stopSDLogging(); + stopLoggingAndReceiveItsAck(device, crcMode, readSize); + + device.readBattery(); + receive(device, readSize, vbattReply(crcMode), dataPacket(payload(2), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertAnswered(device); + assertQueued(device); + assertEquals(VBATT_ADC, device.getBattStatusDetails().getBattAdcValue(), 0); + + assertParsed(device, payload(1), payload(2)); + assertBuffered(device, ShimmerObject.DATA_PACKET); + assertNoResync(device); + }); + } + + /** + * A queued SET command sent in the pause: its bare ACK must complete it, or + * the ACK timer clears the queue behind it. Only with a link CRC: without + * one, the ACK before it isn't processed until the stream resumes, so it is + * not sent in the pause. + */ + @Test + public void bareAckInThePause() throws Exception { + forEachCrcModeAndReadSize(CRC_ON, (crcMode, readSize) -> { + QueueShimmer device = streamingShimmer3r(crcMode); + device.stopSDLogging(); + device.startSDLogging(); + assertQueued(device, ShimmerObject.STOP_LOGGING_ONLY_COMMAND, ShimmerObject.SET_RWC_COMMAND, ShimmerObject.START_LOGGING_ONLY_COMMAND); + stopLoggingAndReceiveItsAck(device, crcMode, readSize); + + sendNextInstruction(device); + assertEquals(ShimmerObject.SET_RWC_COMMAND, device.mCurrentCommand); + receive(device, readSize, bareAck(crcMode), dataPacket(payload(2), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertAnswered(device); + assertQueued(device, ShimmerObject.START_LOGGING_ONLY_COMMAND); + + assertParsed(device, payload(1), payload(2)); + assertBuffered(device, ShimmerObject.DATA_PACKET); + assertNoResync(device); + }); + } + + /** A queued GET command sent in the pause: its in-stream reply must complete it */ + @Test + public void queuedCommandsReplyInThePause() throws Exception { + forEachCrcModeAndReadSize(CRC_ON, (crcMode, readSize) -> { + QueueShimmer device = streamingShimmer3r(crcMode); + device.stopSDLogging(); + device.readDirectoryName(); + stopLoggingAndReceiveItsAck(device, crcMode, readSize); + + sendNextInstruction(device); + assertEquals(ShimmerObject.GET_DIR_COMMAND, device.mCurrentCommand); + receive(device, readSize, dirReply(crcMode), dataPacket(payload(2), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertEquals(DIRECTORY_NAME, device.getDirectoryName()); + assertAnswered(device); + assertQueued(device); + + assertParsed(device, payload(1), payload(2)); + assertBuffered(device, ShimmerObject.DATA_PACKET); + assertNoResync(device); + }); + } + + /** + * An ACK at the start of the buffer is taken only for a reply that is due. A + * status push there completes neither GET_DIR_COMMAND, queued and in flight, + * nor the battery read that readBattery() wrote straight out. + */ + @Test + public void aReplyThatIsNotDueIsNotTaken() throws Exception { + forEachCrcModeAndReadSize(CRC_ON, (crcMode, readSize) -> { + QueueShimmer device = streamingShimmer3r(crcMode); + device.stopSDLogging(); + device.readDirectoryName(); + stopLoggingAndReceiveItsAck(device, crcMode, readSize); + sendNextInstruction(device); + device.readBattery(); + + receive(device, readSize, statusReply(crcMode), dataPacket(payload(2), crcMode), dataPacket(payload(3), crcMode)); + assertAwaitingReply(device, ShimmerObject.GET_DIR_COMMAND); + assertQueued(device, ShimmerObject.GET_DIR_COMMAND); + assertFalse("the status push is not decoded", device.isDocked()); + assertEquals("nor taken for the battery reply", 0, device.getBattStatusDetails().getBattAdcValue(), 0); + }); + } +} From 62a24736598aa858de79cc2d2cfc2ed5f36c7a68 Mon Sep 17 00:00:00 2001 From: JongChern Date: Thu, 22 Oct 2026 14:10:09 +0800 Subject: [PATCH 2/2] DEV-1291 Take a bare ACK straight after an unqueued read's reply, with no link CRC The Android HIL found it on a Shimmer3 with the link CRC off. readBattery() and readStatusLogAndStream() write their reads straight to the socket, so a queued command's ACK can come straight after a read's reply, with no data packet between them. With a link CRC on, the ACK's own CRC shows what it is, and processAckAtStartOfBuffer() already took it. With none, it threw the ACK away; STOP_LOGGING_ONLY_COMMAND then timed out, and the timeout cleared the instruction queue. Without a CRC, a bare ACK at the start of the buffer is now taken only for the queued SET command in flight. The bytes after it must also be a data packet and the start of the next frame, the same check processPacket() makes of a packet. processWhileStreaming() no longer logs "Leaving while loop" on every pass while it waits for the rest of such an ACK's frame. AAPI_00005 adds the case in every link CRC mode; without this change it fails with the CRC off. Co-Authored-By: Claude Opus 5.5 --- .../com/shimmerresearch/android/Shimmer.java | 60 +++++++++++++------ ...AAPI_00005_StreamingAckAfterPauseTest.java | 26 ++++++++ 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java index 473c9284..05f721ed 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java @@ -1070,16 +1070,20 @@ private boolean isReplyToCommandInFlight(byte inStreamResponseCommand) { * reply to an unqueued read was lost; a queued command timed out, and the * timeout dropped every queued instruction. *

+ * Android meets one outside the pause too: readBattery() and + * readStatusLogAndStream() write their reads straight to the socket, so the + * ACK of a queued command can come straight after a read's reply, and a read + * can go out in the pause without a link CRC as well (processPacket() then + * leaves its reply here). + *

* It is taken only while a reply is due, and only when the bytes after it are * that reply: the in-stream reply to the queued GET command in flight, or to a - * read that readBattery() or readStatusLogAndStream() wrote straight to the - * socket (FF 8A, then the response opcode), or for the queued SET command in - * flight, 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 - * here can't be told from a data byte, but then the previous ACK isn't - * processed until the stream resumes either, so no queued command goes out - * during the pause. An unqueued read can, and processPacket() leaves its - * reply here. + * read written straight to the socket (FF 8A, then the response opcode), or + * for the queued SET command in flight, a bare ACK. With a link CRC, a bare + * ACK's own CRC must check out; without one, it must be followed by a data + * packet and then the start of the next frame, as processPacket() checks a + * packet. A 0xFF data byte the parser resyncs onto is therefore not taken for + * one. * * @return true while the bytes at the start of the buffer are taken, or may * still turn out to be such an ACK @@ -1110,8 +1114,11 @@ private boolean processAckAtStartOfBuffer() { } //A bare ACK to the SET command in flight - if(mWaitForAck && isKnownSetCommand(mCurrentCommand) && mBtCommsCrcModeCurrent!=BT_CRC_MODE.OFF){ - int numCrcBytes = mBtCommsCrcModeCurrent.getNumCrcBytes(); + if(!mWaitForAck || !isKnownSetCommand(mCurrentCommand)){ + return false; + } + int numCrcBytes = mBtCommsCrcModeCurrent.getNumCrcBytes(); + if(mBtCommsCrcModeCurrent!=BT_CRC_MODE.OFF){ for(int i=1;i { + QueueShimmer device = streamingShimmer3r(crcMode); + device.stopSDLogging(); + sendNextInstruction(device); + device.readBattery(); + + receive(device, readSize, dataPacket(payload(1), crcMode), vbattReply(crcMode), bareAck(crcMode), + dataPacket(payload(2), crcMode), dataPacket(payload(3), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertAnswered(device); + assertQueued(device); + assertEquals(VBATT_ADC, device.getBattStatusDetails().getBattAdcValue(), 0); + + assertParsed(device, payload(1), payload(2), payload(3)); + assertBuffered(device, ShimmerObject.DATA_PACKET); + assertNoResync(device); + }); + } + /** * An ACK at the start of the buffer is taken only for a reply that is due. A * status push there completes neither GET_DIR_COMMAND, queued and in flight,