altos: Switch all tick variables to AO_TICK_TYPE/AO_TICK_SIGNED
[fw/altos] / src / drivers / ao_gps_ublox.c
1 /*
2  * Copyright © 2013 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 #ifndef AO_GPS_TEST
20 #include "ao.h"
21 #endif
22
23 #include "ao_gps_ublox.h"
24
25 #define AO_UBLOX_DEBUG 0
26
27 #include <stdarg.h>
28
29 uint8_t ao_gps_new;
30 uint8_t ao_gps_mutex;
31 AO_TICK_TYPE ao_gps_tick;
32 struct ao_telemetry_location    ao_gps_data;
33 struct ao_telemetry_satellite   ao_gps_tracking_data;
34
35 #undef AO_SERIAL_SPEED_UBLOX
36
37 #ifndef AO_SERIAL_SPEED_UBLOX
38 #define AO_SERIAL_SPEED_UBLOX AO_SERIAL_SPEED_9600
39 #endif
40
41 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_57600
42 #define SERIAL_SPEED_STRING     "57600"
43 #define SERIAL_SPEED_CHECKSUM   "2d"
44 #endif
45 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_19200
46 #define SERIAL_SPEED_STRING     "19200"
47 #define SERIAL_SPEED_CHECKSUM   "23"
48 #endif
49 #if AO_SERIAL_SPEED_UBLOX == AO_SERIAL_SPEED_9600
50 #define SERIAL_SPEED_STRING     "9600"
51 #define SERIAL_SPEED_CHECKSUM   "16"
52 #endif
53
54 static const char ao_gps_set_nmea[] =
55         "\r\n$PUBX,41,1,3,1," SERIAL_SPEED_STRING ",0*" SERIAL_SPEED_CHECKSUM "\r\n";
56
57 struct ao_ublox_cksum {
58         uint8_t a, b;
59 };
60
61 static struct ao_ublox_cksum ao_ublox_cksum;
62 static uint16_t ao_ublox_len;
63
64 #if AO_UBLOX_DEBUG
65
66 static uint8_t ao_gps_dbg_enable;
67
68 #define DBG_PROTO       1
69 #define DBG_CHAR        2
70 #define DBG_INIT        4
71
72 static void ao_gps_dbg(int level, char *format, ...) {
73         va_list a;
74
75         if (level & ao_gps_dbg_enable) {
76                 va_start(a, format);
77                 vprintf(format, a);
78                 va_end(a);
79                 flush();
80         }
81 }
82
83 #else
84 #define ao_gps_dbg(fmt, ...)
85 #endif
86
87 static inline uint8_t ao_ublox_byte(void) {
88         uint8_t c = (uint8_t) ao_gps_getchar();
89         ao_gps_dbg(DBG_CHAR, " %02x", c);
90         return c;
91 }
92
93 static inline void add_cksum(struct ao_ublox_cksum *cksum, uint8_t c)
94 {
95         cksum->a += c;
96         cksum->b += cksum->a;
97 }
98
99 static void ao_ublox_init_cksum(void)
100 {
101         ao_ublox_cksum.a = ao_ublox_cksum.b = 0;
102 }
103
104 static void ao_ublox_put_u8(uint8_t c)
105 {
106         add_cksum(&ao_ublox_cksum, c);
107         ao_gps_dbg(DBG_CHAR, " (%02x)", c);
108         ao_gps_putchar(c);
109 }
110
111 static void ao_ublox_put_i8(int8_t c)
112 {
113         ao_ublox_put_u8((uint8_t) c);
114 }
115
116 static void ao_ublox_put_u16(uint16_t c)
117 {
118         ao_ublox_put_u8(c);
119         ao_ublox_put_u8(c>>8);
120 }
121
122 #if 0
123 static void ao_ublox_put_i16(int16_t c)
124 {
125         ao_ublox_put_u16((uint16_t) c);
126 }
127 #endif
128
129 static void ao_ublox_put_u32(uint32_t c)
130 {
131         ao_ublox_put_u8(c);
132         ao_ublox_put_u8(c>>8);
133         ao_ublox_put_u8(c>>16);
134         ao_ublox_put_u8(c>>24);
135 }
136
137 static void ao_ublox_put_i32(int32_t c)
138 {
139         ao_ublox_put_u32((uint32_t) c);
140 }
141
142 static uint8_t header_byte(void)
143 {
144         uint8_t c = ao_ublox_byte();
145         add_cksum(&ao_ublox_cksum, c);
146         return c;
147 }
148
149 static uint8_t data_byte(void)
150 {
151         --ao_ublox_len;
152         return header_byte();
153 }
154
155 static char *ublox_target;
156
157 static void ublox_u16(uint8_t offset)
158 {
159         uint16_t *ptr = (uint16_t *) (void *) (ublox_target + offset);
160         uint16_t val;
161
162         val = data_byte();
163         val |= data_byte () << 8;
164         *ptr = val;
165 }
166
167 static void ublox_u8(uint8_t offset)
168 {
169         uint8_t *ptr = (uint8_t *) (ublox_target + offset);
170         uint8_t val;
171
172         val = data_byte ();
173         *ptr = val;
174 }
175
176 static void ublox_u32(uint8_t offset) 
177 {
178         uint32_t *ptr = (uint32_t *) (void *) (ublox_target + offset);
179         uint32_t val;
180
181         val = ((uint32_t) data_byte ());
182         val |= ((uint32_t) data_byte ()) << 8;
183         val |= ((uint32_t) data_byte ()) << 16;
184         val |= ((uint32_t) data_byte ()) << 24;
185         *ptr = val;
186 }
187
188 static void ublox_discard(uint8_t len)
189 {
190         while (len--)
191                 data_byte();
192 }
193
194 #define UBLOX_END       0
195 #define UBLOX_DISCARD   1
196 #define UBLOX_U8        2
197 #define UBLOX_U16       3
198 #define UBLOX_U32       4
199
200 struct ublox_packet_parse {
201         uint8_t type;
202         uint8_t offset;
203 };
204
205 static void
206 ao_ublox_parse(void *target, const struct ublox_packet_parse *parse) 
207 {
208         uint8_t i, offset;
209
210         ublox_target = target;
211         for (i = 0; ; i++) {
212                 offset = parse[i].offset;
213                 switch (parse[i].type) {
214                 case UBLOX_END:
215                         return;
216                 case UBLOX_DISCARD:
217                         ublox_discard(offset);
218                         break;
219                 case UBLOX_U8:
220                         ublox_u8(offset);
221                         break;
222                 case UBLOX_U16:
223                         ublox_u16(offset);
224                         break;
225                 case UBLOX_U32:
226                         ublox_u32(offset);
227                         break;
228                 }
229         }
230         ao_gps_dbg(DBG_PROTO, "\n");
231 }
232
233 /*
234  * NAV-DOP message parsing
235  */
236
237 static struct nav_dop {
238         uint16_t        pdop;
239         uint16_t        hdop;
240         uint16_t        vdop;
241 } nav_dop;
242
243 static const struct ublox_packet_parse nav_dop_packet[] = {
244         { UBLOX_DISCARD, 6 },                                   /* 0 GPS Millisecond Time of Week, gDOP */
245         { UBLOX_U16, offsetof(struct nav_dop, pdop) },          /* 6 pDOP */
246         { UBLOX_DISCARD, 2 },                                   /* 8 tDOP */
247         { UBLOX_U16, offsetof(struct nav_dop, vdop) },          /* 10 vDOP */
248         { UBLOX_U16, offsetof(struct nav_dop, hdop) },          /* 12 hDOP */
249         { UBLOX_DISCARD, 4 },                                   /* 14 nDOP, eDOP */
250         { UBLOX_END, 0 }
251 };
252
253 static void
254 ao_ublox_parse_nav_dop(void)
255 {
256         ao_ublox_parse(&nav_dop, nav_dop_packet);
257 }
258
259 /*
260  * NAV-POSLLH message parsing
261  */
262 static struct nav_posllh {
263         int32_t         lat;
264         int32_t         lon;
265         int32_t         alt_msl;
266 } nav_posllh;
267
268 static const struct ublox_packet_parse nav_posllh_packet[] = {
269         { UBLOX_DISCARD, 4 },                                           /* 0 GPS Millisecond Time of Week */
270         { UBLOX_U32, offsetof(struct nav_posllh, lon) },                /* 4 Longitude */
271         { UBLOX_U32, offsetof(struct nav_posllh, lat) },                /* 8 Latitude */
272         { UBLOX_DISCARD, 4 },                                           /* 12 Height above Ellipsoid */
273         { UBLOX_U32, offsetof(struct nav_posllh, alt_msl) },            /* 16 Height above mean sea level */
274         { UBLOX_DISCARD, 8 },                                           /* 20 hAcc, vAcc */
275         { UBLOX_END, 0 },                                               /* 28 */
276 };
277
278 static void
279 ao_ublox_parse_nav_posllh(void)
280 {
281         ao_ublox_parse(&nav_posllh, nav_posllh_packet);
282 }
283
284 /*
285  * NAV-SOL message parsing
286  */
287 static struct nav_sol {
288         uint8_t         gps_fix;
289         uint8_t         flags;
290         uint8_t         nsat;
291 } nav_sol;
292
293 static const struct ublox_packet_parse nav_sol_packet[] = {
294         { UBLOX_DISCARD, 10 },                                          /* 0 iTOW, fTOW, week */
295         { UBLOX_U8, offsetof(struct nav_sol, gps_fix) },                /* 10 gpsFix */
296         { UBLOX_U8, offsetof(struct nav_sol, flags) },                  /* 11 flags */
297         { UBLOX_DISCARD, 35 },                                          /* 12 ecefX, ecefY, ecefZ, pAcc, ecefVX, ecefVY, ecefVZ, sAcc, pDOP, reserved1 */
298         { UBLOX_U8, offsetof(struct nav_sol, nsat) },                   /* 47 numSV */
299         { UBLOX_DISCARD, 4 },                                           /* 48 reserved2 */
300         { UBLOX_END, 0 }
301 };
302
303 #define NAV_SOL_FLAGS_GPSFIXOK          0
304 #define NAV_SOL_FLAGS_DIFFSOLN          1
305 #define NAV_SOL_FLAGS_WKNSET            2
306 #define NAV_SOL_FLAGS_TOWSET            3
307
308 static void
309 ao_ublox_parse_nav_sol(void)
310 {
311         ao_ublox_parse(&nav_sol, nav_sol_packet);
312 }
313
314 /*
315  * NAV-SVINFO message parsing
316  */
317
318 static struct nav_svinfo {
319         uint8_t num_ch;
320         uint8_t flags;
321 } nav_svinfo;
322
323 static const struct ublox_packet_parse nav_svinfo_packet[] = {
324         { UBLOX_DISCARD, 4 },                                           /* 0 iTOW */
325         { UBLOX_U8, offsetof(struct nav_svinfo, num_ch) },              /* 4 numCh */
326         { UBLOX_U8, offsetof(struct nav_svinfo, flags) },               /* 5 globalFlags */
327         { UBLOX_DISCARD, 2 },                                           /* 6 reserved2 */
328         { UBLOX_END, 0 }
329 };
330
331 #define NAV_SVINFO_MAX_SAT      16
332
333 static struct nav_svinfo_sat {
334         uint8_t chn;
335         uint8_t svid;
336         uint8_t flags;
337         uint8_t quality;
338         uint8_t cno;
339 } nav_svinfo_sat[NAV_SVINFO_MAX_SAT];
340
341 static uint8_t  nav_svinfo_nsat;
342
343 static const struct ublox_packet_parse nav_svinfo_sat_packet[] = {
344         { UBLOX_U8, offsetof(struct nav_svinfo_sat, chn) },             /* 8 + 12*N chn */
345         { UBLOX_U8, offsetof(struct nav_svinfo_sat, svid) },            /* 9 + 12*N svid */
346         { UBLOX_U8, offsetof(struct nav_svinfo_sat, flags) },           /* 10 + 12*N flags */
347         { UBLOX_U8, offsetof(struct nav_svinfo_sat, quality) },         /* 11 + 12*N quality */
348         { UBLOX_U8, offsetof(struct nav_svinfo_sat, cno) },             /* 12 + 12*N cno */
349         { UBLOX_DISCARD, 7 },                                           /* 13 + 12*N elev, azim, prRes */
350         { UBLOX_END, 0 }
351 };
352
353 #define NAV_SVINFO_SAT_FLAGS_SVUSED             0
354 #define NAV_SVINFO_SAT_FLAGS_DIFFCORR           1
355 #define NAV_SVINFO_SAT_FLAGS_ORBITAVAIL         2
356 #define NAV_SVINFO_SAT_FLAGS_ORBITEPH           3
357 #define NAV_SVINFO_SAT_FLAGS_UNHEALTHY          4
358 #define NAV_SVINFO_SAT_FLAGS_ORBITALM           5
359 #define NAV_SVINFO_SAT_FLAGS_ORBITAOP           6
360 #define NAV_SVINFO_SAT_FLAGS_SMOOTHED           7
361
362 #define NAV_SVINFO_SAT_QUALITY_IDLE             0
363 #define NAV_SVINFO_SAT_QUALITY_SEARCHING        1
364 #define NAV_SVINFO_SAT_QUALITY_ACQUIRED         2
365 #define NAV_SVINFO_SAT_QUALITY_UNUSABLE         3
366 #define NAV_SVINFO_SAT_QUALITY_LOCKED           4
367 #define NAV_SVINFO_SAT_QUALITY_RUNNING          5
368
369 static void
370 ao_ublox_parse_nav_svinfo(void)
371 {
372         uint8_t nsat;
373         nav_svinfo_nsat = 0;
374
375         ao_ublox_parse(&nav_svinfo, nav_svinfo_packet);
376         for (nsat = 0; nsat < nav_svinfo.num_ch && ao_ublox_len >= 12; nsat++) {
377                 if (nsat < NAV_SVINFO_MAX_SAT) {
378                         ao_ublox_parse(&nav_svinfo_sat[nav_svinfo_nsat++], nav_svinfo_sat_packet);
379                 } else {
380                         ublox_discard(12);
381                 }
382         }
383 #if AO_UBLOX_DEBUG
384         ao_gps_dbg(DBG_PROTO, "svinfo num_ch %d flags %02x\n", nav_svinfo.num_ch, nav_svinfo.flags);
385         for (nsat = 0; nsat < nav_svinfo.num_ch; nsat++)
386                 ao_gps_dbg(DBG_PROTO, "\t%d: chn %d svid %d flags %02x quality %d cno %d\n",
387                             nsat,
388                             nav_svinfo_sat[nsat].chn,
389                             nav_svinfo_sat[nsat].svid,
390                             nav_svinfo_sat[nsat].flags,
391                             nav_svinfo_sat[nsat].quality,
392                             nav_svinfo_sat[nsat].cno);
393 #endif
394 }
395
396 /*
397  * NAV-TIMEUTC message parsing
398  */
399 static struct nav_timeutc {
400         uint16_t        year;
401         uint8_t         month;
402         uint8_t         day;
403         uint8_t         hour;
404         uint8_t         min;
405         uint8_t         sec;
406         uint8_t         valid;
407 } nav_timeutc;
408
409 #define NAV_TIMEUTC_VALID_TOW   0
410 #define NAV_TIMEUTC_VALID_WKN   1
411 #define NAV_TIMEUTC_VALID_UTC   2
412
413 static const struct ublox_packet_parse nav_timeutc_packet[] = {
414         { UBLOX_DISCARD, 12 },                                          /* 0 iTOW, tAcc, nano */
415         { UBLOX_U16, offsetof(struct nav_timeutc, year) },              /* 12 year */
416         { UBLOX_U8, offsetof(struct nav_timeutc, month) },              /* 14 month */
417         { UBLOX_U8, offsetof(struct nav_timeutc, day) },                /* 15 day */
418         { UBLOX_U8, offsetof(struct nav_timeutc, hour) },               /* 16 hour */
419         { UBLOX_U8, offsetof(struct nav_timeutc, min) },                /* 17 min */
420         { UBLOX_U8, offsetof(struct nav_timeutc, sec) },                /* 18 sec */
421         { UBLOX_U8, offsetof(struct nav_timeutc, valid) },              /* 19 valid */
422         { UBLOX_END, 0 }
423 };
424
425 static void
426 ao_ublox_parse_nav_timeutc(void)
427 {
428         ao_ublox_parse(&nav_timeutc, nav_timeutc_packet);
429 }
430
431 /*
432  * NAV-VELNED message parsing
433  */
434
435 static struct nav_velned {
436         int32_t         vel_d;
437         uint32_t        g_speed;
438         int32_t         heading;
439 } nav_velned;
440
441 static const struct ublox_packet_parse nav_velned_packet[] = {
442         { UBLOX_DISCARD, 12 },                                          /* 0 iTOW, velN, velE */
443         { UBLOX_U32, offsetof(struct nav_velned, vel_d) },              /* 12 velD */
444         { UBLOX_DISCARD, 4 },                                           /* 16 speed */
445         { UBLOX_U32, offsetof(struct nav_velned, g_speed) },            /* 20 gSpeed */
446         { UBLOX_U32, offsetof(struct nav_velned, heading) },            /* 24 heading */
447         { UBLOX_DISCARD, 8 },                                           /* 28 sAcc, cAcc */
448         { UBLOX_END, 0 }
449 };
450
451 static void
452 ao_ublox_parse_nav_velned(void)
453 {
454         ao_ublox_parse(&nav_velned, nav_velned_packet);
455 }
456
457 /*
458  * Set the protocol mode and baud rate
459  */
460
461 static void
462 ao_gps_delay(void)
463 {
464         uint8_t i;
465
466         /*
467          * A bunch of nulls so the start bit
468          * is clear
469          */
470
471         for (i = 0; i < 64; i++)
472                 ao_gps_putchar(0x00);
473 }
474
475 static void
476 ao_gps_setup(void)
477 {
478         uint8_t i, k;
479
480         ao_delay(AO_SEC_TO_TICKS(3));
481
482         ao_gps_dbg(DBG_INIT, "Set speed 9600\n");
483         ao_gps_set_speed(AO_SERIAL_SPEED_9600);
484
485         /*
486          * Send the baud-rate setting and protocol-setting
487          * command three times
488          */
489         for (k = 0; k < 3; k++) {
490                 ao_gps_delay();
491
492                 ao_gps_dbg(DBG_INIT, "Send initial setting\n");
493                 for (i = 0; i < sizeof (ao_gps_set_nmea); i++)
494                         ao_gps_putchar(ao_gps_set_nmea[i]);
495         }
496
497         ao_gps_delay();
498
499 #if AO_SERIAL_SPEED_UBLOX != AO_SERIAL_SPEED_9600
500         ao_gps_dbg(DBG_INIT, "Set speed high\n");
501         /*
502          * Increase the baud rate
503          */
504         ao_gps_set_speed(AO_SERIAL_SPEED_UBLOX);
505 #endif
506
507         ao_gps_delay();
508 }
509
510 static void
511 ao_ublox_putstart(uint8_t class, uint8_t id, uint16_t len)
512 {
513         ao_ublox_init_cksum();
514         ao_gps_putchar(0xb5);
515         ao_gps_putchar(0x62);
516         ao_ublox_put_u8(class);
517         ao_ublox_put_u8(id);
518         ao_ublox_put_u8(len);
519         ao_ublox_put_u8(len >> 8);
520 }
521
522 static void
523 ao_ublox_putend(void)
524 {
525         ao_gps_putchar(ao_ublox_cksum.a);
526         ao_gps_putchar(ao_ublox_cksum.b);
527 }
528
529 static void
530 ao_ublox_set_message_rate(uint8_t class, uint8_t msgid, uint8_t rate)
531 {
532         ao_ublox_putstart(0x06, 0x01, 3);
533         ao_ublox_put_u8(class);
534         ao_ublox_put_u8(msgid);
535         ao_ublox_put_u8(rate);
536         ao_ublox_putend();
537 }
538
539 static void
540 ao_ublox_set_navigation_settings(uint16_t mask,
541                                  uint8_t dyn_model,
542                                  uint8_t fix_mode,
543                                  int32_t fixed_alt,
544                                  uint32_t fixed_alt_var,
545                                  int8_t min_elev,
546                                  uint8_t dr_limit,
547                                  uint16_t pdop,
548                                  uint16_t tdop,
549                                  uint16_t pacc,
550                                  uint16_t tacc,
551                                  uint8_t static_hold_thresh,
552                                  uint8_t dgps_time_out)
553 {
554         ao_ublox_putstart(UBLOX_CFG, UBLOX_CFG_NAV5, 36);
555         ao_ublox_put_u16(mask);
556         ao_ublox_put_u8(dyn_model);
557         ao_ublox_put_u8(fix_mode);
558         ao_ublox_put_i32(fixed_alt);
559         ao_ublox_put_u32(fixed_alt_var);
560         ao_ublox_put_i8(min_elev);
561         ao_ublox_put_u8(dr_limit);
562         ao_ublox_put_u16(pdop);
563         ao_ublox_put_u16(tdop);
564         ao_ublox_put_u16(pacc);
565         ao_ublox_put_u16(tacc);
566         ao_ublox_put_u8(static_hold_thresh);
567         ao_ublox_put_u8(dgps_time_out);
568         ao_ublox_put_u32(0);
569         ao_ublox_put_u32(0);
570         ao_ublox_put_u32(0);
571         ao_ublox_putend();
572 }
573
574
575 /*
576  * Disable all MON message
577  */
578 static const uint8_t ublox_disable_mon[] = {
579         0x0b, 0x09, 0x02, 0x06, 0x07, 0x21, 0x08, 0x04
580 };
581
582 /*
583  * Disable all NAV messages. The desired
584  * ones will be explicitly re-enabled
585  */
586
587 static const uint8_t ublox_disable_nav[] = {
588         0x60, 0x22, 0x31, 0x04, 0x40, 0x01, 0x02, 0x32,
589         0x06, 0x03, 0x30, 0x20, 0x21, 0x11, 0x12
590 };
591
592 /*
593  * Enable enough messages to get all of the data we want
594  */
595 static const uint8_t ublox_enable_nav[] = {
596         UBLOX_NAV_DOP,
597         UBLOX_NAV_POSLLH,
598         UBLOX_NAV_SOL,
599         UBLOX_NAV_SVINFO,
600         UBLOX_NAV_VELNED,
601         UBLOX_NAV_TIMEUTC
602 };
603
604 void
605 ao_gps_set_rate(uint8_t rate)
606 {
607         uint8_t i;
608         for (i = 0; i < sizeof (ublox_enable_nav); i++)
609                 ao_ublox_set_message_rate(UBLOX_NAV, ublox_enable_nav[i], rate);
610 }
611
612 void
613 ao_gps(void) 
614 {
615         uint8_t                 class, id;
616         struct ao_ublox_cksum   cksum;
617         uint8_t                 i;
618         AO_TICK_TYPE            packet_start_tick;
619         AO_TICK_TYPE            solution_tick = 0;
620
621         ao_gps_setup();
622
623         /* Disable all messages */
624         for (i = 0; i < sizeof (ublox_disable_mon); i++)
625                 ao_ublox_set_message_rate(0x0a, ublox_disable_mon[i], 0);
626         for (i = 0; i < sizeof (ublox_disable_nav); i++)
627                 ao_ublox_set_message_rate(UBLOX_NAV, ublox_disable_nav[i], 0);
628
629         /* Enable all of the messages we want */
630         ao_gps_set_rate(1);
631
632         ao_ublox_set_navigation_settings((1 << UBLOX_CFG_NAV5_MASK_DYN) | (1 << UBLOX_CFG_NAV5_MASK_FIXMODE),
633                                          UBLOX_CFG_NAV5_DYNMODEL_AIRBORNE_4G,
634                                          UBLOX_CFG_NAV5_FIXMODE_3D,
635                                          0,
636                                          0,
637                                          0,
638                                          0,
639                                          0,
640                                          0,
641                                          0,
642                                          0,
643                                          0,
644                                          0);
645
646         for (;;) {
647                 /* Locate the begining of the next record */
648                 while (ao_ublox_byte() != (uint8_t) 0xb5)
649                         ;
650                 packet_start_tick = ao_tick_count;
651
652                 if (ao_ublox_byte() != (uint8_t) 0x62)
653                         continue;
654
655                 ao_ublox_init_cksum();
656
657                 class = header_byte();
658                 id = header_byte();
659
660                 /* Length */
661                 ao_ublox_len = header_byte();
662                 ao_ublox_len |= header_byte() << 8;
663
664                 ao_gps_dbg(DBG_PROTO, "%6u class %02x id %02x len %d\n", packet_start_tick, class, id, ao_ublox_len);
665
666                 if (ao_ublox_len > 1023)
667                         continue;
668
669                 switch (class) {
670                 case UBLOX_NAV:
671                         switch (id) {
672                         case UBLOX_NAV_DOP:
673                                 if (ao_ublox_len != 18)
674                                         break;
675                                 ao_ublox_parse_nav_dop();
676                                 break;
677                         case UBLOX_NAV_POSLLH:
678                                 if (ao_ublox_len != 28)
679                                         break;
680                                 ao_ublox_parse_nav_posllh();
681                                 break;
682                         case UBLOX_NAV_SOL:
683                                 if (ao_ublox_len != 52)
684                                         break;
685                                 ao_ublox_parse_nav_sol();
686                                 solution_tick = packet_start_tick;
687                                 break;
688                         case UBLOX_NAV_SVINFO:
689                                 if (ao_ublox_len < 8)
690                                         break;
691                                 ao_ublox_parse_nav_svinfo();
692                                 break;
693                         case UBLOX_NAV_VELNED:
694                                 if (ao_ublox_len != 36)
695                                         break;
696                                 ao_ublox_parse_nav_velned();
697                                 break;
698                         case UBLOX_NAV_TIMEUTC:
699                                 if (ao_ublox_len != 20)
700                                         break;
701                                 ao_ublox_parse_nav_timeutc();
702                                 break;
703                         }
704                         break;
705                 }
706
707                 if (ao_ublox_len != 0) {
708                         ao_gps_dbg(DBG_PROTO, "len left %d\n", ao_ublox_len);
709                         continue;
710                 }
711
712                 /* verify checksum and end sequence */
713                 cksum.a = ao_ublox_byte();
714                 cksum.b = ao_ublox_byte();
715                 if (ao_ublox_cksum.a != cksum.a || ao_ublox_cksum.b != cksum.b)
716                         continue;
717
718                 switch (class) {
719                 case UBLOX_NAV:
720                         switch (id) {
721                         case UBLOX_NAV_TIMEUTC:
722                                 ao_mutex_get(&ao_gps_mutex);
723                                 ao_gps_tick = solution_tick;
724
725                                 ao_gps_data.flags = 0;
726                                 ao_gps_data.flags |= AO_GPS_RUNNING;
727                                 if (nav_sol.gps_fix & (1 << NAV_SOL_FLAGS_GPSFIXOK)) {
728                                         uint8_t nsat = nav_sol.nsat;
729                                         ao_gps_data.flags |= AO_GPS_VALID | AO_GPS_COURSE_VALID;
730                                         if (nsat > 15)
731                                                 nsat = 15;
732                                         ao_gps_data.flags |= nsat;
733                                 }
734                                 if (nav_timeutc.valid & (1 << NAV_TIMEUTC_VALID_UTC))
735                                         ao_gps_data.flags |= AO_GPS_DATE_VALID;
736
737                                 AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_data, nav_posllh.alt_msl / 1000);
738                                 ao_gps_data.latitude = nav_posllh.lat;
739                                 ao_gps_data.longitude = nav_posllh.lon;
740
741                                 ao_gps_data.year = nav_timeutc.year - 2000;
742                                 ao_gps_data.month = nav_timeutc.month;
743                                 ao_gps_data.day = nav_timeutc.day;
744
745                                 ao_gps_data.hour = nav_timeutc.hour;
746                                 ao_gps_data.minute = nav_timeutc.min;
747                                 ao_gps_data.second = nav_timeutc.sec;
748
749                                 /* we report dop scaled by 10, but ublox provides dop scaled by 100
750                                  */
751                                 ao_gps_data.pdop = nav_dop.pdop / 10;
752                                 ao_gps_data.hdop = nav_dop.hdop / 10;
753                                 ao_gps_data.vdop = nav_dop.vdop / 10;
754
755                                 ao_gps_data.ground_speed = nav_velned.g_speed;
756                                 ao_gps_data.climb_rate = -nav_velned.vel_d;
757                                 ao_gps_data.course = nav_velned.heading / 200000;
758
759                                 ao_gps_tracking_data.channels = 0;
760
761                                 struct ao_telemetry_satellite_info *dst = &ao_gps_tracking_data.sats[0];
762                                 struct nav_svinfo_sat *src = &nav_svinfo_sat[0];
763
764                                 for (i = 0; i < nav_svinfo_nsat; i++) {
765                                         if (!(src->flags & (1 << NAV_SVINFO_SAT_FLAGS_UNHEALTHY)) &&
766                                             src->quality >= NAV_SVINFO_SAT_QUALITY_ACQUIRED)
767                                         {
768                                                 if (ao_gps_tracking_data.channels < AO_TELEMETRY_SATELLITE_MAX_SAT) {
769                                                         dst->svid = src->svid;
770                                                         dst->c_n_1 = src->cno;
771                                                         dst++;
772                                                         ao_gps_tracking_data.channels++;
773                                                 }
774                                         }
775                                         src++;
776                                 }
777
778                                 ao_mutex_put(&ao_gps_mutex);
779                                 ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
780                                 ao_wakeup(&ao_gps_new);
781                                 break;
782                         }
783                         break;
784                 }
785         }
786 }
787
788 #if AO_UBLOX_DEBUG
789 static void ao_gps_option(void)
790 {
791         uint16_t r = ao_cmd_hex();
792         if (ao_cmd_status != ao_cmd_success) {
793                 ao_cmd_status = ao_cmd_success;
794                 ao_gps_show();
795         } else {
796                 ao_gps_dbg_enable = r;
797                 printf ("gps debug set to %d\n", ao_gps_dbg_enable);
798         }
799 }
800 #else
801 #define ao_gps_option ao_gps_show
802 #endif
803
804 const struct ao_cmds ao_gps_cmds[] = {
805         { ao_gps_option,        "g\0Display GPS" },
806         { 0, NULL },
807 };
808
809 struct ao_task ao_gps_task;
810
811 void
812 ao_gps_init(void)
813 {
814         ao_cmd_register(&ao_gps_cmds[0]);
815         ao_add_task(&ao_gps_task, ao_gps, "gps");
816 }