xrocusOS_ADXL355 version

Dependencies:   mbed SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers event.cpp Source File

event.cpp

00001 /** --- Includes --- */
00002 #include "mbed.h"
00003 #include "TimeManager.h"
00004 #include "UartReceiver.h"
00005 #include "CommandParser.h"
00006 #include "global.h"
00007 #include "string.h"
00008 
00009 static int sampleHanlder(CommandParser *pC, char *arg, int exarg);
00010 static int softResetHanlder(CommandParser *pC, char *arg, int exarg);
00011 static int setTimeHanlder(CommandParser *pC, char *arg, int exarg);
00012 static int getTimeHanlder(CommandParser *pC, char *arg, int exarg);
00013 static int startSDStore(CommandParser *pC, char *arg, int exarg);
00014 static int endSDStore(CommandParser *pC, char *arg, int exarg);
00015 static int writeSDStore(CommandParser *pC, char *arg, int exarg);
00016 
00017 static int configIDHandler(CommandParser *pC, char *arg, int exarg);
00018 static int configWriteHandler(CommandParser *pC, char *arg, int exarg);
00019 static int configReadHandler(CommandParser *pC, char *arg, int exarg);
00020 
00021 static int startRunHandler(CommandParser *pC, char *arg, int exarg);
00022 static int stopReqHandler(CommandParser *pC, char *arg, int exarg);
00023 static int checkRunStatHandler(CommandParser *pC, char *arg, int exarg);
00024 static int setRepeatCountHandler(CommandParser *pC, char *arg, int exarg);
00025 static int setRepeatStrideHandler(CommandParser *pC, char *arg, int exarg);
00026 static int setRepeatSingleShotMode(CommandParser *pC, char *arg, int exarg);
00027 
00028 /* Event Hankder */
00029 CmdParseRule rules[] = {
00030     /* -- sample -- */
00031     {"CMD", sampleHanlder, 0},
00032     /* Control SD-Card(NOT-SDHC) */
00033     {"SDS", startSDStore, 0},
00034     {"SDE", endSDStore, 0},
00035     {"SDW", writeSDStore, 0}, /* for TEST */
00036     /* Get Timestamp */
00037     {"GTS", getTimeHanlder, 0},
00038     /* Set Timestamp */
00039     {"TYR", setTimeHanlder, TimeManager::SetTimeMethod::Year},
00040     {"TMO", setTimeHanlder, TimeManager::SetTimeMethod::Month},
00041     {"TDA", setTimeHanlder, TimeManager::SetTimeMethod::Day},
00042     {"THR", setTimeHanlder, TimeManager::SetTimeMethod::Hour},
00043     {"TMI", setTimeHanlder, TimeManager::SetTimeMethod::Min},
00044     {"TSE", setTimeHanlder, TimeManager::SetTimeMethod::Sec},
00045     /* Sensing Control */
00046     {"CRS", startRunHandler, 0},        /* Run(start) */
00047     {"CRE", stopReqHandler, 0},         /* Stop -- req */
00048     {"CPS", checkRunStatHandler, 0},    /* Check ReadyToRun */
00049     {"RRP", setRepeatCountHandler, 0},  /* set repeat setting => Count */
00050     {"RMC", setRepeatStrideHandler, 0}, /* set repeat setting => Stride */
00051     {"RSH", setRepeatSingleShotMode, 0}, /* set repeat setting => SingleShot */
00052     /* Device Control */
00053     {"SRS", softResetHanlder, 0},     /* TODO: Software Reset */
00054     {"CID", configIDHandler, 0},      /* Config ID Access */
00055     {"CFW", configWriteHandler, 0},   /* Config Write */
00056     {"CFR", configReadHandler, 0},    /* Config Read */
00057     };
00058 
00059 int getNumOfRules = sizeof(rules)/sizeof(CmdParseRule);
00060 
00061 /****************************************************/
00062 /* Event Handlers (Device Control) */
00063 /****************************************************/
00064 static int startRunHandler(CommandParser *pC, char *arg, int exarg)
00065 {
00066     bool success;
00067     success = pDevRept->start();
00068     if (!success) {
00069         pC->reply(false, CommandParser::NakCode::NAK_IN_RUNNING);
00070         return 1;
00071     }
00072     pC->reply();
00073     return 0;    
00074 }
00075 
00076 static int stopReqHandler(CommandParser *pC, char *arg, int exarg)
00077 {
00078     bool success;
00079     success = pDevRept->stop();
00080     if (!success) {
00081         pC->reply(false, CommandParser::NakCode::NAK_IN_STOPPED);
00082         return 1;
00083     }
00084     pC->reply();
00085     return 0;    
00086 }
00087 
00088 static int checkRunStatHandler(CommandParser *pC, char *arg, int exarg)
00089 {
00090     bool success;
00091     success = pDevRept->readyToStart();
00092     if (!success) {
00093         pC->reply(false, CommandParser::NakCode::NAK_IN_RUNNING);
00094         return 1;
00095     }
00096     pC->reply();
00097     return 0;    
00098 }
00099 
00100 static int setRepeatCountHandler(CommandParser *pC, char *arg, int exarg)
00101 {
00102     bool success;
00103     int setvalue = atoi(arg);
00104     success = pDevRept->setRepeatCount(setvalue);
00105     if (!success) {
00106         pC->reply(false, CommandParser::NakCode::NAK_IN_RUNNING);
00107         return 1;
00108     }
00109     pC->reply();
00110     return 0;    
00111 }
00112 static int setRepeatStrideHandler(CommandParser *pC, char *arg, int exarg)
00113 {
00114     bool success;
00115     char msflag = arg[3];
00116     char valsrc[4] = {0};
00117     int setvalue;
00118     
00119     /* this Argment is "xxxs" or "xxxm" */
00120     memcpy(valsrc, arg, 3);
00121     setvalue = atoi(valsrc);
00122     
00123     if (msflag == 'm') {
00124         setvalue *= 60; /* setvalue x 60(min to sec)*/
00125     } else if (msflag == 's') {
00126         /* NOP setvalue = setvalue */
00127     } else {
00128         /* invalid format */
00129         pC->reply(false, CommandParser::NakCode::NAK_INVAL_PARAM);
00130         return 1;
00131     }
00132     success = pDevRept->setRepeatStride(setvalue);
00133     if (!success) {
00134         pC->reply(false, CommandParser::NakCode::NAK_IN_RUNNING);
00135         return 1;
00136     }
00137     pC->reply();
00138     return 0;    
00139 }
00140 
00141 static int setRepeatSingleShotMode(CommandParser *pC, char *arg, int exarg)
00142 {
00143     bool setMode = true;
00144     bool success;
00145     if (strcmp("SET1", arg) == 0) {
00146         setMode = true;
00147     } else if (strcmp("CLR0", arg) == 0) {
00148         setMode = false;
00149     } else {
00150         /* INVALID COMMAND */
00151         pC->reply(false, CommandParser::NakCode::NAK_INVAL_PARAM);
00152         return 1;        
00153     }
00154     success = pDevRept->setRepeatSingleShot(setMode);
00155     if (success != true) {
00156         /* INVALID COMMAND in SetMode */
00157         pC->reply(false, CommandParser::NakCode::NAK_IN_RUNNING);
00158         return 1;        
00159     }
00160     pC->reply();
00161     return 0;
00162 }
00163 
00164 /****************************************************/
00165 /* Event Handlers (Device Control) */
00166 /****************************************************/
00167 static int configIDHandler(CommandParser *pC, char *arg, int exarg)
00168 {
00169     bool success;
00170     int setvalue = strtol(arg, NULL, 16);
00171     success = pDevRept->setConfigId(setvalue);
00172     if (!success) {
00173         pC->reply(false,  CommandParser::NakCode::NAK_INVAL_SEQ);
00174         return 1;
00175     }
00176     pC->reply();
00177     return 0;    
00178 }
00179 
00180 static int configWriteHandler(CommandParser *pC, char *arg, int exarg)
00181 {
00182     bool success;
00183     int setvalue = strtol(arg, NULL, 16);
00184     success = pDevRept->setConfigValue(setvalue);
00185     if (!success) {
00186         pC->reply(false, CommandParser::NakCode::NAK_INVAL_SEQ);
00187         return 1;
00188     }
00189     pC->reply();
00190     return 0;
00191 }
00192 
00193 static int configReadHandler(CommandParser *pC, char *arg, int exarg)
00194 {
00195     bool success;
00196     int getvalue;
00197     Serial *pUart = pC->getCurrentUart();
00198     
00199     success = pDevRept->getConfigValue(&getvalue);
00200     if (!success) {
00201         pC->reply(false, CommandParser::NakCode::NAK_INVAL_SEQ);
00202         return 1;
00203     }
00204     uprintf(":%d CFG 0004 %04x\n", pC->getDeviceID(), getvalue);
00205     return 0;
00206 }
00207 
00208 static int softResetHanlder(CommandParser *pC, char *arg, int exarg)
00209 {   
00210     bool success;
00211     success = pDevRept->resetAllStatus();
00212     if (!success) {
00213         pC->reply(false, 3);
00214         return 1;
00215     }
00216     pC->reply();
00217     return 0;
00218 }
00219 
00220 /****************************************************/
00221 /* Event Handlers */
00222 /****************************************************/
00223 /* Sample Command */
00224 static int sampleHanlder(CommandParser *pC, char *arg, int exarg)
00225 {   
00226     wait(1);
00227     NVIC_SystemReset();
00228     /* Never Reached. */
00229     pC->reply(false, CommandParser::NakCode::NAK_INVAL_SEQ);
00230     return 1;
00231 }
00232 
00233 /****************************************************/
00234 /* Event Handlers (SD Control) */
00235 /****************************************************/
00236 static int startSDStore(CommandParser *pC, char *arg, int exarg)
00237 {
00238     if (pSds->startFileWithTimeStamp("","txt") != true) {
00239         pC->reply(false, CommandParser::NakCode::NAK_INTERNAL_ERR);
00240         return 1;
00241     }
00242     pC->reply();
00243     return 0;
00244 }
00245 
00246 static int endSDStore(CommandParser *pC, char *arg, int exarg)
00247 {
00248     FILE *fp;
00249     if ((fp = pSds->getFilePointer()) == NULL) {
00250         /* NOP */
00251         pC->reply(false, 2);
00252         return 2;
00253     }
00254     fprintf(fp, "call endSDStore(%s)\n", pSds->getFileName());    
00255     pSds->syncFile();
00256     pSds->closeFile();
00257     pC->reply();
00258     return 0;
00259 }
00260 
00261 static int writeSDStore(CommandParser *pC, char *arg, int exarg)
00262 {
00263     FILE *fp;
00264     char curr[TimeManager::TimeStampLength + 1] = {0};
00265     
00266     if ((fp = pSds->getFilePointer()) == NULL) {
00267         /* NOP */
00268         pC->reply(false, CommandParser::NakCode::NAK_INVAL_SEQ);
00269         return 2;
00270     }
00271     pTM->getTimeStamp(curr);
00272     fprintf(fp, "call writeSDStore at %s\n", curr);    
00273     pSds->syncFile();
00274     pC->reply();
00275     return 0;
00276 }
00277 
00278 /****************************************************/
00279 /* Event Handlers (Time Control) */
00280 /****************************************************/
00281 /* Time Set Command */
00282 static int setTimeHanlder(CommandParser *pC, char *arg, int exarg)
00283 {   
00284     bool success = false;
00285     int setvalue = atoi(arg);
00286     
00287     switch (exarg) {
00288     case TimeManager::SetTimeMethod::Year:
00289     case TimeManager::SetTimeMethod::Month:
00290     case TimeManager::SetTimeMethod::Day:
00291     case TimeManager::SetTimeMethod::Hour:
00292     case TimeManager::SetTimeMethod::Min:
00293     case TimeManager::SetTimeMethod::Sec:
00294         success = pTM->setCurrentTime(exarg,setvalue);
00295         break;
00296     }
00297     pC->reply(success, (success)? 0 : CommandParser::NakCode::NAK_INVAL_PARAM );
00298     return 0;
00299 }
00300 
00301 /* Time Get Command */
00302 static int getTimeHanlder(CommandParser *pC, char *arg, int exarg)
00303 {
00304     int len;
00305     char timestamp[TimeManager::TimeStampLength + 1] = {0};
00306     Serial *pUart = pC->getCurrentUart();
00307     len = pTM->getTimeStamp(timestamp);
00308     uprintf(":%d RTS %04d %s\n", pC->getDeviceID(), len, timestamp);
00309     return 0;
00310 }