Brad Smith / Mbed 2 deprecated rtcController

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Parser.cpp Source File

Parser.cpp

00001 #include "./Parser.h"
00002 #include <string.h>
00003 #include <stdio.h>
00004 
00005  char *commandTokens[20];
00006  char invalidMessage[]     = "INVALID NEMA SENTENCE";
00007  char endOfTokensMessage[] =  "END OF TOKENS";
00008  
00009 char validateCommandSentence(char *pSentence)
00010 {
00011     char chkSumToken[3];
00012     unsigned char calculatedChecksum = 0;
00013     char errorCode = NOERROR;
00014     printf("\n\r\n\r>>>>>>>>>>>> %s <<<<\n\n\n\r", pSentence);
00015     if(*pSentence != '$')
00016         {
00017         printf("error no $\n\r");
00018         return ERROR;
00019         }
00020     pSentence++;
00021     
00022     while (*pSentence != '\0' && *pSentence != '*') {
00023         calculatedChecksum ^= *pSentence;
00024         pSentence++;
00025     }
00026     
00027     if(*pSentence == '*'){
00028         sprintf(chkSumToken, "%02x", calculatedChecksum);
00029         printf("Calc chksum %s<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\r", chkSumToken);
00030         pSentence++;
00031         if(strcmp(pSentence, chkSumToken) != 0)
00032             {
00033             printf("checksum error>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
00034             errorCode = ERROR;
00035             }
00036         }
00037     else {
00038         printf("error:***** %s\n\r",pSentence);
00039         errorCode = ERROR;
00040         }
00041    
00042     printf("<<<<< errorcode %d\n\r", errorCode);
00043     return errorCode;
00044 }
00045 
00046 // parseCommandSentence() requires that the trailing <cr><lf> characters
00047 // be removed prior to calling this function. Failing to do so will cause
00048 // validateNemaSentence() to report an ERROR
00049 
00050 void parseCommandSentence( char *pBuffer)
00051 {
00052     if (validateCommandSentence(pBuffer) == ERROR) {
00053         commandTokens[0]= invalidMessage;
00054         commandTokens[1]= endOfTokensMessage;
00055         }
00056     else{
00057         //so valid sentence ...................................
00058         int tokenCounter = 0;
00059         
00060         commandTokens[tokenCounter]=pBuffer;
00061         pBuffer++;
00062         tokenCounter++;
00063         
00064         while( *pBuffer != '\0'){
00065             //until the end of sentence is found,
00066             //search for comma deliminators
00067             if(*pBuffer == ','){
00068                 //found end of token
00069                 *pBuffer = '\0';    //replace deliminator with a null character
00070                 
00071                 pBuffer++;          // save address of next token
00072                 commandTokens[tokenCounter]=pBuffer;               
00073                 tokenCounter++;
00074                 }
00075             else pBuffer++;
00076             }//eo while not end of string
00077         commandTokens[tokenCounter] = endOfTokensMessage;
00078       
00079         }//eo Valid sentence:::::::::::::::::::::::::::::::::::::
00080     
00081 }//eo parseNemaSentence( char *pBuffer) ========================================
00082 
00083 
00084 //calculate a XOR checksum for a string
00085 char calcChecksum(char *pSentence)
00086 {
00087     unsigned char calculatedChecksum = 0;
00088     
00089     while (*pSentence != '\0' ) {
00090         calculatedChecksum ^= *pSentence;
00091         pSentence++;
00092     }
00093     return calculatedChecksum;
00094 }