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;
33 static __xdata char cmd_line[CMD_LEN];
34 static __pdata uint8_t cmd_len;
35 static __pdata uint8_t cmd_i;
38 ao_put_string(__code char *s)
48 ao_put_string ("\010 \010");
61 /* backspace/delete */
62 if (c == '\010' || c == '\177') {
73 while (cmd_len != 0) {
91 if (cmd_len >= CMD_LEN - 2)
93 cmd_line[cmd_len++] = c;
97 cmd_line[cmd_len++] = '\n';
98 cmd_line[cmd_len++] = '\0';
107 ao_cmd_lex_c = cmd_line[cmd_i++];
116 putchar(v + ('a' - 10));
125 if ('0' <= c && c <= '9')
127 if ('a' <= c && c <= 'f')
128 return c - ('a' - 10);
129 if ('A' <= c && c <= 'F')
130 return c - ('A' - 10);
131 ao_cmd_status = ao_cmd_lex_error;
136 ao_cmd_put16(uint16_t v)
143 ao_cmd_put8(uint8_t v)
145 putnibble((v >> 4) & 0xf);
150 ao_cmd_is_white(void)
152 return ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t';
158 while (ao_cmd_is_white())
163 ao_cmd_hexchar(char c)
165 if ('0' <= c && c <= '9')
167 if ('a' <= c && c <= 'f')
168 return (c - 'a' + 10);
169 if ('A' <= c && c <= 'F')
170 return (c - 'A' + 10);
182 for (i = 0; i < 2; i++) {
183 n = ao_cmd_hexchar(ao_cmd_lex_c);
185 ao_cmd_status = ao_cmd_syntax_error;
188 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | n;
196 __pdata uint8_t r = ao_cmd_lex_error;
202 n = ao_cmd_hexchar(ao_cmd_lex_c);
205 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | n;
209 if (r != ao_cmd_success)
214 ao_cmd_decimal(void) __reentrant
216 uint8_t r = ao_cmd_lex_error;
221 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
222 ao_cmd_lex_u32 = (ao_cmd_lex_u32 * 10) + (ao_cmd_lex_c - '0');
228 if (r != ao_cmd_success)
230 ao_cmd_lex_i = (uint16_t) ao_cmd_lex_u32;
234 ao_match_word(__code char *word)
237 if (ao_cmd_lex_c != *word) {
238 ao_cmd_status = ao_cmd_syntax_error;
251 if (ao_cmd_status == ao_cmd_success)
252 ao_stdios[ao_cur_stdio].echo = ao_cmd_lex_i != 0;
259 if (!ao_match_word("eboot"))
261 /* Delay waiting for the packet master to be turned off
262 * so that we don't end up back in idle mode because we
263 * received a packet after boot.
266 ao_delay(AO_SEC_TO_TICKS(1));
268 ao_panic(AO_PANIC_REBOOT);
272 #define HAS_VERSION 1
279 printf("manufacturer %s\n"
282 #if HAS_FLIGHT || HAS_TRACKER
283 "current-flight %u\n"
287 #if !DISABLE_LOG_SPACE
291 #if defined(AO_BOOT_APPLICATION_BASE) && defined(AO_BOOT_APPLICATION_BOUND)
300 #if HAS_FLIGHT || HAS_TRACKER
305 #if !DISABLE_LOG_SPACE
306 , (unsigned long) ao_storage_log_max
309 #if defined(AO_BOOT_APPLICATION_BASE) && defined(AO_BOOT_APPLICATION_BOUND)
310 , (uint32_t) AO_BOOT_APPLICATION_BOUND - (uint32_t) AO_BOOT_APPLICATION_BASE
313 printf("software-version %s\n", ao_version);
321 static __code struct ao_cmds *__xdata (ao_cmds[NUM_CMDS]);
322 static __pdata uint8_t ao_ncmds;
327 __pdata uint8_t cmds;
329 __code struct ao_cmds * __pdata cs;
330 __code const char *h;
333 for (cmds = 0; cmds < ao_ncmds; cmds++) {
335 for (cmd = 0; cs[cmd].func; cmd++) {
352 switch(ao_cmd_status) {
353 case ao_cmd_lex_error:
354 case ao_cmd_syntax_error:
355 puts("Syntax error");
363 ao_cmd_register(__code struct ao_cmds *cmds)
365 if (ao_ncmds >= NUM_CMDS)
366 ao_panic(AO_PANIC_CMD);
367 ao_cmds[ao_ncmds++] = cmds;
375 __code struct ao_cmds * __xdata cs;
376 void (*__xdata func)(void);
384 if (c == '\r' || c == '\n')
386 func = (void (*)(void)) NULL;
387 for (cmds = 0; cmds < ao_ncmds; cmds++) {
389 for (cmd = 0; cs[cmd].func; cmd++)
390 if (cs[cmd].help[0] == c) {
398 ao_mutex_get(&ao_monitoring_mutex);
403 ao_cmd_status = ao_cmd_syntax_error;
406 ao_mutex_put(&ao_monitoring_mutex);
423 __xdata struct ao_task ao_cmd_task;
425 __code struct ao_cmds ao_base_cmds[] = {
428 { ao_task_info, "T\0Tasks" },
430 { echo, "E <0 off, 1 on>\0Echo" },
431 { ao_reboot, "r eboot\0Reboot" },
433 { version, "v\0Version" },
436 { ao_loader, "X\0Switch to boot loader" },
444 ao_cmd_register(&ao_base_cmds[0]);
445 ao_add_task(&ao_cmd_task, ao_cmd, "cmd");