Skip to content

Commit 0cfa28a

Browse files
authored
Merge pull request #333 from ShimmerResearch/DEV-1275_ready_after_connect_sequence
DEV-1275: Report a Shimmer ready only once its connect sequence has ended
2 parents 8059433 + 9bb3d5d commit 0cfa28a

3 files changed

Lines changed: 134 additions & 3 deletions

File tree

‎ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,13 @@ else if(isSDLogging()){
900900
}
901901
else{
902902
// if(!isStreaming() && !isSDLogging() && isConnected()){
903-
if(!mIsStreaming && !isSDLogging() && isConnected() && mBluetoothRadioState!=BT_STATE.CONNECTED){
904-
setBluetoothRadioState(BT_STATE.CONNECTED);
903+
// While connecting, the connect sequence's own end (inquiryDone(), then
904+
// isReadyForStreaming()) moves CONNECTING to CONNECTED. The sequence reads the
905+
// status part way through, and if that reply did it, the app would be told the
906+
// unit is ready while the rest of the sequence is still queued (DEV-1275).
907+
if(!mIsStreaming && !isSDLogging() && isConnected()
908+
&& mBluetoothRadioState!=BT_STATE.CONNECTED && mBluetoothRadioState!=BT_STATE.CONNECTING){
909+
setBluetoothRadioState(BT_STATE.CONNECTED);
905910
}
906911
// if(getBTState() == BT_STATE.INITIALISED){
907912
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.shimmerresearch.shimmer3.communication;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import com.shimmerresearch.bluetooth.ShimmerBluetooth;
13+
import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE;
14+
import com.shimmerresearch.driver.ShimmerObject;
15+
import com.shimmerresearch.pcDriver.ShimmerPC;
16+
17+
/**
18+
* DEV-1275: the connect sequence reads the unit's status part way through, and
19+
* ShimmerPC.eventLogAndStreamStatusChanged() moved CONNECTING to CONNECTED on
20+
* that reply. ShimmerDeviceCallbackAdapter marks every CONNECTED initialised,
21+
* so an app waiting for CONNECTED and initialised was told the unit was ready
22+
* while the rest of the sequence (battery, calibration dump, inquiry) was still
23+
* queued. Whatever it did next went out in among that sequence: streaming
24+
* started part way through it, and a configure cut the calibration dump short,
25+
* which left a Shimmer3's accel NaN. The sequence's own end, inquiryDone() and
26+
* then isReadyForStreaming(), is what moves CONNECTING to CONNECTED.
27+
* <p>
28+
* No hardware: ByteCommunicationSimulatorS3 answers the whole connect sequence,
29+
* the status read included.
30+
*/
31+
public class API_00031_ConnectReadyAfterConnectSequenceTest {
32+
33+
private ShimmerPC mDevice;
34+
private volatile boolean mStatusRead;
35+
private volatile boolean mSequenceEnded;
36+
/** What had happened when the state first became CONNECTED; null until then */
37+
private volatile Boolean mStatusReadAtFirstConnected;
38+
private volatile Boolean mSequenceEndedAtFirstConnected;
39+
private volatile List<String> mQueuedAtFirstConnected;
40+
41+
@Before
42+
public void setUp() {
43+
mDevice = new ShimmerPC("COM99") {
44+
@Override
45+
public void startTimerReadStatus() {
46+
// As in ByteCommunicationTestUtils.newShimmerPCWithoutStatusTimer: only the
47+
// connect sequence's own status read should run
48+
}
49+
50+
@Override
51+
protected void eventLogAndStreamStatusChanged(byte currentCommand) {
52+
mStatusRead = true;
53+
super.eventLogAndStreamStatusChanged(currentCommand);
54+
}
55+
56+
@Override
57+
protected void isReadyForStreaming() {
58+
mSequenceEnded = true;
59+
super.isReadyForStreaming();
60+
}
61+
62+
@Override
63+
public boolean setBluetoothRadioState(BT_STATE state) {
64+
if(state==BT_STATE.CONNECTED && mSequenceEndedAtFirstConnected==null){
65+
mStatusReadAtFirstConnected = mStatusRead;
66+
mSequenceEndedAtFirstConnected = mSequenceEnded;
67+
List<String> queued = new ArrayList<>();
68+
for(byte[] instruction:new ArrayList<>(getListofInstructions())){
69+
if(instruction!=null){
70+
queued.add(btCommandToString(instruction[0]));
71+
}
72+
}
73+
mQueuedAtFirstConnected = queued;
74+
}
75+
return super.setBluetoothRadioState(state);
76+
}
77+
};
78+
mDevice.setTestRadio(new ByteCommunicationSimulatorS3("COM99"));
79+
}
80+
81+
@After
82+
public void tearDown() {
83+
mDevice.disconnectNoException();
84+
}
85+
86+
@Test
87+
public void theUnitIsReportedReadyOnlyOnceTheConnectSequenceHasEnded() {
88+
ByteCommunicationTestUtils.connectAndWaitUntilInitialised(mDevice);
89+
90+
assertNotNull("the state became CONNECTED", mSequenceEndedAtFirstConnected);
91+
assertTrue("the connect sequence read the status before the end, so the case is covered", mStatusReadAtFirstConnected);
92+
assertTrue("CONNECTED came only once the connect sequence had ended (inquiryDone, isReadyForStreaming); still queued: "
93+
+ mQueuedAtFirstConnected, mSequenceEndedAtFirstConnected);
94+
// Every read of the unit's configuration (status, battery, calibration dump,
95+
// inquiry) is answered. What may remain: the inquiry itself, still at the head
96+
// while its reply is handled, and the RTC write that the status reply queues
97+
// when the unit's clock is not set. That write runs before anything the app
98+
// queues, so it never lands in a stream.
99+
List<String> allowed = new ArrayList<>();
100+
allowed.add(ShimmerBluetooth.btCommandToString(ShimmerObject.INQUIRY_COMMAND));
101+
allowed.add(ShimmerBluetooth.btCommandToString(ShimmerObject.SET_RWC_COMMAND));
102+
for(String queued:mQueuedAtFirstConnected){
103+
assertTrue("no read from the connect sequence is still queued at CONNECTED, but " + queued + " is (queued: "
104+
+ mQueuedAtFirstConnected + ")", allowed.contains(queued));
105+
}
106+
}
107+
}

‎ShimmerDriverPC/src/test/java/com/shimmerresearch/shimmer3/communication/ByteCommunicationSimulatorS3R.java‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,26 @@ protected void txShimmerVersion() {
2424
mBuffer.add((byte) 0x0A);
2525
}
2626

27-
@Override
27+
/**
28+
* A Shimmer3R's inquiry reply has 11 bytes of settings, with the channel count
29+
* at index 9, so the Shimmer3 reply this class inherited never parsed: the
30+
* inquiry timed out, and the tests only passed because a status reply part way
31+
* through the connect sequence reported the unit ready first (DEV-1275). The
32+
* settings match this simulator's InfoMem: the sampling rate, config setup
33+
* bytes 0-3 from the first block and 4-6 from the second, then no channels
34+
* and a buffer size of 1.
35+
*/
36+
@Override
37+
protected void inquiryResponse() {
38+
mBuffer.add((byte) 0xff);
39+
mBuffer.add((byte) 0x02);
40+
byte[] bytes = UtilShimmer.hexStringToByteArray("80024D9B0E080000000001");
41+
for (byte b:bytes) {
42+
mBuffer.add(b);
43+
}
44+
}
45+
46+
@Override
2847
protected void txFirmwareVersion() {
2948
mBuffer.add((byte) 0xff);
3049
mBuffer.add((byte) 0x2f);

0 commit comments

Comments
 (0)