From 9bb3d5da80ffa153fe6b47f930179481a25f7e91 Mon Sep 17 00:00:00 2001 From: JongChern Date: Thu, 22 Oct 2026 13:06:04 +0800 Subject: [PATCH] DEV-1275 Report a Shimmer ready only once its connect sequence has ended The connect sequence reads the unit's status part way through. On that reply, ShimmerPC.eventLogAndStreamStatusChanged() moved the state from CONNECTING to CONNECTED, and ShimmerDeviceCallbackAdapter marks every CONNECTED as initialised. So an app waiting for CONNECTED and initialised was told the unit was ready while the battery read, the calibration dump and the inquiry were still queued. Whatever the app did next went out in among that sequence: - streaming started part way through it, and a reply from the connect sequence landed in the stream, which then went silent; - a configure cut the calibration dump short, which left a Shimmer3's calibrated accel NaN in every sample (7 of 9 runs on the HIL). The sequence's own end, inquiryDone() and then isReadyForStreaming(), already moves CONNECTING to CONNECTED and sends NOTIFICATION_SHIMMER_FULLY_INITIALIZED. A status reply now leaves CONNECTING to it. The Shimmer3R simulator answered INQUIRY with the Shimmer3's 8-byte settings, which never parse as a Shimmer3R's 11. Its tests had only ever passed on the early CONNECTED, before the inquiry timed out. It now answers with settings that match its own InfoMem. API_00031 connects to the Shimmer3 simulator, whose sequence reads the status before the inquiry. It checks that CONNECTED comes only after the sequence's end, with nothing but the RTC write still queued. It fails without the fix. Co-Authored-By: Claude Opus 5.5 --- .../shimmerresearch/pcDriver/ShimmerPC.java | 9 +- ..._ConnectReadyAfterConnectSequenceTest.java | 107 ++++++++++++++++++ .../ByteCommunicationSimulatorS3R.java | 21 +++- 3 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_00031_ConnectReadyAfterConnectSequenceTest.java diff --git a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java index 7c5ee004d..bd30f78ad 100644 --- a/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java +++ b/ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java @@ -900,8 +900,13 @@ else if(isSDLogging()){ } else{ // if(!isStreaming() && !isSDLogging() && isConnected()){ - if(!mIsStreaming && !isSDLogging() && isConnected() && mBluetoothRadioState!=BT_STATE.CONNECTED){ - setBluetoothRadioState(BT_STATE.CONNECTED); + // While connecting, the connect sequence's own end (inquiryDone(), then + // isReadyForStreaming()) moves CONNECTING to CONNECTED. The sequence reads the + // status part way through, and if that reply did it, the app would be told the + // unit is ready while the rest of the sequence is still queued (DEV-1275). + if(!mIsStreaming && !isSDLogging() && isConnected() + && mBluetoothRadioState!=BT_STATE.CONNECTED && mBluetoothRadioState!=BT_STATE.CONNECTING){ + setBluetoothRadioState(BT_STATE.CONNECTED); } // if(getBTState() == BT_STATE.INITIALISED){ // diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_00031_ConnectReadyAfterConnectSequenceTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_00031_ConnectReadyAfterConnectSequenceTest.java new file mode 100644 index 000000000..7bba6aab7 --- /dev/null +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/API_00031_ConnectReadyAfterConnectSequenceTest.java @@ -0,0 +1,107 @@ +package com.shimmerresearch.shimmer3.communication; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.shimmerresearch.bluetooth.ShimmerBluetooth; +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE; +import com.shimmerresearch.driver.ShimmerObject; +import com.shimmerresearch.pcDriver.ShimmerPC; + +/** + * DEV-1275: the connect sequence reads the unit's status part way through, and + * ShimmerPC.eventLogAndStreamStatusChanged() moved CONNECTING to CONNECTED on + * that reply. ShimmerDeviceCallbackAdapter marks every CONNECTED initialised, + * so an app waiting for CONNECTED and initialised was told the unit was ready + * while the rest of the sequence (battery, calibration dump, inquiry) was still + * queued. Whatever it did next went out in among that sequence: streaming + * started part way through it, and a configure cut the calibration dump short, + * which left a Shimmer3's accel NaN. The sequence's own end, inquiryDone() and + * then isReadyForStreaming(), is what moves CONNECTING to CONNECTED. + *

+ * No hardware: ByteCommunicationSimulatorS3 answers the whole connect sequence, + * the status read included. + */ +public class API_00031_ConnectReadyAfterConnectSequenceTest { + + private ShimmerPC mDevice; + private volatile boolean mStatusRead; + private volatile boolean mSequenceEnded; + /** What had happened when the state first became CONNECTED; null until then */ + private volatile Boolean mStatusReadAtFirstConnected; + private volatile Boolean mSequenceEndedAtFirstConnected; + private volatile List mQueuedAtFirstConnected; + + @Before + public void setUp() { + mDevice = new ShimmerPC("COM99") { + @Override + public void startTimerReadStatus() { + // As in ByteCommunicationTestUtils.newShimmerPCWithoutStatusTimer: only the + // connect sequence's own status read should run + } + + @Override + protected void eventLogAndStreamStatusChanged(byte currentCommand) { + mStatusRead = true; + super.eventLogAndStreamStatusChanged(currentCommand); + } + + @Override + protected void isReadyForStreaming() { + mSequenceEnded = true; + super.isReadyForStreaming(); + } + + @Override + public boolean setBluetoothRadioState(BT_STATE state) { + if(state==BT_STATE.CONNECTED && mSequenceEndedAtFirstConnected==null){ + mStatusReadAtFirstConnected = mStatusRead; + mSequenceEndedAtFirstConnected = mSequenceEnded; + List queued = new ArrayList<>(); + for(byte[] instruction:new ArrayList<>(getListofInstructions())){ + if(instruction!=null){ + queued.add(btCommandToString(instruction[0])); + } + } + mQueuedAtFirstConnected = queued; + } + return super.setBluetoothRadioState(state); + } + }; + mDevice.setTestRadio(new ByteCommunicationSimulatorS3("COM99")); + } + + @After + public void tearDown() { + mDevice.disconnectNoException(); + } + + @Test + public void theUnitIsReportedReadyOnlyOnceTheConnectSequenceHasEnded() { + ByteCommunicationTestUtils.connectAndWaitUntilInitialised(mDevice); + + assertNotNull("the state became CONNECTED", mSequenceEndedAtFirstConnected); + assertTrue("the connect sequence read the status before the end, so the case is covered", mStatusReadAtFirstConnected); + assertTrue("CONNECTED came only once the connect sequence had ended (inquiryDone, isReadyForStreaming); still queued: " + + mQueuedAtFirstConnected, mSequenceEndedAtFirstConnected); + // Every read of the unit's configuration (status, battery, calibration dump, + // inquiry) is answered. What may remain: the inquiry itself, still at the head + // while its reply is handled, and the RTC write that the status reply queues + // when the unit's clock is not set. That write runs before anything the app + // queues, so it never lands in a stream. + List allowed = new ArrayList<>(); + allowed.add(ShimmerBluetooth.btCommandToString(ShimmerObject.INQUIRY_COMMAND)); + allowed.add(ShimmerBluetooth.btCommandToString(ShimmerObject.SET_RWC_COMMAND)); + for(String queued:mQueuedAtFirstConnected){ + assertTrue("no read from the connect sequence is still queued at CONNECTED, but " + queued + " is (queued: " + + mQueuedAtFirstConnected + ")", allowed.contains(queued)); + } + } +} diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/ByteCommunicationSimulatorS3R.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/ByteCommunicationSimulatorS3R.java index 4d7151238..b9ec057d7 100644 --- a/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/ByteCommunicationSimulatorS3R.java +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/ByteCommunicationSimulatorS3R.java @@ -24,7 +24,26 @@ protected void txShimmerVersion() { mBuffer.add((byte) 0x0A); } - @Override + /** + * A Shimmer3R's inquiry reply has 11 bytes of settings, with the channel count + * at index 9, so the Shimmer3 reply this class inherited never parsed: the + * inquiry timed out, and the tests only passed because a status reply part way + * through the connect sequence reported the unit ready first (DEV-1275). The + * settings match this simulator's InfoMem: the sampling rate, config setup + * bytes 0-3 from the first block and 4-6 from the second, then no channels + * and a buffer size of 1. + */ + @Override + protected void inquiryResponse() { + mBuffer.add((byte) 0xff); + mBuffer.add((byte) 0x02); + byte[] bytes = UtilShimmer.hexStringToByteArray("80024D9B0E080000000001"); + for (byte b:bytes) { + mBuffer.add(b); + } + } + + @Override protected void txFirmwareVersion() { mBuffer.add((byte) 0xff); mBuffer.add((byte) 0x2f);