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_NEW_DATA 1
30 #define AO_GPS_NEW_TRACKING 2
32 #define AO_GPS_VALID (1 << 4)
33 #define AO_GPS_RUNNING (1 << 5)
34 #define AO_GPS_DATE_VALID (1 << 6)
35 #define AO_GPS_COURSE_VALID (1 << 7)
37 struct ao_telemetry_location {
45 int32_t latitude; /* degrees * 10⁷ */
46 int32_t longitude; /* degrees * 10⁷ */
47 int16_t altitude; /* m */
48 uint16_t ground_speed; /* cm/s */
49 uint8_t course; /* degrees / 2 */
50 uint8_t pdop; /* * 5 */
51 uint8_t hdop; /* * 5 */
52 uint8_t vdop; /* * 5 */
53 int16_t climb_rate; /* cm/s */
54 uint16_t h_error; /* m */
55 uint16_t v_error; /* m */
58 #define UBLOX_SAT_STATE_ACQUIRED (1 << 0)
59 #define UBLOX_SAT_STATE_CARRIER_PHASE_VALID (1 << 1)
60 #define UBLOX_SAT_BIT_SYNC_COMPLETE (1 << 2)
61 #define UBLOX_SAT_SUBFRAME_SYNC_COMPLETE (1 << 3)
62 #define UBLOX_SAT_CARRIER_PULLIN_COMPLETE (1 << 4)
63 #define UBLOX_SAT_CODE_LOCKED (1 << 5)
64 #define UBLOX_SAT_ACQUISITION_FAILED (1 << 6)
65 #define UBLOX_SAT_EPHEMERIS_AVAILABLE (1 << 7)
67 struct ao_telemetry_satellite_info {
72 #define AO_TELEMETRY_SATELLITE_MAX_SAT 12
74 struct ao_telemetry_satellite {
76 struct ao_telemetry_satellite_info sats[AO_TELEMETRY_SATELLITE_MAX_SAT];
79 #define ao_gps_orig ao_telemetry_location
80 #define ao_gps_tracking_orig ao_telemetry_satellite
81 #define ao_gps_sat_orig ao_telemetry_satellite_info
83 extern __xdata struct ao_telemetry_location ao_gps_data;
84 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
89 ao_mutex_get(uint8_t *mutex)
94 ao_mutex_put(uint8_t *mutex)
99 static FILE *ao_gps_file;
109 sprintf (line, "\n");
111 sprintf (line, "\\%02x", ((int) c) & 0xff);
113 sprintf (line, "%c", c);
115 write(1, line, strlen(line));
119 #include <sys/time.h>
125 gettimeofday(&tv, NULL);
126 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
129 static uint8_t in_message[4096];
131 static uint16_t recv_len;
133 static void check_ublox_message(char *which, uint8_t *msg);
142 i = getc(ao_gps_file);
149 if (in_len || uc == 0xb5) {
150 in_message[in_len++] = c;
152 recv_len = in_message[4] | (in_message[5] << 8);
153 } else if (in_len > 6 && in_len == recv_len + 8) {
154 check_ublox_message("recv", in_message + 2);
162 #define MESSAGE_LEN 4096
164 static uint8_t message[MESSAGE_LEN];
165 static int message_len;
166 static uint16_t send_len;
169 ao_gps_putchar(char c)
172 uint8_t uc = (uint8_t) c;
174 if (message_len || uc == 0xb5) {
175 if (message_len < MESSAGE_LEN)
176 message[message_len++] = uc;
177 if (message_len == 6) {
178 send_len = message[4] | (message[5] << 8);
179 } else if (message_len > 6 && message_len == send_len + 8) {
180 check_ublox_message("send", message + 2);
186 i = write(ao_gps_fd, &c, 1);
189 if (i < 0 && (errno == EINTR || errno == EAGAIN))
196 #define AO_SERIAL_SPEED_4800 0
197 #define AO_SERIAL_SPEED_9600 1
198 #define AO_SERIAL_SPEED_57600 2
199 #define AO_SERIAL_SPEED_115200 3
202 ao_gps_set_speed(uint8_t speed)
205 struct termios termios;
207 printf ("\t\tset speed %d\n", speed);
209 tcgetattr(fd, &termios);
211 case AO_SERIAL_SPEED_4800:
212 cfsetspeed(&termios, B4800);
214 case AO_SERIAL_SPEED_9600:
215 cfsetspeed(&termios, B9600);
217 case AO_SERIAL_SPEED_57600:
218 cfsetspeed(&termios, B57600);
220 case AO_SERIAL_SPEED_115200:
221 cfsetspeed(&termios, B115200);
224 tcsetattr(fd, TCSAFLUSH, &termios);
225 tcflush(fd, TCIFLUSH);
230 uint8_t ao_task_minimize_latency;
232 #define ao_usb_getchar() 0
234 #include "ao_gps_print.c"
235 #include "ao_gps_show.c"
236 #include "ao_gps_ublox.c"
239 check_ublox_message(char *which, uint8_t *msg)
241 uint8_t class = msg[0];
243 uint16_t len = msg[2] | (msg[3] << 8);
245 struct ao_ublox_cksum cksum_msg = { .a = msg[4 + len],
246 .b = msg[4 + len + 1] };
247 struct ao_ublox_cksum cksum= { 0, 0 };
249 for (i = 0; i < 4 + len; i++) {
250 add_cksum(&cksum, msg[i]);
252 if (cksum.a != cksum_msg.a || cksum.b != cksum_msg.b) {
253 printf ("\t%s: cksum mismatch %02x,%02x != %02x,%02x\n",
264 case UBLOX_NAV_DOP: ;
265 struct ublox_nav_dop *nav_dop = (void *) msg;
266 printf ("\tnav-dop iTOW %9u gDOP %5u dDOP %5u tDOP %5u vDOP %5u hDOP %5u nDOP %5u eDOP %5u\n",
276 case UBLOX_NAV_POSLLH: ;
277 struct ublox_nav_posllh *nav_posllh = (void *) msg;
278 printf ("\tnav-posllh iTOW %9u lon %12.7f lat %12.7f height %10.3f hMSL %10.3f hAcc %10.3f vAcc %10.3f\n",
280 nav_posllh->lon / 1e7,
281 nav_posllh->lat / 1e7,
282 nav_posllh->height / 1e3,
283 nav_posllh->hmsl / 1e3,
284 nav_posllh->hacc / 1e3,
285 nav_posllh->vacc / 1e3);
287 case UBLOX_NAV_SOL: ;
288 struct ublox_nav_sol *nav_sol = (struct ublox_nav_sol *) msg;
289 printf ("\tnav-sol iTOW %9u fTOW %9d week %5d gpsFix %2d flags %02x\n",
290 nav_sol->itow, nav_sol->ftow, nav_sol->week,
291 nav_sol->gpsfix, nav_sol->flags);
293 case UBLOX_NAV_SVINFO: ;
294 struct ublox_nav_svinfo *nav_svinfo = (struct ublox_nav_svinfo *) msg;
295 printf ("\tnav-svinfo iTOW %9u numCH %3d globalFlags %02x\n",
296 nav_svinfo->itow, nav_svinfo->numch, nav_svinfo->globalflags);
298 for (i = 0; i < nav_svinfo->numch; i++) {
299 struct ublox_nav_svinfo_block *nav_svinfo_block = (void *) (msg + 12 + 12 * i);
300 printf ("\t\tchn %3u svid %3u flags %02x quality %3u cno %3u elev %3d azim %6d prRes %9d\n",
301 nav_svinfo_block->chn,
302 nav_svinfo_block->svid,
303 nav_svinfo_block->flags,
304 nav_svinfo_block->quality,
305 nav_svinfo_block->cno,
306 nav_svinfo_block->elev,
307 nav_svinfo_block->azim,
308 nav_svinfo_block->prres);
311 case UBLOX_NAV_VELNED: ;
312 struct ublox_nav_velned *nav_velned = (void *) msg;
313 printf ("\tnav-velned iTOW %9u velN %10.2f velE %10.2f velD %10.2f speed %10.2f gSpeed %10.2f heading %10.5f sAcc %10.2f cAcc %10.5f\n",
315 nav_velned->veln / 1e2,
316 nav_velned->vele / 1e2,
317 nav_velned->veld / 1e2,
318 nav_velned->speed / 1e2,
319 nav_velned->gspeed / 1e2,
320 nav_velned->heading / 1e5,
321 nav_velned->sacc / 1e5,
322 nav_velned->cacc / 1e6);
324 case UBLOX_NAV_TIMEUTC:;
325 struct ublox_nav_timeutc *nav_timeutc = (void *) msg;
326 printf ("\tnav-timeutc iTOW %9u tAcc %5u nano %5d %4u-%2d-%2d %2d:%02d:%02d flags %02x\n",
342 printf ("\t%s: class %02x id %02x len %d:", which, class & 0xff, id & 0xff, len & 0xffff);
343 for (i = 0; i < len; i++)
344 printf (" %02x", msg[4 + i]);
345 printf (" cksum %02x %02x", cksum_msg.a & 0xff, cksum_msg.b & 0xff);
351 ao_dump_state(void *wchan)
353 if (wchan == &ao_gps_new)
359 ao_gps_open(const char *tty)
361 struct termios termios;
364 fd = open (tty, O_RDWR);
368 tcgetattr(fd, &termios);
370 cfsetspeed(&termios, B4800);
371 tcsetattr(fd, TCSAFLUSH, &termios);
374 tcflush(fd, TCIFLUSH);
380 static const struct option options[] = {
381 { .name = "tty", .has_arg = 1, .val = 'T' },
385 static void usage(char *program)
387 fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
392 main (int argc, char **argv)
394 char *tty = "/dev/ttyUSB0";
397 while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
407 ao_gps_fd = ao_gps_open(tty);
412 ao_gps_file = fdopen(ao_gps_fd, "r");