2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 __pdata uint16_t ao_cmd_lex_i;
23 __pdata uint32_t ao_cmd_lex_u32;
24 __pdata char ao_cmd_lex_c;
25 __pdata enum ao_cmd_status ao_cmd_status;
29 #define AO_CMD_LEN 128
35 static __xdata char cmd_line[AO_CMD_LEN];
36 static __pdata uint8_t cmd_len;
37 static __pdata uint8_t cmd_i;
40 ao_put_string(__code char *s)
50 ao_put_string ("\010 \010");
63 /* backspace/delete */
64 if (c == '\010' || c == '\177') {
75 while (cmd_len != 0) {
93 if (cmd_len >= AO_CMD_LEN - 2)
95 cmd_line[cmd_len++] = c;
99 cmd_line[cmd_len++] = '\n';
100 cmd_line[cmd_len++] = '\0';
109 ao_cmd_lex_c = cmd_line[cmd_i++];
119 putchar(v + ('a' - 10));
128 if ('0' <= c && c <= '9')
130 if ('a' <= c && c <= 'f')
131 return c - ('a' - 10);
132 if ('A' <= c && c <= 'F')
133 return c - ('A' - 10);
134 ao_cmd_status = ao_cmd_lex_error;
139 ao_cmd_put16(uint16_t v)
146 ao_cmd_put8(uint8_t v)
148 putnibble((v >> 4) & 0xf);
153 ao_cmd_is_white(void)
155 return ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t';
161 while (ao_cmd_is_white())
166 ao_cmd_hexchar(char c)
168 if ('0' <= c && c <= '9')
170 if ('a' <= c && c <= 'f')
171 return (c - 'a' + 10);
172 if ('A' <= c && c <= 'F')
173 return (c - 'A' + 10);
185 for (i = 0; i < 2; i++) {
186 n = ao_cmd_hexchar(ao_cmd_lex_c);
188 ao_cmd_status = ao_cmd_syntax_error;
191 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | n;
199 __pdata uint8_t r = ao_cmd_lex_error;
205 n = ao_cmd_hexchar(ao_cmd_lex_c);
208 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | n;
212 if (r != ao_cmd_success)
217 ao_cmd_decimal(void) __reentrant
219 uint8_t r = ao_cmd_lex_error;
224 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
225 ao_cmd_lex_u32 = (ao_cmd_lex_u32 * 10) + (ao_cmd_lex_c - '0');
231 if (r != ao_cmd_success)
233 ao_cmd_lex_i = (uint16_t) ao_cmd_lex_u32;
237 ao_match_word(__code char *word)
240 if (ao_cmd_lex_c != *word) {
241 ao_cmd_status = ao_cmd_syntax_error;
254 if (ao_cmd_status == ao_cmd_success)
255 ao_stdios[ao_cur_stdio].echo = ao_cmd_lex_i != 0;
262 if (!ao_match_word("eboot"))
264 /* Delay waiting for the packet master to be turned off
265 * so that we don't end up back in idle mode because we
266 * received a packet after boot.
269 ao_delay(AO_SEC_TO_TICKS(1));
271 ao_panic(AO_PANIC_REBOOT);
275 #define HAS_VERSION 1
282 printf("manufacturer %s\n"
285 #if HAS_FLIGHT || HAS_TRACKER
286 "current-flight %u\n"
290 #if !DISABLE_LOG_SPACE
294 #if defined(AO_BOOT_APPLICATION_BASE) && defined(AO_BOOT_APPLICATION_BOUND)
303 #if HAS_FLIGHT || HAS_TRACKER
308 #if !DISABLE_LOG_SPACE
309 , (unsigned long) ao_storage_log_max
312 #if defined(AO_BOOT_APPLICATION_BASE) && defined(AO_BOOT_APPLICATION_BOUND)
313 , (unsigned) ((uint32_t) AO_BOOT_APPLICATION_BOUND - (uint32_t) AO_BOOT_APPLICATION_BASE)
316 printf("software-version %s\n", ao_version);
324 static __code struct ao_cmds *__xdata (ao_cmds[NUM_CMDS]);
325 static __pdata uint8_t ao_ncmds;
330 __pdata uint8_t cmds;
332 __code struct ao_cmds * __pdata cs;
333 __code const char *h;
336 for (cmds = 0; cmds < ao_ncmds; cmds++) {
338 for (cmd = 0; cs[cmd].func; cmd++) {
355 switch(ao_cmd_status) {
356 case ao_cmd_lex_error:
357 case ao_cmd_syntax_error:
358 puts("Syntax error");
366 ao_cmd_register(__code struct ao_cmds *cmds)
368 if (ao_ncmds >= NUM_CMDS)
369 ao_panic(AO_PANIC_CMD);
370 ao_cmds[ao_ncmds++] = cmds;
378 __code struct ao_cmds * __xdata cs;
379 void (*__xdata func)(void);
387 if (c == '\r' || c == '\n')
389 func = (void (*)(void)) NULL;
390 for (cmds = 0; cmds < ao_ncmds; cmds++) {
392 for (cmd = 0; cs[cmd].func; cmd++)
393 if (cs[cmd].help[0] == c) {
401 ao_mutex_get(&ao_monitoring_mutex);
406 ao_cmd_status = ao_cmd_syntax_error;
409 ao_mutex_put(&ao_monitoring_mutex);
426 __xdata struct ao_task ao_cmd_task;
428 __code struct ao_cmds ao_base_cmds[] = {
431 { ao_task_info, "T\0Tasks" },
433 { echo, "E <0 off, 1 on>\0Echo" },
434 { ao_reboot, "r eboot\0Reboot" },
436 { version, "v\0Version" },
439 { ao_loader, "X\0Switch to boot loader" },
447 ao_cmd_register(&ao_base_cmds[0]);
448 ao_add_task(&ao_cmd_task, ao_cmd, "cmd");