fix compilation with latest aversive
[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 const parse_ctx_t PROGMEM main_ctx[];
46
47
48 int cmdline_dev_send(char c, FILE* f)
49 {
50         (void)f;
51 #ifdef USE_USB
52         CDC_Device_SendByte(&VirtualSerial1_CDC_Interface, (uint8_t)c);
53 #else
54         uart_send(CMDLINE_UART, c);
55 #endif
56         return 0;
57 }
58
59 int cmdline_dev_recv(FILE* f)
60 {
61         int16_t c;
62
63         (void)f;
64 #ifdef USE_USB
65         /* non-blocking ! */
66         c = CDC_Device_ReceiveByte(&VirtualSerial1_CDC_Interface);
67 #else
68         c = uart_recv_nowait(CMDLINE_UART);
69 #endif
70         if (c < 0)
71                 return _FDEV_EOF;
72
73         return c;
74 }
75
76
77 int xbee_dev_send(char c, FILE* f)
78 {
79         (void)f;
80 #ifdef USE_USB
81         CDC_Device_SendByte(&VirtualSerial2_CDC_Interface, (uint8_t)c);
82 #else
83         uart_send(XBEE_UART, c);
84 #endif
85         return 0;
86 }
87
88 int xbee_dev_recv(FILE* f)
89 {
90         int16_t c;
91
92         (void)f;
93 #ifdef USE_USB
94         /* non-blocking ! */
95         c = CDC_Device_ReceiveByte(&VirtualSerial2_CDC_Interface);
96 #else
97         c = uart_recv_nowait(XBEE_UART);
98 #endif
99         if (c < 0)
100                 return _FDEV_EOF;
101
102         return c;
103 }
104
105 static void
106 valid_buffer(const char *buf, uint8_t size)
107 {
108         int8_t ret;
109         PGM_P ctx = (PGM_P)main_ctx;
110
111         (void)size;
112         ret = parse(ctx, buf);
113         if (ret == PARSE_AMBIGUOUS)
114                 printf_P(PSTR("Ambiguous command\r\n"));
115         else if (ret == PARSE_NOMATCH)
116                 printf_P(PSTR("Command not found\r\n"));
117         else if (ret == PARSE_BAD_ARGS)
118                 printf_P(PSTR("Bad arguments\r\n"));
119 }
120
121 static int8_t
122 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
123                 int16_t *state)
124 {
125         PGM_P ctx = (PGM_P)main_ctx;
126         return complete(ctx, buf, state, dstbuf, dstsize);
127 }
128
129
130 static void write_char(char c)
131 {
132         cmdline_dev_send(c, NULL);
133 }
134
135
136 void cmdline_init(void)
137 {
138         rdline_init(&xbeeboard.rdl, write_char, valid_buffer, complete_buffer);
139         snprintf_P(xbeeboard.prompt, sizeof(xbeeboard.prompt), PSTR("mainboard > "));
140 }
141
142
143 /* sending "pop" on cmdline uart resets the robot */
144 void emergency(char c)
145 {
146         static uint8_t i = 0;
147
148         if ((i == 0 && c == 'p') ||
149             (i == 1 && c == 'o') ||
150             (i == 2 && c == 'p'))
151                 i++;
152         else if ( !(i == 1 && c == 'p') )
153                 i = 0;
154         if (i == 3)
155                 bootloader();
156 }
157
158 /* log function, add a command to configure
159  * it dynamically */
160 void mylog(struct error * e, ...)
161 {
162         va_list ap;
163 #ifndef HOST_VERSION
164         u16 stream_flags = stdout->flags;
165 #endif
166         uint8_t i;
167         time_h tv;
168
169         if (e->severity > ERROR_SEVERITY_ERROR) {
170                 if (xbeeboard.log_level < e->severity)
171                         return;
172
173                 for (i=0; i<NB_LOGS+1; i++)
174                         if (xbeeboard.logs[i] == e->err_num)
175                                 break;
176                 if (i == NB_LOGS+1)
177                         return;
178         }
179
180         va_start(ap, e);
181         tv = time_get_time();
182         printf_P(PSTR("%d.%.3d: "), (int)tv.s, (int)(tv.us/1000UL));
183
184         vfprintf_P(stdout, e->text, ap);
185         printf_P(PSTR("\r\n"));
186         va_end(ap);
187 #ifndef HOST_VERSION
188         stdout->flags = stream_flags;
189 #endif
190 }
191
192 int cmdline_poll(void)
193 {
194         const char *history, *buffer;
195         int8_t ret, same = 0;
196         int16_t c;
197
198         c = cmdline_dev_recv(NULL);
199         if (c < 0)
200                 return -1;
201
202         ret = rdline_char_in(&xbeeboard.rdl, c);
203         if (ret == 1) {
204                 buffer = rdline_get_buffer(&xbeeboard.rdl);
205                 history = rdline_get_history_item(&xbeeboard.rdl, 0);
206                 if (history) {
207                         same = !memcmp(buffer, history, strlen(history)) &&
208                                 buffer[strlen(history)] == '\n';
209                 }
210                 else
211                         same = 0;
212                 if (strlen(buffer) > 1 && !same)
213                         rdline_add_history(&xbeeboard.rdl, buffer);
214
215                 if (xbeeboard.rdl.status != RDLINE_STOPPED)
216                         rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
217         }
218
219         return 0;
220 }
221