Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ public class Shimmer extends ShimmerBluetooth{
//transient public final Handler mHandler;

transient private ConnectThread mConnectThread;
transient private ConnectedThread mConnectedThread;
// volatile: stop() nulls it on the caller's thread while the IO thread writes through it
transient private volatile ConnectedThread mConnectedThread;
private boolean mDummy=false;
transient private LocalDevice localDevice;
//private InputStream mInputStream=null;
Expand Down Expand Up @@ -1217,24 +1218,20 @@ protected void clearSerialBuffer() {
}

/**
* Write to the ConnectedThread in an unsynchronized manner
* Write to the ConnectedThread in an unsynchronized manner. If the connection has
* already been stopped, the bytes are dropped and logged, as a failed write is.
* @param out The bytes to write
* @see ConnectedThread write(byte[])
*/
public void write(byte[] out) {
/*
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
//if (mState != BT_STATE.CONNECTED && mState != BT_STATE.STREAMING) return;
if (mBluetoothRadioState == BT_STATE.DISCONNECTED ) return;
r = mConnectedThread;
}
// Perform the write unsynchronized
r.write(out);
*/
mConnectedThread.write(out);
// stop() can null mConnectedThread on another thread at any point, e.g. while the
// IO thread sleeps before sending a queued command, so read it once (DEV-1372)
ConnectedThread connectedThread = mConnectedThread;
if (connectedThread == null) {
Log.d(mClassName, "Command NOT transmitted, not connected: " + mMyBluetoothAddress + "; Command Issued: " + mCurrentCommand );
return;
}
connectedThread.write(out);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.shimmerresearch.bluetooth;

import android.content.Context;
import android.os.Handler;

import org.junit.Test;

import com.shimmerresearch.android.Shimmer;
import com.shimmerresearch.driver.ShimmerObject;

/**
* DEV-1372: the driver's IO thread takes a queued command, sleeps 10-110 ms and
* then writes it with writeBytes(), which is Shimmer.write() here. disconnect()
* calls stop() on the caller's thread, and stop() nulls the connection without
* waiting for the IO thread. A disconnect inside that sleep made write()
* dereference null; nothing on the IO thread catches it, so Android killed the
* app. The Android HIL hit it on Bluetooth Classic.
* <p>
* A write with no connection is now dropped and logged, as a write that fails on
* a closed socket already was.
* <p>
* No hardware: a Shimmer that was never connected, or has been stopped, has no
* connection, which is the state the IO thread meets after the disconnect.
*/
public class AAPI_00007_WriteAfterDisconnectTest {

private static final byte[] QUEUED_COMMAND = new byte[] {ShimmerObject.GET_STATUS_COMMAND};

/** What the IO thread does with the command it took off the queue before the disconnect */
private static void sendQueuedCommand(ShimmerBluetooth device) {
device.writeBytes(QUEUED_COMMAND);
}

@Test
public void queuedCommandAfterStopIsDropped() {
Shimmer device = new Shimmer((Handler) null, (Context) null);
device.stop();
sendQueuedCommand(device);
}

@Test
public void queuedCommandAfterDisconnectIsDropped() {
Shimmer device = new Shimmer((Handler) null, (Context) null);
device.disconnect();
sendQueuedCommand(device);
}

@Test
public void writeWithNoConnectionIsDropped() {
Shimmer device = new Shimmer((Handler) null, (Context) null);
device.write(QUEUED_COMMAND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ public void streamRest() throws Exception {
* Commands while streaming: start and stop SD logging (SET_RWC_COMMAND, whose bare ACK carries the
* link CRC: DEV-1147 in the driver, DEV-1148 for Android's own parser) and read the battery. No
* packet may be lost around any of them, and logging must actually start and stop. Needs an SD
* card in the unit and a link CRC on to exercise DEV-1148.
* card in the unit, off the dock and USB, and a link CRC on to exercise DEV-1148.
*/
@Test
public void midStreamSet() throws Exception {
begin("mid_stream_set");
double rate = arg("rate", 256.0);
int cycles = arg("cycles", 3);
double maxLoss = arg("maxLossPercent", 1.0);
if (connectAndConfigure(rate)) {
if (connectAndConfigure(rate) && checkCanLog()) {
ShimmerBluetooth d = session.device();
double deviceRate = d.getSamplingRateShimmer();
checkRate(deviceRate, "stream");
Expand Down Expand Up @@ -355,15 +355,16 @@ public void midStreamSet() throws Exception {
* every 40 ms for 400 ms: Android writes those reads straight out, so some land in the pause. Then
* each restart stops logging and starts it again back to back, so the queued START_LOGGING_ONLY goes
* out in the pause and its bare ACK comes first. Every battery read must be answered, logging must
* restart, and nothing may log a parser error or a command timeout. Needs an SD card in the unit.
* restart, and nothing may log a parser error or a command timeout. Needs an SD card in the unit,
* off the dock and USB.
*/
@Test
public void inStream() throws Exception {
begin("in_stream");
double rate = arg("rate", 256.0);
int cycles = arg("cycles", 3);
double maxLoss = arg("maxLossPercent", 1.0);
if (connectAndConfigure(rate)) {
if (connectAndConfigure(rate) && checkCanLog()) {
double deviceRate = session.rateOnDevice();
checkRate(deviceRate, "stream");
LogTally tally = new LogTally();
Expand Down Expand Up @@ -522,6 +523,17 @@ private void checkJumpsAwayFrom(StreamAnalysis a, List<Double> sdMoments, String
report.put("timestampJumpsNearSdLoggingStartStop", nearSd);
}

/**
* For a scenario that logs to the SD card: fails it at once, saying why, if the unit is docked or
* on USB. The firmware then ignores the start, and the API still shows the unit logging, so the
* scenario would test nothing, or fail later on an unrelated check.
*/
private boolean checkCanLog() {
String why = session.whyCannotLog();
report.check(why == null, why + ": the firmware will not start SD logging, so this scenario cannot run. Take the unit off the dock and USB, then run it again");
return why == null;
}

/** Starts SD logging while streaming and waits until it has started */
private boolean startSdLoggingWhileStreaming(String what, List<Double> sdMoments) throws InterruptedException {
sdMoments.add(session.nowMs());
Expand All @@ -537,15 +549,16 @@ private boolean startSdLoggingWhileStreaming(String what, List<Double> sdMoments
* to its SD card. The sequence reads the status part way through, before the calibration dump and
* the inquiry, and that reply used to report the unit ready early, so an app's next command went
* out in among the rest of the sequence. Connects to the unit idle, then again while it logs.
* Needs an SD card in the unit.
* Needs an SD card in the unit, off the dock and USB.
*/
@Test
public void connectReady() throws Exception {
begin("connect_ready");
if (connectAndCheckReady("idle", BT_STATE.CONNECTED)) {
boolean logging = session.startSdLogging()
boolean canLog = checkCanLog();
boolean logging = canLog && session.startSdLogging()
&& session.waitFor(() -> session.state() == BT_STATE.SDLOGGING && session.queuedInstructions() == 0, 5000);
report.check(logging, "SD logging did not start (state " + session.state() + "; is there an SD card?)");
report.check(!canLog || logging, "SD logging did not start (state " + session.state() + "; is there an SD card?)");
report.check(session.disconnect(10000), "did not disconnect within 10 s");
if (logging && session.hung == null) {
Thread.sleep(2000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,26 @@ int queuedInstructions() {
}
}

/**
* Why the unit will not log to its SD card, or null if nothing says it won't. Docked, or on USB
* (Shimmer3R), the firmware ACKs a start of logging and then ignores it (LogAndStream v1.01.002 on
* Shimmer3, v1.01.004 on Shimmer3R). The driver marks the unit logging on that ACK, so the API's
* state shows logging with nothing logged. Taken from the status the connect read.
*/
String whyCannotLog() {
ShimmerBluetooth d = device();
if (d == null) {
return null;
}
if (d.isDocked()) {
return "the unit reports itself docked";
}
if (d.isUsbPluggedIn()) {
return "the unit reports USB plugged in";
}
return null;
}

/**
* Accel only (the low-noise accel on Shimmer3, the LSM6DSV's on Shimmer3R) at rateHz, written
* the way the examples do: a clone, AssembleShimmerConfig, then configureShimmer.
Expand All @@ -400,16 +420,20 @@ boolean configureAccelOnly(double rateHz, long timeoutMs) throws InterruptedExce
return false;
}
// Done when the operation has finished: either the manager's progress report (not every
// version forwards it to the Handler) or the fresh "Ready" the unit posts when the operation
// ends, with the requested rate read back. The status timer is stopped during an operation,
// so that "Ready" comes from the configuration.
// version forwards it to the Handler) or a "Ready" with the requested rate read back, and in
// either case once the queue is empty. The rate and a "Ready" are not enough on their own: the
// rate is in the first config chunk written, and a "Ready" can be the end of an earlier stop.
// Straight after a stopStreaming() both came with commands still queued, so the test
// disconnected under the IO thread (DEV-1372). The configure queues the whole operation before
// it returns, so an empty queue means its last ACK has been processed.
boolean done = waitFor(() -> state() == BT_STATE.CONNECTED
&& queuedInstructions() == 0
&& (configureResult != null
|| (stateMessages > stateMessagesBefore && Math.abs(rateOnDevice() - rateHz) < 0.5)),
timeoutMs);
event(String.format(Locale.ROOT, "configure %s (%s): rate on device %.2f Hz",
event(String.format(Locale.ROOT, "configure %s (%s): rate on device %.2f Hz, %d instructions queued",
done ? "finished" : "timed out", configureResult != null ? configureResult : "no progress report",
rateOnDevice()));
rateOnDevice(), queuedInstructions()));
return done && (configureResult == null || configureResult == BluetoothProgressReportPerDevice.OperationState.SUCCESS);
}

Expand Down
12 changes: 8 additions & 4 deletions tools/hil/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ To find regressions before release day, schedule step 2 nightly with Windows Tas
| Scenario | What it does | Fails when |
|---|---|---|
| `streamRest` | Connects, writes an accel-only config at `-Rate` (default 256 Hz), streams for `-Seconds` (default 30) with the unit lying still, then stops and disconnects. | It can't connect or configure; more than `-MaxLossPercent` (1 %) of packets are lost; a timestamp doesn't increase, or leaps more than a second ahead of when it arrived (a bad unwrap, not loss); the timestamps give no rate, or one more than 1 % off the configured rate; the stream goes silent for over a second before the test stops it; accel is more than 1 m/s² from 9.81; or the API logs a "Packet syncing problem". |
| `midStreamSet` | While streaming, starts and stops SD logging and reads the battery, three times. | One of those calls fails, or a battery reply isn't decoded within 5 s; logging doesn't start or stop (the DEV-1148 symptom with a link CRC on); a packet is lost around a battery read, or more than `-MaxLossPercent` away from the SD transitions; the timestamps fail the same rate and silence checks as `streamRest`; or there's a parser error. Packets missing around SD logging start/stop are reported, not failed: the unit itself pauses while it opens its file, which is for the firmware HIL bench to judge. Needs an SD card in the unit. |
| `midStreamSet` | While streaming, starts and stops SD logging and reads the battery, three times. | One of those calls fails, or a battery reply isn't decoded within 5 s; logging doesn't start or stop (the DEV-1148 symptom with a link CRC on); a packet is lost around a battery read, or more than `-MaxLossPercent` away from the SD transitions; the timestamps fail the same rate and silence checks as `streamRest`; or there's a parser error. Packets missing around SD logging start/stop are reported, not failed: the unit itself pauses while it opens its file, which is for the firmware HIL bench to judge. Needs an SD card in the unit, off the dock and USB. |
| `reconnectLoop` | Connects, streams for 5 s and disconnects, five times. | Any cycle fails to connect within 30 s, loses packets, fails the rate and silence checks, or logs a parser error. |
| `inStream` | While streaming, three times: starts SD logging, stops it, and reads the battery and the status every 40 ms for 400 ms, so some reads land in the ~100 ms the unit pauses its data packets while it closes its file (DEV-1291). Then, three times, stops and starts logging back to back, so the queued start goes out in that pause. | A battery read isn't answered, or fewer status replies than reads are decoded; logging doesn't stop or restart, or a command stays queued; a command times out; there's a parser error; more than `-MaxLossPercent` of packets are lost away from the SD transitions and the reads; or the timestamps fail the same rate and silence checks as `streamRest`. Loss near the reads is reported, not failed: a Shimmer3 now and then skips a sample around a battery read itself. The report's `repliesInAPause` counts the replies that came with no data packet before them. Needs an SD card in the unit. |
| `connectReady` | Connects to the unit idle, starts SD logging, disconnects and connects again while it logs, then stops logging. | The unit is reported ready (a CONNECTED or SDLOGGING state message, or that state in the API) more than 250 ms before `NOTIFICATION_SHIMMER_FULLY_INITIALIZED`, which marks the end of the connect sequence (DEV-1275); or the connect doesn't end in CONNECTED, or SDLOGGING while logging. Needs an SD card in the unit. |
| `inStream` | While streaming, three times: starts SD logging, stops it, and reads the battery and the status every 40 ms for 400 ms, so some reads land in the ~100 ms the unit pauses its data packets while it closes its file (DEV-1291). Then, three times, stops and starts logging back to back, so the queued start goes out in that pause. | A battery read isn't answered, or fewer status replies than reads are decoded; logging doesn't stop or restart, or a command stays queued; a command times out; there's a parser error; more than `-MaxLossPercent` of packets are lost away from the SD transitions and the reads; or the timestamps fail the same rate and silence checks as `streamRest`. Loss near the reads is reported, not failed: a Shimmer3 now and then skips a sample around a battery read itself. The report's `repliesInAPause` counts the replies that came with no data packet before them. Needs an SD card in the unit, off the dock and USB. |
| `connectReady` | Connects to the unit idle, starts SD logging, disconnects and connects again while it logs, then stops logging. | The unit is reported ready (a CONNECTED or SDLOGGING state message, or that state in the API) more than 250 ms before `NOTIFICATION_SHIMMER_FULLY_INITIALIZED`, which marks the end of the connect sequence (DEV-1275); or the connect doesn't end in CONNECTED, or SDLOGGING while logging. Needs an SD card in the unit, off the dock and USB. |
| `queueStress` | Connected and idle, with the API's timers on as in an app: four threads queue battery and status reads in tight loops for 60 s, keeping the queue under 8. Each queued read takes the device's lock and then the queue's while the IO thread fetches the next instruction, the window DEV-1266's lock-order deadlock needs. Idle, because while streaming Android writes these reads straight out, unqueued. | A read doesn't return within 10 s, or no read returns for 10 s (the report then holds every thread's stack); a read throws; or the queue doesn't drain within 15 s after the reads stop. No SD card needed. On driver 0.11.7_beta, which has the deadlock, this is the scenario to reproduce it. |
| `samplingRates` | Writes 51.2, 256 and 512 Hz in turn, the way the examples configure, and streams 5 s at each; then writes the unit's own rate back. Every configure but the first comes straight after a `stopStreaming()`, as an app that changes the rate does: up to driver 0.11.10_beta the stop's reply then retired the configuration's first chunk, the one holding the sampling rate (DEV-1306). | A configuration doesn't complete within 20 s, or the rate read back from the unit isn't the one written; or a stream fails `streamRest`'s loss, timestamp, rate and silence checks, or logs a parser error. No SD card needed. |

Expand Down Expand Up @@ -110,7 +110,11 @@ since Android uses those unchanged.
3. **Prepare each unit.**
- **SD sync and Bluetooth disable off.** Either one keeps Bluetooth off (DEV-1228). Check in
Consensys.
- **SD card in,** for `midStreamSet`, `inStream` and `connectReady`.
- **SD card in, and off the dock and USB,** for `midStreamSet`, `inStream` and `connectReady`.
Docked, or with USB plugged into a Shimmer3R, the firmware ACKs a start of SD logging and then
ignores it (LogAndStream v1.01.002 on Shimmer3, v1.01.004 on Shimmer3R). The API still shows
the unit logging, so these scenarios fail at once, saying the unit is docked or on USB, rather
than pass without logging anything.
- **Lying still** during `streamRest`.
4. **List the units:** copy `rig-android.example.json` to `rig-android.json` and fill in each name,
MAC, `type` (`Shimmer3` or `Shimmer3R`) and `transports` (`["classic"]`, `["ble"]` or both;
Expand Down
Loading