altos: Merge GPS logging into a single function
[fw/altos] / src / test / ao_gps_test.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 <unistd.h>
25 #include <fcntl.h>
26 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
27 #define AO_GPS_NUM_SAT_SHIFT    (0)
28
29 #define AO_GPS_VALID            (1 << 4)
30 #define AO_GPS_RUNNING          (1 << 5)
31 #define AO_GPS_DATE_VALID       (1 << 6)
32 #define AO_GPS_COURSE_VALID     (1 << 7)
33
34 struct ao_gps_orig {
35         uint8_t                 year;
36         uint8_t                 month;
37         uint8_t                 day;
38         uint8_t                 hour;
39         uint8_t                 minute;
40         uint8_t                 second;
41         uint8_t                 flags;
42         int32_t                 latitude;       /* degrees * 10⁷ */
43         int32_t                 longitude;      /* degrees * 10⁷ */
44         int16_t                 altitude;       /* m */
45         uint16_t                ground_speed;   /* cm/s */
46         uint8_t                 course;         /* degrees / 2 */
47         uint8_t                 hdop;           /* * 5 */
48         int16_t                 climb_rate;     /* cm/s */
49         uint16_t                h_error;        /* m */
50         uint16_t                v_error;        /* m */
51 };
52
53 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
54 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
55 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
56 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
57 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
58 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
59 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
60 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
61
62 struct ao_gps_sat_orig {
63         uint8_t         svid;
64         uint8_t         c_n_1;
65 };
66
67 #define AO_MAX_GPS_TRACKING     12
68
69 struct ao_gps_tracking_orig {
70         uint8_t                 channels;
71         struct ao_gps_sat_orig  sats[AO_MAX_GPS_TRACKING];
72 };
73
74 #define ao_telemetry_location ao_gps_orig
75 #define ao_telemetry_satellite ao_gps_tracking_orig
76 #define ao_telemetry_satellite_info ao_gps_sat_orig
77
78 void
79 ao_mutex_get(uint8_t *mutex)
80 {
81 }
82
83 void
84 ao_mutex_put(uint8_t *mutex)
85 {
86 }
87
88 static int
89 ao_gps_fd;
90
91 #if 0
92 static void
93 ao_dbg_char(char c)
94 {
95         char    line[128];
96         line[0] = '\0';
97         if (c < ' ') {
98                 if (c == '\n')
99                         sprintf (line, "\n");
100                 else
101                         sprintf (line, "\\%02x", ((int) c) & 0xff);
102         } else {
103                 sprintf (line, "%c", c);
104         }
105         write(1, line, strlen(line));
106 }
107 #endif
108
109 #define QUEUE_LEN       4096
110
111 static char     input_queue[QUEUE_LEN];
112 int             input_head, input_tail;
113
114 #include <sys/time.h>
115
116 int
117 get_millis(void)
118 {
119         struct timeval  tv;
120         gettimeofday(&tv, NULL);
121         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
122 }
123
124 static void
125 check_sirf_message(char *from, uint8_t *msg, int len)
126 {
127         uint16_t        encoded_len, encoded_cksum;
128         uint16_t        cksum;
129         uint8_t         id;
130         int             i;
131
132         if (msg[0] != 0xa0 || msg[1] != 0xa2) {
133                 printf ("bad header\n");
134                 return;
135         }
136         if (len < 7) {
137                 printf("short\n");
138                 return;
139         }
140         if (msg[len-1] != 0xb3 || msg[len-2] != 0xb0) {
141                 printf ("bad trailer\n");
142                 return;
143         }
144         encoded_len = (msg[2] << 8) | msg[3];
145         id = msg[4];
146 /*      printf ("%9d: %3d\n", get_millis(), id); */
147         if (encoded_len != len - 8) {
148                 if (id != 52)
149                         printf ("length mismatch (got %d, wanted %d)\n",
150                                 len - 8, encoded_len);
151                 return;
152         }
153         encoded_cksum = (msg[len - 4] << 8) | msg[len-3];
154         cksum = 0;
155         for (i = 4; i < len - 4; i++)
156                 cksum = (cksum + msg[i]) & 0x7fff;
157         if (encoded_cksum != cksum) {
158                 printf ("cksum mismatch (got %04x wanted %04x)\n",
159                         cksum, encoded_cksum);
160                 return;
161         }
162         id = msg[4];
163         switch (id) {
164         case 41:{
165                 int     off = 4;
166
167                 uint8_t         id;
168                 uint16_t        nav_valid;
169                 uint16_t        nav_type;
170                 uint16_t        week;
171                 uint32_t        tow;
172                 uint16_t        year;
173                 uint8_t         month;
174                 uint8_t         day;
175                 uint8_t         hour;
176                 uint8_t         minute;
177                 uint16_t        second;
178                 uint32_t        sat_list;
179                 int32_t         lat;
180                 int32_t         lon;
181                 int32_t         alt_ell;
182                 int32_t         alt_msl;
183                 int8_t          datum;
184                 uint16_t        sog;
185                 uint16_t        cog;
186                 int16_t         mag_var;
187                 int16_t         climb_rate;
188                 int16_t         heading_rate;
189                 uint32_t        h_error;
190                 uint32_t        v_error;
191                 uint32_t        t_error;
192                 uint16_t        h_v_error;
193
194 #define get_u8(u)       u = (msg[off]); off+= 1
195 #define get_u16(u)      u = (msg[off] << 8) | (msg[off + 1]); off+= 2
196 #define get_u32(u)      u = (msg[off] << 24) | (msg[off + 1] << 16) | (msg[off+2] << 8) | (msg[off+3]); off+= 4
197
198                 get_u8(id);
199                 get_u16(nav_valid);
200                 get_u16(nav_type);
201                 get_u16(week);
202                 get_u32(tow);
203                 get_u16(year);
204                 get_u8(month);
205                 get_u8(day);
206                 get_u8(hour);
207                 get_u8(minute);
208                 get_u16(second);
209                 get_u32(sat_list);
210                 get_u32(lat);
211                 get_u32(lon);
212                 get_u32(alt_ell);
213                 get_u32(alt_msl);
214                 get_u8(datum);
215                 get_u16(sog);
216                 get_u16(cog);
217                 get_u16(mag_var);
218                 get_u16(climb_rate);
219                 get_u16(heading_rate);
220                 get_u32(h_error);
221                 get_u32(v_error);
222                 get_u32(t_error);
223                 get_u16(h_v_error);
224
225
226                 (void) mag_var;
227                 (void) id;
228                 printf ("Geodetic Navigation Data (41):\n");
229                 printf ("\tNav valid %04x\n", nav_valid);
230                 printf ("\tNav type %04x\n", nav_type);
231                 printf ("\tWeek %5d", week);
232                 printf (" TOW %9d", tow);
233                 printf (" %4d-%2d-%2d %02d:%02d:%07.4f\n",
234                         year, month, day,
235                         hour, minute, second / 1000.0);
236                 printf ("\tsats: %08x\n", sat_list);
237                 printf ("\tlat: %g", lat / 1.0e7);
238                 printf (" lon: %g", lon / 1.0e7);
239                 printf (" alt_ell: %g", alt_ell / 100.0);
240                 printf (" alt_msll: %g", alt_msl / 100.0);
241                 printf (" datum: %d\n", datum);
242                 printf ("\tground speed: %g", sog / 100.0);
243                 printf (" course: %g", cog / 100.0);
244                 printf (" climb: %g", climb_rate / 100.0);
245                 printf (" heading rate: %g\n", heading_rate / 100.0);
246                 printf ("\th error: %g", h_error / 100.0);
247                 printf (" v error: %g", v_error / 100.0);
248                 printf (" t error: %g", t_error / 100.0);
249                 printf (" h vel error: %g\n", h_v_error / 100.0);
250                 break;
251         }
252         case 4: {
253                 int off = 4;
254                 uint8_t         id;
255                 int16_t         gps_week;
256                 uint32_t        gps_tow;
257                 uint8_t         channels;
258                 int             j, k;
259
260                 get_u8(id);
261                 get_u16(gps_week);
262                 get_u32(gps_tow);
263                 get_u8(channels);
264
265                 (void) id;
266                 printf ("Measured Tracker Data (4):\n");
267                 printf ("GPS week: %d\n", gps_week);
268                 printf ("GPS time of week: %d\n", gps_tow);
269                 printf ("channels: %d\n", channels);
270                 for (j = 0; j < 12; j++) {
271                         uint8_t svid, azimuth, elevation;
272                         uint16_t state;
273                         uint8_t c_n[10];
274                         get_u8(svid);
275                         get_u8(azimuth);
276                         get_u8(elevation);
277                         get_u16(state);
278                         for (k = 0; k < 10; k++) {
279                                 get_u8(c_n[k]);
280                         }
281                         printf ("Sat %3d:", svid);
282                         printf (" aziumuth: %6.1f", azimuth * 1.5);
283                         printf (" elevation: %6.1f", elevation * 0.5);
284                         printf (" state: 0x%02x", state);
285                         printf (" c_n:");
286                         for (k = 0; k < 10; k++)
287                                 printf(" %3d", c_n[k]);
288                         if (state & SIRF_SAT_STATE_ACQUIRED)
289                                 printf(" acq,");
290                         if (state & SIRF_SAT_STATE_CARRIER_PHASE_VALID)
291                                 printf(" car,");
292                         if (state & SIRF_SAT_BIT_SYNC_COMPLETE)
293                                 printf(" bit,");
294                         if (state & SIRF_SAT_SUBFRAME_SYNC_COMPLETE)
295                                 printf(" sub,");
296                         if (state & SIRF_SAT_CARRIER_PULLIN_COMPLETE)
297                                 printf(" pullin,");
298                         if (state & SIRF_SAT_CODE_LOCKED)
299                                 printf(" code,");
300                         if (state & SIRF_SAT_ACQUISITION_FAILED)
301                                 printf(" fail,");
302                         if (state & SIRF_SAT_EPHEMERIS_AVAILABLE)
303                                 printf(" ephem,");
304                         printf ("\n");
305                 }
306                 break;
307         }
308         default:
309                 return;
310                 printf ("%s %4d:", from, encoded_len);
311                 for (i = 4; i < len - 4; i++) {
312                         if (((i - 4) & 0xf) == 0)
313                                 printf("\n   ");
314                         printf (" %3d", msg[i]);
315                 }
316                 printf ("\n");
317         }
318 }
319
320 static uint8_t  sirf_message[4096];
321 static int      sirf_message_len;
322 static uint8_t  sirf_in_message[4096];
323 static int      sirf_in_len;
324
325 char
326 ao_serial1_getchar(void)
327 {
328         char    c;
329         uint8_t uc;
330
331         while (input_head == input_tail) {
332                 for (;;) {
333                         input_tail = read(ao_gps_fd, input_queue, QUEUE_LEN);
334                         if (input_tail < 0) {
335                                 if (errno == EINTR || errno == EAGAIN)
336                                         continue;
337                                 perror ("getchar");
338                                 exit (1);
339                         }
340                         input_head = 0;
341                         break;
342                 }
343         }
344         c = input_queue[input_head];
345         input_head = (input_head + 1) % QUEUE_LEN;
346         uc = c;
347         if (sirf_in_len || uc == 0xa0) {
348                 if (sirf_in_len < 4096)
349                         sirf_in_message[sirf_in_len++] = uc;
350                 if (uc == 0xb3) {
351                         check_sirf_message("recv", sirf_in_message, sirf_in_len);
352                         sirf_in_len = 0;
353                 }
354         }
355         return c;
356 }
357
358
359 void
360 ao_serial1_putchar(char c)
361 {
362         int     i;
363         uint8_t uc = (uint8_t) c;
364
365         if (sirf_message_len || uc == 0xa0) {
366                 if (sirf_message_len < 4096)
367                         sirf_message[sirf_message_len++] = uc;
368                 if (uc == 0xb3) {
369                         check_sirf_message("send", sirf_message, sirf_message_len);
370                         sirf_message_len = 0;
371                 }
372         }
373         for (;;) {
374                 i = write(ao_gps_fd, &c, 1);
375                 if (i == 1) {
376                         if ((uint8_t) c == 0xb3 || c == '\r') {
377 /*                              static const struct timespec delay = {
378                                         .tv_sec = 0,
379                                         .tv_nsec = 100 * 1000 * 1000
380                                 };
381 */
382                                 tcdrain(ao_gps_fd);
383 //                              nanosleep(&delay, NULL);
384                         }
385                         break;
386                 }
387                 if (i < 0 && (errno == EINTR || errno == EAGAIN))
388                         continue;
389                 perror("putchar");
390                 exit(1);
391         }
392 }
393
394 #define AO_SERIAL_SPEED_4800    0
395 #define AO_SERIAL_SPEED_57600   1
396 #define AO_SERIAL_SPEED_115200  2
397
398 static void
399 ao_serial1_set_speed(uint8_t speed)
400 {
401         int     fd = ao_gps_fd;
402         struct termios  termios;
403
404         tcdrain(fd);
405         tcgetattr(fd, &termios);
406         switch (speed) {
407         case AO_SERIAL_SPEED_4800:
408                 cfsetspeed(&termios, B4800);
409                 break;
410         case AO_SERIAL_SPEED_57600:
411                 cfsetspeed(&termios, B57600);
412                 break;
413         case AO_SERIAL_SPEED_115200:
414                 cfsetspeed(&termios, B115200);
415                 break;
416         }
417         tcsetattr(fd, TCSAFLUSH, &termios);
418         tcflush(fd, TCIFLUSH);
419 }
420
421 #define ao_time() 0
422
423 #include "ao_gps_print.c"
424 #include "ao_gps_sirf.c"
425
426 void
427 ao_dump_state(void *wchan)
428 {
429         int     i;
430
431         if (wchan != &ao_gps_new)
432                 return;
433         
434         if (ao_gps_new & AO_GPS_NEW_DATA) {
435                 ao_gps_print(&ao_gps_data);
436                 putchar('\n');
437         }
438         if (ao_gps_new & AO_GPS_NEW_TRACKING) {
439                 ao_gps_tracking_print(&ao_gps_tracking_data);
440                 putchar('\n');
441         }
442         return;
443         printf ("%02d:%02d:%02d",
444                 ao_gps_data.hour, ao_gps_data.minute,
445                 ao_gps_data.second);
446         printf (" nsat %d %svalid",
447                 ao_gps_data.flags & AO_GPS_NUM_SAT_MASK,
448                 ao_gps_data.flags & AO_GPS_VALID ? "" : "not ");
449         printf (" lat %g lon %g alt %d",
450                 ao_gps_data.latitude / 1.0e7,
451                 ao_gps_data.longitude / 1.0e7,
452                 ao_gps_data.altitude);
453         printf (" speed %g climb %g course %d",
454                 ao_gps_data.ground_speed / 100.0,
455                 ao_gps_data.climb_rate / 100.0,
456                 ao_gps_data.course * 2);
457         printf (" hdop %g h_error %d v_error %d",
458                 ao_gps_data.hdop / 5.0,
459                 ao_gps_data.h_error, ao_gps_data.v_error);
460         printf("\n");
461         printf ("\t");
462         for (i = 0; i < 12; i++)
463                 printf (" %2d(%02d)",
464                         ao_gps_tracking_data.sats[i].svid,
465                         ao_gps_tracking_data.sats[i].c_n_1);
466         printf ("\n");
467 }
468
469 int
470 ao_gps_open(const char *tty)
471 {
472         struct termios  termios;
473         int fd;
474
475         fd = open (tty, O_RDWR);
476         if (fd < 0)
477                 return -1;
478
479         tcgetattr(fd, &termios);
480         cfmakeraw(&termios);
481         cfsetspeed(&termios, B4800);
482         tcsetattr(fd, TCSAFLUSH, &termios);
483
484         tcdrain(fd);
485         tcflush(fd, TCIFLUSH);
486         return fd;
487 }
488
489 #include <getopt.h>
490
491 static const struct option options[] = {
492         { .name = "tty", .has_arg = 1, .val = 'T' },
493         { 0, 0, 0, 0},
494 };
495
496 static void usage(char *program)
497 {
498         fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
499         exit(1);
500 }
501
502 int
503 main (int argc, char **argv)
504 {
505         char    *tty = "/dev/ttyUSB0";
506         int     c;
507
508         while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
509                 switch (c) {
510                 case 'T':
511                         tty = optarg;
512                         break;
513                 default:
514                         usage(argv[0]);
515                         break;
516                 }
517         }
518         ao_gps_fd = ao_gps_open(tty);
519         if (ao_gps_fd < 0) {
520                 perror (tty);
521                 exit (1);
522         }
523         ao_gps_setup();
524         ao_gps();
525         return 0;
526 }