driver_rtcm2.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*****************************************************************************
  2. This is a decoder for RTCM-104 2.x, an obscure and complicated serial
  3. protocol used for broadcasting pseudorange corrections from
  4. differential-GPS reference stations. The applicable
  5. standard is
  6. RTCM RECOMMENDED STANDARDS FOR DIFFERENTIAL GNSS (GLOBAL NAVIGATION
  7. SATELLITE) SERVICE, VERSION 2.3 (RTCM PAPER 136-2001/SC104-STD)
  8. Ordering instructions are accessible from <http://www.rtcm.org/>
  9. under "Publications". This describes version 2.3 of the RTCM specification.
  10. RTCM-104 was later completely redesigned as level 3.0.
  11. Also applicable is ITU-R M.823: "Technical characteristics of
  12. differential transmissions for global navigation satellite systems
  13. from maritime radio beacons in the frequency band 283.5 - 315 kHz in
  14. region 1 and 285 - 325 kHz in regions 2 & 3."
  15. The RTCM 2.x protocol uses as a transport layer the GPS satellite downlink
  16. protocol described in IS-GPS-200, the Navstar GPS Interface
  17. Specification. This code relies on the lower-level packet-assembly
  18. code for that protocol in isgps.c.
  19. The lower layer's job is done when it has assembled a message of up to
  20. 33 30-bit words of clean parity-checked data. At this point this upper layer
  21. takes over. struct rtcm2_msg_t is overlaid on the buffer and the bitfields
  22. are used to extract pieces of it. Those pieces are copied and (where
  23. necessary) reassembled into a struct rtcm2_t.
  24. This code and the contents of isgps.c are evolved from code by
  25. Wolfgang Rupprecht. Wolfgang's decoder was loosely based on one
  26. written by John Sager in 1999. Here are John Sager's original notes:
  27. The RTCM decoder prints a legible representation of the input data.
  28. The RTCM SC-104 specification is copyrighted, so I cannot
  29. quote it - in fact, I have never read it! Most of the information
  30. used to develop the decoder came from publication ITU-R M.823.
  31. This is a specification of the data transmitted from LF DGPS
  32. beacons in the 300kHz band. M.823 contains most of those parts of
  33. RTCM SC-104 directly relevant to the air interface (there
  34. are one or two annoying and vital omissions!). Information
  35. about the serial interface format was gleaned from studying
  36. the output of a beacon receiver test program made available on
  37. Starlink's website.
  38. This code has been checked against ASCII dumps made by a proprietary
  39. decoder running under Windows and is known to be consistent with it
  40. with respect to message types 1, 3, 9, 14, 16, and 31. Decoding of
  41. message types 4, 5, 6, 7, and 13 has not been checked. Message types
  42. 8, 10-12, 15-27, 28-30 (undefined), 31-37, 38-58 (undefined), and
  43. 60-63 are not yet supported.
  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 <stdio.h>
  49. #include <string.h>
  50. #include "gpsd.h"
  51. /*
  52. __BYTE_ORDER__, __ORDER_BIG_ENDIAN__ and __ORDER_LITTLE_ENDIAN__ are
  53. defined in some gcc versions only, probably depending on the
  54. architecture. Try to use endian.h if the gcc way fails - endian.h also
  55. does not seem to be available on all platforms.
  56. */
  57. #if HAVE_BUILTIN_ENDIANNESS
  58. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  59. #define WORDS_BIGENDIAN 1
  60. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  61. #undef WORDS_BIGENDIAN
  62. #else
  63. #error Unknown endianness!
  64. #endif
  65. #else /* HAVE_BUILTIN_ENDIANNESS */
  66. #if defined(HAVE_ENDIAN_H)
  67. #include <endian.h>
  68. #elif defined(HAVE_SYS_ENDIAN_H)
  69. #include <sys/endian.h>
  70. #elif defined(HAVE_MACHINE_ENDIAN_H)
  71. #include <machine/endian.h>
  72. #endif
  73. /*
  74. * Darwin (Mac OS X) uses special defines.
  75. * This must precede the BSD case, since _BIG_ENDIAN may be incorrectly defined
  76. */
  77. #if !defined( __BYTE_ORDER) && defined(__DARWIN_BYTE_ORDER)
  78. #define __BYTE_ORDER __DARWIN_BYTE_ORDER
  79. #endif
  80. #if !defined( __BIG_ENDIAN) && defined(__DARWIN_BIG_ENDIAN)
  81. #define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
  82. #endif
  83. #if !defined( __LITTLE_ENDIAN) && defined(__DARWIN_LITTLE_ENDIAN)
  84. #define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
  85. #endif
  86. /*
  87. * BSD uses _BYTE_ORDER, and Linux uses __BYTE_ORDER.
  88. */
  89. #if !defined( __BYTE_ORDER) && defined(_BYTE_ORDER)
  90. #define __BYTE_ORDER _BYTE_ORDER
  91. #endif
  92. #if !defined( __BIG_ENDIAN) && defined(_BIG_ENDIAN)
  93. #define __BIG_ENDIAN _BIG_ENDIAN
  94. #endif
  95. #if !defined( __LITTLE_ENDIAN) && defined(_LITTLE_ENDIAN)
  96. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  97. #endif
  98. #if !defined(__BYTE_ORDER) || !defined(__BIG_ENDIAN) || !defined(__LITTLE_ENDIAN)
  99. #error endianness macros are not defined
  100. #endif
  101. #if __BYTE_ORDER == __BIG_ENDIAN
  102. #define WORDS_BIGENDIAN 1
  103. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  104. #undef WORDS_BIGENDIAN
  105. #else
  106. #error Unknown endianness!
  107. #endif /* __BYTE_ORDER */
  108. #endif /* HAVE_BUILTIN_ENDIANNESS */
  109. /*
  110. * Structures for interpreting words in an RTCM-104 2.x message (after
  111. * parity checking and removing inversion). Note, these structures
  112. * are overlayed on the raw data in order to decode them into
  113. * bitfields; this will fail horribly if your C compiler introduces
  114. * padding between or before bit fields, or between 8-bit-aligned
  115. * bitfields and character arrays despite #pragma pack(1). The right
  116. * things happen under gcc 4.x on amd64, i386, ia64, all arm and mips
  117. * variants, m68k, and powerpc)
  118. *
  119. * (In practice, the only class of machines on which this is likely
  120. * to fail are word-aligned architectures without barrel shifters.
  121. * Very few of these are left in 2012. By test, we know of s390, s390x,
  122. * and sparc.)
  123. *
  124. * The RTCM 2.1 standard is less explicit than it should be about
  125. * signed-integer representations. Two's complement is specified for
  126. * some but not all.
  127. */
  128. #define ZCOUNT_SCALE 0.6 /* sec */
  129. #define PRCSMALL 0.02 /* meters */
  130. #define PRCLARGE 0.32 /* meters */
  131. #define RRSMALL 0.002 /* meters/sec */
  132. #define RRLARGE 0.032 /* meters/sec */
  133. #define MAXPCSMALL (0x7FFF * PCSMALL) /* 16-bits signed */
  134. #define MAXRRSMALL (0x7F * RRSMALL) /* 8-bits signed */
  135. #define XYZ_SCALE 0.01 /* meters */
  136. #define DXYZ_SCALE 0.1 /* meters */
  137. #define LA_SCALE (90.0/32767.0) /* degrees */
  138. #define LO_SCALE (180.0/32767.0) /* degrees */
  139. #define FREQ_SCALE 0.1 /* kHz */
  140. #define FREQ_OFFSET 190.0 /* kHz */
  141. #define CNR_OFFSET 24 /* dB */
  142. #define TU_SCALE 5 /* minutes */
  143. #define LATLON_SCALE 0.01 /* degrees */
  144. #define RANGE_SCALE 4 /* kilometers */
  145. #pragma pack(1)
  146. /*
  147. * Reminder: Emacs reverse-region is useful...
  148. */
  149. #ifndef WORDS_BIGENDIAN /* little-endian, like x86 */
  150. struct rtcm2_msg_t {
  151. struct rtcm2_msghw1 { /* header word 1 */
  152. unsigned int parity:6;
  153. unsigned int refstaid:10; /* reference station ID */
  154. unsigned int msgtype:6; /* RTCM message type */
  155. unsigned int preamble:8; /* fixed at 01100110 */
  156. unsigned int _pad:2;
  157. } w1;
  158. struct rtcm2_msghw2 { /* header word 2 */
  159. unsigned int parity:6;
  160. unsigned int stathlth:3; /* station health */
  161. unsigned int frmlen:5;
  162. unsigned int sqnum:3;
  163. unsigned int zcnt:13;
  164. unsigned int _pad:2;
  165. } w2;
  166. union {
  167. /* msg 1 - differential gps corrections */
  168. struct rtcm2_msg1 {
  169. struct gps_correction_t {
  170. struct { /* msg 1 word 3 */
  171. unsigned int parity:6;
  172. int prc1:16;
  173. unsigned int satident1:5; /* satellite ID */
  174. unsigned int udre1:2;
  175. unsigned int scale1:1;
  176. unsigned int _pad:2;
  177. } w3;
  178. struct { /* msg 1 word 4 */
  179. unsigned int parity:6;
  180. unsigned int satident2:5; /* satellite ID */
  181. unsigned int udre2:2;
  182. unsigned int scale2:1;
  183. unsigned int iod1:8;
  184. int rrc1:8;
  185. unsigned int _pad:2;
  186. } w4;
  187. struct { /* msg 1 word 5 */
  188. unsigned int parity:6;
  189. int rrc2:8;
  190. int prc2:16;
  191. unsigned int _pad:2;
  192. } w5;
  193. struct { /* msg 1 word 6 */
  194. unsigned int parity:6;
  195. int prc3_h:8;
  196. unsigned int satident3:5; /* satellite ID */
  197. unsigned int udre3:2;
  198. unsigned int scale3:1;
  199. unsigned int iod2:8;
  200. unsigned int _pad:2;
  201. } w6;
  202. struct { /* msg 1 word 7 */
  203. unsigned int parity:6;
  204. unsigned int iod3:8;
  205. int rrc3:8;
  206. unsigned int prc3_l:8; /* NOTE: unsigned int for low byte */
  207. unsigned int _pad:2;
  208. } w7;
  209. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  210. } type1;
  211. /* msg 3 - reference station parameters */
  212. struct rtcm2_msg3 {
  213. struct {
  214. unsigned int parity:6;
  215. unsigned int x_h:24;
  216. unsigned int _pad:2;
  217. } w3;
  218. struct {
  219. unsigned int parity:6;
  220. unsigned int y_h:16;
  221. unsigned int x_l:8;
  222. unsigned int _pad:2;
  223. } w4;
  224. struct {
  225. unsigned int parity:6;
  226. unsigned int z_h:8;
  227. unsigned int y_l:16;
  228. unsigned int _pad:2;
  229. } w5;
  230. struct {
  231. unsigned int parity:6;
  232. unsigned int z_l:24;
  233. unsigned int _pad:2;
  234. } w6;
  235. } type3;
  236. /* msg 4 - reference station datum */
  237. struct rtcm2_msg4 {
  238. struct {
  239. unsigned int parity:6;
  240. unsigned int datum_alpha_char2:8;
  241. unsigned int datum_alpha_char1:8;
  242. unsigned int spare:4;
  243. unsigned int dat:1;
  244. unsigned int dgnss:3;
  245. unsigned int _pad:2;
  246. } w3;
  247. struct {
  248. unsigned int parity:6;
  249. unsigned int datum_sub_div_char2:8;
  250. unsigned int datum_sub_div_char1:8;
  251. unsigned int datum_sub_div_char3:8;
  252. unsigned int _pad:2;
  253. } w4;
  254. struct {
  255. unsigned int parity:6;
  256. unsigned int dy_h:8;
  257. unsigned int dx:16;
  258. unsigned int _pad:2;
  259. } w5;
  260. struct {
  261. unsigned int parity:6;
  262. unsigned int dz:24;
  263. unsigned int dy_l:8;
  264. unsigned int _pad:2;
  265. } w6;
  266. } type4;
  267. /* msg 5 - constellation health */
  268. struct rtcm2_msg5 {
  269. struct b_health_t {
  270. unsigned int parity:6;
  271. unsigned int unassigned:2;
  272. unsigned int time_unhealthy:4;
  273. unsigned int loss_warn:1;
  274. unsigned int new_nav_data:1;
  275. unsigned int health_enable:1;
  276. unsigned int cn0:5;
  277. unsigned int data_health:3;
  278. unsigned int issue_of_data_link:1;
  279. unsigned int sat_id:5;
  280. unsigned int reserved:1;
  281. unsigned int _pad:2;
  282. } health[MAXHEALTH];
  283. } type5;
  284. /* msg 6 - null message */
  285. /* msg 7 - beacon almanac */
  286. struct rtcm2_msg7 {
  287. struct b_station_t {
  288. struct {
  289. unsigned int parity:6;
  290. int lon_h:8;
  291. int lat:16;
  292. unsigned int _pad:2;
  293. } w3;
  294. struct {
  295. unsigned int parity:6;
  296. unsigned int freq_h:6;
  297. unsigned int range:10;
  298. unsigned int lon_l:8;
  299. unsigned int _pad:2;
  300. } w4;
  301. struct {
  302. unsigned int parity:6;
  303. unsigned int encoding:1;
  304. unsigned int sync_type:1;
  305. unsigned int mod_mode:1;
  306. unsigned int bit_rate:3;
  307. /*
  308. * ITU-R M.823-2 page 9 and RTCM-SC104 v2.1 pages
  309. * 4-21 and 4-22 are in conflict over the next two
  310. * field sizes. ITU says 9+3, RTCM says 10+2.
  311. * The latter correctly decodes the USCG station
  312. * id's so I'll use that one here. -wsr
  313. */
  314. unsigned int station_id:10;
  315. unsigned int health:2;
  316. unsigned int freq_l:6;
  317. unsigned int _pad:2;
  318. } w5;
  319. } almanac[(RTCM2_WORDS_MAX - 2)/3];
  320. } type7;
  321. /* msg 13 - Ground Transmitter Parameters (RTCM2.3 only) */
  322. struct rtcm2_msg13 {
  323. struct {
  324. unsigned int parity:6;
  325. int lat:16;
  326. unsigned int reserved:6;
  327. unsigned int rangeflag:1;
  328. unsigned int status:1;
  329. unsigned int _pad:2;
  330. } w1;
  331. struct {
  332. unsigned int parity:6;
  333. unsigned int range:8;
  334. int lon:16;
  335. unsigned int _pad:2;
  336. } w2;
  337. } type13;
  338. /* msg 14 - GPS Time of Week (RTCM2.3 only) */
  339. struct rtcm2_msg14 {
  340. struct {
  341. unsigned int parity:6;
  342. unsigned int leapsecs:6;
  343. unsigned int hour:8;
  344. unsigned int week:10;
  345. unsigned int _pad:2;
  346. } w1;
  347. } type14;
  348. /* msg 16 - text msg */
  349. struct rtcm2_msg16 {
  350. struct {
  351. unsigned int parity:6;
  352. unsigned int byte3:8;
  353. unsigned int byte2:8;
  354. unsigned int byte1:8;
  355. unsigned int _pad:2;
  356. } txt[RTCM2_WORDS_MAX-2];
  357. } type16;
  358. /* msg 31 - differential GLONASS corrections */
  359. struct rtcm2_msg31 {
  360. struct glonass_correction_t {
  361. struct { /* msg 1 word 3 */
  362. unsigned int parity:6;
  363. int prc1:16;
  364. unsigned int satident1:5; /* satellite ID */
  365. unsigned int udre1:2;
  366. unsigned int scale1:1;
  367. unsigned int _pad:2;
  368. } w3;
  369. struct { /* msg 1 word 4 */
  370. unsigned int parity:6;
  371. unsigned int satident2:5; /* satellite ID */
  372. unsigned int udre2:2;
  373. unsigned int scale2:1;
  374. unsigned int tod1:7;
  375. unsigned int change1:1;
  376. int rrc1:8;
  377. unsigned int _pad:2;
  378. } w4;
  379. struct { /* msg 1 word 5 */
  380. unsigned int parity:6;
  381. int rrc2:8;
  382. int prc2:16;
  383. unsigned int _pad:2;
  384. } w5;
  385. struct { /* msg 1 word 6 */
  386. unsigned int parity:6;
  387. int prc3_h:8;
  388. unsigned int satident3:5; /* satellite ID */
  389. unsigned int udre3:2;
  390. unsigned int scale3:1;
  391. unsigned int tod2:7;
  392. unsigned int change2:1;
  393. unsigned int _pad:2;
  394. } w6;
  395. struct { /* msg 1 word 7 */
  396. unsigned int parity:6;
  397. unsigned int tod3:7;
  398. unsigned int change3:1;
  399. int rrc3:8;
  400. unsigned int prc3_l:8; /* NOTE: unsigned int for low byte */
  401. unsigned int _pad:2;
  402. } w7;
  403. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  404. } type31;
  405. /* unknown message */
  406. isgps30bits_t rtcm2_msgunk[RTCM2_WORDS_MAX-2];
  407. } msg_type;
  408. } __attribute__((__packed__));
  409. #endif /* LITTLE_ENDIAN */
  410. #ifdef WORDS_BIGENDIAN
  411. struct rtcm2_msg_t {
  412. struct rtcm2_msghw1 { /* header word 1 */
  413. unsigned int _pad:2;
  414. unsigned int preamble:8; /* fixed at 01100110 */
  415. unsigned int msgtype:6; /* RTCM message type */
  416. unsigned int refstaid:10; /* reference station ID */
  417. unsigned int parity:6;
  418. } w1;
  419. struct rtcm2_msghw2 { /* header word 2 */
  420. unsigned int _pad:2;
  421. unsigned int zcnt:13;
  422. unsigned int sqnum:3;
  423. unsigned int frmlen:5;
  424. unsigned int stathlth:3; /* station health */
  425. unsigned int parity:6;
  426. } w2;
  427. union {
  428. /* msg 1 - differential GPS corrections */
  429. struct rtcm2_msg1 {
  430. struct gps_correction_t {
  431. struct { /* msg 1 word 3 */
  432. unsigned int _pad:2;
  433. unsigned int scale1:1;
  434. unsigned int udre1:2;
  435. unsigned int satident1:5; /* satellite ID */
  436. int prc1:16;
  437. unsigned int parity:6;
  438. } w3;
  439. struct { /* msg 1 word 4 */
  440. unsigned int _pad:2;
  441. int rrc1:8;
  442. unsigned int iod1:8;
  443. unsigned int scale2:1;
  444. unsigned int udre2:2;
  445. unsigned int satident2:5; /* satellite ID */
  446. unsigned int parity:6;
  447. } w4;
  448. struct { /* msg 1 word 5 */
  449. unsigned int _pad:2;
  450. int prc2:16;
  451. int rrc2:8;
  452. unsigned int parity:6;
  453. } w5;
  454. struct { /* msg 1 word 6 */
  455. unsigned int _pad:2;
  456. unsigned int iod2:8;
  457. unsigned int scale3:1;
  458. unsigned int udre3:2;
  459. unsigned int satident3:5; /* satellite ID */
  460. int prc3_h:8;
  461. unsigned int parity:6;
  462. } w6;
  463. struct { /* msg 1 word 7 */
  464. unsigned int _pad:2;
  465. unsigned int prc3_l:8; /* NOTE: unsigned for low byte */
  466. int rrc3:8;
  467. unsigned int iod3:8;
  468. unsigned int parity:6;
  469. } w7;
  470. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  471. } type1;
  472. /* msg 3 - reference station parameters */
  473. struct rtcm2_msg3 {
  474. struct {
  475. unsigned int _pad:2;
  476. unsigned int x_h:24;
  477. unsigned int parity:6;
  478. } w3;
  479. struct {
  480. unsigned int _pad:2;
  481. unsigned int x_l:8;
  482. unsigned int y_h:16;
  483. unsigned int parity:6;
  484. } w4;
  485. struct {
  486. unsigned int _pad:2;
  487. unsigned int y_l:16;
  488. unsigned int z_h:8;
  489. unsigned int parity:6;
  490. } w5;
  491. struct {
  492. unsigned int _pad:2;
  493. unsigned int z_l:24;
  494. unsigned int parity:6;
  495. } w6;
  496. } type3;
  497. /* msg 4 - reference station datum */
  498. struct rtcm2_msg4 {
  499. struct {
  500. unsigned int _pad:2;
  501. unsigned int dgnss:3;
  502. unsigned int dat:1;
  503. unsigned int spare:4;
  504. unsigned int datum_alpha_char1:8;
  505. unsigned int datum_alpha_char2:8;
  506. unsigned int parity:6;
  507. } w3;
  508. struct {
  509. unsigned int _pad:2;
  510. unsigned int datum_sub_div_char3:8;
  511. unsigned int datum_sub_div_char1:8;
  512. unsigned int datum_sub_div_char2:8;
  513. unsigned int parity:6;
  514. } w4;
  515. struct {
  516. unsigned int _pad:2;
  517. unsigned int dx:16;
  518. unsigned int dy_h:8;
  519. unsigned int parity:6;
  520. } w5;
  521. struct {
  522. unsigned int _pad:2;
  523. unsigned int dy_l:8;
  524. unsigned int dz:24;
  525. unsigned int parity:6;
  526. } w6;
  527. } type4;
  528. /* msg 5 - constellation health */
  529. struct rtcm2_msg5 {
  530. struct b_health_t {
  531. unsigned int _pad:2;
  532. unsigned int reserved:1;
  533. unsigned int sat_id:5;
  534. unsigned int issue_of_data_link:1;
  535. unsigned int data_health:3;
  536. unsigned int cn0:5;
  537. unsigned int health_enable:1;
  538. unsigned int new_nav_data:1;
  539. unsigned int loss_warn:1;
  540. unsigned int time_unhealthy:4;
  541. unsigned int unassigned:2;
  542. unsigned int parity:6;
  543. } health[MAXHEALTH];
  544. } type5;
  545. /* msg 6 - null message */
  546. /* msg 7 - beacon almanac */
  547. struct rtcm2_msg7 {
  548. struct b_station_t {
  549. struct {
  550. unsigned int _pad:2;
  551. int lat:16;
  552. int lon_h:8;
  553. unsigned int parity:6;
  554. } w3;
  555. struct {
  556. unsigned int _pad:2;
  557. unsigned int lon_l:8;
  558. unsigned int range:10;
  559. unsigned int freq_h:6;
  560. unsigned int parity:6;
  561. } w4;
  562. struct {
  563. unsigned int _pad:2;
  564. unsigned int freq_l:6;
  565. unsigned int health:2;
  566. unsigned int station_id:10;
  567. /* see comments in LE struct above. */
  568. unsigned int bit_rate:3;
  569. unsigned int mod_mode:1;
  570. unsigned int sync_type:1;
  571. unsigned int encoding:1;
  572. unsigned int parity:6;
  573. } w5;
  574. } almanac[(RTCM2_WORDS_MAX - 2)/3];
  575. } type7;
  576. /* msg 13 - Ground Transmitter Parameters (RTCM2.3 only) */
  577. struct rtcm2_msg13 {
  578. struct {
  579. unsigned int _pad:2;
  580. unsigned int status:1;
  581. unsigned int rangeflag:1;
  582. unsigned int reserved:6;
  583. int lat:16;
  584. unsigned int parity:6;
  585. } w1;
  586. struct {
  587. unsigned int _pad:2;
  588. int lon:16;
  589. unsigned int range:8;
  590. unsigned int parity:6;
  591. } w2;
  592. } type13;
  593. /* msg 14 - GPS Time of Week (RTCM2.3 only) */
  594. struct rtcm2_msg14 {
  595. struct {
  596. unsigned int _pad:2;
  597. unsigned int week:10;
  598. unsigned int hour:8;
  599. unsigned int leapsecs:6;
  600. unsigned int parity:6;
  601. } w1;
  602. } type14;
  603. /* msg 16 - text msg */
  604. struct rtcm2_msg16 {
  605. struct {
  606. unsigned int _pad:2;
  607. unsigned int byte1:8;
  608. unsigned int byte2:8;
  609. unsigned int byte3:8;
  610. unsigned int parity:6;
  611. } txt[RTCM2_WORDS_MAX-2];
  612. } type16;
  613. /* msg 31 - differential GLONASS corrections */
  614. struct rtcm2_msg31 {
  615. struct glonass_correction_t {
  616. struct { /* msg 1 word 3 */
  617. unsigned int _pad:2;
  618. unsigned int scale1:1;
  619. unsigned int udre1:2;
  620. unsigned int satident1:5; /* satellite ID */
  621. int prc1:16;
  622. unsigned int parity:6;
  623. } w3;
  624. struct { /* msg 1 word 4 */
  625. unsigned int _pad:2;
  626. int rrc1:8;
  627. unsigned int change1:1;
  628. unsigned int tod1:7;
  629. unsigned int scale2:1;
  630. unsigned int udre2:2;
  631. unsigned int satident2:5; /* satellite ID */
  632. unsigned int parity:6;
  633. } w4;
  634. struct { /* msg 1 word 5 */
  635. unsigned int _pad:2;
  636. int prc2:16;
  637. int rrc2:8;
  638. unsigned int parity:6;
  639. } w5;
  640. struct { /* msg 1 word 6 */
  641. unsigned int _pad:2;
  642. unsigned int change2:1;
  643. unsigned int tod2:7;
  644. unsigned int scale3:1;
  645. unsigned int udre3:2;
  646. unsigned int satident3:5; /* satellite ID */
  647. int prc3_h:8;
  648. unsigned int parity:6;
  649. } w6;
  650. struct { /* msg 1 word 7 */
  651. unsigned int _pad:2;
  652. unsigned int prc3_l:8; /* NOTE: unsigned for low byte */
  653. int rrc3:8;
  654. unsigned int change3:1;
  655. unsigned int tod3:7;
  656. unsigned int parity:6;
  657. } w7;
  658. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  659. } type31;
  660. /* unknown message */
  661. isgps30bits_t rtcm2_msgunk[RTCM2_WORDS_MAX-2];
  662. } msg_type;
  663. } __attribute__((__packed__));
  664. #endif /* BIG ENDIAN */
  665. #ifdef RTCM104V2_ENABLE
  666. #define PREAMBLE_PATTERN 0x66
  667. static unsigned int tx_speed[] = { 25, 50, 100, 110, 150, 200, 250, 300 };
  668. #define DIMENSION(a) (unsigned)(sizeof(a)/sizeof(a[0]))
  669. void rtcm2_unpack(struct rtcm2_t *tp, char *buf)
  670. /* break out the raw bits into the content fields */
  671. {
  672. int len;
  673. unsigned int n, w;
  674. struct rtcm2_msg_t *msg = (struct rtcm2_msg_t *)buf;
  675. tp->type = msg->w1.msgtype;
  676. tp->length = msg->w2.frmlen;
  677. tp->zcount = msg->w2.zcnt * ZCOUNT_SCALE;
  678. tp->refstaid = msg->w1.refstaid;
  679. tp->seqnum = msg->w2.sqnum;
  680. tp->stathlth = msg->w2.stathlth;
  681. len = (int)tp->length;
  682. n = 0;
  683. switch (tp->type) {
  684. case 1:
  685. case 9:
  686. {
  687. struct gps_correction_t *m = &msg->msg_type.type1.corrections[0];
  688. while (len >= 0) {
  689. if (len >= 2) {
  690. tp->gps_ranges.sat[n].ident = m->w3.satident1;
  691. tp->gps_ranges.sat[n].udre = m->w3.udre1;
  692. tp->gps_ranges.sat[n].iod = m->w4.iod1;
  693. tp->gps_ranges.sat[n].prc = m->w3.prc1 *
  694. (m->w3.scale1 ? PRCLARGE : PRCSMALL);
  695. tp->gps_ranges.sat[n].rrc = m->w4.rrc1 *
  696. (m->w3.scale1 ? RRLARGE : RRSMALL);
  697. n++;
  698. }
  699. if (len >= 4) {
  700. tp->gps_ranges.sat[n].ident = m->w4.satident2;
  701. tp->gps_ranges.sat[n].udre = m->w4.udre2;
  702. tp->gps_ranges.sat[n].iod = m->w6.iod2;
  703. tp->gps_ranges.sat[n].prc = m->w5.prc2 *
  704. (m->w4.scale2 ? PRCLARGE : PRCSMALL);
  705. tp->gps_ranges.sat[n].rrc = m->w5.rrc2 *
  706. (m->w4.scale2 ? RRLARGE : RRSMALL);
  707. n++;
  708. }
  709. if (len >= 5) {
  710. tp->gps_ranges.sat[n].ident = m->w6.satident3;
  711. tp->gps_ranges.sat[n].udre = m->w6.udre3;
  712. tp->gps_ranges.sat[n].iod = m->w7.iod3;
  713. tp->gps_ranges.sat[n].prc =
  714. ((m->w6.prc3_h << 8) | (m->w7.prc3_l)) *
  715. (m->w6.scale3 ? PRCLARGE : PRCSMALL);
  716. tp->gps_ranges.sat[n].rrc =
  717. m->w7.rrc3 * (m->w6.scale3 ? RRLARGE : RRSMALL);
  718. n++;
  719. }
  720. len -= 5;
  721. m++;
  722. }
  723. tp->gps_ranges.nentries = n;
  724. }
  725. break;
  726. case 3:
  727. {
  728. struct rtcm2_msg3 *m = &msg->msg_type.type3;
  729. if ((tp->ecef.valid = len >= 4)) {
  730. tp->ecef.x = ((m->w3.x_h << 8) | (m->w4.x_l)) * XYZ_SCALE;
  731. tp->ecef.y = ((m->w4.y_h << 16) | (m->w5.y_l)) * XYZ_SCALE;
  732. tp->ecef.z = ((m->w5.z_h << 24) | (m->w6.z_l)) * XYZ_SCALE;
  733. }
  734. }
  735. break;
  736. case 4:
  737. if ((tp->reference.valid = len >= 2)) {
  738. struct rtcm2_msg4 *m = &msg->msg_type.type4;
  739. tp->reference.system =
  740. (m->w3.dgnss == 0) ? NAVSYSTEM_GPS :
  741. ((m->w3.dgnss == 1) ? NAVSYSTEM_GLONASS : NAVSYSTEM_UNKNOWN);
  742. tp->reference.sense =
  743. (m->w3.dat != 0) ? SENSE_GLOBAL : SENSE_LOCAL;
  744. if (m->w3.datum_alpha_char1) {
  745. tp->reference.datum[n++] = (char)(m->w3.datum_alpha_char1);
  746. }
  747. if (m->w3.datum_alpha_char2) {
  748. tp->reference.datum[n++] = (char)(m->w3.datum_alpha_char2);
  749. }
  750. if (m->w4.datum_sub_div_char1) {
  751. tp->reference.datum[n++] = (char)(m->w4.datum_sub_div_char1);
  752. }
  753. if (m->w4.datum_sub_div_char2) {
  754. tp->reference.datum[n++] = (char)(m->w4.datum_sub_div_char2);
  755. }
  756. if (m->w4.datum_sub_div_char3) {
  757. tp->reference.datum[n++] = (char)(m->w4.datum_sub_div_char3);
  758. }
  759. /* we used to say n++ here, but scan-build complains */
  760. tp->reference.datum[n] = '\0';
  761. if (len >= 4) {
  762. tp->reference.dx = m->w5.dx * DXYZ_SCALE;
  763. tp->reference.dy =
  764. ((m->w5.dy_h << 8) | m->w6.dy_l) * DXYZ_SCALE;
  765. tp->reference.dz = m->w6.dz * DXYZ_SCALE;
  766. } else
  767. tp->reference.sense = SENSE_INVALID;
  768. }
  769. break;
  770. case 5:
  771. for (n = 0; n < (unsigned)len; n++) {
  772. struct consat_t *csp = &tp->conhealth.sat[n];
  773. struct b_health_t *m = &msg->msg_type.type5.health[n];
  774. csp->ident = m->sat_id;
  775. csp->iodl = m->issue_of_data_link != 0;
  776. csp->health = m->data_health;
  777. csp->snr = (int)(m->cn0 ? (m->cn0 + CNR_OFFSET) : SNR_BAD);
  778. csp->health_en = m->health_enable != 0;
  779. csp->new_data = m->new_nav_data != 0;
  780. csp->los_warning = m->loss_warn != 0;
  781. csp->tou = m->time_unhealthy * TU_SCALE;
  782. }
  783. tp->conhealth.nentries = n;
  784. break;
  785. case 7:
  786. for (w = 0; w < (unsigned)len; w++) {
  787. struct station_t *np = &tp->almanac.station[n];
  788. struct b_station_t *mp = &msg->msg_type.type7.almanac[w];
  789. np->latitude = mp->w3.lat * LA_SCALE;
  790. np->longitude = ((mp->w3.lon_h << 8) | mp->w4.lon_l) * LO_SCALE;
  791. np->range = mp->w4.range;
  792. np->frequency =
  793. (((mp->w4.freq_h << 6) | mp->w5.freq_l) * FREQ_SCALE) +
  794. FREQ_OFFSET;
  795. np->health = mp->w5.health;
  796. np->station_id = mp->w5.station_id,
  797. np->bitrate = tx_speed[mp->w5.bit_rate];
  798. n++;
  799. }
  800. tp->almanac.nentries = (unsigned)(len / 3);
  801. break;
  802. case 13:
  803. tp->xmitter.status = (bool)msg->msg_type.type13.w1.status;
  804. tp->xmitter.rangeflag = (bool)msg->msg_type.type13.w1.rangeflag;
  805. tp->xmitter.lat = msg->msg_type.type13.w1.lat * LATLON_SCALE;
  806. tp->xmitter.lon = msg->msg_type.type13.w2.lon * LATLON_SCALE;
  807. tp->xmitter.range = msg->msg_type.type13.w2.range * RANGE_SCALE;
  808. if (tp->xmitter.range == 0)
  809. tp->xmitter.range = 1024;
  810. break;
  811. case 14:
  812. tp->gpstime.week = msg->msg_type.type14.w1.week;
  813. tp->gpstime.hour = msg->msg_type.type14.w1.hour;
  814. tp->gpstime.leapsecs = msg->msg_type.type14.w1.leapsecs;
  815. break;
  816. case 16:
  817. for (w = 0; w < (unsigned)len; w++) {
  818. if (!msg->msg_type.type16.txt[w].byte1) {
  819. break;
  820. }
  821. tp->message[n++] = (char)(msg->msg_type.type16.txt[w].byte1);
  822. if (!msg->msg_type.type16.txt[w].byte2) {
  823. break;
  824. }
  825. tp->message[n++] = (char)(msg->msg_type.type16.txt[w].byte2);
  826. if (!msg->msg_type.type16.txt[w].byte3) {
  827. break;
  828. }
  829. tp->message[n++] = (char)(msg->msg_type.type16.txt[w].byte3);
  830. }
  831. tp->message[n] = '\0';
  832. break;
  833. case 31:
  834. {
  835. struct glonass_correction_t *m = &msg->msg_type.type31.corrections[0];
  836. while (len >= 0) {
  837. if (len >= 2) {
  838. tp->glonass_ranges.sat[n].ident = m->w3.satident1;
  839. tp->glonass_ranges.sat[n].udre = m->w3.udre1;
  840. tp->glonass_ranges.sat[n].change = (bool)m->w4.change1;
  841. tp->glonass_ranges.sat[n].tod = m->w4.tod1;
  842. tp->glonass_ranges.sat[n].prc = m->w3.prc1 *
  843. (m->w3.scale1 ? PRCLARGE : PRCSMALL);
  844. tp->glonass_ranges.sat[n].rrc = m->w4.rrc1 *
  845. (m->w3.scale1 ? RRLARGE : RRSMALL);
  846. n++;
  847. }
  848. if (len >= 4) {
  849. tp->glonass_ranges.sat[n].ident = m->w4.satident2;
  850. tp->glonass_ranges.sat[n].udre = m->w4.udre2;
  851. tp->glonass_ranges.sat[n].change = (bool)m->w6.change2;
  852. tp->glonass_ranges.sat[n].tod = m->w6.tod2;
  853. tp->glonass_ranges.sat[n].prc = m->w5.prc2 *
  854. (m->w4.scale2 ? PRCLARGE : PRCSMALL);
  855. tp->glonass_ranges.sat[n].rrc = m->w5.rrc2 *
  856. (m->w4.scale2 ? RRLARGE : RRSMALL);
  857. n++;
  858. }
  859. if (len >= 5) {
  860. tp->glonass_ranges.sat[n].ident = m->w6.satident3;
  861. tp->glonass_ranges.sat[n].udre = m->w6.udre3;
  862. tp->glonass_ranges.sat[n].change = (bool)m->w7.change3;
  863. tp->glonass_ranges.sat[n].tod = m->w7.tod3;
  864. tp->glonass_ranges.sat[n].prc =
  865. ((m->w6.prc3_h << 8) | (m->w7.prc3_l)) *
  866. (m->w6.scale3 ? PRCLARGE : PRCSMALL);
  867. tp->glonass_ranges.sat[n].rrc =
  868. m->w7.rrc3 * (m->w6.scale3 ? RRLARGE : RRSMALL);
  869. n++;
  870. }
  871. len -= 5;
  872. m++;
  873. }
  874. tp->glonass_ranges.nentries = n;
  875. }
  876. break;
  877. default:
  878. memcpy(tp->words, msg->msg_type.rtcm2_msgunk,
  879. (RTCM2_WORDS_MAX - 2) * sizeof(isgps30bits_t));
  880. break;
  881. }
  882. }
  883. static bool preamble_match(isgps30bits_t * w)
  884. {
  885. return (((struct rtcm2_msghw1 *)w)->preamble == PREAMBLE_PATTERN);
  886. }
  887. static bool length_check(struct gps_lexer_t *lexer)
  888. {
  889. return lexer->isgps.bufindex >= 2
  890. && lexer->isgps.bufindex >=
  891. ((struct rtcm2_msg_t *)lexer->isgps.buf)->w2.frmlen + 2u;
  892. }
  893. enum isgpsstat_t rtcm2_decode(struct gps_lexer_t *lexer, unsigned int c)
  894. {
  895. return isgps_decode(lexer,
  896. preamble_match, length_check, RTCM2_WORDS_MAX, c);
  897. }
  898. #endif /* RTCM104V2_ENABLE */