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.
20 __pdata uint16_t ao_cmd_lex_i;
21 __pdata uint32_t ao_cmd_lex_u32;
22 __pdata char ao_cmd_lex_c;
23 __pdata enum ao_cmd_status ao_cmd_status;
27 static __xdata char cmd_line[CMD_LEN];
28 static __pdata uint8_t cmd_len;
29 static __pdata uint8_t cmd_i;
32 put_string(__code char *s)
42 put_string ("\010 \010");
55 /* backspace/delete */
56 if (c == '\010' || c == '\177') {
67 while (cmd_len != 0) {
85 if (cmd_len >= CMD_LEN - 2)
87 cmd_line[cmd_len++] = c;
91 cmd_line[cmd_len++] = '\n';
92 cmd_line[cmd_len++] = '\0';
101 ao_cmd_lex_c = cmd_line[cmd_i++];
110 putchar(v + ('a' - 10));
119 if ('0' <= c && c <= '9')
121 if ('a' <= c && c <= 'f')
122 return c - ('a' - 10);
123 if ('A' <= c && c <= 'F')
124 return c - ('A' - 10);
125 ao_cmd_status = ao_cmd_lex_error;
130 ao_cmd_put16(uint16_t v)
137 ao_cmd_put8(uint8_t v)
139 putnibble((v >> 4) & 0xf);
144 ao_cmd_is_white(void)
146 return ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t';
152 while (ao_cmd_is_white())
157 ao_cmd_hexchar(char c)
159 if ('0' <= c && c <= '9')
161 if ('a' <= c && c <= 'f')
162 return (c - 'a' + 10);
163 if ('A' <= c && c <= 'F')
164 return (c - 'A' + 10);
176 for (i = 0; i < 2; i++) {
177 n = ao_cmd_hexchar(ao_cmd_lex_c);
179 ao_cmd_status = ao_cmd_syntax_error;
182 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | n;
190 __pdata uint8_t r = ao_cmd_lex_error;
196 n = ao_cmd_hexchar(ao_cmd_lex_c);
199 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | n;
203 if (r != ao_cmd_success)
210 __pdata uint8_t r = ao_cmd_lex_error;
215 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
216 ao_cmd_lex_u32 = (ao_cmd_lex_u32 * 10) + (ao_cmd_lex_c - '0');
222 if (r != ao_cmd_success)
224 ao_cmd_lex_i = (uint16_t) ao_cmd_lex_u32;
228 ao_match_word(__code char *word)
231 if (ao_cmd_lex_c != *word) {
232 ao_cmd_status = ao_cmd_syntax_error;
245 if (ao_cmd_status == ao_cmd_success)
246 ao_stdios[ao_cur_stdio].echo = ao_cmd_lex_i != 0;
253 if (!ao_match_word("eboot"))
255 /* Delay waiting for the packet master to be turned off
256 * so that we don't end up back in idle mode because we
257 * received a packet after boot.
260 ao_delay(AO_SEC_TO_TICKS(1));
262 ao_panic(AO_PANIC_REBOOT);
268 printf("manufacturer %s\n"
272 "current-flight %u\n"
290 printf("software-version %s\n", ao_version);
297 static __code struct ao_cmds *__xdata (ao_cmds[NUM_CMDS]);
298 static __pdata uint8_t ao_ncmds;
303 __pdata uint8_t cmds;
305 __code struct ao_cmds * __pdata cs;
308 for (cmds = 0; cmds < ao_ncmds; cmds++) {
310 for (cmd = 0; cs[cmd].func; cmd++) {
312 printf("%-45s %s\n", h, h + 1 + strlen(h));
320 switch(ao_cmd_status) {
321 case ao_cmd_lex_error:
322 case ao_cmd_syntax_error:
323 puts("Syntax error");
331 ao_cmd_register(__code struct ao_cmds *cmds)
333 if (ao_ncmds >= NUM_CMDS)
334 ao_panic(AO_PANIC_CMD);
335 ao_cmds[ao_ncmds++] = cmds;
343 __code struct ao_cmds * __xdata cs;
344 void (*__xdata func)(void);
352 if (c == '\r' || c == '\n')
354 func = (void (*)(void)) NULL;
355 for (cmds = 0; cmds < ao_ncmds; cmds++) {
357 for (cmd = 0; cs[cmd].func; cmd++)
358 if (cs[cmd].help[0] == c) {
368 ao_cmd_status = ao_cmd_syntax_error;
373 __xdata struct ao_task ao_cmd_task;
375 __code struct ao_cmds ao_base_cmds[] = {
377 { ao_task_info, "T\0Tasks" },
378 { echo, "E <0 off, 1 on>\0Echo" },
379 { ao_reboot, "r eboot\0Reboot" },
380 { version, "v\0Version" },
387 ao_cmd_register(&ao_base_cmds[0]);
388 ao_add_task(&ao_cmd_task, ao_cmd, "cmd");