blob: 46db914e0a6a848abea6035204ab2593b9f98f72 [file] [log] [blame]
Sylvain Munaut341542b2009-12-22 21:53:22 +01001/*
2 * ubx-parse.c
3 *
4 * Implementation of parsing code converting UBX messages to GPS assist
5 * data
6 *
7 *
8 * Copyright (C) 2009 Sylvain Munaut <tnt@246tNt.com>
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include <stdio.h>
25
26#include "gps.h"
27#include "ubx.h"
28#include "ubx-parse.h"
29
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +020030#define DEBUG 1
31#if DEBUG
32 #define printd(x, args ...) printf(x, ## args)
33#else
34 #define printd(x, args ...)
35#endif
36
37#define DEBUG1 0
38#if DEBUG1
39 #define printd1(x, args ...) printf(x, ## args)
40#else
41 #define printd1(x, args ...)
42#endif
Sylvain Munaut341542b2009-12-22 21:53:22 +010043
44/* Helpers */
45
46static int
47float_to_fixedpoint(float f, int sf)
48{
49 if (sf < 0) {
50 while (sf++ < 0)
51 f *= 2.0f;
52 } else {
53 while (sf-- > 0)
54 f *= 0.5f;
55 }
56
57 return (int)f;
58}
59
60static inline int
61double_to_fixedpoint(double d, int sf)
62{
63 if (sf < 0) {
64 while (sf++ < 0)
65 d *= 2.0;
66 } else {
67 while (sf-- > 0)
68 d *= 0.5;
69 }
70
71 return (int)d;
72}
73
74
75/* UBX message parsing to fill gps assist data */
76
77static void
78_ubx_msg_parse_nav_posllh(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
79{
80 struct ubx_nav_posllh *nav_posllh = pl;
81 struct gps_assist_data *gps = ud;
82
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +020083 printd("[.] NAV_POSLLH\n");
Sylvain Munaut341542b2009-12-22 21:53:22 +010084
Sylvain Munaut9819d8b2009-12-28 14:51:08 +010085 gps->fields |= GPS_FIELD_REFPOS;
86
87 gps->ref_pos.latitude = (double)(nav_posllh->lat) * 1e-7;
88 gps->ref_pos.longitude = (double)(nav_posllh->lon) * 1e-7;
89 gps->ref_pos.altitude = (double)(nav_posllh->height) * 1e-3;
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +020090
91 printd(" TOW %lu\n", nav_posllh->itow);
92 printd(" latitude %f\n", gps->ref_pos.latitude);
93 printd(" longitude %f\n", gps->ref_pos.longitude);
94 printd(" altitude %f\n", gps->ref_pos.altitude);
Sylvain Munaut341542b2009-12-22 21:53:22 +010095}
96
97static void
98_ubx_msg_parse_aid_ini(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
99{
100 struct ubx_aid_ini *aid_ini = pl;
101 struct gps_assist_data *gps = ud;
102
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +0200103 printd("[.] AID_INI\n");
Sylvain Munaut341542b2009-12-22 21:53:22 +0100104
Sylvain Munaut9c3ae672009-12-28 15:49:29 +0100105 /* Extract info for "Reference Time" */
106 gps->fields |= GPS_FIELD_REFTIME;
107
108 gps->ref_time.wn = aid_ini->wn;
109 gps->ref_time.tow = (double)aid_ini->tow * 1e-3;
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +0200110 gps->ref_time.when = time(NULL);
111
112 printd(" WN %d\n", gps->ref_time.wn);
113 printd(" TOW %ld\n", aid_ini->tow);
114
115 if((aid_ini->flags & 0x03) != 0x03) { /* time and pos valid ? */
116 fprintf(stderr, "Postion and/or time not valid (0x%lx)", aid_ini->flags);
117 }
118
Sylvain Munaut639889b2009-12-28 15:34:30 +0100119 // FIXME: We could extract ref position as well but we need it in
120 // WGS84 geodetic coordinates and it's provided as ecef, so
121 // we need a lot of math ...
Sylvain Munaut341542b2009-12-22 21:53:22 +0100122}
123
124static void
125_ubx_msg_parse_aid_hui(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
126{
127 struct ubx_aid_hui *aid_hui = pl;
128 struct gps_assist_data *gps = ud;
129
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +0200130 printd("[.] AID_HUI\n");
Sylvain Munaut341542b2009-12-22 21:53:22 +0100131
132 if (aid_hui->flags & 0x2) { /* UTC parameters valid */
133 struct gps_utc_model *utc = &gps->utc;
134
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +0200135 printd(" UTC\n");
136
Sylvain Munaut341542b2009-12-22 21:53:22 +0100137 gps->fields |= GPS_FIELD_UTC;
138
139 utc->a0 = double_to_fixedpoint(aid_hui->utc_a0, -30);
140 utc->a1 = double_to_fixedpoint(aid_hui->utc_a1, -50);
141 utc->delta_t_ls = aid_hui->utc_ls;
142 utc->t_ot = aid_hui->utc_tot >> 12;
143 utc->wn_t = aid_hui->utc_wnt;
144 utc->wn_lsf = aid_hui->utc_wnf;
145 utc->dn = aid_hui->utc_dn;
146 utc->delta_t_lsf = aid_hui->utc_lsf;
147 }
148
149 if (aid_hui->flags & 0x04) { /* Klobuchar parameters valid */
150 struct gps_ionosphere_model *iono = &gps->ionosphere;
151
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +0200152 printd(" IONOSPHERE\n");
153
Sylvain Munaut341542b2009-12-22 21:53:22 +0100154 gps->fields |= GPS_FIELD_IONOSPHERE;
155
156 iono->alpha_0 = float_to_fixedpoint(aid_hui->klob_a0, -30);
157 iono->alpha_1 = float_to_fixedpoint(aid_hui->klob_a1, -27);
158 iono->alpha_2 = float_to_fixedpoint(aid_hui->klob_a2, -24);
159 iono->alpha_3 = float_to_fixedpoint(aid_hui->klob_a3, -24);
160 iono->beta_0 = float_to_fixedpoint(aid_hui->klob_b0, 11);
161 iono->beta_1 = float_to_fixedpoint(aid_hui->klob_b1, 14);
162 iono->beta_2 = float_to_fixedpoint(aid_hui->klob_b2, 16);
163 iono->beta_3 = float_to_fixedpoint(aid_hui->klob_b3, 16);
164 }
165}
166
167static void
168_ubx_msg_parse_aid_alm(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
169{
170 struct ubx_aid_alm *aid_alm = pl;
171 struct gps_assist_data *gps = ud;
172
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +0200173 if(pl_len == 8) /* length if not available */
174 return;
175
176 if(pl_len != sizeof(struct ubx_aid_alm)) {
177 fprintf(stderr, "pl_len != sizeof(struct ubx_aid_alm) (%d)\n", pl_len);
178 return;
179 }
180
181 printd("[.] AID_ALM %2ld - %ld (nsv = %d)\n", aid_alm->sv_id, aid_alm->gps_week, gps->almanac.n_sv);
Sylvain Munaut341542b2009-12-22 21:53:22 +0100182
183 if (aid_alm->gps_week) {
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +0200184 int i = gps->almanac.n_sv++;
Sylvain Munaut341542b2009-12-22 21:53:22 +0100185 gps->fields |= GPS_FIELD_ALMANAC;
186 gps->almanac.wna = aid_alm->gps_week & 0xff;
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +0200187 gps_unpack_sf45_almanac(aid_alm->alm_words, &gps->almanac.svs[i]);
188 /* set satellite ID this way, otherwise it will be wrong */
189 gps->almanac.svs[i].sv_id = aid_alm->sv_id;
Sylvain Munaut341542b2009-12-22 21:53:22 +0100190 }
191}
192
193static void
194_ubx_msg_parse_aid_eph(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
195{
196 struct ubx_aid_eph *aid_eph = pl;
197 struct gps_assist_data *gps = ud;
198
Dieter Spaar5ffe5fe2012-07-18 22:18:16 +0200199 if(pl_len == 8) /* length if not available */
200 return;
201
202 if(pl_len != sizeof(struct ubx_aid_eph)) {
203 fprintf(stderr, "pl_len != sizeof(struct ubx_aid_eph) (%d)\n", pl_len);
204 return;
205 }
206
207 printd("[.] AID_EPH %2ld - %s (nsv = %d)\n", aid_eph->sv_id, aid_eph->present ? "present" : "", gps->ephemeris.n_sv);
Sylvain Munaut341542b2009-12-22 21:53:22 +0100208
209 if (aid_eph->present) {
210 int i = gps->ephemeris.n_sv++;
211 gps->fields |= GPS_FIELD_EPHEMERIS;
212 gps->ephemeris.svs[i].sv_id = aid_eph->sv_id;
213 gps_unpack_sf123(aid_eph->eph_words, &gps->ephemeris.svs[i]);
214 }
215}
216
217
Dieter Spaard652a322012-07-18 22:18:42 +0200218static void
219_ubx_msg_parse_nav_timegps(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
220{
221 struct ubx_nav_timegps *nav_timegps = pl;
222 struct gps_assist_data *gps = ud;
223
224 printd1("[.] NAV_TIMEGPS\n");
225
226 /* Extract info for "Reference Time" */
227 gps->fields |= GPS_FIELD_REFTIME;
228
229 gps->ref_time.wn = nav_timegps->week;
230 gps->ref_time.tow = (double)nav_timegps->itow * 1e-3;
231 gps->ref_time.when = time(NULL);
232
233 printd1(" WN %d\n", nav_timegps->week);
234 printd1(" TOW %ld\n", nav_timegps->itow);
235}
236
Sylvain Munaut341542b2009-12-22 21:53:22 +0100237/* Dispatch table */
238struct ubx_dispatch_entry ubx_parse_dt[] = {
239 UBX_DISPATCH(NAV, POSLLH, _ubx_msg_parse_nav_posllh),
240 UBX_DISPATCH(AID, INI, _ubx_msg_parse_aid_ini),
241 UBX_DISPATCH(AID, HUI, _ubx_msg_parse_aid_hui),
242 UBX_DISPATCH(AID, ALM, _ubx_msg_parse_aid_alm),
243 UBX_DISPATCH(AID, EPH, _ubx_msg_parse_aid_eph),
Dieter Spaard652a322012-07-18 22:18:42 +0200244 UBX_DISPATCH(NAV, TIMEGPS, _ubx_msg_parse_nav_timegps),
Sylvain Munaut341542b2009-12-22 21:53:22 +0100245};
246