parser.c File Reference

Serial protocol parser and commands. More...

#include "parser.h"
#include "cfg/cfg_parser.h"
#include <drv/ser.h>
#include <struct/hashtable.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.


Functions

static const void * get_key_from_command (const void *cmd, uint8_t *length)
 Hashtable hook to extract the key from a command.
 DECLARE_HASHTABLE_STATIC (commands, MAX_COMMANDS_NUMBER, get_key_from_command)
 Hashtable that handles the commands that can be executed.
static bool get_word (const char **begin, const char **end)
 Tokenize one word at a time from a text.
static bool parseArgs (const char *fmt, const char *input, parms argv[])
 Command arguments parser.
const char * parser_rl_match (UNUSED_ARG(void *, dummy), const char *word, int word_len)
 Hook provided by the parser for matching of command names (TAB completion) for readline.
bool parser_get_cmd_id (const char *line, unsigned long *ID)
 Extract the ID from the command text line.
struct CmdTemplateparser_get_cmd_template (const char *input)
 Find the template for the command contained in the text line.
bool parser_get_cmd_arguments (const char *input, const struct CmdTemplate *cmdp, parms args[PARSER_MAX_ARGS])
 Extract the arguments for the command contained in the text line.
bool parser_process_line (const char *input)
 Command input handler.
void parser_register_cmd (const struct CmdTemplate *cmd)
 Register a new command into the parser.
void parser_init (void)
 Initialize the parser module.

Detailed Description

Serial protocol parser and commands.

This file contains the serial protocol parser and the definition of the protocol commands. Commands are defined in a "CmdTemplate" type array, containing:

  • the name of the command,
  • the arguments it expects to receive,
  • the output values,
  • the name of the function implementing the command.

The arguments and results are passed to command function using an union: the element of the union to use for each argument is determined by format strings present in the CmdTemplate table.

Version:
Id
parser.c 2506 2009-04-15 08:29:07Z duplo

Author:
Bernie Innocenti <bernie@codewiz.org>

Stefano Fedrigo <aleph@develer.com>

Giovanni Bajo <rasky@develer.com>

Definition in file parser.c.


Function Documentation

static bool get_word ( const char **  begin,
const char **  end 
) [static]

Tokenize one word at a time from a text.

This function is similar to strtok, but does not use any implicit context, nor it does modify the input buffer in any form. The word is returned as a STL-like [begin,end) range.

To extract the first word, make both begin and end point at the start of the text, and call the function. Then, subsequent calls will return the following words (assuming the begin/end variable are not modified between calls).

Parameters:
begin Will contain the index of the first character of the word
end Will contain the index of the character after the last character of the word
Returns:
True if a word was extracted, false if we got to the end of the string without extracting any word.

Definition at line 101 of file parser.c.

static bool parseArgs ( const char *  fmt,
const char *  input,
parms  argv[] 
) [static]

Command arguments parser.

Using the format pointed by the argument fmt parses the input string filling the array argv with input parameters of the correct type.

Parameters:
fmt Parameters format string.
input Input string.
argv Array filled with parameters.
Returns:
False in case of errors, otherwise true.

Definition at line 132 of file parser.c.

bool parser_get_cmd_arguments ( const char *  line,
const struct CmdTemplate templ,
parms  args[PARSER_MAX_ARGS] 
)

Extract the arguments for the command contained in the text line.

Parameters:
line Text line to be processed (ASCIIZ)
templ Command template for this line
args Will contain the extracted parameters
Returns:
True if everything OK, false in case of parsing error.

Definition at line 317 of file parser.c.

bool parser_get_cmd_id ( const char *  line,
unsigned long *  ID 
)

Extract the ID from the command text line.

Parameters:
line Text line to be processed (ASCIIZ)
ID Will contain the ID extracted.
Returns:
True if everything ok, false if there is no ID

Definition at line 270 of file parser.c.

struct CmdTemplate* parser_get_cmd_template ( const char *  line  )  [read]

Find the template for the command contained in the text line.

The template can be used to tokenize the command and interpret it.

This function can be used to find out which command is contained in a given text line without parsing all the parameters and executing it.

Parameters:
line Text line to be processed (ASCIIZ)
Returns:
The command template associated with the command contained in the line, or NULL if the command is invalid.

Definition at line 286 of file parser.c.

void parser_init ( void   ) 

Initialize the parser module.

Note:
This function must be called before any other function in this module

Definition at line 389 of file parser.c.

bool parser_process_line ( const char *  line  ) 

Command input handler.

Process the input, calling the requested command (if found) and calling printResult() to give out the result (on device specified with parameter fd).

Parameters:
line Text line to be processed (ASCIIZ)
Returns:
true if everything is OK, false in case of errors

Definition at line 337 of file parser.c.

void parser_register_cmd ( const struct CmdTemplate cmd  ) 

Register a new command into the parser.

Parameters:
cmd Command template describing the command

Definition at line 355 of file parser.c.