@@ -1052,22 +1052,34 @@ else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTR
10521052 printLogDataForDebugging ("COMMAND TXed and ACK RECEIVED IN STREAM" );
10531053 printLogDataForDebugging ("INS CMD RESP" );
10541054
1055- //Firstly handle the in-stream response
1056- stopTimerCheckForAckOrResp (); //cancel the ack timer
1057- mWaitForResponse =false ;
1058- mWaitForAck =false ;
1055+ // The firmware also sends a status response unprompted (on a dock or
1056+ // undock, the user button, a low-battery stop), so this completes the
1057+ // command in flight only if the response is that command's reply
1058+ byte [] inStreamResponseCommand = readBytes (1 , INSTREAM_CMD_RESPONSE );
1059+ boolean isReplyToCommandInFlight = inStreamResponseCommand !=null
1060+ && isReplyToCommandInFlight (inStreamResponseCommand [0 ]);
1061+ if (isReplyToCommandInFlight ){
1062+ stopTimerCheckForAckOrResp (); //cancel the ack timer
1063+ mWaitForResponse =false ;
1064+ mWaitForAck =false ;
1065+ }
10591066
1060- processInstreamResponse (true );
1067+ //Firstly handle the in-stream response
1068+ if (inStreamResponseCommand !=null ){
1069+ processInstreamResponse (inStreamResponseCommand [0 ], true );
1070+ }
10611071
1062- // Need to remove here because it is an
1063- // in-stream response while streaming so not
1064- // handled elsewhere
1065- if (getListofInstructions ().size ()>0 ){
1066- removeInstruction (0 );
1072+ if (isReplyToCommandInFlight ){
1073+ // Need to remove here because it is an
1074+ // in-stream response while streaming so not
1075+ // handled elsewhere
1076+ if (getListofInstructions ().size ()>0 ){
1077+ removeInstruction (0 );
1078+ }
1079+
1080+ mTransactionCompleted =true ;
1081+ setInstructionStackLock (false );
10671082 }
1068-
1069- mTransactionCompleted =true ;
1070- setInstructionStackLock (false );
10711083
10721084 //Then process the Data packet
10731085 processDataPacket (bufferTemp );
@@ -1203,76 +1215,94 @@ else if(currentCommand==GET_INFOMEM_COMMAND || currentCommand==GET_CALIB_DUMP_CO
12031215 }
12041216 }
12051217
1218+ /**
1219+ * Whether an in-stream response is the reply to the command in flight. The
1220+ * protocol matches a reply to its command by the response opcode, never by the
1221+ * ACK (log-and-stream-common docs/SHIMMER3_BT_COMMUNICATION_PROTOCOL.md
1222+ * section 5.4, rule 2).
1223+ */
1224+ private boolean isReplyToCommandInFlight (byte inStreamResponseCommand ) {
1225+ if (!mWaitForAck && !mWaitForResponse ){
1226+ return false ;
1227+ }
1228+ BtCommandDetails commandDetails = mBtGetCommandMap .get (mCurrentCommand );
1229+ return commandDetails !=null && commandDetails .expectedResponse ==inStreamResponseCommand ;
1230+ }
1231+
12061232 /** process responses to in-stream response */
12071233 protected void processInstreamResponse (boolean shouldClearCrcFromBuffer ) {
1208- boolean responseWasParsed = true ;
1209-
12101234 byte [] inStreamResponseCommandBuffer = readBytes (1 , INSTREAM_CMD_RESPONSE );
12111235 if (inStreamResponseCommandBuffer !=null ){
1212- byte inStreamResponseCommand = inStreamResponseCommandBuffer [0 ];
1213- consolePrintLn ("In-stream received = " + btCommandToString (inStreamResponseCommand ));
1236+ processInstreamResponse (inStreamResponseCommandBuffer [0 ], shouldClearCrcFromBuffer );
1237+ }
1238+ }
12141239
1215- if (inStreamResponseCommand ==DIR_RESPONSE ){
1216- byte [] responseData = readBytes (1 , inStreamResponseCommand );
1217- if (responseData !=null ){
1218- int directoryNameLength = responseData [0 ];
1219- byte [] bufferDirectoryName = new byte [directoryNameLength ];
1220- bufferDirectoryName = readBytes (directoryNameLength , inStreamResponseCommand );
1221- if (bufferDirectoryName !=null ){
1222- String tempDirectory = new String (bufferDirectoryName );
1223- mDirectoryName = tempDirectory ;
1224- printLogDataForDebugging ("Directory Name = " + mDirectoryName );
1225- }
1240+ /** process an in-stream response whose response command has already been read */
1241+ protected void processInstreamResponse (byte inStreamResponseCommand , boolean shouldClearCrcFromBuffer ) {
1242+ boolean responseWasParsed = true ;
1243+
1244+ consolePrintLn ("In-stream received = " + btCommandToString (inStreamResponseCommand ));
1245+
1246+ if (inStreamResponseCommand ==DIR_RESPONSE ){
1247+ byte [] responseData = readBytes (1 , inStreamResponseCommand );
1248+ if (responseData !=null ){
1249+ int directoryNameLength = responseData [0 ];
1250+ byte [] bufferDirectoryName = new byte [directoryNameLength ];
1251+ bufferDirectoryName = readBytes (directoryNameLength , inStreamResponseCommand );
1252+ if (bufferDirectoryName !=null ){
1253+ String tempDirectory = new String (bufferDirectoryName );
1254+ mDirectoryName = tempDirectory ;
1255+ printLogDataForDebugging ("Directory Name = " + mDirectoryName );
12261256 }
12271257 }
1228- else if ( inStreamResponseCommand == STATUS_RESPONSE ){
1229- int statusBytesToRead = 0 ;
1230- if ( isSupportedUSBPluggedInStatus ()) {
1231- statusBytesToRead = 2 ;
1232- } else {
1233- statusBytesToRead = 1 ;
1234- }
1235- byte [] responseData = readBytes ( statusBytesToRead , inStreamResponseCommand );
1236- if ( responseData != null ){
1237- parseStatusByte (responseData );
1238-
1239- if (! isSupportedRtcStateInStatus ()){
1240- if (!mIsSensing && ! isInitialised ()){
1241- writeRealTimeClock ();
1242- }
1243- } else {
1244- //New case to make sure RTC is set if it hasn't been already
1245- if (! isSDLogging () && (! isInitialised () || ! mIsRtcSet )){
1246- writeRealTimeClock ();
1247- }
1258+ }
1259+ else if ( inStreamResponseCommand == STATUS_RESPONSE ){
1260+ int statusBytesToRead = 0 ;
1261+ if ( isSupportedUSBPluggedInStatus ()) {
1262+ statusBytesToRead = 2 ;
1263+ } else {
1264+ statusBytesToRead = 1 ;
1265+ }
1266+ byte [] responseData = readBytes ( statusBytesToRead , inStreamResponseCommand );
1267+ if (responseData != null ){
1268+ parseStatusByte ( responseData );
1269+
1270+ if (!isSupportedRtcStateInStatus ()){
1271+ if (! mIsSensing && ! isInitialised ()){
1272+ writeRealTimeClock ();
1273+ }
1274+ } else {
1275+ //New case to make sure RTC is set if it hasn't been already
1276+ if (! isSDLogging () && (! isInitialised () || ! mIsRtcSet )){
1277+ writeRealTimeClock ();
12481278 }
1249-
1250- eventLogAndStreamStatusChanged (mCurrentCommand );
1251- }
1252- }
1253- else if (inStreamResponseCommand ==VBATT_RESPONSE ) {
1254- byte [] responseData = readBytes (3 , inStreamResponseCommand );
1255- if (responseData !=null ){
1256- ShimmerBattStatusDetails battStatusDetails = new ShimmerBattStatusDetails (((responseData [1 ]&0xFF )<<8 )+(responseData [0 ]&0xFF ),responseData [2 ]);
1257- setBattStatusDetails (battStatusDetails );
1258- printLogDataForDebugging ("Battery Status:"
1259- + "\t Voltage=" + battStatusDetails .getBattVoltageParsed ()
1260- + "\t Charging status=" + battStatusDetails .getChargingStatusParsed ()
1261- + "\t Batt %=" + battStatusDetails .getEstimatedChargePercentageParsed ());
12621279 }
1280+
1281+ eventLogAndStreamStatusChanged (mCurrentCommand );
12631282 }
1264- else
1265- {
1266- responseWasParsed = false ;
1267- }
1268-
1269- if ( shouldClearCrcFromBuffer && mBtCommsCrcModeCurrent != BT_CRC_MODE . OFF && responseWasParsed ) {
1270- // The driver doesn't currently support checking of CRC bytes on any response
1271- // other then data packets but we still need to clear them from the buffer if
1272- // CRC is enabled.
1273- clearCrcBytesFromBuffer ( inStreamResponseCommand );
1283+ }
1284+ else if ( inStreamResponseCommand == VBATT_RESPONSE ) {
1285+ byte [] responseData = readBytes ( 3 , inStreamResponseCommand ) ;
1286+ if ( responseData != null ){
1287+ ShimmerBattStatusDetails battStatusDetails = new ShimmerBattStatusDetails ((( responseData [ 1 ]& 0xFF )<< 8 )+( responseData [ 0 ]& 0xFF ), responseData [ 2 ]);
1288+ setBattStatusDetails ( battStatusDetails );
1289+ printLogDataForDebugging ( "Battery Status:"
1290+ + " \t Voltage=" + battStatusDetails . getBattVoltageParsed ()
1291+ + " \t Charging status=" + battStatusDetails . getChargingStatusParsed ()
1292+ + " \t Batt %=" + battStatusDetails . getEstimatedChargePercentageParsed () );
12741293 }
12751294 }
1295+ else
1296+ {
1297+ responseWasParsed = false ;
1298+ }
1299+
1300+ if (shouldClearCrcFromBuffer && mBtCommsCrcModeCurrent != BT_CRC_MODE .OFF && responseWasParsed ) {
1301+ // The driver doesn't currently support checking of CRC bytes on any response
1302+ // other then data packets but we still need to clear them from the buffer if
1303+ // CRC is enabled.
1304+ clearCrcBytesFromBuffer (inStreamResponseCommand );
1305+ }
12761306
12771307 }
12781308
0 commit comments