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