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; version 2 of the License.
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.
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.
22 #include <sys/types.h>
26 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
27 #define AO_GPS_NUM_SAT_SHIFT (0)
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)
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 */
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)
62 struct ao_gps_sat_orig {
67 #define AO_MAX_GPS_TRACKING 12
69 struct ao_gps_tracking_orig {
71 struct ao_gps_sat_orig sats[AO_MAX_GPS_TRACKING];
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
79 ao_mutex_get(uint8_t *mutex)
84 ao_mutex_put(uint8_t *mutex)
101 sprintf (line, "\\%02x", ((int) c) & 0xff);
103 sprintf (line, "%c", c);
105 write(1, line, strlen(line));
109 #define QUEUE_LEN 4096
111 static char input_queue[QUEUE_LEN];
112 int input_head, input_tail;
114 #include <sys/time.h>
120 gettimeofday(&tv, NULL);
121 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
125 check_skytraq_message(char *from, uint8_t *msg, int len)
127 uint16_t encoded_len, encoded_cksum;
132 // fwrite(msg, 1, len, stdout);
134 if (msg[0] != 0xa0 || msg[1] != 0xa2) {
135 printf ("bad header\n");
142 if (msg[len-1] != 0xb3 || msg[len-2] != 0xb0) {
143 printf ("bad trailer\n");
146 encoded_len = (msg[2] << 8) | msg[3];
148 /* printf ("%9d: %3d\n", get_millis(), id); */
149 if (encoded_len != len - 8) {
151 printf ("length mismatch (got %d, wanted %d)\n",
152 len - 8, encoded_len);
155 encoded_cksum = (msg[len - 4] << 8) | msg[len-3];
157 for (i = 4; i < len - 4; i++)
158 cksum = (cksum + msg[i]) & 0x7fff;
159 if (encoded_cksum != cksum) {
160 printf ("cksum mismatch (got %04x wanted %04x)\n",
161 cksum, encoded_cksum);
190 int16_t heading_rate;
196 #define get_u8(u) u = (msg[off]); off+= 1
197 #define get_u16(u) u = (msg[off] << 8) | (msg[off + 1]); off+= 2
198 #define get_u32(u) u = (msg[off] << 24) | (msg[off + 1] << 16) | (msg[off+2] << 8) | (msg[off+3]); off+= 4
221 get_u16(heading_rate);
230 printf ("Geodetic Navigation Data (41):\n");
231 printf ("\tNav valid %04x\n", nav_valid);
232 printf ("\tNav type %04x\n", nav_type);
233 printf ("\tWeek %5d", week);
234 printf (" TOW %9d", tow);
235 printf (" %4d-%2d-%2d %02d:%02d:%07.4f\n",
237 hour, minute, second / 1000.0);
238 printf ("\tsats: %08x\n", sat_list);
239 printf ("\tlat: %g", lat / 1.0e7);
240 printf (" lon: %g", lon / 1.0e7);
241 printf (" alt_ell: %g", alt_ell / 100.0);
242 printf (" alt_msll: %g", alt_msl / 100.0);
243 printf (" datum: %d\n", datum);
244 printf ("\tground speed: %g", sog / 100.0);
245 printf (" course: %g", cog / 100.0);
246 printf (" climb: %g", climb_rate / 100.0);
247 printf (" heading rate: %g\n", heading_rate / 100.0);
248 printf ("\th error: %g", h_error / 100.0);
249 printf (" v error: %g", v_error / 100.0);
250 printf (" t error: %g", t_error / 100.0);
251 printf (" h vel error: %g\n", h_v_error / 100.0);
268 printf ("Measured Tracker Data (4):\n");
269 printf ("GPS week: %d\n", gps_week);
270 printf ("GPS time of week: %d\n", gps_tow);
271 printf ("channels: %d\n", channels);
272 for (j = 0; j < 12; j++) {
273 uint8_t svid, azimuth, elevation;
280 for (k = 0; k < 10; k++) {
283 printf ("Sat %3d:", svid);
284 printf (" aziumuth: %6.1f", azimuth * 1.5);
285 printf (" elevation: %6.1f", elevation * 0.5);
286 printf (" state: 0x%02x", state);
288 for (k = 0; k < 10; k++)
289 printf(" %3d", c_n[k]);
290 if (state & SIRF_SAT_STATE_ACQUIRED)
292 if (state & SIRF_SAT_STATE_CARRIER_PHASE_VALID)
294 if (state & SIRF_SAT_BIT_SYNC_COMPLETE)
296 if (state & SIRF_SAT_SUBFRAME_SYNC_COMPLETE)
298 if (state & SIRF_SAT_CARRIER_PULLIN_COMPLETE)
300 if (state & SIRF_SAT_CODE_LOCKED)
302 if (state & SIRF_SAT_ACQUISITION_FAILED)
304 if (state & SIRF_SAT_EPHEMERIS_AVAILABLE)
312 printf ("%s %4d:", from, encoded_len);
313 for (i = 4; i < len - 4; i++) {
314 if (((i - 4) & 0xf) == 0)
316 printf (" %3d", msg[i]);
322 static uint8_t skytraq_message[4096];
323 static int skytraq_message_len;
324 static uint8_t skytraq_in_message[4096];
325 static int skytraq_in_len;
328 ao_serial1_getchar(void)
333 while (input_head == input_tail) {
335 input_tail = read(ao_gps_fd, input_queue, QUEUE_LEN);
336 if (input_tail < 0) {
337 if (errno == EINTR || errno == EAGAIN)
346 c = input_queue[input_head];
347 input_head = (input_head + 1) % QUEUE_LEN;
349 // printf ("c: %02x %c\n", uc, uc);
350 if (skytraq_in_len || uc == '$') {
351 if (skytraq_in_len < 4096)
352 skytraq_in_message[skytraq_in_len++] = uc;
354 check_skytraq_message("recv", skytraq_in_message, skytraq_in_len);
363 ao_serial1_putchar(char c)
366 uint8_t uc = (uint8_t) c;
368 if (skytraq_message_len || uc == 0xa0) {
369 if (skytraq_message_len < 4096)
370 skytraq_message[skytraq_message_len++] = uc;
372 check_skytraq_message("send", skytraq_message, skytraq_message_len);
373 skytraq_message_len = 0;
377 i = write(ao_gps_fd, &c, 1);
379 if ((uint8_t) c == 0xb3 || c == '\r') {
380 /* static const struct timespec delay = {
382 .tv_nsec = 100 * 1000 * 1000
386 // nanosleep(&delay, NULL);
390 if (i < 0 && (errno == EINTR || errno == EAGAIN))
397 #define AO_SERIAL_SPEED_4800 0
398 #define AO_SERIAL_SPEED_9600 1
399 #define AO_SERIAL_SPEED_57600 2
402 ao_serial1_set_speed(uint8_t speed)
405 struct termios termios;
408 tcgetattr(fd, &termios);
410 case AO_SERIAL_SPEED_4800:
411 cfsetspeed(&termios, B4800);
413 case AO_SERIAL_SPEED_9600:
414 cfsetspeed(&termios, B38400);
416 case AO_SERIAL_SPEED_57600:
417 cfsetspeed(&termios, B57600);
420 tcsetattr(fd, TCSAFLUSH, &termios);
421 tcflush(fd, TCIFLUSH);
426 #include "ao_gps_print.c"
427 #include "ao_gps_skytraq.c"
430 ao_dump_state(void *wchan)
432 if (wchan == &ao_gps_data)
433 ao_gps_print(&ao_gps_data);
435 ao_gps_tracking_print(&ao_gps_tracking_data);
441 ao_gps_open(const char *tty)
443 struct termios termios;
446 fd = open (tty, O_RDWR);
450 tcgetattr(fd, &termios);
452 cfsetspeed(&termios, B4800);
453 tcsetattr(fd, TCSAFLUSH, &termios);
456 tcflush(fd, TCIFLUSH);
462 static const struct option options[] = {
463 { .name = "tty", .has_arg = 1, .val = 'T' },
467 static void usage(char *program)
469 fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
474 main (int argc, char **argv)
476 char *tty = "/dev/ttyUSB0";
479 while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
489 ao_gps_fd = ao_gps_open(tty);