remove unused vector.[ch]
[protos/imu.git] / commands.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.c,v 1.9 2009-11-08 17:24:33 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org>
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <aversive.h>
28 #include <aversive/pgmspace.h>
29 #include <aversive/queue.h>
30 #include <aversive/endian.h>
31 #include <aversive/error.h>
32 #include <aversive/wait.h>
33 #include <parse.h>
34 #include <rdline.h>
35 #include <parse_string.h>
36 #include <parse_num.h>
37 #include <uart.h>
38 #include <xbee.h>
39 #include <callout.h>
40
41 #include "main.h"
42 #include "cmdline.h"
43 #include "eeprom_config.h"
44
45 /* commands_gen.c */
46 extern const parse_inst_t PROGMEM cmd_reset;
47 extern const parse_inst_t PROGMEM cmd_bootloader;
48 extern const parse_inst_t PROGMEM cmd_log;
49 extern const parse_inst_t PROGMEM cmd_log_show;
50 extern const parse_inst_t PROGMEM cmd_log_type;
51 extern const parse_inst_t PROGMEM cmd_stack_space;
52 extern const parse_inst_t PROGMEM cmd_callout;
53
54 /**********************************************************/
55
56 /* this structure is filled when cmd_test_eeprom_config is parsed successfully */
57 struct cmd_test_eeprom_config_result {
58         fixed_string_t arg0;
59 };
60
61 static void cmd_test_eeprom_config_parsed(void *parsed_result, void *data)
62 {
63         (void)parsed_result;
64         (void)data;
65
66         eeprom_dump_cmds();
67         eeprom_append_cmd("salut1\n");
68         eeprom_dump_cmds();
69         eeprom_append_cmd("salut2\n");
70         eeprom_append_cmd("salut3\n");
71         eeprom_append_cmd("salut4\n");
72         eeprom_dump_cmds();
73         eeprom_insert_cmd_before("coin\n", 0);
74         eeprom_insert_cmd_before("coin2\n", 2);
75         eeprom_dump_cmds();
76         eeprom_delete_cmd(2);
77         eeprom_delete_cmd(0);
78         eeprom_dump_cmds();
79 }
80
81 const char PROGMEM str_test_eeprom_config_arg0[] = "test_eeprom_config";
82 const parse_token_string_t PROGMEM cmd_test_eeprom_config_arg0 =
83         TOKEN_STRING_INITIALIZER(struct cmd_test_eeprom_config_result, arg0,
84                                  str_test_eeprom_config_arg0);
85
86 const char PROGMEM help_test_eeprom_config[] = "Test the eeprom configuration";
87 const parse_inst_t PROGMEM cmd_test_eeprom_config = {
88         .f = cmd_test_eeprom_config_parsed,  /* function to call */
89         .data = NULL,      /* 2nd arg of func */
90         .help_str = help_test_eeprom_config,
91         .tokens = {        /* token list, NULL terminated */
92                 (PGM_P)&cmd_test_eeprom_config_arg0,
93                 NULL,
94         },
95 };
96
97 /* ************* */
98
99 struct cmd_eeprom_del_result {
100         fixed_string_t cmd;
101         fixed_string_t action;
102         uint8_t n;
103 };
104
105 static void cmd_eeprom_del_parsed(void *parsed_result,
106                                 void *data)
107 {
108         struct cmd_eeprom_del_result *res = parsed_result;
109
110         (void)data;
111         if (eeprom_delete_cmd(res->n) < 0)
112                 printf_P(PSTR("cannot delete command\n"));
113         eeprom_dump_cmds();
114 }
115
116 const char PROGMEM str_eeprom_del_eeprom[] = "eeprom";
117 const parse_token_string_t PROGMEM cmd_eeprom_del_cmd =
118         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_del_result, cmd,
119                                  str_eeprom_del_eeprom);
120 const char PROGMEM str_eeprom_del_del[] = "del";
121 const parse_token_string_t PROGMEM cmd_eeprom_del_action =
122         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_del_result, action,
123                                  str_eeprom_del_del);
124 const parse_token_num_t PROGMEM cmd_eeprom_del_num =
125         TOKEN_NUM_INITIALIZER(struct cmd_eeprom_del_result, n,
126                               UINT8);
127
128 const char PROGMEM help_eeprom_del[] = "delete an eeprom init command";
129 const parse_inst_t PROGMEM cmd_eeprom_del = {
130         .f = cmd_eeprom_del_parsed,  /* function to call */
131         .data = NULL,      /* 2nd arg of func */
132         .help_str = help_eeprom_del,
133         .tokens = {        /* token list, NULL terminated */
134                 (PGM_P)&cmd_eeprom_del_cmd,
135                 (PGM_P)&cmd_eeprom_del_action,
136                 (PGM_P)&cmd_eeprom_del_num,
137                 NULL,
138         },
139 };
140
141 /* ************* */
142
143 struct cmd_eeprom_add_result {
144         fixed_string_t cmd;
145         fixed_string_t action;
146         uint8_t n;
147 };
148
149 static void cmd_eeprom_add_parsed(void *parsed_result,
150                                  void *data)
151 {
152         struct cmd_eeprom_add_result *res = parsed_result;
153         struct rdline rdl;
154         const char *buffer;
155         int8_t ret;
156         int16_t c;
157
158         rdline_init(&rdl, cmdline_write_char, NULL, NULL);
159         rdline_newline(&rdl, "> ");
160
161         while (1) {
162                 c = cmdline_dev_recv(NULL);
163                 if (c < 0)
164                         continue;
165
166                 ret = rdline_char_in(&rdl, c);
167                 if (ret == -2) {
168                         printf_P(PSTR("abort\n"));
169                         return;
170                 }
171                 if (ret == 1)
172                         break;
173         }
174
175         buffer = rdline_get_buffer(&rdl);
176         if (data == NULL)
177                 eeprom_insert_cmd_before(buffer, res->n);
178         else
179                 eeprom_append_cmd(buffer);
180         eeprom_dump_cmds();
181 }
182
183 const char PROGMEM str_eeprom_add_eeprom[] = "eeprom";
184 const parse_token_string_t PROGMEM cmd_eeprom_add_cmd =
185         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_add_result, cmd,
186                                  str_eeprom_add_eeprom);
187 const char PROGMEM str_eeprom_add_add[] = "add";
188 const parse_token_string_t PROGMEM cmd_eeprom_add_action =
189         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_add_result, action,
190                                  str_eeprom_add_add);
191 const parse_token_num_t PROGMEM cmd_eeprom_add_num =
192         TOKEN_NUM_INITIALIZER(struct cmd_eeprom_add_result, n,
193                               UINT8);
194
195 const char PROGMEM help_eeprom_add[] = "insert an eeprom init command";
196 const parse_inst_t PROGMEM cmd_eeprom_add = {
197         .f = cmd_eeprom_add_parsed,  /* function to call */
198         .data = NULL,      /* 2nd arg of func */
199         .help_str = help_eeprom_add,
200         .tokens = {        /* token list, NULL terminated */
201                 (PGM_P)&cmd_eeprom_add_cmd,
202                 (PGM_P)&cmd_eeprom_add_action,
203                 (PGM_P)&cmd_eeprom_add_num,
204                 NULL,
205         },
206 };
207
208 const char PROGMEM help_eeprom_add2[] = "append an eeprom init command";
209 const parse_inst_t PROGMEM cmd_eeprom_add2 = {
210         .f = cmd_eeprom_add_parsed,  /* function to call */
211         .data = (void *)1,      /* 2nd arg of func */
212         .help_str = help_eeprom_add2,
213         .tokens = {        /* token list, NULL terminated */
214                 (PGM_P)&cmd_eeprom_add_cmd,
215                 (PGM_P)&cmd_eeprom_add_action,
216                 NULL,
217         },
218 };
219
220 /* ************* */
221
222 struct cmd_eeprom_list_result {
223         fixed_string_t cmd;
224         fixed_string_t action;
225 };
226
227 static void cmd_eeprom_list_parsed(void *parsed_result,
228                                 void *data)
229 {
230         (void)parsed_result;
231         (void)data;
232         eeprom_dump_cmds();
233 }
234
235 const char PROGMEM str_eeprom_list_eeprom[] = "eeprom";
236 const parse_token_string_t PROGMEM cmd_eeprom_list_cmd =
237         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_list_result, cmd,
238                                  str_eeprom_list_eeprom);
239 const char PROGMEM str_eeprom_list_list[] = "list";
240 const parse_token_string_t PROGMEM cmd_eeprom_list_action =
241         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_list_result, action,
242                                  str_eeprom_list_list);
243
244 const char PROGMEM help_eeprom_list[] = "list all eeprom init commands";
245 const parse_inst_t PROGMEM cmd_eeprom_list = {
246         .f = cmd_eeprom_list_parsed,  /* function to call */
247         .data = NULL,      /* 2nd arg of func */
248         .help_str = help_eeprom_list,
249         .tokens = {        /* token list, NULL terminated */
250                 (PGM_P)&cmd_eeprom_list_cmd,
251                 (PGM_P)&cmd_eeprom_list_action,
252                 NULL,
253         },
254 };
255
256
257 /* ************* */
258
259 /* in progmem */
260 const parse_ctx_t PROGMEM main_ctx[] = {
261
262         /* commands_gen.c */
263         &cmd_reset,
264         &cmd_bootloader,
265         &cmd_log,
266         &cmd_log_show,
267         &cmd_log_type,
268         &cmd_stack_space,
269         &cmd_callout,
270         &cmd_test_eeprom_config,
271         &cmd_eeprom_del,
272         &cmd_eeprom_add,
273         &cmd_eeprom_add2,
274         &cmd_eeprom_list,
275         NULL,
276 };