ao-tools: Add ao-test-baro, ao-test-igniter and ao-test-flash
[fw/altos] / ao-tools / ao-test-igniter / ao-test-igniter.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 "ao-elf.h"
30 #include "ccdbg.h"
31 #include "cc-usb.h"
32 #include "cc.h"
33 #include "ao-verbose.h"
34
35 static const struct option options[] = {
36         { .name = "tty", .has_arg = 1, .val = 'T' },
37         { .name = "device", .has_arg = 1, .val = 'D' },
38         { .name = "raw", .has_arg = 0, .val = 'r' },
39         { .name = "verbose", .has_arg = 1, .val = 'v' },
40         { 0, 0, 0, 0},
41 };
42
43 static void usage(char *program)
44 {
45         fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] main|drogue\n", program);
46         exit(1);
47 }
48
49 void
50 done(struct cc_usb *cc, int code)
51 {
52 /*      cc_usb_printf(cc, "a\n"); */
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 struct igniter {
80         struct igniter  *next;
81         char            name[512];
82         char            status[512];
83 };
84
85 static struct igniter *
86 igniters(struct cc_usb *usb)
87 {
88         struct igniter  *head = NULL, **tail = &head;
89         cc_usb_printf(usb, "t\nv\n");
90         for (;;) {
91                 char    line[512];
92                 char    name[512];
93                 char    status[512];
94
95                 cc_usb_getline(usb, line, sizeof (line));
96                 if (strstr(line, "software-version"))
97                         break;
98                 if (sscanf(line, "Igniter: %s Status: %s", &name, &status) == 2) {
99                         struct igniter  *i = malloc (sizeof (struct igniter));
100                         strcpy(i->name, name);
101                         strcpy(i->status, status);
102                         i->next = NULL;
103                         *tail = i;
104                         tail = &i->next;
105                 }
106         }
107         return head;
108 }
109
110 static void
111 free_igniters(struct igniter *i) {
112         struct igniter *n;
113
114         while (i) {
115                 n = i->next;
116                 free(i);
117                 i = n;
118         }
119 }
120
121 static struct igniter *
122 find_igniter(struct igniter *i, char *name)
123 {
124         for (; i; i = i->next)
125                 if (strcmp(i->name, name) == 0)
126                         return i;
127 }
128
129 static int
130 do_igniter(struct cc_usb *usb, char *name)
131 {
132         struct igniter  *all = igniters(usb);
133         struct igniter  *this = find_igniter(all, name);
134         if (!this) {
135                 struct igniter  *i;
136                 printf("no igniter %s found in");
137                 for (i = all; i; i = i->next)
138                         printf(" %s", i->name);
139                 printf("\n");
140                 free_igniters(all);
141                 return 0;
142         }
143         if (strcmp(this->status, "ready") != 0) {
144                 printf("igniter %s status is %s\n", this->name, this->status);
145                 free_igniters(all);
146                 return 0;
147         }
148         cc_usb_printf(usb, "i DoIt %s\n", this->name);
149         cc_usb_sync(usb);
150         free_igniters(all);
151         usleep(200000);
152         return 1;
153 }
154
155 int
156 main (int argc, char **argv)
157 {
158         char                    *device = NULL;
159         char                    *filename;
160         Elf                     *e;
161         unsigned int            s;
162         int                     i;
163         int                     c;
164         int                     tries;
165         struct cc_usb           *cc = NULL;
166         char                    *tty = NULL;
167         int                     success;
168         int                     verbose = 0;
169         int                     ret = 0;
170
171         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
172                 switch (c) {
173                 case 'T':
174                         tty = optarg;
175                         break;
176                 case 'D':
177                         device = optarg;
178                         break;
179                 case 'v':
180                         verbose++;
181                         break;
182                 default:
183                         usage(argv[0]);
184                         break;
185                 }
186         }
187
188         ao_verbose = verbose;
189
190         if (verbose > 1)
191                 ccdbg_add_debug(CC_DEBUG_BITBANG);
192
193         printf ("device %s\n", device);
194         if (!tty)
195                 tty = cc_usbdevs_find_by_arg(device, "TeleMega-v1.0");
196         if (!tty)
197                 tty = cc_usbdevs_find_by_arg(device, "TeleMetrum-v2.0");
198         if (!tty)
199                 tty = cc_usbdevs_find_by_arg(device, "TeleMini-v2.0");
200         if (!tty)
201                 tty = cc_usbdevs_find_by_arg(device, "EasyMega-v1.0");
202         if (!tty)
203                 tty = cc_usbdevs_find_by_arg(device, "EasyMetrum-v1.0");
204         if (!tty)
205                 tty = cc_usbdevs_find_by_arg(device, "EasyMini-v1.0");
206         if (!tty)
207                 tty = getenv("ALTOS_TTY");
208         if (!tty)
209                 tty="/dev/ttyACM0";
210
211         cc = cc_usb_open(tty);
212
213         if (!cc)
214                 exit(1);
215
216         for (i = optind; i < argc; i++) {
217                 char    *name = argv[i];
218
219                 if (!do_igniter(cc, name))
220                         ret++;
221         }
222         done(cc, ret);
223 }