/src/gpsd/gpsd-3.28.1~dev/gpsd/isgps.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | |
3 | | This is a decoder for the unnamed protocol described in IS-GPS-200, |
4 | | the Navstar GPS Interface Specification, and used as a transport layer |
5 | | for both GPS satellite downlink transmissions and the RTCM104 version 2 |
6 | | format for broadcasting differential-GPS corrections. |
7 | | |
8 | | The purpose of this protocol is to support analyzing a serial bit |
9 | | stream without byte framing into parity-checked 30-bit words. |
10 | | Interpretation of the words is left to an upper layer. Note that |
11 | | RTCM104 version 3 does *not* use this code; it assumes a byte-oriented |
12 | | underlayer. |
13 | | |
14 | | The upper layer must supply a preamble_match() hook to tell our |
15 | | decoder when it has a legitimate start of packet, and a length_check() |
16 | | hook to tell it when the packet has reached the length it is supposed |
17 | | to have. |
18 | | |
19 | | This decoder is overkill as GNSS receivers already find the preamble |
20 | | and word boundaries of the bitstream. |
21 | | |
22 | | Here are Wolfgang's original rather cryptic notes on this code: |
23 | | |
24 | | -------------------------------------------------------------------------- |
25 | | 1) trim and bitflip the input. |
26 | | |
27 | | While syncing the msb of the input gets shifted into lsb of the |
28 | | assembled word. |
29 | | word <<= 1, or in input >> 5 |
30 | | word <<= 1, or in input >> 4 |
31 | | word <<= 1, or in input >> 3 |
32 | | word <<= 1, or in input >> 2 |
33 | | word <<= 1, or in input >> 1 |
34 | | word <<= 1, or in input |
35 | | |
36 | | At one point it should sync-lock. |
37 | | |
38 | | ---- |
39 | | |
40 | | Shift 6 bytes of RTCM data in as such: |
41 | | |
42 | | ---> (trim-bits-to-5-bits) ---> (end-for-end-bit-flip) ---> |
43 | | |
44 | | ---> shift-into-30-bit-shift-register |
45 | | ||||||||||||||||||||||| |
46 | | detector-for-preamble |
47 | | ||||||||||||||||||||||| |
48 | | detector-for-parity |
49 | | ||||||||||||||||||||||| |
50 | | -------------------------------------------------------------------------- |
51 | | |
52 | | The code was originally by Wolfgang Rupprecht. ESR severely hacked |
53 | | it, with Wolfgang's help, in order to separate message analysis from |
54 | | message dumping and separate this lower layer from the upper layer |
55 | | handing GPS-subframe and RTCM decoding. |
56 | | |
57 | | You are not expected to understand any of this. |
58 | | |
59 | | This file is Copyright by the GPSD project |
60 | | SPDX-License-Identifier: BSD-2-clause |
61 | | |
62 | | *****************************************************************************/ |
63 | | |
64 | | #include "../include/gpsd_config.h" // must be before all includes */ |
65 | | |
66 | | #include <stdbool.h> |
67 | | #include "../include/gpsd.h" |
68 | | |
69 | 0 | #define MAG_SHIFT 6u |
70 | 0 | #define MAG_TAG_DATA (1 << MAG_SHIFT) |
71 | 0 | #define MAG_TAG_MASK (3 << MAG_SHIFT) |
72 | | |
73 | 0 | #define W_DATA_MASK 0x3fffffc0u |
74 | | |
75 | | static unsigned char parity_array[] = { |
76 | | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
77 | | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
78 | | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
79 | | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
80 | | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
81 | | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
82 | | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
83 | | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
84 | | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
85 | | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
86 | | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
87 | | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
88 | | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, |
89 | | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
90 | | 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, |
91 | | 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 |
92 | | }; |
93 | | |
94 | | static unsigned int reverse_bits[] = { |
95 | | 0, 32, 16, 48, 8, 40, 24, 56, 4, 36, 20, 52, 12, 44, 28, 60, |
96 | | 2, 34, 18, 50, 10, 42, 26, 58, 6, 38, 22, 54, 14, 46, 30, 62, |
97 | | 1, 33, 17, 49, 9, 41, 25, 57, 5, 37, 21, 53, 13, 45, 29, 61, |
98 | | 3, 35, 19, 51, 11, 43, 27, 59, 7, 39, 23, 55, 15, 47, 31, 63 |
99 | | }; |
100 | | |
101 | | |
102 | | unsigned int isgps_parity(isgps30bits_t th) |
103 | 0 | { |
104 | 0 | #define P_30_MASK 0x40000000u |
105 | |
|
106 | 0 | #define PARITY_25 0xbb1f3480u |
107 | 0 | #define PARITY_26 0x5d8f9a40u |
108 | 0 | #define PARITY_27 0xaec7cd00u |
109 | 0 | #define PARITY_28 0x5763e680u |
110 | 0 | #define PARITY_29 0x6bb1f340u |
111 | 0 | #define PARITY_30 0x8b7a89c0u |
112 | 0 | isgps30bits_t t; |
113 | 0 | unsigned int p; |
114 | | |
115 | | /* |
116 | | * if (th & P_30_MASK) |
117 | | * th ^= W_DATA_MASK; |
118 | | */ |
119 | |
|
120 | 0 | t = th & PARITY_25; |
121 | 0 | p = parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^ |
122 | 0 | parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) & 0xff]; |
123 | 0 | t = th & PARITY_26; |
124 | 0 | p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^ |
125 | 0 | parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) & |
126 | 0 | 0xff]); |
127 | 0 | t = th & PARITY_27; |
128 | 0 | p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^ |
129 | 0 | parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) & |
130 | 0 | 0xff]); |
131 | 0 | t = th & PARITY_28; |
132 | 0 | p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^ |
133 | 0 | parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) & |
134 | 0 | 0xff]); |
135 | 0 | t = th & PARITY_29; |
136 | 0 | p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^ |
137 | 0 | parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) & |
138 | 0 | 0xff]); |
139 | 0 | t = th & PARITY_30; |
140 | 0 | p = (p << 1) | (parity_array[t & 0xff] ^ parity_array[(t >> 8) & 0xff] ^ |
141 | 0 | parity_array[(t >> 16) & 0xff] ^ parity_array[(t >> 24) & |
142 | 0 | 0xff]); |
143 | |
|
144 | | #ifdef __UNUSED__ |
145 | | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 2, errout, "ISGPS parity %u\n", p); |
146 | | #endif // __UNUSED__ |
147 | 0 | return p; |
148 | 0 | } |
149 | | |
150 | | /* |
151 | | * ESR found a doozy of a bug... |
152 | | * |
153 | | * Defining isgps_parityok as a function triggers an optimizer bug in gcc |
154 | | * 3.4.2. The symptom is that parity computation is screwed up and the decoder |
155 | | * never achieves sync lock. Something steps on the argument to |
156 | | * isgpsparity(); the lossage appears to be related to the compiler's |
157 | | * attempt to fold the isgps_parity() call into isgps_parityok() in some |
158 | | * tail-recursion-like manner. This happens under -O2, but not -O1, on |
159 | | * both i386 and amd64. Disabling all of the individual -O2 suboptions |
160 | | * does *not* fix it. |
161 | | * |
162 | | * And the fun doesn't stop there! It turns out that even with this fix, bare |
163 | | * -O2 generates bad code. It takes "-O2 -fschedule-insns" to generate good |
164 | | * code under 3.4.[23]...which is weird because -O2 is supposed to *imply* |
165 | | * -fschedule-insns. |
166 | | * |
167 | | * gcc 4.0 does not manifest these bugs. |
168 | | */ |
169 | 0 | #define isgps_parityok(w) (isgps_parity(w) == ((w) & 0x3f)) |
170 | | |
171 | | void isgps_init(struct gps_lexer_t *lexer) |
172 | 0 | { |
173 | 0 | lexer->isgps.curr_word = 0; |
174 | 0 | lexer->isgps.curr_offset = 24; // first word |
175 | 0 | lexer->isgps.locked = false; |
176 | 0 | lexer->isgps.bufindex = 0; |
177 | 0 | lexer->isgps.buflen = 0; |
178 | 0 | } |
179 | | |
180 | | enum isgpsstat_t isgps_decode(struct gps_lexer_t *lexer, |
181 | | bool(*preamble_match) (isgps30bits_t *), |
182 | | bool(*length_check) (struct gps_lexer_t *), |
183 | | size_t maxlen, unsigned int c) |
184 | 0 | { |
185 | | // ASCII characters 64-127, @ through DEL |
186 | 0 | if (MAG_TAG_DATA != (c & MAG_TAG_MASK)) { |
187 | 0 | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 1, &lexer->errout, |
188 | 0 | "ISGPS word tag not correct, skipping byte\n"); |
189 | 0 | return ISGPS_SKIP; |
190 | 0 | } |
191 | | |
192 | 0 | c = reverse_bits[c & 0x3f]; |
193 | |
|
194 | 0 | if (!lexer->isgps.locked) { |
195 | 0 | lexer->isgps.curr_offset = -5; |
196 | 0 | lexer->isgps.bufindex = 0; |
197 | |
|
198 | 0 | while (0 >= lexer->isgps.curr_offset) { |
199 | 0 | lexer->isgps.curr_word <<= 1; |
200 | 0 | if (0 < lexer->isgps.curr_offset) { |
201 | 0 | lexer->isgps.curr_word |= c << lexer->isgps.curr_offset; |
202 | 0 | } else { |
203 | 0 | lexer->isgps.curr_word |= |
204 | 0 | c >> -(lexer->isgps.curr_offset); |
205 | 0 | } |
206 | 0 | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 2, &lexer->errout, |
207 | 0 | "ISGPS syncing at byte %lu: 0x%08x\n", |
208 | 0 | lexer->char_counter, lexer->isgps.curr_word); |
209 | |
|
210 | 0 | if (preamble_match(&lexer->isgps.curr_word)) { |
211 | 0 | if (isgps_parityok(lexer->isgps.curr_word)) { |
212 | 0 | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 1, &lexer->errout, |
213 | 0 | "ISGPS preamble ok, parity ok -- locked\n"); |
214 | 0 | lexer->isgps.locked = true; |
215 | 0 | break; |
216 | 0 | } |
217 | 0 | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 1, &lexer->errout, |
218 | 0 | "ISGPS preamble ok, parity fail\n"); |
219 | 0 | } |
220 | 0 | lexer->isgps.curr_offset++; |
221 | 0 | } // end while |
222 | 0 | } |
223 | 0 | if (lexer->isgps.locked) { |
224 | 0 | enum isgpsstat_t res; |
225 | |
|
226 | 0 | res = ISGPS_SYNC; |
227 | |
|
228 | 0 | if (0 < lexer->isgps.curr_offset) { |
229 | 0 | lexer->isgps.curr_word |= c << lexer->isgps.curr_offset; |
230 | 0 | } else { |
231 | 0 | lexer->isgps.curr_word |= c >> -(lexer->isgps.curr_offset); |
232 | 0 | } |
233 | |
|
234 | 0 | if (0 >=lexer->isgps.curr_offset) { |
235 | | // weird-assed inversion |
236 | 0 | if (lexer->isgps.curr_word & P_30_MASK) { |
237 | 0 | lexer->isgps.curr_word ^= W_DATA_MASK; |
238 | 0 | } |
239 | |
|
240 | 0 | if (isgps_parityok(lexer->isgps.curr_word)) { |
241 | | #if 0 |
242 | | /* |
243 | | * Don't clobber the buffer just because we spot |
244 | | * another preamble pattern in the data stream. -wsr |
245 | | */ |
246 | | if (preamble_match(&lexer->isgps.curr_word)) { |
247 | | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 2, &lexer->errout, |
248 | | "ISGPS preamble spotted (index: %u)\n", |
249 | | lexer->isgps.bufindex); |
250 | | lexer->isgps.bufindex = 0; |
251 | | } |
252 | | #endif |
253 | 0 | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 2, &lexer->errout, |
254 | 0 | "ISGPS processing word %u (offset %d)\n", |
255 | 0 | lexer->isgps.bufindex, |
256 | 0 | lexer->isgps.curr_offset); |
257 | 0 | { |
258 | | /* |
259 | | * Guard against a buffer overflow attack. Just wait for |
260 | | * the next preamble match and go on from there. |
261 | | */ |
262 | 0 | if (lexer->isgps.bufindex >= (unsigned)maxlen) { |
263 | 0 | lexer->isgps.bufindex = 0; |
264 | 0 | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 1, &lexer->errout, |
265 | 0 | "ISGPS buffer overflowing -- resetting\n"); |
266 | 0 | return ISGPS_NO_SYNC; |
267 | 0 | } |
268 | | |
269 | 0 | lexer->isgps.buf[lexer->isgps.bufindex] = |
270 | 0 | lexer->isgps.curr_word; |
271 | |
|
272 | 0 | if ((lexer->isgps.bufindex == 0) && |
273 | 0 | !preamble_match((isgps30bits_t *) lexer->isgps.buf)) { |
274 | 0 | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 1, &lexer->errout, |
275 | 0 | "ISGPS word 0 not a preamble- punting\n"); |
276 | 0 | return ISGPS_NO_SYNC; |
277 | 0 | } |
278 | | |
279 | 0 | lexer->isgps.bufindex++; |
280 | |
|
281 | 0 | if (length_check(lexer)) { |
282 | | // jackpot, we have a complete packet |
283 | 0 | lexer->isgps.buflen = lexer->isgps.bufindex * |
284 | 0 | sizeof(isgps30bits_t); |
285 | 0 | lexer->isgps.bufindex = 0; |
286 | 0 | res = ISGPS_MESSAGE; |
287 | 0 | } |
288 | 0 | } |
289 | 0 | lexer->isgps.curr_word <<= 30; // preserve the 2 low bits |
290 | 0 | lexer->isgps.curr_offset += 30; |
291 | 0 | if (0 < lexer->isgps.curr_offset) { |
292 | 0 | lexer->isgps.curr_word |= |
293 | 0 | c << lexer->isgps.curr_offset; |
294 | 0 | } else { |
295 | 0 | lexer->isgps.curr_word |= |
296 | 0 | c >> -(lexer->isgps.curr_offset); |
297 | 0 | } |
298 | 0 | } else { |
299 | 0 | GPSD_LOG(ISGPS_ERRLEVEL_BASE, &lexer->errout, |
300 | 0 | "ISGPS parity failure, lost lock\n"); |
301 | 0 | lexer->isgps.locked = false; |
302 | 0 | } |
303 | 0 | } |
304 | 0 | lexer->isgps.curr_offset -= 6; |
305 | 0 | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 2, &lexer->errout, |
306 | 0 | "ISGPS residual %d\n", |
307 | 0 | lexer->isgps.curr_offset); |
308 | 0 | return res; |
309 | 0 | } |
310 | | |
311 | | // never achieved lock |
312 | 0 | GPSD_LOG(ISGPS_ERRLEVEL_BASE + 1, &lexer->errout, |
313 | 0 | "ISGPS lock never achieved\n"); |
314 | 0 | return ISGPS_NO_SYNC; |
315 | 0 | } |
316 | | |
317 | | // vim: set expandtab shiftwidth=4 |