3 * Copyright 2003, 2006 Develer S.r.l. (http://www.develer.com/)
8 * \author Bernardo Innocenti <bernie@develer.com>
9 * \author Stefano Fedrigo <aleph@develer.com>
10 * \author Giovanni Bajo <rasky@develer.com>
12 * \brief serial protocol parser and commands.
17 *#* Revision 1.2 2006/06/12 21:37:02 marco
18 *#* implemented some commands (ver and sleep)
20 *#* Revision 1.1 2006/06/01 12:27:39 marco
21 *#* Added utilities for protocols
30 /** Max number of arguments and results for each command */
31 #define PARSER_MAX_ARGS 8
34 * Error generated by the commands through the return code.
38 RC_ERROR = -1, //!< Reply with error.
39 RC_OK = 0, //!< No reply (ignore reply arguments).
40 RC_REPLY = 1, //!< Reply command arguments.
41 RC_SKIP = 2 //!< Skip following commands
44 /** union that contains parameters passed to and from commands */
45 typedef union { long l; const char *s; } parms;
46 /** pointer to commands */
47 typedef ResultCode (*CmdFuncPtr)(parms args_results[]);
50 * Define a command that can be tokenized by the parser.
52 * The format strings are sequences of characters, one for each
53 * parameter/result. Valid characters are:
55 * d - a long integer, in decimal format
56 * s - a var string (in RAM)
58 * \note To create and fill an instance for this function, it is strongly
59 * advised to use \c DECLARE_CMD_HUNK (cmd_hunk.h).
63 const char *name; ///< Name of command
64 const char *arg_fmt; ///< Format string for the input
65 const char *result_fmt; ///< Format string for the output
66 CmdFuncPtr func; ///< Pointer to the handler function
67 uint16_t flags; ///< Currently unused.
71 * Initialize the parser module
73 * \note This function must be called before any other function in this module
75 void parser_init(void);
79 * Register a new command into the parser
81 * \param cmd Command template describing the command
84 void parser_register_cmd(const struct CmdTemplate* cmd);
88 * Hook for readline to provide completion support for the commands
89 * registered in the parser.
91 * \note This is meant to be used with mware/readline.c. See the
92 * documentation there for a description of this hook.
94 const char* parser_rl_match(void* dummy, const char* word, int word_len);
98 * \brief Command input handler.
100 * Process the input, calling the requested command
101 * (if found) and calling printResult() to give out
102 * the result (on device specified with parameter fd).
104 * \param line Text line to be processed (ASCIIZ)
106 * \return true if everything is OK, false in case of errors
108 bool parser_process_line(const char* line);
112 * Execute a command with its arguments, and fetch its results.
114 * \param templ Template of the command to be executed
115 * \param args Arguments for the command, and will contain the results
117 * \return False if the command returned an error, true otherwise
119 INLINE bool parser_execute_cmd(const struct CmdTemplate* templ, parms args[PARSER_MAX_ARGS])
121 return (templ->func(args) == 0);
126 * Find the template for the command contained in the text line.
127 * The template can be used to tokenize the command and interpret
130 * This function can be used to find out which command is contained
131 * in a given text line without parsing all the parameters and
134 * \param line Text line to be processed (ASCIIZ)
136 * \return The command template associated with the command contained
137 * in the line, or NULL if the command is invalid.
139 const struct CmdTemplate* parser_get_cmd_template(const char* line);
143 * Extract the arguments for the command contained in the text line.
145 * \param line Text line to be processed (ASCIIZ)
146 * \param templ Command template for this line
147 * \param args Will contain the extracted parameters
149 * \return True if everything OK, false in case of parsing error.
151 bool parser_get_cmd_arguments(const char* line, const struct CmdTemplate* templ, parms args[PARSER_MAX_ARGS]);
155 * Extract the ID from the command text line.
157 * \param line Text line to be processed (ASCIIZ)
158 * \param ID Will contain the ID extracted.
160 * \return True if everything ok, false if there is no ID
163 bool parser_get_cmd_id(const char* line, unsigned long* ID);
166 #endif /* PARSER_H */