Преглед изворни кода

added checks for packet lengths

stefan пре 9 месеци
родитељ
комит
bd64923590
2 измењених фајлова са 27 додато и 15 уклоњено
  1. 5 0
      doc/shellmatta_transport_layer.dox
  2. 22 15
      src/shellmatta_transport.c

+ 5 - 0
doc/shellmatta_transport_layer.dox

@@ -18,6 +18,11 @@
                 To check if all packets have been received by the shellmatta
                 the sequence counters included in any packet can be used.
 
+                The packet counter will be incremented on every packet which
+                is received with a valid CRC and address.
+                Unknown packets or packets with wrong payload lengths are
+                ignored - but the sequence counter will still be incremented.
+
     @section shellmatta_transport_layer_sequence Basic sequence
 
     @startuml

+ 22 - 15
src/shellmatta_transport.c

@@ -317,10 +317,13 @@ shellmatta_retCode_t shellmatta_transport_process(shellmatta_transport_layer_t
                     break;
 
                 case SHELLMATTA_TRANSPORT_PACKET_SEQ_CNT_REQUEST:
-                    /** -# send out packet with no payload */
-                    intPacket.header.packetType = SHELLMATTA_TRANSPORT_PACKET_SEQ_CNT_RESPOND;
-                    intPacket.header.payloadLength = 0u;
-                    (void)shellmatta_transport_send(transportLayer, (shellmatta_transport_packet_t *)&intPacket);
+                    if(transportLayer->inPacket.header.payloadLength == 0u)
+                    {
+                        /** -# send out packet with no payload */
+                        intPacket.header.packetType = SHELLMATTA_TRANSPORT_PACKET_SEQ_CNT_RESPOND;
+                        intPacket.header.payloadLength = 0u;
+                        (void)shellmatta_transport_send(transportLayer, (shellmatta_transport_packet_t *)&intPacket);
+                    }
                     break;
 
                 case SHELLMATTA_TRANSPORT_PACKET_SEQ_CNT_RESPOND:
@@ -328,14 +331,18 @@ shellmatta_retCode_t shellmatta_transport_process(shellmatta_transport_layer_t
                     break;
 
                 case SHELLMATTA_TRANSPORT_PACKET_MAX_BUFFERSIZE_REQUEST:
-                    /** -# store the hosts buffersize */
-                    transportLayer->hostBufferSize = (uint8_t)transportLayer->inPacket.payload[0];
-
-                    /** -# respont with our own buffer size */
-                    intPacket.header.packetType = SHELLMATTA_TRANSPORT_PACKET_MAX_BUFFERSIZE_RESPOND;
-                    intPacket.header.payloadLength = 1u;
-                    intPacket.payload[0] = SHELLMATTA_TRANPORT_PAYLOAD_MAXLENGTH;
-                    (void)shellmatta_transport_send(transportLayer, (shellmatta_transport_packet_t *)&intPacket);
+
+                    if(transportLayer->inPacket.header.payloadLength == 1u)
+                    {
+                        /** -# store the hosts buffersize */
+                        transportLayer->hostBufferSize = (uint8_t)transportLayer->inPacket.payload[0];
+
+                        /** -# respont with our own buffer size */
+                        intPacket.header.packetType = SHELLMATTA_TRANSPORT_PACKET_MAX_BUFFERSIZE_RESPOND;
+                        intPacket.header.payloadLength = 1u;
+                        intPacket.payload[0] = SHELLMATTA_TRANPORT_PAYLOAD_MAXLENGTH;
+                        (void)shellmatta_transport_send(transportLayer, (shellmatta_transport_packet_t *)&intPacket);
+                    }
                     break;
 
                 case SHELLMATTA_TRANSPORT_PACKET_MAX_BUFFERSIZE_RESPOND:
@@ -344,7 +351,8 @@ shellmatta_retCode_t shellmatta_transport_process(shellmatta_transport_layer_t
 
                 case SHELLMATTA_TRANSPORT_PACKET_SEARCH_DEVICE_REQUEST:
                     /** -# check if our own uuid is inside the passed range */
-                    if(true == shellmatta_transport_search(transportLayer, &transportLayer->inPacket))
+                    if((transportLayer->inPacket.header.payloadLength == SHELLMATTA_TRANPORT_UUID_LENGTH * 2u) &&
+                       (true == shellmatta_transport_search(transportLayer, &transportLayer->inPacket)))
                     {
                         intPacket.header.packetType = SHELLMATTA_TRANSPORT_PACKET_SEARCH_DEVICE_RESPOND;
                         intPacket.header.payloadLength = SHELLMATTA_TRANPORT_UUID_LENGTH;
@@ -389,8 +397,7 @@ shellmatta_retCode_t shellmatta_transport_process(shellmatta_transport_layer_t
                     break;
 
                 default:
-                    /** -# undo sequence counter increment on wrong packet type */
-                    transportLayer->sequenceH2S--;
+                    /** -# ignore unknown packets */
                     break;
                 }
             }