Reformat. Fix headguard name. Clean up.
[bertos.git] / bertos / mware / parser.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2006 Develer S.r.l. (http://www.develer.com/)
30  * All Rights Reserved.
31  * -->
32  *
33  * \version $Id$
34  *
35  * \author Bernardo Innocenti <bernie@develer.com>
36  * \author Stefano Fedrigo <aleph@develer.com>
37  * \author Giovanni Bajo <rasky@develer.com>
38  *
39  * \brief serial protocol parser and commands.
40  */
41
42
43 #ifndef MWARE_PARSER_H
44 #define MWARE_PARSER_H
45
46 /** Max number of arguments and results for each command */
47 #define PARSER_MAX_ARGS       8
48
49 /**
50  * Error generated by the commands through the return code.
51  */
52 typedef enum
53 {
54         RC_ERROR  = -1, ///< Reply with error.
55         RC_OK     = 0,  ///< No reply (ignore reply arguments).
56         RC_REPLY  = 1,  ///< Reply command arguments.
57         RC_SKIP   = 2   ///< Skip following commands
58 } ResultCode;
59
60 /** union that contains parameters passed to and from commands */
61 typedef union { long l; const char *s; } parms;
62 /** pointer to commands */
63 typedef ResultCode (*CmdFuncPtr)(parms args_results[]);
64
65 /**
66  * Define a command that can be tokenized by the parser.
67  *
68  * The format strings are sequences of characters, one for each
69  * parameter/result. Valid characters are:
70  *
71  *  d - a long integer, in decimal format
72  *  s - a var string (in RAM)
73  *
74  * \note To create and fill an instance for this function, it is strongly
75  * advised to use \c DECLARE_CMD_HUNK (cmd_hunk.h).
76  */
77 struct CmdTemplate
78 {
79         const char *name;          ///< Name of command
80         const char *arg_fmt;       ///< Format string for the input
81         const char *result_fmt;    ///< Format string for the output
82         CmdFuncPtr func;           ///< Pointer to the handler function
83         uint16_t   flags;          ///< Currently unused.
84 };
85
86 /**
87  * Initialize the parser module
88  *
89  * \note This function must be called before any other function in this module
90  */
91 void parser_init(void);
92
93
94 /**
95  * Register a new command into the parser
96  *
97  * \param cmd Command template describing the command
98  *
99  */
100 void parser_register_cmd(const struct CmdTemplate* cmd);
101
102
103 /**
104  * Hook for readline to provide completion support for the commands
105  * registered in the parser.
106  *
107  * \note This is meant to be used with mware/readline.c. See the
108  * documentation there for a description of this hook.
109  */
110 const char* parser_rl_match(void* dummy, const char* word, int word_len);
111
112
113 /**
114  * \brief Command input handler.
115  *
116  * Process the input, calling the requested command
117  * (if found) and calling printResult() to give out
118  * the result (on device specified with parameter fd).
119  *
120  * \param line Text line to be processed (ASCIIZ)
121  *
122  * \return true if everything is OK, false in case of errors
123  */
124 bool parser_process_line(const char* line);
125
126
127 /**
128  * Execute a command with its arguments, and fetch its results.
129  *
130  * \param templ Template of the command to be executed
131  * \param args Arguments for the command, and will contain the results
132  *
133  * \return False if the command returned an error, true otherwise
134  */
135 INLINE bool parser_execute_cmd(const struct CmdTemplate* templ, parms args[PARSER_MAX_ARGS])
136 {
137         return (templ->func(args) == 0);
138 }
139
140
141 /**
142  * Find the template for the command contained in the text line.
143  * The template can be used to tokenize the command and interpret
144  * it.
145  *
146  * This function can be used to find out which command is contained
147  * in a given text line without parsing all the parameters and
148  * executing it.
149  *
150  * \param line Text line to be processed (ASCIIZ)
151  *
152  * \return The command template associated with the command contained
153  * in the line, or NULL if the command is invalid.
154  */
155 const struct CmdTemplate* parser_get_cmd_template(const char* line);
156
157
158 /**
159  * Extract the arguments for the command contained in the text line.
160  *
161  * \param line Text line to be processed (ASCIIZ)
162  * \param templ Command template for this line
163  * \param args Will contain the extracted parameters
164  *
165  * \return True if everything OK, false in case of parsing error.
166  */
167 bool parser_get_cmd_arguments(const char* line, const struct CmdTemplate* templ, parms args[PARSER_MAX_ARGS]);
168
169
170 /**
171  * Extract the ID from the command text line.
172  *
173  * \param line Text line to be processed (ASCIIZ)
174  * \param ID Will contain the ID extracted.
175  *
176  * \return True if everything ok, false if there is no ID
177  *
178  */
179 bool parser_get_cmd_id(const char* line, unsigned long* ID);
180
181
182 #endif /* MWARE_PARSER_H */
183