Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / ao-tools / ao-cal-accel / ao-cal-accel.c
1 /*
2  * Copyright © 2014 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 <err.h>
19 #include <fcntl.h>
20 #include <gelf.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <sysexits.h>
25 #include <unistd.h>
26 #include <getopt.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <termios.h>
30 #include "ao-elf.h"
31 #include "ccdbg.h"
32 #include "cc-usb.h"
33 #include "cc.h"
34 #include "ao-verbose.h"
35
36 static const struct option options[] = {
37         { .name = "tty", .has_arg = 1, .val = 'T' },
38         { .name = "device", .has_arg = 1, .val = 'D' },
39         { .name = "raw", .has_arg = 0, .val = 'r' },
40         { .name = "verbose", .has_arg = 1, .val = 'v' },
41         { 0, 0, 0, 0},
42 };
43
44 static void usage(char *program)
45 {
46         fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>]\n", program);
47         exit(1);
48 }
49
50 void
51 done(struct cc_usb *cc, int code)
52 {
53         cc_usb_close(cc);
54         exit (code);
55 }
56
57 static int
58 ends_with(char *whole, char *suffix)
59 {
60         int whole_len = strlen(whole);
61         int suffix_len = strlen(suffix);
62
63         if (suffix_len > whole_len)
64                 return 0;
65         return strcmp(whole + whole_len - suffix_len, suffix) == 0;
66 }
67
68 static int
69 starts_with(char *whole, char *prefix)
70 {
71         int whole_len = strlen(whole);
72         int prefix_len = strlen(prefix);
73
74         if (prefix_len > whole_len)
75                 return 0;
76         return strncmp(whole, prefix, prefix_len) == 0;
77 }
78
79 static char **
80 tok(char *line) {
81         char    **strs = malloc (sizeof (char *)), *str;
82         int     n = 0;
83
84         while ((str = strtok(line, " \t"))) {
85                 line = NULL;
86                 strs = realloc(strs, (n + 2) * sizeof (char *));
87                 strs[n] = strdup(str);
88                 n++;
89         }
90         strs[n] = '\0';
91         return strs;
92 }
93
94 static void
95 free_strs(char **strs) {
96         char    *str;
97         int     i;
98
99         for (i = 0; (str = strs[i]) != NULL; i++)
100                 free(str);
101         free(strs);
102 }
103
104 struct flash {
105         struct flash    *next;
106         char            line[512];
107         char            **strs;
108 };
109
110 static struct flash *
111 flash(struct cc_usb *usb)
112 {
113         struct flash    *head = NULL, **tail = &head;
114         cc_usb_printf(usb, "c s\nv\n");
115         for (;;) {
116                 char    line[512];
117                 struct flash    *b;
118
119                 cc_usb_getline(usb, line, sizeof (line));
120                 b = malloc (sizeof (struct flash));
121                 strcpy(b->line, line);
122                 b->strs = tok(line);
123                 b->next = NULL;
124                 *tail = b;
125                 tail = &b->next;
126                 if (strstr(line, "software-version"))
127                         break;
128         }
129         return head;
130 }
131
132 static void
133 free_flash(struct flash *b) {
134         struct flash *n;
135
136         while (b) {
137                 n = b->next;
138                 free_strs(b->strs);
139                 free(b);
140                 b = n;
141         }
142 }
143
144 char **
145 find_flash(struct flash *b, char *word0) {
146         int i;
147         for (;b; b = b->next) {
148                 if (strstr(b->line, word0))
149                         return b->strs;
150         }
151         return NULL;
152 }
153
154 void
155 await_key(void)
156 {
157         struct termios  termios, termios_save;
158         char    buf[512];
159
160         tcgetattr(0, &termios);
161         termios_save = termios;
162         cfmakeraw(&termios);
163         tcsetattr(0, TCSAFLUSH, &termios);
164         read(0, buf, sizeof (buf));
165         tcsetattr(0, TCSAFLUSH, &termios_save);
166 }
167
168 int
169 do_cal(struct cc_usb *usb) {
170         struct flash    *b;
171         char    **accel;
172         char    line[1024];
173         int     l;
174         int     running = 0;
175         int     worked = 1;
176
177         cc_usb_printf(usb, "E 1\nc a 0\n");
178
179         for (;;) {
180                 int     c = cc_usb_getchar_timeout(usb, 20*1000);
181
182                 if (c == '\n')
183                         l = 0;
184                 else if (l < sizeof (line) - 1)
185                         line[l++] = c;
186                 line[l] = '\0';
187                 putchar(c); fflush(stdout);
188                 if (strstr(line, "press a key...")) {
189                         await_key();
190                         cc_usb_printf(usb, " ");
191                         l = 0;
192                         running = 1;
193                 }
194                 else if (strstr(line, "Invalid"))
195                         worked = 0;
196                 if (running && strstr(line, ">")) {
197                         printf("\n");
198                         break;
199                 }
200         }
201         cc_usb_printf(usb, "E 0\n");
202
203         if (!worked) {
204                 printf("Calibration failed\n");
205                 return 0;
206         }
207
208         b = flash(usb);
209
210         accel = find_flash(b, "Accel cal");
211         if (!accel) {
212                 printf("no response\n");
213                 return 0;
214         }
215
216         printf ("Accel cal +1g: %s -1g: %s\n",
217                 accel[3], accel[5]);
218
219         printf ("Saving..."); fflush(stdout);
220         cc_usb_printf (usb, "c w\n");
221         cc_usb_sync(usb);
222         b = flash(usb);
223         printf ("done\n");
224
225         return worked;
226 }
227
228 int
229 main (int argc, char **argv)
230 {
231         char                    *device = NULL;
232         char                    *filename;
233         Elf                     *e;
234         unsigned int            s;
235         int                     i;
236         int                     c;
237         int                     tries;
238         struct cc_usb           *cc = NULL;
239         char                    *tty = NULL;
240         int                     success;
241         int                     verbose = 0;
242         int                     ret = 0;
243         int                     expected_size;
244
245         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
246                 switch (c) {
247                 case 'T':
248                         tty = optarg;
249                         break;
250                 case 'D':
251                         device = optarg;
252                         break;
253                 case 'v':
254                         verbose++;
255                         break;
256                 default:
257                         usage(argv[0]);
258                         break;
259                 }
260         }
261
262         ao_verbose = verbose;
263
264         if (verbose > 1)
265                 ccdbg_add_debug(CC_DEBUG_BITBANG);
266
267         if (!tty)
268                 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
269         if (!tty)
270                 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
271         if (!tty)
272                 tty = getenv("ALTOS_TTY");
273         if (!tty)
274                 tty="/dev/ttyACM0";
275
276         cc = cc_usb_open(tty);
277
278         if (!cc)
279                 exit(1);
280
281         if (!do_cal(cc))
282                 ret = 1;
283         done(cc, ret);
284 }