Explorar el Código

add wrapper function for write function to send data using the transport layer protocol + prepare handling of different packet types

Fischer, Simon hace 3 años
padre
commit
69eda82225
Se han modificado 1 ficheros con 87 adiciones y 10 borrados
  1. 87 10
      src/shellmatta_transport.c

+ 87 - 10
src/shellmatta_transport.c

@@ -9,21 +9,24 @@
 #include <string.h>
 
 /* init global variables */
-uint8_t protocolVersion                  = 0u;
-shellmatta_transport_packet_t packetType = 0u;
-uint8_t payloadLength                    = 0u;
-uint8_t source                           = 0u;
-uint8_t destination                      = 0u;
-uint32_t crc32                           = 0u;
-uint8_t payloadCounter                   = 0u;
-uint8_t crcCounter                       = 0u;
+uint8_t protocolVersion                     = 0u;
+shellmatta_transport_packet_t packetType    = 0u;
+uint8_t payloadLength                       = 0u;
+uint8_t source                              = 0u;
+uint8_t destination                         = 0u;
+uint32_t crc32                              = 0u;
+uint8_t payloadCounter                      = 0u;
+uint8_t crcCounter                          = 0u;
+uint32_t packetSequenceCounter_h2s          = 0u;
+uint32_t packetSequenceCounter_s2h          = 0u;
 shellmatta_transport_layer_t transportLayerInst = {
     0,
     0,
     STATE_GET_SOH,
     false,
     false,
-    false
+    false,
+    NULL
 };
 char payloadBuffer[SHELLMATTA_PAYLOAD_MAXLENGTH + 1];
 
@@ -134,11 +137,56 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
 
             if (crc32 == refCrc)
             {
-                transportLayerInst.state = STATE_PROCESS_PAYLOAD;
+                /* crc is correct */
+
+                switch (packetType)
+                {
+                case PACKET_DATA:
+                    transportLayerInst.state = STATE_PROCESS_PAYLOAD;
+                    transportLayerInst.h2s_sequenceCnt++;
+                    break;
+                
+                case PACKET_SEQ_CNT_REQUEST:
+                    transportLayerInst.h2s_sequenceCnt++;
+                    break;
+
+                case PACKET_SEQ_CNT_RESPOND:
+                    transportLayerInst.h2s_sequenceCnt++;
+                    break;
+                
+                case PACKET_MAX_BUFFERSIZE_REQUEST:
+                    transportLayerInst.h2s_sequenceCnt++;
+                    break;
+
+                case PACKET_MAX_BUFFERSIZE_RESPOND:
+                    transportLayerInst.h2s_sequenceCnt++;
+                    break;
+                
+                case PACKET_SEARCH_DEVICE_REQUEST:
+                    transportLayerInst.h2s_sequenceCnt++;
+                    break;
+                
+                case PACKET_SEARCH_DEVICE_RESPOND:
+                    transportLayerInst.h2s_sequenceCnt++;
+                    break;
+
+                case PACKET_SET_ADDRESS_REQUEST:
+                    transportLayerInst.h2s_sequenceCnt++;
+                    break;
+
+                case PACKET_SET_ADDRESS_RESPOND:
+                    transportLayerInst.h2s_sequenceCnt++;
+                    break;
+
+                /* should not happen */
+                default:
+                    break;
+                }
                 break;
             }
             else
             {
+                /* crc is incorrect */
                 return SHELLMATTA_ERROR;
             }
         }
@@ -164,3 +212,32 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
 
     return SHELLMATTA_OK;
 }
+
+shellmatta_retCode_t shellmatta_write_transport(const char* data, uint32_t length)
+{
+    uint8_t outputBuffer[   SHELLMATTA_HEADER_LENGTH +          /* length of header */
+                            SHELLMATTA_PAYLOAD_MAXLENGTH + 1 +  /* max length of payload */
+                            SHELLMATTA_LENGTH_CRC];             /* length of crc */
+    
+    uint8_t outPayloadLength = length + SHELLMATTA_HEADER_LENGTH + SHELLMATTA_LENGTH_CRC;
+
+    outputBuffer[0] = SHELLMATTA_START_OF_HEADER;
+    outputBuffer[1] = 0x01u;
+    outputBuffer[2] = 0x00u;
+    outputBuffer[3] = length;
+    outputBuffer[4] = 0x01u;
+    outputBuffer[5] = 0x00u;
+    outputBuffer[6] = transportLayerInst.h2s_sequenceCnt;
+    outputBuffer[7] = ++transportLayerInst.s2h_sequenceCnt;
+
+    memcpy(&outputBuffer[8], data, length);
+    uint32_t outCrc = crc32Calc((char*) outputBuffer, SHELLMATTA_HEADER_LENGTH + length);
+
+    outputBuffer[length + SHELLMATTA_HEADER_LENGTH + 0] = (uint8_t)(outCrc >> 0);
+    outputBuffer[length + SHELLMATTA_HEADER_LENGTH + 1] = (uint8_t)(outCrc >> 8);
+    outputBuffer[length + SHELLMATTA_HEADER_LENGTH + 2] = (uint8_t)(outCrc >> 16);
+    outputBuffer[length + SHELLMATTA_HEADER_LENGTH + 3] = (uint8_t)(outCrc >> 24);
+
+    shellmatta_retCode_t ret = transportLayerInst.originalWrite((char*) outputBuffer, outPayloadLength);
+    return ret;
+}