My Project
|
Base class for a command shell. More...
#include <uuid/console.h>
Classes | |
class | BlockingData |
Data for the Mode::BLOCKING shell mode. More... | |
class | DelayData |
Data for the Mode::DELAY shell mode. More... | |
class | ModeData |
Base class of data for a shell mode. More... | |
class | PasswordData |
Data for the Mode::PASSWORD shell mode. More... | |
class | QueuedLogMessage |
Log message that has been queued. More... | |
Public Types | |
using | password_function = std::function< void(Shell &shell, bool completed, const std::string &password)> |
Function to handle the response to a password entry prompt. More... | |
using | delay_function = std::function< void(Shell &shell)> |
Function to handle the end of an execution delay. More... | |
using | blocking_function = std::function< bool(Shell &shell, bool stop)> |
Function to handle a blocking operation. More... | |
Public Member Functions | |
Shell (Stream &stream, std::shared_ptr< Commands > commands, unsigned int context=0, unsigned int flags=0) | |
Create a new Shell operating on a Stream with the given commands, default context and initial flags. More... | |
void | start () |
Perform startup process for this shell. More... | |
void | loop_one () |
Perform one execution step of this shell. More... | |
bool | running () const |
Determine if this shell is still running. More... | |
void | stop () |
Stop this shell from running. More... | |
virtual void | operator<< (std::shared_ptr< uuid::log::Message > message) |
Add a new log message. More... | |
uuid::log::Level | log_level () const |
Get the current log level. More... | |
void | log_level (uuid::log::Level level) |
Set the current log level. More... | |
size_t | maximum_command_line_length () const |
Get the maximum length of a command line. More... | |
void | maximum_command_line_length (size_t length) |
Set the maximum length of a command line. More... | |
size_t | maximum_log_messages () const |
Get the maximum number of queued log messages. More... | |
void | maximum_log_messages (size_t count) |
Set the maximum number of queued log messages. More... | |
unsigned long | idle_timeout () const |
Get the idle timeout. More... | |
void | idle_timeout (unsigned long timeout) |
Set the idle timeout. More... | |
unsigned int | context () const |
Get the context at the top of the stack. More... | |
void | enter_context (unsigned int context) |
Push a new context onto the stack. More... | |
virtual bool | exit_context () |
Pop a context off the stack. More... | |
void | add_flags (unsigned int flags) |
Add one or more flags to the current flags. More... | |
bool | has_flags (unsigned int flags, unsigned int not_flags=0) const |
Check if the current flags include all of the specified flags and none of the specified not_flags. More... | |
bool | has_all_flags (unsigned int flags) const |
Check if the current flags include all of the specified flags. More... | |
bool | has_any_flags (unsigned int flags) const |
Check if the current flags include any of the specified flags. More... | |
void | remove_flags (unsigned int flags) |
Remove one or more flags from the current flags. More... | |
void | enter_password (const __FlashStringHelper *prompt, password_function function) |
Prompt for a password to be entered on this shell. More... | |
void | delay_for (unsigned long ms, delay_function function) |
Stop executing anything on this shell for a period of time. More... | |
void | delay_until (uint64_t ms, delay_function function) |
Stop executing anything on this shell until a future time is reached. More... | |
void | block_with (blocking_function function) |
Execute a blocking function on this shell until it returns true. More... | |
int | available () final override |
Check for available input. More... | |
int | read () final override |
Read one byte from the available input. More... | |
int | peek () final override |
Read one byte from the available input without advancing to the next one. More... | |
size_t | write (uint8_t data) final override |
Write one byte to the output stream. More... | |
size_t | write (const uint8_t *buffer, size_t size) final override |
Write an array of bytes to the output stream. More... | |
void | flush () final override |
Does nothing. More... | |
size_t | print (const std::string &data) |
Output a string. More... | |
size_t | println (const std::string &data) |
Output a string followed by CRLF end of line characters. More... | |
size_t | printf (const char *format,...) |
Output a message. More... | |
size_t | printf (const __FlashStringHelper *format,...) |
Output a message. More... | |
size_t | printfln (const char *format,...) |
Output a message followed by CRLF end of line characters. More... | |
size_t | printfln (const __FlashStringHelper *format,...) |
Output a message followed by CRLF end of line characters. More... | |
Commands::AvailableCommands | available_commands () const |
Get the available commands in the current context and with the current flags. More... | |
void | print_all_available_commands () |
Output a list of all available commands with their arguments. More... | |
Static Public Member Functions | |
static void | loop_all () |
Loop through all registered shell objects. More... | |
static const uuid::log::Logger & | logger () |
Get the built-in uuid::log::Logger instance for shells. More... | |
Static Public Attributes | |
static constexpr size_t | MAX_COMMAND_LINE_LENGTH = 80 |
static constexpr size_t | MAX_LOG_MESSAGES = 20 |
Protected Member Functions | |
virtual void | erase_current_line () |
Output ANSI escape sequence to erase the current line. More... | |
virtual void | erase_characters (size_t count) |
Output ANSI escape sequence to erase characters. More... | |
virtual void | started () |
Startup complete event. More... | |
virtual void | display_banner () |
Output the startup banner. More... | |
virtual std::string | hostname_text () |
Get the hostname to be included in the command prompt. More... | |
virtual std::string | context_text () |
Get the text indicating the current context, to be included in the command prompt. More... | |
virtual std::string | prompt_prefix () |
Get the prefix to be used at the beginning of the command prompt. More... | |
virtual std::string | prompt_suffix () |
Get the prefix to be used at the end of the command prompt. More... | |
virtual void | end_of_transmission () |
The end of transmission character has been received. More... | |
virtual void | stopped () |
Stop request event. More... | |
void | invoke_command (const std::string &line) |
Invoke a command on the shell. More... | |
Private Types | |
enum | Mode : uint8_t { Mode::NORMAL, Mode::PASSWORD, Mode::DELAY, Mode::BLOCKING } |
Current mode of the shell. More... | |
Private Member Functions | |
Shell (const Shell &)=delete | |
Shell & | operator= (const Shell &)=delete |
void | loop_normal () |
Perform one execution step in Mode::NORMAL mode. More... | |
void | loop_password () |
Perform one execution step in Mode::PASSWORD mode. More... | |
void | loop_delay () |
Perform one execution step in Mode::DELAY mode. More... | |
void | loop_blocking () |
Perform one execution step in Mode::BLOCKING mode. More... | |
void | display_prompt () |
Output a prompt on the shell. More... | |
void | output_logs () |
Output queued log messages for this shell. More... | |
void | process_command () |
Try to execute a command with the current command line. More... | |
void | process_completion () |
Try to complete a command from the current command line. More... | |
void | process_password (bool completed) |
Finish password entry. More... | |
void | check_idle_timeout () |
Check idle timeout expiry. More... | |
void | delete_buffer_word (bool display) |
Delete a word from the command line buffer. More... | |
size_t | vprintf (const char *format, va_list ap) |
Output a message. More... | |
size_t | vprintf (const __FlashStringHelper *format, va_list ap) |
Output a message. More... | |
Static Private Member Functions | |
static std::set< std::shared_ptr< Shell > > & | registered_shells () |
Get registered running shells to be executed. More... | |
Private Attributes | |
Stream & | stream_ |
std::shared_ptr< Commands > | commands_ |
std::deque< unsigned int > | context_ |
unsigned int | flags_ = 0 |
std::mutex | mutex_ |
unsigned long | log_message_id_ = 0 |
std::list< QueuedLogMessage > | log_messages_ |
size_t | maximum_log_messages_ = MAX_LOG_MESSAGES |
std::string | line_buffer_ |
size_t | maximum_command_line_length_ = MAX_COMMAND_LINE_LENGTH |
unsigned char | previous_ = 0 |
Mode | mode_ = Mode::NORMAL |
std::unique_ptr< ModeData > | mode_data_ = nullptr |
bool | stopped_ = false |
bool | prompt_displayed_ = false |
uint64_t | idle_time_ = 0 |
uint64_t | idle_timeout_ = 0 |
Base class for a command shell.
Must be constructed within a std::shared_ptr.
using uuid::console::Shell::blocking_function = std::function<bool(Shell &shell, bool stop)> |
Function to handle a blocking operation.
[in] | shell | Shell instance where execution is blocked. |
[in] | stop | Request to return so that the shell can stop (true) or continue (false). |
using uuid::console::Shell::delay_function = std::function<void(Shell &shell)> |
using uuid::console::Shell::password_function = std::function<void(Shell &shell, bool completed, const std::string &password)> |
Function to handle the response to a password entry prompt.
[in] | shell | Shell instance where the password entry prompt was requested. |
[in] | completed | Password entry at the prompt was either completed (true) or aborted (false). |
[in] | password | Password entered at the prompt (may be empty). |
|
strongprivate |
uuid::console::Shell::Shell | ( | Stream & | stream, |
std::shared_ptr< Commands > | commands, | ||
unsigned int | context = 0 , |
||
unsigned int | flags = 0 |
||
) |
Create a new Shell operating on a Stream with the given commands, default context and initial flags.
The default context is put on the stack and cannot be removed.
The Stream must remain valid until the Shell has been destroyed. Monitor the Shell using a std::weak_ptr and destroy the Stream only after it has expired.
[in] | stream | Stream used for the input/output of this shell. |
[in] | commands | Commands available for execution in this shell. |
[in] | context | Default context for the shell. |
[in] | flags | Initial flags for the shell. |
|
inline |
|
finaloverride |
Check for available input.
The shell must be currently executing a blocking function otherwise it will always return 0.
Definition at line 27 of file shell_stream.cpp.
Commands::AvailableCommands uuid::console::Shell::available_commands | ( | ) | const |
Get the available commands in the current context and with the current flags.
The iterable object and its iterators are invalidated if the commands, context or flags for the shell change.
void uuid::console::Shell::block_with | ( | blocking_function | function | ) |
Execute a blocking function on this shell until it returns true.
The function will be executed every time loop_one() is called, which allows commands that do not complete immediately to be run asynchronously.
The shell must not be currently executing a blocking function.
[in] | function | Function to be executed on every loop_one() until normal execution resumes. |
|
private |
|
inline |
|
protectedvirtual |
Get the text indicating the current context, to be included in the command prompt.
Defaults to "".
Definition at line 35 of file shell_prompt.cpp.
void uuid::console::Shell::delay_for | ( | unsigned long | ms, |
delay_function | function | ||
) |
Stop executing anything on this shell for a period of time.
There is an assumption that 2^64 milliseconds uptime will always be enough time for this delay process.
The shell must not be currently executing a blocking function.
[in] | ms | Time in milliseconds to delay execution for. |
[in] | function | Function to be executed at a future time, prior to resuming normal execution. |
void uuid::console::Shell::delay_until | ( | uint64_t | ms, |
delay_function | function | ||
) |
Stop executing anything on this shell until a future time is reached.
There is an assumption that 2^64 milliseconds uptime will always be enough time for this delay process.
The reference time is uuid::get_uptime_ms().
The shell must not be currently executing a blocking function.
[in] | ms | Uptime in the future (in milliseconds) when the function should be executed. |
[in] | function | Function to be executed at a future time, prior to resuming normal execution. |
|
private |
|
protectedvirtual |
Output the startup banner.
There is no default banner.
Definition at line 27 of file shell_prompt.cpp.
|
private |
Output a prompt on the shell.
Based on the current mode this will output the appropriate text, e.g. the command prompt with the current command line (for Mode::NORMAL mode).
Definition at line 54 of file shell_prompt.cpp.
|
protectedvirtual |
The end of transmission character has been received.
A command can be invoked using invoke_command(). All other actions may result in an inconsistent display of the command prompt. If the shell is stopped without invoking a command then a blank line should usually be printed first.
If an idle timeout is set then the default action (since 0.7.2) is to stop the shell, otherwise there is no default action.
Definition at line 47 of file shell_prompt.cpp.
|
inline |
void uuid::console::Shell::enter_password | ( | const __FlashStringHelper * | prompt, |
password_function | function | ||
) |
Prompt for a password to be entered on this shell.
Password entry is not visible and can be interrupted by the user.
The shell must not be currently executing a blocking function.
[in] | prompt | Message to display prompting for password input. |
[in] | function | Function to be executed after the password has been entered prior to resuming normal execution. |
|
protectedvirtual |
Output ANSI escape sequence to erase characters.
[in] | count | The number of characters to erase. |
Definition at line 135 of file shell_print.cpp.
|
protectedvirtual |
Output ANSI escape sequence to erase the current line.
Definition at line 131 of file shell_print.cpp.
|
virtual |
|
finaloverride |
Does nothing.
This is a pure virtual function in Arduino's Stream class, which makes no sense because that class is for input and this is an output function. Later versions move it to Print as an empty virtual function so this is here for backward compatibility.
Definition at line 125 of file shell_stream.cpp.
|
inline |
Check if the current flags include all of the specified flags.
Flags are not affected by execution context. The current flags affect which commands are available (for access control).
[in] | flags | Flag bits to check for. |
|
inline |
Check if the current flags include any of the specified flags.
Flags are not affected by execution context. The current flags affect which commands are available (for access control).
[in] | flags | Flag bits to check for. |
|
inline |
Check if the current flags include all of the specified flags and none of the specified not_flags.
Flags are not affected by execution context. The current flags affect which commands are available (for access control).
[in] | flags | Flag bits to check for presence of. |
[in] | not_flags | Flag bits to check for absence of. |
|
protectedvirtual |
Get the hostname to be included in the command prompt.
Defaults to "".
Definition at line 31 of file shell_prompt.cpp.
unsigned long uuid::console::Shell::idle_timeout | ( | ) | const |
void uuid::console::Shell::idle_timeout | ( | unsigned long | timeout | ) |
|
protected |
Invoke a command on the shell.
This will output a prompt with the provided command line and then try to execute it.
Intended for use from end_of_transmission() to execute an "exit" or "logout" command.
[in] | line | The command line to be executed. |
uuid::log::Level uuid::console::Shell::log_level | ( | ) | const |
Get the current log level.
This only affects newly received log messages, not messages that have already been queued.
Definition at line 67 of file shell_log.cpp.
void uuid::console::Shell::log_level | ( | uuid::log::Level | level | ) |
Set the current log level.
This only affects newly received log messages, not messages that have already been queued.
[in] | level | Minimum log level that the shell will receive messages for. |
Definition at line 71 of file shell_log.cpp.
|
static |
Get the built-in uuid::log::Logger instance for shells.
Definition at line 44 of file shell_log.cpp.
|
static |
Loop through all registered shell objects.
Call loop_one() on every Shell (if it has not been stopped). Any Shell that is stopped is then unregistered.
Definition at line 34 of file shell_loop_all.cpp.
|
private |
|
private |
|
private |
Perform one execution step in Mode::NORMAL mode.
Read characters and execute commands or invoke tab completion.
void uuid::console::Shell::loop_one | ( | ) |
|
private |
Perform one execution step in Mode::PASSWORD mode.
Read characters until interrupted or password entry is complete.
size_t uuid::console::Shell::maximum_command_line_length | ( | ) | const |
void uuid::console::Shell::maximum_command_line_length | ( | size_t | length | ) |
Set the maximum length of a command line.
Defaults to Shell::MAX_COMMAND_LINE_LENGTH.
[in] | length | The maximum length of a command line in bytes. |
size_t uuid::console::Shell::maximum_log_messages | ( | ) | const |
Get the maximum number of queued log messages.
Definition at line 75 of file shell_log.cpp.
void uuid::console::Shell::maximum_log_messages | ( | size_t | count | ) |
Set the maximum number of queued log messages.
Defaults to Shell::MAX_LOG_MESSAGES.
[in] | count | The maximum number of queued log messages. |
Definition at line 83 of file shell_log.cpp.
|
virtual |
Add a new log message.
This will be put in a queue for output at the next loop_one() process. The queue has a maximum size of maximum_log_messages() and will discard the oldest message first.
[in] | message | New log message, shared by all handlers. |
Implements uuid::log::Handler.
Definition at line 55 of file shell_log.cpp.
|
private |
|
finaloverride |
Read one byte from the available input without advancing to the next one.
The shell must be currently executing a blocking function otherwise it will always return -1.
Definition at line 90 of file shell_stream.cpp.
size_t uuid::console::Shell::print | ( | const std::string & | data | ) |
Output a string.
[in] | data | String to be output. |
Definition at line 30 of file shell_print.cpp.
void uuid::console::Shell::print_all_available_commands | ( | ) |
Output a list of all available commands with their arguments.
Definition at line 122 of file shell_print.cpp.
size_t uuid::console::Shell::printf | ( | const __FlashStringHelper * | format, |
... | |||
) |
Output a message.
[in] | format | Format string (flash string). |
[in] | ... | Format string arguments. |
Definition at line 54 of file shell_print.cpp.
size_t uuid::console::Shell::printf | ( | const char * | format, |
... | |||
) |
Output a message.
[in] | format | Format string. |
[in] | ... | Format string arguments. |
Definition at line 44 of file shell_print.cpp.
size_t uuid::console::Shell::printfln | ( | const __FlashStringHelper * | format, |
... | |||
) |
Output a message followed by CRLF end of line characters.
[in] | format | Format string (flash string). |
[in] | ... | Format string arguments. |
Definition at line 75 of file shell_print.cpp.
size_t uuid::console::Shell::printfln | ( | const char * | format, |
... | |||
) |
Output a message followed by CRLF end of line characters.
[in] | format | Format string. |
[in] | ... | Format string arguments. |
Definition at line 64 of file shell_print.cpp.
size_t uuid::console::Shell::println | ( | const std::string & | data | ) |
Output a string followed by CRLF end of line characters.
[in] | data | String to be output. |
Definition at line 38 of file shell_print.cpp.
|
private |
|
private |
|
private |
|
protectedvirtual |
Get the prefix to be used at the beginning of the command prompt.
Defaults to "".
Definition at line 39 of file shell_prompt.cpp.
|
protectedvirtual |
Get the prefix to be used at the end of the command prompt.
Defaults to "$".
Definition at line 43 of file shell_prompt.cpp.
|
finaloverride |
Read one byte from the available input.
The shell must be currently executing a blocking function otherwise it will always return -1.
Definition at line 62 of file shell_stream.cpp.
|
staticprivate |
Get registered running shells to be executed.
Definition at line 28 of file shell_loop_all.cpp.
|
inline |
bool uuid::console::Shell::running | ( | ) | const |
void uuid::console::Shell::start | ( | ) |
Perform startup process for this shell.
Register as a uuid::log::Handler at the uuid::log::Level::NOTICE log level, output the banner and register this Shell with the loop_all() set.
The started() function will be called after startup is complete.
Do not call this function more than once.
|
protectedvirtual |
void uuid::console::Shell::stop | ( | ) |
|
protectedvirtual |
|
private |
Output a message.
[in] | format | Format string (flash string). |
[in] | ap | Variable arguments pointer for format string. |
Definition at line 104 of file shell_print.cpp.
|
private |
Output a message.
[in] | format | Format string. |
[in] | ap | Variable arguments pointer for format string. |
Definition at line 86 of file shell_print.cpp.
|
finaloverride |
Write an array of bytes to the output stream.
[in] | buffer | Buffer to be output. |
[in] | size | Length of the buffer. |
Definition at line 120 of file shell_stream.cpp.
|
finaloverride |
Write one byte to the output stream.
[in] | data | Data to be output. |
Definition at line 116 of file shell_stream.cpp.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
Command line buffer. Limited to maximum_command_line_length() bytes.
|
private |
|
private |
|
staticconstexpr |
|
staticconstexpr |
|
private |
|
private |
|
private |
|
private |
|
mutableprivate |
|
private |
|
private |
Indicates that a command prompt has been displayed, so that the output of invoke_command() is correct.
|
private |
|
private |