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