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.4 2006/06/14 00:26:48 marco
20 *#* Use new macros for defining commands.
22 *#* Revision 1.3 2006/06/13 19:07:31 marco
23 *#* Fixed a bug in protocol_reply. Simplified rpc.
25 *#* Revision 1.2 2006/06/12 21:37:02 marco
26 *#* implemented some commands (ver and sleep)
28 *#* Revision 1.1 2006/06/01 12:29:21 marco
29 *#* Add first simple protocol command (version request).
36 #include <drv/timer.h>
37 #include <mware/readline.h>
38 #include <mware/parser.h>
39 #include <cfg/compiler.h>
40 #include <cfg/debug.h>
47 #include "cmd_ctor.h" // MAKE_CMD, REGISTER_CMD
49 // DEBUG: set to 1 to force interactive mode
50 #define FORCE_INTERACTIVE 1
53 * True if we are in interactive mode, false if we are in protocol mode.
54 * In interactive mode, commands are read through readline() (prompt,
55 * completion, history) without IDs, and replies/errors are sent to the serial
57 * In protocol mode, we implement the default protocol
59 static bool interactive;
61 /// Readline context, used for interactive mode.
62 static struct RLContext rl_ctx;
66 * Send a NAK asking the host to send the current message again.
68 * \param err human-readable description of the error for debug purposes.
70 INLINE void NAK(Serial *ser, const char *err)
73 ser_printf(ser, "NAK \"%s\"\r\n", err);
75 ser_printf(ser, "NAK\r\n");
80 * Print args on s, with format specified in t->result_fmt.
81 * Return number of valid arguments or -1 in case of error.
83 static int protocol_reply(Serial *s, const struct CmdTemplate *t,
86 unsigned short offset = strlen(t->arg_fmt) + 1;
87 unsigned short nres = strlen(t->result_fmt);
91 for (unsigned short i = 0; i < nres; ++i)
93 if (t->result_fmt[i] == 'd')
95 ser_printf(s, "%ld", args[offset+i].l);
97 else if (t->result_fmt[i] == 's')
99 ser_printf(s, "%s ", args[offset+i].s);
107 ser_print(s, "\r\n");
111 static void protocol_parse(Serial *ser, const char *buf)
113 const struct CmdTemplate *templ;
116 templ = parser_get_cmd_template(buf);
119 NAK(ser, "Invalid command.");
123 parms args[PARSER_MAX_ARGS];
126 if (!parser_get_cmd_arguments(buf, templ, args))
128 NAK(ser, "Invalid arguments.");
133 if (!parser_execute_cmd(templ, args))
135 NAK(ser, "Command failed.");
139 if (protocol_reply(ser, templ, args) < 0)
141 NAK(ser, "Invalid return format.");
147 void protocol_run(Serial *ser)
150 * \todo to be removed, we could probably access the serial FIFO
153 static char linebuf[80];
157 ser_gets(ser, linebuf, sizeof(linebuf));
159 // reset serial port error anyway
160 ser_setstatus(ser, 0);
162 // check message minimum length
165 if (linebuf[0] == 0x1B && linebuf[1] == 0x1B) // ESC
169 "Entering interactive mode\r\n");
173 protocol_parse(ser, linebuf);
182 * Read a line from serial. We use a temporary buffer
183 * because otherwise we would have to extract a message
184 * from the port immediately: there might not be any
185 * available, and one might get free while we read
186 * the line. We also add a fake ID at the start to
189 buf = rl_readline(&rl_ctx);
191 if (buf && buf[0] != '\0')
193 // exit special case to immediately change serial input
194 if (!strcmp(buf, "exit") || !strcmp(buf, "quit"))
196 rl_clear_history(&rl_ctx);
198 "Leaving interactive mode...\r\n");
199 interactive = FORCE_INTERACTIVE;
203 //TODO: remove sequence numbers
207 strncpy(linebuf + 2, buf, sizeof(linebuf) - 3);
208 linebuf[sizeof(linebuf) - 1] = '\0';
209 protocol_parse(ser, linebuf);
217 * TODO: Command declarations and definitions should be in another file(s).
218 * Maybe we should use CMD_HUNK_TEMPLATE.
223 MAKE_CMD(ver, "", "s",
225 args[1].s = VERS_TAG;
230 MAKE_CMD(sleep, "d", "",
232 timer_delay((mtime_t)args[1].l);
236 /* Register commands. */
237 static void protocol_registerCmds(void)
243 /* Initialization: readline context, parser and register commands. */
244 void protocol_init(Serial *ser)
246 interactive = FORCE_INTERACTIVE;
248 rl_init_ctx(&rl_ctx);
249 rl_setprompt(&rl_ctx, ">> ");
250 rl_sethook_get(&rl_ctx, (getc_hook)ser_getchar, ser);
251 rl_sethook_put(&rl_ctx, (putc_hook)ser_putchar, ser);
252 rl_sethook_match(&rl_ctx, parser_rl_match, NULL);
256 protocol_registerCmds();