diff --git a/README.md b/README.md index 9842fd51..60275bd9 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,6 @@ Use the following default credentials to get access to the PIXELIX web interface ## Variant 2: Configure wifi station SSID and passphrase with the terminal Connect PIXELIX with your PC via usb and start a terminal. Use the following commands to set the wifi SSID and passphrase of your home wifi network: -* Test: ```ping``` * Write wifi passphrase: ```write wifi passphrase ``` * Write wifi SSID: ```write wifi ssid ``` * Restart PIXELIX: ```reset``` @@ -138,6 +137,8 @@ Connect PIXELIX with your PC via usb and start a terminal. Use the following com * A status of 0 means everything is ok. * Other than 0, see their meaning in the low [low level error code table](#the-display-only-shows-a-error-code-like-e4-what-does-that-mean). Note, the status of 1 is equal to E1 in the error code table and etc. +Enter ```help``` to get a list of all supported commands. + ## PIXELIX Is Ready After configuration, restart again and voila, PIXELIX will be available in your wifi network. diff --git a/src/Common/MiniTerminal.cpp b/src/Common/MiniTerminal.cpp index a43bdd9a..c4d6b601 100644 --- a/src/Common/MiniTerminal.cpp +++ b/src/Common/MiniTerminal.cpp @@ -61,41 +61,32 @@ * Local Variables *****************************************************************************/ -/** Command: ping */ -static const char* PING = "ping"; - -/** Command length: ping */ -static const size_t PING_LEN = strlen(PING); - /** Command: reset */ -static const char* RESET = "reset"; - -/** Command length: reset */ -static const size_t RESET_LEN = strlen(RESET); +static const char RESET[] = "reset"; /** Command: write wifi passphrase */ -static const char* WRITE_WIFI_PASSPHRASE = "write wifi passphrase "; - -/** Command length: write wifi passphrase */ -static const size_t WRITE_WIFI_PASSPHRASE_LEN = strlen(WRITE_WIFI_PASSPHRASE); +static const char WRITE_WIFI_PASSPHRASE[] = "write wifi passphrase "; /** Command: write wifi ssid */ -static const char* WRITE_WIFI_SSID = "write wifi ssid "; - -/** Command length: write wifi ssid */ -static const size_t WRITE_WIFI_SSID_LEN = strlen(WRITE_WIFI_SSID); +static const char WRITE_WIFI_SSID[] = "write wifi ssid "; /** Command: get ip */ -static const char* GET_IP = "get ip"; - -/** Command length: get ipaddress */ -static const size_t GET_IP_LEN = strlen(GET_IP); +static const char GET_IP[] = "get ip"; /** Command: status */ -static const char* GET_STATUS = "get status"; +static const char GET_STATUS[] = "get status"; -/** Command length: status */ -static const size_t GET_STATUS_LEN = strlen(GET_STATUS); +/** Command: help */ +static const char HELP[] = "help"; + +const MiniTerminal::CmdTableEntry MiniTerminal::m_cmdTable[] = { + { RESET, &MiniTerminal::cmdReset }, + { WRITE_WIFI_PASSPHRASE, &MiniTerminal::cmdWriteWifiPassphrase }, + { WRITE_WIFI_SSID, &MiniTerminal::cmdWriteWifiSSID }, + { GET_IP, &MiniTerminal::cmdGetIPAddress }, + { GET_STATUS, &MiniTerminal::cmdGetStatus }, + { HELP, &MiniTerminal::cmdHelp }, +}; /****************************************************************************** * Public Methods @@ -110,15 +101,15 @@ void MiniTerminal::process() /* Process the read input data. */ while(read > idx) { - bool echoOn = false; + char currentChar = buffer[idx]; /* Command finished? */ - if (ASCII_LF == buffer[idx]) + if (ASCII_LF == currentChar) { /* Don't echo mechanism, because its too late in case the * command may write a result too. */ - (void)m_stream.write(buffer[idx]); + (void)m_stream.write(currentChar); m_input[m_writeIndex] = '\0'; @@ -133,12 +124,12 @@ void MiniTerminal::process() m_input[m_writeIndex] = '\0'; } /* Remove the last character from command line? */ - else if ((ASCII_DEL == buffer[idx]) || - (ASCII_BS == buffer[idx])) + else if ((ASCII_DEL == currentChar) || + (ASCII_BS == currentChar)) { if (0 < m_writeIndex) { - char removeSeq[] = + static const char removeSeq[] = { ASCII_BS, ASCII_SP, @@ -153,21 +144,15 @@ void MiniTerminal::process() else if (INPUT_BUFFER_SIZE > (m_writeIndex + 1U)) { /* Valid character? */ - if ((' ' <= buffer[idx]) && - ('~' >= buffer[idx])) + if ((' ' <= currentChar) && + ('~' >= currentChar)) { - m_input[m_writeIndex] = buffer[idx]; + m_input[m_writeIndex] = currentChar; ++m_writeIndex; - - echoOn = true; + (void)m_stream.write(currentChar); } } - if (true == echoOn) - { - (void)m_stream.write(buffer[idx]); - } - ++idx; } } @@ -202,42 +187,26 @@ void MiniTerminal::writeError(const char* result) void MiniTerminal::executeCommand(const char* cmdLine) { - if (0 == strcmp(cmdLine, PING)) - { - cmdPing(&cmdLine[PING_LEN]); - } - else if (0 == strcmp(cmdLine, RESET)) - { - cmdReset(&cmdLine[RESET_LEN]); - } - else if (0 == strncmp(cmdLine, WRITE_WIFI_PASSPHRASE, WRITE_WIFI_PASSPHRASE_LEN)) - { - cmdWriteWifiPassphrase(&cmdLine[WRITE_WIFI_PASSPHRASE_LEN]); - } - else if (0 == strncmp(cmdLine, WRITE_WIFI_SSID, WRITE_WIFI_SSID_LEN)) - { - cmdWriteWifiSSID(&cmdLine[WRITE_WIFI_SSID_LEN]); - } - else if (0 == strncmp(cmdLine, GET_IP, GET_IP_LEN)) - { - cmdGetIPAddress(&cmdLine[GET_IP_LEN]); - } - else if (0 == strncmp(cmdLine, GET_STATUS, GET_STATUS_LEN)) + uint32_t idx = 0U; + + for (idx = 0U; UTIL_ARRAY_NUM(m_cmdTable) > idx; ++idx) { - cmdGetStatus(&cmdLine[GET_STATUS_LEN]); + const CmdTableEntry entry = m_cmdTable[idx]; + const size_t len = strlen(entry.cmdStr); + + if (0 == strncmp(cmdLine, entry.cmdStr, len)) + { + (this->*entry.handler)(&cmdLine[len]); + break; + } } - else + + if (UTIL_ARRAY_NUM(m_cmdTable) == idx) { writeError("Unknown command.\n"); } } -void MiniTerminal::cmdPing(const char* par) -{ - UTIL_NOT_USED(par); - writeSuccessful("pong\n"); -} - void MiniTerminal::cmdReset(const char* par) { UTIL_NOT_USED(par); @@ -291,37 +260,51 @@ void MiniTerminal::cmdWriteWifiSSID(const char* par) void MiniTerminal::cmdGetIPAddress(const char* par) { - if (nullptr != par) - { - String result; - - if (WIFI_MODE_AP == WiFi.getMode()) - { - result = WiFi.softAPIP().toString(); - } - else - { - result = WiFi.localIP().toString(); - } + UTIL_NOT_USED(par); - result += "\n"; + String result; - writeSuccessful(result.c_str()); + if (WIFI_MODE_AP == WiFi.getMode()) + { + result = WiFi.softAPIP().toString(); } + else + { + result = WiFi.localIP().toString(); + } + + result += "\n"; + + writeSuccessful(result.c_str()); } void MiniTerminal::cmdGetStatus(const char* par) { - if (nullptr != par) - { - ErrorState::ErrorId status = ErrorState::getInstance().getErrorId(); - String result; + UTIL_NOT_USED(par); + + ErrorState::ErrorId status = ErrorState::getInstance().getErrorId(); + String result; + + result += static_cast(status); + result += "\n"; + + writeSuccessful(result.c_str()); +} + +void MiniTerminal::cmdHelp(const char* par) +{ + UTIL_NOT_USED(par); - result += static_cast(status); - result += "\n"; + m_stream.write("Supported commands:\n"); - writeSuccessful(result.c_str()); + for (size_t idx = 0U; UTIL_ARRAY_NUM(m_cmdTable) > idx; ++idx) + { + (void)m_stream.write(" "); + (void)m_stream.write(m_cmdTable[idx].cmdStr); + (void)m_stream.write("\n"); } + + writeSuccessful(); } /****************************************************************************** diff --git a/src/Common/MiniTerminal.h b/src/Common/MiniTerminal.h index 0707ddca..4a567795 100644 --- a/src/Common/MiniTerminal.h +++ b/src/Common/MiniTerminal.h @@ -107,13 +107,22 @@ class MiniTerminal return isRestartRequested; } + /** + * @brief Table entry for known terminal commands + * + */ + struct CmdTableEntry { + const char *cmdStr; /**< Command string. */ + void (MiniTerminal::*handler)(const char *); /**< Command handler function. */ + }; + private: static const char ASCII_BS = 8; /**< ASCII backspace value */ static const char ASCII_LF = 10; /**< ASCII line feed value */ static const char ASCII_SP = 32; /**< ASCII space value */ static const char ASCII_DEL = 127; /**< ASCII delete value */ - static const size_t LOCAL_BUFFER_SIZE = 10U; /**< Buffer size in byte to read during processing. */ + static const size_t LOCAL_BUFFER_SIZE = 12U; /**< Buffer size in byte to read during processing. */ static const size_t INPUT_BUFFER_SIZE = 80U; /**< Buffer size of one input command line in byte. */ Stream& m_stream; /**< In/Out-stream. */ @@ -121,6 +130,8 @@ class MiniTerminal size_t m_writeIndex; /**< Write index to the command line buffer. */ bool m_isRestartRequested; /**< Restart requested? */ + static const CmdTableEntry m_cmdTable[]; /**< Table with supported commands. */ + /** * Write successful response. * @@ -142,13 +153,6 @@ class MiniTerminal */ void executeCommand(const char* cmdLine); - /** - * Ping command. - * - * @param[in] par Parameter - */ - void cmdPing(const char* par); - /** * Reset the device. * @@ -183,6 +187,13 @@ class MiniTerminal * @param[in] par Parameter */ void cmdGetStatus(const char* par); + + /** + * Print command help message. + * + * @param[in] par Parameter + */ + void cmdHelp(const char* par); }; /******************************************************************************