Skip to content
Open
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 @@ -6,6 +6,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.ArrayUtils;

Expand Down Expand Up @@ -58,6 +60,12 @@ public abstract class AbstractCommsProtocolWired extends BasicProcessWithCallBac
// private List<UartRxPacketObject> mListOfUartRxPacketObjects = new ArrayList<UartRxPacketObject>();
private List<UartRxPacketObject> mListOfUartRxPacketObjects = Collections.synchronizedList(new ArrayList<UartRxPacketObject>());
public DockException mThrownException = null;
/** A READ burst being collected (readBurst()): its packets, or the DockException the reader
* hit on one, go here as they arrive rather than to mListOfUartRxPacketObjects */
private volatile LinkedBlockingQueue<Object> mBurstQueue = null;
private volatile byte mBurstComponent;
private volatile byte mBurstDataProperty;
private volatile byte mBurstEndProperty;
// private UartRxCallback mUartRxCallback = null;
private boolean mSendCallbackRxOverride = false;

Expand Down Expand Up @@ -369,9 +377,11 @@ protected byte[] shimmerUartSetMemCommand(UartComponentPropertyDetails msgArg, i
*/
protected byte[] shimmerUartCommandTxRx(UART_PACKET_CMD packetCmd, UartComponentPropertyDetails msgArg, byte[] payload) throws DockException {
mListOfUartRxPacketObjects.clear();
// Before the request goes, as the NoWait commands do: a NACK can be back and parsed
// before txPacket() returns, and clearing after it lost that NACK to a timeout
mThrownException = null;

txPacket(packetCmd, msgArg, payload);
mThrownException = null;
return waitForResponse(packetCmd, msgArg);
}

Expand Down Expand Up @@ -635,8 +645,24 @@ public void serialPortRxEvent(int eventLength){
}
}

//TODO not setup to handle streaming via dock connector -> add additional capability?
/**
* Every packet in a read, one a pass. A loop and not a call per packet: a read can
* hold thousands - a NeuroLynQ node's READ burst sends 4 KB a page, and a read taken
* after the reader has been held up holds all that arrived meanwhile - and a call per
* packet, or per byte of noise, ran the reader's thread out of stack (DEV-1061).
*/
private void processRxBuf(byte[] rxBuf) throws ShimmerException {
byte[] next = rxBuf;
while(next.length>0){
processFirstPacket(next);
// What is left of the read after that packet
next = carriedRxBuf;
carriedRxBuf = new byte[]{};
}
}

