1st release (without comment)

Revision:
1:5e6c5fbd48d6
Parent:
0:bc10ea82aec3
Child:
2:38567b4310a4
--- a/RMS_UART.cpp	Thu Sep 15 01:47:46 2016 +0000
+++ b/RMS_UART.cpp	Thu Sep 15 08:07:31 2016 +0000
@@ -1,206 +1,336 @@
+/**
+  ******************************************************************************
+  * @file    RMS_UART.cpp
+  * @author  Surasak N
+  * @version V1
+  * @date    15/09/2016
+  * @brief   Command for Mini-RMS <> RMS via UART
+  ******************************************************************************/
+  
 #include "mbed.h"
-#include "RMS_UART.h"
+#include "main.h"
 
 #ifdef EXT_SERIAL
+    /* Using UART1 as in Debuger (ST-Link) */
     Serial serial_device(SERIAL_TX, SERIAL_RX);
 #else
-    Serial serial_device(SERIAL_TX, SERIAL_RX);
+    /* Using UART2 via external UART to USB */
+    Serial serial_device(PA_3, PB_3);
 #endif
 
-char Msg_RxBuf[MSG_BUF_SIZE + 1];            // Reading Cmd Buffer 
-int  Msg_index;                              // An Cmd index
+char Msg_RxBuf[MSG_BUF_SIZE + 1];               // An Command Buffer 
+int  Msg_index;                                 // An Command index
 
-char Content_RxBuf[CONTENT_BUF_SIZE + 1];    // Reading Cmd Buffer 
-int  Content_index;                          // An Cmd index
+char Content_RxBuf[CONTENT_BUF_SIZE + 1];       // An Contents Buffer 
+int  Content_index;                             // An Contents index
 
