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