Switch from GPLv2 to GPLv2+
[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 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 gps {
106         struct gps      *next;
107         char            **strs;
108 };
109
110 static struct gps *
111 gps(struct cc_usb *usb)
112 {
113         struct gps      *head = NULL, **tail = &head;
114         cc_usb_printf(usb, "g\nv\n");
115         for (;;) {
116                 char    line[512];
117                 struct gps      *b;
118
119                 cc_usb_getline(usb, line, sizeof (line));
120                 b = malloc (sizeof (struct gps));
121                 b->strs = tok(line);
122                 b->next = NULL;
123                 *tail = b;
124                 tail = &b->next;
125                 if (strstr(line, "software-version"))
126                         break;
127         }
128         return head;
129 }
130
131 static void
132 free_gps(struct gps *b) {
133         struct gps *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_gps(struct gps *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_gps(struct cc_usb *usb) {
154         int     count = 0;
155
156         for (;;) {
157                 struct gps *b = gps(usb);
158                 char **flags = find_gps(b, "Flags:");
159                 char **sats = find_gps(b, "Sats:");
160                 int actual_flags = strtol(flags[1], NULL, 0);
161                 int actual_sats = strtol(sats[1], NULL, 0);
162
163                 if (actual_flags & (1 << 4)) {
164                         printf("\n");
165                         printf("Flags: %s (0x%x)\n", flags[1], actual_flags);
166                         printf("Sats: %s (%d)\n", sats[1], actual_sats);
167                         break;
168                 }
169
170                 free_gps(b);
171                 printf("%d", actual_sats);
172                 ++count;
173                 if (count >= 50) {
174                         putchar('\n');
175                         count = 0;
176                 }
177                 else if (count % 10 == 0)
178                         putchar(' ');
179                 fflush(stdout);
180                 sleep(1);
181         }
182         return 1;
183 }
184
185 int
186 main (int argc, char **argv)
187 {
188         char                    *device = NULL;
189         char                    *filename;
190         Elf                     *e;
191         unsigned int            s;
192         int                     i;
193         int                     c;
194         int                     tries;
195         struct cc_usb           *cc = NULL;
196         char                    *tty = NULL;
197         int                     success;
198         int                     verbose = 0;
199         int                     ret = 0;
200
201         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
202                 switch (c) {
203                 case 'T':
204                         tty = optarg;
205                         break;
206                 case 'D':
207                         device = optarg;
208                         break;
209                 case 'v':
210                         verbose++;
211                         break;
212                 default:
213                         usage(argv[0]);
214                         break;
215                 }
216         }
217
218         ao_verbose = verbose;
219
220         if (verbose > 1)
221                 ccdbg_add_debug(CC_DEBUG_BITBANG);
222
223         if (!tty)
224                 tty = cc_usbdevs_find_by_arg(device, "TeleGPS");
225         if (!tty)
226                 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
227         if (!tty)
228                 tty = cc_usbdevs_find_by_arg(device, "TeleMetrum");
229         if (!tty)
230                 tty = getenv("ALTOS_TTY");
231         if (!tty)
232                 tty="/dev/ttyACM0";
233
234         cc = cc_usb_open(tty);
235
236         if (!cc)
237                 exit(1);
238
239         if (!do_gps(cc))
240                 ret = 1;
241         done(cc, ret);
242 }