Command-Line Interface Processing
The command-line interface module is responsible for building
the incoming command, parsing a completed command, and executing the
function associated with the command. The command-line interface module
contains two functions to handle these tasks: cliBuildCommand and cliProcessCommand.
The command_t struct has two
members: the command name (defined by the pointer name), and the function to execute when the
command is entered (defined by the pointer to a function function).
The command table, gCommandTable, is a command_t type array. The last command name
and function in the table are set to NULL in order to aid the command lookup
procedure. Additional commands can be added by following the format
shown, but new commands must be added before the terminating last
command.
You may notice that a macro called MAX_COMMAND_LEN is defined. Command names must
be less than or equal to the maximum command length. The following code
shows the command struct type and the command table:
#define MAX_COMMAND_LEN (10)
#define COMMAND_TABLE_SIZE (4)
typedef struct
{
char const *name;
void (*function)(void);
} command_t;
command_t const gCommandTable[COMMAND_TABLE_LEN] =
{
{"HELP", commandsHelp,},
{"LED", commandsLed, },
{"BUZZER", commandsBuzzer, },
{NULL, NULL }
};As characters are received from the serial port, the main
processing loop calls the cliBuildCommand function, as we saw earlier in this chapter. Once a completed command is received, indicated by a carriage ...