From e53cfb1b48d551f02972cda89876ab8f04d179e2 Mon Sep 17 00:00:00 2001 From: JongChern Date: Thu, 22 Oct 2026 13:30:38 +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, before the battery, the BT firmware version, the calibration dump and the inquiry. On that reply, logAndStreamStatusChanged() moved the state from CONNECTING to CONNECTED, or to SDLOGGING for a unit that is logging, which also sets mIsInitialised. ShimmerBluetoothManagerAndroid and apps take either state as ready, so whatever the app did next went out in among the rest of the sequence. The Java HIL found the same defect in the driver's ShimmerPC: a configure sent then cut a Shimmer3's calibration dump short and left its accel NaN. The sequence's own end, inquiryDone() and then isReadyForStreaming(), already moves CONNECTING to CONNECTED or SDLOGGING and sends NOTIFICATION_SHIMMER_FULLY_INITIALIZED. A status reply now leaves CONNECTING to it. Once connected, a status reply still moves the state as before. This is the driver's DEV-1275 fix, ported: Shimmer overrides the status handler, so the driver's fix doesn't reach it. Unlike ShimmerPC, Shimmer also holds back SDLOGGING, because here it marks the unit initialised. AAPI_00004 hands a status reply to the driver's parser while CONNECTING, as the IO thread does while not streaming. Without the fix, the idle and the logging cases fail: the state moves to CONNECTED or SDLOGGING before the sequence ends. Co-Authored-By: Claude Opus 5.5 --- .../com/shimmerresearch/android/Shimmer.java | 10 +- ..._ConnectReadyAfterConnectSequenceTest.java | 164 ++++++++++++++++++ 2 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00004_ConnectReadyAfterConnectSequenceTest.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 8ae9faef..cc286b95 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/Shimmer.java @@ -1700,7 +1700,15 @@ protected void hasStopStreaming() { protected void logAndStreamStatusChanged() { - + // While connecting, the connect sequence's own end (inquiryDone(), then + // isReadyForStreaming()) moves CONNECTING to CONNECTED, or to SDLOGGING for a + // unit that is logging. 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). SDLOGGING also sets mIsInitialised. + if(mBluetoothRadioState==BT_STATE.CONNECTING){ + return; + } + // if(mCurrentCommand==START_LOGGING_ONLY_COMMAND){ // TODO this causing a problem Shimmer Bluetooth disconnects // setState(BT_STATE.SDLOGGING); diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00004_ConnectReadyAfterConnectSequenceTest.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00004_ConnectReadyAfterConnectSequenceTest.java new file mode 100644 index 00000000..3210f963 --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/test/java/com/shimmerresearch/bluetooth/AAPI_00004_ConnectReadyAfterConnectSequenceTest.java @@ -0,0 +1,164 @@ +package com.shimmerresearch.bluetooth; + +import static org.junit.Assert.*; + +import android.content.Context; +import android.os.Handler; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Deque; +import java.util.List; + +import org.junit.Test; + +import com.shimmerresearch.android.Shimmer; +import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE; +import com.shimmerresearch.driver.ShimmerObject; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerObject; + +/** + * DEV-1275: the connect sequence reads the unit's status part way through, before + * the battery, the calibration dump and the inquiry. Shimmer's + * logAndStreamStatusChanged() moved the state on that reply, from CONNECTING to + * CONNECTED, or to SDLOGGING (which also sets mIsInitialised) for a unit that is + * logging. So the app was told the unit was ready while the rest of the sequence + * was still queued, and whatever it did next went out in among it. The Java HIL + * found it on the driver's ShimmerPC (DEV-1269): a configure sent then cut a + * Shimmer3's calibration dump short and left its accel NaN. + *

+ * The sequence's own end, inquiryDone() and then isReadyForStreaming(), is what + * reports the unit ready. The driver fixed ShimmerPC (API_00031); Shimmer + * overrides the status handler, so it needs the same fix. + *

+ * No hardware: the status reply is read from an in-memory byte queue by the + * driver's own parser, as the IO thread does while not streaming. + */ +public class AAPI_00004_ConnectReadyAfterConnectSequenceTest { + + /** Not docked, sensing or logging; RTC set */ + private static final byte STATUS_IDLE = 0x04; + + /** Sensing and SD logging; RTC set */ + private static final byte STATUS_SD_LOGGING = 0x0E; + + /** A Shimmer whose Bluetooth link is in memory, and which records every state it is set to */ + private static class QueueShimmer extends Shimmer { + final Deque rx = new ArrayDeque(); + final List states = new ArrayList(); + + QueueShimmer() { + super((Handler) null, (Context) null); + } + + void queue(byte... bytes) { + for(byte b:bytes){ + rx.add(b); + } + } + + /** What the IO thread does with an in-stream response while not streaming, once it has read its ACK and INSTREAM_CMD_RESPONSE */ + void parseInstreamResponse() { + processInstreamResponse(false); + } + + /** The end of the connect sequence: the inquiry's reply, or the ACK to the SET_SENSORS_COMMAND that ends it */ + void endConnectSequence() { + inquiryDone(); + } + + @Override + public boolean setBluetoothRadioState(BT_STATE state) { + states.add(state); + return super.setBluetoothRadioState(state); + } + + @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(), device.states); + assertEquals(BT_STATE.CONNECTING, device.getBluetoothRadioState()); + assertFalse("not reported initialised before the sequence ends", device.isInitialised()); + } + + @Test + public void statusReplyWhileConnectingLeavesTheUnitConnecting() { + QueueShimmer device = connectingShimmer3(); + receiveStatusReply(device, STATUS_IDLE); + assertFalse("the status is still decoded", device.isSDLogging()); + assertStillConnecting(device); + + device.endConnectSequence(); + assertEquals("ready once the sequence ends", Arrays.asList(BT_STATE.CONNECTED), device.states); + assertTrue(device.isInitialised()); + } + + @Test + public void statusReplyWhileConnectingToALoggingUnitLeavesTheUnitConnecting() { + QueueShimmer device = connectingShimmer3(); + receiveStatusReply(device, STATUS_SD_LOGGING); + assertTrue("the status is still decoded", device.isSDLogging()); + assertStillConnecting(device); + + device.endConnectSequence(); + assertEquals("ready once the sequence ends", Arrays.asList(BT_STATE.SDLOGGING), device.states); + assertTrue(device.isInitialised()); + } + + /** Once connected, a status reply still moves the state */ + @Test + public void statusReplyAfterConnectingStillChangesTheState() { + QueueShimmer device = connectingShimmer3(); + device.endConnectSequence(); + assertEquals(BT_STATE.CONNECTED, device.getBluetoothRadioState()); + + receiveStatusReply(device, STATUS_SD_LOGGING); + assertEquals(BT_STATE.SDLOGGING, device.getBluetoothRadioState()); + + receiveStatusReply(device, STATUS_IDLE); + assertEquals(BT_STATE.CONNECTED, device.getBluetoothRadioState()); + } +}