void APP_Run(void) { CLS1_ConstStdIOType *io; WAIT1_Waitms(1000); /* wait after power-on */ ESP_Init(); SHELL_Init(); io = CLS1_GetStdio(); CLS1_SendStr("\r\n------------------------------------------\r\n", io->stdOut); CLS1_SendStr("ESP8266 with FRDM-KL25Z\r\n", io->stdOut); CLS1_SendStr("------------------------------------------\r\n", io->stdOut); CLS1_PrintPrompt(io); for(;;) { ESP_Process(); SHELL_Parse(); WAIT1_Waitms(10); } }
/* ** =================================================================== ** Method : CLS1_ParseWithCommandTable (component Shell) ** Description : ** Parses a shell command. It handles first the internal ** commands and will call the provided callback. ** Parameters : ** NAME - DESCRIPTION ** * cmd - Pointer to command string ** * io - Pointer to I/O callbacks ** * parseCallback - Pointer to callback ** which will be called to parse commands in ** the user application, or NULL if not used. ** Returns : ** --- - Error code ** =================================================================== */ uint8_t CLS1_ParseWithCommandTable(const uint8_t *cmd, CLS1_ConstStdIOType *io, CLS1_ConstParseCommandCallback *parseCallback) { uint8_t res = ERR_OK; bool handled; #if CLS1_SILENT_PREFIX_CHAR_ENABLED bool silent = FALSE; #endif #if CLS1_MULTI_CMD_ENABLED uint8_t buf[CLS1_MULTI_CMD_SIZE]; uint8_t i; bool parseBuffer, finished; #endif if (*cmd=='\0') { /* empty command */ return ERR_OK; } #if CLS1_MULTI_CMD_ENABLED parseBuffer = FALSE; finished = FALSE; i = 0; for(;;) { /* breaks */ if (i>sizeof(buf)-2) { res = ERR_FAILED; break; /* buffer overflow */ } buf[i] = *cmd; cmd++; i++; #if CLS1_SILENT_PREFIX_CHAR_ENABLED if (i==1 && buf[0]==CLS1_SILENT_PREFIX_CHAR) { /* first character is silent character */ silent |= (bool)(buf[0]==CLS1_SILENT_PREFIX_CHAR); buf[0] = *cmd; /* skip silent character */ cmd++; } #endif if (buf[i-1] == CLS1_MULTI_CMD_CHAR) { /* found separator */ buf[i-1] = '\0'; parseBuffer = TRUE; } else if (buf[i-1]=='\0') { parseBuffer = TRUE; finished = TRUE; } if (parseBuffer) { handled = FALSE; res = CLS1_IterateTable(buf, &handled, io, parseCallback); /* iterate through all parser functions in table */ if (!handled || res!=ERR_OK) { /* no handler has handled the command, or error? */ CLS1_PrintCommandFailed(buf, io); res = ERR_FAILED; } parseBuffer = FALSE; i = 0; /* restart */ } if (finished) { break; /* get out of loop */ } } /* for */ #else #if CLS1_SILENT_PREFIX_CHAR_ENABLED silent = (bool)(*cmd==CLS1_SILENT_PREFIX_CHAR); if (silent) { cmd++; /* skip silent character */ } #endif handled = FALSE; res = CLS1_IterateTable(cmd, &handled, io, parseCallback); /* iterate through all parser functions in table */ if (!handled || res!=ERR_OK) { /* no handler has handled the command? */ CLS1_PrintCommandFailed(cmd, io); res = ERR_FAILED; } #endif #if CLS1_SILENT_PREFIX_CHAR_ENABLED if (!silent) { CLS1_PrintPrompt(io); } #else CLS1_PrintPrompt(io); #endif return res; }
/* ** =================================================================== ** Method : CLS1_ReadLine (component Shell) ** Description : ** Reads a line from stdIn and returns TRUE if we have a line, ** FALSE otherwise. ** Parameters : ** NAME - DESCRIPTION ** * bufStart - Pointer to start of buffer ** * buf - Pointer to buffer where to read in the ** information ** bufSize - size of buffer ** * io - Pointer to I/O callbacks ** Returns : ** --- - TRUE if something has been read, FALSE ** otherwise ** =================================================================== */ bool CLS1_ReadLine(uint8_t *bufStart, uint8_t *buf, size_t bufSize, CLS1_ConstStdIOType *io) { uint8_t c; bool isBackwardHistory; if (io->keyPressed()) { for(;;) { /* while not '\r' or '\n' */ c = '\0'; /* initialize character */ io->stdIn(&c); /* read character */ if (c=='\0') { /* nothing in rx buffer? Something is wrong... */ break; /* get out of loop */ } if (c=='\b' || c=='\177') { /* check for backspace */ if (buf > bufStart) { /* Avoid buffer underflow */ #if CLS1_ECHO_ENABLED io->stdOut('\b'); /* delete character on terminal */ io->stdOut(' '); io->stdOut('\b'); #endif buf--; /* delete last character in buffer */ *buf = '\0'; bufSize++; } } else if (CLS1_IsHistoryCharacter(c, bufStart, buf-bufStart, &isBackwardHistory)) { #if CLS1_HISTORY_ENABLED uint8_t cBuf[3]={'\0','\0','\0'}, cBufIdx = 0; bool prevInHistory; #endif while (c!='\0') { /* empty the rx buffer (escape sequence) */ #if CLS1_HISTORY_ENABLED cBuf[cBufIdx] = c; cBufIdx++; if (cBufIdx==sizeof(cBuf)) { cBufIdx = 0; /* ring buffer */ } #endif c = '\0'; /* initialize character */ io->stdIn(&c); /* read character */ } #if CLS1_HISTORY_ENABLED /* if not an alphanumeric switch to history */ prevInHistory = cBufIdx==0 && cBuf[0]==0x1b && cBuf[1]==0x5b && (cBuf[2]==0x41 /*up*/ || cBuf[2]==0x44 /*left*/); /* up: 0x27 0x5b 0x41 * down: 0x27 0x5b 0x42 * right: 0x27 0x5b 0x43 * left: 0x27 0x5b 0x44 */ if (prevInHistory) { UTIL1_strcpy(bufStart, CLS1_HIST_LEN, CLS1_history[CLS1_history_index]); CLS1_history_index++; /* update the index */ if (CLS1_history_index==CLS1_NOF_HISTORY) { CLS1_history_index = 0; } } else { if (CLS1_history_index==0) { CLS1_history_index = (CLS1_NOF_HISTORY-1); } else { CLS1_history_index--; } UTIL1_strcpy(bufStart, CLS1_HIST_LEN, CLS1_history[CLS1_history_index]); } bufSize = bufSize + buf - bufStart - UTIL1_strlen(bufStart); /* update the buffer */ buf = bufStart + UTIL1_strlen(bufStart); #endif #if CLS1_ECHO_ENABLED CLS1_SendStr((unsigned char*)"\r\n", io->stdOut); CLS1_PrintPrompt(io); CLS1_SendStr(bufStart, io->stdOut); #endif } else { #if CLS1_ECHO_ENABLED io->stdOut(c); /* echo character */ #endif *buf = (uint8_t)c; /* append character to the string */ buf++; bufSize--; if ((c=='\r') || (c=='\n')) { #if CLS1_ECHO_ENABLED CLS1_SendStr((unsigned char*)"\n", io->stdOut); #endif #if CLS1_HISTORY_ENABLED if ((bufStart[0] != '\0') && (bufStart[0] != '\r') && (bufStart[0] != '\n')) { int i; for(i=CLS1_NOF_HISTORY-1; i>0;i--) { UTIL1_strcpy(CLS1_history[i], CLS1_HIST_LEN, CLS1_history[i-1]); /* move previous commands */ } CLS1_history_index = 0; /* update the history with the current command */ UTIL1_strcpy(CLS1_history[0], CLS1_HIST_LEN, bufStart); /* add the current command to the history */ if (buf-bufStart <= CLS1_HIST_LEN) { /* size check */ CLS1_history[0][buf-bufStart-1] = '\0'; } else { CLS1_history[0][CLS1_HIST_LEN-1] = '\0'; } } #endif break; } if (bufSize <= 1) { /* buffer full */ break; } } } /* for */ *buf = '\0'; /* zero terminate string */ return TRUE; } else { return FALSE; } }