Explorar o código

fixed some ymodem bugs

Fischer, Simon | Friedrich Lütze GmbH hai 1 ano
pai
achega
864819ab02
Modificáronse 1 ficheiros con 38 adicións e 8 borrados
  1. 38 8
      src/shellmatta_ymodem.c

+ 38 - 8
src/shellmatta_ymodem.c

@@ -11,17 +11,21 @@
 /* current ymodem packet size */
 uint16_t    yModemPacketSize = YMODEM_PACKET_SIZE;
 /* packet counter - counts all correctly received packets during one transmission */
-uint8_t     shellmatta_ymodem_packet_counter = 0u;
+uint16_t    shellmatta_ymodem_packet_counter = 0u;
 /* counts total amount of data bytes received during one transmission */
-uint16_t    shellmatta_ymodem_total_bytes_received = 0u;
+uint32_t    shellmatta_ymodem_total_bytes_received = 0u;
 /* counts total amount of data bytes received during one packet */
 uint16_t    shellmatta_ymodem_byte_counter = 0u;
+uint8_t fileNameDelimiterPosition = 255u;
+uint8_t fileSizeDelimiterPosition = 255u;
 /* structure of a packet consisting of data and metadata */
 shellmatta_ymodem_packet_t shellmatta_ymodem_packet;
 /* holds information about which data type is currently actice: YMODEM_NONE, YMODEM_HEADER, YMODEM_BODY, YMODEM_FOOTER */
 shellmatta_ymodem_datatype_t shellmatta_ymodem_current_data_type = YMODEM_NONE;
 /* structure of ymodem callback functions */
 shellmatta_ymodem_callbacks_t shellmatta_ymodem_callbacks;
+/* flag to disable byte processing in ymodem */
+bool ymodem_is_enabled = true;
 
 /**
  * @brief                       Initialise the ymodem prior to actually receiving data
@@ -84,11 +88,16 @@ shellmatta_ymodem_state_t shellmatta_ymodem_get_state(shellmatta_handle_t handle
 */
 void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn)
 {
-    static uint8_t fileNameDelimiterPosition = 255u;
-    static uint8_t fileSizeDelimiterPosition = 255u;
     static uint32_t fileSize = 0u;
     static char fileSizeStr[7u]; /* hopefully no more bytes than a million will ever be transmitted */
     shellmatta_ymodem_rcv_retcode_t recvRetCode;
+
+    /* skip byte processing if ymodem is not enabled */
+    if (!ymodem_is_enabled)
+    {
+        return;
+    }
+
     recvRetCode = shellmatta_ymodem_receive_byte(handle, byteIn);
     switch (shellmatta_ymodem_get_state(handle))
     {
@@ -111,7 +120,7 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
             break;
         
         case YMODEM_HEADER: /* go here if the first body packet is to be expected */
-            if (recvRetCode == SOH_RECEIVED || recvRetCode == STX_RECEIVED)
+            if (recvRetCode == STX_RECEIVED || SOH_RECEIVED)
             {
                 shellmatta_ymodem_byte_counter = 0u;
                 shellmatta_ymodem_set_state(handle, RECEIVE_PACKET);
@@ -131,13 +140,13 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
             if (recvRetCode == EOT_RECEIVED)
             {
                 shellmatta_ymodem_byte_counter = 0u;
-                /* go to footer when EOT was received */
                 /* answer with ACK */
                 shellmatta_ymodem_ack(handle);
                 /* then send 0x43 symbol */
                 shellmatta_ymodem_control(handle, YMODEM_CRC);
-                /* then wait for SOH */
+                /* go to footer when EOT was received */
                 shellmatta_ymodem_current_data_type = YMODEM_FOOTER;
+                /* then wait for SOH */
             }
             break;
         case YMODEM_FOOTER: /* go here if the end of transmission packet is to be expected */
@@ -397,7 +406,7 @@ void shellmatta_ymodem_check_packet(shellmatta_handle_t handle)
         return;
     }
     /* compare to internal packet counter */
-    if (shellmatta_ymodem_packet.packetNumber != shellmatta_ymodem_packet_counter)
+    if (shellmatta_ymodem_packet.packetNumber != ((uint8_t)shellmatta_ymodem_packet_counter)) /* uint8_t cast to cause overflow */
     {
         shellmatta_ymodem_nak(handle);
         return;
@@ -522,6 +531,12 @@ void shellmatta_ymodem_reset(shellmatta_handle_t handle, bool doCancel)
             shellmatta_ymodem_callbacks.yModemCancelCallback();
         }
     }
+    /* reset global variables */
+    shellmatta_ymodem_byte_counter = 0u;
+    shellmatta_ymodem_packet_counter = 0u;
+    shellmatta_ymodem_total_bytes_received = 0u;
+    fileNameDelimiterPosition = 255u;
+    fileSizeDelimiterPosition = 255u;
 
     /* reset all ymodem_packet data */
     shellmatta_ymodem_packet.packetNumber = 0u;
@@ -541,3 +556,18 @@ void shellmatta_ymodem_reset(shellmatta_handle_t handle, bool doCancel)
     ((shellmatta_instance_t*)handle)->transportEnabled = true;
 #endif
 }
+
+shellmatta_ymodem_datatype_t shellmatta_ymodem_get_current_datatype(void)
+{
+    return shellmatta_ymodem_current_data_type;
+}
+
+void shellmatta_ymodem_enable(void)
+{
+    ymodem_is_enabled = true;
+}
+
+void shellmatta_ymodem_disable(void)
+{
+    ymodem_is_enabled = false;
+}