4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
34 * \brief Implementation of the command protocol between the board and the host
39 * \author Giovanni Bajo <rasky@develer.com>
40 * \author Marco Benelli <marco@develer.com>
41 * \author Bernardo Innocenti <bernie@develer.com>
42 * \author Daniele Basile <asterix@develer.com>
46 #include "cmd_ctor.h" // MAKE_CMD, REGISTER_CMD
51 #include <drv/timer.h>
55 #include <drv/buzzer.h>
57 #include <mware/readline.h>
58 #include <mware/parser.h>
60 #include <cfg/compiler.h>
61 #include <cfg/debug.h>
63 #include <kern/kfile.h>
68 #include "appconfig.h"
70 // Define the format string for ADC
71 #define ADC_FORMAT_STR "dddd"
73 // DEBUG: set to 1 to force interactive mode
74 #define FORCE_INTERACTIVE 1
77 * True if we are in interactive mode, false if we are in protocol mode.
78 * In interactive mode, commands are read through readline() (prompt,
79 * completion, history) without IDs, and replies/errors are sent to the serial
81 * In protocol mode, we implement the default protocol
83 static bool interactive;
85 /// Readline context, used for interactive mode.
86 static struct RLContext rl_ctx;
88 uint8_t reg_status_dout;
90 * Send a NAK asking the host to send the current message again.
92 * \a fd kfile handler for serial.
93 * \a err human-readable description of the error for debug purposes.
95 INLINE void NAK(KFile *fd, const char *err)
98 kfile_printf(fd, "NAK \"%s\"\r\n", err);
100 kfile_printf(fd, "NAK\r\n");
104 static void protocol_prompt(KFile *fd)
106 kfile_print(fd, ">> ");
110 * Print args on s, with format specified in t->result_fmt.
111 * Return number of valid arguments or -1 in case of error.
113 static bool protocol_reply(KFile *fd, const struct CmdTemplate *t,
116 unsigned short offset = strlen(t->arg_fmt) + 1;
117 unsigned short nres = strlen(t->result_fmt);
119 for (unsigned short i = 0; i < nres; ++i)
121 if (t->result_fmt[i] == 'd')
123 kfile_printf(fd, " %ld", args[offset+i].l);
125 else if (t->result_fmt[i] == 's')
127 kfile_printf(fd, " %s", args[offset+i].s);
135 kfile_printf(fd, "\r\n");
139 static void protocol_parse(KFile *fd, const char *buf)
141 const struct CmdTemplate *templ;
144 templ = parser_get_cmd_template(buf);
147 kfile_print(fd, "-1 Invalid command.\r\n");
152 parms args[PARSER_MAX_ARGS];
154 /* Args Check. TODO: Handle different case. see doc/PROTOCOL . */
155 if (!parser_get_cmd_arguments(buf, templ, args))
157 kfile_print(fd, "-2 Invalid arguments.\r\n");
163 if(!parser_execute_cmd(templ, args))
165 NAK(fd, "Error in executing command.");
167 if (!protocol_reply(fd, templ, args))
169 NAK(fd, "Invalid return format.");
176 void protocol_run(KFile *fd)
179 * \todo to be removed, we could probably access the serial FIFO
182 static char linebuf[80];
186 kfile_gets(fd, linebuf, sizeof(linebuf));
188 // reset serial port error anyway
191 // check message minimum length
194 /* If we enter lines beginning with sharp(#)
195 they are stripped out from commands */
196 if(linebuf[0] != '#')
198 if (linebuf[0] == 0x1B && linebuf[1] == 0x1B) // ESC
201 kfile_printf(fd, "Entering interactive mode\r\n");
205 protocol_parse(fd, linebuf);
215 * Read a line from serial. We use a temporary buffer
216 * because otherwise we would have to extract a message
217 * from the port immediately: there might not be any
218 * available, and one might get free while we read
219 * the line. We also add a fake ID at the start to
222 buf = rl_readline(&rl_ctx);
224 /* If we enter lines beginning with sharp(#)
225 they are stripped out from commands */
226 if(buf && buf[0] != '#')
230 // exit special case to immediately change serial input
231 if (!strcmp(buf, "exit") || !strcmp(buf, "quit"))
233 rl_clear_history(&rl_ctx);
234 kfile_printf(fd, "Leaving interactive mode...\r\n");
235 interactive = FORCE_INTERACTIVE;
239 //TODO: remove sequence numbers
243 strncpy(linebuf + 2, buf, sizeof(linebuf) - 3);
244 linebuf[sizeof(linebuf) - 1] = '\0';
245 protocol_parse(fd, linebuf);
254 * TODO: Command declarations and definitions should be in another file(s).
255 * Maybe we should use CMD_HUNK_TEMPLATE.
259 MAKE_CMD(ver, "", "ddd",
261 args[1].l = VERS_MAJOR;
262 args[2].l = VERS_MINOR;
263 args[3].l = VERS_REV;
267 /* Sleep. Example of declaring function body directly in macro call. */
268 MAKE_CMD(sleep, "d", "",
270 timer_delay((mtime_t)args[1].l);
275 MAKE_CMD(ping, "", "",
277 //Silence "args not used" warning.
283 MAKE_CMD(dout, "d", "",
285 sipo_putchar((uint8_t)args[1].l);
287 //Store status of dout ports.
288 reg_status_dout = (uint8_t)args[1].l;
292 /* rdout read the status of out ports.*/
293 MAKE_CMD(rdout, "", "d",
295 args[1].l = reg_status_dout;
299 /* Doutx sperimentale....... */
300 MAKE_CMD(doutx, "d", "",
302 sipo_putchar((uint8_t)args[1].l);
304 //Store status of dout ports.
305 reg_status_dout = (uint8_t)args[1].l;
310 MAKE_CMD(reset, "", "",
312 //Silence "args not used" warning.
320 MAKE_CMD(din, "", "d",
322 args[1].l = INPUT_GET();
329 MAKE_CMD(ain, "", ADC_FORMAT_STR,
331 STATIC_ASSERT((sizeof(ADC_FORMAT_STR) - 1) == ADC_CHANNEL_NUM);
332 for(int i = 0; i < ADC_CHANNEL_NUM; i++)
333 args[i+1].l = adc_read_ai_channel(i);
339 MAKE_CMD(beep, "d", "",
345 /* Register commands. */
346 static void protocol_registerCmds(void)
352 //Set off all dout ports.
362 /* Initialization: readline context, parser and register commands. */
363 void protocol_init(KFile *fd)
365 interactive = FORCE_INTERACTIVE;
367 rl_init_ctx(&rl_ctx);
368 //rl_setprompt(&rl_ctx, ">> ");
369 rl_sethook_get(&rl_ctx, (getc_hook)kfile_getc, fd);
370 rl_sethook_put(&rl_ctx, (putc_hook)kfile_putc, fd);
371 rl_sethook_match(&rl_ctx, parser_rl_match, NULL);
372 rl_sethook_clear(&rl_ctx, (clear_hook)kfile_clearerr,fd);
376 protocol_registerCmds();