Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any good reference implementations available for command line implementations for embedded systems?

I am aware that this is nothing new and has been done several times. But I am looking for some reference implementation (or even just reference design) as a "best practices guide". We have a real-time embedded environment and the idea is to be able to use a "debug shell" in order to invoke some commands. Example: "SomeDevice print reg xyz" will request the SomeDevice sub-system to print the value of the register named xyz.

like image 830
venk Avatar asked Dec 16 '09 13:12

venk


2 Answers

I have a small set of routines that is essentially made up of 3 functions and a lookup table:

  • a function that gathers a command line - it's simple; there's no command line history or anything, just the ability to backspace or press escape to discard the whole thing. But if I thought fancier editing capabilities were needed, it wouldn't be too hard to add them here.
  • a function that parses a line of text argc/argv style (see Parse string into argv/argc for some ideas on this)
  • a function that takes the first arg on the parsed command line and looks it up in a table of commands & function pointers to determine which function to call for the command, so the command handlers just need to match the prototype:

    int command_handler( int argc, char* argv[]);

Then that function is called with the appropriate argc/argv parameters.

Actually, the lookup table also has pointers to basic help text for each command, and if the command is followed by '-?' or '/?' that bit of help text is displayed. Also, if 'help' is used for a command, the command table is dumped (possible only a subset if a parameter is passed to the 'help' command).

Sorry, I can't post the actual source - but it's pretty simple and straight forward to implement, and functional enough for pretty much all the command line handling needs I've had for embedded systems development.

like image 150
Michael Burr Avatar answered Sep 28 '22 00:09

Michael Burr


You might bristle at this response, but many years ago we did something like this for a large-scale embedded telecom system using lex/yacc (nowadays I guess it would be flex/bison, this was literally 20 years ago).

Define your grammar, define ranges for parameters, etc... and then let lex/yacc generate the code.

There is a bit of a learning curve, as opposed to rolling a 1-off custom implementation, but then you can extend the grammar, add new commands & parameters, change ranges, etc... extremely quickly.

like image 38
Dan Avatar answered Sep 28 '22 01:09

Dan