Переглянути джерело

added ymodem robustness to missing filesize
added response to cancel events

stefan 9 місяців тому
батько
коміт
6f802670eb
2 змінених файлів з 20 додано та 9 видалено
  1. 1 0
      api/shellmatta.h
  2. 19 9
      src/shellmatta_ymodem.c

+ 1 - 0
api/shellmatta.h

@@ -263,6 +263,7 @@ typedef struct
     uint32_t fileSize;                                          /**< size of the file received in packet 0          */
     bool pauseRequested;                                        /**< pause requested from the application           */
     uint32_t pollCyclesLeft;                                    /**< number of poll cycles left before ending       */
+    uint32_t cancelCounter;                                     /**< counter to count the amount of cancels         */
     shellmatta_ymodem_packet_t packet;                          /**< currently processed packet                     */
     shellmatta_ymodem_cancel_t cancelCallback;                  /**< callback to pass cancel events                 */
     shellmatta_ymodem_recvHeader_t recvHeaderCallback;          /**< callback to pass received header data          */

+ 19 - 9
src/shellmatta_ymodem.c

@@ -74,6 +74,7 @@ static void shellmatta_ymodem_reset(shellmatta_handle_t handle, bool doCancel)
     inst->ymodem.fileSize = 0u;
     inst->ymodem.pauseRequested = false;
     inst->ymodem.pollCyclesLeft = 0u;
+    inst->ymodem.cancelCounter = 0u;
 
 #ifdef SHELLMATTA_TRANSPORT
     /** .-# reenable transport layer optional mode */
@@ -105,7 +106,8 @@ shellmatta_retCode_t processPacket(shellmatta_handle_t handle)
     else
     {
         /** -# calculate packet size - when it is the last packet this is limited by the file size */
-        if((inst->ymodem.totalBytesReceived + inst->ymodem.packet.size) > inst->ymodem.fileSize)
+        if((0u != inst->ymodem.fileSize) &&
+           (inst->ymodem.totalBytesReceived + inst->ymodem.packet.size) > inst->ymodem.fileSize)
         {
             packetSize = inst->ymodem.fileSize % inst->ymodem.packet.size;
         }
@@ -261,18 +263,25 @@ shellmatta_retCode_t shellmatta_ymodem_processByte(shellmatta_handle_t handle, c
     shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
     shellmatta_retCode_t ret;
 
-    /** -# check if session is cancelled -accept only if ymodem is between packets */
-    if(('\x03' == byte) && (inst->ymodem.state == SHELLMATTA_YMODEM_WAIT_FOR_START))
+    /** -# check if session is cancelled - accept only if ymodem is between packets */
+    if((('\x03' == byte) || (YMODEM_CA == byte)) && (inst->ymodem.state == SHELLMATTA_YMODEM_WAIT_FOR_START))
     {
-        /** -# explicitly reset ymodem with cancel, if it was active */
-        shellmatta_ymodem_reset(handle, true);
+        inst->ymodem.cancelCounter ++;
 
-        utils_terminateInput(inst);
+        /** -# cancel after at least 2 cancel characters */
+        if(inst->ymodem.cancelCounter >= 2u)
+        {
+            /** -# explicitly reset ymodem with cancel, if it was active */
+            shellmatta_ymodem_reset(handle, true);
+
+            utils_terminateInput(inst);
 
-        ret = SHELLMATTA_ERROR;
+            ret = SHELLMATTA_ERROR;
+        }
     }
     else
     {
+        inst->ymodem.cancelCounter = 0u;
         ret = ymodem_stateMachine(handle, (uint8_t)byte);
     }
 
@@ -300,8 +309,9 @@ shellmatta_retCode_t shellmatta_ymodem_poll(shellmatta_handle_t handle)
         {
             ret = SHELLMATTA_OK;
             /** -# check if the received data matches the file size */
-            if((inst->ymodem.totalBytesReceived < inst->ymodem.fileSize) ||
-                ((inst->ymodem.totalBytesReceived - inst->ymodem.fileSize) >= YMODEM_PACKET_SIZE_1K))
+            if((0u != inst->ymodem.fileSize) &&
+                ((inst->ymodem.totalBytesReceived < inst->ymodem.fileSize) ||
+                ((inst->ymodem.totalBytesReceived - inst->ymodem.fileSize) >= YMODEM_PACKET_SIZE_1K)))
             {
                 ret = SHELLMATTA_ERROR;
             }