Home

Awesome

mbed-client-cli

This is the Command Line Library for a CLI application. It uses only ansi C features so it is portable and works in Mbed OS, linux and windows.

Features

Library provides features such:

API

Command Line Library basic API's is described in the snipplet below:

// if thread safety for CLI terminal output is needed
// configure output mutex wait cb before initialization so it's available immediately
cmd_set_mutex_wait_func( (func)(void) );
// configure output mutex release cb before initialization so it's available immediately
cmd_set_mutex_wait_func( (func)(void) );
// initialize cmdline with print function
cmd_init( (func)(const char* fmt, va_list ap) );
// configure ready cb
cmd_set_ready_cb( (func)(int retcode)  );
// register command for library
cmd_add( <command>, (int func)(int argc, char *argv[]), <help>, <man>);
//execute some existing commands
cmd_exe( <command> );

Full API is described here

Configuration

Following defines can be used to configure defaults:

definetypedefault valuedescription
MBED_CONF_CMDLINE_USE_MINIMUM_SETboolfalseUse preconfigured minimum build. See more details from below
MBED_CONF_CMDLINE_ENABLE_ALIASESbooltrueEnable aliases
MBED_CONF_CMDLINE_USE_DUMMY_SET_ECHO_COMMANDSbooltrueEnable dummy set and echo commands
MBED_CONF_CMDLINE_INIT_AUTOMATION_MODEboolfalseEnable automation mode during initalize phase
MBED_CONF_CMDLINE_ENABLE_ESCAPE_HANDLINGbooltrueEnable escape handling
MBED_CONF_CMDLINE_ENABLE_OPERATORSbooltrueEnable operators. E.g. echo abc && echo def
MBED_CONF_CMDLINE_ENABLE_INTERNAL_COMMANDSbooltrueEnable internal commands. E.g. echo
MBED_CONF_CMDLINE_ENABLE_INTERNAL_VARIABLESbooltrueEnable internal variables
MBED_CONF_CMDLINE_BOOT_MESSAGEC stringARM Ltd\r\ndefault boot message
MBED_CONF_CMDLINE_MAX_LINE_LENGTHint2000maximum command line length
MBED_CONF_CMDLINE_ARGS_MAX_COUNTint30maximum count of command arguments
MBED_CONF_CMDLINE_ENABLE_HISTORYbooltrueEnable command history. browsable using key up/down
MBED_CONF_CMDLINE_HISTORY_MAX_COUNTint32maximum history size
MBED_CONF_CMDLINE_INCLUDE_MANbooltrueInclude man pages
MBED_CONF_CMDLINE_ENABLE_INTERNAL_TRACESboolfalseEnable cli internal traces
MBED_CONF_CMDLINE_ENABLE_DEEP_INTERNAL_TRACESboolfalseEnable cli deep internal traces

Minimize footprint

To reduce required flash and RAM usage there is pre-defined profile which can be enabled by using precompiler variable:

MBED_CONF_CMDLINE_USE_MINIMUM_SET=1

This switch off most of features and reduce buffer sizes. Below is whole configueration:

definevalue
MBED_CONF_CMDLINE_ENABLE_ALIASESfalse
MBED_CONF_CMDLINE_USE_DUMMY_SET_ECHO_COMMANDSfalse
MBED_CONF_CMDLINE_INIT_AUTOMATION_MODEfalse
MBED_CONF_CMDLINE_ENABLE_ESCAPE_HANDLINGfalse
MBED_CONF_CMDLINE_ENABLE_OPERATORSfalse
MBED_CONF_CMDLINE_ENABLE_INTERNAL_COMMANDSfalse
MBED_CONF_CMDLINE_ENABLE_INTERNAL_VARIABLESfalse
MBED_CONF_CMDLINE_MAX_LINE_LENGTH100
MBED_CONF_CMDLINE_ARGS_MAX_COUNT10
MBED_CONF_CMDLINE_ENABLE_HISTORYfalse
MBED_CONF_CMDLINE_INCLUDE_MANfalse

Pre defines return codes

each command should return some of pre-defines return codes. These codes are reserved and used in test tools.

definedescription
CMDLINE_RETCODE_COMMAND_BUSYCommand Busy
CMDLINE_RETCODE_EXCUTING_CONTINUEExecution continue in background
CMDLINE_RETCODE_SUCCESSExecution Success
CMDLINE_RETCODE_FAILExecution Fail
CMDLINE_RETCODE_INVALID_PARAMETERSCommand parameters was incorrect
CMDLINE_RETCODE_COMMAND_NOT_IMPLEMENTEDCommand not implemented
CMDLINE_RETCODE_COMMAND_CB_MISSINGCommand callback function missing
CMDLINE_RETCODE_COMMAND_NOT_FOUNDCommand not found

Tracing

Command Line Library has trace messages, which are disabled by default. MBED_CONF_CMDLINE_ENABLE_INTERNAL_TRACES flag if defined, enables all the trace prints for debugging.

Usage example

See full examples here.

Adding new commands to the Command Line Library and executing the commands:

//example print function
void myprint(const char* fmt, va_list ap){ vprintf(fmt, ap); }

// ready cb, calls next command to be executed
void cmd_ready_cb(int retcode) { cmd_next( retcode ); }

// dummy command with some option
int cmd_dummy(int argc, char *argv[]){
  if( cmd_has_option(argc, argv, "o") ) {
    cmd_printf("This is o option");
  } else {
        return CMDLINE_RETCODE_INVALID_PARAMETERS;
  }
  return CMDLINE_RETCODE_SUCCESS;
}

// timer cb (pseudo-timer-code)
void timer_ready_cb(void) {
   cmd_ready(CMDLINE_RETCODE_SUCCESS);
}

// long command, that needs for example some events to complete the command execution
int cmd_long(int argc, char *argv[] ) {
   timer_start( 5000, timer_ready_cb ); //pseudo timer code
   return CMDLINE_RETCODE_EXCUTING_CONTINUE;
}
void main(void) {
   cmd_init( &myprint );              // initialize cmdline with print function
   cmd_set_ready_cb( cmd_ready_cb );  // configure ready cb
   cmd_add("dummy", cmd_dummy, 0, 0); // add one dummy command
   cmd_add("long", cmd_long, 0, 0);   // add one dummy command
   //execute dummy and long commands
   cmd_exe( "dummy;long" );
}

Thread safety

The CLI library is not thread safe, but the CLI terminal output can be locked against other output streams, for example if both traces and CLI output are using serial out.

Thread safety example for Mbed OS is available here.

Unit tests

Unit tests are available in the ./test folder. To run the unit tests in linux environment:

cd test
./run_unit_tests.sh

Unit tests xml output will be generated to folder: test/build. Code coverage report can be found from: ./test/build/html/coverage_index.html.