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