UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]
Dependencies: mbed SDFileSystem
Diff: common/CommandParser.cpp
- Revision:
- 0:c347f602596d
- Child:
- 1:71c9c97c9f3d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/CommandParser.cpp Sat Apr 20 09:23:39 2019 +0000 @@ -0,0 +1,73 @@ +#include "global.h" + +static void lineHandler(char *line) +{ + pCP->parse(line); +} + +CommandParser::CommandParser(UartReceiver *setUartReceiver, int setDeviceID, + CmdParseRule *setRuleTable, int setRuleTableLen) +{ + pR = setUartReceiver; + pR->setLineHandler(lineHandler); + deviceID = setDeviceID; + ruleTable = setRuleTable; + ruleTableLen = setRuleTableLen; + pUart = pR->getCurrentUart(); +} + +void CommandParser::run(void) +{ + pR->run(); +} + +int CommandParser::parse(char *pStr) +{ + int rn; + int len; + char *head; + /** skip - empty command*/ + if (pStr[0] == '\0') { + pUart->printf("[CommandParser::parse] (empty)\n"); + return 0; + } + /* protection for buffer overrun */ + pStr[UartReceiver::MaxStr] = '\0'; + len = strlen(pStr); + + /* parsing */ + head = pStr; + pUart->printf("[CommandParser::parse] %s\n", pStr); + + /** Command Format ":0 CMD 0000" */ + if (len != CmdLen) { + pUart->printf("[CommandParser::parse] Invalid Command Length\n"); + return -1; + } + /** check Command Header */ + if (head[0] != ':') { + pUart->printf("[CommandParser::parse] Invalid Command\n"); + 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)(head); + + } + pUart->printf("[CommandParser::parse] Invalid Command String\n"); + return -1; +} \ No newline at end of file