isgps.c 10 KB

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