|
@@ -7,8 +7,11 @@
|
|
|
#include "shellmatta_transport.h"
|
|
|
#include "shellmatta_crc.h"
|
|
|
#include <string.h>
|
|
|
+#include <stdio.h>
|
|
|
|
|
|
/* init global variables */
|
|
|
+uint32_t sentPayloadBytes = 0;
|
|
|
+
|
|
|
uint8_t protocolVersion = 0u;
|
|
|
shellmatta_transport_packet_t packetType = 0u;
|
|
|
uint8_t payloadLength = 0u;
|
|
@@ -17,8 +20,8 @@ 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;
|
|
|
+uint8_t packetSequenceCounter_h2s = 0u;
|
|
|
+uint8_t packetSequenceCounter_s2h = 0u;
|
|
|
shellmatta_transport_layer_t transportLayerInst = {
|
|
|
0,
|
|
|
0,
|
|
@@ -105,7 +108,14 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
|
|
|
break;
|
|
|
|
|
|
case STATE_GET_S2H_SEQUENCE_CNT:
|
|
|
- transportLayerInst.state = STATE_GET_PAYLOAD;
|
|
|
+ if (packetType == PACKET_SEQ_CNT_REQUEST)
|
|
|
+ {
|
|
|
+ transportLayerInst.state = STATE_GET_CRC;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ transportLayerInst.state = STATE_GET_PAYLOAD;
|
|
|
+ }
|
|
|
break;
|
|
|
|
|
|
case STATE_GET_PAYLOAD:
|
|
@@ -119,73 +129,85 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
|
|
|
case STATE_GET_CRC:
|
|
|
if (SHELLMATTA_LENGTH_CRC <= crcCounter)
|
|
|
{
|
|
|
- char data[SHELLMATTA_PAYLOAD_MAXLENGTH + 1 + SHELLMATTA_HEADER_LENGTH];
|
|
|
- memset(data, 0, SHELLMATTA_PAYLOAD_MAXLENGTH + 1 + SHELLMATTA_HEADER_LENGTH);
|
|
|
- data[0] = SHELLMATTA_START_OF_HEADER;
|
|
|
- data[1] = protocolVersion;
|
|
|
- data[2] = packetType;
|
|
|
- data[3] = payloadLength;
|
|
|
- data[4] = source;
|
|
|
- data[5] = destination;
|
|
|
- data[6] = transportLayerInst.h2s_sequenceCnt;
|
|
|
- data[7] = transportLayerInst.s2h_sequenceCnt;
|
|
|
- strncat(&data[8], payloadBuffer, payloadLength);
|
|
|
+
|
|
|
+ char crcdata[SHELLMATTA_PAYLOAD_MAXLENGTH + 1 + SHELLMATTA_HEADER_LENGTH];
|
|
|
+ memset(crcdata, 0, SHELLMATTA_PAYLOAD_MAXLENGTH + 1 + SHELLMATTA_HEADER_LENGTH);
|
|
|
+ crcdata[0] = SHELLMATTA_START_OF_HEADER;
|
|
|
+ crcdata[1] = protocolVersion;
|
|
|
+ crcdata[2] = packetType;
|
|
|
+ crcdata[3] = payloadLength;
|
|
|
+ crcdata[4] = source;
|
|
|
+ crcdata[5] = destination;
|
|
|
+ crcdata[6] = packetSequenceCounter_h2s;
|
|
|
+ crcdata[7] = packetSequenceCounter_s2h;
|
|
|
+ strncat(&crcdata[8], payloadBuffer, payloadLength);
|
|
|
|
|
|
uint32_t refCrc;
|
|
|
|
|
|
- refCrc = crc32Calc(data, SHELLMATTA_HEADER_LENGTH + payloadLength);
|
|
|
+ refCrc = crc32Calc(crcdata, SHELLMATTA_HEADER_LENGTH + payloadLength);
|
|
|
|
|
|
if (crc32 == refCrc)
|
|
|
{
|
|
|
+ crcCounter = 0;
|
|
|
+ crc32 = 0;
|
|
|
+ transportLayerInst.state = STATE_GET_SOH;
|
|
|
+
|
|
|
/* crc is correct */
|
|
|
+ transportLayerInst.h2s_sequenceCnt++;
|
|
|
|
|
|
switch (packetType)
|
|
|
{
|
|
|
case PACKET_DATA:
|
|
|
transportLayerInst.state = STATE_PROCESS_PAYLOAD;
|
|
|
- transportLayerInst.h2s_sequenceCnt++;
|
|
|
break;
|
|
|
|
|
|
case PACKET_SEQ_CNT_REQUEST:
|
|
|
- transportLayerInst.h2s_sequenceCnt++;
|
|
|
+ /* change packet type to response */
|
|
|
+ packetType = PACKET_SEQ_CNT_RESPOND;
|
|
|
+ /* send out packet with no payload */
|
|
|
+ shellmatta_write_transport("", 0);
|
|
|
+ if (transportLayerInst.h2s_sequenceCnt != packetSequenceCounter_h2s)
|
|
|
+ {
|
|
|
+ crc32 = 0;
|
|
|
+ }
|
|
|
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 */
|
|
|
+ /* wrong packet type */
|
|
|
default:
|
|
|
+ /* undo sequence counter increment */
|
|
|
+ transportLayerInst.h2s_sequenceCnt--;
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ // TODO: improve this
|
|
|
+ crcCounter = 0;
|
|
|
+ crc32 = 0;
|
|
|
+ transportLayerInst.state = STATE_GET_SOH;
|
|
|
+
|
|
|
/* crc is incorrect */
|
|
|
return SHELLMATTA_ERROR;
|
|
|
}
|
|
@@ -215,29 +237,38 @@ shellmatta_retCode_t shellmatta_handle_transport_fsm(char *data)
|
|
|
|
|
|
shellmatta_retCode_t shellmatta_write_transport(const char* data, uint32_t length)
|
|
|
{
|
|
|
+ /* create buffer for data + header + crc with maximum size */
|
|
|
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 outPayloadLength = length + SHELLMATTA_HEADER_LENGTH + SHELLMATTA_LENGTH_CRC;
|
|
|
+
|
|
|
+ /* fill buffer with header data */
|
|
|
+ outputBuffer[0] = SHELLMATTA_START_OF_HEADER; /* start of header */
|
|
|
+ outputBuffer[1] = PROTOCOL_VERSION; /* protocol version */
|
|
|
+ outputBuffer[2] = packetType; /* packet type */
|
|
|
+ outputBuffer[3] = length; /* payload length */
|
|
|
+ outputBuffer[4] = 0x00u; /* source */
|
|
|
+ outputBuffer[5] = 0x00u; /* destination */
|
|
|
+ outputBuffer[6] = transportLayerInst.h2s_sequenceCnt; /* sequence counter host to shellmatta */
|
|
|
+ outputBuffer[7] = ++transportLayerInst.s2h_sequenceCnt; /* sequence counter shellmatta to host */
|
|
|
+
|
|
|
+ /* response of sequence counters is only packet type without payload */
|
|
|
+ if (packetType != PACKET_SEQ_CNT_RESPOND)
|
|
|
+ {
|
|
|
+ memcpy(&outputBuffer[8], data, length);
|
|
|
+ }
|
|
|
+
|
|
|
uint32_t outCrc = crc32Calc((char*) outputBuffer, SHELLMATTA_HEADER_LENGTH + length);
|
|
|
|
|
|
+ /* append crc to end of payload */
|
|
|
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);
|
|
|
|
|
|
+ /* use original write function to send full buffer */
|
|
|
shellmatta_retCode_t ret = transportLayerInst.originalWrite((char*) outputBuffer, outPayloadLength);
|
|
|
return ret;
|
|
|
}
|