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)
211 __pdata 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"
296 printf("software-version %s\n", ao_version);
304 static __code struct ao_cmds *__xdata (ao_cmds[NUM_CMDS]);
305 static __pdata uint8_t ao_ncmds;
310 __pdata uint8_t cmds;
312 __code struct ao_cmds * __pdata cs;
313 __code const char *h;
316 for (cmds = 0; cmds < ao_ncmds; cmds++) {
318 for (cmd = 0; cs[cmd].func; cmd++) {
335 switch(ao_cmd_status) {
336 case ao_cmd_lex_error:
337 case ao_cmd_syntax_error:
338 puts("Syntax error");
346 ao_cmd_register(__code struct ao_cmds *cmds)
348 if (ao_ncmds >= NUM_CMDS)
349 ao_panic(AO_PANIC_CMD);
350 ao_cmds[ao_ncmds++] = cmds;
358 __code struct ao_cmds * __xdata cs;
359 void (*__xdata func)(void);
367 if (c == '\r' || c == '\n')
369 func = (void (*)(void)) NULL;
370 for (cmds = 0; cmds < ao_ncmds; cmds++) {
372 for (cmd = 0; cs[cmd].func; cmd++)
373 if (cs[cmd].help[0] == c) {
383 ao_cmd_status = ao_cmd_syntax_error;
388 __xdata struct ao_task ao_cmd_task;
390 __code struct ao_cmds ao_base_cmds[] = {
393 { ao_task_info, "T\0Tasks" },
395 { echo, "E <0 off, 1 on>\0Echo" },
396 { ao_reboot, "r eboot\0Reboot" },
398 { version, "v\0Version" },
406 ao_cmd_register(&ao_base_cmds[0]);
407 ao_add_task(&ao_cmd_task, ao_cmd, "cmd");