ao-tools: Fix warnings in ao-tools
[fw/altos] / ao-tools / ao-test-gps / ao-test-gps.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 gps {
84         struct gps      *next;
85         char            **strs;
86 };
87
88 static struct gps *
89 gps(struct cc_usb *usb)
90 {
91         struct gps      *head = NULL, **tail = &head;
92         cc_usb_printf(usb, "g\nv\n");
93         for (;;) {
94                 char    line[512];
95                 struct gps      *b;
96
97                 cc_usb_getline(usb, line, sizeof (line));
98                 b = malloc (sizeof (struct gps));
99                 b->strs = tok(line);
100                 b->next = NULL;
101                 *tail = b;
102                 tail = &b->next;
103                 if (strstr(line, "software-version"))
104                         break;
105         }
106         return head;
107 }
108
109 static void
110 free_gps(struct gps *b) {
111         struct gps *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_gps(struct gps *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_gps(struct cc_usb *usb) {
131         int     count = 0;
132
133         for (;;) {
134                 struct gps *b = gps(usb);
135                 char **flags = find_gps(b, "Flags:");
136                 char **sats = find_gps(b, "Sats:");
137                 int actual_flags = strtol(flags[1], NULL, 0);
138                 int actual_sats = strtol(sats[1], NULL, 0);
139
140                 if (actual_flags & (1 << 4)) {
141                         printf("\n");
142                         printf("Flags: %s (0x%x)\n", flags[1], actual_flags);
143                         printf("Sats: %s (%d)\n", sats[1], actual_sats);
144                         break;
145                 }
146
147                 free_gps(b);
148                 printf("%d", actual_sats);
149                 ++count;
150                 if (count >= 50) {
151                         putchar('\n');
152                         count = 0;
153                 }
154                 else if (count % 10 == 0)
155                         putchar(' ');
156                 fflush(stdout);
157                 sleep(1);
158         }
159         return 1;
160 }
161
162 int
163 main (int argc, char **argv)
164 {
165         char                    *device = NULL;
166         int                     c;
167         struct cc_usb           *cc = NULL;
168         char                    *tty = NULL;
169         int                     verbose = 0;
170         int                     ret = 0;
171
172         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
173                 switch (c) {
174                 case 'T':
175                         tty = optarg;
176                         break;
177                 case 'D':
178                         device = optarg;
179                         break;
180                 case 'v':
181                         verbose++;
182                         break;
183                 default:
184                         usage(argv[0]);
185                         break;
186                 }
187         }
188
189         ao_verbose = verbose;
190
191         if (verbose > 1)
192                 ccdbg_add_debug(CC_DEBUG_BITBANG);
193
194         if (!tty)
195                 tty = cc_usbdevs_find_by_arg(device, "TeleGPS");
196         if (!tty)
197                 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
198         if (!tty)
199                 tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
200         if (!tty)
201                 tty = getenv("ALTOS_TTY");
202         if (!tty)
203                 tty="/dev/ttyACM0";
204
205         cc = cc_usb_open(tty);
206
207         if (!cc)
208                 exit(1);
209
210         if (!do_gps(cc))
211                 ret = 1;
212         done(cc, ret);
213 }