Przeglądaj źródła

better description of transport fsm + some refactoring

Fischer, Simon 3 lat temu
rodzic
commit
5abe15c415
1 zmienionych plików z 36 dodań i 12 usunięć
  1. 36 12
      src/shellmatta_transport.c

+ 36 - 12
src/shellmatta_transport.c

@@ -7,7 +7,6 @@
 #include "shellmatta_transport.h"
 #include "shellmatta_crc.h"
 #include <string.h>
-#include <stdio.h>
 
 /* init global variables */
 uint8_t protocolVersion                     = 0u;
@@ -72,14 +71,18 @@ shellmatta_retCode_t shellmatta_reset_transport()
 /**
  * @brief       processes the passed amount of data
  * @param[in]   data    pointer to input data to process
- * @return      errorcode   #SHELLMATTA_OK
+ * @return      errorcode   #SHELLMATTA_OK\n
  *                          #SHELLMATTA_ERROR   in case of crc error
  */
 shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
 {
+    shellmatta_retCode_t ret = SHELLMATTA_OK;
+
     switch (transportLayerInst.state)
     {
+        /** -# look for start of header */
     case STATE_GET_SOH:
+        /* if start of header is found, continue transport layer fsm */
         if (SHELLMATTA_START_OF_HEADER == *data)
         {
             transportLayerInst.state = STATE_GET_PROTOCOL_VERSION;
@@ -87,6 +90,7 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
         }
         else
         {
+            /* if SOH is not found, handle data as manual input */
             transportLayerInst.state = STATE_MANUAL_INPUT;
             transportLayerInst.active = false;
         }
@@ -95,30 +99,47 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
     case STATE_MANUAL_INPUT:
         break;
 
+    /** -# look for protocol version */
     case STATE_GET_PROTOCOL_VERSION:
-        transportLayerInst.state = STATE_GET_PACKET_TYPE;
+        /* check protocol version */
+        if (PROTOCOL_VERSION == *data)
+        {
+            transportLayerInst.state = STATE_GET_PACKET_TYPE;
+        }
+        else
+        {
+            /* reset fsm in case of wrong protocol version */
+            transportLayerInst.state = STATE_GET_SOH;
+            transportLayerInst.active = false;
+        }
         break;
 
+    /** -# read packet type */
     case STATE_GET_PACKET_TYPE:
         transportLayerInst.state = STATE_GET_PAYLOAD_LENGTH;
         break;
 
+    /** -# look for length of payload */
     case STATE_GET_PAYLOAD_LENGTH:
         transportLayerInst.state = STATE_GET_SOURCE;
         break;
 
+    /** -# look for source */
     case STATE_GET_SOURCE:
         transportLayerInst.state = STATE_GET_DESTINATION;
         break;
 
+    /** -# look for destination */
     case STATE_GET_DESTINATION:
         transportLayerInst.state = STATE_GET_H2S_SEQUENCE_CNT;
         break;
 
+    /** -# look for h2s sequence counter*/
     case STATE_GET_H2S_SEQUENCE_CNT:
         transportLayerInst.state = STATE_GET_S2H_SEQUENCE_CNT;
         break;
 
+    /** -# look for s2h sequence counter */
     case STATE_GET_S2H_SEQUENCE_CNT:
         if (packetType == PACKET_SEQ_CNT_REQUEST)
         {
@@ -130,6 +151,7 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
         }
         break;
 
+    /** -# read payload for previously read length of payload */
     case STATE_GET_PAYLOAD:
         if (    (payloadLength <= payloadCounter)
             &&  (true == transportLayerInst.active))
@@ -138,7 +160,9 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
         }
         break;
 
+    /** -# read crc32 for four bytes */
     case STATE_GET_CRC:
+        /** -# if crc32 is read, start computing */
         if (SHELLMATTA_LENGTH_CRC <= crcCounter)
         {
             /* for crc computation only */
@@ -158,10 +182,10 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
 
             refCrc = crc32Calc(crcdata, SHELLMATTA_HEADER_LENGTH + payloadLength);
 
+            /** #- if crc is correct, further handling of data depends on type of packet */
             if (crc32 == refCrc)
             {
-                crcCounter = 0;
-                crc32 = 0;
+                /* reset fsm by default */
                 transportLayerInst.state = STATE_GET_SOH;
 
                 /* crc is correct */
@@ -207,21 +231,21 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
                     transportLayerInst.h2s_sequenceCnt--;
                     break;
                 }
-                break;
             }
             else
             {
-                // TODO: improve this
-                crcCounter = 0;
-                crc32 = 0;
                 transportLayerInst.state = STATE_GET_SOH;
-
                 /* crc is incorrect */
-                return SHELLMATTA_ERROR;
+                ret = SHELLMATTA_ERROR;
             }
+
+            /* reset crc variables */
+            crcCounter = 0;
+            crc32 = 0;
         }
         break;
 
+    /** -# start to process payload as if it was entered manually */
     case STATE_PROCESS_PAYLOAD:
         transportLayerInst.state = STATE_GET_SOH;
         protocolVersion = 0u;
@@ -240,7 +264,7 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
         break;
     }
 
-    return SHELLMATTA_OK;
+    return ret;
 }
 
 /**