Skip to content

Commit 7c6786b

Browse files
committed
DEV-574 #time 1h better byte throwing handling and timestamp fail-safe
1 parent 56dffae commit 7c6786b

3 files changed

Lines changed: 58 additions & 13 deletions

File tree

ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ else if((mCurrentCommand==GET_FW_VERSION_COMMAND)
715715
}
716716
threadSleep((int)((Math.random()+.1)*100.0));
717717
writeBytes(insBytes);
718-
printLogDataForDebugging("Command Transmitted: \t\t\t" + btCommandToString(mCurrentCommand) + " " + UtilShimmer.bytesToHexStringWithSpacesFormatted(insBytes));
718+
printLogDataForDebugging("Command Transmitted: \t\t\t" + btCommandToString(mCurrentCommand) + ", len=" + insBytes.length + " " + UtilShimmer.bytesToHexStringWithSpacesFormatted(insBytes));
719719

720720
//TODO: are the two stops needed here? better to wait for ack from Shimmer
721721
if(mCurrentCommand==STOP_STREAMING_COMMAND
@@ -880,7 +880,7 @@ private void processNotStreamingWaitForResp() {
880880
mWaitForResponse=false;
881881
mTransactionCompleted=true;
882882
setInstructionStackLock(false);
883-
printLogDataForDebugging("Response Received:\t\t\t" + btCommandToString(responseCommand));
883+
printLogDataForDebugging("Response Received:\t\t\t" + btCommandToString(responseCommand) + ", len=" + byteBuffer.length + " " + UtilShimmer.bytesToHexStringWithSpacesFormatted(byteBuffer));
884884

885885
// Special case for FW_VERSION_RESPONSE because it
886886
// needs to initialize the Shimmer after releasing
@@ -939,7 +939,7 @@ protected void processPacket() {
939939
&& bufferTemp[getPacketSizeWithCrc()+1]==DATA_PACKET){
940940

941941
if (mBtCommsCrcModeCurrent != BT_CRC_MODE.OFF && !checkCrc(bufferTemp, getPacketSize() + 1)) {
942-
discardFirstBufferByte();
942+
discardBufferBytesToNextPacket();
943943
return;
944944
}
945945

@@ -1002,8 +1002,8 @@ else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTR
10021002
}
10031003
}
10041004
if(mByteArrayOutputStream.size()>getPacketSizeWithCrc()+2){
1005-
printLogDataForDebugging("Unknown packet error (check with JC):\tExpected: " + (getPacketSizeWithCrc()+2) + "bytes but buffer contains " + mByteArrayOutputStream.size() + "bytes");
1006-
discardFirstBufferByte(); //throw the first byte away
1005+
printLogDataForDebugging("Unknown packet error: \tExpected: " + (getPacketSizeWithCrc()+2) + "bytes but buffer contains " + mByteArrayOutputStream.size() + "bytes");
1006+
discardBufferBytesToNextPacket(); // Skip to start of next packet
10071007
}
10081008

10091009
}
@@ -1012,7 +1012,7 @@ else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTR
10121012
else {
10131013
printLogDataForDebugging("Packet syncing problem:\tExpected: " + (getPacketSizeWithCrc()+2) + "bytes. Buffer contains " + mByteArrayOutputStream.size() + "bytes"
10141014
+ "\nBuffer = " + UtilShimmer.bytesToHexStringWithSpacesFormatted(mByteArrayOutputStream.toByteArray()));
1015-
discardFirstBufferByte(); //throw the first byte away
1015+
discardBufferBytesToNextPacket(); // Skip to start of next packet
10161016
}
10171017
}
10181018

