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)
135 ao_cmd_put8((uint8_t) (v >> 8));
136 ao_cmd_put8((uint8_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')
163 return (int8_t) (c - '0');
164 if ('a' <= c && c <= 'f')
165 return (int8_t) (c - 'a' + 10);
166 if ('A' <= c && c <= 'F')
167 return (int8_t) (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 = (uint32_t) ((result << 4) | (uint32_t) n);
194 return (uint8_t) get_hex(2);
200 return get_hex(0xff);
207 uint8_t r = ao_cmd_lex_error;
208 bool negative = false;
211 if (ao_cmd_lex_c == '-') {
216 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
217 result = result * 10 + (ao_cmd_lex_c - '0');
223 if (r != ao_cmd_success)
231 ao_match_word(const char *word)
234 if (ao_cmd_lex_c != *word) {
235 ao_cmd_status = ao_cmd_syntax_error;
247 uint32_t v = ao_cmd_hex();
248 if (ao_cmd_status == ao_cmd_success)
249 ao_stdios[ao_cur_stdio].echo = v != 0;
256 if (!ao_match_word("eboot"))
258 /* Delay waiting for the packet master to be turned off
259 * so that we don't end up back in idle mode because we
260 * received a packet after boot.
263 ao_delay(AO_SEC_TO_TICKS(1));
265 ao_panic(AO_PANIC_REBOOT);
269 #define HAS_VERSION 1
273 #define _stringify(x) #x
274 #define stringify(x) _stringify(x)
278 printf("manufacturer %s\n"
281 #if HAS_LOG && (HAS_FLIGHT || HAS_TRACKER)
282 "current-flight %u\n"
286 #if !DISABLE_LOG_SPACE
290 #if defined(AO_BOOT_APPLICATION_BASE) && defined(AO_BOOT_APPLICATION_BOUND)
299 #if HAS_LOG && (HAS_FLIGHT || HAS_TRACKER)
304 #if !DISABLE_LOG_SPACE
305 , (unsigned long) ao_storage_log_max
308 #if defined(AO_BOOT_APPLICATION_BASE) && defined(AO_BOOT_APPLICATION_BOUND)
309 , (unsigned) ((uint32_t) AO_BOOT_APPLICATION_BOUND - (uint32_t) AO_BOOT_APPLICATION_BASE)
312 printf("software-version %." stringify(AO_MAX_VERSION) "s\n", ao_version);
320 static const struct ao_cmds *(ao_cmds[NUM_CMDS]);
321 static uint8_t ao_ncmds;
328 const struct ao_cmds * cs;
332 for (cmds = 0; cmds < ao_ncmds; cmds++) {
334 for (cmd = 0; cs[cmd].func; cmd++) {
351 switch(ao_cmd_status) {
352 case ao_cmd_lex_error:
353 case ao_cmd_syntax_error:
354 ao_put_string("Syntax error\n");
362 ao_cmd_register(const struct ao_cmds *cmds)
364 if (ao_ncmds >= NUM_CMDS)
365 ao_panic(AO_PANIC_CMD);
366 ao_cmds[ao_ncmds++] = cmds;
374 const struct ao_cmds * cs;
378 ao_cmd_readline("> ");
383 if (c == '\r' || c == '\n')
385 func = (void (*)(void)) NULL;
386 for (cmds = 0; cmds < ao_ncmds; cmds++) {
388 for (cmd = 0; cs[cmd].func; cmd++)
389 if (cs[cmd].help[0] == c) {
397 ao_mutex_get(&ao_monitoring_mutex);
402 ao_cmd_status = ao_cmd_syntax_error;
405 ao_mutex_put(&ao_monitoring_mutex);
423 struct ao_task ao_cmd_task;
426 const struct ao_cmds ao_base_cmds[] = {
428 #if HAS_TASK_INFO && HAS_TASK
429 { ao_task_info, "T\0Tasks" },
431 { echo, "E <0 off, 1 on>\0Echo" },
432 { ao_reboot, "r eboot\0Reboot" },
434 { version, "v\0Version" },
437 { ao_loader, "X\0Switch to boot loader" },
445 ao_cmd_register(&ao_base_cmds[0]);
447 ao_add_task(&ao_cmd_task, ao_cmd, "cmd");