@@ -293,6 +293,13 @@ public int getNumCrcBytes() {
293293 }
294294 }
295295
296+ /**
297+ * With a link CRC on, the firmware sends one after a bare ACK too (the reply
298+ * to a SET command). It is the CRC of the single byte 0xFF: F4 65, low byte
299+ * first.
300+ */
301+ private static final byte [] BARE_ACK_CRC = ShimmerCrc .shimmerUartCrcCalc (new byte [] {ACK_COMMAND_PROCESSED }, 1 );
302+
296303 public enum SHIMMER_FEATURE {
297304 NONE ,
298305 RN4678_ERROR_DETECTION ,
@@ -958,6 +965,30 @@ protected void processPacket() {
958965 //Data packet followed by an ACK (suggesting an ACK in response to a SET BT command or else a BT response command)
959966 else if (bufferTemp [0 ]==DATA_PACKET
960967 && bufferTemp [getPacketSizeWithCrc ()+1 ]==ACK_COMMAND_PROCESSED ){
968+
969+ //With a link CRC on, a bare ACK is followed by its own CRC rather than
970+ //by the next packet
971+ if (isBareAckCrcSoFar (bufferTemp )){
972+ int bareAckEnd = getPacketSizeWithCrc ()+2 +mBtCommsCrcModeCurrent .getNumCrcBytes ();
973+ if (bufferTemp .length <bareAckEnd ){
974+ return ; //wait for the rest of the ACK's CRC
975+ }
976+
977+ //Firstly handle the data packet. If its CRC fails, only the packet is
978+ //dropped: the ACK's own CRC has already checked out.
979+ if (checkCrc (bufferTemp , getPacketSize ()+1 )){
980+ processDataPacket (bufferTemp );
981+ }
982+ else {
983+ printLogDataForDebugging ("CRC error in the data packet before an ACK, packet discarded" );
984+ }
985+ clearBytesFromStartOfBuffers (bufferTemp , bareAckEnd );
986+
987+ //Then handle the ACK from the last SET command
988+ processAckWhileStreaming ();
989+ return ;
990+ }
991+
961992 if (mByteArrayOutputStream .size ()>getPacketSizeWithCrc ()+2 ){
962993
963994 if (bufferTemp [getPacketSizeWithCrc ()+2 ]==DATA_PACKET ){
@@ -966,16 +997,7 @@ else if(bufferTemp[0]==DATA_PACKET
966997 clearSingleDataPacketFromBuffers (bufferTemp , getPacketSizeWithCrc ()+2 );
967998
968999 //Then handle the ACK from the last SET command
969- if (isKnownSetCommand (mCurrentCommand )){
970- stopTimerCheckForAckOrResp (); //cancel the ack timer
971- mWaitForAck =false ;
972-
973- processAckFromSetCommand (mCurrentCommand );
974-
975- mTransactionCompleted = true ;
976- setInstructionStackLock (false );
977- }
978- printLogDataForDebugging ("Ack Received for Command: \t \t \t " + btCommandToString (mCurrentCommand ));
1000+ processAckWhileStreaming ();
9791001 }
9801002
9811003 //this is for LogAndStream support, command is transmitted and ack received
@@ -1048,6 +1070,10 @@ private static boolean isKnownResponse(byte response) {
10481070 return mBtResponseMap .containsKey (response );
10491071 }
10501072
1073+ /**
1074+ * Checks the CRC of the data packet at the start of bufferTemp. It leaves the
1075+ * buffers alone, so the caller decides what to discard.
1076+ */
10511077 public boolean checkCrc (byte [] bufferTemp , int length ) {
10521078 byte [] crcCalc = ShimmerCrc .shimmerUartCrcCalc (bufferTemp , length );
10531079
@@ -1061,13 +1087,45 @@ public boolean checkCrc(byte[] bufferTemp, int length) {
10611087 if (mBtCommsCrcModeCurrent == BT_CRC_MODE .TWO_BYTE_CRC ) {
10621088 // + 2 as this is the location of the CRC's MSB
10631089 if (bufferTemp [getPacketSize () + 2 ] != crcCalc [1 ]) {
1064- discardBufferBytesToNextPacket ();
10651090 return false ;
10661091 }
10671092 }
10681093 return true ;
10691094 }
10701095
1096+ /**
1097+ * Whether the bytes received so far after the ACK that follows a data packet
1098+ * are the CRC on a bare ACK. Nothing else can start that way: an in-stream
1099+ * response starts 0x8A, a data packet 0x00, and no response opcode is 0xF4.
1100+ */
1101+ private boolean isBareAckCrcSoFar (byte [] bufferTemp ) {
1102+ int crcStart = getPacketSizeWithCrc ()+2 ;
1103+ int numCrcBytesReceived = Math .min (bufferTemp .length -crcStart , mBtCommsCrcModeCurrent .getNumCrcBytes ());
1104+ if (numCrcBytesReceived <=0 ){
1105+ return false ;
1106+ }
1107+ for (int i =0 ;i <numCrcBytesReceived ;i ++){
1108+ if (bufferTemp [crcStart +i ]!=BARE_ACK_CRC [i ]){
1109+ return false ;
1110+ }
1111+ }
1112+ return true ;
1113+ }
1114+
1115+ /** Handles a bare ACK received while streaming: the reply to the last SET command */
1116+ private void processAckWhileStreaming () {
1117+ if (isKnownSetCommand (mCurrentCommand )){
1118+ stopTimerCheckForAckOrResp (); //cancel the ack timer
1119+ mWaitForAck =false ;
1120+
1121+ processAckFromSetCommand (mCurrentCommand );
1122+
1123+ mTransactionCompleted = true ;
1124+ setInstructionStackLock (false );
1125+ }
1126+ printLogDataForDebugging ("Ack Received for Command: \t \t \t " + btCommandToString (mCurrentCommand ));
1127+ }
1128+
10711129 private void clearCrcBytesFromBuffer (byte responseCommand ) {
10721130 byte [] crcBytes = readBytes (mBtCommsCrcModeCurrent .numCrcBytes );
10731131 consolePrintLn ("Clearing CRC bytes:\t \t \t " + btCommandToString (responseCommand ) + " " + UtilShimmer .bytesToHexStringWithSpacesFormatted (crcBytes ));
@@ -1294,6 +1352,21 @@ protected void clearSingleDataPacketFromBuffers(byte[] bufferTemp, int packetSiz
12941352 }
12951353 }
12961354
1355+ /**
1356+ * Clears the first numBytes bytes from mByteArrayOutputStream, with their
1357+ * PC timestamps, and keeps any bytes after them
1358+ *
1359+ * @param bufferTemp the contents of mByteArrayOutputStream
1360+ * @param numBytes
1361+ */
1362+ private void clearBytesFromStartOfBuffers (byte [] bufferTemp , int numBytes ) {
1363+ mByteArrayOutputStream .reset ();
1364+ mByteArrayOutputStream .write (bufferTemp , numBytes , bufferTemp .length -numBytes );
1365+ if (mEnablePCTimeStamps ) {
1366+ mListofPCTimeStamps .subList (0 , Math .min (numBytes , mListofPCTimeStamps .size ())).clear ();
1367+ }
1368+ }
1369+
12971370 /**
12981371 *
12991372 */
0 commit comments