//TODO not setup to handle streaming via dock connector -> add additional capability?
private void processFirstPacket(byte[] rxBuf) throws ShimmerException {

byte headerByte = rxBuf[0];
if(headerByte==UartPacketDetails.PACKET_HEADER.toCharArray()[0]){
Expand Down Expand Up @@ -726,6 +752,10 @@ else if(rxBuf.length<expectedResponseLength){
parseSinglePacket(packet, timestampMs);
} catch (DockException de) {
mThrownException = de;
LinkedBlockingQueue<Object> burst = mBurstQueue;
if(burst!=null){
burst.offer(de);
}

if(currentPacketCmd!=null && currentMsgArg!=null){
System.out.println(mUniqueId + "\tProblem parsing received packet while waiting for:");
Expand All @@ -746,14 +776,7 @@ else if(rxBuf.length<expectedResponseLength){
//remove first and add remaining bytes to start of next serial port read
carriedRxBuf = removeFirstByteFromArray(rxBuf);
}

// Attempt to re-process any remaining bytes
if(carriedRxBuf.length>0){
byte[] tempBuf = carriedRxBuf;
carriedRxBuf = new byte[]{};
processRxBuf(tempBuf);
}

// Any bytes left in carriedRxBuf are processRxBuf()'s next pass
}

/** */
Expand Down Expand Up @@ -787,7 +810,11 @@ private void processParsedPacket(UartRxPacketObject uRPO, long timestampMs) thro
throw de;
}

if(mSendCallbackRxOverride){
LinkedBlockingQueue<Object> burst = mBurstQueue;
if(burst!=null && isBurstPacket(uRPO)){
burst.offer(uRPO);
}
else if(mSendCallbackRxOverride){
wrapMsgSpanAndSend(MsgDock.MSG_ID_SHIMMERUART_PACKET_RX, uRPO);
}
else {
Expand All @@ -805,6 +832,86 @@ private void processParsedPacket(UartRxPacketObject uRPO, long timestampMs) thro
}
}

private boolean isBurstPacket(UartRxPacketObject uRPO) {
return uRPO.mUartCommandByte == UartPacketDetails.UART_PACKET_CMD.DATA_RESPONSE.toCmdByte()
&& uRPO.mUartComponentByte == mBurstComponent
&& (uRPO.mUartPropertyByte == mBurstDataProperty || uRPO.mUartPropertyByte == mBurstEndProperty);
}

/** What a READ burst brought: its packets in order, the end frame last if it came */
public static final class ReadBurst {
public final List<UartRxPacketObject> packets;
/** The end frame arrived; otherwise the burst stalled or ran out of time */
public final boolean ended;
/** Frames lost to a `$` CRC that did not check: their bytes are to be asked for again */
public final int crcErrors;

public ReadBurst(List<UartRxPacketObject> packets, boolean ended, int crcErrors) {
this.packets = Collections.unmodifiableList(packets);
this.ended = ended;
this.crcErrors = crcErrors;
}
}

/**
* Send a READ and collect the burst that answers it: every data frame with the
* request's component and property, until the one with {@code endProperty}, nothing
* for {@code stallMs}, or {@code totalMs} gone (DEV-1061, a NeuroLynQ node's storage).
* The frames go to a queue as the reader parses them, not to the list other requests
* poll every 100 ms, so a burst is read as fast as it arrives. A frame whose CRC fails
* is counted and lost; a NACK throws, as it does for any request.
*/
public ReadBurst readBurst(UartComponentPropertyDetails request, byte[] payload, byte endProperty,
long stallMs, long totalMs, int errorCode) throws DockException {
if(!mLeavePortOpen) openSafely();
LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();
List<UartRxPacketObject> packets = new ArrayList<UartRxPacketObject>();
int crcErrors = 0;
boolean ended = false;
mListOfUartRxPacketObjects.clear();
mThrownException = null;
mBurstComponent = request.mComponentByte;
mBurstDataProperty = request.mPropertyByte;
mBurstEndProperty = endProperty;
mBurstQueue = queue;
try {
txPacket(UartPacketDetails.UART_PACKET_CMD.READ, request, payload);
long deadline = System.currentTimeMillis() + totalMs;
for(;;) {
long wait = Math.min(stallMs, deadline - System.currentTimeMillis());
if(wait <= 0) {
break;
}
Object next = queue.poll(wait, TimeUnit.MILLISECONDS);
if(next == null) {
break;
}
if(next instanceof DockException) {
DockException de = (DockException) next;
if(de.mErrorCodeLowLevel == ErrorCodesWiredProtocol.SHIMMERUART_COMM_ERR_CRC) {
crcErrors++;
continue;
}
de.mErrorCode = errorCode;
throw de;
}
UartRxPacketObject uRPO = (UartRxPacketObject) next;
packets.add(uRPO);
if(uRPO.mUartPropertyByte == endProperty) {
ended = true;
break;
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new DockException(mComPort, errorCode, ErrorCodesWiredProtocol.SHIMMERUART_COMM_ERR_TIMEOUT, mUniqueId);
} finally {
mBurstQueue = null;
if(!mLeavePortOpen) closeSafely();
}
return new ReadBurst(packets, ended, crcErrors);
}

/** remove first and add remaining bytes to start of next serial port read
* @return */
private byte[] removeFirstByteFromArray(byte[] rxBuf){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class ErrorCodesWiredProtocol extends AbstractErrorCodes {
public final static int SHIMMERUART_CMD_ERR_BT_FW_VERSION_INFO_GET = (ERROR_CODES_ID*1000) + 37;
public final static int SHIMMERUART_CMD_ERR_ENTER_BOOTLOADER_SET = (ERROR_CODES_ID*1000) + 38;

// A NeuroLynQ node's storage component (DEV-1061)
public final static int SHIMMERUART_CMD_ERR_STORAGE_INFO_GET = (ERROR_CODES_ID*1000) + 40;
public final static int SHIMMERUART_CMD_ERR_STORAGE_SESSION_GET = (ERROR_CODES_ID*1000) + 41;
public final static int SHIMMERUART_CMD_ERR_STORAGE_FILES_GET = (ERROR_CODES_ID*1000) + 42;
public final static int SHIMMERUART_CMD_ERR_STORAGE_READ = (ERROR_CODES_ID*1000) + 43;
public final static int SHIMMERUART_CMD_ERR_STORAGE_ERASE_SET = (ERROR_CODES_ID*1000) + 44;
public final static int SHIMMERUART_CMD_ERR_STORAGE_FORMAT_SET = (ERROR_CODES_ID*1000) + 45;

//SR7 (802.15.4 radio) related errors
public final static int SHIMMERUART_CMD_ERR_RADIO_802154_SET_SETTINGS = (ERROR_CODES_ID*1000) + 33;
public final static int SHIMMERUART_CMD_ERR_RADIO_802154_GET_SETTINGS = (ERROR_CODES_ID*1000) + 34;
Expand Down Expand Up @@ -79,6 +87,13 @@ public class ErrorCodesWiredProtocol extends AbstractErrorCodes {
aMap.put(SHIMMERUART_CMD_ERR_BT_FW_VERSION_INFO_GET, "Error reading BT FW version info");
aMap.put(SHIMMERUART_CMD_ERR_ENTER_BOOTLOADER_SET, "Error writing enter bootloader command");

aMap.put(SHIMMERUART_CMD_ERR_STORAGE_INFO_GET, "Error reading the node's storage information");
aMap.put(SHIMMERUART_CMD_ERR_STORAGE_SESSION_GET, "Error reading a session from the node's storage");
aMap.put(SHIMMERUART_CMD_ERR_STORAGE_FILES_GET, "Error reading a session's files from the node's storage");
aMap.put(SHIMMERUART_CMD_ERR_STORAGE_READ, "Error reading a file from the node's storage");
aMap.put(SHIMMERUART_CMD_ERR_STORAGE_ERASE_SET, "Error erasing sessions from the node's storage");
aMap.put(SHIMMERUART_CMD_ERR_STORAGE_FORMAT_SET, "Error formatting the node's storage");

//SR7 (802.15.4 radio) related errors
aMap.put(SHIMMERUART_CMD_ERR_RADIO_802154_GET_SETTINGS, "Error reading 802.15.4 radio settings");
aMap.put(SHIMMERUART_CMD_ERR_RADIO_802154_SET_SETTINGS, "Error writing 802.15.4 radio settings");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public static enum UART_COMPONENT {
BEACON ((byte)0x08),
RADIO_802154 ((byte)0x09),
RADIO_BLUETOOTH ((byte)0x0A),
TEST ((byte)0x0B);
TEST ((byte)0x0B),
/** A NeuroLynQ node's session storage (DEV-1061). A GQ answers it BAD_CMD. */
STORAGE ((byte)0x0C);

private final byte command;

Expand Down Expand Up @@ -93,6 +95,9 @@ public byte toCmdByte() {
public static List<ShimmerVerObject> listOfCompatibleVersionInfoGq = Arrays.asList(svoGqBle, svoGq802154NR, svoGq802154LR, svoGq802154Shimmer2r);
public static List<ShimmerVerObject> listOfCompatibleVersionInfoTest = Arrays.asList(svoS3Test, svoS3RTest);
public static List<ShimmerVerObject> listOfCompatibleVersionInfoUsbDfu = Arrays.asList(svoS3RUsbComms);
/** A NeuroLynQ node: Verisense hardware running the GQ 802.15.4 firmware (DEV-1061) */
public static ShimmerVerObject svoNeuroLynQNode = new ShimmerVerObject(ShimmerVerDetails.ANY_VERSION,FW_ID.GQ_802154,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION);
public static List<ShimmerVerObject> listOfCompatibleVersionInfoNeuroLynQNode = Arrays.asList(svoNeuroLynQNode);

/** Class listing all of the components and property combinations that can be used with the Shimmer UART commands */
public static class UART_COMPONENT_AND_PROPERTY {
Expand Down Expand Up @@ -152,6 +157,27 @@ public static class BLUETOOTH {
public static final UartComponentPropertyDetails VER = new UartComponentPropertyDetails(UART_COMPONENT.RADIO_BLUETOOTH, 0x03, PERMISSION.READ_ONLY, null, "BT_FW_VER");
}

/**
* A NeuroLynQ node's session storage (DEV-1061): verisense-firmware
* docs/VERISENSE_NEUROLYNQ_STORAGE.md section 7, and neurolynq.NeuroLynQStorageCodec for the bytes.
*/
public static class STORAGE {
/** 36 bytes: state, geometry, capacity, sessions */
public static final UartComponentPropertyDetails INFO = new UartComponentPropertyDetails(UART_COMPONENT.STORAGE, 0x00, PERMISSION.READ_ONLY, listOfCompatibleVersionInfoNeuroLynQNode, "STORAGE_INFO");
/** GET idx u16: one session, oldest first */
public static final UartComponentPropertyDetails SESSION = new UartComponentPropertyDetails(UART_COMPONENT.STORAGE, 0x01, PERMISSION.READ_ONLY, listOfCompatibleVersionInfoNeuroLynQNode, "STORAGE_SESSION");
/** GET handle u16, first u8, max u8: up to 40 {file, size} */
public static final UartComponentPropertyDetails FILES = new UartComponentPropertyDetails(UART_COMPONENT.STORAGE, 0x02, PERMISSION.READ_ONLY, listOfCompatibleVersionInfoNeuroLynQNode, "STORAGE_FILES");
/** GET handle, file, offset u32, len u32: answered by a burst of these, then END */
public static final UartComponentPropertyDetails READ = new UartComponentPropertyDetails(UART_COMPONENT.STORAGE, 0x03, PERMISSION.READ_ONLY, listOfCompatibleVersionInfoNeuroLynQNode, "STORAGE_READ");
/** The frame that ends a READ burst. Never requested. */
public static final UartComponentPropertyDetails END = new UartComponentPropertyDetails(UART_COMPONENT.STORAGE, 0x04, PERMISSION.READ_ONLY, listOfCompatibleVersionInfoNeuroLynQNode, "STORAGE_END");
/** SET scope u8, handle u16, 'ERAS' */
public static final UartComponentPropertyDetails ERASE = new UartComponentPropertyDetails(UART_COMPONENT.STORAGE, 0x05, PERMISSION.WRITE_ONLY, listOfCompatibleVersionInfoNeuroLynQNode, "STORAGE_ERASE");
/** SET mode u8, 'FRMT': ACKed at once, progress in INFO */
public static final UartComponentPropertyDetails FORMAT = new UartComponentPropertyDetails(UART_COMPONENT.STORAGE, 0x06, PERMISSION.WRITE_ONLY, listOfCompatibleVersionInfoNeuroLynQNode, "STORAGE_FORMAT");
}

public static class DEVICE_TEST {
public static final UartComponentPropertyDetails MAIN_TEST = new UartComponentPropertyDetails(UART_COMPONENT.TEST, 0x00, PERMISSION.WRITE_ONLY, listOfCompatibleVersionInfoTest, "Main Test");
public static final UartComponentPropertyDetails LED_TEST = new UartComponentPropertyDetails(UART_COMPONENT.TEST, 0x01, PERMISSION.WRITE_ONLY, listOfCompatibleVersionInfoTest, "LED Test");
Expand Down
Loading