From 11a790d176d982803b306dc73743566dc02db789 Mon Sep 17 00:00:00 2001 From: Mark Nolan Date: Tue, 10 Nov 2026 15:04:27 +0000 Subject: [PATCH] DEV-1431: a log start refused mid-stream ends at once Firmware that cannot start a log (docked or on USB power, no SD card or a bad one, logging turned off in its configuration) is to NACK START_LOGGING_ONLY_COMMAND, including in the middle of a stream, the NACK coming between two data packets with its own link CRC (C5, or C5 56). Shimmer.processPacket() had no NACK branch: it threw the NACK away a byte at a time with the packet before it, and 2 s later the driver's ACK timer took the device for logging. Ported from the driver's processPacket() (Shimmer-Java-Android-API #345, DEV-1431, on top of #293's NACK handling): a data packet followed by a NACK waits for the NACK's CRC and checks it, as a bare ACK's is; exactly the packet, the NACK and its CRC are consumed; a packet whose CRC fails is dropped without losing the NACK; a 0xFE whose CRC does not match is not a NACK; and the command in flight is failed by the driver's processNackFromCommand(), which leaves a refused log start not logging and calls eventLogStartRefused (a toast here, by default). With no command in flight a NACK is ignored. An idle refusal needs nothing here: it goes through the driver's own parser. Needs a driver release that has #293 and #345: processNackFromCommand() and NACK_COMMAND_PROCESSED are not in 0.11.12_beta, so this does not build against the current pin. Test: AAPI_00009_StreamingLogStartRefusedTest, a port of the driver's API_00037, every scenario at every read size from 1 to 48 bytes. Run locally against a build of #345's driver: the library's unit tests 39/39, and assembleDebug builds the library and every example. Against master's Shimmer.java 6 of the 7 new tests fail. Co-Authored-By: Claude Opus 5.5 --- .../com/shimmerresearch/android/Shimmer.java | 63 ++++ ...PI_00009_StreamingLogStartRefusedTest.java | 328 ++++++++++++++++++ 2 files changed, 391 insertions(+) create mode 100644 ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00009_StreamingLogStartRefusedTest.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 8d88278..436e9da 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java @@ -857,6 +857,47 @@ else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTR */ } + //Data packet followed by a NACK: a command refused while streaming, e.g. a SET + //blocked because the device is sensing, or a log start it cannot carry out + //(DEV-1431). Ported from the driver's processPacket(): this parser had no NACK + //branch, so the NACK was thrown away with the packet before it, and the ACK + //timer then took START_LOGGING_ONLY_COMMAND for started. + else if(bufferTemp[0]==DATA_PACKET + && bufferTemp[getPacketSizeWithCrc()+1]==NACK_COMMAND_PROCESSED){ + + //With a link CRC on, the NACK is followed by its own CRC, as a bare ACK + //is, rather than by the next packet + if(!isBareNackCrcSoFar(allBytes)){ + printLogDataForDebugging("0xFE after a data packet is not a NACK: its CRC does not match"); + discardFirstBufferByte(); //throw the first byte away + return; + } + int bareNackEnd = getPacketSizeWithCrc()+2+mBtCommsCrcModeCurrent.getNumCrcBytes(); + if(allBytes.length + * Shimmer.processPacket() had no branch for a NACK: it threw the NACK away a + * byte at a time with the packet in front of it, and 2 s later the driver's + * ACK timer took the device for logging. Now the packet is kept, the NACK's + * CRC is checked and consumed, and the command is failed by the driver's + * processNackFromCommand(), as the driver's own parser does + * (Shimmer-Java-Android-API API_00037_StreamingLogStartRefusedTest, which this + * ports): the device is not taken to be logging, the stream goes on, and + * eventLogStartRefused() is called. + *

+ * Shimmer reads whatever has arrived, so 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. + */ +public class AAPI_00009_StreamingLogStartRefusedTest { + + /** Bytes after the DATA_PACKET byte: a 3-byte timestamp and three 2-byte channels */ + private static final int PACKET_SIZE = 9; + + /** More than any scenario here sends in one go, so the largest read takes everything at once */ + private static final int MAX_READ_SIZE = 48; + + /** A Shimmer whose Bluetooth link is in memory, recording the refusals it is told of */ + private static class QueueShimmer extends Shimmer { + final Deque rx = new ArrayDeque(); + final List logStartsRefused = Collections.synchronizedList(new ArrayList()); + final List commandsRefused = Collections.synchronizedList(new ArrayList()); + int bytesSkipped = 0; + + QueueShimmer() { + super((Handler) null, (Context) null); + } + + void queue(byte[] bytes) { + for(byte b:bytes){ + rx.add(b); + } + } + + /** What the IO thread does on each pass while streaming (protected in Shimmer's package) */ + 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(); + } + } + + private static void forEveryReadSize(Scenario scenario) throws Exception { + for(int readSize=1;readSize<=MAX_READ_SIZE;readSize++){ + try { + scenario.run(readSize); + } + catch(AssertionError e){ + throw new AssertionError("bytes arriving " + readSize + " at a time: " + e.getMessage(), e); + } + } + } + + /** A Shimmer3R on LogAndStream v1.01.025 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, 25)); + device.setPacketSize(PACKET_SIZE); + device.setCurrentBtCommsCrcMode(crcMode); + device.setIsStreaming(true); + device.setBluetoothRadioState(BT_STATE.STREAMING); + // Queue each parsed packet rather than build an ObjectCluster from it + device.mUseProcessingThread = true; + return device; + } + + /** startSDLogging() while streaming, and the IO thread sending its START_LOGGING_ONLY */ + private static void startSdLoggingWhileStreaming(QueueShimmer device) { + device.startSDLogging(); + assertEquals("only START_LOGGING_ONLY_COMMAND is queued", 1, device.getListofInstructions().size()); + device.mCurrentCommand = device.getListofInstructions().get(0)[0]; + assertEquals(ShimmerObject.START_LOGGING_ONLY_COMMAND, device.mCurrentCommand); + 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(); + 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); + } + + /** 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 start retired as refused, the device not logging and still streaming */ + private static void assertRefused(QueueShimmer device) { + assertFalse("the NACK is taken", device.mWaitForAck); + assertFalse("the next instruction can be sent", device.isInstructionStackLock()); + assertNull("the ACK timer is cancelled, so its special case cannot take the device for logging", + device.mTimerCheckForAckOrResp); + assertTrue("the refused start is retired", device.getListofInstructions().isEmpty()); + assertFalse("the device is not taken to be logging", device.isSDLogging()); + assertTrue("the stream goes on", device.isStreaming()); + assertEquals(BT_STATE.STREAMING, device.getBluetoothRadioState()); + assertEquals("eventLogStartRefused, once", Collections.singletonList(ShimmerObject.START_LOGGING_ONLY_COMMAND), + device.logStartsRefused); + assertEquals("eventCommandRefused, once", Collections.singletonList(ShimmerObject.START_LOGGING_ONLY_COMMAND), + device.commandsRefused); + } + + private void refusedBetweenTwoPackets(BT_CRC_MODE crcMode, int readSize) { + QueueShimmer device = streamingShimmer3r(crcMode); + startSdLoggingWhileStreaming(device); + receive(device, readSize, dataPacket(payload(1), crcMode), bareNack(crcMode), + dataPacket(payload(2), crcMode), dataPacket(payload(3), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertRefused(device); + assertParsed(device, payload(1), payload(2), payload(3)); + assertBuffered(device, ShimmerObject.DATA_PACKET); + assertEquals("bytes skipped to find the next frame", 0, device.bytesSkipped); + } + + @Test + public void aLogStartRefusedMidStreamWithoutACrc() throws Exception { + forEveryReadSize(readSize -> refusedBetweenTwoPackets(BT_CRC_MODE.OFF, readSize)); + } + + @Test + public void aLogStartRefusedMidStreamWithAOneByteCrc() throws Exception { + forEveryReadSize(readSize -> refusedBetweenTwoPackets(BT_CRC_MODE.ONE_BYTE_CRC, readSize)); + } + + @Test + public void aLogStartRefusedMidStreamWithATwoByteCrc() throws Exception { + forEveryReadSize(readSize -> refusedBetweenTwoPackets(BT_CRC_MODE.TWO_BYTE_CRC, readSize)); + } + + /** + * The NACK's own CRC shows it is a NACK, so a corrupt data packet ahead of it + * is dropped without losing the refusal + */ + private void refusedAfterACorruptPacket(BT_CRC_MODE crcMode, int indexToCorrupt, int readSize) { + QueueShimmer device = streamingShimmer3r(crcMode); + startSdLoggingWhileStreaming(device); + byte[] corrupt = dataPacket(payload(1), crcMode); + corrupt[indexToCorrupt] ^= 0x01; + receive(device, readSize, corrupt, bareNack(crcMode), dataPacket(payload(2), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertRefused(device); + assertParsed(device, payload(2)); + assertBuffered(device, ShimmerObject.DATA_PACKET); + } + + @Test + public void aRefusalWithAOneByteCrcAfterACorruptPacket() throws Exception { + forEveryReadSize(readSize -> refusedAfterACorruptPacket(BT_CRC_MODE.ONE_BYTE_CRC, 1, readSize)); + } + + /** Only the packet CRC's second byte is wrong, which is checked separately */ + @Test + public void aRefusalWithATwoByteCrcAfterACorruptPacket() throws Exception { + forEveryReadSize(readSize -> refusedAfterACorruptPacket(BT_CRC_MODE.TWO_BYTE_CRC, 1+PACKET_SIZE+1, readSize)); + } + + /** + * A 0xFE after a packet whose next bytes are not a NACK's CRC is not taken + * for a refusal: the command still waits for its reply + */ + @Test + public void aFeWhoseCrcDoesNotMatchIsNotARefusal() throws Exception { + forEveryReadSize(readSize -> { + BT_CRC_MODE crcMode = BT_CRC_MODE.TWO_BYTE_CRC; + QueueShimmer device = streamingShimmer3r(crcMode); + startSdLoggingWhileStreaming(device); + receive(device, readSize, dataPacket(payload(1), crcMode), new byte[] {ShimmerObject.NACK_COMMAND_PROCESSED, 0x11, 0x22}, + dataPacket(payload(2), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertTrue("the start still waits for its reply", device.mWaitForAck); + assertTrue("no refusal", device.logStartsRefused.isEmpty() && device.commandsRefused.isEmpty()); + }); + } + + /** A NACK with no command waiting for a reply has nothing to refuse, and the stream goes on */ + @Test + public void aNackWithNoCommandInFlightIsIgnored() throws Exception { + forEveryReadSize(readSize -> { + BT_CRC_MODE crcMode = BT_CRC_MODE.ONE_BYTE_CRC; + QueueShimmer device = streamingShimmer3r(crcMode); + receive(device, readSize, dataPacket(payload(1), crcMode), bareNack(crcMode), + dataPacket(payload(2), crcMode), new byte[] {ShimmerObject.DATA_PACKET}); + assertTrue("no refusal", device.logStartsRefused.isEmpty() && device.commandsRefused.isEmpty()); + assertTrue(device.isStreaming()); + assertParsed(device, payload(1), payload(2)); + assertBuffered(device, ShimmerObject.DATA_PACKET); + }); + } +}