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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 __pdata uint16_t ao_cmd_lex_i;
22 __pdata uint32_t ao_cmd_lex_u32;
23 __pdata char ao_cmd_lex_c;
24 __pdata enum ao_cmd_status ao_cmd_status;
28 static __xdata char cmd_line[CMD_LEN];
29 static __pdata uint8_t cmd_len;
30 static __pdata uint8_t cmd_i;
33 ao_put_string(__code char *s)
43 ao_put_string ("\010 \010");
56 /* backspace/delete */
57 if (c == '\010' || c == '\177') {
68 while (cmd_len != 0) {
86 if (cmd_len >= CMD_LEN - 2)
88 cmd_line[cmd_len++] = c;
92 cmd_line[cmd_len++] = '\n';
93 cmd_line[cmd_len++] = '\0';
102 ao_cmd_lex_c = cmd_line[cmd_i++];
111 putchar(v + ('a' - 10));
120 if ('0' <= c && c <= '9')
122 if ('a' <= c && c <= 'f')
123 return c - ('a' - 10);
124 if ('A' <= c && c <= 'F')
125 return c - ('A' - 10);
126 ao_cmd_status = ao_cmd_lex_error;
131 ao_cmd_put16(uint16_t v)
138 ao_cmd_put8(uint8_t v)
140 putnibble((v >> 4) & 0xf);
145 ao_cmd_is_white(void)
147 return ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t';
153 while (ao_cmd_is_white())
158 ao_cmd_hexchar(char c)
160 if ('0' <= c && c <= '9')
162 if ('a' <= c && c <= 'f')
163 return (c - 'a' + 10);
164 if ('A' <= c && c <= 'F')
165 return (c - 'A' + 10);
177 for (i = 0; i < 2; i++) {
178 n = ao_cmd_hexchar(ao_cmd_lex_c);
180 ao_cmd_status = ao_cmd_syntax_error;
183 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | n;
191 __pdata uint8_t r = ao_cmd_lex_error;
197 n = ao_cmd_hexchar(ao_cmd_lex_c);
200 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | n;
204 if (r != ao_cmd_success)
209 ao_cmd_decimal(void) __reentrant
211 uint8_t r = ao_cmd_lex_error;
216 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
217 ao_cmd_lex_u32 = (ao_cmd_lex_u32 * 10) + (ao_cmd_lex_c - '0');
223 if (r != ao_cmd_success)
225 ao_cmd_lex_i = (uint16_t) ao_cmd_lex_u32;
229 ao_match_word(__code char *word)
232 if (ao_cmd_lex_c != *word) {
233 ao_cmd_status = ao_cmd_syntax_error;
246 if (ao_cmd_status == ao_cmd_success)
247 ao_stdios[ao_cur_stdio].echo = ao_cmd_lex_i != 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"
278 "current-flight %u\n"
293 printf("software-version %s\n", ao_version);
301 static __code struct ao_cmds *__xdata (ao_cmds[NUM_CMDS]);
302 static __pdata uint8_t ao_ncmds;
307 __pdata uint8_t cmds;
309 __code struct ao_cmds * __pdata cs;
310 __code const char *h;
313 for (cmds = 0; cmds < ao_ncmds; cmds++) {
315 for (cmd = 0; cs[cmd].func; cmd++) {
332 switch(ao_cmd_status) {
333 case ao_cmd_lex_error:
334 case ao_cmd_syntax_error:
335 puts("Syntax error");
343 ao_cmd_register(__code struct ao_cmds *cmds)
345 if (ao_ncmds >= NUM_CMDS)
346 ao_panic(AO_PANIC_CMD);
347 ao_cmds[ao_ncmds++] = cmds;
355 __code struct ao_cmds * __xdata cs;
356 void (*__xdata func)(void);
364 if (c == '\r' || c == '\n')
366 func = (void (*)(void)) NULL;
367 for (cmds = 0; cmds < ao_ncmds; cmds++) {
369 for (cmd = 0; cs[cmd].func; cmd++)
370 if (cs[cmd].help[0] == c) {
380 ao_cmd_status = ao_cmd_syntax_error;
397 __xdata struct ao_task ao_cmd_task;
399 __code struct ao_cmds ao_base_cmds[] = {
402 { ao_task_info, "T\0Tasks" },
404 { echo, "E <0 off, 1 on>\0Echo" },
405 { ao_reboot, "r eboot\0Reboot" },
407 { version, "v\0Version" },
410 { ao_loader, "X\0Switch to boot loader" },
418 ao_cmd_register(&ao_base_cmds[0]);
419 ao_add_task(&ao_cmd_task, ao_cmd, "cmd");