@@ -1052,7 +1052,7 @@ public boolean checkCrc(byte[] bufferTemp, int length) {
10521052
if (mBtCommsCrcModeCurrent == BT_CRC_MODE.TWO_BYTE_CRC) {
10531053
// + 2 as this is the location of the CRC's MSB
10541054
if (bufferTemp[getPacketSize() + 2] != crcCalc[1]) {
1055-
discardFirstBufferByte();
1055+
discardBufferBytesToNextPacket();
10561056
return false;
10571057
}
10581058
}
@@ -1190,6 +1190,10 @@ private void buildAndSendMsg(byte[] packet, COMMUNICATION_TYPE fwType, boolean t
11901190
ObjectCluster objectCluster = null;
11911191
try {
11921192
objectCluster = buildMsg(packet, fwType, timeSync, pcTimeStamp);
1193+
if(objectCluster.successfullyParsed==false){
1194+
printLogDataForDebugging("Packet timestamp is not contiguous - skipping:\t\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(packet));
1195+
return;
1196+
}
11931197
objectCluster = systemTimestampPlot.processSystemTimestampPlot(objectCluster);
11941198
} catch (Exception e) {
11951199
e.printStackTrace();
@@ -1294,16 +1298,34 @@ protected void clearBuffers() {
12941298
}
12951299

12961300
/**
1297-
*
1301+
* Next packet start should begin with DATA_PACKET or ACK_COMMAND_PROCESSED byte so skip to that point
12981302
*/
1299-
protected void discardFirstBufferByte(){
1303+
protected void discardBufferBytesToNextPacket(){
13001304
byte[] bTemp = mByteArrayOutputStream.toByteArray();
1305+
1306+
//Find index of first DATA_PACKET or ACK byte within the buffer
1307+
int offset = findOffsetOfNextZeroOrFF(bTemp);
1308+
//If not found, just skip one byte
1309+
offset = (offset == -1) ? 1 : offset;
1310+
13011311
mByteArrayOutputStream.reset();
1302-
mByteArrayOutputStream.write(bTemp, 1, bTemp.length-1); //this will throw the first byte away
1312+
mByteArrayOutputStream.write(bTemp, offset, bTemp.length-offset); //this will throw the first byte away
13031313
if(mEnablePCTimeStamps) {
1304-
mListofPCTimeStamps.remove(0);
1314+
for (int i=0;i<offset;i++)
1315+
{
1316+
mListofPCTimeStamps.remove(0);
1317+
}
1318+
}
1319+
consolePrintLn("Throw Bytes " + UtilShimmer.bytesToHexStringWithSpacesFormatted(Arrays.copyOfRange(bTemp, 0, offset)));
1320+
}
1321+
1322+
private static int findOffsetOfNextZeroOrFF(byte[] a) {
1323+
for (int i = 1; i < a.length; i++) {
1324+
byte b = a[i];
1325+
if (b == 0 || b == (byte) 0xFF)
1326+
return i;
13051327
}
1306-
consolePrintLn("Throw Byte" + UtilShimmer.bytesToHexStringWithSpacesFormatted(bTemp));
1328+
return -1;
13071329
}
13081330

13091331
/**

ShimmerDriver/src/main/java/com/shimmerresearch/driver/ObjectCluster.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ public enum OBJECTCLUSTER_TYPE{
156156
public int mIndexCal = 0;
157157
public int mIndexUncal = 0;
158158
public boolean mEnableArraysDataStructure = false;
159+
160+
public boolean successfullyParsed = true;
159161

160162
public class SensorDataPerType {
161163

ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ public class BTStream {
494494
public static final int MAX_NUMBER_OF_SIGNALS = 77;//50; //used to be 11 but now 13 because of the SR30 + 8 for 3d orientation
495495
public static final int MAX_INQUIRY_PACKET_SIZE = 47;
496496

497+
public static final int CONTIGUOUS_TIMESTAMP_TICKS_LIMIT = (10*32768); // 10 seconds worth of ticks
498+
497499
public enum TEST_MODE {
498500
MAIN_TEST((byte)0, "Main Test"),
499501
LED_TEST((byte)1, "LED Test"),
@@ -786,6 +788,8 @@ public String toString() {
786788

787789
// ---------- GSR end ------------------
788790

791+
private double mLastReceivedTimeStampTicks = 0;
792+
789793
public ObjectCluster setLSLTimeIfAvailable(ObjectCluster ojc) {
790794
return ojc;
791795
}
@@ -923,7 +927,24 @@ public ObjectCluster buildMsg(byte[] newPacket, COMMUNICATION_TYPE fwType, boole
923927

924928
if (getHardwareVersion()==HW_ID.SHIMMER_SR30 || getHardwareVersion()==HW_ID.SHIMMER_3 || getHardwareVersion()==HW_ID.SHIMMER_3R
925929
|| getHardwareVersion()==HW_ID.SHIMMER_GQ_802154_LR || getHardwareVersion()==HW_ID.SHIMMER_GQ_802154_NR || getHardwareVersion()==HW_ID.SHIMMER_2R_GQ){
926-
930+
931+
/*
932+
* Even with checking for start and stop of packets while parsing and performing
933+
* CRC checks, some corrupted packets can still slip through. This is a final
934+
* check help to make sure the time stamp ticks are contiguous before parsing the
935+
* packet.
936+
*/
937+
if (fwType == COMMUNICATION_TYPE.BLUETOOTH)
938+
{
939+
double shimmerTimestampTicks = (double)newPacketInt[getSignalIndex(Configuration.Shimmer3.ObjectClusterSensorName.TIMESTAMP)];
940+
if(mLastReceivedTimeStampTicks!=0 && Math.abs(shimmerTimestampTicks - mLastReceivedTimeStampTicks) > CONTIGUOUS_TIMESTAMP_TICKS_LIMIT) {
941+
mLastReceivedTimeStampTicks = shimmerTimestampTicks;
942+
objectCluster.successfullyParsed = false;
943+
return objectCluster; //discard packet
944+
}
945+
mLastReceivedTimeStampTicks = shimmerTimestampTicks;
946+
}
947+
927948
parseTimestampShimmer3(fwType, objectCluster, uncalibratedData, uncalibratedDataUnits, calibratedData, calibratedDataUnits, sensorNames, newPacketInt);
928949

929950
//OFFSET

0 commit comments

Comments
 (0)