4 * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
7 * \brief Implementation of the command protocol between the board and the host
12 * \author Giovanni Bajo <rasky@develer.com>
13 * \author Marco Benelli <marco@develer.com>
14 * \author Bernardo Innocenti <bernie@develer.com>
19 *#* Revision 1.7 2007/01/09 09:00:16 bernie
22 *#* Revision 1.6 2006/09/20 13:54:04 marco
23 *#* Usage examples of MAKE_CMD.
25 *#* Revision 1.5 2006/06/14 01:03:01 marco
26 *#* Add response code. Add command ping.
28 *#* Revision 1.4 2006/06/14 00:26:48 marco
29 *#* Use new macros for defining commands.
31 *#* Revision 1.3 2006/06/13 19:07:31 marco
32 *#* Fixed a bug in protocol_reply. Simplified rpc.
34 *#* Revision 1.2 2006/06/12 21:37:02 marco
35 *#* implemented some commands (ver and sleep)
37 *#* Revision 1.1 2006/06/01 12:29:21 marco
38 *#* Add first simple protocol command (version request).
45 #include <drv/timer.h>
46 #include <mware/readline.h>
47 #include <mware/parser.h>
48 #include <cfg/compiler.h>
49 #include <cfg/debug.h>
55 //#include <cmd_hunk.h>
57 #include "cmd_ctor.h" // MAKE_CMD, REGISTER_CMD
59 // DEBUG: set to 1 to force interactive mode
60 #define FORCE_INTERACTIVE 1
63 * True if we are in interactive mode, false if we are in protocol mode.
64 * In interactive mode, commands are read through readline() (prompt,
65 * completion, history) without IDs, and replies/errors are sent to the serial
67 * In protocol mode, we implement the default protocol
69 static bool interactive;
71 /// Readline context, used for interactive mode.
72 static struct RLContext rl_ctx;
76 * Send a NAK asking the host to send the current message again.
78 * \param ser serial port handle to output to.
79 * \param err human-readable description of the error for debug purposes.
81 INLINE void NAK(Serial *ser, const char *err)
84 ser_printf(ser, "NAK \"%s\"\r\n", err);
86 ser_printf(ser, "NAK\r\n");
91 * Print args on s, with format specified in t->result_fmt.
92 * Return number of valid arguments or -1 in case of error.
94 static bool protocol_reply(Serial *s, const struct CmdTemplate *t,
97 unsigned short offset = strlen(t->arg_fmt) + 1;
98 unsigned short nres = strlen(t->result_fmt);
101 for (unsigned short i = 0; i < nres; ++i)
103 if (t->result_fmt[i] == 'd')
105 ser_printf(s, " %ld", args[offset+i].l);
107 else if (t->result_fmt[i] == 's')
109 ser_printf(s, " %s", args[offset+i].s);
116 ser_printf(s, "\r\n");
120 static void protocol_parse(Serial *ser, const char *buf)
122 const struct CmdTemplate *templ;
125 templ = parser_get_cmd_template(buf);
128 ser_print(ser, "-1 Invalid command.");
132 parms args[PARSER_MAX_ARGS];
134 /* Args Check. TODO: Handle different case. see doc/PROTOCOL . */
135 if (!parser_get_cmd_arguments(buf, templ, args))
137 ser_print(ser, "-2 Invalid arguments.");
142 if(!parser_execute_cmd(templ, args))
144 NAK(ser, "Error in executing command.");
146 if (!protocol_reply(ser, templ, args))
148 NAK(ser, "Invalid return format.");
153 void protocol_run(Serial *ser)
156 * \todo to be removed, we could probably access the serial FIFO
159 static char linebuf[80];
163 ser_gets(ser, linebuf, sizeof(linebuf));
165 // reset serial port error anyway
166 ser_setstatus(ser, 0);
168 // check message minimum length
171 if (linebuf[0] == 0x1B && linebuf[1] == 0x1B) // ESC
175 "Entering interactive mode\r\n");
179 protocol_parse(ser, linebuf);
188 * Read a line from serial. We use a temporary buffer
189 * because otherwise we would have to extract a message
190 * from the port immediately: there might not be any
191 * available, and one might get free while we read
192 * the line. We also add a fake ID at the start to
195 buf = rl_readline(&rl_ctx);
197 if (buf && buf[0] != '\0')
199 // exit special case to immediately change serial input
200 if (!strcmp(buf, "exit") || !strcmp(buf, "quit"))
202 rl_clear_history(&rl_ctx);
204 "Leaving interactive mode...\r\n");
205 interactive = FORCE_INTERACTIVE;
209 //TODO: remove sequence numbers
213 strncpy(linebuf + 2, buf, sizeof(linebuf) - 3);
214 linebuf[sizeof(linebuf) - 1] = '\0';
215 protocol_parse(ser, linebuf);
223 * TODO: Command declarations and definitions should be in another file(s).
224 * Maybe we should use CMD_HUNK_TEMPLATE.
228 /* Version. Example of declaring function and passing it to MAKE_CMD. */
229 static int ver_fn(const char **str)
234 MAKE_CMD(ver, "", "s", ver_fn(&args[1].s))
236 /* Sleep. Example of declaring function body directly in macro call. */
237 MAKE_CMD(sleep, "d", "",
239 timer_delay((mtime_t)args[1].l);
244 MAKE_CMD(ping, "", "",
249 /* Register commands. */
250 static void protocol_registerCmds(void)
257 /* Initialization: readline context, parser and register commands. */
258 void protocol_init(Serial *ser)
260 interactive = FORCE_INTERACTIVE;
262 rl_init_ctx(&rl_ctx);
263 rl_setprompt(&rl_ctx, ">> ");
264 rl_sethook_get(&rl_ctx, (getc_hook)ser_getchar, ser);
265 rl_sethook_put(&rl_ctx, (putc_hook)ser_putchar, ser);
266 rl_sethook_match(&rl_ctx, parser_rl_match, NULL);
270 protocol_registerCmds();