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 @@ -186,7 +186,7 @@ public String toString() {
protected byte mCurrentCommand;
protected boolean mWaitForAck=false; // This indicates whether the device is waiting for an acknowledge packet from the Shimmer Device
protected boolean mWaitForResponse=false; // This indicates whether the device is waiting for a response packet from the Shimmer Device
protected boolean mTransactionCompleted=true; // Variable is used to ensure a command has finished execution prior to executing the next command (see initialize())
protected volatile boolean mTransactionCompleted=true; // Variable is used to ensure a command has finished execution prior to executing the next command (see initialize()). Volatile: operationPrepare() waits on it from an app thread
transient protected IOThread mIOThread;
transient protected ProcessingThread mPThread;
@Deprecated // mContinousSync doesn't do anything
Expand Down Expand Up @@ -858,13 +858,21 @@ private void processNotStreamingWaitForAck() {

clearSerialBuffer();

hasStopStreaming();
hasStopStreaming();
removeInstruction(0);
removeAllNulls();
if (mCurrentCommand==STOP_SDBT_COMMAND){
eventLogAndStreamStatusChanged(mCurrentCommand);
eventLogAndStreamStatusChanged(mCurrentCommand);
}
setInstructionStackLock(false);
// The stop is handled and the STOP retired. The byte that started the handling
// must not also go on to the ACK processing below: when it was the unit's ACK,
// that retired the next instruction as well, a command queued straight after
// stopStreaming() that was never sent (DEV-1306)
if((byte)byteBuffer[0]==ACK_COMMAND_PROCESSED){
printLogDataForDebugging("Stop handled on its ACK, the first byte back");
}
return;
}
// //TODO: ACK is probably now working for STOP_STREAMING_COMMAND so merge in with others?
// if(mCurrentCommand==STOP_SDBT_COMMAND) { //due to not receiving the ack from stop streaming command we will skip looking for it.
Expand Down Expand Up @@ -2989,6 +2997,14 @@ public void setSendProgressReport(boolean state) {
public void operationPrepare(){
stopAllTimers();

// Let a command already sent finish first. Clearing the list now would remove it while it is
// still in flight, and its reply, which retires the head of the list (removeInstruction(0)),
// would then remove this operation's first instruction in its place. After stopStreaming() the
// STOP is in flight for about 400 ms, as its stop handling sleeps that long, so a configuration
// started straight after a stop lost its first SET_INFOMEM, the chunk that holds the sampling
// rate (DEV-1306).
waitForInstructionInFlight(OPERATION_PREPARE_WAIT_MS);

//make sure no more instructions
//shouldnt matter since the configuration are being rewritten by consensys
//used in initializeshimmer3 (this class) and BluetoothManager class
Expand Down Expand Up @@ -6065,6 +6081,46 @@ public void enableCheckifAlive(boolean set){
}
}

/** How long operationPrepare() waits for a command in flight: past its 2 s ACK timeout */
private static final long OPERATION_PREPARE_WAIT_MS = 3000;

/**
* Waits until the instruction at the head of the list, if it has been sent and is waiting for its
* reply, has been answered and retired, or until the timeout. Returns at once on the IOThread,
* which is the thread that would retire it.
*/
private void waitForInstructionInFlight(long timeoutMs) {
byte[] inFlight;
synchronized(mListofInstructions){
inFlight = (!mTransactionCompleted && !mListofInstructions.isEmpty()) ? mListofInstructions.get(0) : null;
}
if(inFlight==null || Thread.currentThread()==mIOThread){
return;
}
long end = System.currentTimeMillis()+timeoutMs;
while(System.currentTimeMillis()<end){
synchronized(mListofInstructions){
boolean stillListed = false;
for(byte[] instruction:mListofInstructions){
if(instruction==inFlight){
stillListed = true;
break;
}
}
if(mTransactionCompleted && !stillListed){
return;
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
printLogDataForDebugging("operationPrepare: " + btCommandToString(inFlight[0]) + " was still in flight after " + timeoutMs + " ms; clearing it");
}

private synchronized void clearAllInstructions(){
synchronized(mListofInstructions){
getListofInstructions().clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package com.shimmerresearch.bluetooth;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Test;

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;
import com.shimmerresearch.pcDriver.ShimmerPC;
import com.shimmerresearch.shimmer3.communication.ByteCommunication;
import com.shimmerresearch.verisense.communication.ByteCommunicationListener;

import jssc.SerialPortTimeoutException;

/**
* DEV-1306: a configuration started straight after stopStreaming() lost its first SET_INFOMEM, the
* chunk that holds the sampling rate, so the unit kept its old rate. The Java HIL's samplingRates (DEV-1305)
* found it on a Shimmer3 and a Shimmer3R: 4 of 4 configurations started within ~400 ms of a stop lost
* the chunk, and 4 of 4 started on a quiet link sent it.
* <p>
* The STOP stays at the head of the instruction list, in flight, until its stop handling retires it:
* the first byte after it (stream data, while streaming) starts that handling, which sleeps 400 ms,
* then removes the head of the list (removeInstruction(0)). A configuration's operationPrepare()
* clears the list, so inside that window it removed the STOP, the operation queued its chunks, and
* the stop handling then removed the first chunk in the STOP's place.
* <p>
* No hardware: the IOThread runs as connect() starts it, over an in-memory radio that acknowledges
* every SET_INFOMEM as the firmware does. It lives in this package because mIOThread is a protected
* member of ShimmerBluetooth.
*/
public class API_00031_ConfigureStraightAfterStopTest {

/** Three 128-byte chunks, as a Shimmer3 or Shimmer3R configuration is written */
private static final int CONFIG_BYTES = 384;

private static class AnsweringRadio implements ByteCommunication {
final ConcurrentLinkedDeque<Byte> rx = new ConcurrentLinkedDeque<Byte>();
final List<byte[]> written = Collections.synchronizedList(new ArrayList<byte[]>());
final CountDownLatch stopSent = new CountDownLatch(1);

void queue(byte... bytes) {
for(byte b:bytes){
rx.add(b);
}
}

/** The address of each SET_INFOMEM written: [0x8C, length, address LSB, address MSB, data...] */
List<Integer> infoMemAddressesWritten() {
List<Integer> addresses = new ArrayList<Integer>();
synchronized(written){
for(byte[] w:written){
if(w[0]==ShimmerObject.SET_INFOMEM_COMMAND){
addresses.add((w[2]&0xFF) | ((w[3]&0xFF)<<8));
}
}
}
return addresses;
}

boolean wrote(byte command) {
synchronized(written){
for(byte[] w:written){
if(w[0]==command){
return true;
}
}
}
return false;
}

@Override
public boolean writeBytes(byte[] buffer) {
written.add(buffer.clone());
if(buffer[0]==ShimmerObject.STOP_STREAMING_COMMAND){
// Its reply is the test's to send, so the test controls when the stop handling starts
stopSent.countDown();
}
else {
// Every other command here is a SET, which the firmware acknowledges
queue(ShimmerObject.ACK_COMMAND_PROCESSED);
}
return true;
}

@Override
public byte[] readBytes(int byteCount, int timeout) throws SerialPortTimeoutException {
if(rx.size()<byteCount){
throw new SerialPortTimeoutException("TEST", "readBytes", timeout);
}
byte[] out = new byte[byteCount];
for(int i=0;i<byteCount;i++){
out[i] = rx.poll();
}
return out;
}

@Override public int getInputBufferBytesCount() { return rx.size(); }
@Override public boolean isOpened() { return true; }
@Override public boolean closePort() { return true; }
@Override public boolean openPort() { return true; }
@Override public boolean setParams(int i, int j, int k, int l) { return true; }
@Override public boolean purgePort(int i) { return true; }
@Override public void setByteCommunicationListener(ByteCommunicationListener byteCommListener) { }
@Override public void removeRadioListenerList() { }
}

private ShimmerPC mDevice;

@After
public void tearDown() {
if(mDevice!=null){
mDevice.stopTimerCheckForAckOrResp();
mDevice.disconnectNoException();
}
}

/** An initialised Shimmer3R part way through streaming, with its IOThread running as connect() starts it */
private AnsweringRadio streamingShimmer3r() {
AnsweringRadio radio = new AnsweringRadio();
mDevice = new ShimmerPC("COM99");
mDevice.setTestRadio(radio);
mDevice.setShimmerVersionObjectAndCreateSensorMap(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 7));
mDevice.setIsInitialised(true);
mDevice.setIsStreaming(true);
mDevice.mIOThread = mDevice.new IOThread();
mDevice.mIOThread.start();
return radio;
}

/** Writes a configuration the way ShimmerBluetoothManager does: prepare, queue the chunks, start */
private void configure() {
mDevice.operationPrepare();
mDevice.writeConfigBytes(0, new byte[CONFIG_BYTES], CONFIG_BYTES-1, false);
mDevice.operationStart(BT_STATE.CONFIGURING);
}

private List<Integer> waitForChunks(AnsweringRadio radio, int count, long timeoutMs) throws InterruptedException {
long end = System.currentTimeMillis()+timeoutMs;
while(radio.infoMemAddressesWritten().size()<count && System.currentTimeMillis()<end){
Thread.sleep(20);
}
return radio.infoMemAddressesWritten();
}

/** The firmware ready for the configuration */
private static final Integer[] ALL_THREE_CHUNKS = {0x0000, 0x0080, 0x0100};

/** The first byte of a data packet: while streaming, the unit's next byte after the STOP is usually stream data */
private static final byte STREAM_DATA = ShimmerObject.DATA_PACKET;

/** Hands the stop handling its first byte, and waits until it has taken it: it then sleeps 400 ms */
private void startStopHandling(AnsweringRadio radio, byte first) throws InterruptedException {
radio.queue(first);
long end = System.currentTimeMillis()+1000;
while(!radio.rx.isEmpty() && System.currentTimeMillis()<end){
Thread.sleep(5);
}
assertTrue("the stop handling took the byte", radio.rx.isEmpty());
}

/** As the HIL's samplingRates does: stop, then configure while the stop is still being handled */
@Test
public void aConfigurationStartedInTheStopWindowSendsEveryChunk() throws Exception {
AnsweringRadio radio = streamingShimmer3r();
mDevice.stopStreaming();
assertTrue("the STOP was sent", radio.stopSent.await(2, TimeUnit.SECONDS));
startStopHandling(radio, STREAM_DATA);

configure();

List<Integer> chunks = waitForChunks(radio, ALL_THREE_CHUNKS.length, 5000);
assertEquals("the SET_INFOMEM chunks sent, by address", java.util.Arrays.asList(ALL_THREE_CHUNKS), chunks);
}

/**
* When the unit's ACK is the first byte after the STOP, with no data packet in flight, the stop
* handling retired the STOP and the same byte then went on as an ACK and retired the next
* instruction too: here a plain SET queued straight after stopStreaming(), with no configuration
* operation at all.
*/
@Test
public void aCommandQueuedAfterTheStopIsSentWhenTheAckComesFirst() throws Exception {
AnsweringRadio radio = streamingShimmer3r();
mDevice.stopStreaming();
mDevice.writeAccelRange(1);
assertTrue("the STOP was sent", radio.stopSent.await(2, TimeUnit.SECONDS));
startStopHandling(radio, ShimmerObject.ACK_COMMAND_PROCESSED);

long end = System.currentTimeMillis()+3000;
while(!radio.wrote(ShimmerObject.SET_ACCEL_SENSITIVITY_COMMAND) && System.currentTimeMillis()<end){
Thread.sleep(20);
}
assertTrue("the SET_ACCEL_SENSITIVITY queued after the STOP was sent", radio.wrote(ShimmerObject.SET_ACCEL_SENSITIVITY_COMMAND));
}

/** The same configuration once the stop has finished: what Consensys does, seconds after a stop */
@Test
public void aConfigurationStartedAfterTheStopSendsEveryChunk() throws Exception {
AnsweringRadio radio = streamingShimmer3r();
mDevice.stopStreaming();
assertTrue("the STOP was sent", radio.stopSent.await(2, TimeUnit.SECONDS));
radio.queue(STREAM_DATA);
long end = System.currentTimeMillis()+3000;
while(!mDevice.getListofInstructions().isEmpty() && System.currentTimeMillis()<end){
Thread.sleep(20);
}
assertTrue("the stop was handled", mDevice.getListofInstructions().isEmpty());

configure();

List<Integer> chunks = waitForChunks(radio, ALL_THREE_CHUNKS.length, 5000);
assertEquals("the SET_INFOMEM chunks sent, by address", java.util.Arrays.asList(ALL_THREE_CHUNKS), chunks);
}
}
Loading