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