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