altos/test: Adjust CRC error rate after FEC fix
[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 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 static char **
59 tok(char *line) {
60         char    **strs = malloc (sizeof (char *)), *str;
61         int     n = 0;
62
63         while ((str = strtok(line, " \t"))) {
64                 line = NULL;
65                 strs = realloc(strs, (n + 2) * sizeof (char *));
66                 strs[n] = strdup(str);
67                 n++;
68         }
69         strs[n] = '\0';
70         return strs;
71 }
72
73 static void
74 free_strs(char **strs) {
75         char    *str;
76         int     i;
77
78         for (i = 0; (str = strs[i]) != NULL; i++)
79                 free(str);
80         free(strs);
81 }
82
83 struct baro {
84         struct baro     *next;
85         char            **strs;
86 };
87
88 static struct baro *
89 baro(struct cc_usb *usb)
90 {
91         struct baro     *head = NULL, **tail = &head;
92         cc_usb_printf(usb, "B\n");
93         for (;;) {
94                 char    line[512];
95                 struct baro     *b;
96
97                 cc_usb_getline(usb, line, sizeof (line));
98                 b = malloc (sizeof (struct baro));
99                 b->strs = tok(line);
100                 b->next = NULL;
101                 *tail = b;
102                 tail = &b->next;
103                 if (strstr(line, "Altitude:"))
104                         break;
105         }
106         return head;
107 }
108
109 static void
110 free_baro(struct baro *b) {
111         struct baro *n;
112
113         while (b) {
114                 n = b->next;
115                 free_strs(b->strs);
116                 free(b);
117                 b = n;
118         }
119 }
120
121 static char **
122 find_baro(struct baro *b, char *word0) {
123         for (;b; b = b->next)
124                 if (b->strs[0] && !strcmp(b->strs[0], word0))
125                         return b->strs;
126         return NULL;
127 }
128
129 static int
130 do_baro(struct cc_usb *usb) {
131         struct baro *b = baro(usb);
132         char **temp = find_baro(b, "Temperature:");
133         char **alt = find_baro(b, "Altitude:");
134
135         if (!temp || !alt) {
136                 printf("no response\n");
137                 free_baro(b);
138                 return 0;
139         }
140
141         double temperature = strtod(temp[2], NULL) / 100.0;
142         double altitude = strtod(alt[1], NULL);
143
144         if (altitude < -100 || 3000 < altitude) {
145                 printf ("weird altitude %f\n", altitude);
146                 free_baro(b);
147                 return 0;
148         }
149
150         if (temperature < 20 || 40 < temperature) {
151                 printf ("weird temperature %f\n", temperature);
152                 free_baro(b);
153                 return 0;
154         }
155
156         printf ("altitude %f temperature %f\n", altitude, temperature);
157
158         return 1;
159 }
160
161 int
162 main (int argc, char **argv)
163 {
164         char                    *device = NULL;
165         int                     c;
166         struct cc_usb           *cc = NULL;
167         char                    *tty = NULL;
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         if (!tty)
194                 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
195         if (!tty)
196                 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
197         if (!tty)
198                 tty = getenv("ALTOS_TTY");
199         if (!tty)
200                 tty="/dev/ttyACM0";
201
202         cc = cc_usb_open(tty);
203
204         if (!cc)
205                 exit(1);
206
207         if (!do_baro(cc))
208                 ret = 1;
209         done(cc, ret);
210 }