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>
47 #include <drv/timer.h>
49 #include <mware/readline.h>
50 #include <mware/parser.h>
51 #include <cfg/compiler.h>
52 #include <cfg/debug.h>
60 //#include <cmd_hunk.h>
62 #include "cmd_ctor.h" // MAKE_CMD, REGISTER_CMD
65 #include <drv/buzzer.h>
67 // Define the format string for ADC
68 #define ADC_FORMAT_STR "dddd"
70 // DEBUG: set to 1 to force interactive mode
71 #define FORCE_INTERACTIVE 1
74 * True if we are in interactive mode, false if we are in protocol mode.
75 * In interactive mode, commands are read through readline() (prompt,
76 * completion, history) without IDs, and replies/errors are sent to the serial
78 * In protocol mode, we implement the default protocol
80 static bool interactive;
82 /// Readline context, used for interactive mode.
83 static struct RLContext rl_ctx;
85 uint8_t reg_status_dout;
87 * Send a NAK asking the host to send the current message again.
89 * \param err human-readable description of the error for debug purposes.
91 INLINE void NAK(Serial *ser, const char *err)
94 ser_printf(ser, "NAK \"%s\"\r\n", err);
96 ser_printf(ser, "NAK\r\n");
100 static void protocol_prompt(Serial *ser)
102 ser_print(ser, ">> ");
106 * Print args on s, with format specified in t->result_fmt.
107 * Return number of valid arguments or -1 in case of error.
109 static bool protocol_reply(Serial *s, const struct CmdTemplate *t,
112 unsigned short offset = strlen(t->arg_fmt) + 1;
113 unsigned short nres = strlen(t->result_fmt);
115 for (unsigned short i = 0; i < nres; ++i)
117 if (t->result_fmt[i] == 'd')
119 ser_printf(s, " %ld", args[offset+i].l);
121 else if (t->result_fmt[i] == 's')
123 ser_printf(s, " %s", args[offset+i].s);
131 ser_printf(s, "\r\n");
135 static void protocol_parse(Serial *ser, const char *buf)
137 const struct CmdTemplate *templ;
140 templ = parser_get_cmd_template(buf);
143 ser_print(ser, "-1 Invalid command.\r\n");
144 protocol_prompt(ser);
148 parms args[PARSER_MAX_ARGS];
150 /* Args Check. TODO: Handle different case. see doc/PROTOCOL . */
151 if (!parser_get_cmd_arguments(buf, templ, args))
153 ser_print(ser, "-2 Invalid arguments.\r\n");
154 protocol_prompt(ser);
159 if(!parser_execute_cmd(templ, args))
161 NAK(ser, "Error in executing command.");
163 if (!protocol_reply(ser, templ, args))
165 NAK(ser, "Invalid return format.");
168 protocol_prompt(ser);
172 void protocol_run(Serial *ser)
175 * \todo to be removed, we could probably access the serial FIFO
178 static char linebuf[80];
182 ser_gets(ser, linebuf, sizeof(linebuf));
184 // reset serial port error anyway
185 ser_setstatus(ser, 0);
187 // check message minimum length
190 /* If we enter lines beginning with sharp(#)
191 they are stripped out from commands */
192 if(linebuf[0] != '#')
194 if (linebuf[0] == 0x1B && linebuf[1] == 0x1B) // ESC
198 "Entering interactive mode\r\n");
202 protocol_parse(ser, linebuf);
212 * Read a line from serial. We use a temporary buffer
213 * because otherwise we would have to extract a message
214 * from the port immediately: there might not be any
215 * available, and one might get free while we read
216 * the line. We also add a fake ID at the start to
219 buf = rl_readline(&rl_ctx);
221 /* If we enter lines beginning with sharp(#)
222 they are stripped out from commands */
223 if(buf && buf[0] != '#')
227 // exit special case to immediately change serial input
228 if (!strcmp(buf, "exit") || !strcmp(buf, "quit"))
230 rl_clear_history(&rl_ctx);
232 "Leaving interactive mode...\r\n");
233 interactive = FORCE_INTERACTIVE;
237 //TODO: remove sequence numbers
241 strncpy(linebuf + 2, buf, sizeof(linebuf) - 3);
242 linebuf[sizeof(linebuf) - 1] = '\0';
243 protocol_parse(ser, linebuf);
252 * TODO: Command declarations and definitions should be in another file(s).
253 * Maybe we should use CMD_HUNK_TEMPLATE.
257 MAKE_CMD(ver, "", "ddd",
259 args[1].l = VERS_MAJOR;
260 args[2].l = VERS_MINOR;
261 args[3].l = VERS_REV;
265 /* Sleep. Example of declaring function body directly in macro call. */
266 MAKE_CMD(sleep, "d", "",
268 timer_delay((mtime_t)args[1].l);
273 MAKE_CMD(ping, "", "",
275 //Silence "args not used" warning.
281 MAKE_CMD(dout, "d", "",
283 sipo_putchar((uint8_t)args[1].l);
285 //Store status of dout ports.
286 reg_status_dout = (uint8_t)args[1].l;
290 /* rdout read the status of out ports.*/
291 MAKE_CMD(rdout, "", "d",
293 args[1].l = reg_status_dout;
297 /* Doutx sperimentale....... */
298 MAKE_CMD(doutx, "d", "",
300 sipo_putchar((uint8_t)args[1].l);
302 //Store status of dout ports.
303 reg_status_dout = (uint8_t)args[1].l;
308 MAKE_CMD(reset, "", "",
310 //Silence "args not used" warning.
318 MAKE_CMD(din, "", "d",
320 args[1].l = INPUT_GET();
327 MAKE_CMD(ain, "", ADC_FORMAT_STR,
329 STATIC_ASSERT((sizeof(ADC_FORMAT_STR) - 1) == ADC_CHANNEL_NUM);
330 for(int i = 0; i < ADC_CHANNEL_NUM; i++)
331 args[i+1].l = adc_read_ai_channel(i);
337 MAKE_CMD(beep, "d", "",
343 /* Register commands. */
344 static void protocol_registerCmds(void)
350 //Set off all dout ports.
360 /* Initialization: readline context, parser and register commands. */
361 void protocol_init(Serial *ser)
363 interactive = FORCE_INTERACTIVE;
365 rl_init_ctx(&rl_ctx);
366 //rl_setprompt(&rl_ctx, ">> ");
367 rl_sethook_get(&rl_ctx, (getc_hook)ser_getchar, ser);
368 rl_sethook_put(&rl_ctx, (putc_hook)ser_putchar, ser);
369 rl_sethook_match(&rl_ctx, parser_rl_match, NULL);
370 rl_sethook_clear(&rl_ctx, (clear_hook)ser_clearstatus,ser);
374 protocol_registerCmds();
376 protocol_prompt(ser);