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.
23 enum ao_cmd_status ao_cmd_status;
27 #define AO_CMD_LEN 128
33 static char cmd_line[AO_CMD_LEN];
34 static uint8_t cmd_len;
37 static const char backspace[] = "\010 \010";
40 ao_put_string(const char *s)
48 ao_cmd_readline(const char *prompt)
52 ao_put_string(prompt);
57 /* backspace/delete */
58 if (c == '\010' || c == '\177') {
61 ao_put_string(backspace);
69 while (cmd_len != 0) {
71 ao_put_string(backspace);
87 if (cmd_len >= AO_CMD_LEN - 2)
89 cmd_line[cmd_len++] = c;
93 cmd_line[cmd_len++] = '\n';
94 cmd_line[cmd_len++] = '\0';
103 ao_cmd_lex_c = cmd_line[cmd_i++];
113 putchar(v + ('a' - 10));
122 if ('0' <= c && c <= '9')
124 if ('a' <= c && c <= 'f')
125 return c - ('a' - 10);
126 if ('A' <= c && c <= 'F')
127 return c - ('A' - 10);
128 ao_cmd_status = ao_cmd_lex_error;
133 ao_cmd_put16(uint16_t v)
140 ao_cmd_put8(uint8_t v)
142 putnibble((v >> 4) & 0xf);
147 ao_cmd_is_white(void)
149 return ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t';
155 while (ao_cmd_is_white())
160 ao_cmd_hexchar(char c)
162 if ('0' <= c && c <= '9')
164 if ('a' <= c && c <= 'f')
165 return (c - 'a' + 10);
166 if ('A' <= c && c <= 'F')
167 return (c - 'A' + 10);
178 for (i = 0; i < lim; i++) {
179 int8_t n = ao_cmd_hexchar(ao_cmd_lex_c);
181 if (i == 0 || lim != 0xff)
182 ao_cmd_status = ao_cmd_lex_error;
185 result = (result << 4) | n;
200 return get_hex(0xff);
207 uint8_t r = ao_cmd_lex_error;
211 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
212 result = result * 10 + (ao_cmd_lex_c - '0');
218 if (r != ao_cmd_success)
224 ao_match_word(const char *word)
227 if (ao_cmd_lex_c != *word) {
228 ao_cmd_status = ao_cmd_syntax_error;
240 uint32_t v = ao_cmd_hex();
241 if (ao_cmd_status == ao_cmd_success)
242 ao_stdios[ao_cur_stdio].echo = v != 0;
249 if (!ao_match_word("eboot"))
251 /* Delay waiting for the packet master to be turned off
252 * so that we don't end up back in idle mode because we
253 * received a packet after boot.
256 ao_delay(AO_SEC_TO_TICKS(1));
258 ao_panic(AO_PANIC_REBOOT);
262 #define HAS_VERSION 1
269 printf("manufacturer %s\n"
272 #if HAS_LOG && (HAS_FLIGHT || HAS_TRACKER)
273 "current-flight %u\n"
277 #if !DISABLE_LOG_SPACE
281 #if defined(AO_BOOT_APPLICATION_BASE) && defined(AO_BOOT_APPLICATION_BOUND)
290 #if HAS_LOG && (HAS_FLIGHT || HAS_TRACKER)
295 #if !DISABLE_LOG_SPACE
296 , (unsigned long) ao_storage_log_max
299 #if defined(AO_BOOT_APPLICATION_BASE) && defined(AO_BOOT_APPLICATION_BOUND)
300 , (unsigned) ((uint32_t) AO_BOOT_APPLICATION_BOUND - (uint32_t) AO_BOOT_APPLICATION_BASE)
303 printf("software-version %s\n", ao_version);
311 static const struct ao_cmds *(ao_cmds[NUM_CMDS]);
312 static uint8_t ao_ncmds;
319 const struct ao_cmds * cs;
323 for (cmds = 0; cmds < ao_ncmds; cmds++) {
325 for (cmd = 0; cs[cmd].func; cmd++) {
342 switch(ao_cmd_status) {
343 case ao_cmd_lex_error:
344 case ao_cmd_syntax_error:
345 ao_put_string("Syntax error\n");
353 ao_cmd_register(const struct ao_cmds *cmds)
355 if (ao_ncmds >= NUM_CMDS)
356 ao_panic(AO_PANIC_CMD);
357 ao_cmds[ao_ncmds++] = cmds;
365 const struct ao_cmds * cs;
369 ao_cmd_readline("> ");
374 if (c == '\r' || c == '\n')
376 func = (void (*)(void)) NULL;
377 for (cmds = 0; cmds < ao_ncmds; cmds++) {
379 for (cmd = 0; cs[cmd].func; cmd++)
380 if (cs[cmd].help[0] == c) {
388 ao_mutex_get(&ao_monitoring_mutex);
393 ao_cmd_status = ao_cmd_syntax_error;
396 ao_mutex_put(&ao_monitoring_mutex);
414 struct ao_task ao_cmd_task;
417 const struct ao_cmds ao_base_cmds[] = {
419 #if HAS_TASK_INFO && HAS_TASK
420 { ao_task_info, "T\0Tasks" },
422 { echo, "E <0 off, 1 on>\0Echo" },
423 { ao_reboot, "r eboot\0Reboot" },
425 { version, "v\0Version" },
428 { ao_loader, "X\0Switch to boot loader" },
436 ao_cmd_register(&ao_base_cmds[0]);
438 ao_add_task(&ao_cmd_task, ao_cmd, "cmd");