Command Line Interface#
The Command Line Interface (CLI) software component simplifies creating command line user interfaces for programs. It handles tasks such as receiving keyboard input, parsing the input strings for commands and arguments, and finally calling the proper C function with decoded and validated arguments. Functions can use standard output functions such as printf to write information to the console. Many modules of the SDK have built-in commands that are provided via CLI, users can even add the custom commands if required.
The Command Line Interface (CLI) depends on I/O Stream for reading and writing bytes. You can set the I/O stream instance in the CLI instance configuration.
Modules#
Enumerations#
Enum of all the argument types.
Typedefs#
Argument type typedef.
Command handler data type.
Redirect command function.
sl cli input type t
sl cli handle t
Functions#
Initialize module.
Logout connected user from an instance.
Set a new password.
Destroy password key and force to configure a new password.
Reset security warning flag.
Add a new command group.
Remove a command group.
Find a command given the command groups and input arguments.
Find and execute a command given the string input and the command table.
Split a C-string on space, ignoring sections in {} or "".
Handle new input char from the terminal, both in terms of writing back to terminal and writing to the input buffer.
Empty the input buffer.
Get the hex argument length and value.
Clear (reset) a CLI.
Redirect user input.
Handle input.
Initialize a CLI instance.
Check if the CLI instance can allow sleep.
The bare metal tick function.
Macros#
sl cli nvm3 key count
sl cli nvm3 key begin
sl cli nvm3 key end
Ordinary text or non-special characters.
RETURN character.
escape character (0xb)
Must follow escape character. (0x5b)
A macro, which is used to create commands.
A macro, which is used to create command groups.
UINT8 argument type.
UINT16 argument type.
UINT32 argument type.
INT8 argument type.
INT16 argument type.
INT32 argument type.
STRING argument type.
HEX argument type.
Optional (zero or more) UINT8 argument type.
Optional (zero or more) UINT16 argument type.
Optional (zero or more) UINT32 argument type.
Optional (zero or more) INT8 argument type.
Optional (zero or more) INT16 argument type.
Optional (zero or more) INT32 argument type.
Optional (zero or more) STRING argument type.
Optional (zero or more) HEX argument type.
ADDITIONAL argument type.
WILDCARD argument type.
GROUP argument type.
END argument type.
MACROS ************************************.
Define the DOWN-direction.
Enable the CLI tick.
Obtain the number of command arguments.
Obtain the type of argument.
Obtain int8 arguments.
Obtain int16 arguments.
Obtain int32 arguments.
Obtain uint8 arguments.
Obtain uint16 arguments.
Obtain uint32 arguments.
Obtain string arguments.
Obtain the number of command strings.
Obtain the command string(s).
Enumeration Documentation#
sl_cli_arg_t#
sl_cli_arg_t
Enum of all the argument types.
Enumerator | |
---|---|
sl_cli_arg_uint8 | UINT8 argument type. |
sl_cli_arg_uint16 | UINT16 argument type. |
sl_cli_arg_uint32 | UINT32 argument type. |
sl_cli_arg_int8 | INT8 argument type. |
sl_cli_arg_int16 | INT16 argument type. |
sl_cli_arg_int32 | INT32 argument type. |
sl_cli_arg_string | STRING argument type. |
sl_cli_arg_hex | HEX argument type. |
sl_cli_arg_uint8opt | Optional (zero or more) UINT8 argument type. |
sl_cli_arg_uint16opt | Optional (zero or more) UINT16 argument type. |
sl_cli_arg_uint32opt | Optional (zero or more) UINT32 argument type. |
sl_cli_arg_int8opt | Optional (zero or more) INT8 argument type. |
sl_cli_arg_int16opt | Optional (zero or more) INT16 argument type. |
sl_cli_arg_int32opt | Optional (zero or more) INT32 argument type. |
sl_cli_arg_stringopt | Optional (zero or more) STRING argument type. |
sl_cli_arg_hexopt | Optional (zero or more) HEX argument type. |
sl_cli_arg_additionnal | ADDITIONAL argument type. |
sl_cli_arg_wildcard | WILDCARD argument type. |
sl_cli_arg_group | GROUP argument type. |
sl_cli_arg_end | END argument type. |
Typedef Documentation#
sl_cli_command_func_t#
typedef void(* sl_cli_command_func_t) (sl_cli_command_arg_t *arguments) )(sl_cli_command_arg_t *arguments)
Command handler data type.
sl_cli_command_function_t#
typedef void(* sl_cli_command_function_t) (char *arg_str, void *user) )(char *arg_str, void *user)
Redirect command function.
Variable Documentation#
sl_cli_default_command_group#
sl_cli_command_group_t* sl_cli_default_command_group
The default CLI command group.
Function Documentation#
sl_cli_simple_password_init#
sl_status_t sl_cli_simple_password_init (void )
Initialize module.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Returns
Initialization status .
sl_cli_simple_password_logout#
sl_status_t sl_cli_simple_password_logout (sl_cli_handle_t handle)
Logout connected user from an instance.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [in] | handle | A handle to the CLI. |
Returns
Initialization status.
sl_cli_set_simple_password#
sl_status_t sl_cli_set_simple_password (char * new_password)
Set a new password.
Type | Direction | Argument Name | Description |
---|---|---|---|
char * | [in] | new_password | Password string |
Returns
Operation status.
sl_cli_simple_password_destroy_key#
sl_status_t sl_cli_simple_password_destroy_key (void )
Destroy password key and force to configure a new password.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Returns
Operation status.
sl_cli_reset_security_warning_flag#
sl_status_t sl_cli_reset_security_warning_flag (void )
Reset security warning flag.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Returns
Operation status.
sl_cli_command_add_command_group#
bool sl_cli_command_add_command_group (sl_cli_handle_t handle, sl_cli_command_group_t * command_group)
Add a new command group.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [inout] | handle | A handle to a CLI instance. |
sl_cli_command_group_t * | [in] | command_group | A pointer to a command group structure. Note that the structure must initially have NULL in all elements except the command_table. |
Returns
Returns true if the command_group could be added, false otherwise.
sl_cli_command_remove_command_group#
bool sl_cli_command_remove_command_group (sl_cli_handle_t handle, sl_cli_command_group_t * command_group)
Remove a command group.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [inout] | handle | A handle to a CLI instance. |
sl_cli_command_group_t * | [in] | command_group | A pointer to a command group structure. |
Returns
Returns true if the command_group could be removed, false otherwise.
sl_cli_command_find#
const sl_cli_command_entry_t * sl_cli_command_find (sl_cli_handle_t handle, int * token_c, char * token_v, int * arg_ofs, bool * single_flag, bool * help_flag)
Find a command given the command groups and input arguments.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [inout] | handle | A handle to a CLI instance. |
int * | [in] | token_c | A number of arguments given. |
char * | [in] | token_v | An array containing the token_v obtained from tokenization. |
int * | [out] | arg_ofs | An integer that will get the index to the first command argument. Whether the command is in a group or not will affect the arg_ofs value. |
bool * | [out] | single_flag | Boolean that is set to true if a specific command is detected. For help, the function may return a pointer to a command entry table or NULL, and in these cases the single_flag is set to false. |
bool * | [out] | help_flag | Boolean that is set to true if "help" is detected, else set to false. |
Returns
A pointer to a command entry for the given command. If the command is not found, the returned value can be NULL. If a help command is issued, the returned value may point to a command entry table.
sl_cli_command_execute#
sl_status_t sl_cli_command_execute (sl_cli_handle_t handle, char * input)
Find and execute a command given the string input and the command table.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [inout] | handle | A handle to a CLI instance. |
char * | [inout] | input | C-string containing the user input. Must be '\0'-terminated. |
Note
The input string will be modified in-place.
Returns
Status of the execution.
sl_cli_tokenize#
sl_status_t sl_cli_tokenize (char input, int * token_c, char * token_v)
Split a C-string on space, ignoring sections in {} or "".
Type | Direction | Argument Name | Description |
---|---|---|---|
char | [inout] | input | The C-string to be split. Must be '\0'-terminated. Will be modified, even if an error occurs. |
int * | [out] | token_c | The number of tokens put in the token_v array. |
char * | [out] | token_v | An array containing the tokens obtained from splitting. |
Note
This function modifies the content of the input string, and points the pointers in tokens to addresses in the input string. In quote strings ("") or curly braces ({}), the string will not be split, The quotes will be removed, but curly braces are kept for validation. To avoid '"' being interpreted as start/end of quote string, it may be escaped using '\"', both within and outside of "...". '{' may be escaped using * '\{'. There is no need for escaping '}', since only hex digits are allowed within {...}. Backslash also needs be be escaped, as '\'.
Returns
Status of the operation.
sl_cli_input_char#
bool sl_cli_input_char (sl_cli_handle_t handle, char c)
Handle new input char from the terminal, both in terms of writing back to terminal and writing to the input buffer.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [inout] | handle | A handle to a CLI instance. |
char | [in] | c | Input char to be processed by the function. |
This function should be called every time new input is detected. The behavior of the function is highly configurable through settings in cli_config.h.
Returns
Returns true if newline or return is detected, false otherwise.
sl_cli_input_clear#
void sl_cli_input_clear (sl_cli_handle_t handle)
Empty the input buffer.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [inout] | handle | A handle to a CLI instance. |
This function should be called after newline has been detected and the information in the buffer has been processed.
sl_cli_get_argument_hex#
uint8_t * sl_cli_get_argument_hex (sl_cli_command_arg_t * a, size_t n, size_t * l)
Get the hex argument length and value.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_command_arg_t * | [in] | a | A pointer to the command arguments. |
size_t | [in] | n | The argument number. The first argument is number 0, the next 1, etc. |
size_t * | [in] | l | A pointer to the variable that receives the argument value length. Note: The length is stored in a 2 byte variable. Valid lengths are in the range 0 .. 65535. |
Returns
A pointer to the value buffer.
sl_cli_clear#
void sl_cli_clear (sl_cli_handle_t handle)
Clear (reset) a CLI.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [in] | handle | A handle to the CLI that will be cleared. |
sl_cli_redirect_command#
void sl_cli_redirect_command (sl_cli_handle_t handle, sl_cli_command_function_t command_function, const char * prompt, void * aux)
Redirect user input.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [in] | handle | A handle to the CLI. |
sl_cli_command_function_t | [in] | command_function | A pointer to the function that will receive user input when the re-direct is active. NULL to stop the redirect. |
const char * | [in] | prompt | A pointer to a string that will be used as command prompt in the redirect function. NULL to stop the redirect. |
void * | [in] | aux | A pointer that will be added to the redirect function arguments. NULL to stop the redirect. |
Normally, an input string is passed to the CLI command handler for execution, but with the redirect function the input string will be passed to the alternative function. If the alternative input function should not to be used, this function can be called with NULL in the command_function, prompt and aux.
sl_cli_handle_input#
sl_status_t sl_cli_handle_input (sl_cli_handle_t handle, char * string)
Handle input.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [in] | handle | A handle to the CLI. |
char * | [in] | string | A pointer to the string containing the command line that shall be executed. |
Execute a complete command line with command and arguments.
Returns
Execution status. Note: If the command is redirected, the function will always return SL_STATUS_OK.
sl_cli_instance_init#
sl_status_t sl_cli_instance_init (sl_cli_handle_t handle, sl_cli_instance_parameters_t * parameters)
Initialize a CLI instance.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [in] | handle | A handle to the CLI. |
sl_cli_instance_parameters_t * | [in] | parameters | A pointer to the structure containing instance parameters. |
Returns
Initialization status.
sl_cli_is_ok_to_sleep#
bool sl_cli_is_ok_to_sleep (sl_cli_handle_t handle)
Check if the CLI instance can allow sleep.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [in] | handle | A handle to the CLI instance. |
This function is available in a bare metal configuration only.
Returns
A boolean that is true if the CLI allows sleep, else false.
sl_cli_tick_instance#
void sl_cli_tick_instance (sl_cli_handle_t handle)
The bare metal tick function.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cli_handle_t | [in] | handle | A handle to the CLI instance. |