dump/set rc_mode
[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
34 #include "callout.h"
35 #include "main.h"
36 #include "cmdline.h"
37
38 #define FLUSH_LOGS_MS 1000 /* every second */
39 #define LOG_PER_SEC_MAX 10
40
41 extern const parse_ctx_t PROGMEM main_ctx[];
42
43 static struct callout flush_log_timer;
44 static uint8_t log_count;
45
46 int cmdline_dev_send(char c, FILE* f)
47 {
48         (void)f;
49         uart_send(CMDLINE_UART, c);
50         return 0;
51 }
52
53 int cmdline_dev_recv(FILE* f)
54 {
55         int16_t c;
56
57         (void)f;
58         c = uart_recv_nowait(CMDLINE_UART);
59         if (c < 0)
60                 return _FDEV_EOF;
61
62         return c;
63 }
64
65
66 int xbee_dev_send(char c, FILE* f)
67 {
68         (void)f;
69         uart_send(XBEE_UART, c);
70         return 0;
71 }
72
73 int xbee_dev_recv(FILE* f)
74 {
75         int16_t c;
76
77         (void)f;
78         c = uart_recv_nowait(XBEE_UART);
79         if (c < 0)
80                 return _FDEV_EOF;
81
82         return c;
83 }
84
85 void cmdline_valid_buffer(const char *buf, uint8_t size)
86 {
87         int8_t ret;
88         PGM_P ctx = (PGM_P)main_ctx;
89
90         (void)size;
91         ret = parse(ctx, buf);
92         if (ret == PARSE_AMBIGUOUS)
93                 printf_P(PSTR("Ambiguous command\r\n"));
94         else if (ret == PARSE_NOMATCH)
95                 printf_P(PSTR("Command not found\r\n"));
96         else if (ret == PARSE_BAD_ARGS)
97                 printf_P(PSTR("Bad arguments\r\n"));
98 }
99
100 static int8_t
101 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
102                 int16_t *state)
103 {
104         PGM_P ctx = (PGM_P)main_ctx;
105         return complete(ctx, buf, state, dstbuf, dstsize);
106 }
107
108
109 void cmdline_write_char(char c)
110 {
111         cmdline_dev_send(c, NULL);
112 }
113
114
115 /* sending "pop" on cmdline uart resets the robot */
116 void emergency(char c)
117 {
118         static uint8_t i = 0;
119
120         if ((i == 0 && c == 'p') ||
121             (i == 1 && c == 'o') ||
122             (i == 2 && c == 'p'))
123                 i++;
124         else if ( !(i == 1 && c == 'p') )
125                 i = 0;
126         if (i == 3) {
127                 //bootloader();
128                 reset();
129         }
130 }
131
132 /* log function, configured dynamically */
133 void mylog(struct error * e, ...)
134 {
135 #ifndef HOST_VERSION
136         u16 stream_flags = stdout->flags;
137 #endif
138         va_list ap;
139         uint8_t i, flags;
140         uint32_t ms;
141         uint8_t prio;
142
143         /* too many logs */
144         if (log_count >= LOG_PER_SEC_MAX)
145                 return;
146
147         /* higher log value means lower criticity */
148         if (e->severity > ERROR_SEVERITY_ERROR) {
149                 if (xbeeboard.log_level < e->severity)
150                         return;
151
152                 for (i=0; i<NB_LOGS+1; i++)
153                         if (xbeeboard.logs[i] == e->err_num)
154                                 break;
155                 if (i == NB_LOGS+1)
156                         return;
157         }
158
159         /* get time */
160         IRQ_LOCK(flags);
161         ms = global_ms;
162         IRQ_UNLOCK(flags);
163
164         /* prevent flush log to occur */
165         prio = callout_mgr_set_prio(&xbeeboard.intr_cm,
166                 LOW_PRIO);
167
168         /* display the log */
169         va_start(ap, e);
170         printf_P(PSTR("%d.%.3d: "), (int)(ms/1000UL), (int)(ms%1000UL));
171         vfprintf_P(stdout, e->text, ap);
172         printf_P(PSTR("\r\n"));
173         va_end(ap);
174
175 #ifndef HOST_VERSION
176         stdout->flags = stream_flags;
177 #endif
178         callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
179 }
180
181 static void flush_logs_cb(struct callout_mgr *cm, struct callout *tim,
182         void *arg)
183 {
184         (void)cm;
185         (void)tim;
186         (void)arg;
187
188         if (log_count == LOG_PER_SEC_MAX)
189                 printf_P("some logs were dropped\n");
190         callout_reschedule(&xbeeboard.intr_cm, &flush_log_timer,
191                 FLUSH_LOGS_MS);
192 }
193
194
195 int cmdline_poll(void)
196 {
197         const char *history, *buffer;
198         int8_t ret, same = 0;
199         int16_t c;
200
201         c = cmdline_dev_recv(NULL);
202         if (c < 0)
203                 return -1;
204
205         ret = rdline_char_in(&xbeeboard.rdl, c);
206         if (ret == 1) {
207                 buffer = rdline_get_buffer(&xbeeboard.rdl);
208                 history = rdline_get_history_item(&xbeeboard.rdl, 0);
209                 if (history) {
210                         same = !memcmp(buffer, history, strlen(history)) &&
211                                 buffer[strlen(history)] == '\n';
212                 }
213                 else
214                         same = 0;
215                 if (strlen(buffer) > 1 && !same)
216                         rdline_add_history(&xbeeboard.rdl, buffer);
217
218                 if (xbeeboard.rdl.status != RDLINE_STOPPED)
219                         rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
220         }
221
222         return 0;
223 }
224
225 void cmdline_init(void)
226 {
227         /* init command line */
228         rdline_init(&xbeeboard.rdl, cmdline_write_char, cmdline_valid_buffer,
229                 complete_buffer);
230         snprintf_P(xbeeboard.prompt, sizeof(xbeeboard.prompt),
231                 PSTR("mainboard > "));
232
233         /* load a timer for flushing logs */
234         callout_init(&flush_log_timer, flush_logs_cb, NULL, LOW_PRIO);
235         callout_schedule(&xbeeboard.intr_cm, &flush_log_timer, FLUSH_LOGS_MS);
236 }