blob: f25919358e0ad7ba813410bfd8d74299f6ed77ab [file] [log] [blame]
Sylvain Munaut341542b2009-12-22 21:53:22 +01001/*
2 * gps.c
3 *
4 * A few utility functions to deal with low level GPS data
5 *
6 *
7 * Copyright (C) 2009 Sylvain Munaut <tnt@246tNt.com>
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
Dieter Spaara5c8d802012-07-18 22:20:02 +020023#include <stdio.h>
24
Sylvain Munaut341542b2009-12-22 21:53:22 +010025#include "gps.h"
26
27
28#define GET_FIELD_U(w, nb, pos) (((w) >> (pos)) & ((1<<(nb))-1))
29#define GET_FIELD_S(w, nb, pos) (((int)((w) << (32-(nb)-(pos)))) >> (32-(nb)))
30
31/*
32 * Unpacks GPS Subframe 1,2,3 payloads (3 * 8 words)
33 *
34 * Note: eph->sv_id is not filled here since not present in those subframes
35 *
36 * (no parity bit checking is done, only the lower 24 bits of each word
37 * are used)
38 */
39int
40gps_unpack_sf123(uint32_t *sf, struct gps_ephemeris_sv *eph)
41{
42 uint32_t *sf1 = &sf[0];
43 uint32_t *sf2 = &sf[8];
44 uint32_t *sf3 = &sf[16];
45
46 int iode1, iode2;
47
48 eph->week_no = GET_FIELD_U(sf1[0], 10, 14);
49 eph->code_on_l2 = GET_FIELD_U(sf1[0], 2, 12);
50 eph->sv_ura = GET_FIELD_U(sf1[0], 4, 8);
51 eph->sv_health = GET_FIELD_U(sf1[0], 6, 2);
52 eph->l2_p_flag = GET_FIELD_U(sf1[1], 1, 23);
53 eph->t_gd = GET_FIELD_S(sf1[4], 8, 0);
54 eph->iodc = (GET_FIELD_U(sf1[0], 2, 0) << 8) | \
55 GET_FIELD_U(sf1[5], 8, 16);
56 eph->t_oc = GET_FIELD_U(sf1[5], 16, 0);
57 eph->a_f2 = GET_FIELD_S(sf1[6], 8, 16);
58 eph->a_f1 = GET_FIELD_S(sf1[6], 16, 0);
59 eph->a_f0 = GET_FIELD_S(sf1[7], 22, 2);
60
61 iode1 = GET_FIELD_U(sf2[0], 8, 16);
62 eph->c_rs = GET_FIELD_S(sf2[0], 16, 0);
63 eph->delta_n = GET_FIELD_S(sf2[1], 16, 8);
64 eph->m_0 = (GET_FIELD_S(sf2[1], 8, 0) << 24) | \
65 GET_FIELD_U(sf2[2], 24, 0);
66 eph->c_uc = GET_FIELD_S(sf2[3], 16, 8);
67 eph->e = (GET_FIELD_U(sf2[3], 8, 0) << 24) | \
68 GET_FIELD_U(sf2[4], 24, 0);
69 eph->c_us = GET_FIELD_S(sf2[5], 16, 8);
70 eph->a_powhalf = (GET_FIELD_U(sf2[5], 8, 0) << 24) | \
71 GET_FIELD_U(sf2[6], 24, 0);
72 eph->t_oe = GET_FIELD_U(sf2[7], 16, 8);
73 eph->fit_flag = GET_FIELD_U(sf2[7], 1, 7);
74
75 eph->c_ic = GET_FIELD_S(sf3[0], 16, 8);
76 eph->omega_0 = (GET_FIELD_S(sf3[0], 8, 0) << 24) | \
77 GET_FIELD_U(sf3[1], 24, 0);
78 eph->c_is = GET_FIELD_S(sf3[2], 16, 8);
79 eph->i_0 = (GET_FIELD_S(sf3[2], 8, 0) << 24) | \
80 GET_FIELD_U(sf3[3], 24, 0);
81 eph->c_rc = GET_FIELD_S(sf3[4], 16, 8);
82 eph->w = (GET_FIELD_S(sf3[4], 8, 0) << 24) | \
83 GET_FIELD_U(sf3[5], 24, 0);
84 eph->omega_dot = GET_FIELD_S(sf3[6], 24, 0);
85 iode2 = GET_FIELD_U(sf3[7], 8, 16);
86 eph->idot = GET_FIELD_S(sf3[7], 14, 2);
87
88 eph->_rsvd1 = GET_FIELD_U(sf1[1], 23, 0);
89 eph->_rsvd2 = GET_FIELD_U(sf1[2], 24, 0);
90 eph->_rsvd3 = GET_FIELD_U(sf1[3], 24, 0);
91 eph->_rsvd4 = GET_FIELD_U(sf1[4], 16, 8);
92 eph->aodo = GET_FIELD_U(sf2[7], 5, 2);
93
94 /* Check & cross-validate iodc[7:0], iode1, iode2 */
Dieter Spaara5c8d802012-07-18 22:20:02 +020095 if ((iode1 != iode2) || (iode1 != (eph->iodc & 0xff))) {
96 fprintf(stderr, "gps_unpack_sf123 failed\n");
Sylvain Munaut341542b2009-12-22 21:53:22 +010097 return -1;
Dieter Spaara5c8d802012-07-18 22:20:02 +020098 }
Sylvain Munaut341542b2009-12-22 21:53:22 +010099
100 return 0;
101}
102
103
104/*
105 * Unpacks GPS Subframe 4 or 5 Almanac pages payload (8 words)
106 *
107 * (no parity bit checking is done, only the lower 24 bits of each word
108 * are used)
109 */
110int
111gps_unpack_sf45_almanac(uint32_t *sf, struct gps_almanac_sv *alm)
112{
Dieter Spaara5c8d802012-07-18 22:20:02 +0200113 /* this is the page ID but not the satellite ID, its wrong in subframe 5 */
Sylvain Munaut341542b2009-12-22 21:53:22 +0100114 alm->sv_id = GET_FIELD_U(sf[0], 6, 16);
115
116 alm->e = GET_FIELD_U(sf[0], 16, 0);
117 alm->t_oa = GET_FIELD_U(sf[1], 8, 16);
118 alm->ksii = GET_FIELD_S(sf[1], 16, 0);
119 alm->omega_dot = GET_FIELD_S(sf[2], 16, 8);
120 alm->sv_health = GET_FIELD_U(sf[2], 8, 0);
121 alm->a_powhalf = GET_FIELD_U(sf[3], 24, 0);
122 alm->omega_0 = GET_FIELD_S(sf[4], 24, 0);
123 alm->w = GET_FIELD_S(sf[5], 24, 0);
124 alm->m_0 = GET_FIELD_S(sf[6], 24, 0);
125 alm->a_f0 = (GET_FIELD_S(sf[7], 8, 16) << 3) | \
126 GET_FIELD_U(sf[7], 3, 2);
127 alm->a_f1 = GET_FIELD_S(sf[7], 11, 5);
128
129 return 0;
130}
131