2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 #include <sys/types.h>
27 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
28 #define AO_GPS_NUM_SAT_SHIFT (0)
30 #define AO_GPS_NEW_DATA 1
31 #define AO_GPS_NEW_TRACKING 2
33 #define AO_GPS_VALID (1 << 4)
34 #define AO_GPS_RUNNING (1 << 5)
35 #define AO_GPS_DATE_VALID (1 << 6)
36 #define AO_GPS_COURSE_VALID (1 << 7)
46 int32_t latitude; /* degrees * 10⁷ */
47 int32_t longitude; /* degrees * 10⁷ */
48 int16_t altitude; /* m */
49 uint16_t ground_speed; /* cm/s */
50 uint8_t course; /* degrees / 2 */
51 uint8_t hdop; /* * 5 */
52 int16_t climb_rate; /* cm/s */
53 uint16_t h_error; /* m */
54 uint16_t v_error; /* m */
57 #define AO_TELEMETRY_LOCATION_ALTITUDE(l) ((l)->altitude)
58 #define AO_TELEMETRY_LOCATION_SET_ALTITUDE(l,a) ((l)->altitude = (a))
60 #define SIRF_SAT_STATE_ACQUIRED (1 << 0)
61 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID (1 << 1)
62 #define SIRF_SAT_BIT_SYNC_COMPLETE (1 << 2)
63 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE (1 << 3)
64 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE (1 << 4)
65 #define SIRF_SAT_CODE_LOCKED (1 << 5)
66 #define SIRF_SAT_ACQUISITION_FAILED (1 << 6)
67 #define SIRF_SAT_EPHEMERIS_AVAILABLE (1 << 7)
69 struct ao_gps_sat_orig {
74 #define AO_MAX_GPS_TRACKING 12
76 struct ao_gps_tracking_orig {
78 struct ao_gps_sat_orig sats[AO_MAX_GPS_TRACKING];
81 #define ao_telemetry_location ao_gps_orig
82 #define ao_telemetry_satellite ao_gps_tracking_orig
83 #define ao_telemetry_satellite_info ao_gps_sat_orig
86 ao_mutex_get(uint8_t *mutex)
91 ao_mutex_put(uint8_t *mutex)
106 sprintf (line, "\n");
108 sprintf (line, "\\%02x", ((int) c) & 0xff);
110 sprintf (line, "%c", c);
112 write(1, line, strlen(line));
116 #define QUEUE_LEN 4096
118 static char input_queue[QUEUE_LEN];
119 int input_head, input_tail;
121 #include <sys/time.h>
127 gettimeofday(&tv, NULL);
128 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
132 check_sirf_message(char *from, uint8_t *msg, int len)
134 uint16_t encoded_len, encoded_cksum;
139 if (msg[0] != 0xa0 || msg[1] != 0xa2) {
140 printf ("bad header\n");
147 if (msg[len-1] != 0xb3 || msg[len-2] != 0xb0) {
148 printf ("bad trailer\n");
151 encoded_len = (msg[2] << 8) | msg[3];
153 /* printf ("%9d: %3d\n", get_millis(), id); */
154 if (encoded_len != len - 8) {
156 printf ("length mismatch (got %d, wanted %d)\n",
157 len - 8, encoded_len);
160 encoded_cksum = (msg[len - 4] << 8) | msg[len-3];
162 for (i = 4; i < len - 4; i++)
163 cksum = (cksum + msg[i]) & 0x7fff;
164 if (encoded_cksum != cksum) {
165 printf ("cksum mismatch (got %04x wanted %04x)\n",
166 cksum, encoded_cksum);
195 int16_t heading_rate;
201 #define get_u8(u) u = (msg[off]); off+= 1
202 #define get_u16(u) u = (msg[off] << 8) | (msg[off + 1]); off+= 2
203 #define get_u32(u) u = (msg[off] << 24) | (msg[off + 1] << 16) | (msg[off+2] << 8) | (msg[off+3]); off+= 4
226 get_u16(heading_rate);
235 printf ("Geodetic Navigation Data (41):\n");
236 printf ("\tNav valid %04x\n", nav_valid);
237 printf ("\tNav type %04x\n", nav_type);
238 printf ("\tWeek %5d", week);
239 printf (" TOW %9d", tow);
240 printf (" %4d-%2d-%2d %02d:%02d:%07.4f\n",
242 hour, minute, second / 1000.0);
243 printf ("\tsats: %08x\n", sat_list);
244 printf ("\tlat: %g", lat / 1.0e7);
245 printf (" lon: %g", lon / 1.0e7);
246 printf (" alt_ell: %g", alt_ell / 100.0);
247 printf (" alt_msll: %g", alt_msl / 100.0);
248 printf (" datum: %d\n", datum);
249 printf ("\tground speed: %g", sog / 100.0);
250 printf (" course: %g", cog / 100.0);
251 printf (" climb: %g", climb_rate / 100.0);
252 printf (" heading rate: %g\n", heading_rate / 100.0);
253 printf ("\th error: %g", h_error / 100.0);
254 printf (" v error: %g", v_error / 100.0);
255 printf (" t error: %g", t_error / 100.0);
256 printf (" h vel error: %g\n", h_v_error / 100.0);
273 printf ("Measured Tracker Data (4):\n");
274 printf ("GPS week: %d\n", gps_week);
275 printf ("GPS time of week: %d\n", gps_tow);
276 printf ("channels: %d\n", channels);
277 for (j = 0; j < 12; j++) {
278 uint8_t svid, azimuth, elevation;
285 for (k = 0; k < 10; k++) {
288 printf ("Sat %3d:", svid);
289 printf (" aziumuth: %6.1f", azimuth * 1.5);
290 printf (" elevation: %6.1f", elevation * 0.5);
291 printf (" state: 0x%02x", state);
293 for (k = 0; k < 10; k++)
294 printf(" %3d", c_n[k]);
295 if (state & SIRF_SAT_STATE_ACQUIRED)
297 if (state & SIRF_SAT_STATE_CARRIER_PHASE_VALID)
299 if (state & SIRF_SAT_BIT_SYNC_COMPLETE)
301 if (state & SIRF_SAT_SUBFRAME_SYNC_COMPLETE)
303 if (state & SIRF_SAT_CARRIER_PULLIN_COMPLETE)
305 if (state & SIRF_SAT_CODE_LOCKED)
307 if (state & SIRF_SAT_ACQUISITION_FAILED)
309 if (state & SIRF_SAT_EPHEMERIS_AVAILABLE)
317 printf ("%s %4d:", from, encoded_len);
318 for (i = 4; i < len - 4; i++) {
319 if (((i - 4) & 0xf) == 0)
321 printf (" %3d", msg[i]);
327 static uint8_t sirf_message[4096];
328 static int sirf_message_len;
329 static uint8_t sirf_in_message[4096];
330 static int sirf_in_len;
333 ao_serial1_getchar(void)
338 while (input_head == input_tail) {
340 input_tail = read(ao_gps_fd, input_queue, QUEUE_LEN);
341 if (input_tail < 0) {
342 if (errno == EINTR || errno == EAGAIN)
351 c = input_queue[input_head];
352 input_head = (input_head + 1) % QUEUE_LEN;
354 if (sirf_in_len || uc == 0xa0) {
355 if (sirf_in_len < 4096)
356 sirf_in_message[sirf_in_len++] = uc;
358 check_sirf_message("recv", sirf_in_message, sirf_in_len);
367 ao_serial1_putchar(char c)
370 uint8_t uc = (uint8_t) c;
372 if (sirf_message_len || uc == 0xa0) {
373 if (sirf_message_len < 4096)
374 sirf_message[sirf_message_len++] = uc;
376 check_sirf_message("send", sirf_message, sirf_message_len);
377 sirf_message_len = 0;
381 i = write(ao_gps_fd, &c, 1);
383 if ((uint8_t) c == 0xb3 || c == '\r') {
384 /* static const struct timespec delay = {
386 .tv_nsec = 100 * 1000 * 1000
390 // nanosleep(&delay, NULL);
394 if (i < 0 && (errno == EINTR || errno == EAGAIN))
401 #define AO_SERIAL_SPEED_4800 0
402 #define AO_SERIAL_SPEED_57600 1
403 #define AO_SERIAL_SPEED_115200 2
406 ao_serial1_set_speed(uint8_t speed)
409 struct termios termios;
412 tcgetattr(fd, &termios);
414 case AO_SERIAL_SPEED_4800:
415 cfsetspeed(&termios, B4800);
417 case AO_SERIAL_SPEED_57600:
418 cfsetspeed(&termios, B57600);
420 case AO_SERIAL_SPEED_115200:
421 cfsetspeed(&termios, B115200);
424 tcsetattr(fd, TCSAFLUSH, &termios);
425 tcflush(fd, TCIFLUSH);
430 #include "ao_gps_print.c"
431 #include "ao_gps_sirf.c"
434 ao_dump_state(void *wchan)
438 if (wchan != &ao_gps_new)
441 if (ao_gps_new & AO_GPS_NEW_DATA) {
442 ao_gps_print(&ao_gps_data);
445 if (ao_gps_new & AO_GPS_NEW_TRACKING) {
446 ao_gps_tracking_print(&ao_gps_tracking_data);
450 printf ("%02d:%02d:%02d",
451 ao_gps_data.hour, ao_gps_data.minute,
453 printf (" nsat %d %svalid",
454 ao_gps_data.flags & AO_GPS_NUM_SAT_MASK,
455 ao_gps_data.flags & AO_GPS_VALID ? "" : "not ");
456 printf (" lat %g lon %g alt %d",
457 ao_gps_data.latitude / 1.0e7,
458 ao_gps_data.longitude / 1.0e7,
459 ao_gps_data.altitude);
460 printf (" speed %g climb %g course %d",
461 ao_gps_data.ground_speed / 100.0,
462 ao_gps_data.climb_rate / 100.0,
463 ao_gps_data.course * 2);
464 printf (" hdop %g h_error %d v_error %d",
465 ao_gps_data.hdop / 5.0,
466 ao_gps_data.h_error, ao_gps_data.v_error);
469 for (i = 0; i < 12; i++)
470 printf (" %2d(%02d)",
471 ao_gps_tracking_data.sats[i].svid,
472 ao_gps_tracking_data.sats[i].c_n_1);
477 ao_gps_open(const char *tty)
479 struct termios termios;
482 fd = open (tty, O_RDWR);
486 tcgetattr(fd, &termios);
488 cfsetspeed(&termios, B4800);
489 tcsetattr(fd, TCSAFLUSH, &termios);
492 tcflush(fd, TCIFLUSH);
498 static const struct option options[] = {
499 { .name = "tty", .has_arg = 1, .val = 'T' },
503 static void usage(char *program)
505 fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
510 main (int argc, char **argv)
512 char *tty = "/dev/ttyUSB0";
515 while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
525 ao_gps_fd = ao_gps_open(tty);