14cb7569b5df9b0e9840afc8d494cccd5f1386ba
[fw/altos] / src / core / ao_cmd.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
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;
24
25 #define CMD_LEN 48
26
27 static __xdata char     cmd_line[CMD_LEN];
28 static __pdata uint8_t  cmd_len;
29 static __pdata uint8_t  cmd_i;
30
31 static void
32 put_string(__code char *s)
33 {
34         char    c;
35         while ((c = *s++))
36                 putchar(c);
37 }
38
39 static void
40 readline(void)
41 {
42         __pdata char c;
43         if (ao_echo())
44                 put_string("> ");
45         cmd_len = 0;
46         for (;;) {
47                 flush();
48                 c = getchar();
49                 /* backspace/delete */
50                 if (c == '\010' || c == '\177') {
51                         if (cmd_len != 0) {
52                                 if (ao_echo())
53                                         put_string("\010 \010");
54                                 --cmd_len;
55                         }
56                         continue;
57                 }
58
59                 /* ^U */
60                 if (c == '\025') {
61                         while (cmd_len != 0) {
62                                 if (ao_echo())
63                                         put_string("\010 \010");
64                                 --cmd_len;
65                         }
66                         continue;
67                 }
68
69                 /* map CR to NL */
70                 if (c == '\r')
71                         c = '\n';
72
73                 if (c == '\n') {
74                         if (ao_echo())
75                                 putchar('\n');
76                         break;
77                 }
78
79                 if (cmd_len >= CMD_LEN - 2) {
80                         if (ao_echo())
81                                 putchar('\007');
82                         continue;
83                 }
84                 cmd_line[cmd_len++] = c;
85                 if (ao_echo())
86                         putchar(c);
87         }
88         cmd_line[cmd_len++] = '\n';
89         cmd_line[cmd_len++] = '\0';
90         cmd_i = 0;
91 }
92
93 void
94 ao_cmd_lex(void)
95 {
96         ao_cmd_lex_c = '\n';
97         if (cmd_i < cmd_len)
98                 ao_cmd_lex_c = cmd_line[cmd_i++];
99 }
100
101 static void
102 putnibble(uint8_t v)
103 {
104         if (v < 10)
105                 putchar(v + '0');
106         else
107                 putchar(v + ('a' - 10));
108 }
109
110 void
111 ao_cmd_put16(uint16_t v)
112 {
113         ao_cmd_put8(v >> 8);
114         ao_cmd_put8(v);
115 }
116
117 void
118 ao_cmd_put8(uint8_t v)
119 {
120         putnibble((v >> 4) & 0xf);
121         putnibble(v & 0xf);
122 }
123
124 uint8_t
125 ao_cmd_is_white(void)
126 {
127         return ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t';
128 }
129
130 void
131 ao_cmd_white(void)
132 {
133         while (ao_cmd_is_white())
134                 ao_cmd_lex();
135 }
136
137 int8_t
138 ao_cmd_hexchar(char c)
139 {
140         if ('0' <= c && c <= '9')
141                 return (c - '0');
142         if ('a' <= c && c <= 'f')
143                 return (c - 'a' + 10);
144         if ('A' <= c && c <= 'F')
145                 return (c - 'A' + 10);
146         return -1;
147 }
148
149 void
150 ao_cmd_hexbyte(void)
151 {
152         uint8_t i;
153         int8_t  n;
154
155         ao_cmd_lex_i = 0;
156         ao_cmd_white();
157         for (i = 0; i < 2; i++) {
158                 n = ao_cmd_hexchar(ao_cmd_lex_c);
159                 if (n < 0) {
160                         ao_cmd_status = ao_cmd_syntax_error;
161                         break;
162                 }
163                 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | n;
164                 ao_cmd_lex();
165         }
166 }
167
168 void
169 ao_cmd_hex(void)
170 {
171         __pdata uint8_t r = ao_cmd_lex_error;
172         int8_t  n;
173
174         ao_cmd_lex_i = 0;
175         ao_cmd_white();
176         for(;;) {
177                 n = ao_cmd_hexchar(ao_cmd_lex_c);
178                 if (n < 0)
179                         break;
180                 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | n;
181                 r = ao_cmd_success;
182                 ao_cmd_lex();
183         }
184         if (r != ao_cmd_success)
185                 ao_cmd_status = r;
186 }
187
188 void
189 ao_cmd_decimal(void)
190 {
191         __pdata uint8_t r = ao_cmd_lex_error;
192
193         ao_cmd_lex_u32 = 0;
194         ao_cmd_white();
195         for(;;) {
196                 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
197                         ao_cmd_lex_u32 = (ao_cmd_lex_u32 * 10) + (ao_cmd_lex_c - '0');
198                 else
199                         break;
200                 r = ao_cmd_success;
201                 ao_cmd_lex();
202         }
203         if (r != ao_cmd_success)
204                 ao_cmd_status = r;
205         ao_cmd_lex_i = (uint16_t) ao_cmd_lex_u32;
206 }
207
208 uint8_t
209 ao_match_word(__code char *word)
210 {
211         while (*word) {
212                 if (ao_cmd_lex_c != *word) {
213                         ao_cmd_status = ao_cmd_syntax_error;
214                         return 0;
215                 }
216                 word++;
217                 ao_cmd_lex();
218         }
219         return 1;
220 }
221
222 static void
223 echo(void)
224 {
225         ao_cmd_hex();
226         if (ao_cmd_status == ao_cmd_success)
227                 ao_stdios[ao_cur_stdio].echo = ao_cmd_lex_i != 0;
228 }
229
230 static void
231 ao_reboot(void)
232 {
233         ao_cmd_white();
234         if (!ao_match_word("eboot"))
235                 return;
236         /* Delay waiting for the packet master to be turned off
237          * so that we don't end up back in idle mode because we
238          * received a packet after boot.
239          */
240         flush();
241         ao_delay(AO_SEC_TO_TICKS(1));
242         ao_arch_reboot();
243         ao_panic(AO_PANIC_REBOOT);
244 }
245
246 static void
247 version(void)
248 {
249         printf("manufacturer     %s\n", ao_manufacturer);
250         printf("product          %s\n", ao_product);
251         printf("serial-number    %u\n", ao_serial_number);
252 #if HAS_LOG
253         printf("log-format       %u\n", ao_log_format);
254 #endif
255         printf("software-version %s\n", ao_version);
256 }
257
258 #define NUM_CMDS        11
259
260 static __code struct ao_cmds    *__xdata (ao_cmds[NUM_CMDS]);
261 static __pdata uint8_t          ao_ncmds;
262
263 static void
264 help(void)
265 {
266         register uint8_t cmds;
267         register uint8_t cmd;
268         register __code struct ao_cmds * cs;
269
270         for (cmds = 0; cmds < ao_ncmds; cmds++) {
271                 cs = ao_cmds[cmds];
272                 for (cmd = 0; cs[cmd].func; cmd++)
273                         printf("%-45s %s\n",
274                                cs[cmd].help,
275                                cs[cmd].help+1+strlen(cs[cmd].help));
276         }
277 }
278
279 static void
280 report(void)
281 {
282         switch(ao_cmd_status) {
283         case ao_cmd_lex_error:
284         case ao_cmd_syntax_error:
285                 puts("Syntax error");
286                 ao_cmd_status = 0;
287         default:
288                 break;
289         }
290 }
291
292 void
293 ao_cmd_register(__code struct ao_cmds *cmds)
294 {
295         if (ao_ncmds >= NUM_CMDS)
296                 ao_panic(AO_PANIC_CMD);
297         ao_cmds[ao_ncmds++] = cmds;
298 }
299
300 void
301 ao_cmd(void)
302 {
303         char    c;
304         uint8_t cmd, cmds;
305         __code struct ao_cmds * __xdata cs;
306         void (*__xdata func)(void);
307
308         for (;;) {
309                 readline();
310                 ao_cmd_lex();
311                 ao_cmd_white();
312                 c = ao_cmd_lex_c;
313                 ao_cmd_lex();
314                 if (c == '\r' || c == '\n')
315                         continue;
316                 func = (void (*)(void)) NULL;
317                 for (cmds = 0; cmds < ao_ncmds; cmds++) {
318                         cs = ao_cmds[cmds];
319                         for (cmd = 0; cs[cmd].func; cmd++)
320                                 if (cs[cmd].help[0] == c) {
321                                         func = cs[cmd].func;
322                                         break;
323                                 }
324                         if (func)
325                                 break;
326                 }
327                 if (func)
328                         (*func)();
329                 else
330                         ao_cmd_status = ao_cmd_syntax_error;
331                 report();
332         }
333 }
334
335 __xdata struct ao_task ao_cmd_task;
336
337 __code struct ao_cmds   ao_base_cmds[] = {
338         { help,         "?\0Help" },
339         { ao_task_info, "T\0Show tasks" },
340         { echo,         "E <0 off, 1 on>\0Set echo mode" },
341         { ao_reboot,    "r eboot\0Reboot" },
342         { version,      "v\0Version" },
343         { 0,    NULL },
344 };
345
346 void
347 ao_cmd_init(void)
348 {
349         ao_cmd_register(&ao_base_cmds[0]);
350         ao_add_task(&ao_cmd_task, ao_cmd, "cmd");
351 }