Kaynağa Gözat

messed around with ymodem breaking it in the process...

stefan 9 ay önce
ebeveyn
işleme
0ff4483f95
5 değiştirilmiş dosya ile 61 ekleme ve 77 silme
  1. 6 9
      api/shellmatta.h
  2. 8 11
      example/main.c
  3. 15 18
      src/shellmatta.c
  4. 29 35
      src/shellmatta_ymodem.c
  5. 3 4
      src/shellmatta_ymodem.h

+ 6 - 9
api/shellmatta.h

@@ -444,16 +444,13 @@ shellmatta_retCode_t shellmatta_auth_chpasswd(              shellmatta_handle_t
 
 #endif
 
-uint8_t shellmatta_ymodem(  shellmatta_handle_t             handle,
-                            uint8_t*                        buffer,
-                            uint32_t*                       fileSize,
-                            uint16_t*                       packetSize,
-                            shellmatta_ymodem_callbacks_t   callbacks);
+uint8_t shellmatta_ymodem(      shellmatta_handle_t             handle,
+                                uint8_t*                        buffer,
+                                uint32_t*                       fileSize,
+                                uint16_t*                       packetSize,
+                                shellmatta_ymodem_callbacks_t   callbacks);
 
-void shellmatta_ymodem_end( shellmatta_handle_t             handle,
-                            bool                            doCancel);
-
-void shellmatta_ymodem_set_enable(bool                      doEnable);
+void shellmatta_ymodem_cancel(  shellmatta_handle_t             handle);
 
 bool shellmatta_ymodem_is_active(void);
 

+ 8 - 11
example/main.c

@@ -205,19 +205,14 @@ static shellmatta_retCode_t ymodem(shellmatta_handle_t handle, const char *argum
 {
     (void)arguments;
     (void)length;
-
-    if (SHELLMATTA_OK == ret)
-    {
-        shellmatta_printf(handle, "Starting ymodem session\r\n");
-        ret = SHELLMATTA_CONTINUE;
-    }
+    shellmatta_printf(handle, "Starting ymodem session\r\n");
     shellmatta_ymodem(handle,
                     ymodemBuffer,
                     &ymodemFileSize,
                     &ymodemPacketSize,
                     ymodemCallbacks);
 
-    return ret;
+    return SHELLMATTA_OK;
 }
 shellmatta_cmd_t ymodemCommand = {"ymodem", NULL, NULL, NULL, ymodem, NULL, NULL};
 
@@ -294,16 +289,18 @@ int main(int argc, char **argv)
         res = read (f, &c, 1);
         if (res == -1)
         {
-            i = (i + 1) % 1000;
+            i = (i + 1) % 10000;
             usleep(1);
             if (i != 0)
             {
                 continue;
             }
         }
-
-        fprintf(stdout, "0x%02x \n", c);
-        fflush(stdout);
+        else
+        {
+            fprintf(stdout, "0x%02x \n", c);
+            fflush(stdout);
+        }
 
         do
         {

+ 15 - 18
src/shellmatta.c

@@ -56,7 +56,6 @@ static shellmatta_retCode_t shellmatta_processDataInt(shellmatta_handle_t     ha
     shellmatta_retCode_t        ret = SHELLMATTA_OK;
     shellmatta_retCode_t        cmdRet;
     shellmatta_instance_t       *inst = (shellmatta_instance_t*)handle;
-    shellmatta_ymodem_state_t   yModemState;
 
     /** -# in busy mode - keep calling this command */
     if(NULL != inst->busyCmd)
@@ -79,6 +78,7 @@ static shellmatta_retCode_t shellmatta_processDataInt(shellmatta_handle_t     ha
             utils_terminateInput(inst);
         }
     }
+    // todo call ymodem implementation to send initial NCKs
     /** -# call continuous function even if there is no data */
     else if((0u == size) && (NULL != inst->continuousCmd))
     {
@@ -110,15 +110,20 @@ static shellmatta_retCode_t shellmatta_processDataInt(shellmatta_handle_t     ha
     /** -# process byte wise */
     for (; (inst->byteCounter < size) && (NULL == inst->busyCmd); inst->byteCounter++)
     {
-        /** -# in continuous mode - pass data directly to the command */
-        if(NULL != inst->continuousCmd)
+
+        /** -# handle ymodem when a session is active */
+        if (inst->ymodemState != INACTIVE)
         {
-            yModemState = shellmatta_ymodem_get_state(handle);
-            /* if data is coming in while ymodem module is active, forward data to it */
-            if (yModemState != INACTIVE)
+            ret = shellmatta_ymodem_processByte(handle, data[inst->byteCounter]);
+
+            if (SHELLMATTA_OK == ret)
             {
-                shellmatta_ymodem_receive_packet(handle, data[inst->byteCounter]);
+                return ret;
             }
+        }
+        /** -# in continuous mode - pass data directly to the command */
+        if(NULL != inst->continuousCmd)
+        {
             /** -# copy data and call command function */
             inst->buffer[inst->stdinIdx]        = data[inst->byteCounter];
             inst->buffer[inst->stdinIdx + 1u]   = '\0';
@@ -134,17 +139,9 @@ static shellmatta_retCode_t shellmatta_processDataInt(shellmatta_handle_t     ha
             }
             else if(('\x03' == data[inst->byteCounter]))
             {
-                /* accept cancel only if ymodem is inactive or between packets */
-                if (yModemState == INACTIVE || yModemState == WAIT_FOR_START)
-                {
-                    /** -# cancel continue session */
-                    utils_terminateInput(inst);
-                    if (yModemState == WAIT_FOR_START)
-                    {
-                        /** -# explicitly reset ymodem with cancel, if it was active */
-                        shellmatta_ymodem_reset(handle, true);
-                    }
-                }
+                /** -# cancel continue session */
+                utils_terminateInput(inst);
+
                 ret = SHELLMATTA_OK;
             }
             else if(SHELLMATTA_CONTINUE == ret)

+ 29 - 35
src/shellmatta_ymodem.c

@@ -25,8 +25,6 @@ shellmatta_ymodem_packet_t shellmatta_ymodem_packet;
 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
@@ -93,12 +91,6 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
     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))
     {
@@ -121,7 +113,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 == STX_RECEIVED || SOH_RECEIVED)
+            if (recvRetCode == STX_RECEIVED || recvRetCode == SOH_RECEIVED)
             {
                 shellmatta_ymodem_byte_counter = 0u;
                 shellmatta_ymodem_set_state(handle, RECEIVE_PACKET);
@@ -341,6 +333,34 @@ void shellmatta_ymodem_receive_packet(shellmatta_handle_t handle, uint8_t byteIn
     }
 }
 
+shellmatta_retCode_t shellmatta_ymodem_processByte(shellmatta_handle_t handle, char data)
+{
+    shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
+    uint32_t i;
+
+    /** -# check if session is cancelled -accept only if ymodem is between packets */
+    if(('\x03' == data) && (inst->ymodemState == WAIT_FOR_START))
+    {
+        /** -# explicitly reset ymodem with cancel, if it was active */
+        shellmatta_ymodem_reset(handle, true);
+
+        utils_terminateInput(inst);
+
+        return SHELLMATTA_ERROR;
+    }
+
+    shellmatta_ymodem_receive_packet(handle, data);
+
+    if ((0u == size) && (inst->ymodemState == WAIT_FOR_START))
+    {
+        /* send initial ymodem symbol to start transmission */
+        shellmatta_ymodem_control(handle, YMODEM_CRC);
+    }
+    return SHELLMATTA_OK;
+}
+
+
+
 /**
  * @brief                   Gives a return code depending on symbol received at different states of the ymodem module
  * @param[in, out]  handle  shellmatta handle of the instance
@@ -565,16 +585,6 @@ 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;
-}
-
 /** 
  * @brief       starts the ymodem receive module 
  * @param[in]   handle      shellmatta instance handle
@@ -590,10 +600,6 @@ uint8_t shellmatta_ymodem(  shellmatta_handle_t             handle,
                             uint16_t*                       packetSize,
                             shellmatta_ymodem_callbacks_t   callbacks)
 {
-#ifdef SHELLMATTA_TRANSPORT
-    /* disable transport layer so control symbols won't be caught by it */
-    ((shellmatta_instance_t*)handle)->transportEnabled = false;
-#endif
     shellmatta_ymodem_init(handle, buffer, fileSize, packetSize, callbacks);
 
     /* send initial ymodem symbol to start transmission */
@@ -613,18 +619,6 @@ void shellmatta_ymodem_end(shellmatta_handle_t handle, bool doCancel)
     utils_clearInput((shellmatta_instance_t*)handle);
 }
 
-void shellmatta_ymodem_set_enable(bool doEnable)
-{
-    if (doEnable) 
-    {
-        shellmatta_ymodem_enable();
-    }
-    else
-    {
-        shellmatta_ymodem_disable();
-    }
-}
-
 bool shellmatta_ymodem_is_active(void)
 {
     return shellmatta_ymodem_get_current_datatype() != YMODEM_NONE;

+ 3 - 4
src/shellmatta_ymodem.h

@@ -67,6 +67,9 @@ void shellmatta_ymodem_init(            shellmatta_handle_t             handle,
 void shellmatta_ymodem_receive_packet(  shellmatta_handle_t             handle, 
                                         uint8_t                         byteIn);
 
+shellmatta_retCode_t shellmatta_ymodem_processByte( shellmatta_handle_t handle,
+                                                    char                *data);
+
 void shellmatta_ymodem_set_state(       shellmatta_handle_t             handle, 
                                         shellmatta_ymodem_state_t       newState);
 
@@ -89,8 +92,4 @@ void shellmatta_ymodem_reset(           shellmatta_handle_t handle,
 
 shellmatta_ymodem_datatype_t shellmatta_ymodem_get_current_datatype(void);
 
-void shellmatta_ymodem_enable(void);
-
-void shellmatta_ymodem_disable(void);
-
 #endif /* _SHELLMATTA_YMODEM_H_ */