altos: Fix GPS test frameworks to handle shared ao_gps_new variable
[fw/altos] / src / test / ao_gps_test_ublox.c
1 /*
2  * Copyright © 2009 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #define AO_GPS_TEST
19 #include "ao_host.h"
20 #include <termios.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
27 #define AO_GPS_NUM_SAT_SHIFT    (0)
28
29 #define AO_GPS_NEW_DATA         1
30 #define AO_GPS_NEW_TRACKING     2
31
32 #define AO_GPS_VALID            (1 << 4)
33 #define AO_GPS_RUNNING          (1 << 5)
34 #define AO_GPS_DATE_VALID       (1 << 6)
35 #define AO_GPS_COURSE_VALID     (1 << 7)
36
37 struct ao_telemetry_location {
38         uint8_t                 year;
39         uint8_t                 month;
40         uint8_t                 day;
41         uint8_t                 hour;
42         uint8_t                 minute;
43         uint8_t                 second;
44         uint8_t                 flags;
45         int32_t                 latitude;       /* degrees * 10⁷ */
46         int32_t                 longitude;      /* degrees * 10⁷ */
47         int16_t                 altitude;       /* m */
48         uint16_t                ground_speed;   /* cm/s */
49         uint8_t                 course;         /* degrees / 2 */
50         uint8_t                 pdop;           /* * 5 */
51         uint8_t                 hdop;           /* * 5 */
52         uint8_t                 vdop;           /* * 5 */
53         int16_t                 climb_rate;     /* cm/s */
54         uint16_t                h_error;        /* m */
55         uint16_t                v_error;        /* m */
56 };
57
58 #define UBLOX_SAT_STATE_ACQUIRED                (1 << 0)
59 #define UBLOX_SAT_STATE_CARRIER_PHASE_VALID     (1 << 1)
60 #define UBLOX_SAT_BIT_SYNC_COMPLETE             (1 << 2)
61 #define UBLOX_SAT_SUBFRAME_SYNC_COMPLETE        (1 << 3)
62 #define UBLOX_SAT_CARRIER_PULLIN_COMPLETE       (1 << 4)
63 #define UBLOX_SAT_CODE_LOCKED                   (1 << 5)
64 #define UBLOX_SAT_ACQUISITION_FAILED            (1 << 6)
65 #define UBLOX_SAT_EPHEMERIS_AVAILABLE           (1 << 7)
66
67 struct ao_telemetry_satellite_info {
68         uint8_t         svid;
69         uint8_t         c_n_1;
70 };
71
72 #define AO_TELEMETRY_SATELLITE_MAX_SAT  12
73
74 struct ao_telemetry_satellite {
75         uint8_t                                 channels;
76         struct ao_telemetry_satellite_info      sats[AO_TELEMETRY_SATELLITE_MAX_SAT];
77 };
78
79 #define ao_gps_orig ao_telemetry_location
80 #define ao_gps_tracking_orig ao_telemetry_satellite
81 #define ao_gps_sat_orig ao_telemetry_satellite_info
82
83 extern __xdata struct ao_telemetry_location     ao_gps_data;
84 extern __xdata struct ao_telemetry_satellite    ao_gps_tracking_data;
85
86 uint8_t ao_gps_mutex;
87
88 void
89 ao_mutex_get(uint8_t *mutex)
90 {
91 }
92
93 void
94 ao_mutex_put(uint8_t *mutex)
95 {
96 }
97
98 static int ao_gps_fd;
99 static FILE *ao_gps_file;
100
101 #if 0
102 static void
103 ao_dbg_char(char c)
104 {
105         char    line[128];
106         line[0] = '\0';
107         if (c < ' ') {
108                 if (c == '\n')
109                         sprintf (line, "\n");
110                 else
111                         sprintf (line, "\\%02x", ((int) c) & 0xff);
112         } else {
113                 sprintf (line, "%c", c);
114         }
115         write(1, line, strlen(line));
116 }
117 #endif
118
119 #include <sys/time.h>
120
121 int
122 get_millis(void)
123 {
124         struct timeval  tv;
125         gettimeofday(&tv, NULL);
126         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
127 }
128
129 static uint8_t  in_message[4096];
130 static int      in_len;
131 static uint16_t recv_len;
132
133 static void check_ublox_message(char *which, uint8_t *msg);
134
135 char
136 ao_gps_getchar(void)
137 {
138         char    c;
139         uint8_t uc;
140         int     i;
141
142         i = getc(ao_gps_file);
143         if (i == EOF) {
144                 perror("getchar");
145                 exit(1);
146         }
147         c = i;
148         uc = (uint8_t) c;
149         if (in_len || uc == 0xb5) {
150                 in_message[in_len++] = c;
151                 if (in_len == 6) {
152                         recv_len = in_message[4] | (in_message[5] << 8);
153                 } else if (in_len > 6 && in_len == recv_len + 8) {
154                         check_ublox_message("recv", in_message + 2);
155                         in_len = 0;
156                 }
157                 
158         }
159         return c;
160 }
161
162 #define MESSAGE_LEN     4096
163
164 static uint8_t  message[MESSAGE_LEN];
165 static int      message_len;
166 static uint16_t send_len;
167
168 void
169 ao_gps_putchar(char c)
170 {
171         int     i;
172         uint8_t uc = (uint8_t) c;
173
174         if (message_len || uc == 0xb5) {
175                 if (message_len < MESSAGE_LEN)
176                         message[message_len++] = uc;
177                 if (message_len == 6) {
178                         send_len = message[4] | (message[5] << 8);
179                 } else if (message_len > 6 && message_len == send_len + 8) {
180                         check_ublox_message("send", message + 2);
181                         message_len = 0;
182                 }
183         }
184
185         for (;;) {
186                 i = write(ao_gps_fd, &c, 1);
187                 if (i == 1)
188                         break;
189                 if (i < 0 && (errno == EINTR || errno == EAGAIN))
190                         continue;
191                 perror("putchar");
192                 exit(1);
193         }
194 }
195
196 #define AO_SERIAL_SPEED_4800    0
197 #define AO_SERIAL_SPEED_9600    1
198 #define AO_SERIAL_SPEED_57600   2
199 #define AO_SERIAL_SPEED_115200  3
200
201 static void
202 ao_gps_set_speed(uint8_t speed)
203 {
204         int     fd = ao_gps_fd;
205         struct termios  termios;
206
207         printf ("\t\tset speed %d\n", speed);
208         tcdrain(fd);
209         tcgetattr(fd, &termios);
210         switch (speed) {
211         case AO_SERIAL_SPEED_4800:
212                 cfsetspeed(&termios, B4800);
213                 break;
214         case AO_SERIAL_SPEED_9600:
215                 cfsetspeed(&termios, B9600);
216                 break;
217         case AO_SERIAL_SPEED_57600:
218                 cfsetspeed(&termios, B57600);
219                 break;
220         case AO_SERIAL_SPEED_115200:
221                 cfsetspeed(&termios, B115200);
222                 break;
223         }
224         tcsetattr(fd, TCSAFLUSH, &termios);
225         tcflush(fd, TCIFLUSH);
226 }
227
228 #define ao_time() 0
229
230 uint8_t ao_task_minimize_latency;
231
232 #define ao_usb_getchar()        0
233
234 #include "ao_gps_print.c"
235 #include "ao_gps_show.c"
236 #include "ao_gps_ublox.c"
237
238 static void
239 check_ublox_message(char *which, uint8_t *msg)
240 {
241         uint8_t class = msg[0];
242         uint8_t id = msg[1];
243         uint16_t len = msg[2] | (msg[3] << 8);
244         uint16_t i;
245         struct ao_ublox_cksum   cksum_msg = { .a = msg[4 + len],
246                                               .b = msg[4 + len + 1] };
247         struct ao_ublox_cksum   cksum= { 0, 0 };
248
249         for (i = 0; i < 4 + len; i++) {
250                 add_cksum(&cksum, msg[i]);
251         }
252         if (cksum.a != cksum_msg.a || cksum.b != cksum_msg.b) {
253                 printf ("\t%s: cksum mismatch %02x,%02x != %02x,%02x\n",
254                         which,
255                         cksum_msg.a & 0xff,
256                         cksum_msg.b & 0xff,
257                         cksum.a & 0xff,
258                         cksum.b & 0xff);
259                 return;
260         }
261         switch (class) {
262         case UBLOX_NAV:
263                 switch (id) {
264                 case UBLOX_NAV_DOP: ;
265                         struct ublox_nav_dop    *nav_dop = (void *) msg;
266                         printf ("\tnav-dop    iTOW %9u gDOP %5u dDOP %5u tDOP %5u vDOP %5u hDOP %5u nDOP %5u eDOP %5u\n",
267                                 nav_dop->itow,
268                                 nav_dop->gdop,
269                                 nav_dop->ddop,
270                                 nav_dop->tdop,
271                                 nav_dop->vdop,
272                                 nav_dop->hdop,
273                                 nav_dop->ndop,
274                                 nav_dop->edop);
275                         return;
276                 case UBLOX_NAV_POSLLH: ;
277                         struct ublox_nav_posllh *nav_posllh = (void *) msg;
278                         printf ("\tnav-posllh iTOW %9u lon %12.7f lat %12.7f height %10.3f hMSL %10.3f hAcc %10.3f vAcc %10.3f\n",
279                                 nav_posllh->itow,
280                                 nav_posllh->lon / 1e7,
281                                 nav_posllh->lat / 1e7,
282                                 nav_posllh->height / 1e3,
283                                 nav_posllh->hmsl / 1e3,
284                                 nav_posllh->hacc / 1e3,
285                                 nav_posllh->vacc / 1e3);
286                         return;
287                 case UBLOX_NAV_SOL: ;
288                         struct ublox_nav_sol    *nav_sol = (struct ublox_nav_sol *) msg;
289                         printf ("\tnav-sol    iTOW %9u fTOW %9d week %5d gpsFix %2d flags %02x\n",
290                                 nav_sol->itow, nav_sol->ftow, nav_sol->week,
291                                 nav_sol->gpsfix, nav_sol->flags);
292                         return;
293                 case UBLOX_NAV_SVINFO: ;
294                         struct ublox_nav_svinfo *nav_svinfo = (struct ublox_nav_svinfo *) msg;
295                         printf ("\tnav-svinfo iTOW %9u numCH %3d globalFlags %02x\n",
296                                 nav_svinfo->itow, nav_svinfo->numch, nav_svinfo->globalflags);
297                         int i;
298                         for (i = 0; i < nav_svinfo->numch; i++) {
299                                 struct ublox_nav_svinfo_block *nav_svinfo_block = (void *) (msg + 12 + 12 * i);
300                                 printf ("\t\tchn %3u svid %3u flags %02x quality %3u cno %3u elev %3d azim %6d prRes %9d\n",
301                                         nav_svinfo_block->chn,
302                                         nav_svinfo_block->svid,
303                                         nav_svinfo_block->flags,
304                                         nav_svinfo_block->quality,
305                                         nav_svinfo_block->cno,
306                                         nav_svinfo_block->elev,
307                                         nav_svinfo_block->azim,
308                                         nav_svinfo_block->prres);
309                         }
310                         return;
311                 case UBLOX_NAV_VELNED: ;
312                         struct ublox_nav_velned *nav_velned = (void *) msg;
313                         printf ("\tnav-velned iTOW %9u velN %10.2f velE %10.2f velD %10.2f speed %10.2f gSpeed %10.2f heading %10.5f sAcc %10.2f cAcc %10.5f\n",
314                                 nav_velned->itow,
315                                 nav_velned->veln / 1e2,
316                                 nav_velned->vele / 1e2,
317                                 nav_velned->veld / 1e2,
318                                 nav_velned->speed / 1e2,
319                                 nav_velned->gspeed / 1e2,
320                                 nav_velned->heading / 1e5,
321                                 nav_velned->sacc / 1e5,
322                                 nav_velned->cacc / 1e6);
323                         return;
324                 case UBLOX_NAV_TIMEUTC:;
325                         struct ublox_nav_timeutc *nav_timeutc = (void *) msg;
326                         printf ("\tnav-timeutc iTOW %9u tAcc %5u nano %5d %4u-%2d-%2d %2d:%02d:%02d flags %02x\n",
327                                 nav_timeutc->itow,
328                                 nav_timeutc->tacc,
329                                 nav_timeutc->nano,
330                                 nav_timeutc->year,
331                                 nav_timeutc->month,
332                                 nav_timeutc->day,
333                                 nav_timeutc->hour,
334                                 nav_timeutc->min,
335                                 nav_timeutc->sec,
336                                 nav_timeutc->valid);
337                         return;
338                 }
339                 break;
340         }
341 #if 1
342         printf ("\t%s: class %02x id %02x len %d:", which, class & 0xff, id & 0xff, len & 0xffff);
343         for (i = 0; i < len; i++)
344                 printf (" %02x", msg[4 + i]);
345         printf (" cksum %02x %02x", cksum_msg.a & 0xff, cksum_msg.b & 0xff);
346 #endif
347         printf ("\n");
348 }
349
350 void
351 ao_dump_state(void *wchan)
352 {
353         if (wchan == &ao_gps_new)
354                 ao_gps_show();
355         return;
356 }
357
358 int
359 ao_gps_open(const char *tty)
360 {
361         struct termios  termios;
362         int fd;
363
364         fd = open (tty, O_RDWR);
365         if (fd < 0)
366                 return -1;
367
368         tcgetattr(fd, &termios);
369         cfmakeraw(&termios);
370         cfsetspeed(&termios, B4800);
371         tcsetattr(fd, TCSAFLUSH, &termios);
372
373         tcdrain(fd);
374         tcflush(fd, TCIFLUSH);
375         return fd;
376 }
377
378 #include <getopt.h>
379
380 static const struct option options[] = {
381         { .name = "tty", .has_arg = 1, .val = 'T' },
382         { 0, 0, 0, 0},
383 };
384
385 static void usage(char *program)
386 {
387         fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
388         exit(1);
389 }
390
391 int
392 main (int argc, char **argv)
393 {
394         char    *tty = "/dev/ttyUSB0";
395         int     c;
396
397         while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
398                 switch (c) {
399                 case 'T':
400                         tty = optarg;
401                         break;
402                 default:
403                         usage(argv[0]);
404                         break;
405                 }
406         }
407         ao_gps_fd = ao_gps_open(tty);
408         if (ao_gps_fd < 0) {
409                 perror (tty);
410                 exit (1);
411         }
412         ao_gps_file = fdopen(ao_gps_fd, "r");
413         ao_gps();
414         return 0;
415 }