blob: c3d0f70d703246d7782d3ef4988952e48ab30f77 [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
30
31/* Helpers */
32
33static int
34float_to_fixedpoint(float f, int sf)
35{
36 if (sf < 0) {
37 while (sf++ < 0)
38 f *= 2.0f;
39 } else {
40 while (sf-- > 0)
41 f *= 0.5f;
42 }
43
44 return (int)f;
45}
46
47static inline int
48double_to_fixedpoint(double d, int sf)
49{
50 if (sf < 0) {
51 while (sf++ < 0)
52 d *= 2.0;
53 } else {
54 while (sf-- > 0)
55 d *= 0.5;
56 }
57
58 return (int)d;
59}
60
61
62/* UBX message parsing to fill gps assist data */
63
64static void
65_ubx_msg_parse_nav_posllh(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
66{
67 struct ubx_nav_posllh *nav_posllh = pl;
68 struct gps_assist_data *gps = ud;
69
70 //printf("[.] NAV_POSLLH\n");
71
Sylvain Munaut9819d8b2009-12-28 14:51:08 +010072 gps->fields |= GPS_FIELD_REFPOS;
73
74 gps->ref_pos.latitude = (double)(nav_posllh->lat) * 1e-7;
75 gps->ref_pos.longitude = (double)(nav_posllh->lon) * 1e-7;
76 gps->ref_pos.altitude = (double)(nav_posllh->height) * 1e-3;
Sylvain Munaut341542b2009-12-22 21:53:22 +010077}
78
79static void
80_ubx_msg_parse_aid_ini(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
81{
82 struct ubx_aid_ini *aid_ini = pl;
83 struct gps_assist_data *gps = ud;
84
85 //printf("[.] AID_INI\n");
86
Sylvain Munaut9c3ae672009-12-28 15:49:29 +010087 /* Extract info for "Reference Time" */
88 gps->fields |= GPS_FIELD_REFTIME;
89
90 gps->ref_time.wn = aid_ini->wn;
91 gps->ref_time.tow = (double)aid_ini->tow * 1e-3;
92
Sylvain Munaut639889b2009-12-28 15:34:30 +010093 // FIXME: We could extract ref position as well but we need it in
94 // WGS84 geodetic coordinates and it's provided as ecef, so
95 // we need a lot of math ...
Sylvain Munaut341542b2009-12-22 21:53:22 +010096}
97
98static void
99_ubx_msg_parse_aid_hui(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
100{
101 struct ubx_aid_hui *aid_hui = pl;
102 struct gps_assist_data *gps = ud;
103
104 //printf("[.] AID_HUI\n");
105
106 if (aid_hui->flags & 0x2) { /* UTC parameters valid */
107 struct gps_utc_model *utc = &gps->utc;
108
109 gps->fields |= GPS_FIELD_UTC;
110
111 utc->a0 = double_to_fixedpoint(aid_hui->utc_a0, -30);
112 utc->a1 = double_to_fixedpoint(aid_hui->utc_a1, -50);
113 utc->delta_t_ls = aid_hui->utc_ls;
114 utc->t_ot = aid_hui->utc_tot >> 12;
115 utc->wn_t = aid_hui->utc_wnt;
116 utc->wn_lsf = aid_hui->utc_wnf;
117 utc->dn = aid_hui->utc_dn;
118 utc->delta_t_lsf = aid_hui->utc_lsf;
119 }
120
121 if (aid_hui->flags & 0x04) { /* Klobuchar parameters valid */
122 struct gps_ionosphere_model *iono = &gps->ionosphere;
123
124 gps->fields |= GPS_FIELD_IONOSPHERE;
125
126 iono->alpha_0 = float_to_fixedpoint(aid_hui->klob_a0, -30);
127 iono->alpha_1 = float_to_fixedpoint(aid_hui->klob_a1, -27);
128 iono->alpha_2 = float_to_fixedpoint(aid_hui->klob_a2, -24);
129 iono->alpha_3 = float_to_fixedpoint(aid_hui->klob_a3, -24);
130 iono->beta_0 = float_to_fixedpoint(aid_hui->klob_b0, 11);
131 iono->beta_1 = float_to_fixedpoint(aid_hui->klob_b1, 14);
132 iono->beta_2 = float_to_fixedpoint(aid_hui->klob_b2, 16);
133 iono->beta_3 = float_to_fixedpoint(aid_hui->klob_b3, 16);
134 }
135}
136
137static void
138_ubx_msg_parse_aid_alm(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
139{
140 struct ubx_aid_alm *aid_alm = pl;
141 struct gps_assist_data *gps = ud;
142
143 //printf("[.] AID_ALM %d - %d\n", aid_alm->sv_id, aid_alm->gps_week);
144
145 if (aid_alm->gps_week) {
146 gps->fields |= GPS_FIELD_ALMANAC;
147 gps->almanac.wna = aid_alm->gps_week & 0xff;
148 gps_unpack_sf45_almanac(aid_alm->alm_words, &gps->almanac.svs[gps->almanac.n_sv++]);
149 }
150}
151
152static void
153_ubx_msg_parse_aid_eph(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
154{
155 struct ubx_aid_eph *aid_eph = pl;
156 struct gps_assist_data *gps = ud;
157
158 //printf("[.] AID_EPH %d - %s\n", aid_eph->sv_id, aid_eph->present ? "present" : "not present");
159
160 if (aid_eph->present) {
161 int i = gps->ephemeris.n_sv++;
162 gps->fields |= GPS_FIELD_EPHEMERIS;
163 gps->ephemeris.svs[i].sv_id = aid_eph->sv_id;
164 gps_unpack_sf123(aid_eph->eph_words, &gps->ephemeris.svs[i]);
165 }
166}
167
168
169/* Dispatch table */
170struct ubx_dispatch_entry ubx_parse_dt[] = {
171 UBX_DISPATCH(NAV, POSLLH, _ubx_msg_parse_nav_posllh),
172 UBX_DISPATCH(AID, INI, _ubx_msg_parse_aid_ini),
173 UBX_DISPATCH(AID, HUI, _ubx_msg_parse_aid_hui),
174 UBX_DISPATCH(AID, ALM, _ubx_msg_parse_aid_alm),
175 UBX_DISPATCH(AID, EPH, _ubx_msg_parse_aid_eph),
176};
177