-char FileName[MSG2FILENAME];
-
-bool MsgContentManagement = false;
+char FileName[MSG2FILENAME];                    // An Command Buffer 
+FileRequest_CMD_Type reqFileCmdType = REQ_NONE; // An Events Request and management
+bool MsgContentManagement = false;              // Command and Contents buffer management
 
 /**
- * @brief   
- * @note 
+ * @brief   Initials bsp and interrupt of serial communication
+ * @note    BPS have 9600bps only 
  * @retval 
  */
 void Init_SerialDevice()
 {
+    /* Initial begining bps with 9600bps */
     serial_device.baud(BPS_9600);
+    
+    /* Initial interrupt callback function */
     serial_device.attach(&RxMsgInterruptCallback);
 }
 
 /**
- * @brief   
- * @note 
+ * @brief   Intertupt callback function
+ * @note    Management fill buffer direction
  * @retval 
  */
 void RxMsgInterruptCallback()
 {
-    /* Start Rx interrupt  */
+    /* Received serial Rx interrupt */
+    
+    /* Check buffer direction */
     if(MsgContentManagement == false)
     {
+        /* Fill buffer to commands */
         DoTheMsgCmd();
     }
     else
     {
+        /* Fill buffer to contents */
         DoTheContent();
     }
 }
 
 /**
- * @brief 
- * @note 
+ * @brief   Fill serial data as in contents buffer
+ * @note    
  * @retval
  */
 void DoTheContent()
 {   
     // Note: you need to actually read from the serial to clear the RX interrupt
+    
+    /* Fill data as serial to contents buffer */
     Content_RxBuf[Content_index] = serial_device.getc();
+    
+    /* Shift contents index */
     Content_index++;
     
-    if(strstr(Content_RxBuf,"*end*"))
-    {
-        SendContentToFile();
-        ClearContentIndexAndBuf();
-        MsgContentManagement = false;
-    }
-    
-    if(Content_index == CONTENT_BUF_SIZE)
-    {
-        SendContentToFile();
-        ClearContentIndexAndBuf();
-    }
+    /* Check end of content */
+    CheckContentMsg();
 }
 
  /**
- * @brief 
- * @note 
+ * @brief   Fill serial data as in command buffer
+ * @note    Command type and FileName
  * @retval
  */
 void DoTheMsgCmd()
 {
         
     // Note: you need to actually read from the serial to clear the RX interrupt
+    
+    /* Fill data as serial to command and file name buffer */
     Msg_RxBuf[Msg_index] = serial_device.getc();
+    
+    /* Shift command and filename index */
     Msg_index++;
     
-    if(strstr(Msg_RxBuf,"ls"))
+    /* Check command type */
+    CheckCmdType();
+}
+
+/**
+ * @brief   Checking and processing contents data
+ * @note    Check command *end* and clear buffer
+ * @retval
+ */
+void CheckContentMsg()
+{
+    /* Found the *end* */
+    if(strstr(Content_RxBuf,CMD_END_OF_CONTENT))
     {
-        GetListFileCmd();
+        /* Call function for check contents */
+        SendContentToFile();
+        
+        /* Set fill buffer to command */
+        MsgContentManagement = false;
     }
+    /* Content buffer are full */
+    if(Content_index == CONTENT_BUF_SIZE)
+    {
+        /* Call function for check contents */
+        SendContentToFile();
+    }
+}
+
+ /**
+ * @brief   Checking and processing command type
+ * @note    $ls, $ed, $rd, $rs and $df and etc.
+ * @retval
+ */
+void CheckCmdType()
+{   
+    // Note : Procese the command type and file name
     
-    if(strstr(Msg_RxBuf,".csv"))
+    /* Command type is list file ($ls) */
+    if(strstr(Msg_RxBuf,CMD_LISTFILE))
     {
-        CheckReadEditCmd();
+        /* Set REQ_LISTDIR  event's reqFileCmdType */
+        reqFileCmdType = REQ_LISTDIR;
+        
+        /* Set flag evetnt */
+        /* Waiting! main program call event */
+        UART_File_event = true;
+        
+        /* Clear command index and buffer */
+        ClearCmdIndexAndBuf();
     }
-        
-    if(strstr(Msg_RxBuf,".xml"))
+    /* Got the *.csv file name */
+    else if(strstr(Msg_RxBuf,FOUND_CSV_FILE))
     {
+        /* Categorization ommand type */
         CheckReadEditCmd();
     }
-    
-    if(strstr(Msg_RxBuf,".log"))
+    /* Got the *.xml file name */  
+    else if(strstr(Msg_RxBuf,FOUND_XML_FILE))
     {
+        /* Categorization command type */
+        CheckReadEditCmd();
+    }
+    /* Got the *.log file name */
+    else if(strstr(Msg_RxBuf,FOUND_LOG_FILE))
+    {
+        /* Categorization command type */
         CheckReadEditCmd();
     }
-    
-    if(Msg_index >= 2)
+    /* Command type is system restart ($rs) */
+    else if(strstr(Msg_RxBuf,CMD_SYS_RESTART))
+    {
+        /* Request system restart */
+        NVIC_SystemReset();
+    }
+    /* Command may be mismatched type */
+    else
     {
-        if( !((strstr(Msg_RxBuf,"ls")) || (strstr(Msg_RxBuf,"rd")) || (strstr(Msg_RxBuf,"ed"))))
+        /* Not receive true type command */
+        if(Msg_index >= 2)
         {
-            GetCmdError();
+            if(!((strstr(Msg_RxBuf,CMD_LISTFILE)) || 
+                  (strstr(Msg_RxBuf,CMD_READFILE)) || 
+                  (strstr(Msg_RxBuf,CMD_WRITEFILE) || 
+                  (strstr(Msg_RxBuf,CMD_DELETEFILE)))))
+            {
+                /* Call command error function */ 
+                GetCmdError();
+            }
         }
     }
-    
+
+    /* Command and file name buffer are full */
     if(Msg_index == MSG_BUF_SIZE)
     {
+       /* Clear command index and buffer */
        ClearCmdIndexAndBuf();
     }
 }
 
 /**
- * @brief 
- * @note 
- * @retval
- */
-void GetListFileCmd()
-{   
-    ClearCmdIndexAndBuf();
-}
-
-/**
- * @brief 
- * @note 
+ * @brief   Categorization commands type
+ * @note    Received file name and type
  * @retval
  */
 void CheckReadEditCmd()
 {
-    if(strstr(Msg_RxBuf,"rd"))
+    /* Command type is read file ($rd) */
+    if(strstr(Msg_RxBuf,CMD_READFILE))
     {   
+        /* Check file name */
         GetFileName();
+        
+        /* Set REQ_READ  event's reqFileCmdType */
+        reqFileCmdType = REQ_READ;
+        
+        /* Set flag evetnt */
+        /* Waiting! main program call event */
+        UART_File_event = true;
     }
-    else if(strstr(Msg_RxBuf,"ed"))
+    /* Command type is write/create file ($ed) */
+    else if(strstr(Msg_RxBuf,CMD_WRITEFILE))
     {
+        /* Check file name */
         GetFileName();
-        MsgContentManagement = true;
+        
+        /* Set event flag for fill contents buffer */
+        MsgContentManagement = true; 
+        
+        /* User indicated write command status */
+        printf("\n-- Write command received\n");
+    }
+    /* Command type is delete ($df) */
+    else if(strstr(Msg_RxBuf,CMD_DELETEFILE))
+    {
+        /* Check file name */
+        GetFileName();
+        
+        /* Set REQ_DELETE  event's reqFileCmdType */
+        reqFileCmdType = REQ_DELETE;
+        
+        /* Set flag evetnt */
+        /* Waiting! main program call event */
+        UART_File_event = true;
     }
     else
     {
+        /* Call command error function */ 
         GetCmdError();
     }
 }
 
 /**
- * @brief 
- * @note 
+ * @brief   Received command error 
+ * @note    $??
  * @retval
  */
 void GetCmdError()
 {   
-    serial_device.printf("$?? -- Command Error\n",Msg_RxBuf + 1);
+    serial_device.printf("$%s -- Command Error %s\n",Msg_RxBuf);
+    
+    /* Clear command index and buffer */
     ClearCmdIndexAndBuf();
 }
 
 /**
- * @brief 
- * @note 
+ * @brief   FileName process
+ * @note    Fill file name as buffer
  * @retval
  */
 void GetFileName()
 {   
+    /* Copy file name from Msg to FileName */
     memcpy(FileName,&Msg_RxBuf[3],Msg_index - 3);
+    
+    /* Clear command index and buffer */
     ClearCmdIndexAndBuf();
 }
 
 /**
- * @brief 
- * @note 
+ * @brief   Request sent contents to main 
+ * @note    
  * @retval
  */
 void SendContentToFile()
 {   
+    /* Set fill buffer to command */
     MsgContentManagement = true;
-    serial_device.printf("Content -- %s \n",Content_RxBuf);
-    ClearContentIndexAndBuf();
+    
+    /* Set REQ_WRITE  event's reqFileCmdType */
+    reqFileCmdType = REQ_WRITE;
+    
+    /* Set flag evetnt */
+    /* Waiting! main program call event */
+    UART_File_event = true;
+    
 }
 
 /**
- * @brief 
- * @note 
+ * @brief   Clear index and buffer of command and file name
+ * @note    
  * @retval
  */
 void ClearCmdIndexAndBuf()
 {   
+    /* Clear counter */
     Msg_index = 0;
+    
+    /* Clear buffer */
     memset(Msg_RxBuf,' ',MSG_BUF_SIZE);
 }
 
 /**
- * @brief 
+ * @brief   Clear index and buffer of contents 
  * @note 
  * @retval
  */
 void ClearContentIndexAndBuf()
 {   
+    /* Clear counter */
     Content_index = 0;
+    
+    /* Clear buffer */
     memset(Content_RxBuf,' ',CONTENT_BUF_SIZE);
 }
+
+/**
+ * @brief   Clear fileName buffer
+ * @note 
+ * @retval
+ */
+void ClearFileNameBuf()
+{   
+    /* Clear buffer */
+    memset(FileName,' ',MSG2FILENAME);
+}
\ No newline at end of file