Browse Source

bugfix and remove debug messages

Simon Fischer 2 years ago
parent
commit
de38f019cf
2 changed files with 21 additions and 34 deletions
  1. 20 33
      src/shellmatta_ymodem.c
  2. 1 1
      src/shellmatta_ymodem.h

+ 20 - 33
src/shellmatta_ymodem.c

@@ -105,7 +105,6 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
                 /* reset packet counter */
                 shellmatta_ymodem_packet_counter = 0u;
                 shellmatta_ymodem_byte_counter = 0u;
-                shellmatta_printf(handle, "SOH receive\r\n");
                 shellmatta_ymodem_set_state(handle, RECEIVE_PACKET);
                 shellmatta_ymodem_current_data_type = YMODEM_HEADER;
             }
@@ -115,7 +114,6 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
             if (recvRetCode == SOH_RECEIVED || recvRetCode == STX_RECEIVED)
             {
                 shellmatta_ymodem_byte_counter = 0u;
-                shellmatta_printf(handle, "SOH receive\r\n");
                 shellmatta_ymodem_set_state(handle, RECEIVE_PACKET);
                 shellmatta_ymodem_current_data_type = YMODEM_BODY;
             }
@@ -126,7 +124,6 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
             {
                 shellmatta_ymodem_byte_counter = 0u;
                 /* stay in body when SOH was received */
-                shellmatta_printf(handle, "SOH receive\r\n");
                 shellmatta_ymodem_set_state(handle, RECEIVE_PACKET);
                 shellmatta_ymodem_current_data_type = YMODEM_BODY;
             }
@@ -135,7 +132,6 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
             {
                 shellmatta_ymodem_byte_counter = 0u;
                 /* go to footer when EOT was received */
-                shellmatta_printf(handle, "EOT receive\r\n");
                 /* answer with ACK */
                 shellmatta_ymodem_ack(handle);
                 /* then send 0x43 symbol */
@@ -159,7 +155,6 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
                 shellmatta_ymodem_packet_counter = 0u;
                 /* reset byte counter to avoid error by one caused by previously received EOT symbol */
                 shellmatta_ymodem_byte_counter = 0u;
-                shellmatta_printf(handle, "EOT packet starting\r\n");
                 shellmatta_ymodem_set_state(handle, RECEIVE_PACKET);
             }
             break;
@@ -180,17 +175,16 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
                 /* first two bytes are packet number and reverse packet number */
                 case 0:
                     shellmatta_ymodem_packet.packetNumber = byteIn;
-                    shellmatta_printf(handle, "packet number = %u\r\n", shellmatta_ymodem_packet.packetNumber);
                     break;
                 case 1:
                     shellmatta_ymodem_packet.reversePacketNumber = byteIn;
-                    shellmatta_printf(handle, "reverse packet number = %u\r\n", shellmatta_ymodem_packet.reversePacketNumber);
                     break;
                 default:
                     break;
                 }
                 /* after packet numbers, data field begins */
-                if (shellmatta_ymodem_byte_counter >= YMODEM_HEADER_DATA_OFFSET && shellmatta_ymodem_byte_counter < YMODEM_HEADER_CRC_POSITION)
+                if ((shellmatta_ymodem_byte_counter >= YMODEM_HEADER_DATA_OFFSET) && 
+                    (shellmatta_ymodem_byte_counter < YMODEM_HEADER_CRC_POSITION))
                 {
                     shellmatta_ymodem_packet.packetData[shellmatta_ymodem_byte_counter - YMODEM_HEADER_DATA_OFFSET] = byteIn;
                     /* check for filename */
@@ -200,12 +194,12 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
                         if (byteIn == YMODEM_NULL)
                         {
                             fileNameDelimiterPosition = shellmatta_ymodem_byte_counter + 1;
-                            shellmatta_printf(handle, "%s\r\n", shellmatta_ymodem_packet.packetData);
                         }
                     }
                     /* after filename the filesize begins */
                     else
-                    if (shellmatta_ymodem_byte_counter >= fileNameDelimiterPosition && shellmatta_ymodem_byte_counter < fileSizeDelimiterPosition)
+                    if (shellmatta_ymodem_byte_counter >= fileNameDelimiterPosition && 
+                        (shellmatta_ymodem_byte_counter < fileSizeDelimiterPosition))
                     {
                         /* find space as delimiter after filesize */
                         if (byteIn == YMODEM_SPACE)
@@ -214,7 +208,6 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
                             /* convert file size string to actual number */
                             fileSize = utils_shellAsciiToUInt32(fileSizeStr, fileSizeDelimiterPosition - fileNameDelimiterPosition);
                             *shellmatta_ymodem_packet.fileSize = fileSize;
-                            shellmatta_printf(handle, "fileSize = %u\r\n", fileSize);
                         }
                         /* save characters in string otherwise */
                         else
@@ -241,7 +234,6 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
                         /* lower byte */
                         shellmatta_ymodem_packet.packetCrc |= byteIn;
                         /* change ymodem state to check packet */
-                        shellmatta_printf(handle, "packetCrc = 0x%04x\r\n", shellmatta_ymodem_packet.packetCrc);
                         shellmatta_ymodem_check_packet(handle);
                     }
                 }
@@ -253,36 +245,33 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
                 {
                 case 0:
                     shellmatta_ymodem_packet.packetNumber = byteIn;
-                    shellmatta_printf(handle, "packet number = %u\r\n", shellmatta_ymodem_packet.packetNumber);
                     break;
                 case 1:
                     shellmatta_ymodem_packet.reversePacketNumber = byteIn;
-                    shellmatta_printf(handle, "reverse packet number = %u\r\n", shellmatta_ymodem_packet.reversePacketNumber);
                     break;
                 default:
                     break;
                 }
 
