blob: 60bc05763991e743b6aad82c800f761b6f9873ea [file] [log] [blame]
Sylvain Munaut0f914132009-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 Munautccea4392009-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 Munaut0f914132009-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
87 // FIXME: Extract info for "Reference Time"
88}
89
90static void
91_ubx_msg_parse_aid_hui(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
92{
93 struct ubx_aid_hui *aid_hui = pl;
94 struct gps_assist_data *gps = ud;
95
96 //printf("[.] AID_HUI\n");
97
98 if (aid_hui->flags & 0x2) { /* UTC parameters valid */
99 struct gps_utc_model *utc = &gps->utc;
100
101 gps->fields |= GPS_FIELD_UTC;
102
103 utc->a0 = double_to_fixedpoint(aid_hui->utc_a0, -30);
104 utc->a1 = double_to_fixedpoint(aid_hui->utc_a1, -50);
105 utc->delta_t_ls = aid_hui->utc_ls;
106 utc->t_ot = aid_hui->utc_tot >> 12;
107 utc->wn_t = aid_hui->utc_wnt;
108 utc->wn_lsf = aid_hui->utc_wnf;
109 utc->dn = aid_hui->utc_dn;
110 utc->delta_t_lsf = aid_hui->utc_lsf;
111 }
112
113 if (aid_hui->flags & 0x04) { /* Klobuchar parameters valid */
114 struct gps_ionosphere_model *iono = &gps->ionosphere;
115
116 gps->fields |= GPS_FIELD_IONOSPHERE;
117
118 iono->alpha_0 = float_to_fixedpoint(aid_hui->klob_a0, -30);
119 iono->alpha_1 = float_to_fixedpoint(aid_hui->klob_a1, -27);
120 iono->alpha_2 = float_to_fixedpoint(aid_hui->klob_a2, -24);
121 iono->alpha_3 = float_to_fixedpoint(aid_hui->klob_a3, -24);
122 iono->beta_0 = float_to_fixedpoint(aid_hui->klob_b0, 11);
123 iono->beta_1 = float_to_fixedpoint(aid_hui->klob_b1, 14);
124 iono->beta_2 = float_to_fixedpoint(aid_hui->klob_b2, 16);
125 iono->beta_3 = float_to_fixedpoint(aid_hui->klob_b3, 16);
126 }
127}
128
129static void
130_ubx_msg_parse_aid_alm(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
131{
132 struct ubx_aid_alm *aid_alm = pl;
133 struct gps_assist_data *gps = ud;
134
135 //printf("[.] AID_ALM %d - %d\n", aid_alm->sv_id, aid_alm->gps_week);
136
137 if (aid_alm->gps_week) {
138 gps->fields |= GPS_FIELD_ALMANAC;
139 gps->almanac.wna = aid_alm->gps_week & 0xff;
140 gps_unpack_sf45_almanac(aid_alm->alm_words, &gps->almanac.svs[gps->almanac.n_sv++]);
141 }
142}
143
144static void
145_ubx_msg_parse_aid_eph(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
146{
147 struct ubx_aid_eph *aid_eph = pl;
148 struct gps_assist_data *gps = ud;
149
150 //printf("[.] AID_EPH %d - %s\n", aid_eph->sv_id, aid_eph->present ? "present" : "not present");
151
152 if (aid_eph->present) {
153 int i = gps->ephemeris.n_sv++;
154 gps->fields |= GPS_FIELD_EPHEMERIS;
155 gps->ephemeris.svs[i].sv_id = aid_eph->sv_id;
156 gps_unpack_sf123(aid_eph->eph_words, &gps->ephemeris.svs[i]);
157 }
158}
159
160
161/* Dispatch table */
162struct ubx_dispatch_entry ubx_parse_dt[] = {
163 UBX_DISPATCH(NAV, POSLLH, _ubx_msg_parse_nav_posllh),
164 UBX_DISPATCH(AID, INI, _ubx_msg_parse_aid_ini),
165 UBX_DISPATCH(AID, HUI, _ubx_msg_parse_aid_hui),
166 UBX_DISPATCH(AID, ALM, _ubx_msg_parse_aid_alm),
167 UBX_DISPATCH(AID, EPH, _ubx_msg_parse_aid_eph),
168};
169