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