use callout instead of scheduler
[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
39 extern const parse_ctx_t PROGMEM main_ctx[];
40
41
42 int cmdline_dev_send(char c, FILE* f)
43 {
44         (void)f;
45         uart_send(CMDLINE_UART, c);
46         return 0;
47 }
48
49 int cmdline_dev_recv(FILE* f)
50 {
51         int16_t c;
52
53         (void)f;
54         c = uart_recv_nowait(CMDLINE_UART);
55         if (c < 0)
56                 return _FDEV_EOF;
57
58         return c;
59 }
60
61
62 int xbee_dev_send(char c, FILE* f)
63 {
64         (void)f;
65         uart_send(XBEE_UART, c);
66         return 0;
67 }
68
69 int xbee_dev_recv(FILE* f)
70 {
71         int16_t c;
72
73         (void)f;
74         c = uart_recv_nowait(XBEE_UART);
75         if (c < 0)
76                 return _FDEV_EOF;
77
78         return c;
79 }
80
81 void cmdline_valid_buffer(const char *buf, uint8_t size)
82 {
83         int8_t ret;
84         PGM_P ctx = (PGM_P)main_ctx;
85
86         (void)size;
87         ret = parse(ctx, buf);
88         if (ret == PARSE_AMBIGUOUS)
89                 printf_P(PSTR("Ambiguous command\r\n"));
90         else if (ret == PARSE_NOMATCH)
91                 printf_P(PSTR("Command not found\r\n"));
92         else if (ret == PARSE_BAD_ARGS)
93                 printf_P(PSTR("Bad arguments\r\n"));
94 }
95
96 static int8_t
97 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
98                 int16_t *state)
99 {
100         PGM_P ctx = (PGM_P)main_ctx;
101         return complete(ctx, buf, state, dstbuf, dstsize);
102 }
103
104
105 void cmdline_write_char(char c)
106 {
107         cmdline_dev_send(c, NULL);
108 }
109
110
111 void cmdline_init(void)
112 {
113         rdline_init(&xbeeboard.rdl, cmdline_write_char, cmdline_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                 reset();
132         }
133 }
134
135 /* log function, add a command to configure
136  * it dynamically */
137 void mylog(struct error * e, ...)
138 {
139 #ifndef HOST_VERSION
140         u16 stream_flags = stdout->flags;
141 #endif
142         va_list ap;
143         uint8_t i, flags;
144         uint32_t ms;
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         IRQ_LOCK(flags);
159         ms = global_ms;
160         IRQ_UNLOCK(flags);
161
162         printf_P(PSTR("%d.%.3d: "), (int)(ms/1000UL), (int)(ms%1000UL));
163
164         vfprintf_P(stdout, e->text, ap);
165         printf_P(PSTR("\r\n"));
166         va_end(ap);
167 #ifndef HOST_VERSION
168         stdout->flags = stream_flags;
169 #endif
170 }
171
172 int cmdline_poll(void)
173 {
174         const char *history, *buffer;
175         int8_t ret, same = 0;
176         int16_t c;
177
178         c = cmdline_dev_recv(NULL);
179         if (c < 0)
180                 return -1;
181
182         ret = rdline_char_in(&xbeeboard.rdl, c);
183         if (ret == 1) {
184                 buffer = rdline_get_buffer(&xbeeboard.rdl);
185                 history = rdline_get_history_item(&xbeeboard.rdl, 0);
186                 if (history) {
187                         same = !memcmp(buffer, history, strlen(history)) &&
188                                 buffer[strlen(history)] == '\n';
189                 }
190                 else
191                         same = 0;
192                 if (strlen(buffer) > 1 && !same)
193                         rdline_add_history(&xbeeboard.rdl, buffer);
194
195                 if (xbeeboard.rdl.status != RDLINE_STOPPED)
196                         rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
197         }
198
199         return 0;
200 }
201