make it compile
[protos/imu.git] / commands_gen.c
1 /*
2  *  Copyright Droids Corporation (2011)
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; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: commands_gen.c,v 1.8 2009-11-08 17:24:33 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org>
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <aversive/pgmspace.h>
27 #include <aversive/wait.h>
28 #include <aversive/error.h>
29 #include <aversive/queue.h>
30
31 #include <uart.h>
32
33 #include <rdline.h>
34 #include <parse.h>
35 #include <parse_string.h>
36 #include <parse_num.h>
37
38 #include <diagnostic.h>
39
40 #include "callout.h"
41 #include "main.h"
42 #include "cmdline.h"
43
44 /**********************************************************/
45 /* Reset */
46
47 /* this structure is filled when cmd_reset is parsed successfully */
48 struct cmd_reset_result {
49         fixed_string_t arg0;
50 };
51
52 /* function called when cmd_reset is parsed successfully */
53 static void cmd_reset_parsed(void * parsed_result, void * data)
54 {
55         (void)parsed_result;
56         (void)data;
57 #ifdef HOST_VERSION
58         hostsim_exit();
59 #endif
60         reset();
61 }
62
63 const char PROGMEM str_reset_arg0[] = "reset";
64 const parse_token_string_t PROGMEM cmd_reset_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_reset_result, arg0, str_reset_arg0);
65
66 const char PROGMEM help_reset[] = "Reset the board";
67 const parse_inst_t PROGMEM cmd_reset = {
68         .f = cmd_reset_parsed,  /* function to call */
69         .data = NULL,      /* 2nd arg of func */
70         .help_str = help_reset,
71         .tokens = {        /* token list, NULL terminated */
72                 (PGM_P)&cmd_reset_arg0,
73                 NULL,
74         },
75 };
76
77 /**********************************************************/
78 /* Bootloader */
79
80 /* this structure is filled when cmd_bootloader is parsed successfully */
81 struct cmd_bootloader_result {
82         fixed_string_t arg0;
83 };
84
85 /* function called when cmd_bootloader is parsed successfully */
86 static void cmd_bootloader_parsed(void *parsed_result, void *data)
87 {
88         (void)parsed_result;
89         (void)data;
90 #ifndef HOST_VERSION
91         bootloader();
92 #else
93         printf("not implemented\n");
94 #endif
95 }
96
97 const char PROGMEM str_bootloader_arg0[] = "bootloader";
98 const parse_token_string_t PROGMEM cmd_bootloader_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_bootloader_result, arg0, str_bootloader_arg0);
99
100 const char PROGMEM help_bootloader[] = "Launch the bootloader";
101 const parse_inst_t PROGMEM cmd_bootloader = {
102         .f = cmd_bootloader_parsed,  /* function to call */
103         .data = NULL,      /* 2nd arg of func */
104         .help_str = help_bootloader,
105         .tokens = {        /* token list, NULL terminated */
106                 (PGM_P)&cmd_bootloader_arg0,
107                 NULL,
108         },
109 };
110
111 /**********************************************************/
112 /* Callout show */
113
114 /* this structure is filled when cmd_callout is parsed successfully */
115 struct cmd_callout_result {
116         fixed_string_t arg0;
117         fixed_string_t arg1;
118 };
119
120 /* function called when cmd_callout is parsed successfully */
121 static void cmd_callout_parsed(void *parsed_result, void *data)
122 {
123         (void)parsed_result;
124         (void)data;
125         callout_dump_stats(&imuboard.intr_cm);
126 }
127
128 const char PROGMEM str_callout_arg0[] = "callout";
129 const parse_token_string_t PROGMEM cmd_callout_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_callout_result, arg0, str_callout_arg0);
130 const char PROGMEM str_callout_arg1[] = "show";
131 const parse_token_string_t PROGMEM cmd_callout_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_callout_result, arg1, str_callout_arg1);
132
133 const char PROGMEM help_callout[] = "Show callout events";
134 const parse_inst_t PROGMEM cmd_callout = {
135         .f = cmd_callout_parsed,  /* function to call */
136         .data = NULL,      /* 2nd arg of func */
137         .help_str = help_callout,
138         .tokens = {        /* token list, NULL terminated */
139                 (PGM_P)&cmd_callout_arg0,
140                 (PGM_P)&cmd_callout_arg1,
141                 NULL,
142         },
143 };
144
145 /**********************************************************/
146 /* Log */
147
148 /* this structure is filled when cmd_log is parsed successfully */
149 struct cmd_log_result {
150         fixed_string_t arg0;
151         fixed_string_t arg1;
152         uint8_t arg2;
153         fixed_string_t arg3;
154 };
155
156 /* keep it sync with string choice */
157 static const char PROGMEM uart_log[] = "uart";
158 static const char PROGMEM i2c_log[] = "i2c";
159 static const char PROGMEM default_log[] = "default";
160 static const char PROGMEM xbee_log[] = "xbee";
161 static const char PROGMEM rc_proto_log[] = "rc_proto";
162
163 struct log_name_and_num {
164         const char *name;
165         uint8_t num;
166 };
167
168 static const struct log_name_and_num log_name_and_num[] = {
169         { uart_log, E_UART },
170         { i2c_log, E_I2C },
171         { default_log, E_USER_DEFAULT },
172         { xbee_log, E_USER_XBEE },
173         { rc_proto_log, E_USER_RC_PROTO },
174 };
175
176 static uint8_t
177 log_name2num(const char * s)
178 {
179         uint8_t i;
180
181         for (i=0; i<sizeof(log_name_and_num)/sizeof(struct log_name_and_num); i++) {
182                 if (!strcmp_P(s, log_name_and_num[i].name)) {
183                         return log_name_and_num[i].num;
184                 }
185         }
186         return 0;
187 }
188
189 const char *
190 log_num2name(uint8_t num)
191 {
192         uint8_t i;
193
194         for (i=0; i<sizeof(log_name_and_num)/sizeof(struct log_name_and_num); i++) {
195                 if (num ==  log_name_and_num[i].num) {
196                         return log_name_and_num[i].name;
197                 }
198         }
199         return NULL;
200 }
201
202 /* function called when cmd_log is parsed successfully */
203 static void cmd_log_do_show(void)
204 {
205         uint8_t i, empty=1;
206         const char *name;
207
208         printf_P(PSTR("log level is %d\r\n"), imuboard.log_level);
209         for (i=0; i<NB_LOGS; i++) {
210                 name = log_num2name(imuboard.logs[i]);
211                 if (name) {
212 #ifdef HOST_VERSION
213                         printf_P(PSTR("log type %s is on\r\n"), name);
214 #else
215                         printf_P(PSTR("log type %S is on\r\n"), name);
216 #endif
217                         empty = 0;
218                 }
219         }
220         if (empty)
221                 printf_P(PSTR("no log configured\r\n"));
222 }
223
224 /* function called when cmd_log is parsed successfully */
225 static void cmd_log_parsed(void * parsed_result, void *data)
226 {
227         struct cmd_log_result *res = (struct cmd_log_result *) parsed_result;
228
229         (void)data;
230         if (!strcmp_P(res->arg1, PSTR("level"))) {
231                 imuboard.log_level = res->arg2;
232         }
233
234         /* else it is a show */
235         cmd_log_do_show();
236 }
237
238 const char PROGMEM str_log_arg0[] = "log";
239 const parse_token_string_t PROGMEM cmd_log_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_log_result, arg0, str_log_arg0);
240 const char PROGMEM str_log_arg1[] = "level";
241 const parse_token_string_t PROGMEM cmd_log_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_log_result, arg1, str_log_arg1);
242 const parse_token_num_t PROGMEM cmd_log_arg2 = TOKEN_NUM_INITIALIZER(struct cmd_log_result, arg2, INT8);
243
244 const char PROGMEM help_log[] = "Set log options: level (0 -> 5)";
245 const parse_inst_t PROGMEM cmd_log = {
246         .f = cmd_log_parsed,  /* function to call */
247         .data = NULL,      /* 2nd arg of func */
248         .help_str = help_log,
249         .tokens = {        /* token list, NULL terminated */
250                 (PGM_P)&cmd_log_arg0,
251                 (PGM_P)&cmd_log_arg1,
252                 (PGM_P)&cmd_log_arg2,
253                 NULL,
254         },
255 };
256
257 const char PROGMEM str_log_arg1_show[] = "show";
258 const parse_token_string_t PROGMEM cmd_log_arg1_show = TOKEN_STRING_INITIALIZER(struct cmd_log_result, arg1, str_log_arg1_show);
259
260 const char PROGMEM help_log_show[] = "Show configured logs";
261 const parse_inst_t PROGMEM cmd_log_show = {
262         .f = cmd_log_parsed,  /* function to call */
263         .data = NULL,      /* 2nd arg of func */
264         .help_str = help_log_show,
265         .tokens = {        /* token list, NULL terminated */
266                 (PGM_P)&cmd_log_arg0,
267                 (PGM_P)&cmd_log_arg1_show,
268                 NULL,
269         },
270 };
271
272 /* this structure is filled when cmd_log is parsed successfully */
273 struct cmd_log_type_result {
274         fixed_string_t arg0;
275         fixed_string_t arg1;
276         fixed_string_t arg2;
277         fixed_string_t arg3;
278 };
279
280 /* function called when cmd_log is parsed successfully */
281 static void cmd_log_type_parsed(void * parsed_result, void *data)
282 {
283         struct cmd_log_type_result *res = (struct cmd_log_type_result *) parsed_result;
284         uint8_t lognum;
285         uint8_t i;
286
287         (void)data;
288
289         lognum = log_name2num(res->arg2);
290         if (lognum == 0) {
291                 printf_P(PSTR("Cannot find log num\r\n"));
292                 return;
293         }
294
295         if (!strcmp_P(res->arg3, PSTR("on"))) {
296                 for (i=0; i<NB_LOGS; i++) {
297                         if (imuboard.logs[i] == lognum) {
298                                 printf_P(PSTR("Already on\r\n"));
299                                 return;
300                         }
301                 }
302                 for (i=0; i<NB_LOGS; i++) {
303                         if (imuboard.logs[i] == 0) {
304                                 imuboard.logs[i] = lognum;
305                                 break;
306                         }
307                 }
308                 if (i==NB_LOGS) {
309                         printf_P(PSTR("no more room\r\n"));
310                 }
311         }
312         else if (!strcmp_P(res->arg3, PSTR("off"))) {
313                 for (i=0; i<NB_LOGS; i++) {
314                         if (imuboard.logs[i] == lognum) {
315                                 imuboard.logs[i] = 0;
316                                 break;
317                         }
318                 }
319                 if (i==NB_LOGS) {
320                         printf_P(PSTR("already off\r\n"));
321                 }
322         }
323         cmd_log_do_show();
324 }
325
326 const char PROGMEM str_log_arg1_type[] = "type";
327 const parse_token_string_t PROGMEM cmd_log_arg1_type = TOKEN_STRING_INITIALIZER(struct cmd_log_type_result, arg1, str_log_arg1_type);
328 /* keep it sync with log_name_and_num above */
329 const char PROGMEM str_log_arg2_type[] =
330         "uart#i2c#i2cproto#default#xbee#rc_proto";
331 const parse_token_string_t PROGMEM cmd_log_arg2_type = TOKEN_STRING_INITIALIZER(struct cmd_log_type_result, arg2, str_log_arg2_type);
332 const char PROGMEM str_log_arg3[] = "on#off";
333 const parse_token_string_t PROGMEM cmd_log_arg3 = TOKEN_STRING_INITIALIZER(struct cmd_log_type_result, arg3, str_log_arg3);
334
335 const char PROGMEM help_log_type[] = "Set log type";
336 const parse_inst_t PROGMEM cmd_log_type = {
337         .f = cmd_log_type_parsed,  /* function to call */
338         .data = NULL,      /* 2nd arg of func */
339         .help_str = help_log_type,
340         .tokens = {        /* token list, NULL terminated */
341                 (PGM_P)&cmd_log_arg0,
342                 (PGM_P)&cmd_log_arg1_type,
343                 (PGM_P)&cmd_log_arg2_type,
344                 (PGM_P)&cmd_log_arg3,
345                 NULL,
346         },
347 };
348
349
350 /**********************************************************/
351 /* Stack_Space */
352
353 /* this structure is filled when cmd_stack_space is parsed successfully */
354 struct cmd_stack_space_result {
355         fixed_string_t arg0;
356 };
357
358 /* function called when cmd_stack_space is parsed successfully */
359 static void cmd_stack_space_parsed(void *parsed_result, void *data)
360 {
361         (void)parsed_result;
362         (void)data;
363 #ifdef HOST_VERSION
364         printf("not implemented\n");
365 #else
366         printf("res stack: %d\r\n", min_stack_space_available());
367 #endif
368 }
369
370 const char PROGMEM str_stack_space_arg0[] = "stack_space";
371 const parse_token_string_t PROGMEM cmd_stack_space_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_stack_space_result, arg0, str_stack_space_arg0);
372
373 const char PROGMEM help_stack_space[] = "Display remaining stack space";
374 const parse_inst_t PROGMEM cmd_stack_space = {
375         .f = cmd_stack_space_parsed,  /* function to call */
376         .data = NULL,      /* 2nd arg of func */
377         .help_str = help_stack_space,
378         .tokens = {        /* token list, NULL terminated */
379                 (PGM_P)&cmd_stack_space_arg0,
380                 NULL,
381         },
382 };