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