UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]
Dependencies: mbed SDFileSystem
common/CommandParser.cpp
- Committer:
- Inscape_ao
- Date:
- 2019-04-23
- Revision:
- 2:a694440145e9
- Parent:
- 1:71c9c97c9f3d
- Child:
- 7:9ab8809f9693
File content as of revision 2:a694440145e9:
#include "global.h" /* UartLineHandler (start parsing) */ static void lineHandler(char *line) { pCP->parse(line); } /* constructor */ CommandParser::CommandParser(UartReceiver *setUartReceiver, int setDeviceID, CmdParseRule *setRuleTable, int setRuleTableLen) { pR = setUartReceiver; pR->setLineHandler(lineHandler); deviceID = setDeviceID; ruleTable = setRuleTable; ruleTableLen = setRuleTableLen; pUart = pR->getCurrentUart(); } /* start parsing */ void CommandParser::run(void) { pR->run(); } /* process parsing */ int CommandParser::parse(char *pStr) { int rn; int len; char *head; /** skip - empty command*/ if (pStr[0] == '\0') { pUart->printf("(empty)\n"); return 0; } /* protection for buffer overrun */ pStr[UartReceiver::MaxStr] = '\0'; len = strlen(pStr); /* parsing */ head = pStr; pUart->printf("%s\n", pStr); /** Command Format ":0 CMD 0000" */ if (len != CmdLen) { //pUart->printf("[CommandParser::parse] Invalid Command Length\n"); this->reply(false, CommandParser::CmdReply::InvalidLen); return -1; } /** check Command Header */ if (head[0] != ':') { //pUart->printf("[CommandParser::parse] Invalid Format\n"); this->reply(false, CommandParser::CmdReply::InvalidFmt); return -1; } /** check Command DeviceID */ if ((head[1] - '0') != deviceID) { //pUart->printf("[CommandParser::parse] Ignore (DST is other)\n"); return 0; } /** search Command */ head += 3; for(rn = 0; rn < ruleTableLen; rn++) { int check; check = strncmp((const char*)(ruleTable[rn].cmdName), (const char*)(head),CmdNameLen); if (check != 0) { continue; } head += 4; return (*ruleTable[rn].func)(this, head, ruleTable[rn].exarg); } //pUart->printf("[CommandParser::parse] Invalid Command"); this->reply(false, CommandParser::CmdReply::InvalidCmd); return -1; } /* get my Device ID */ int CommandParser::getDeviceID(void) { return deviceID; } /* generate ACK/NAK with int code */ void CommandParser::reply(bool ack, int replyCode) { char replyStr[] = "0000"; sprintf(replyStr, "%04d", replyCode); this->reply(ack, replyStr); } /* generate ACK/NAK with String */ void CommandParser::reply(bool ack, char* replyStr) { const char CmdReplyStrDefault[] = ":0 ACK 0000\n"; const int CmdReplyLen = sizeof(CmdReplyStrDefault) /* include \0 */; const int CmdReplyDevPos = 1; const int CmdReplyAckPos = 3; const int CmdReplyCodePos = 7; const int CmdReplyEndPos = 11; char str[CmdReplyLen]; /* set default handler */ memcpy(str, CmdReplyStrDefault, CmdReplyLen); str[CmdReplyDevPos] = '0' + getDeviceID(); if (!ack) { memcpy(&str[CmdReplyAckPos], "NAK", 3); } memcpy(&str[CmdReplyCodePos], replyStr, 4); str[CmdReplyEndPos] = '\n'; pUart->printf(str); } /* get currentUart */ Serial *CommandParser::getCurrentUart(void) { return pUart; }