support lolo's board
[protos/xbee-avr.git] / cmdline.c
1 /*
2  *  Copyright Droids Corporation
3  *  Olivier Matz <zer0@droids-corp.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Revision : $Id: cmdline.c,v 1.7 2009-11-08 17:24:33 zer0 Exp $
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <aversive.h>
27 #include <aversive/error.h>
28 #include <aversive/queue.h>
29
30 #include <parse.h>
31 #include <rdline.h>
32 #include <uart.h>
33 #include <clock_time.h>
34
35 #include "callout.h"
36 #include "main.h"
37 #include "cmdline.h"
38
39
40 #ifdef USE_USB
41 #include "DualVirtualSerial.h"
42 #endif
43
44
45 extern parse_pgm_ctx_t main_ctx[];
46
47 int cmdline_dev_send(char c, FILE* f)
48 {
49 #ifdef USE_USB
50         CDC_Device_SendByte(&VirtualSerial1_CDC_Interface, (uint8_t)c);
51 #else
52         uart_send(CMDLINE_UART, c);
53 #endif
54         return 0;
55 }
56
57 int cmdline_dev_recv(FILE* f)
58 {
59         int16_t c;
60 #ifdef USE_USB
61         /* non-blocking ! */
62         c = CDC_Device_ReceiveByte(&VirtualSerial1_CDC_Interface);
63 #else
64         c = uart_recv_nowait(CMDLINE_UART);
65 #endif
66         if (c < 0)
67                 return _FDEV_EOF;
68
69         return c;
70 }
71
72
73 int xbee_dev_send(char c, FILE* f)
74 {
75 #ifdef USE_USB
76         CDC_Device_SendByte(&VirtualSerial2_CDC_Interface, (uint8_t)c);
77 #else
78         uart_send(XBEE_UART, c);
79 #endif
80         return 0;
81 }
82
83 int xbee_dev_recv(FILE* f)
84 {
85         int16_t c;
86
87 #ifdef USE_USB
88         /* non-blocking ! */
89         c = CDC_Device_ReceiveByte(&VirtualSerial2_CDC_Interface);
90 #else
91         c = uart_recv_nowait(XBEE_UART);
92 #endif
93         if (c < 0)
94                 return _FDEV_EOF;
95
96         return c;
97 }
98
99 static void
100 valid_buffer(const char *buf, uint8_t size)
101 {
102         int8_t ret;
103
104         ret = parse(main_ctx, buf);
105         if (ret == PARSE_AMBIGUOUS)
106                 printf_P(PSTR("Ambiguous command\r\n"));
107         else if (ret == PARSE_NOMATCH)
108                 printf_P(PSTR("Command not found\r\n"));
109         else if (ret == PARSE_BAD_ARGS)
110                 printf_P(PSTR("Bad arguments\r\n"));
111 }
112
113 static int8_t
114 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
115                 int16_t *state)
116 {
117         return complete(main_ctx, buf, state, dstbuf, dstsize);
118 }
119
120
121 static void write_char(char c)
122 {
123         cmdline_dev_send(c, NULL);
124 }
125
126
127 void cmdline_init(void)
128 {
129         rdline_init(&xbeeboard.rdl, write_char, valid_buffer, complete_buffer);
130         snprintf_P(xbeeboard.prompt, sizeof(xbeeboard.prompt), PSTR("mainboard > "));
131 }
132
133
134 /* sending "pop" on cmdline uart resets the robot */
135 void emergency(char c)
136 {
137         static uint8_t i = 0;
138
139         if ((i == 0 && c == 'p') ||
140             (i == 1 && c == 'o') ||
141             (i == 2 && c == 'p'))
142                 i++;
143         else if ( !(i == 1 && c == 'p') )
144                 i = 0;
145         if (i == 3)
146                 bootloader();
147 }
148
149 /* log function, add a command to configure
150  * it dynamically */
151 void mylog(struct error * e, ...)
152 {
153         va_list ap;
154 #ifndef HOST_VERSION
155         u16 stream_flags = stdout->flags;
156 #endif
157         uint8_t i;
158         time_h tv;
159
160         if (e->severity > ERROR_SEVERITY_ERROR) {
161                 if (xbeeboard.log_level < e->severity)
162                         return;
163
164                 for (i=0; i<NB_LOGS+1; i++)
165                         if (xbeeboard.logs[i] == e->err_num)
166                                 break;
167                 if (i == NB_LOGS+1)
168                         return;
169         }
170
171         va_start(ap, e);
172         tv = time_get_time();
173         printf_P(PSTR("%d.%.3d: "), (int)tv.s, (int)(tv.us/1000UL));
174
175         vfprintf_P(stdout, e->text, ap);
176         printf_P(PSTR("\r\n"));
177         va_end(ap);
178 #ifndef HOST_VERSION
179         stdout->flags = stream_flags;
180 #endif
181 }
182
183 int cmdline_poll(void)
184 {
185         const char *history, *buffer;
186         int8_t ret, same = 0;
187         int16_t c;
188
189         c = cmdline_dev_recv(NULL);
190         if (c < 0)
191                 return -1;
192
193         ret = rdline_char_in(&xbeeboard.rdl, c);
194         if (ret == 1) {
195                 buffer = rdline_get_buffer(&xbeeboard.rdl);
196                 history = rdline_get_history_item(&xbeeboard.rdl, 0);
197                 if (history) {
198                         same = !memcmp(buffer, history, strlen(history)) &&
199                                 buffer[strlen(history)] == '\n';
200                 }
201                 else
202                         same = 0;
203                 if (strlen(buffer) > 1 && !same)
204                         rdline_add_history(&xbeeboard.rdl, buffer);
205
206                 if (xbeeboard.rdl.status != RDLINE_STOPPED)
207                         rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
208         }
209
210         return 0;
211 }
212