36c805c201bdb34181bd719b7c85b828eeccce00
[fw/altos] / ao-tools / ao-test-baro / ao-test-baro.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 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 static int
59 ends_with(char *whole, char *suffix)
60 {
61         int whole_len = strlen(whole);
62         int suffix_len = strlen(suffix);
63
64         if (suffix_len > whole_len)
65                 return 0;
66         return strcmp(whole + whole_len - suffix_len, suffix) == 0;
67 }
68
69 static int
70 starts_with(char *whole, char *prefix)
71 {
72         int whole_len = strlen(whole);
73         int prefix_len = strlen(prefix);
74
75         if (prefix_len > whole_len)
76                 return 0;
77         return strncmp(whole, prefix, prefix_len) == 0;
78 }
79
80 static char **
81 tok(char *line) {
82         char    **strs = malloc (sizeof (char *)), *str;
83         int     n = 0;
84
85         while ((str = strtok(line, " \t"))) {
86                 line = NULL;
87                 strs = realloc(strs, (n + 2) * sizeof (char *));
88                 strs[n] = strdup(str);
89                 n++;
90         }
91         strs[n] = '\0';
92         return strs;
93 }
94
95 static void
96 free_strs(char **strs) {
97         char    *str;
98         int     i;
99
100         for (i = 0; (str = strs[i]) != NULL; i++)
101                 free(str);
102         free(strs);
103 }
104
105 struct baro {
106         struct baro     *next;
107         char            **strs;
108 };
109
110 static struct baro *
111 baro(struct cc_usb *usb)
112 {
113         struct baro     *head = NULL, **tail = &head;
114         cc_usb_printf(usb, "B\n");
115         for (;;) {
116                 char    line[512];
117                 struct baro     *b;
118
119                 cc_usb_getline(usb, line, sizeof (line));
120                 b = malloc (sizeof (struct baro));
121                 b->strs = tok(line);
122                 b->next = NULL;
123                 *tail = b;
124                 tail = &b->next;
125                 if (strstr(line, "Altitude:"))
126                         break;
127         }
128         return head;
129 }
130
131 static void
132 free_baro(struct baro *b) {
133         struct baro *n;
134
135         while (b) {
136                 n = b->next;
137                 free_strs(b->strs);
138                 free(b);
139                 b = n;
140         }
141 }
142
143 char **
144 find_baro(struct baro *b, char *word0) {
145         int i;
146         for (;b; b = b->next)
147                 if (b->strs[0] && !strcmp(b->strs[0], word0))
148                         return b->strs;
149         return NULL;
150 }
151
152 int
153 do_baro(struct cc_usb *usb) {
154         struct baro *b = baro(usb);
155         char **temp = find_baro(b, "Temperature:");
156         char **alt = find_baro(b, "Altitude:");
157
158         if (!temp || !alt) {
159                 printf("no response\n");
160                 free_baro(b);
161                 return 0;
162         }
163
164         double temperature = strtod(temp[2], NULL) / 100.0;
165         double altitude = strtod(alt[1], NULL);
166
167         if (altitude < -100 || 3000 < altitude) {
168                 printf ("weird altitude %f\n", altitude);
169                 free_baro(b);
170                 return 0;
171         }
172
173         if (temperature < 20 || 40 < temperature) {
174                 printf ("weird temperature %f\n", temperature);
175                 free_baro(b);
176                 return 0;
177         }
178
179         printf ("altitude %f temperature %f\n", altitude, temperature);
180
181         return 1;
182 }
183
184 int
185 main (int argc, char **argv)
186 {
187         char                    *device = NULL;
188         char                    *filename;
189         Elf                     *e;
190         unsigned int            s;
191         int                     i;
192         int                     c;
193         int                     tries;
194         struct cc_usb           *cc = NULL;
195         char                    *tty = NULL;
196         int                     success;
197         int                     verbose = 0;
198         int                     ret = 0;
199
200         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
201                 switch (c) {
202                 case 'T':
203                         tty = optarg;
204                         break;
205                 case 'D':
206                         device = optarg;
207                         break;
208                 case 'v':
209                         verbose++;
210                         break;
211                 default:
212                         usage(argv[0]);
213                         break;
214                 }
215         }
216
217         ao_verbose = verbose;
218
219         if (verbose > 1)
220                 ccdbg_add_debug(CC_DEBUG_BITBANG);
221
222         if (!tty)
223                 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
224         if (!tty)
225                 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
226         if (!tty)
227                 tty = getenv("ALTOS_TTY");
228         if (!tty)
229                 tty="/dev/ttyACM0";
230
231         cc = cc_usb_open(tty);
232
233         if (!cc)
234                 exit(1);
235
236         if (!do_baro(cc))
237                 ret = 1;
238         done(cc, ret);
239 }