-                // TODO: Something might still be wrong here
                 /* after data field, crc begins */
-                if (shellmatta_ymodem_byte_counter > yModemPacketSize + 2)
+                if (shellmatta_ymodem_byte_counter >= yModemPacketSize + YMODEM_HEADER_DATA_OFFSET)
                 {
-                    if (shellmatta_ymodem_byte_counter == yModemPacketSize + 3)
+                    if (shellmatta_ymodem_byte_counter == yModemPacketSize + YMODEM_HEADER_DATA_OFFSET)
                     {
                         /* upper byte */
                         shellmatta_ymodem_packet.packetCrc = byteIn << 8u;
                     }
-                    if (shellmatta_ymodem_byte_counter == yModemPacketSize + 4)
+                    if (shellmatta_ymodem_byte_counter == yModemPacketSize + YMODEM_HEADER_DATA_OFFSET + 1)
                     {
                         /* lower byte */
                         shellmatta_ymodem_packet.packetCrc |= byteIn;
                         /* change ymodem state to check packet */
-                        shellmatta_printf(handle, "packetCrc = 0x%04x\r\n", shellmatta_ymodem_packet.packetCrc);
                         shellmatta_ymodem_check_packet(handle);
                     }
                 }
                 else
-                if (shellmatta_ymodem_byte_counter > 1)
+                if ((shellmatta_ymodem_byte_counter > 1) && 
+                    (shellmatta_ymodem_byte_counter < (yModemPacketSize + YMODEM_HEADER_DATA_OFFSET)))
                 {
                     shellmatta_ymodem_packet.packetData[shellmatta_ymodem_byte_counter - YMODEM_HEADER_DATA_OFFSET] = byteIn;
                 }
@@ -297,11 +286,9 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
                 {
                 case 0:
                     shellmatta_ymodem_packet.packetNumber = byteIn;
-                    shellmatta_printf(handle, "packet number = %u\r\n", shellmatta_ymodem_packet.packetNumber);
                     break;
                 case 1:
                     shellmatta_ymodem_packet.reversePacketNumber = byteIn;
-                    shellmatta_printf(handle, "reverse packet number = %u\r\n", shellmatta_ymodem_packet.reversePacketNumber);
                     break;
                 default:
                     break;
@@ -387,27 +374,23 @@ void shellmatta_ymodem_check_packet(shellmatta_handle_t handle)
     uint8_t computedCrc = 0u;
     if (shellmatta_ymodem_packet.packetNumber != (0xFF - shellmatta_ymodem_packet.reversePacketNumber))
     {
-        shellmatta_printf(handle, "wrong packet number pair\r\n");
         shellmatta_ymodem_nak(handle);
         return;
     }
     /* compare to internal packet counter */
     if (shellmatta_ymodem_packet.packetNumber != shellmatta_ymodem_packet_counter)
     {
-        shellmatta_printf(handle, "wrong packet number\r\n");
-        shellmatta_printf(handle, "expected %u, got %u\r\n", shellmatta_ymodem_packet_counter, shellmatta_ymodem_packet.packetNumber);
         shellmatta_ymodem_nak(handle);
         return;
     }
     /* compare packet crc only after other checks succeeded */
     computedCrc = crc16Calc((const char*)shellmatta_ymodem_packet.packetData, yModemPacketSize);
-    //! uncomment for debug purposes
-    // if (shellmatta_ymodem_packet.packetCrc != computedCrc)
-    // {
-    //     shellmatta_printf(handle, "wrong packet crc: expected 0x%04x\r\n", computedCrc);
-    //     shellmatta_ymodem_nak(handle);
-    //     return;
-    // }
+    //! uncomment to ignore crc check for debug purposes
+    if (shellmatta_ymodem_packet.packetCrc != computedCrc)
+    {
+        shellmatta_ymodem_nak(handle);
+        return;
+    }
     /* if previous checks passed, packet is acknowledged */
     (void) computedCrc;
     shellmatta_ymodem_total_bytes_received += shellmatta_ymodem_byte_counter;
@@ -495,9 +478,13 @@ void shellmatta_ymodem_nak(shellmatta_handle_t handle)
     }
 }
 
+/**
+ * @brief               forwards the given character to write-function without formatting
+ * @param[in]   handle  shellmatta handle of the instance
+ * @param[in]   c       character to be sent
+*/
 void shellmatta_ymodem_control(shellmatta_handle_t handle, const char c)
 {
-    shellmatta_printf(handle, "0x%02x\r\n", c);
     ((shellmatta_instance_t*)handle)->write(&c, 1);
 }
 

+ 1 - 1
src/shellmatta_ymodem.h

@@ -70,7 +70,7 @@ void shellmatta_ymodem_receive_packet(  shellmatta_handle_t             handle,
 void shellmatta_ymodem_set_state(       shellmatta_handle_t             handle, 
                                         shellmatta_ymodem_state_t       newState);
 
-shellmatta_ymodem_state_t shellmatta_ymodem_get_state(shellmatta_handle_t handle);
+shellmatta_ymodem_state_t shellmatta_ymodem_get_state(          shellmatta_handle_t handle);
 
 shellmatta_ymodem_rcv_retcode_t shellmatta_ymodem_receive_byte( shellmatta_handle_t handle, 
                                                                 uint8_t             byteIn);