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:
Sat May 11 04:03:33 2019 +0000
Revision:
7:9ab8809f9693
Parent:
5:a37e3a15444b
Child:
8:b18a8764ecae
add Repeatable Controllor; add Dummy Devicedriver; add Sensing Control Command; (All Host->Device Commands are already implemented)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Inscape_ao 2:a694440145e9 1 /** --- Includes --- */
Inscape_ao 2:a694440145e9 2 #include "mbed.h"
Inscape_ao 2:a694440145e9 3 #include "TimeManager.h"
Inscape_ao 2:a694440145e9 4 #include "UartReceiver.h"
Inscape_ao 2:a694440145e9 5 #include "CommandParser.h"
Inscape_ao 2:a694440145e9 6 #include "global.h"
Inscape_ao 2:a694440145e9 7 #include "string.h"
Inscape_ao 2:a694440145e9 8
Inscape_ao 2:a694440145e9 9 static int sampleHanlder(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 10 static int softResetHanlder(CommandParser *pC, char *arg, int exarg);
Inscape_ao 2:a694440145e9 11 static int setTimeHanlder(CommandParser *pC, char *arg, int exarg);
Inscape_ao 2:a694440145e9 12 static int getTimeHanlder(CommandParser *pC, char *arg, int exarg);
Inscape_ao 5:a37e3a15444b 13 static int startSDStore(CommandParser *pC, char *arg, int exarg);
Inscape_ao 5:a37e3a15444b 14 static int endSDStore(CommandParser *pC, char *arg, int exarg);
Inscape_ao 5:a37e3a15444b 15 static int writeSDStore(CommandParser *pC, char *arg, int exarg);
Inscape_ao 2:a694440145e9 16
Inscape_ao 7:9ab8809f9693 17 static int configIDHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 18 static int configWriteHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 19 static int configReadHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 20
Inscape_ao 7:9ab8809f9693 21 static int startRunHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 22 static int stopReqHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 23 static int checkRunStatHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 24 static int setRepeatCountHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 25 static int setRepeatStrideHandler(CommandParser *pC, char *arg, int exarg);
Inscape_ao 7:9ab8809f9693 26
Inscape_ao 2:a694440145e9 27 /* Event Hankder */
Inscape_ao 2:a694440145e9 28 CmdParseRule rules[] = {
Inscape_ao 7:9ab8809f9693 29 /* -- sample -- */
Inscape_ao 2:a694440145e9 30 {"CMD", sampleHanlder, 0},
Inscape_ao 7:9ab8809f9693 31 /* Control SD-Card(NOT-SDHC) */
Inscape_ao 5:a37e3a15444b 32 {"SDS", startSDStore, 0},
Inscape_ao 5:a37e3a15444b 33 {"SDE", endSDStore, 0},
Inscape_ao 5:a37e3a15444b 34 {"SDW", writeSDStore, 0}, /* for TEST */
Inscape_ao 7:9ab8809f9693 35 /* Get Timestamp */
Inscape_ao 2:a694440145e9 36 {"GTS", getTimeHanlder, 0},
Inscape_ao 7:9ab8809f9693 37 /* Set Timestamp */
Inscape_ao 2:a694440145e9 38 {"TYR", setTimeHanlder, TimeManager::SetTimeMethod::Year},
Inscape_ao 2:a694440145e9 39 {"TMO", setTimeHanlder, TimeManager::SetTimeMethod::Month},
Inscape_ao 2:a694440145e9 40 {"TDA", setTimeHanlder, TimeManager::SetTimeMethod::Day},
Inscape_ao 2:a694440145e9 41 {"THR", setTimeHanlder, TimeManager::SetTimeMethod::Hour},
Inscape_ao 2:a694440145e9 42 {"TMI", setTimeHanlder, TimeManager::SetTimeMethod::Min},
Inscape_ao 2:a694440145e9 43 {"TSE", setTimeHanlder, TimeManager::SetTimeMethod::Sec},
Inscape_ao 7:9ab8809f9693 44 /* Sensing Control */
Inscape_ao 7:9ab8809f9693 45 {"CRS", startRunHandler, 0}, /* Run(start) */
Inscape_ao 7:9ab8809f9693 46 {"CRE", stopReqHandler, 0}, /* Stop -- req */
Inscape_ao 7:9ab8809f9693 47 {"CPS", checkRunStatHandler, 0}, /* Check ReadyToRun */
Inscape_ao 7:9ab8809f9693 48 {"RRP", setRepeatCountHandler, 0}, /* set repeat setting => Count */
Inscape_ao 7:9ab8809f9693 49 {"RMC", setRepeatStrideHandler, 0}, /* set repeat setting => Stride */
Inscape_ao 7:9ab8809f9693 50 /* Device Control */
Inscape_ao 7:9ab8809f9693 51 {"SRS", softResetHanlder, 0}, /* TODO: Software Reset */
Inscape_ao 7:9ab8809f9693 52 {"CID", configIDHandler, 0}, /* Config ID Access */
Inscape_ao 7:9ab8809f9693 53 {"CFW", configWriteHandler, 0}, /* Config Write */
Inscape_ao 7:9ab8809f9693 54 {"CFR", configReadHandler, 0}, /* Config Read */
Inscape_ao 2:a694440145e9 55 };
Inscape_ao 2:a694440145e9 56
Inscape_ao 2:a694440145e9 57 int getNumOfRules = sizeof(rules)/sizeof(CmdParseRule);
Inscape_ao 2:a694440145e9 58
Inscape_ao 7:9ab8809f9693 59 /****************************************************/
Inscape_ao 7:9ab8809f9693 60 /* Event Handlers (Device Control) */
Inscape_ao 7:9ab8809f9693 61 /****************************************************/
Inscape_ao 7:9ab8809f9693 62 static int startRunHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 63 {
Inscape_ao 7:9ab8809f9693 64 bool success;
Inscape_ao 7:9ab8809f9693 65 success = pDevRept->start();
Inscape_ao 7:9ab8809f9693 66 if (!success) {
Inscape_ao 7:9ab8809f9693 67 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 68 return 1;
Inscape_ao 7:9ab8809f9693 69 }
Inscape_ao 7:9ab8809f9693 70 pC->reply();
Inscape_ao 7:9ab8809f9693 71 return 0;
Inscape_ao 7:9ab8809f9693 72 }
Inscape_ao 7:9ab8809f9693 73
Inscape_ao 7:9ab8809f9693 74 static int stopReqHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 75 {
Inscape_ao 7:9ab8809f9693 76 bool success;
Inscape_ao 7:9ab8809f9693 77 success = pDevRept->stop();
Inscape_ao 7:9ab8809f9693 78 if (!success) {
Inscape_ao 7:9ab8809f9693 79 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 80 return 1;
Inscape_ao 7:9ab8809f9693 81 }
Inscape_ao 7:9ab8809f9693 82 pC->reply();
Inscape_ao 7:9ab8809f9693 83 return 0;
Inscape_ao 7:9ab8809f9693 84 }
Inscape_ao 7:9ab8809f9693 85
Inscape_ao 7:9ab8809f9693 86 static int checkRunStatHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 87 {
Inscape_ao 7:9ab8809f9693 88 bool success;
Inscape_ao 7:9ab8809f9693 89 success = pDevRept->readyToStart();
Inscape_ao 7:9ab8809f9693 90 if (!success) {
Inscape_ao 7:9ab8809f9693 91 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 92 return 1;
Inscape_ao 7:9ab8809f9693 93 }
Inscape_ao 7:9ab8809f9693 94 pC->reply();
Inscape_ao 7:9ab8809f9693 95 return 0;
Inscape_ao 7:9ab8809f9693 96 }
Inscape_ao 7:9ab8809f9693 97
Inscape_ao 7:9ab8809f9693 98 static int setRepeatCountHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 99 {
Inscape_ao 7:9ab8809f9693 100 bool success;
Inscape_ao 7:9ab8809f9693 101 int setvalue = atoi(arg);
Inscape_ao 7:9ab8809f9693 102 success = pDevRept->setRepeatCount(setvalue);
Inscape_ao 7:9ab8809f9693 103 if (!success) {
Inscape_ao 7:9ab8809f9693 104 pC->reply(false, 4);
Inscape_ao 7:9ab8809f9693 105 return 1;
Inscape_ao 7:9ab8809f9693 106 }
Inscape_ao 7:9ab8809f9693 107 pC->reply();
Inscape_ao 7:9ab8809f9693 108 return 0;
Inscape_ao 7:9ab8809f9693 109 }
Inscape_ao 7:9ab8809f9693 110 static int setRepeatStrideHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 111 {
Inscape_ao 7:9ab8809f9693 112 bool success;
Inscape_ao 7:9ab8809f9693 113 char msflag = arg[3];
Inscape_ao 7:9ab8809f9693 114 char valsrc[4] = {0};
Inscape_ao 7:9ab8809f9693 115 int setvalue;
Inscape_ao 7:9ab8809f9693 116
Inscape_ao 7:9ab8809f9693 117 /* this Argment is "xxxs" or "xxxm" */
Inscape_ao 7:9ab8809f9693 118 memcpy(valsrc, arg, 3);
Inscape_ao 7:9ab8809f9693 119 setvalue = atoi(valsrc);
Inscape_ao 7:9ab8809f9693 120
Inscape_ao 7:9ab8809f9693 121 if (msflag == 'm') {
Inscape_ao 7:9ab8809f9693 122 setvalue *= 60; /* setvalue x 60(min to sec)*/
Inscape_ao 7:9ab8809f9693 123 } else if (msflag == 's') {
Inscape_ao 7:9ab8809f9693 124 /* NOP setvalue = setvalue */
Inscape_ao 7:9ab8809f9693 125 } else {
Inscape_ao 7:9ab8809f9693 126 /* invalid format */
Inscape_ao 7:9ab8809f9693 127 pC->reply(false, 6);
Inscape_ao 7:9ab8809f9693 128 return 1;
Inscape_ao 7:9ab8809f9693 129 }
Inscape_ao 7:9ab8809f9693 130 success = pDevRept->setRepeatStride(setvalue);
Inscape_ao 7:9ab8809f9693 131 if (!success) {
Inscape_ao 7:9ab8809f9693 132 pC->reply(false, 5);
Inscape_ao 7:9ab8809f9693 133 return 1;
Inscape_ao 7:9ab8809f9693 134 }
Inscape_ao 7:9ab8809f9693 135 pC->reply();
Inscape_ao 7:9ab8809f9693 136 return 0;
Inscape_ao 7:9ab8809f9693 137 }
Inscape_ao 7:9ab8809f9693 138 /****************************************************/
Inscape_ao 7:9ab8809f9693 139 /* Event Handlers (Device Control) */
Inscape_ao 7:9ab8809f9693 140 /****************************************************/
Inscape_ao 7:9ab8809f9693 141 static int configIDHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 142 {
Inscape_ao 7:9ab8809f9693 143 bool success;
Inscape_ao 7:9ab8809f9693 144 int setvalue = strtol(arg, NULL, 16);
Inscape_ao 7:9ab8809f9693 145 success = pDevRept->setConfigId(setvalue);
Inscape_ao 7:9ab8809f9693 146 if (!success) {
Inscape_ao 7:9ab8809f9693 147 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 148 return 1;
Inscape_ao 7:9ab8809f9693 149 }
Inscape_ao 7:9ab8809f9693 150 pC->reply();
Inscape_ao 7:9ab8809f9693 151 return 0;
Inscape_ao 7:9ab8809f9693 152 }
Inscape_ao 7:9ab8809f9693 153
Inscape_ao 7:9ab8809f9693 154 static int configWriteHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 155 {
Inscape_ao 7:9ab8809f9693 156 bool success;
Inscape_ao 7:9ab8809f9693 157 int setvalue = strtol(arg, NULL, 16);
Inscape_ao 7:9ab8809f9693 158 success = pDevRept->setConfigValue(setvalue);
Inscape_ao 7:9ab8809f9693 159 if (!success) {
Inscape_ao 7:9ab8809f9693 160 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 161 return 1;
Inscape_ao 7:9ab8809f9693 162 }
Inscape_ao 7:9ab8809f9693 163 pC->reply();
Inscape_ao 7:9ab8809f9693 164 return 0;
Inscape_ao 7:9ab8809f9693 165 }
Inscape_ao 7:9ab8809f9693 166
Inscape_ao 7:9ab8809f9693 167 static int configReadHandler(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 168 {
Inscape_ao 7:9ab8809f9693 169 bool success;
Inscape_ao 7:9ab8809f9693 170 int getvalue;
Inscape_ao 7:9ab8809f9693 171 Serial *pUart = pC->getCurrentUart();
Inscape_ao 7:9ab8809f9693 172
Inscape_ao 7:9ab8809f9693 173 success = pDevRept->getConfigValue(&getvalue);
Inscape_ao 7:9ab8809f9693 174 if (!success) {
Inscape_ao 7:9ab8809f9693 175 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 176 return 1;
Inscape_ao 7:9ab8809f9693 177 }
Inscape_ao 7:9ab8809f9693 178 pUart->printf(":%d CFG 0004 %04x\n", pC->getDeviceID(), getvalue);
Inscape_ao 7:9ab8809f9693 179 return 0;
Inscape_ao 7:9ab8809f9693 180 }
Inscape_ao 7:9ab8809f9693 181
Inscape_ao 7:9ab8809f9693 182 static int softResetHanlder(CommandParser *pC, char *arg, int exarg)
Inscape_ao 7:9ab8809f9693 183 {
Inscape_ao 7:9ab8809f9693 184 bool success;
Inscape_ao 7:9ab8809f9693 185 success = pDevRept->resetAllStatus();
Inscape_ao 7:9ab8809f9693 186 if (!success) {
Inscape_ao 7:9ab8809f9693 187 pC->reply(false, 3);
Inscape_ao 7:9ab8809f9693 188 return 1;
Inscape_ao 7:9ab8809f9693 189 }
Inscape_ao 7:9ab8809f9693 190 pC->reply();
Inscape_ao 7:9ab8809f9693 191 return 0;
Inscape_ao 7:9ab8809f9693 192 }
Inscape_ao 2:a694440145e9 193
Inscape_ao 2:a694440145e9 194 /****************************************************/
Inscape_ao 2:a694440145e9 195 /* Event Handlers */
Inscape_ao 2:a694440145e9 196 /****************************************************/
Inscape_ao 2:a694440145e9 197 /* Sample Command */
Inscape_ao 2:a694440145e9 198 static int sampleHanlder(CommandParser *pC, char *arg, int exarg)
Inscape_ao 2:a694440145e9 199 {
Inscape_ao 2:a694440145e9 200 pC->reply();
Inscape_ao 2:a694440145e9 201 return 0;
Inscape_ao 2:a694440145e9 202 }
Inscape_ao 2:a694440145e9 203
Inscape_ao 5:a37e3a15444b 204 /****************************************************/
Inscape_ao 5:a37e3a15444b 205 /* Event Handlers (SD Control) */
Inscape_ao 5:a37e3a15444b 206 /****************************************************/
Inscape_ao 5:a37e3a15444b 207 static int startSDStore(CommandParser *pC, char *arg, int exarg)
Inscape_ao 5:a37e3a15444b 208 {
Inscape_ao 5:a37e3a15444b 209 if (pSds->startFileWithTimeStamp("","txt") != true) {
Inscape_ao 5:a37e3a15444b 210 pC->reply(false, 1);
Inscape_ao 5:a37e3a15444b 211 return 1;
Inscape_ao 5:a37e3a15444b 212 }
Inscape_ao 5:a37e3a15444b 213 pC->reply();
Inscape_ao 5:a37e3a15444b 214 return 0;
Inscape_ao 5:a37e3a15444b 215 }
Inscape_ao 5:a37e3a15444b 216
Inscape_ao 5:a37e3a15444b 217 static int endSDStore(CommandParser *pC, char *arg, int exarg)
Inscape_ao 5:a37e3a15444b 218 {
Inscape_ao 5:a37e3a15444b 219 FILE *fp;
Inscape_ao 5:a37e3a15444b 220 if ((fp = pSds->getFilePointer()) == NULL) {
Inscape_ao 5:a37e3a15444b 221 /* NOP */
Inscape_ao 5:a37e3a15444b 222 pC->reply(false, 2);
Inscape_ao 5:a37e3a15444b 223 return 2;
Inscape_ao 5:a37e3a15444b 224 }
Inscape_ao 5:a37e3a15444b 225 fprintf(fp, "call endSDStore(%s)\n", pSds->getFileName());
Inscape_ao 5:a37e3a15444b 226 pSds->syncFile();
Inscape_ao 5:a37e3a15444b 227 pSds->closeFile();
Inscape_ao 5:a37e3a15444b 228 pC->reply();
Inscape_ao 5:a37e3a15444b 229 return 0;
Inscape_ao 5:a37e3a15444b 230 }
Inscape_ao 5:a37e3a15444b 231
Inscape_ao 5:a37e3a15444b 232 static int writeSDStore(CommandParser *pC, char *arg, int exarg)
Inscape_ao 5:a37e3a15444b 233 {
Inscape_ao 5:a37e3a15444b 234 FILE *fp;
Inscape_ao 5:a37e3a15444b 235 char curr[TimeManager::TimeStampLength + 1] = {0};
Inscape_ao 5:a37e3a15444b 236
Inscape_ao 5:a37e3a15444b 237 if ((fp = pSds->getFilePointer()) == NULL) {
Inscape_ao 5:a37e3a15444b 238 /* NOP */
Inscape_ao 5:a37e3a15444b 239 pC->reply(false, 2);
Inscape_ao 5:a37e3a15444b 240 return 2;
Inscape_ao 5:a37e3a15444b 241 }
Inscape_ao 5:a37e3a15444b 242 pTM->getTimeStamp(curr);
Inscape_ao 5:a37e3a15444b 243 fprintf(fp, "call writeSDStore at %s\n", curr);
Inscape_ao 5:a37e3a15444b 244 pSds->syncFile();
Inscape_ao 5:a37e3a15444b 245 pC->reply();
Inscape_ao 5:a37e3a15444b 246 return 0;
Inscape_ao 5:a37e3a15444b 247 }
Inscape_ao 5:a37e3a15444b 248
Inscape_ao 5:a37e3a15444b 249 /****************************************************/
Inscape_ao 5:a37e3a15444b 250 /* Event Handlers (Time Control) */
Inscape_ao 5:a37e3a15444b 251 /****************************************************/
Inscape_ao 2:a694440145e9 252 /* Time Set Command */
Inscape_ao 2:a694440145e9 253 static int setTimeHanlder(CommandParser *pC, char *arg, int exarg)
Inscape_ao 2:a694440145e9 254 {
Inscape_ao 2:a694440145e9 255 bool success = false;
Inscape_ao 2:a694440145e9 256 int setvalue = atoi(arg);
Inscape_ao 2:a694440145e9 257
Inscape_ao 2:a694440145e9 258 switch (exarg) {
Inscape_ao 2:a694440145e9 259 case TimeManager::SetTimeMethod::Year:
Inscape_ao 2:a694440145e9 260 case TimeManager::SetTimeMethod::Month:
Inscape_ao 2:a694440145e9 261 case TimeManager::SetTimeMethod::Day:
Inscape_ao 2:a694440145e9 262 case TimeManager::SetTimeMethod::Hour:
Inscape_ao 2:a694440145e9 263 case TimeManager::SetTimeMethod::Min:
Inscape_ao 2:a694440145e9 264 case TimeManager::SetTimeMethod::Sec:
Inscape_ao 2:a694440145e9 265 success = pTM->setCurrentTime(exarg,setvalue);
Inscape_ao 2:a694440145e9 266 break;
Inscape_ao 2:a694440145e9 267 }
Inscape_ao 2:a694440145e9 268 pC->reply(success, (success)? 0 : setvalue );
Inscape_ao 2:a694440145e9 269 return 0;
Inscape_ao 2:a694440145e9 270 }
Inscape_ao 2:a694440145e9 271
Inscape_ao 2:a694440145e9 272 /* Time Get Command */
Inscape_ao 2:a694440145e9 273 static int getTimeHanlder(CommandParser *pC, char *arg, int exarg)
Inscape_ao 2:a694440145e9 274 {
Inscape_ao 2:a694440145e9 275 int len;
Inscape_ao 2:a694440145e9 276 char timestamp[TimeManager::TimeStampLength + 1] = {0};
Inscape_ao 2:a694440145e9 277 Serial *pUart = pC->getCurrentUart();
Inscape_ao 2:a694440145e9 278 len = pTM->getTimeStamp(timestamp);
Inscape_ao 2:a694440145e9 279 pUart->printf(":%d RTS %04d %s\n", pC->getDeviceID(), len, timestamp);
Inscape_ao 2:a694440145e9 280 return 0;
Inscape_ao 2:a694440145e9 281 }