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;
38 ao_put_string(const 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 >= AO_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++];
117 putchar(v + ('a' - 10));
126 if ('0' <= c && c <= '9')
128 if ('a' <= c && c <= 'f')
129 return c - ('a' - 10);
130 if ('A' <= c && c <= 'F')
131 return c - ('A' - 10);
132 ao_cmd_status = ao_cmd_lex_error;
137 ao_cmd_put16(uint16_t v)
144 ao_cmd_put8(uint8_t v)
146 putnibble((v >> 4) & 0xf);
151 ao_cmd_is_white(void)
153 return ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t';
159 while (ao_cmd_is_white())
164 ao_cmd_hexchar(char c)
166 if ('0' <= c && c <= '9')
168 if ('a' <= c && c <= 'f')
169 return (c - 'a' + 10);
170 if ('A' <= c && c <= 'F')
171 return (c - 'A' + 10);
177 _ao_cmd_hex(uint8_t lim)
183 for (i = 0; i < lim; i++) {
184 int8_t n = ao_cmd_hexchar(ao_cmd_lex_c);
186 if (i == 0 || lim != 0xff)
187 ao_cmd_status = ao_cmd_lex_error;
190 result = (result << 4) | n;
199 return _ao_cmd_hex(2);
205 return _ao_cmd_hex(0xff);
212 uint8_t r = ao_cmd_lex_error;
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)
229 ao_match_word(const char *word)
232 if (ao_cmd_lex_c != *word) {
233 ao_cmd_status = ao_cmd_syntax_error;
245 uint32_t v = ao_cmd_hex();
246 if (ao_cmd_status == ao_cmd_success)
247 ao_stdios[ao_cur_stdio].echo = v != 0;
254 if (!ao_match_word("eboot"))
256 /* Delay waiting for the packet master to be turned off
257 * so that we don't end up back in idle mode because we
258 * received a packet after boot.
261 ao_delay(AO_SEC_TO_TICKS(1));
263 ao_panic(AO_PANIC_REBOOT);
267 #define HAS_VERSION 1
274 printf("manufacturer %s\n"
277 #if HAS_FLIGHT || HAS_TRACKER
278 "current-flight %u\n"
282 #if !DISABLE_LOG_SPACE
286 #if defined(AO_BOOT_APPLICATION_BASE) && defined(AO_BOOT_APPLICATION_BOUND)
295 #if HAS_FLIGHT || HAS_TRACKER
300 #if !DISABLE_LOG_SPACE
301 , (unsigned long) ao_storage_log_max
304 #if defined(AO_BOOT_APPLICATION_BASE) && defined(AO_BOOT_APPLICATION_BOUND)
305 , (unsigned) ((uint32_t) AO_BOOT_APPLICATION_BOUND - (uint32_t) AO_BOOT_APPLICATION_BASE)
308 printf("software-version %s\n", ao_version);
316 static const struct ao_cmds *(ao_cmds[NUM_CMDS]);
317 static uint8_t ao_ncmds;
324 const struct ao_cmds * cs;
328 for (cmds = 0; cmds < ao_ncmds; cmds++) {
330 for (cmd = 0; cs[cmd].func; cmd++) {
347 switch(ao_cmd_status) {
348 case ao_cmd_lex_error:
349 case ao_cmd_syntax_error:
350 ao_put_string("Syntax error\n");
358 ao_cmd_register(const struct ao_cmds *cmds)
360 if (ao_ncmds >= NUM_CMDS)
361 ao_panic(AO_PANIC_CMD);
362 ao_cmds[ao_ncmds++] = cmds;
370 const struct ao_cmds * cs;
379 if (c == '\r' || c == '\n')
381 func = (void (*)(void)) NULL;
382 for (cmds = 0; cmds < ao_ncmds; cmds++) {
384 for (cmd = 0; cs[cmd].func; cmd++)
385 if (cs[cmd].help[0] == c) {
393 ao_mutex_get(&ao_monitoring_mutex);
398 ao_cmd_status = ao_cmd_syntax_error;
401 ao_mutex_put(&ao_monitoring_mutex);
419 struct ao_task ao_cmd_task;
422 const struct ao_cmds ao_base_cmds[] = {
424 #if HAS_TASK_INFO && HAS_TASK
425 { ao_task_info, "T\0Tasks" },
427 { echo, "E <0 off, 1 on>\0Echo" },
428 { ao_reboot, "r eboot\0Reboot" },
430 { version, "v\0Version" },
433 { ao_loader, "X\0Switch to boot loader" },
441 ao_cmd_register(&ao_base_cmds[0]);
443 ao_add_task(&ao_cmd_task, ao_cmd, "cmd");