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