ao-tools: Fix warnings in ao-tools
[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; 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, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <err.h>
20 #include <fcntl.h>
21 #include <gelf.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <sysexits.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <string.h>
29 #include <stdbool.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>] main|drogue\n", program);
47         exit(1);
48 }
49
50 static void
51 done(struct cc_usb *cc, int code)
52 {
53 /*      cc_usb_printf(cc, "a\n"); */
54         cc_usb_close(cc);
55         exit (code);
56 }
57
58 struct igniter {
59         struct igniter  *next;
60         char            name[512];
61         char            status[512];
62 };
63
64 static struct igniter *
65 igniters(struct cc_usb *usb)
66 {
67         struct igniter  *head = NULL, **tail = &head;
68         cc_usb_printf(usb, "t\nv\n");
69         for (;;) {
70                 char    line[512];
71                 char    name[512];
72                 char    status[512];
73
74                 cc_usb_getline(usb, line, sizeof (line));
75                 if (strstr(line, "software-version"))
76                         break;
77                 if (sscanf(line, "Igniter: %s Status: %s", name, status) == 2) {
78                         struct igniter  *i = malloc (sizeof (struct igniter));
79                         strcpy(i->name, name);
80                         strcpy(i->status, status);
81                         i->next = NULL;
82                         *tail = i;
83                         tail = &i->next;
84                 }
85         }
86         return head;
87 }
88
89 static void
90 free_igniters(struct igniter *i) {
91         struct igniter *n;
92
93         while (i) {
94                 n = i->next;
95                 free(i);
96                 i = n;
97         }
98 }
99
100 static struct igniter *
101 find_igniter(struct igniter *i, char *name)
102 {
103         for (; i; i = i->next)
104                 if (strcmp(i->name, name) == 0)
105                         return i;
106         return NULL;
107 }
108
109 static int
110 do_igniter(struct cc_usb *usb, char *name)
111 {
112         struct igniter  *all = igniters(usb);
113         struct igniter  *this = find_igniter(all, name);
114         if (!this) {
115                 struct igniter  *i;
116                 printf("no igniter %s found in", name);
117                 for (i = all; i; i = i->next)
118                         printf(" %s", i->name);
119                 printf("\n");
120                 free_igniters(all);
121                 return 0;
122         }
123         if (strcmp(this->status, "ready") != 0) {
124                 printf("igniter %s status is %s\n", this->name, this->status);
125                 free_igniters(all);
126                 return 0;
127         }
128         cc_usb_printf(usb, "i DoIt %s\n", this->name);
129         cc_usb_sync(usb);
130         free_igniters(all);
131         usleep(200000);
132         return 1;
133 }
134
135 int
136 main (int argc, char **argv)
137 {
138         char                    *device = NULL;
139         int                     i;
140         int                     c;
141         struct cc_usb           *cc = NULL;
142         char                    *tty = NULL;
143         int                     verbose = 0;
144         int                     ret = 0;
145
146         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
147                 switch (c) {
148                 case 'T':
149                         tty = optarg;
150                         break;
151                 case 'D':
152                         device = optarg;
153                         break;
154                 case 'v':
155                         verbose++;
156                         break;
157                 default:
158                         usage(argv[0]);
159                         break;
160                 }
161         }
162
163         ao_verbose = verbose;
164
165         if (verbose > 1)
166                 ccdbg_add_debug(CC_DEBUG_BITBANG);
167
168         if (!tty)
169                 tty = cc_usbdevs_find_by_arg(device, "TeleMega-v1.0");
170         if (!tty)
171                 tty = cc_usbdevs_find_by_arg(device, "TeleMetrum-v2.0");
172         if (!tty)
173                 tty = cc_usbdevs_find_by_arg(device, "TeleMini-v2.0");
174         if (!tty)
175                 tty = cc_usbdevs_find_by_arg(device, "EasyMega-v1.0");
176         if (!tty)
177                 tty = cc_usbdevs_find_by_arg(device, "EasyMetrum-v1.0");
178         if (!tty)
179                 tty = cc_usbdevs_find_by_arg(device, "EasyMini-v1.0");
180         if (!tty)
181                 tty = getenv("ALTOS_TTY");
182         if (!tty)
183                 tty="/dev/ttyACM0";
184
185         cc = cc_usb_open(tty);
186
187         if (!cc)
188                 exit(1);
189
190         for (i = optind; i < argc; i++) {
191                 char    *name = argv[i];
192
193                 if (!do_igniter(cc, name))
194                         ret++;
195         }
196         done(cc, ret);
197 }