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 3 commits
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <your-passphrase>```
* Write wifi SSID: ```write wifi ssid <your-ssid>```
* Restart PIXELIX: ```reset```
Expand All @@ -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.

Expand Down
165 changes: 74 additions & 91 deletions src/Common/MiniTerminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';

Expand All @@ -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 char removeSeq[] =
BlueAndi marked this conversation as resolved.
Show resolved Hide resolved
{
ASCII_BS,
ASCII_SP,
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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];
BlueAndi marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand Down Expand Up @@ -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<int32_t>(status);
result += "\n";

writeSuccessful(result.c_str());
}

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

result += static_cast<int32_t>(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();
}

/******************************************************************************
Expand Down
27 changes: 19 additions & 8 deletions src/Common/MiniTerminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,31 @@ 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. */
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 All @@ -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.
*
Expand Down Expand Up @@ -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);
};

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