1st release (without comment)

RMS_UART.cpp

Committer:
nsrwsurasak
Date:
2016-09-19
Revision:
7:5401cc8ee60f
Parent:
5:4ffe57c4bfaa

File content as of revision 7:5401cc8ee60f:

/**
  ******************************************************************************
  * @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 "main.h"

#ifdef EXT_SERIAL
    /* Using UART1 as in Debuger (ST-Link) */
    Serial serial_device(SERIAL_TX, SERIAL_RX);
#else
    /* Using UART2 via external UART to USB */
    Serial serial_device(PA_3, PB_3);
#endif

char Msg_RxBuf[MSG_BUF_SIZE + 1];               // An Command Buffer 
int  Msg_index;                                 // An Command index

char Content_RxBuf[CONTENT_BUF_SIZE + 1];       // An Contents Buffer 
int  Content_index;                             // An Contents index

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   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   Intertupt callback function
 * @note    Management fill buffer direction
 * @retval 
 */
void RxMsgInterruptCallback()
{
    /* Received serial Rx interrupt */
    
    /* Check buffer direction */
    if(MsgContentManagement == false)
    {
        /* Fill buffer to commands */
        DoTheMsgCmd();
    }
    else
    {
        /* Fill buffer to contents */
        DoTheContent();
    }
}

/**
 * @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++;
    
    /* Check end of content */
    CheckContentMsg();
}

 /**
 * @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++;
    
    /* 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))
    {
        /* Call function for check contents */
        SendContentToFile();
        
        
    }
    /* Content buffer are full */
    if(Content_index == CONTENT_BUF_SIZE)
    {
        /* Call function for check contents */
        serial_device.printf("\n -- File is too large!!\r\n");
        ClearCmdIndexAndBuf();
        ClearContentIndexAndBuf();
        ClearFileNameBuf();
        MsgContentManagement = false;
        
    }
}

 /**
 * @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
    
    /* Command type is list file ($ls) */
    if(strstr(Msg_RxBuf,CMD_LISTFILE))
    {
        /* 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();
    }
    /* Got the *.csv file name */
    else if(strstr(Msg_RxBuf,FOUND_CSV_FILE))
    {
        /* Categorization ommand type */
        CheckReadEditCmd();
    }
    /* 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();
    }
    /* Command type is system restart ($rs) */
    else if(strstr(Msg_RxBuf,CMD_SYS_RESTART))
    {
        /* Request system restart */
        NVIC_SystemReset();
    }
    /* Command type is help ($-h) */
    else if(strstr(Msg_RxBuf,CMD_HELP))
    {
        /* List all command */
        GetHelpCmd();
    }
    /* Command type is stop ($st) */
    else if(strstr(Msg_RxBuf,CMD_STOP))
    {
        /* ## waiting call function for stop or hold or self mode */
        
    }
    
    /* Command may be mismatched type */
    else
    {
        /* Not receive true type command */
        if(Msg_index >= MSG2CMD)
        {
            if(!((strstr(Msg_RxBuf,CMD_LISTFILE))  || 
                  (strstr(Msg_RxBuf,CMD_READFILE)) || 
                  (strstr(Msg_RxBuf,CMD_WRITEFILE) ||
                  (strstr(Msg_RxBuf,CMD_HELP))     ||
                  (strstr(Msg_RxBuf,CMD_STOP))     || 
                  (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   Categorization commands type
 * @note    Received file name and type
 * @retval
 */
void CheckReadEditCmd()
{
    /* 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;
    }
    /* Command type is write/create file ($ed) */
    else if(strstr(Msg_RxBuf,CMD_WRITEFILE))
    {
        /* Check file name */
        GetFileName();
        
        /* Set event flag for fill contents buffer */
        MsgContentManagement = true; 

    }
    /* 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   Received command error 
 * @note    $??
 * @retval
 */
void GetCmdError()
{   
    serial_device.printf("$%s -- Command Error %s\n",Msg_RxBuf);
    
    /* Clear command index and buffer */
    ClearCmdIndexAndBuf();
}

/**
 * @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   Request sent contents to main 
 * @note    
 * @retval
 */
void SendContentToFile()
{   
    /* Set fill buffer to command */
    MsgContentManagement = true;
    
    /* Set REQ_WRITE  event's reqFileCmdType */
    reqFileCmdType = REQ_WRITE;
    
    /* Set flag evetnt */
    /* Waiting! main program call event */
    UART_File_event = true;
    
}

/**
 * @brief   Command help
 * @note    $-h
 * @retval
 */
void GetHelpCmd()
{   
    serial_device.printf("\n$ls : List fils as in directory");
    serial_device.printf("\n$rd [File Name] : Read fils as file name");
    serial_device.printf("\n$df [File Name] : Delete fils as file name");
    serial_device.printf("\n$ed [File Name] : Write/Create fils as in directory");
    serial_device.printf("\n$rs : Mini-RMS system restart");
    serial_device.printf("\n$df : List fils as in directory");
    
    /* Clear buffer */
    ClearCmdIndexAndBuf();
}
/**
 * @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   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);
}