Browse Source

fixed packet size calculation of ymodem

stefan 9 months ago
parent
commit
5c632acbcd
3 changed files with 40 additions and 18 deletions
  1. 23 11
      example/main.c
  2. 2 1
      src/shellmatta_utils.c
  3. 15 6
      src/shellmatta_ymodem.c

+ 23 - 11
example/main.c

@@ -182,15 +182,30 @@ void ymodemCallbackCancel() {
 }
 
 void ymodemCallbackReceiveHeader(uint32_t fileSize, char* fileName) {
-    (void)fileSize;
-    (void)fileName;
+
+    printf("Received Packet Header: %u %s\n", fileSize, fileName);
+
     return;
 }
 
-void ymodemCallbackReceivePacket(uint8_t *data, uint32_t packetSize, uint32_t packetNum) {
-    (void)data;
-    (void)packetSize;
-    (void)packetNum;
+void ymodemCallbackReceivePacket(uint8_t *data, uint32_t packetSize, uint32_t packetNum)
+{
+
+    uint32_t i;
+
+    printf("Received Packet: %u %u\n", packetNum, packetSize);
+
+    for(i = 0u; i < packetSize; i ++)
+    {
+        printf("0x%02x ", data[i]);
+
+        if(15u == (i % 16))
+        {
+            printf("\n");
+        }
+    }
+    printf("\n");
+
     return;
 }
 
@@ -246,6 +261,8 @@ int main(int argc, char **argv)
         return f;
     }
 
+    printf("Starting Shellmatta\n");
+
     shellmatta_doInit(  &instance,
                         &handle,
                         buffer,
@@ -298,11 +315,6 @@ int main(int argc, char **argv)
                 continue;
             }
         }
-        else
-        {
-            fprintf(stdout, "0x%02x \n", c);
-            fflush(stdout);
-        }
 
         do
         {

+ 2 - 1
src/shellmatta_utils.c

@@ -106,7 +106,7 @@ shellmatta_retCode_t utils_shellAsciiToUInt32(char* str, uint32_t *result)
     {
         *result = 0u;
 
-        while(*str && (SHELLMATTA_OK == ret))
+        while(*str && (' ' != *str) && (SHELLMATTA_OK == ret))
         {
             val = ((uint32_t)*str - 48u);
 
@@ -118,6 +118,7 @@ shellmatta_retCode_t utils_shellAsciiToUInt32(char* str, uint32_t *result)
             {
                 *result = (*result * 10u) + val;
             }
+            str ++;
         }
     }
     else

+ 15 - 6
src/shellmatta_ymodem.c

@@ -88,7 +88,7 @@ shellmatta_retCode_t processPacket(shellmatta_handle_t handle)
     {
         fileName = (char*)inst->ymodem.packet.packetData;
 
-        ret = utils_shellAsciiToUInt32((char*)&inst->ymodem.packet.packetData[strlen(fileName)],
+        ret = utils_shellAsciiToUInt32((char*)&inst->ymodem.packet.packetData[strlen(fileName) + 1u],
                                        &inst->ymodem.fileSize);
 
         /** -# pass filename and size to the callback */
@@ -97,9 +97,14 @@ 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 */
-        packetSize = SHELLMATTA_MAX((inst->ymodem.totalBytesReceived + inst->ymodem.packet.size),
-                                    inst->ymodem.fileSize);
-        packetSize = packetSize % inst->ymodem.packet.size;
+        if((inst->ymodem.totalBytesReceived + inst->ymodem.packet.size) > inst->ymodem.fileSize)
+        {
+            packetSize = inst->ymodem.fileSize % inst->ymodem.packet.size;
+        }
+        else
+        {
+            packetSize = inst->ymodem.packet.size;
+        }
 
         /** -# pass data to the application using the callback */
         inst->ymodem.recvPacketCallback(inst->ymodem.packet.packetData, packetSize, inst->ymodem.packetCounter);
@@ -133,12 +138,17 @@ static shellmatta_retCode_t ymodem_stateMachine(shellmatta_handle_t handle, uint
                     inst->ymodem.state = SHELLMATTA_YMODEM_RECEIVE_HEADER;
                     break;
 
+                case YMODEM_EOT:
                     inst->ymodem.transmissionCompleteCallback();
 
+                    // todo check for a complete telegram having been received
+                    // ...add a wait state to answer some more EOTs after time
+
                     /** -# ACK the successful file reception */
                     shellmatta_ymodem_control(handle, YMODEM_ACK);
                     shellmatta_ymodem_control(handle, YMODEM_CRC);
                     inst->ymodem.state = SHELLMATTA_YMODEM_INACTIVE;
+                    (void)utils_terminateInput(inst);
                     break;
                 default:
                     /** -# ignore unexpected characters on start */
@@ -203,8 +213,6 @@ static shellmatta_retCode_t ymodem_stateMachine(shellmatta_handle_t handle, uint
                     ret = processPacket(handle);
                     if(SHELLMATTA_OK == ret)
                     {
-                        inst->ymodem.packetCounter ++;
-
                         if(0u != inst->ymodem.packetCounter)
                         {
                             /** -# Calculate the total bytes received */
@@ -220,6 +228,7 @@ static shellmatta_retCode_t ymodem_stateMachine(shellmatta_handle_t handle, uint
                                 shellmatta_ymodem_control(handle, YMODEM_CRC);
                             }
                         }
+                        inst->ymodem.packetCounter ++;
                     }
                 }
             }