Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added help terminal command #182

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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.

Expand Down
117 changes: 55 additions & 62 deletions src/Common/MiniTerminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,40 +62,35 @@
*****************************************************************************/

/** Command: ping */
static const char* PING = "ping";

/** Command length: ping */
static const size_t PING_LEN = strlen(PING);
static const char PING[] = "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[] = {
{ PING, sizeof(PING) - 1U, &MiniTerminal::cmdPing },
{ RESET, sizeof(RESET) - 1U, &MiniTerminal::cmdReset },
{ WRITE_WIFI_PASSPHRASE, sizeof(WRITE_WIFI_PASSPHRASE) - 1U, &MiniTerminal::cmdWriteWifiPassphrase },
{ WRITE_WIFI_SSID, sizeof(WRITE_WIFI_SSID) - 1U, &MiniTerminal::cmdWriteWifiSSID },
{ GET_IP, sizeof(GET_IP) - 1U, &MiniTerminal::cmdGetIPAddress },
{ GET_STATUS, sizeof(GET_STATUS) - 1U, &MiniTerminal::cmdGetStatus },
{ HELP, sizeof(HELP) - 1U, &MiniTerminal::cmdHelp },
};

/******************************************************************************
* Public Methods
Expand All @@ -110,15 +105,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';

Expand All @@ -133,8 +128,8 @@ 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)
{
Expand All @@ -153,21 +148,14 @@ 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_writeIndex;

echoOn = true;
m_input[m_writeIndex++] = currentChar;
BlueAndi marked this conversation as resolved.
Show resolved Hide resolved
(void)m_stream.write(currentChar);
}
}

if (true == echoOn)
{
(void)m_stream.write(buffer[idx]);
}

++idx;
}
}
Expand Down Expand Up @@ -202,31 +190,20 @@ 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]);

if (0 == strncmp(cmdLine, entry.cmdStr, entry.cmdLen))
{
(this->*entry.handler)(&cmdLine[entry.cmdLen]);
break;
}
}
else

if (UTIL_ARRAY_NUM(m_cmdTable) == idx)
{
writeError("Unknown command.\n");
}
Expand Down Expand Up @@ -324,6 +301,22 @@ void MiniTerminal::cmdGetStatus(const char* par)
}
}

void MiniTerminal::cmdHelp(const char* par)
{
UTIL_NOT_USED(par);

m_stream.write("Supported commands:\n");

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();
}

/******************************************************************************
* External Functions
*****************************************************************************/
Expand Down
21 changes: 20 additions & 1 deletion src/Common/MiniTerminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,32 @@ class MiniTerminal
return isRestartRequested;
}

/**
* @brief Table entry for known terminal commands
*
*/
struct CmdTableEntry {
const char *cmdStr; /**< Command string. */
size_t cmdLen; /**< Length of 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. */
char m_input[INPUT_BUFFER_SIZE]; /**< Input command line buffer. */
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.
*
Expand Down Expand Up @@ -183,6 +195,13 @@ class MiniTerminal
* @param[in] par Parameter
*/
void cmdGetStatus(const char* par);

/**
* print command help message.
BlueAndi marked this conversation as resolved.
Show resolved Hide resolved
*
* @param[in] par Parameter
*/
void cmdHelp(const char* par);
};

/******************************************************************************
Expand Down