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>
25 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
26 #define AO_GPS_NUM_SAT_SHIFT (0)
28 #define AO_GPS_VALID (1 << 4)
29 #define AO_GPS_RUNNING (1 << 5)
36 int32_t latitude; /* degrees * 10⁷ */
37 int32_t longitude; /* degrees * 10⁷ */
38 int16_t altitude; /* m */
39 uint16_t ground_speed; /* cm/s */
40 uint8_t course; /* degrees / 2 */
41 uint8_t hdop; /* * 5 */
42 int16_t climb_rate; /* cm/s */
43 uint16_t h_error; /* m */
44 uint16_t v_error; /* m */
47 #define SIRF_SAT_STATE_ACQUIRED (1 << 0)
48 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID (1 << 1)
49 #define SIRF_SAT_BIT_SYNC_COMPLETE (1 << 2)
50 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE (1 << 3)
51 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE (1 << 4)
52 #define SIRF_SAT_CODE_LOCKED (1 << 5)
53 #define SIRF_SAT_ACQUISITION_FAILED (1 << 6)
54 #define SIRF_SAT_EPHEMERIS_AVAILABLE (1 << 7)
56 struct ao_gps_sat_data {
62 struct ao_gps_tracking_data {
64 struct ao_gps_sat_data sats[12];
68 ao_mutex_get(uint8_t *mutex)
73 ao_mutex_put(uint8_t *mutex)
89 sprintf (line, "\\%02x", ((int) c) & 0xff);
91 sprintf (line, "%c", c);
93 write(1, line, strlen(line));
96 #define QUEUE_LEN 4096
98 static char input_queue[QUEUE_LEN];
99 int input_head, input_tail;
101 #include <sys/time.h>
107 gettimeofday(&tv, NULL);
108 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
112 check_skytraq_message(char *from, uint8_t *msg, int len)
114 uint16_t encoded_len, encoded_cksum;
119 // fwrite(msg, 1, len, stdout);
121 if (msg[0] != 0xa0 || msg[1] != 0xa2) {
122 printf ("bad header\n");
129 if (msg[len-1] != 0xb3 || msg[len-2] != 0xb0) {
130 printf ("bad trailer\n");
133 encoded_len = (msg[2] << 8) | msg[3];
135 /* printf ("%9d: %3d\n", get_millis(), id); */
136 if (encoded_len != len - 8) {
138 printf ("length mismatch (got %d, wanted %d)\n",
139 len - 8, encoded_len);
142 encoded_cksum = (msg[len - 4] << 8) | msg[len-3];
144 for (i = 4; i < len - 4; i++)
145 cksum = (cksum + msg[i]) & 0x7fff;
146 if (encoded_cksum != cksum) {
147 printf ("cksum mismatch (got %04x wanted %04x)\n",
148 cksum, encoded_cksum);
177 int16_t heading_rate;
183 #define get_u8(u) u = (msg[off]); off+= 1
184 #define get_u16(u) u = (msg[off] << 8) | (msg[off + 1]); off+= 2
185 #define get_u32(u) u = (msg[off] << 24) | (msg[off + 1] << 16) | (msg[off+2] << 8) | (msg[off+3]); off+= 4
208 get_u16(heading_rate);
215 printf ("Geodetic Navigation Data (41):\n");
216 printf ("\tNav valid %04x\n", nav_valid);
217 printf ("\tNav type %04x\n", nav_type);
218 printf ("\tWeek %5d", week);
219 printf (" TOW %9d", tow);
220 printf (" %4d-%2d-%2d %02d:%02d:%07.4f\n",
222 hour, minute, second / 1000.0);
223 printf ("\tsats: %08x\n", sat_list);
224 printf ("\tlat: %g", lat / 1.0e7);
225 printf (" lon: %g", lon / 1.0e7);
226 printf (" alt_ell: %g", alt_ell / 100.0);
227 printf (" alt_msll: %g", alt_msl / 100.0);
228 printf (" datum: %d\n", datum);
229 printf ("\tground speed: %g", sog / 100.0);
230 printf (" course: %g", cog / 100.0);
231 printf (" climb: %g", climb_rate / 100.0);
232 printf (" heading rate: %g\n", heading_rate / 100.0);
233 printf ("\th error: %g", h_error / 100.0);
234 printf (" v error: %g", v_error / 100.0);
235 printf (" t error: %g", t_error / 100.0);
236 printf (" h vel error: %g\n", h_v_error / 100.0);
252 printf ("Measured Tracker Data (4):\n");
253 printf ("GPS week: %d\n", gps_week);
254 printf ("GPS time of week: %d\n", gps_tow);
255 printf ("channels: %d\n", channels);
256 for (j = 0; j < 12; j++) {
257 uint8_t svid, azimuth, elevation;
264 for (k = 0; k < 10; k++) {
267 printf ("Sat %3d:", svid);
268 printf (" aziumuth: %6.1f", azimuth * 1.5);
269 printf (" elevation: %6.1f", elevation * 0.5);
270 printf (" state: 0x%02x", state);
272 for (k = 0; k < 10; k++)
273 printf(" %3d", c_n[k]);
274 if (state & SIRF_SAT_STATE_ACQUIRED)
276 if (state & SIRF_SAT_STATE_CARRIER_PHASE_VALID)
278 if (state & SIRF_SAT_BIT_SYNC_COMPLETE)
280 if (state & SIRF_SAT_SUBFRAME_SYNC_COMPLETE)
282 if (state & SIRF_SAT_CARRIER_PULLIN_COMPLETE)
284 if (state & SIRF_SAT_CODE_LOCKED)
286 if (state & SIRF_SAT_ACQUISITION_FAILED)
288 if (state & SIRF_SAT_EPHEMERIS_AVAILABLE)
296 printf ("%s %4d:", from, encoded_len);
297 for (i = 4; i < len - 4; i++) {
298 if (((i - 4) & 0xf) == 0)
300 printf (" %3d", msg[i]);
306 static uint8_t skytraq_message[4096];
307 static int skytraq_message_len;
308 static uint8_t skytraq_in_message[4096];
309 static int skytraq_in_len;
312 ao_serial_getchar(void)
317 while (input_head == input_tail) {
319 input_tail = read(ao_gps_fd, input_queue, QUEUE_LEN);
320 if (input_tail < 0) {
321 if (errno == EINTR || errno == EAGAIN)
330 c = input_queue[input_head];
331 input_head = (input_head + 1) % QUEUE_LEN;
333 // printf ("c: %02x %c\n", uc, uc);
334 if (skytraq_in_len || uc == '$') {
335 if (skytraq_in_len < 4096)
336 skytraq_in_message[skytraq_in_len++] = uc;
338 check_skytraq_message("recv", skytraq_in_message, skytraq_in_len);
347 ao_serial_putchar(char c)
350 uint8_t uc = (uint8_t) c;
352 if (skytraq_message_len || uc == 0xa0) {
353 if (skytraq_message_len < 4096)
354 skytraq_message[skytraq_message_len++] = uc;
356 check_skytraq_message("send", skytraq_message, skytraq_message_len);
357 skytraq_message_len = 0;
361 i = write(ao_gps_fd, &c, 1);
363 if ((uint8_t) c == 0xb3 || c == '\r') {
364 static const struct timespec delay = {
366 .tv_nsec = 100 * 1000 * 1000
369 // nanosleep(&delay, NULL);
373 if (i < 0 && (errno == EINTR || errno == EAGAIN))
380 #define AO_SERIAL_SPEED_4800 0
381 #define AO_SERIAL_SPEED_9600 1
382 #define AO_SERIAL_SPEED_57600 2
385 ao_serial_set_speed(uint8_t speed)
388 struct termios termios;
391 tcgetattr(fd, &termios);
393 case AO_SERIAL_SPEED_4800:
394 cfsetspeed(&termios, B4800);
396 case AO_SERIAL_SPEED_9600:
397 cfsetspeed(&termios, B38400);
399 case AO_SERIAL_SPEED_57600:
400 cfsetspeed(&termios, B57600);
403 tcsetattr(fd, TCSAFLUSH, &termios);
404 tcflush(fd, TCIFLUSH);
407 #include "ao_gps_print.c"
408 #include "ao_gps_skytraq.c"
411 ao_dump_state(void *wchan)
415 if (wchan == &ao_gps_data)
416 ao_gps_print(&ao_gps_data);
418 ao_gps_tracking_print(&ao_gps_tracking_data);
424 ao_gps_open(const char *tty)
426 struct termios termios;
429 fd = open (tty, O_RDWR);
433 tcgetattr(fd, &termios);
435 cfsetspeed(&termios, B4800);
436 tcsetattr(fd, TCSAFLUSH, &termios);
439 tcflush(fd, TCIFLUSH);
445 static const struct option options[] = {
446 { .name = "tty", .has_arg = 1, .val = 'T' },
450 static void usage(char *program)
452 fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
457 main (int argc, char **argv)
459 char *tty = "/dev/ttyUSB0";
462 while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
472 ao_gps_fd = ao_gps_open(tty);