You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

driver_rtcm2.c 40KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. /* NOTE: unsigned int for low byte */
  207. unsigned int prc3_l:8;
  208. unsigned int _pad:2;
  209. } w7;
  210. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  211. } type1;
  212. /* msg 2 - Pseudo-range corrections, referring to previous orbit
  213. * data records (maximum 12 satellites) */
  214. /* msg 3 - reference station parameters */
  215. struct rtcm2_msg3 {
  216. struct {
  217. unsigned int parity:6;
  218. unsigned int x_h:24;
  219. unsigned int _pad:2;
  220. } w3;
  221. struct {
  222. unsigned int parity:6;
  223. unsigned int y_h:16;
  224. unsigned int x_l:8;
  225. unsigned int _pad:2;
  226. } w4;
  227. struct {
  228. unsigned int parity:6;
  229. unsigned int z_h:8;
  230. unsigned int y_l:16;
  231. unsigned int _pad:2;
  232. } w5;
  233. struct {
  234. unsigned int parity:6;
  235. unsigned int z_l:24;
  236. unsigned int _pad:2;
  237. } w6;
  238. } type3;
  239. /* msg 4 - reference station datum */
  240. struct rtcm2_msg4 {
  241. struct {
  242. unsigned int parity:6;
  243. unsigned int datum_alpha_char2:8;
  244. unsigned int datum_alpha_char1:8;
  245. unsigned int spare:4;
  246. unsigned int dat:1;
  247. unsigned int dgnss:3;
  248. unsigned int _pad:2;
  249. } w3;
  250. struct {
  251. unsigned int parity:6;
  252. unsigned int datum_sub_div_char2:8;
  253. unsigned int datum_sub_div_char1:8;
  254. unsigned int datum_sub_div_char3:8;
  255. unsigned int _pad:2;
  256. } w4;
  257. struct {
  258. unsigned int parity:6;
  259. unsigned int dy_h:8;
  260. unsigned int dx:16;
  261. unsigned int _pad:2;
  262. } w5;
  263. struct {
  264. unsigned int parity:6;
  265. unsigned int dz:24;
  266. unsigned int dy_l:8;
  267. unsigned int _pad:2;
  268. } w6;
  269. } type4;
  270. /* msg 5 - constellation health */
  271. struct rtcm2_msg5 {
  272. struct b_health_t {
  273. unsigned int parity:6;
  274. unsigned int unassigned:2;
  275. unsigned int time_unhealthy:4;
  276. unsigned int loss_warn:1;
  277. unsigned int new_nav_data:1;
  278. unsigned int health_enable:1;
  279. unsigned int cn0:5;
  280. unsigned int data_health:3;
  281. unsigned int issue_of_data_link:1;
  282. unsigned int sat_id:5;
  283. unsigned int reserved:1;
  284. unsigned int _pad:2;
  285. } health[MAXHEALTH];
  286. } type5;
  287. /* msg 6 - null message */
  288. /* msg 7 - beacon almanac */
  289. struct rtcm2_msg7 {
  290. struct b_station_t {
  291. struct {
  292. unsigned int parity:6;
  293. int lon_h:8;
  294. int lat:16;
  295. unsigned int _pad:2;
  296. } w3;
  297. struct {
  298. unsigned int parity:6;
  299. unsigned int freq_h:6;
  300. unsigned int range:10;
  301. unsigned int lon_l:8;
  302. unsigned int _pad:2;
  303. } w4;
  304. struct {
  305. unsigned int parity:6;
  306. unsigned int encoding:1;
  307. unsigned int sync_type:1;
  308. unsigned int mod_mode:1;
  309. unsigned int bit_rate:3;
  310. /*
  311. * ITU-R M.823-2 page 9 and RTCM-SC104 v2.1 pages
  312. * 4-21 and 4-22 are in conflict over the next two
  313. * field sizes. ITU says 9+3, RTCM says 10+2.
  314. * The latter correctly decodes the USCG station
  315. * id's so I'll use that one here. -wsr
  316. */
  317. unsigned int station_id:10;
  318. unsigned int health:2;
  319. unsigned int freq_l:6;
  320. unsigned int _pad:2;
  321. } w5;
  322. } almanac[(RTCM2_WORDS_MAX - 2)/3];
  323. } type7;
  324. // msg 8 - Pseudolite almanac
  325. // msg 9 - GPS Partial correction set
  326. // msg 10 - P-code differential corrections
  327. // msg 11 - C/A-code L1, L2 delta corrections
  328. // msg 12 - Pseudolite station parameters.
  329. /* msg 13 - Ground Transmitter Parameters (RTCM2.3 only) */
  330. struct rtcm2_msg13 {
  331. struct {
  332. unsigned int parity:6;
  333. int lat:16;
  334. unsigned int reserved:6;
  335. unsigned int rangeflag:1;
  336. unsigned int status:1;
  337. unsigned int _pad:2;
  338. } w1;
  339. struct {
  340. unsigned int parity:6;
  341. unsigned int range:8;
  342. int lon:16;
  343. unsigned int _pad:2;
  344. } w2;
  345. } type13;
  346. /* msg 14 - GPS Time of Week (RTCM2.3 only) */
  347. struct rtcm2_msg14 {
  348. struct {
  349. unsigned int parity:6;
  350. unsigned int leapsecs:6;
  351. unsigned int hour:8;
  352. unsigned int week:10;
  353. unsigned int _pad:2;
  354. } w1;
  355. } type14;
  356. // msg 15 - Ionospheric delay message
  357. /* msg 16 - text msg */
  358. struct rtcm2_msg16 {
  359. struct {
  360. unsigned int parity:6;
  361. unsigned int byte3:8;
  362. unsigned int byte2:8;
  363. unsigned int byte1:8;
  364. unsigned int _pad:2;
  365. } txt[RTCM2_WORDS_MAX-2];
  366. } type16;
  367. // msg 17 - GPS ephmerides. RTCM 2.1
  368. // msg 18 - RTK uncorrected carrier phases. RTCM 2.1
  369. // msg 19 - RTK uncorrected psuedoranges. RTCM 2.1
  370. // msg 20 - RTK carrier phase corrections. RTCM 2.1
  371. // msg 21 - RTK/high accuracy psuedorange corrections. RTCM 2.1
  372. // msg 22 - Extended reference station parameters
  373. // msg 23 - Type of Antenna. RTCM 2.3
  374. // msg 24 - Reference station ARP.. RTCM 2.3
  375. // msg 27 - Extended almanac of DGPS.
  376. /* msg 31 - differential GLONASS corrections */
  377. struct rtcm2_msg31 {
  378. struct glonass_correction_t {
  379. struct { /* msg 1 word 3 */
  380. unsigned int parity:6;
  381. int prc1:16;
  382. unsigned int satident1:5; /* satellite ID */
  383. unsigned int udre1:2;
  384. unsigned int scale1:1;
  385. unsigned int _pad:2;
  386. } w3;
  387. struct { /* msg 1 word 4 */
  388. unsigned int parity:6;
  389. unsigned int satident2:5; /* satellite ID */
  390. unsigned int udre2:2;
  391. unsigned int scale2:1;
  392. unsigned int tod1:7;
  393. unsigned int change1:1;
  394. int rrc1:8;
  395. unsigned int _pad:2;
  396. } w4;
  397. struct { /* msg 1 word 5 */
  398. unsigned int parity:6;
  399. int rrc2:8;
  400. int prc2:16;
  401. unsigned int _pad:2;
  402. } w5;
  403. struct { /* msg 1 word 6 */
  404. unsigned int parity:6;
  405. int prc3_h:8;
  406. unsigned int satident3:5; /* satellite ID */
  407. unsigned int udre3:2;
  408. unsigned int scale3:1;
  409. unsigned int tod2:7;
  410. unsigned int change2:1;
  411. unsigned int _pad:2;
  412. } w6;
  413. struct { /* msg 1 word 7 */
  414. unsigned int parity:6;
  415. unsigned int tod3:7;
  416. unsigned int change3:1;
  417. int rrc3:8;
  418. /* NOTE: unsigned int for low byte */
  419. unsigned int prc3_l:8;
  420. unsigned int _pad:2;
  421. } w7;
  422. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  423. } type31;
  424. // msg 32 - Differential GLONASS reference station parameters, RTCM 2.3
  425. // msg 33 - GLONASS constellation health, RTCM 2.3
  426. // msg 34 - DGLONASS corrections, RTCM 2.3
  427. // msg 35 - GLONASS radiobeacon almanac, RTCM 2.3
  428. // msg 36 - GLONASS special message, RTCM 2.3
  429. // msg 37 - GNSS system time offset, RTCM 2.3
  430. /* msg 59 - Proprietary messages, for transmission of any
  431. * required data */
  432. // msg 60-63 - Multipurpsoe usage
  433. /* unknown message */
  434. isgps30bits_t rtcm2_msgunk[RTCM2_WORDS_MAX-2];
  435. } msg_type;
  436. } __attribute__((__packed__));
  437. #endif /* LITTLE_ENDIAN */
  438. #ifdef WORDS_BIGENDIAN
  439. struct rtcm2_msg_t {
  440. struct rtcm2_msghw1 { /* header word 1 */
  441. unsigned int _pad:2;
  442. unsigned int preamble:8; /* fixed at 01100110 */
  443. unsigned int msgtype:6; /* RTCM message type */
  444. unsigned int refstaid:10; /* reference station ID */
  445. unsigned int parity:6;
  446. } w1;
  447. struct rtcm2_msghw2 { /* header word 2 */
  448. unsigned int _pad:2;
  449. unsigned int zcnt:13;
  450. unsigned int sqnum:3;
  451. unsigned int frmlen:5;
  452. unsigned int stathlth:3; /* station health */
  453. unsigned int parity:6;
  454. } w2;
  455. union {
  456. /* msg 1 - differential GPS corrections */
  457. struct rtcm2_msg1 {
  458. struct gps_correction_t {
  459. struct { /* msg 1 word 3 */
  460. unsigned int _pad:2;
  461. unsigned int scale1:1;
  462. unsigned int udre1:2;
  463. unsigned int satident1:5; /* satellite ID */
  464. int prc1:16;
  465. unsigned int parity:6;
  466. } w3;
  467. struct { /* msg 1 word 4 */
  468. unsigned int _pad:2;
  469. int rrc1:8;
  470. unsigned int iod1:8;
  471. unsigned int scale2:1;
  472. unsigned int udre2:2;
  473. unsigned int satident2:5; /* satellite ID */
  474. unsigned int parity:6;
  475. } w4;
  476. struct { /* msg 1 word 5 */
  477. unsigned int _pad:2;
  478. int prc2:16;
  479. int rrc2:8;
  480. unsigned int parity:6;
  481. } w5;
  482. struct { /* msg 1 word 6 */
  483. unsigned int _pad:2;
  484. unsigned int iod2:8;
  485. unsigned int scale3:1;
  486. unsigned int udre3:2;
  487. unsigned int satident3:5; /* satellite ID */
  488. int prc3_h:8;
  489. unsigned int parity:6;
  490. } w6;
  491. struct { /* msg 1 word 7 */
  492. unsigned int _pad:2;
  493. unsigned int prc3_l:8; // NOTE: unsigned for low byte
  494. int rrc3:8;
  495. unsigned int iod3:8;
  496. unsigned int parity:6;
  497. } w7;
  498. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  499. } type1;
  500. /* msg 2 - Pseudo-range corrections, referring to previous orbit
  501. * data records (maximum 12 satellites) */
  502. /* msg 3 - reference station parameters */
  503. struct rtcm2_msg3 {
  504. struct {
  505. unsigned int _pad:2;
  506. unsigned int x_h:24;
  507. unsigned int parity:6;
  508. } w3;
  509. struct {
  510. unsigned int _pad:2;
  511. unsigned int x_l:8;
  512. unsigned int y_h:16;
  513. unsigned int parity:6;
  514. } w4;
  515. struct {
  516. unsigned int _pad:2;
  517. unsigned int y_l:16;
  518. unsigned int z_h:8;
  519. unsigned int parity:6;
  520. } w5;
  521. struct {
  522. unsigned int _pad:2;
  523. unsigned int z_l:24;
  524. unsigned int parity:6;
  525. } w6;
  526. } type3;
  527. /* msg 4 - reference station datum */
  528. struct rtcm2_msg4 {
  529. struct {
  530. unsigned int _pad:2;
  531. unsigned int dgnss:3;
  532. unsigned int dat:1;
  533. unsigned int spare:4;
  534. unsigned int datum_alpha_char1:8;
  535. unsigned int datum_alpha_char2:8;
  536. unsigned int parity:6;
  537. } w3;
  538. struct {
  539. unsigned int _pad:2;
  540. unsigned int datum_sub_div_char3:8;
  541. unsigned int datum_sub_div_char1:8;
  542. unsigned int datum_sub_div_char2:8;
  543. unsigned int parity:6;
  544. } w4;
  545. struct {
  546. unsigned int _pad:2;
  547. unsigned int dx:16;
  548. unsigned int dy_h:8;
  549. unsigned int parity:6;
  550. } w5;
  551. struct {
  552. unsigned int _pad:2;
  553. unsigned int dy_l:8;
  554. unsigned int dz:24;
  555. unsigned int parity:6;
  556. } w6;
  557. } type4;
  558. /* msg 5 - constellation health */
  559. struct rtcm2_msg5 {
  560. struct b_health_t {
  561. unsigned int _pad:2;
  562. unsigned int reserved:1;
  563. unsigned int sat_id:5;
  564. unsigned int issue_of_data_link:1;
  565. unsigned int data_health:3;
  566. unsigned int cn0:5;
  567. unsigned int health_enable:1;
  568. unsigned int new_nav_data:1;
  569. unsigned int loss_warn:1;
  570. unsigned int time_unhealthy:4;
  571. unsigned int unassigned:2;
  572. unsigned int parity:6;
  573. } health[MAXHEALTH];
  574. } type5;
  575. // msg - 6 - Null message, used as filler record during time-outs
  576. /* msg 7 - beacon almanac */
  577. struct rtcm2_msg7 {
  578. struct b_station_t {
  579. struct {
  580. unsigned int _pad:2;
  581. int lat:16;
  582. int lon_h:8;
  583. unsigned int parity:6;
  584. } w3;
  585. struct {
  586. unsigned int _pad:2;
  587. unsigned int lon_l:8;
  588. unsigned int range:10;
  589. unsigned int freq_h:6;
  590. unsigned int parity:6;
  591. } w4;
  592. struct {
  593. unsigned int _pad:2;
  594. unsigned int freq_l:6;
  595. unsigned int health:2;
  596. unsigned int station_id:10;
  597. /* see comments in LE struct above. */
  598. unsigned int bit_rate:3;
  599. unsigned int mod_mode:1;
  600. unsigned int sync_type:1;
  601. unsigned int encoding:1;
  602. unsigned int parity:6;
  603. } w5;
  604. } almanac[(RTCM2_WORDS_MAX - 2)/3];
  605. } type7;
  606. // msg 8 - Pseudolite almanac
  607. // msg 9 - GPS Partial correction set
  608. // msg 10 - P-code differential corrections
  609. // msg 11 - C/A-code L1, L2 delta corrections
  610. // msg 12 - Pseudolite station parameters.
  611. /* msg 13 - Ground Transmitter Parameters (RTCM2.3 only) */
  612. struct rtcm2_msg13 {
  613. struct {
  614. unsigned int _pad:2;
  615. unsigned int status:1;
  616. unsigned int rangeflag:1;
  617. unsigned int reserved:6;
  618. int lat:16;
  619. unsigned int parity:6;
  620. } w1;
  621. struct {
  622. unsigned int _pad:2;
  623. int lon:16;
  624. unsigned int range:8;
  625. unsigned int parity:6;
  626. } w2;
  627. } type13;
  628. /* msg 14 - GPS Time of Week (RTCM2.3 only) */
  629. struct rtcm2_msg14 {
  630. struct {
  631. unsigned int _pad:2;
  632. unsigned int week:10;
  633. unsigned int hour:8;
  634. unsigned int leapsecs:6;
  635. unsigned int parity:6;
  636. } w1;
  637. } type14;
  638. // msg 15 - Ionospheric delay message
  639. /* msg 16 - text msg */
  640. struct rtcm2_msg16 {
  641. struct {
  642. unsigned int _pad:2;
  643. unsigned int byte1:8;
  644. unsigned int byte2:8;
  645. unsigned int byte3:8;
  646. unsigned int parity:6;
  647. } txt[RTCM2_WORDS_MAX-2];
  648. } type16;
  649. // msg 17 - GPS ephmerides. RTCM 2.1
  650. // msg 18 - RTK uncorrected carrier phases. RTCM 2.1
  651. // msg 19 - RTK uncorrected psuedoranges. RTCM 2.1
  652. // msg 20 - RTK carrier phase corrections. RTCM 2.1
  653. // msg 21 - RTK/high accuracy psuedorange corrections. RTCM 2.1
  654. // msg 22 - Extended reference station parameters
  655. // msg 23 - Type of Antenna. RTCM 2.3
  656. // msg 24 - Reference station ARP.. RTCM 2.3
  657. // msg 27 - Extended almanac of DGPS.
  658. /* msg 31 - differential GLONASS corrections */
  659. struct rtcm2_msg31 {
  660. struct glonass_correction_t {
  661. struct { /* msg 1 word 3 */
  662. unsigned int _pad:2;
  663. unsigned int scale1:1;
  664. unsigned int udre1:2;
  665. unsigned int satident1:5; /* satellite ID */
  666. int prc1:16;
  667. unsigned int parity:6;
  668. } w3;
  669. struct { /* msg 1 word 4 */
  670. unsigned int _pad:2;
  671. int rrc1:8;
  672. unsigned int change1:1;
  673. unsigned int tod1:7;
  674. unsigned int scale2:1;
  675. unsigned int udre2:2;
  676. unsigned int satident2:5; /* satellite ID */
  677. unsigned int parity:6;
  678. } w4;
  679. struct { /* msg 1 word 5 */
  680. unsigned int _pad:2;
  681. int prc2:16;
  682. int rrc2:8;
  683. unsigned int parity:6;
  684. } w5;
  685. struct { /* msg 1 word 6 */
  686. unsigned int _pad:2;
  687. unsigned int change2:1;
  688. unsigned int tod2:7;
  689. unsigned int scale3:1;
  690. unsigned int udre3:2;
  691. unsigned int satident3:5; /* satellite ID */
  692. int prc3_h:8;
  693. unsigned int parity:6;
  694. } w6;
  695. struct { /* msg 1 word 7 */
  696. unsigned int _pad:2;
  697. unsigned int prc3_l:8; // NOTE: unsigned for low byte
  698. int rrc3:8;
  699. unsigned int change3:1;
  700. unsigned int tod3:7;
  701. unsigned int parity:6;
  702. } w7;
  703. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  704. } type31;
  705. // msg 32 - Differential GLONASS reference station parameters, RTCM 2.3
  706. // msg 33 - GLONASS constellation health, RTCM 2.3
  707. // msg 34 - DGLONASS corrections, RTCM 2.3
  708. // msg 35 - GLONASS radiobeacon almanac, RTCM 2.3
  709. // msg 36 - GLONASS special message, RTCM 2.3
  710. // msg 37 - GNSS system time offset, RTCM 2.3
  711. /* msg 59 - Proprietary messages, for transmission of any
  712. * required data */
  713. // msg 60-63 - Multipurpsoe usage
  714. /* unknown message */
  715. isgps30bits_t rtcm2_msgunk[RTCM2_WORDS_MAX-2];
  716. } msg_type;
  717. } __attribute__((__packed__));
  718. #endif /* BIG ENDIAN */
  719. #ifdef RTCM104V2_ENABLE
  720. #define PREAMBLE_PATTERN 0x66
  721. static unsigned int tx_speed[] = { 25, 50, 100, 110, 150, 200, 250, 300 };
  722. #define DIMENSION(a) (unsigned)(sizeof(a)/sizeof(a[0]))
  723. void rtcm2_unpack(struct rtcm2_t *tp, char *buf)
  724. /* break out the raw bits into the content fields */
  725. {
  726. int len;
  727. unsigned int n, w;
  728. struct rtcm2_msg_t *msg = (struct rtcm2_msg_t *)buf;
  729. tp->type = msg->w1.msgtype;
  730. tp->length = msg->w2.frmlen;
  731. tp->zcount = msg->w2.zcnt * ZCOUNT_SCALE;
  732. tp->refstaid = msg->w1.refstaid;
  733. tp->seqnum = msg->w2.sqnum;
  734. tp->stathlth = msg->w2.stathlth;
  735. len = (int)tp->length;
  736. n = 0;
  737. switch (tp->type) {
  738. case 1:
  739. case 9:
  740. {
  741. struct gps_correction_t *m = &msg->msg_type.type1.corrections[0];
  742. while (len >= 0) {
  743. if (len >= 2) {
  744. tp->gps_ranges.sat[n].ident = m->w3.satident1;
  745. tp->gps_ranges.sat[n].udre = m->w3.udre1;
  746. tp->gps_ranges.sat[n].iod = m->w4.iod1;
  747. tp->gps_ranges.sat[n].prc = m->w3.prc1 *
  748. (m->w3.scale1 ? PRCLARGE : PRCSMALL);
  749. tp->gps_ranges.sat[n].rrc = m->w4.rrc1 *
  750. (m->w3.scale1 ? RRLARGE : RRSMALL);
  751. n++;
  752. }
  753. if (len >= 4) {
  754. tp->gps_ranges.sat[n].ident = m->w4.satident2;
  755. tp->gps_ranges.sat[n].udre = m->w4.udre2;
  756. tp->gps_ranges.sat[n].iod = m->w6.iod2;
  757. tp->gps_ranges.sat[n].prc = m->w5.prc2 *
  758. (m->w4.scale2 ? PRCLARGE : PRCSMALL);
  759. tp->gps_ranges.sat[n].rrc = m->w5.rrc2 *
  760. (m->w4.scale2 ? RRLARGE : RRSMALL);
  761. n++;
  762. }
  763. if (len >= 5) {
  764. tp->gps_ranges.sat[n].ident = m->w6.satident3;
  765. tp->gps_ranges.sat[n].udre = m->w6.udre3;
  766. tp->gps_ranges.sat[n].iod = m->w7.iod3;
  767. tp->gps_ranges.sat[n].prc =
  768. ((m->w6.prc3_h << 8) | (m->w7.prc3_l)) *
  769. (m->w6.scale3 ? PRCLARGE : PRCSMALL);
  770. tp->gps_ranges.sat[n].rrc =
  771. m->w7.rrc3 * (m->w6.scale3 ? RRLARGE : RRSMALL);
  772. n++;
  773. }
  774. len -= 5;
  775. m++;
  776. }
  777. tp->gps_ranges.nentries = n;
  778. }
  779. break;
  780. case 3:
  781. {
  782. struct rtcm2_msg3 *m = &msg->msg_type.type3;
  783. if ((tp->ecef.valid = len >= 4)) {
  784. tp->ecef.x = ((m->w3.x_h << 8) | (m->w4.x_l)) * XYZ_SCALE;
  785. tp->ecef.y = ((m->w4.y_h << 16) | (m->w5.y_l)) * XYZ_SCALE;
  786. tp->ecef.z = ((m->w5.z_h << 24) | (m->w6.z_l)) * XYZ_SCALE;
  787. }
  788. }
  789. break;
  790. case 4:
  791. if ((tp->reference.valid = len >= 2)) {
  792. struct rtcm2_msg4 *m = &msg->msg_type.type4;
  793. tp->reference.system =
  794. (m->w3.dgnss == 0) ? NAVSYSTEM_GPS :
  795. ((m->w3.dgnss == 1) ? NAVSYSTEM_GLONASS : NAVSYSTEM_UNKNOWN);
  796. tp->reference.sense =
  797. (m->w3.dat != 0) ? SENSE_GLOBAL : SENSE_LOCAL;
  798. if (m->w3.datum_alpha_char1) {
  799. tp->reference.datum[n++] = (char)(m->w3.datum_alpha_char1);
  800. }
  801. if (m->w3.datum_alpha_char2) {
  802. tp->reference.datum[n++] = (char)(m->w3.datum_alpha_char2);
  803. }
  804. if (m->w4.datum_sub_div_char1) {
  805. tp->reference.datum[n++] = (char)(m->w4.datum_sub_div_char1);
  806. }
  807. if (m->w4.datum_sub_div_char2) {
  808. tp->reference.datum[n++] = (char)(m->w4.datum_sub_div_char2);
  809. }
  810. if (m->w4.datum_sub_div_char3) {
  811. tp->reference.datum[n++] = (char)(m->w4.datum_sub_div_char3);
  812. }
  813. /* we used to say n++ here, but scan-build complains */
  814. tp->reference.datum[n] = '\0';
  815. if (len >= 4) {
  816. tp->reference.dx = m->w5.dx * DXYZ_SCALE;
  817. tp->reference.dy =
  818. ((m->w5.dy_h << 8) | m->w6.dy_l) * DXYZ_SCALE;
  819. tp->reference.dz = m->w6.dz * DXYZ_SCALE;
  820. } else
  821. tp->reference.sense = SENSE_INVALID;
  822. }
  823. break;
  824. case 5:
  825. for (n = 0; n < (unsigned)len; n++) {
  826. struct consat_t *csp = &tp->conhealth.sat[n];
  827. struct b_health_t *m = &msg->msg_type.type5.health[n];
  828. csp->ident = m->sat_id;
  829. csp->iodl = m->issue_of_data_link != 0;
  830. csp->health = m->data_health;
  831. csp->snr = (int)(m->cn0 ? (m->cn0 + CNR_OFFSET) : SNR_BAD);
  832. csp->health_en = m->health_enable != 0;
  833. csp->new_data = m->new_nav_data != 0;
  834. csp->los_warning = m->loss_warn != 0;
  835. csp->tou = m->time_unhealthy * TU_SCALE;
  836. }
  837. tp->conhealth.nentries = n;
  838. break;
  839. case 7:
  840. for (w = 0; w < (unsigned)len; w++) {
  841. struct station_t *np = &tp->almanac.station[n];
  842. struct b_station_t *mp = &msg->msg_type.type7.almanac[w];
  843. np->latitude = mp->w3.lat * LA_SCALE;
  844. np->longitude = ((mp->w3.lon_h << 8) | mp->w4.lon_l) * LO_SCALE;
  845. np->range = mp->w4.range;
  846. np->frequency =
  847. (((mp->w4.freq_h << 6) | mp->w5.freq_l) * FREQ_SCALE) +
  848. FREQ_OFFSET;
  849. np->health = mp->w5.health;
  850. np->station_id = mp->w5.station_id,
  851. np->bitrate = tx_speed[mp->w5.bit_rate];
  852. n++;
  853. }
  854. tp->almanac.nentries = (unsigned)(len / 3);
  855. break;
  856. case 13:
  857. tp->xmitter.status = (bool)msg->msg_type.type13.w1.status;
  858. tp->xmitter.rangeflag = (bool)msg->msg_type.type13.w1.rangeflag;
  859. tp->xmitter.lat = msg->msg_type.type13.w1.lat * LATLON_SCALE;
  860. tp->xmitter.lon = msg->msg_type.type13.w2.lon * LATLON_SCALE;
  861. tp->xmitter.range = msg->msg_type.type13.w2.range * RANGE_SCALE;
  862. if (tp->xmitter.range == 0)
  863. tp->xmitter.range = 1024;
  864. break;
  865. case 14:
  866. tp->gpstime.week = msg->msg_type.type14.w1.week;
  867. tp->gpstime.hour = msg->msg_type.type14.w1.hour;
  868. tp->gpstime.leapsecs = msg->msg_type.type14.w1.leapsecs;
  869. break;
  870. case 16:
  871. for (w = 0; w < (unsigned)len; w++) {
  872. if (!msg->msg_type.type16.txt[w].byte1) {
  873. break;
  874. }
  875. tp->message[n++] = (char)(msg->msg_type.type16.txt[w].byte1);
  876. if (!msg->msg_type.type16.txt[w].byte2) {
  877. break;
  878. }
  879. tp->message[n++] = (char)(msg->msg_type.type16.txt[w].byte2);
  880. if (!msg->msg_type.type16.txt[w].byte3) {
  881. break;
  882. }
  883. tp->message[n++] = (char)(msg->msg_type.type16.txt[w].byte3);
  884. }
  885. tp->message[n] = '\0';
  886. break;
  887. case 31:
  888. {
  889. struct glonass_correction_t *m = &msg->msg_type.type31.corrections[0];
  890. while (len >= 0) {
  891. if (len >= 2) {
  892. tp->glonass_ranges.sat[n].ident = m->w3.satident1;
  893. tp->glonass_ranges.sat[n].udre = m->w3.udre1;
  894. tp->glonass_ranges.sat[n].change = (bool)m->w4.change1;
  895. tp->glonass_ranges.sat[n].tod = m->w4.tod1;
  896. tp->glonass_ranges.sat[n].prc = m->w3.prc1 *
  897. (m->w3.scale1 ? PRCLARGE : PRCSMALL);
  898. tp->glonass_ranges.sat[n].rrc = m->w4.rrc1 *
  899. (m->w3.scale1 ? RRLARGE : RRSMALL);
  900. n++;
  901. }
  902. if (len >= 4) {
  903. tp->glonass_ranges.sat[n].ident = m->w4.satident2;
  904. tp->glonass_ranges.sat[n].udre = m->w4.udre2;
  905. tp->glonass_ranges.sat[n].change = (bool)m->w6.change2;
  906. tp->glonass_ranges.sat[n].tod = m->w6.tod2;
  907. tp->glonass_ranges.sat[n].prc = m->w5.prc2 *
  908. (m->w4.scale2 ? PRCLARGE : PRCSMALL);
  909. tp->glonass_ranges.sat[n].rrc = m->w5.rrc2 *
  910. (m->w4.scale2 ? RRLARGE : RRSMALL);
  911. n++;
  912. }
  913. if (len >= 5) {
  914. tp->glonass_ranges.sat[n].ident = m->w6.satident3;
  915. tp->glonass_ranges.sat[n].udre = m->w6.udre3;
  916. tp->glonass_ranges.sat[n].change = (bool)m->w7.change3;
  917. tp->glonass_ranges.sat[n].tod = m->w7.tod3;
  918. tp->glonass_ranges.sat[n].prc =
  919. ((m->w6.prc3_h << 8) | (m->w7.prc3_l)) *
  920. (m->w6.scale3 ? PRCLARGE : PRCSMALL);
  921. tp->glonass_ranges.sat[n].rrc =
  922. m->w7.rrc3 * (m->w6.scale3 ? RRLARGE : RRSMALL);
  923. n++;
  924. }
  925. len -= 5;
  926. m++;
  927. }
  928. tp->glonass_ranges.nentries = n;
  929. }
  930. break;
  931. default:
  932. memcpy(tp->words, msg->msg_type.rtcm2_msgunk,
  933. (RTCM2_WORDS_MAX - 2) * sizeof(isgps30bits_t));
  934. break;
  935. }
  936. }
  937. static bool preamble_match(isgps30bits_t * w)
  938. {
  939. return (((struct rtcm2_msghw1 *)w)->preamble == PREAMBLE_PATTERN);
  940. }
  941. static bool length_check(struct gps_lexer_t *lexer)
  942. {
  943. return lexer->isgps.bufindex >= 2
  944. && lexer->isgps.bufindex >=
  945. ((struct rtcm2_msg_t *)lexer->isgps.buf)->w2.frmlen + 2u;
  946. }
  947. enum isgpsstat_t rtcm2_decode(struct gps_lexer_t *lexer, unsigned int c)
  948. {
  949. return isgps_decode(lexer,
  950. preamble_match, length_check, RTCM2_WORDS_MAX, c);
  951. }
  952. #endif /* RTCM104V2_ENABLE */
  953. // vim: set expandtab shiftwidth=4