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