UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]

Dependencies:   mbed SDFileSystem

Committer:
Inscape_ao
Date:
Sun Apr 21 02:50:19 2019 +0000
Revision:
1:71c9c97c9f3d
Parent:
0:c347f602596d
Child:
2:a694440145e9
add Reply Method in Command Parser

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Inscape_ao 0:c347f602596d 1 #include "global.h"
Inscape_ao 0:c347f602596d 2
Inscape_ao 0:c347f602596d 3 static void lineHandler(char *line)
Inscape_ao 0:c347f602596d 4 {
Inscape_ao 0:c347f602596d 5 pCP->parse(line);
Inscape_ao 0:c347f602596d 6 }
Inscape_ao 0:c347f602596d 7
Inscape_ao 0:c347f602596d 8 CommandParser::CommandParser(UartReceiver *setUartReceiver, int setDeviceID,
Inscape_ao 0:c347f602596d 9 CmdParseRule *setRuleTable, int setRuleTableLen)
Inscape_ao 0:c347f602596d 10 {
Inscape_ao 0:c347f602596d 11 pR = setUartReceiver;
Inscape_ao 0:c347f602596d 12 pR->setLineHandler(lineHandler);
Inscape_ao 0:c347f602596d 13 deviceID = setDeviceID;
Inscape_ao 0:c347f602596d 14 ruleTable = setRuleTable;
Inscape_ao 0:c347f602596d 15 ruleTableLen = setRuleTableLen;
Inscape_ao 0:c347f602596d 16 pUart = pR->getCurrentUart();
Inscape_ao 0:c347f602596d 17 }
Inscape_ao 0:c347f602596d 18
Inscape_ao 0:c347f602596d 19 void CommandParser::run(void)
Inscape_ao 0:c347f602596d 20 {
Inscape_ao 0:c347f602596d 21 pR->run();
Inscape_ao 0:c347f602596d 22 }
Inscape_ao 0:c347f602596d 23
Inscape_ao 0:c347f602596d 24 int CommandParser::parse(char *pStr)
Inscape_ao 0:c347f602596d 25 {
Inscape_ao 0:c347f602596d 26 int rn;
Inscape_ao 0:c347f602596d 27 int len;
Inscape_ao 0:c347f602596d 28 char *head;
Inscape_ao 0:c347f602596d 29 /** skip - empty command*/
Inscape_ao 0:c347f602596d 30 if (pStr[0] == '\0') {
Inscape_ao 0:c347f602596d 31 pUart->printf("[CommandParser::parse] (empty)\n");
Inscape_ao 0:c347f602596d 32 return 0;
Inscape_ao 0:c347f602596d 33 }
Inscape_ao 0:c347f602596d 34 /* protection for buffer overrun */
Inscape_ao 0:c347f602596d 35 pStr[UartReceiver::MaxStr] = '\0';
Inscape_ao 0:c347f602596d 36 len = strlen(pStr);
Inscape_ao 0:c347f602596d 37
Inscape_ao 0:c347f602596d 38 /* parsing */
Inscape_ao 0:c347f602596d 39 head = pStr;
Inscape_ao 0:c347f602596d 40 pUart->printf("[CommandParser::parse] %s\n", pStr);
Inscape_ao 0:c347f602596d 41
Inscape_ao 0:c347f602596d 42 /** Command Format ":0 CMD 0000" */
Inscape_ao 0:c347f602596d 43 if (len != CmdLen) {
Inscape_ao 1:71c9c97c9f3d 44 //pUart->printf("[CommandParser::parse] Invalid Command Length\n");
Inscape_ao 1:71c9c97c9f3d 45 this->reply(false, CommandParser::CmdReply::InvalidLen);
Inscape_ao 0:c347f602596d 46 return -1;
Inscape_ao 0:c347f602596d 47 }
Inscape_ao 0:c347f602596d 48 /** check Command Header */
Inscape_ao 0:c347f602596d 49 if (head[0] != ':') {
Inscape_ao 1:71c9c97c9f3d 50 //pUart->printf("[CommandParser::parse] Invalid Format\n");
Inscape_ao 1:71c9c97c9f3d 51 this->reply(false, CommandParser::CmdReply::InvalidFmt);
Inscape_ao 0:c347f602596d 52 return -1;
Inscape_ao 0:c347f602596d 53 }
Inscape_ao 0:c347f602596d 54 /** check Command DeviceID */
Inscape_ao 0:c347f602596d 55 if ((head[1] - '0') != deviceID) {
Inscape_ao 1:71c9c97c9f3d 56 //pUart->printf("[CommandParser::parse] Ignore (DST is other)\n");
Inscape_ao 0:c347f602596d 57 return 0;
Inscape_ao 0:c347f602596d 58 }
Inscape_ao 0:c347f602596d 59
Inscape_ao 0:c347f602596d 60 /** search Command */
Inscape_ao 0:c347f602596d 61 head += 3;
Inscape_ao 0:c347f602596d 62 for(rn = 0; rn < ruleTableLen; rn++) {
Inscape_ao 0:c347f602596d 63 int check;
Inscape_ao 0:c347f602596d 64 check = strncmp((const char*)(ruleTable[rn].cmdName),
Inscape_ao 0:c347f602596d 65 (const char*)(head),CmdNameLen);
Inscape_ao 0:c347f602596d 66 if (check != 0) {
Inscape_ao 0:c347f602596d 67 continue;
Inscape_ao 0:c347f602596d 68 }
Inscape_ao 0:c347f602596d 69 head += 4;
Inscape_ao 1:71c9c97c9f3d 70 return (*ruleTable[rn].func)(this, head);
Inscape_ao 0:c347f602596d 71
Inscape_ao 0:c347f602596d 72 }
Inscape_ao 1:71c9c97c9f3d 73 //pUart->printf("[CommandParser::parse] Invalid Command");
Inscape_ao 1:71c9c97c9f3d 74 this->reply(false, CommandParser::CmdReply::InvalidCmd);
Inscape_ao 0:c347f602596d 75 return -1;
Inscape_ao 1:71c9c97c9f3d 76 }
Inscape_ao 1:71c9c97c9f3d 77
Inscape_ao 1:71c9c97c9f3d 78 int CommandParser::getDeviceID(void)
Inscape_ao 1:71c9c97c9f3d 79 {
Inscape_ao 1:71c9c97c9f3d 80 return deviceID;
Inscape_ao 1:71c9c97c9f3d 81 }
Inscape_ao 1:71c9c97c9f3d 82
Inscape_ao 1:71c9c97c9f3d 83 void CommandParser::reply(bool ack, int replyCode)
Inscape_ao 1:71c9c97c9f3d 84 {
Inscape_ao 1:71c9c97c9f3d 85 char replyStr[] = "0000";
Inscape_ao 1:71c9c97c9f3d 86 sprintf(replyStr, "%04d", replyCode);
Inscape_ao 1:71c9c97c9f3d 87 this->reply(ack, replyStr);
Inscape_ao 1:71c9c97c9f3d 88 }
Inscape_ao 1:71c9c97c9f3d 89
Inscape_ao 1:71c9c97c9f3d 90 void CommandParser::reply(bool ack, char* replyStr)
Inscape_ao 1:71c9c97c9f3d 91 {
Inscape_ao 1:71c9c97c9f3d 92 const char CmdReplyStrDefault[] = ":0 ACK 0000\n";
Inscape_ao 1:71c9c97c9f3d 93 const int CmdReplyLen = sizeof(CmdReplyStrDefault) /* include \0 */;
Inscape_ao 1:71c9c97c9f3d 94 const int CmdReplyDevPos = 1;
Inscape_ao 1:71c9c97c9f3d 95 const int CmdReplyAckPos = 3;
Inscape_ao 1:71c9c97c9f3d 96 const int CmdReplyCodePos = 7;
Inscape_ao 1:71c9c97c9f3d 97 const int CmdReplyEndPos = 11;
Inscape_ao 1:71c9c97c9f3d 98
Inscape_ao 1:71c9c97c9f3d 99 char str[CmdReplyLen];
Inscape_ao 1:71c9c97c9f3d 100
Inscape_ao 1:71c9c97c9f3d 101 /* set default handler */
Inscape_ao 1:71c9c97c9f3d 102 memcpy(str, CmdReplyStrDefault, CmdReplyLen);
Inscape_ao 1:71c9c97c9f3d 103 str[CmdReplyDevPos] = '0' + getDeviceID();
Inscape_ao 1:71c9c97c9f3d 104 if (!ack) {
Inscape_ao 1:71c9c97c9f3d 105 memcpy(&str[CmdReplyAckPos], "NAK", 3);
Inscape_ao 1:71c9c97c9f3d 106 }
Inscape_ao 1:71c9c97c9f3d 107 memcpy(&str[CmdReplyCodePos], replyStr, 4);
Inscape_ao 1:71c9c97c9f3d 108 str[CmdReplyEndPos] = '\n';
Inscape_ao 1:71c9c97c9f3d 109 pUart->printf(str);
Inscape_ao 0:c347f602596d 110 }