From 9601e0439fcac806affdf4c95e8d6ca5975af9c0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Phil=C3=A9mon=20Pr=C3=A9vot?= <philemon.prevot@gmail.com>
Date: Mon, 2 Sep 2024 15:37:36 +0200
Subject: [PATCH] Get rid of checksum (there are none on Device Mode samples)

---
 src/filewriter.cpp | 55 +++++++++-------------------------------------
 src/filewriter.h   |  2 +-
 2 files changed, 11 insertions(+), 46 deletions(-)

diff --git a/src/filewriter.cpp b/src/filewriter.cpp
index f69c5b8..91b4942 100644
--- a/src/filewriter.cpp
+++ b/src/filewriter.cpp
@@ -150,21 +150,6 @@ float IMUFileWriter::GetFloatSafe(const unsigned char *p, int index) {
     return result;
 }
 
-unsigned char IMUFileWriter::CalculateChecksum(int msgFunction,
-                int msgPayloadLength, const unsigned char msgPayload[])
-{
-    unsigned char checksum = 0;
-    checksum ^= static_cast<unsigned char>(0xFE);
-    checksum ^= static_cast<unsigned char>(msgFunction >> 8);
-    checksum ^= static_cast<unsigned char>(msgFunction >> 0);
-    checksum ^= static_cast<unsigned char>(msgPayloadLength >> 8);
-    checksum ^= static_cast<unsigned char>(msgPayloadLength >> 0);
-    for (int i = 0; i < msgPayloadLength; i++) {
-        checksum ^= msgPayload[i];
-    }
-    return checksum;
-}
-
 void IMUFileWriter::DecodeMessage(unsigned char c) {
     switch (rcvState) {
         case StateReception::Waiting:
@@ -198,7 +183,7 @@ void IMUFileWriter::DecodeMessage(unsigned char c) {
                     rcvState = StateReception::Waiting;
                 }
             } else
-                rcvState = StateReception::CheckSum;
+                rcvState = StateReception::Decode;
             break;
         case StateReception::Payload:
             if (msgDecodedPayloadIndex > msgDecodedPayloadLength)
@@ -210,21 +195,15 @@ void IMUFileWriter::DecodeMessage(unsigned char c) {
             msgDecodedPayload[msgDecodedPayloadIndex++] = c;
             if (msgDecodedPayloadIndex >= msgDecodedPayloadLength)
             {
-                rcvState = StateReception::CheckSum;
+                rcvState = StateReception::Decode;
                 msgDecodedPayloadIndex = 0;
             }
             break;
-        case StateReception::CheckSum:
+        case StateReception::Decode:
         {
-            unsigned char calculatedChecksum = CalculateChecksum(msgDecodedFunction, msgDecodedPayloadLength, msgDecodedPayload);
-            unsigned char receivedChecksum = c;
-            if (calculatedChecksum == receivedChecksum) {
-                //Lance l'event de fin de decodage
-                ProcessDecodedMessage(msgDecodedFunction, msgDecodedPayloadLength, msgDecodedPayload);
-                msgDecoded++;
-            } else {
-                std::cerr << "Checksum error" << std::endl;
-            }
+            //Lance l'event de fin de decodage
+            ProcessDecodedMessage(msgDecodedFunction, msgDecodedPayloadLength, msgDecodedPayload);
+            msgDecoded++;
             rcvState = StateReception::Waiting;
         }
         break;
@@ -477,27 +456,13 @@ void IMUFileWriter::ProcessDecodedMessage(int msgFunction, int msgPayloadLength,
 void IMUFileWriter::write(uint8_t *sample, size_t size, uint8_t *imu_data) {
     uint8_t *imu_data_cur(imu_data);
 
-    uint8_t softwareMajorRev=sample[5];
+    uint8_t softwareMajorRev=sample[0];
     uint8_t softwareMinorRev=sample[6];
-    std::cerr << "sMR" << static_cast<int>(sample[10]) << std::endl;
+    std::cerr << "sMR" << static_cast<int>(sample[0]) << std::endl;
     std::cerr << "Size : " << size << std::endl;
     
-    uint64_t timeStamp100MHzCurrentPacket=0;
-    if(true) {
-        //On recupere l'instant de fin du paquet courant
-        timeStamp100MHzCurrentPacket=BUILD_UINT64(sample[14],
-                    sample[13],
-                    sample[12],
-                    sample[11],
-                    sample[10],
-                    sample[9],
-                    sample[8],
-                    sample[7]);
-        timeStamp100MHzCurrentPacket*=10;     //To put the ts in ns
-        uint8_t enteteSize=16;
-
-        outfile << "PACKET TIMESTAMP: " << static_cast<int>(timeStamp100MHzCurrentPacket) << std::endl;
-        for(int i=enteteSize;i<size-enteteSize; i++)
+    if(softwareMajorRev > 2) {
+        for(int i=1; i<size-enteteSize; i++)
         {
             DecodeMessage(sample[i]);
         }
diff --git a/src/filewriter.h b/src/filewriter.h
index 23f85bb..a702af1 100644
--- a/src/filewriter.h
+++ b/src/filewriter.h
@@ -203,7 +203,7 @@ private:
         PayloadLengthMSB,
         PayloadLengthLSB,
         Payload,
-        CheckSum
+        Decode
     };
 
     StateReception rcvState;
-- 
GitLab