driver_rtcm2.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  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 2010 by the GPSD project
  45. SPDX-License-Identifier: BSD-2-clause
  46. *****************************************************************************/
  47. #include "../include/gpsd_config.h" /* must be before all includes */
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include "../include/compiler.h" // for FALLTHROUGH
  51. #include "../include/gpsd.h"
  52. /*
  53. __BYTE_ORDER__, __ORDER_BIG_ENDIAN__ and __ORDER_LITTLE_ENDIAN__ are
  54. defined in some gcc versions only, probably depending on the
  55. architecture. Try to use endian.h if the gcc way fails - endian.h also
  56. does not seem to be available on all platforms.
  57. */
  58. #if HAVE_BUILTIN_ENDIANNESS
  59. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  60. #define WORDS_BIGENDIAN 1
  61. #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  62. #undef WORDS_BIGENDIAN
  63. #else
  64. #error Unknown endianness!
  65. #endif
  66. #else /* HAVE_BUILTIN_ENDIANNESS */
  67. #if defined(HAVE_ENDIAN_H)
  68. #include <endian.h>
  69. #elif defined(HAVE_SYS_ENDIAN_H)
  70. #include <sys/endian.h>
  71. #elif defined(HAVE_MACHINE_ENDIAN_H)
  72. #include <machine/endian.h>
  73. #endif
  74. /*
  75. * Darwin (Mac OS X) uses special defines.
  76. * This must precede the BSD case, since _BIG_ENDIAN may be incorrectly defined
  77. */
  78. #if !defined( __BYTE_ORDER) && defined(__DARWIN_BYTE_ORDER)
  79. #define __BYTE_ORDER __DARWIN_BYTE_ORDER
  80. #endif
  81. #if !defined( __BIG_ENDIAN) && defined(__DARWIN_BIG_ENDIAN)
  82. #define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
  83. #endif
  84. #if !defined( __LITTLE_ENDIAN) && defined(__DARWIN_LITTLE_ENDIAN)
  85. #define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
  86. #endif
  87. /*
  88. * BSD uses _BYTE_ORDER, and Linux uses __BYTE_ORDER.
  89. */
  90. #if !defined( __BYTE_ORDER) && defined(_BYTE_ORDER)
  91. #define __BYTE_ORDER _BYTE_ORDER
  92. #endif
  93. #if !defined( __BIG_ENDIAN) && defined(_BIG_ENDIAN)
  94. #define __BIG_ENDIAN _BIG_ENDIAN
  95. #endif
  96. #if !defined( __LITTLE_ENDIAN) && defined(_LITTLE_ENDIAN)
  97. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  98. #endif
  99. #if !defined(__BYTE_ORDER) || !defined(__BIG_ENDIAN) || \
  100. !defined(__LITTLE_ENDIAN)
  101. #error endianness macros are not defined
  102. #endif
  103. #if __BYTE_ORDER == __BIG_ENDIAN
  104. #define WORDS_BIGENDIAN 1
  105. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  106. #undef WORDS_BIGENDIAN
  107. #else
  108. #error Unknown endianness!
  109. #endif /* __BYTE_ORDER */
  110. #endif /* HAVE_BUILTIN_ENDIANNESS */
  111. /*
  112. * Structures for interpreting words in an RTCM-104 2.x message (after
  113. * parity checking and removing inversion). Note, these structures
  114. * are overlaid on the raw data in order to decode them into
  115. * bitfields; this will fail horribly if your C compiler introduces
  116. * padding between or before bit fields, or between 8-bit-aligned
  117. * bitfields and character arrays despite #pragma pack(1). The right
  118. * things happen under gcc 4.x on amd64, i386, ia64, all arm and mips
  119. * variants, m68k, and powerpc)
  120. *
  121. * (In practice, the only class of machines on which this is likely
  122. * to fail are word-aligned architectures without barrel shifters.
  123. * Very few of these are left in 2012. By test, we know of s390, s390x,
  124. * and sparc.)
  125. *
  126. * The RTCM 2.1 standard is less explicit than it should be about
  127. * signed-integer representations. Two's complement is specified for
  128. * some but not all.
  129. */
  130. #define ZCOUNT_SCALE 0.6 /* sec */
  131. #define PRCSMALL 0.02 /* meters */
  132. #define PRCLARGE 0.32 /* meters */
  133. #define RRSMALL 0.002 /* meters/sec */
  134. #define RRLARGE 0.032 /* meters/sec */
  135. #define MAXPCSMALL (0x7FFF * PCSMALL) /* 16-bits signed */
  136. #define MAXRRSMALL (0x7F * RRSMALL) /* 8-bits signed */
  137. #define XYZ_SCALE 0.01 /* meters */
  138. // extended ECEF scale
  139. #define EXYZ_SCALE 0.0001 // meters
  140. // ECEF delta scale
  141. #define DXYZ_SCALE 0.1 /* meters */
  142. // extended ECEF delta scale: 1/256 cm
  143. #define EDXYZ_SCALE (1.0/256.0) // centimeter
  144. // L2 extended ECEF delta scale: 1/16 cm
  145. #define EDXYZ2_SCALE (1.0/16.0) // centimeter
  146. #define LA_SCALE (90.0/32767.0) /* degrees */
  147. #define LO_SCALE (180.0/32767.0) /* degrees */
  148. #define FREQ_SCALE 0.1 /* kHz */
  149. #define FREQ_OFFSET 190.0 /* kHz */
  150. #define CNR_OFFSET 24 /* dB */
  151. #define TU_SCALE 5 /* minutes */
  152. #define LATLON_SCALE 0.01 /* degrees */
  153. #define RANGE_SCALE 4 /* kilometers */
  154. #pragma pack(1)
  155. /*
  156. * Reminder: Emacs reverse-region is useful...
  157. */
  158. #ifndef WORDS_BIGENDIAN /* little-endian, like x86, amd64 */
  159. struct rtcm2_msg_t {
  160. struct rtcm2_msghw1 { /* header word 1 */
  161. unsigned int parity:6;
  162. unsigned int refstaid:10; /* reference station ID */
  163. unsigned int msgtype:6; /* RTCM message type */
  164. unsigned int preamble:8; /* fixed at 01100110 */
  165. unsigned int _pad:2;
  166. } w1;
  167. struct rtcm2_msghw2 { /* header word 2 */
  168. unsigned int parity:6;
  169. unsigned int stathlth:3; /* station health */
  170. unsigned int frmlen:5;
  171. unsigned int sqnum:3;
  172. unsigned int zcnt:13;
  173. unsigned int _pad:2;
  174. } w2;
  175. union {
  176. /* msg 1 - differential gps corrections */
  177. struct rtcm2_msg1 {
  178. struct gps_correction_t {
  179. struct { /* msg 1 word 3 */
  180. unsigned int parity:6;
  181. int prc1:16;
  182. unsigned int satident1:5; /* satellite ID */
  183. unsigned int udre1:2;
  184. unsigned int scale1:1;
  185. unsigned int _pad:2;
  186. } w3;
  187. struct { /* msg 1 word 4 */
  188. unsigned int parity:6;
  189. unsigned int satident2:5; /* satellite ID */
  190. unsigned int udre2:2;
  191. unsigned int scale2:1;
  192. unsigned int iod1:8;
  193. int rrc1:8;
  194. unsigned int _pad:2;
  195. } w4;
  196. struct { /* msg 1 word 5 */
  197. unsigned int parity:6;
  198. int rrc2:8;
  199. int prc2:16;
  200. unsigned int _pad:2;
  201. } w5;
  202. struct { /* msg 1 word 6 */
  203. unsigned int parity:6;
  204. int prc3_h:8;
  205. unsigned int satident3:5; /* satellite ID */
  206. unsigned int udre3:2;
  207. unsigned int scale3:1;
  208. unsigned int iod2:8;
  209. unsigned int _pad:2;
  210. } w6;
  211. struct { /* msg 1 word 7 */
  212. unsigned int parity:6;
  213. unsigned int iod3:8;
  214. int rrc3:8;
  215. /* NOTE: unsigned int for low byte */
  216. unsigned int prc3_l:8;
  217. unsigned int _pad:2;
  218. } w7;
  219. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  220. } type1;
  221. /* msg 2 - Pseudo-range corrections, referring to previous orbit
  222. * data records (maximum 12 satellites) */
  223. /* msg 3 - reference station parameters */
  224. struct rtcm2_msg3 {
  225. struct {
  226. unsigned int parity:6;
  227. unsigned int x_h:24;
  228. unsigned int _pad:2;
  229. } w3;
  230. struct {
  231. unsigned int parity:6;
  232. unsigned int y_h:16;
  233. unsigned int x_l:8;
  234. unsigned int _pad:2;
  235. } w4;
  236. struct {
  237. unsigned int parity:6;
  238. unsigned int z_h:8;
  239. unsigned int y_l:16;
  240. unsigned int _pad:2;
  241. } w5;
  242. struct {
  243. unsigned int parity:6;
  244. unsigned int z_l:24;
  245. unsigned int _pad:2;
  246. } w6;
  247. } type3;
  248. /* msg 4 - reference station datum */
  249. struct rtcm2_msg4 {
  250. struct {
  251. unsigned int parity:6;
  252. unsigned int datum_alpha_char2:8;
  253. unsigned int datum_alpha_char1:8;
  254. unsigned int spare:4;
  255. unsigned int dat:1;
  256. unsigned int dgnss:3;
  257. unsigned int _pad:2;
  258. } w3;
  259. struct {
  260. unsigned int parity:6;
  261. unsigned int datum_sub_div_char2:8;
  262. unsigned int datum_sub_div_char1:8;
  263. unsigned int datum_sub_div_char3:8;
  264. unsigned int _pad:2;
  265. } w4;
  266. struct {
  267. unsigned int parity:6;
  268. unsigned int dy_h:8;
  269. unsigned int dx:16;
  270. unsigned int _pad:2;
  271. } w5;
  272. struct {
  273. unsigned int parity:6;
  274. unsigned int dz:24;
  275. unsigned int dy_l:8;
  276. unsigned int _pad:2;
  277. } w6;
  278. } type4;
  279. /* msg 5 - constellation health */
  280. struct rtcm2_msg5 {
  281. struct b_health_t {
  282. unsigned int parity:6;
  283. unsigned int unassigned:2;
  284. unsigned int time_unhealthy:4;
  285. unsigned int loss_warn:1;
  286. unsigned int new_nav_data:1;
  287. unsigned int health_enable:1;
  288. unsigned int cn0:5;
  289. unsigned int data_health:3;
  290. unsigned int issue_of_data_link:1;
  291. unsigned int sat_id:5;
  292. unsigned int reserved:1;
  293. unsigned int _pad:2;
  294. } health[MAXHEALTH];
  295. } type5;
  296. /* msg 6 - null message */
  297. /* msg 7 - beacon almanac */
  298. struct rtcm2_msg7 {
  299. struct b_station_t {
  300. struct {
  301. unsigned int parity:6;
  302. int lon_h:8;
  303. int lat:16;
  304. unsigned int _pad:2;
  305. } w3;
  306. struct {
  307. unsigned int parity:6;
  308. unsigned int freq_h:6;
  309. unsigned int range:10;
  310. unsigned int lon_l:8;
  311. unsigned int _pad:2;
  312. } w4;
  313. struct {
  314. unsigned int parity:6;
  315. unsigned int encoding:1;
  316. unsigned int sync_type:1;
  317. unsigned int mod_mode:1;
  318. unsigned int bit_rate:3;
  319. /*
  320. * ITU-R M.823-2 page 9 and RTCM-SC104 v2.1 pages
  321. * 4-21 and 4-22 are in conflict over the next two
  322. * field sizes. ITU says 9+3, RTCM says 10+2.
  323. * The latter correctly decodes the USCG station
  324. * id's so I'll use that one here. -wsr
  325. */
  326. unsigned int station_id:10;
  327. unsigned int health:2;
  328. unsigned int freq_l:6;
  329. unsigned int _pad:2;
  330. } w5;
  331. } almanac[(RTCM2_WORDS_MAX - 2)/3];
  332. } type7;
  333. // msg 8 - Pseudolite almanac
  334. // msg 9 - GPS Partial correction set
  335. // msg 10 - P-code differential corrections
  336. // msg 11 - C/A-code L1, L2 delta corrections
  337. // msg 12 - Pseudolite station parameters.
  338. /* msg 13 - Ground Transmitter Parameters (RTCM2.3 only) */
  339. struct rtcm2_msg13 {
  340. struct {
  341. unsigned int parity:6;
  342. int lat:16;
  343. unsigned int reserved:6;
  344. unsigned int rangeflag:1;
  345. unsigned int status:1;
  346. unsigned int _pad:2;
  347. } w1;
  348. struct {
  349. unsigned int parity:6;
  350. unsigned int range:8;
  351. int lon:16;
  352. unsigned int _pad:2;
  353. } w2;
  354. } type13;
  355. /* msg 14 - GPS Time of Week (RTCM2.3 only) */
  356. struct rtcm2_msg14 {
  357. struct {
  358. unsigned int parity:6;
  359. unsigned int leapsecs:6;
  360. unsigned int hour:8;
  361. unsigned int week:10;
  362. unsigned int _pad:2;
  363. } w1;
  364. } type14;
  365. // msg 15 - Ionospheric delay message
  366. /* msg 16 - text msg */
  367. struct rtcm2_msg16 {
  368. struct {
  369. unsigned int parity:6;
  370. unsigned int byte3:8;
  371. unsigned int byte2:8;
  372. unsigned int byte1:8;
  373. unsigned int _pad:2;
  374. } txt[RTCM2_WORDS_MAX-2];
  375. } type16;
  376. // msg 17 - GPS ephmerides. RTCM 2.1
  377. // msg 18 - RTK uncorrected carrier phases. RTCM 2.1
  378. struct rtcm2_msg18 {
  379. unsigned int parity:6;
  380. unsigned int tom:20;
  381. unsigned int r:2;
  382. unsigned int f:2;
  383. unsigned int _pad:2;
  384. struct {
  385. unsigned int parity:6;
  386. unsigned int cp_h:8;
  387. unsigned int clc:5;
  388. unsigned int dq:3;
  389. unsigned int ident:5;
  390. unsigned int g:1;
  391. unsigned int pc:1;
  392. unsigned int m:1;
  393. unsigned int _pad:2;
  394. unsigned int parity1:6;
  395. unsigned int cp_l:24;
  396. unsigned int _pad1:2;
  397. } sat[15];
  398. } type18;
  399. // msg 19 - RTK uncorrected pseudoranges. RTCM 2.1
  400. struct rtcm2_msg19 {
  401. unsigned int parity:6;
  402. unsigned int tom:20;
  403. unsigned int sm:2;
  404. unsigned int f:2;
  405. unsigned int _pad:2;
  406. struct {
  407. unsigned int parity:6;
  408. unsigned int pr_h:8;
  409. unsigned int me:4;
  410. unsigned int dq:4;
  411. unsigned int ident:5;
  412. unsigned int g:1;
  413. unsigned int pc:1;
  414. unsigned int m:1;
  415. unsigned int _pad:2;
  416. unsigned int parity1:6;
  417. unsigned int pr_l:24;
  418. unsigned int _pad1:2;
  419. } sat[15];
  420. } type19;
  421. // msg 20 - RTK carrier phase corrections. RTCM 2.1
  422. struct rtcm2_msg20 {
  423. unsigned int parity:6;
  424. unsigned int tom:20;
  425. unsigned int r:2;
  426. unsigned int f:2;
  427. unsigned int _pad:2;
  428. struct {
  429. unsigned int parity:6;
  430. unsigned int iod:8;
  431. unsigned int clc:5;
  432. unsigned int dq:3;
  433. unsigned int ident:5;
  434. unsigned int g:1;
  435. unsigned int pc:1;
  436. unsigned int m:1;
  437. unsigned int _pad:2;
  438. unsigned int parity1:6;
  439. unsigned int cpc:24;
  440. unsigned int _pad1:2;
  441. } sat[RTCM2_WORDS_MAX / 2];
  442. } type20;
  443. // msg 21 - RTK/high accuracy pseudorange corrections. RTCM 2.1
  444. struct rtcm2_msg21 {
  445. unsigned int parity:6;
  446. unsigned int tom:20;
  447. unsigned int sm:2;
  448. unsigned int f:2;
  449. unsigned int _pad:2;
  450. } type21;
  451. // msg 22 - Extended reference station parameters
  452. struct rtcm2_msg22 {
  453. unsigned int parity:6;
  454. int ecef_dz:8;
  455. int ecef_dy:8;
  456. int ecef_dx:8;
  457. unsigned int _pad:2;
  458. // word 4
  459. unsigned int parity1:6;
  460. unsigned int ah:18;
  461. unsigned int nh:1;
  462. unsigned int ap:1;
  463. unsigned int at:1;
  464. unsigned int gs:1;
  465. unsigned int res:2;
  466. unsigned int _pad1:2;
  467. // word 5
  468. unsigned int parity2:6;
  469. int ecef_dz2:8;
  470. int ecef_dy2:8;
  471. int ecef_dx2:8;
  472. unsigned int _pad2:2;
  473. } type22;
  474. // msg 23 - Type of Antenna. RTCM 2.3
  475. struct rtcm2_msg23 {
  476. struct {
  477. unsigned int parity:6;
  478. unsigned char byte2:8;
  479. unsigned char byte1:8;
  480. unsigned char byte0:8;
  481. unsigned int _pad:2;
  482. } words[RTCM2_WORDS_MAX - 2];
  483. } type23;
  484. // msg 24 - Reference station ARP.. RTCM 2.3
  485. struct rtcm2_msg24 {
  486. struct {
  487. unsigned int parity:6;
  488. unsigned int x_h:24;
  489. unsigned int _pad:2;
  490. } w3;
  491. struct {
  492. unsigned int parity:6;
  493. unsigned int y_h:8;
  494. unsigned int r:2;
  495. unsigned int x_l:14;
  496. unsigned int _pad:2;
  497. } w4;
  498. struct {
  499. unsigned int parity:6;
  500. unsigned int y_m:24;
  501. unsigned int _pad:2;
  502. } w5;
  503. struct {
  504. unsigned int parity:6;
  505. unsigned int z_h:16;
  506. unsigned int r:2;
  507. unsigned int y_l:6;
  508. unsigned int _pad:2;
  509. } w6;
  510. struct {
  511. unsigned int parity:6;
  512. unsigned int ah:1;
  513. unsigned int gs:1;
  514. unsigned int z_l:22;
  515. unsigned int _pad:2;
  516. } w7;
  517. struct {
  518. unsigned int parity:6;
  519. unsigned int res:6;
  520. unsigned int ah:18;
  521. unsigned int _pad:2;
  522. } w8;
  523. } type24;
  524. // msg 27 - Extended almanac of DGPS.
  525. /* msg 31 - differential GLONASS corrections */
  526. struct rtcm2_msg31 {
  527. struct glonass_correction_t {
  528. struct { /* msg 1 word 3 */
  529. unsigned int parity:6;
  530. int prc1:16;
  531. unsigned int satident1:5; /* satellite ID */
  532. unsigned int udre1:2;
  533. unsigned int scale1:1;
  534. unsigned int _pad:2;
  535. } w3;
  536. struct { /* msg 1 word 4 */
  537. unsigned int parity:6;
  538. unsigned int satident2:5; /* satellite ID */
  539. unsigned int udre2:2;
  540. unsigned int scale2:1;
  541. unsigned int tod1:7;
  542. unsigned int change1:1;
  543. int rrc1:8;
  544. unsigned int _pad:2;
  545. } w4;
  546. struct { /* msg 1 word 5 */
  547. unsigned int parity:6;
  548. int rrc2:8;
  549. int prc2:16;
  550. unsigned int _pad:2;
  551. } w5;
  552. struct { /* msg 1 word 6 */
  553. unsigned int parity:6;
  554. int prc3_h:8;
  555. unsigned int satident3:5; /* satellite ID */
  556. unsigned int udre3:2;
  557. unsigned int scale3:1;
  558. unsigned int tod2:7;
  559. unsigned int change2:1;
  560. unsigned int _pad:2;
  561. } w6;
  562. struct { /* msg 1 word 7 */
  563. unsigned int parity:6;
  564. unsigned int tod3:7;
  565. unsigned int change3:1;
  566. int rrc3:8;
  567. /* NOTE: unsigned int for low byte */
  568. unsigned int prc3_l:8;
  569. unsigned int _pad:2;
  570. } w7;
  571. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  572. } type31;
  573. // msg 32 - Differential GLONASS reference station parameters, RTCM 2.3
  574. // msg 33 - GLONASS constellation health, RTCM 2.3
  575. // msg 34 - DGLONASS corrections, RTCM 2.3
  576. // msg 35 - GLONASS radiobeacon almanac, RTCM 2.3
  577. // msg 36 - GLONASS special message, RTCM 2.3
  578. // msg 37 - GNSS system time offset, RTCM 2.3
  579. /* msg 59 - Proprietary messages, for transmission of any
  580. * required data */
  581. // msg 60-63 - Multipurpose usage
  582. /* unknown message */
  583. isgps30bits_t rtcm2_msgunk[RTCM2_WORDS_MAX - 2];
  584. } msg_type;
  585. } __attribute__((__packed__));
  586. #endif /* LITTLE_ENDIAN */
  587. #ifdef WORDS_BIGENDIAN
  588. struct rtcm2_msg_t {
  589. struct rtcm2_msghw1 { /* header word 1 */
  590. unsigned int _pad:2;
  591. unsigned int preamble:8; /* fixed at 01100110 */
  592. unsigned int msgtype:6; /* RTCM message type */
  593. unsigned int refstaid:10; /* reference station ID */
  594. unsigned int parity:6;
  595. } w1;
  596. struct rtcm2_msghw2 { /* header word 2 */
  597. unsigned int _pad:2;
  598. unsigned int zcnt:13;
  599. unsigned int sqnum:3;
  600. unsigned int frmlen:5;
  601. unsigned int stathlth:3; /* station health */
  602. unsigned int parity:6;
  603. } w2;
  604. union {
  605. /* msg 1 - differential GPS corrections */
  606. struct rtcm2_msg1 {
  607. struct gps_correction_t {
  608. struct { /* msg 1 word 3 */
  609. unsigned int _pad:2;
  610. unsigned int scale1:1;
  611. unsigned int udre1:2;
  612. unsigned int satident1:5; /* satellite ID */
  613. int prc1:16;
  614. unsigned int parity:6;
  615. } w3;
  616. struct { /* msg 1 word 4 */
  617. unsigned int _pad:2;
  618. int rrc1:8;
  619. unsigned int iod1:8;
  620. unsigned int scale2:1;
  621. unsigned int udre2:2;
  622. unsigned int satident2:5; /* satellite ID */
  623. unsigned int parity:6;
  624. } w4;
  625. struct { /* msg 1 word 5 */
  626. unsigned int _pad:2;
  627. int prc2:16;
  628. int rrc2:8;
  629. unsigned int parity:6;
  630. } w5;
  631. struct { /* msg 1 word 6 */
  632. unsigned int _pad:2;
  633. unsigned int iod2:8;
  634. unsigned int scale3:1;
  635. unsigned int udre3:2;
  636. unsigned int satident3:5; /* satellite ID */
  637. int prc3_h:8;
  638. unsigned int parity:6;
  639. } w6;
  640. struct { /* msg 1 word 7 */
  641. unsigned int _pad:2;
  642. unsigned int prc3_l:8; // NOTE: unsigned for low byte
  643. int rrc3:8;
  644. unsigned int iod3:8;
  645. unsigned int parity:6;
  646. } w7;
  647. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  648. } type1;
  649. /* msg 2 - Pseudo-range corrections, referring to previous orbit
  650. * data records (maximum 12 satellites) */
  651. /* msg 3 - reference station parameters */
  652. struct rtcm2_msg3 {
  653. struct {
  654. unsigned int _pad:2;
  655. unsigned int x_h:24;
  656. unsigned int parity:6;
  657. } w3;
  658. struct {
  659. unsigned int _pad:2;
  660. unsigned int x_l:8;
  661. unsigned int y_h:16;
  662. unsigned int parity:6;
  663. } w4;
  664. struct {
  665. unsigned int _pad:2;
  666. unsigned int y_l:16;
  667. unsigned int z_h:8;
  668. unsigned int parity:6;
  669. } w5;
  670. struct {
  671. unsigned int _pad:2;
  672. unsigned int z_l:24;
  673. unsigned int parity:6;
  674. } w6;
  675. } type3;
  676. /* msg 4 - reference station datum */
  677. struct rtcm2_msg4 {
  678. struct {
  679. unsigned int _pad:2;
  680. unsigned int dgnss:3;
  681. unsigned int dat:1;
  682. unsigned int spare:4;
  683. unsigned int datum_alpha_char1:8;
  684. unsigned int datum_alpha_char2:8;
  685. unsigned int parity:6;
  686. } w3;
  687. struct {
  688. unsigned int _pad:2;
  689. unsigned int datum_sub_div_char3:8;
  690. unsigned int datum_sub_div_char1:8;
  691. unsigned int datum_sub_div_char2:8;
  692. unsigned int parity:6;
  693. } w4;
  694. struct {
  695. unsigned int _pad:2;
  696. unsigned int dx:16;
  697. unsigned int dy_h:8;
  698. unsigned int parity:6;
  699. } w5;
  700. struct {
  701. unsigned int _pad:2;
  702. unsigned int dy_l:8;
  703. unsigned int dz:24;
  704. unsigned int parity:6;
  705. } w6;
  706. } type4;
  707. /* msg 5 - constellation health */
  708. struct rtcm2_msg5 {
  709. struct b_health_t {
  710. unsigned int _pad:2;
  711. unsigned int reserved:1;
  712. unsigned int sat_id:5;
  713. unsigned int issue_of_data_link:1;
  714. unsigned int data_health:3;
  715. unsigned int cn0:5;
  716. unsigned int health_enable:1;
  717. unsigned int new_nav_data:1;
  718. unsigned int loss_warn:1;
  719. unsigned int time_unhealthy:4;
  720. unsigned int unassigned:2;
  721. unsigned int parity:6;
  722. } health[MAXHEALTH];
  723. } type5;
  724. // msg - 6 - Null message, used as filler record during time-outs
  725. /* msg 7 - beacon almanac */
  726. struct rtcm2_msg7 {
  727. struct b_station_t {
  728. struct {
  729. unsigned int _pad:2;
  730. int lat:16;
  731. int lon_h:8;
  732. unsigned int parity:6;
  733. } w3;
  734. struct {
  735. unsigned int _pad:2;
  736. unsigned int lon_l:8;
  737. unsigned int range:10;
  738. unsigned int freq_h:6;
  739. unsigned int parity:6;
  740. } w4;
  741. struct {
  742. unsigned int _pad:2;
  743. unsigned int freq_l:6;
  744. unsigned int health:2;
  745. unsigned int station_id:10;
  746. /* see comments in LE struct above. */
  747. unsigned int bit_rate:3;
  748. unsigned int mod_mode:1;
  749. unsigned int sync_type:1;
  750. unsigned int encoding:1;
  751. unsigned int parity:6;
  752. } w5;
  753. } almanac[(RTCM2_WORDS_MAX - 2)/3];
  754. } type7;
  755. // msg 8 - Pseudolite almanac
  756. // msg 9 - GPS Partial correction set
  757. // msg 10 - P-code differential corrections
  758. // msg 11 - C/A-code L1, L2 delta corrections
  759. // msg 12 - Pseudolite station parameters.
  760. /* msg 13 - Ground Transmitter Parameters (RTCM2.3 only) */
  761. struct rtcm2_msg13 {
  762. struct {
  763. unsigned int _pad:2;
  764. unsigned int status:1;
  765. unsigned int rangeflag:1;
  766. unsigned int reserved:6;
  767. int lat:16;
  768. unsigned int parity:6;
  769. } w1;
  770. struct {
  771. unsigned int _pad:2;
  772. int lon:16;
  773. unsigned int range:8;
  774. unsigned int parity:6;
  775. } w2;
  776. } type13;
  777. /* msg 14 - GPS Time of Week (RTCM2.3 only) */
  778. struct rtcm2_msg14 {
  779. struct {
  780. unsigned int _pad:2;
  781. unsigned int week:10;
  782. unsigned int hour:8;
  783. unsigned int leapsecs:6;
  784. unsigned int parity:6;
  785. } w1;
  786. } type14;
  787. // msg 15 - Ionospheric delay message
  788. /* msg 16 - text msg */
  789. struct rtcm2_msg16 {
  790. struct {
  791. unsigned int _pad:2;
  792. unsigned int byte1:8;
  793. unsigned int byte2:8;
  794. unsigned int byte3:8;
  795. unsigned int parity:6;
  796. } txt[RTCM2_WORDS_MAX-2];
  797. } type16;
  798. // msg 17 - GPS ephmerides. RTCM 2.1
  799. // msg 18 - RTK uncorrected carrier phases. RTCM 2.1
  800. struct rtcm2_msg18 {
  801. unsigned int _pad:2;
  802. unsigned int f:2;
  803. unsigned int r:2;
  804. unsigned int tom:20;
  805. unsigned int parity:6;
  806. struct {
  807. unsigned int _pad:2;
  808. unsigned int m:1;
  809. unsigned int pc:1;
  810. unsigned int g:1;
  811. unsigned int ident:5;
  812. unsigned int dq:3;
  813. unsigned int clc:5;
  814. unsigned int cp_h:8;
  815. unsigned int parity:6;
  816. unsigned int _pad1:2;
  817. unsigned int cp_l:24;
  818. unsigned int parity1:6;
  819. } sat[15];
  820. } type18;
  821. // msg 19 - RTK uncorrected pseudoranges. RTCM 2.1
  822. struct rtcm2_msg19 {
  823. unsigned int _pad:2;
  824. unsigned int f:2;
  825. unsigned int sm:2;
  826. unsigned int tom:20;
  827. unsigned int parity:6;
  828. struct {
  829. unsigned int _pad:2;
  830. unsigned int m:1;
  831. unsigned int pc:1;
  832. unsigned int g:1;
  833. unsigned int ident:5;
  834. unsigned int dq:4;
  835. unsigned int me:4;
  836. unsigned int pr_h:8;
  837. unsigned int parity:6;
  838. unsigned int _pad1:2;
  839. unsigned int pr_l:24;
  840. unsigned int parity1:6;
  841. } sat[15];
  842. } type19;
  843. // msg 20 - RTK carrier phase corrections. RTCM 2.1
  844. struct rtcm2_msg20 {
  845. unsigned int _pad:2;
  846. unsigned int f:2;
  847. unsigned int r:2;
  848. unsigned int tom:20;
  849. unsigned int parity:6;
  850. struct {
  851. unsigned int _pad:2;
  852. unsigned int m:1;
  853. unsigned int pc:1;
  854. unsigned int g:1;
  855. unsigned int dq:3;
  856. unsigned int ident:5;
  857. unsigned int clc:5;
  858. unsigned int iod:8;
  859. unsigned int parity:6;
  860. unsigned int _pad1:2;
  861. unsigned int cpc:24;
  862. unsigned int parity1:6;
  863. } sat[RTCM2_WORDS_MAX / 2];
  864. } type20;
  865. // msg 21 - RTK/high accuracy pseudorange corrections. RTCM 2.1
  866. struct rtcm2_msg21 {
  867. unsigned int _pad:2;
  868. unsigned int f:2;
  869. unsigned int sm:2;
  870. unsigned int tom:20;
  871. unsigned int parity:6;
  872. } type21;
  873. // msg 22 - Extended reference station parameters
  874. struct rtcm2_msg22 {
  875. unsigned int _pad:2;
  876. int ecef_dx:8;
  877. int ecef_dy:8;
  878. int ecef_dz:8;
  879. unsigned int parity:6;
  880. // word 4
  881. unsigned int _pad1:2;
  882. unsigned int res:2;
  883. unsigned int gs:1;
  884. unsigned int at:1;
  885. unsigned int ap:1;
  886. unsigned int nh:1;
  887. unsigned int ah:18;
  888. unsigned int parity1:6;
  889. // word 5
  890. unsigned int _pad2:2;
  891. int ecef_dx2:8;
  892. int ecef_dy2:8;
  893. int ecef_dz2:8;
  894. unsigned int parity2:6;
  895. } type22;
  896. // msg 23 - Type of Antenna. RTCM 2.3
  897. struct rtcm2_msg23 {
  898. struct {
  899. unsigned int _pad:2;
  900. unsigned char byte0:8;
  901. unsigned char byte1:8;
  902. unsigned char byte2:8;
  903. unsigned int parity:6;
  904. } words[RTCM2_WORDS_MAX - 2];
  905. } type23;
  906. // msg 24 - Reference station ARP.. RTCM 2.3
  907. struct rtcm2_msg24 {
  908. struct {
  909. unsigned int _pad:2;
  910. unsigned int x_h:24;
  911. unsigned int parity:6;
  912. } w3;
  913. struct {
  914. unsigned int _pad:2;
  915. unsigned int x_l:14;
  916. unsigned int r:2;
  917. unsigned int y_h:8;
  918. unsigned int parity:6;
  919. } w4;
  920. struct {
  921. unsigned int _pad:2;
  922. unsigned int y_m:24;
  923. unsigned int parity:6;
  924. } w5;
  925. struct {
  926. unsigned int _pad:2;
  927. unsigned int y_l:6;
  928. unsigned int r:2;
  929. unsigned int z_h:16;
  930. unsigned int parity:6;
  931. } w6;
  932. struct {
  933. unsigned int _pad:2;
  934. unsigned int z_l:22;
  935. unsigned int gs:1;
  936. unsigned int ah:1;
  937. unsigned int parity:6;
  938. } w7;
  939. struct {
  940. unsigned int _pad:2;
  941. unsigned int ah:18;
  942. unsigned int res:6;
  943. unsigned int parity:6;
  944. } w8;
  945. } type24;
  946. // msg 27 - Extended almanac of DGPS.
  947. /* msg 31 - differential GLONASS corrections */
  948. struct rtcm2_msg31 {
  949. struct glonass_correction_t {
  950. struct { /* msg 1 word 3 */
  951. unsigned int _pad:2;
  952. unsigned int scale1:1;
  953. unsigned int udre1:2;
  954. unsigned int satident1:5; /* satellite ID */
  955. int prc1:16;
  956. unsigned int parity:6;
  957. } w3;
  958. struct { /* msg 1 word 4 */
  959. unsigned int _pad:2;
  960. int rrc1:8;
  961. unsigned int change1:1;
  962. unsigned int tod1:7;
  963. unsigned int scale2:1;
  964. unsigned int udre2:2;
  965. unsigned int satident2:5; /* satellite ID */
  966. unsigned int parity:6;
  967. } w4;
  968. struct { /* msg 1 word 5 */
  969. unsigned int _pad:2;
  970. int prc2:16;
  971. int rrc2:8;
  972. unsigned int parity:6;
  973. } w5;
  974. struct { /* msg 1 word 6 */
  975. unsigned int _pad:2;
  976. unsigned int change2:1;
  977. unsigned int tod2:7;
  978. unsigned int scale3:1;
  979. unsigned int udre3:2;
  980. unsigned int satident3:5; /* satellite ID */
  981. int prc3_h:8;
  982. unsigned int parity:6;
  983. } w6;
  984. struct { /* msg 1 word 7 */
  985. unsigned int _pad:2;
  986. unsigned int prc3_l:8; // NOTE: unsigned for low byte
  987. int rrc3:8;
  988. unsigned int change3:1;
  989. unsigned int tod3:7;
  990. unsigned int parity:6;
  991. } w7;
  992. } corrections[(RTCM2_WORDS_MAX - 2) / 5];
  993. } type31;
  994. // msg 32 - Differential GLONASS reference station parameters, RTCM 2.3
  995. // msg 33 - GLONASS constellation health, RTCM 2.3
  996. // msg 34 - DGLONASS corrections, RTCM 2.3
  997. // msg 35 - GLONASS radiobeacon almanac, RTCM 2.3
  998. // msg 36 - GLONASS special message, RTCM 2.3
  999. // msg 37 - GNSS system time offset, RTCM 2.3
  1000. /* msg 59 - Proprietary messages, for transmission of any
  1001. * required data */
  1002. // msg 60-63 - Multipurpsoe usage
  1003. /* unknown message */
  1004. isgps30bits_t rtcm2_msgunk[RTCM2_WORDS_MAX-2];
  1005. } msg_type;
  1006. } __attribute__((__packed__));
  1007. #endif /* BIG ENDIAN */
  1008. #ifdef RTCM104V2_ENABLE
  1009. #define PREAMBLE_PATTERN 0x66
  1010. static unsigned int tx_speed[] = { 25, 50, 100, 110, 150, 200, 250, 300 };
  1011. #define DIMENSION(a) (unsigned)(sizeof(a)/sizeof(a[0]))
  1012. /* break out the raw bits into the content fields */
  1013. void rtcm2_unpack(struct gps_device_t *session, struct rtcm2_t *tp, char *buf)
  1014. {
  1015. int len;
  1016. unsigned int n, w;
  1017. struct rtcm2_msg_t *msg = (struct rtcm2_msg_t *)buf;
  1018. bool unknown = true; // we don't know how to decode
  1019. const char *msg_name = NULL; // no decode, but maybe we know the name
  1020. tp->type = msg->w1.msgtype; // 1 to 64. Zero means 64
  1021. tp->refstaid = msg->w1.refstaid; // 0 to 1023
  1022. tp->length = msg->w2.frmlen; // 0 to 31
  1023. tp->zcount = msg->w2.zcnt * ZCOUNT_SCALE; // 0 to 3599.4 sec
  1024. tp->seqnum = msg->w2.sqnum; // 0 to 7
  1025. tp->stathlth = msg->w2.stathlth;
  1026. // clear rtk struct
  1027. memset(&tp->rtk, 0, sizeof(tp->rtk));
  1028. // clear ref_sta struct
  1029. memset(&tp->ref_sta, 0, sizeof(tp->ref_sta));
  1030. tp->ref_sta.x = NAN;
  1031. tp->ref_sta.y = NAN;
  1032. tp->ref_sta.z = NAN;
  1033. tp->ref_sta.dx = NAN;
  1034. tp->ref_sta.dy = NAN;
  1035. tp->ref_sta.dz = NAN;
  1036. tp->ref_sta.ah = NAN;
  1037. tp->ref_sta.dx2 = NAN;
  1038. tp->ref_sta.dy2 = NAN;
  1039. tp->ref_sta.dz2 = NAN;
  1040. /* FIXME: test tp->length + 2, against session->lexer.isgps.buflen, */
  1041. // len does not include 2 byte header
  1042. len = (int)tp->length;
  1043. n = 0;
  1044. switch (tp->type) {
  1045. case 1:
  1046. FALLTHROUGH
  1047. case 9:
  1048. {
  1049. struct gps_correction_t *m = &msg->msg_type.type1.corrections[0];
  1050. msg_name = "Differential GPS Corrections";
  1051. unknown = false;
  1052. while (len >= 0) {
  1053. if (len >= 2) {
  1054. tp->gps_ranges.sat[n].ident = m->w3.satident1;
  1055. tp->gps_ranges.sat[n].udre = m->w3.udre1;
  1056. tp->gps_ranges.sat[n].iod = m->w4.iod1;
  1057. tp->gps_ranges.sat[n].prc = m->w3.prc1 *
  1058. (m->w3.scale1 ? PRCLARGE : PRCSMALL);
  1059. tp->gps_ranges.sat[n].rrc = m->w4.rrc1 *
  1060. (m->w3.scale1 ? RRLARGE : RRSMALL);
  1061. n++;
  1062. }
  1063. if (len >= 4) {
  1064. tp->gps_ranges.sat[n].ident = m->w4.satident2;
  1065. tp->gps_ranges.sat[n].udre = m->w4.udre2;
  1066. tp->gps_ranges.sat[n].iod = m->w6.iod2;
  1067. tp->gps_ranges.sat[n].prc = m->w5.prc2 *
  1068. (m->w4.scale2 ? PRCLARGE : PRCSMALL);
  1069. tp->gps_ranges.sat[n].rrc = m->w5.rrc2 *
  1070. (m->w4.scale2 ? RRLARGE : RRSMALL);
  1071. n++;
  1072. }
  1073. if (len >= 5) {
  1074. tp->gps_ranges.sat[n].ident = m->w6.satident3;
  1075. tp->gps_ranges.sat[n].udre = m->w6.udre3;
  1076. tp->gps_ranges.sat[n].iod = m->w7.iod3;
  1077. tp->gps_ranges.sat[n].prc =
  1078. ((m->w6.prc3_h << 8) | (m->w7.prc3_l)) *
  1079. (m->w6.scale3 ? PRCLARGE : PRCSMALL);
  1080. tp->gps_ranges.sat[n].rrc =
  1081. m->w7.rrc3 * (m->w6.scale3 ? RRLARGE : RRSMALL);
  1082. n++;
  1083. }
  1084. len -= 5;
  1085. m++;
  1086. }
  1087. tp->gps_ranges.nentries = n;
  1088. }
  1089. break;
  1090. case 2:
  1091. msg_name = "Delta Differential GPS Corrections";
  1092. break;
  1093. case 3:
  1094. {
  1095. struct rtcm2_msg3 *m = &msg->msg_type.type3;
  1096. msg_name = "Reference Station Parameters (GPS)";
  1097. unknown = false;
  1098. if ((tp->ref_sta.valid = len >= 4)) {
  1099. tp->ref_sta.x = ((m->w3.x_h << 8) | (m->w4.x_l)) * XYZ_SCALE;
  1100. tp->ref_sta.y = ((m->w4.y_h << 16) | (m->w5.y_l)) * XYZ_SCALE;
  1101. tp->ref_sta.z = ((m->w5.z_h << 24) | (m->w6.z_l)) * XYZ_SCALE;
  1102. }
  1103. }
  1104. break;
  1105. case 4:
  1106. msg_name = "Reference Station Datum";
  1107. unknown = false;
  1108. if ((tp->reference.valid = len >= 2)) {
  1109. struct rtcm2_msg4 *m = &msg->msg_type.type4;
  1110. tp->reference.system =
  1111. (m->w3.dgnss == 0) ? NAVSYSTEM_GPS :
  1112. ((m->w3.dgnss == 1) ? NAVSYSTEM_GLONASS : NAVSYSTEM_UNKNOWN);
  1113. tp->reference.sense =
  1114. (m->w3.dat != 0) ? SENSE_GLOBAL : SENSE_LOCAL;
  1115. if (m->w3.datum_alpha_char1) {
  1116. tp->reference.datum[n++] = (char)(m->w3.datum_alpha_char1);
  1117. }
  1118. if (m->w3.datum_alpha_char2) {
  1119. tp->reference.datum[n++] = (char)(m->w3.datum_alpha_char2);
  1120. }
  1121. if (m->w4.datum_sub_div_char1) {
  1122. tp->reference.datum[n++] = (char)(m->w4.datum_sub_div_char1);
  1123. }
  1124. if (m->w4.datum_sub_div_char2) {
  1125. tp->reference.datum[n++] = (char)(m->w4.datum_sub_div_char2);
  1126. }
  1127. if (m->w4.datum_sub_div_char3) {
  1128. tp->reference.datum[n++] = (char)(m->w4.datum_sub_div_char3);
  1129. }
  1130. /* we used to say n++ here, but scan-build complains */
  1131. tp->reference.datum[n] = '\0';
  1132. if (len >= 4) {
  1133. tp->reference.dx = m->w5.dx * DXYZ_SCALE;
  1134. tp->reference.dy =
  1135. ((m->w5.dy_h << 8) | m->w6.dy_l) * DXYZ_SCALE;
  1136. tp->reference.dz = m->w6.dz * DXYZ_SCALE;
  1137. } else
  1138. tp->reference.sense = SENSE_INVALID;
  1139. }
  1140. break;
  1141. case 5:
  1142. msg_name = "Constellation health (GPS)";
  1143. unknown = false;
  1144. for (n = 0; n < (unsigned)len; n++) {
  1145. struct consat_t *csp = &tp->conhealth.sat[n];
  1146. struct b_health_t *m = &msg->msg_type.type5.health[n];
  1147. csp->ident = m->sat_id;
  1148. csp->iodl = m->issue_of_data_link != 0;
  1149. csp->health = m->data_health;
  1150. csp->snr = (int)(m->cn0 ? (m->cn0 + CNR_OFFSET) : SNR_BAD);
  1151. csp->health_en = m->health_enable != 0;
  1152. csp->new_data = m->new_nav_data != 0;
  1153. csp->los_warning = m->loss_warn != 0;
  1154. csp->tou = m->time_unhealthy * TU_SCALE;
  1155. }
  1156. tp->conhealth.nentries = n;
  1157. break;
  1158. case 6:
  1159. msg_name = "Null frame (GPS)";
  1160. unknown = false;
  1161. break;
  1162. case 7:
  1163. msg_name = "Beacon Almanac (GPS)";
  1164. unknown = false;
  1165. for (w = 0; w < (unsigned)len; w++) {
  1166. struct station_t *np = &tp->almanac.station[n];
  1167. struct b_station_t *mp = &msg->msg_type.type7.almanac[w];
  1168. np->latitude = mp->w3.lat * LA_SCALE;
  1169. np->longitude = ((mp->w3.lon_h << 8) | mp->w4.lon_l) * LO_SCALE;
  1170. np->range = mp->w4.range;
  1171. np->frequency =
  1172. (((mp->w4.freq_h << 6) | mp->w5.freq_l) * FREQ_SCALE) +
  1173. FREQ_OFFSET;
  1174. np->health = mp->w5.health;
  1175. np->station_id = mp->w5.station_id,
  1176. np->bitrate = tx_speed[mp->w5.bit_rate];
  1177. n++;
  1178. }
  1179. tp->almanac.nentries = (unsigned)(len / 3);
  1180. break;
  1181. case 8:
  1182. msg_name = "Pseudolite Almanac";
  1183. break;
  1184. // case 9 is handled above with case 1
  1185. case 10:
  1186. msg_name = "P-Code Differential Corrections";
  1187. break;
  1188. case 11:
  1189. msg_name = "C/A Code L2 Corrections";
  1190. break;
  1191. case 12:
  1192. msg_name = "Pseudolite Station Parameters";
  1193. break;
  1194. case 13:
  1195. msg_name = "Ground Transmitter Parameters";
  1196. unknown = false;
  1197. tp->xmitter.status = (bool)msg->msg_type.type13.w1.status;
  1198. tp->xmitter.rangeflag = (bool)msg->msg_type.type13.w1.rangeflag;
  1199. tp->xmitter.lat = msg->msg_type.type13.w1.lat * LATLON_SCALE;
  1200. tp->xmitter.lon = msg->msg_type.type13.w2.lon * LATLON_SCALE;
  1201. tp->xmitter.range = msg->msg_type.type13.w2.range * RANGE_SCALE;
  1202. if (tp->xmitter.range == 0)
  1203. tp->xmitter.range = 1024;
  1204. break;
  1205. case 14:
  1206. msg_name = "GPS Time Of Week";
  1207. unknown = false;
  1208. tp->gpstime.week = msg->msg_type.type14.w1.week;
  1209. tp->gpstime.hour = msg->msg_type.type14.w1.hour;
  1210. tp->gpstime.leapsecs = msg->msg_type.type14.w1.leapsecs;
  1211. break;
  1212. case 15:
  1213. msg_name = "Ionospheric Delay Message";
  1214. break;
  1215. case 16:
  1216. msg_name = "Special Message (GPS)";
  1217. unknown = false;
  1218. for (w = 0; w < (unsigned)len; w++) {
  1219. if (!msg->msg_type.type16.txt[w].byte1) {
  1220. break;
  1221. }
  1222. tp->message[n++] = (char)(msg->msg_type.type16.txt[w].byte1);
  1223. if (!msg->msg_type.type16.txt[w].byte2) {
  1224. break;
  1225. }
  1226. tp->message[n++] = (char)(msg->msg_type.type16.txt[w].byte2);
  1227. if (!msg->msg_type.type16.txt[w].byte3) {
  1228. break;
  1229. }
  1230. tp->message[n++] = (char)(msg->msg_type.type16.txt[w].byte3);
  1231. }
  1232. tp->message[n] = '\0';
  1233. break;
  1234. case 17:
  1235. msg_name = "GPS Ephemeris";
  1236. break;
  1237. case 18:
  1238. msg_name = "RTK Uncorrected Carrier-phase";
  1239. unknown = false;
  1240. // WIP: partial decode
  1241. if (1 > len) {
  1242. // too short
  1243. break;
  1244. }
  1245. {
  1246. struct rtcm2_msg18 *m = &msg->msg_type.type18;
  1247. tp->rtk.nentries = (len - 1) / 2;
  1248. unsigned i;
  1249. tp->rtk.tom = m->tom;
  1250. tp->rtk.f = m->f;
  1251. for (i = 0; i < tp->rtk.nentries; i++) {
  1252. tp->rtk.sat[i].m = m->sat[i].m;
  1253. tp->rtk.sat[i].pc = m->sat[i].pc;
  1254. tp->rtk.sat[i].g = m->sat[i].g;
  1255. tp->rtk.sat[i].ident = m->sat[i].ident;
  1256. tp->rtk.sat[i].dq = m->sat[i].dq;
  1257. tp->rtk.sat[i].carrier_phase = m->sat[i].cp_l;
  1258. tp->rtk.sat[i].carrier_phase |= m->sat[i].cp_h << 24;
  1259. tp->rtk.sat[i].clc = m->sat[i].clc;
  1260. }
  1261. }
  1262. break;
  1263. case 19:
  1264. msg_name = "RTK Corrected Pseudorange";
  1265. // WIP: partial decode
  1266. if (1 > len) {
  1267. // too short
  1268. break;
  1269. }
  1270. {
  1271. struct rtcm2_msg19 *m = &msg->msg_type.type19;
  1272. unsigned i;
  1273. tp->rtk.tom = m->tom;
  1274. tp->rtk.f = m->f;
  1275. tp->rtk.sm = m->sm;
  1276. tp->rtk.nentries = (len - 1) / 2;
  1277. for (i = 0; i < tp->rtk.nentries; i++) {
  1278. tp->rtk.sat[i].m = m->sat[i].m;
  1279. tp->rtk.sat[i].pc = m->sat[i].pc;
  1280. tp->rtk.sat[i].g = m->sat[i].g;
  1281. tp->rtk.sat[i].ident = m->sat[i].ident;
  1282. tp->rtk.sat[i].dq = m->sat[i].dq;
  1283. tp->rtk.sat[i].pseudorange = m->sat[i].pr_l;
  1284. tp->rtk.sat[i].pseudorange |= m->sat[i].pr_h << 24;
  1285. }
  1286. }
  1287. break;
  1288. case 20:
  1289. msg_name = "RTK Carrier Phase Corrections";
  1290. // WIP: partial decode
  1291. if (3 > len) {
  1292. // too short
  1293. break;
  1294. }
  1295. {
  1296. struct rtcm2_msg20 *m = &msg->msg_type.type20;
  1297. tp->rtk.tom = m->tom;
  1298. tp->rtk.f = m->f;
  1299. }
  1300. break;
  1301. case 21:
  1302. msg_name = "High-Accuracy Pseudorange Corrections";
  1303. // WIP: partial decode
  1304. if (3 > len) {
  1305. // too short
  1306. break;
  1307. }
  1308. {
  1309. struct rtcm2_msg21 *m = &msg->msg_type.type21;
  1310. tp->rtk.tom = m->tom;
  1311. tp->rtk.f = m->f;
  1312. tp->rtk.sm = m->sm;
  1313. }
  1314. break;
  1315. case 22:
  1316. // Extends types 3 and 34
  1317. msg_name = "Extended Reference Station Parameters";
  1318. unknown = false;
  1319. // WIP: partial decode
  1320. if (3 > len) {
  1321. // too short
  1322. break;
  1323. }
  1324. // May be length 3, 4 or 5!
  1325. {
  1326. struct rtcm2_msg22 *m = &msg->msg_type.type22;
  1327. tp->ref_sta.dx = m->ecef_dx * EDXYZ_SCALE;
  1328. tp->ref_sta.dy = m->ecef_dy * EDXYZ_SCALE;
  1329. tp->ref_sta.dz = m->ecef_dz * EDXYZ_SCALE;
  1330. if (3 < len) {
  1331. // word 4 present
  1332. tp->ref_sta.gs = m->gs;
  1333. if (0 == m->nh) {
  1334. tp->ref_sta.ah = m->ah * EDXYZ_SCALE;
  1335. }
  1336. }
  1337. if (4 < len) {
  1338. // word 5 present
  1339. tp->ref_sta.dx2 = m->ecef_dx2 * EDXYZ2_SCALE;
  1340. tp->ref_sta.dy2 = m->ecef_dy2 * EDXYZ2_SCALE;
  1341. tp->ref_sta.dz2 = m->ecef_dz2 * EDXYZ2_SCALE;
  1342. }
  1343. }
  1344. break;
  1345. case 23:
  1346. msg_name = "Antenna Type Definition";
  1347. // WIP
  1348. unknown = false;
  1349. if (1 > len) {
  1350. // too short
  1351. break;
  1352. }
  1353. {
  1354. unsigned sf;
  1355. unsigned nad = 0, nas = 0;
  1356. unsigned char tbuf[RTCM2_WORDS_MAX * 3];
  1357. struct rtcm2_msg23 *m = &msg->msg_type.type23;
  1358. int i = 0, j = 0;
  1359. tp->ref_sta.ar = (m->words[0].byte0 >> 6) & 1;
  1360. sf = (m->words[0].byte0 >> 5) & 1;
  1361. // nad is 0 to 31
  1362. nad = m->words[0].byte0 & 0x1f;
  1363. // crazy packing... move to simple array first
  1364. for (i = 0; i < len; i++) {
  1365. tbuf[j++] = m->words[i].byte0;
  1366. tbuf[j++] = m->words[i].byte1;
  1367. tbuf[j++] = m->words[i].byte2;
  1368. }
  1369. #if __UNUSED__
  1370. // debug code
  1371. {
  1372. char tmpbuf[100];
  1373. GPSD_LOG(LOG_SHOUT, &session->context->errout,
  1374. "RTCM2: len %d nad %d tbuf %s\n",
  1375. len, nad,
  1376. gpsd_hexdump(tmpbuf, sizeof(tmpbuf),
  1377. (char *)tbuf, len * 3));
  1378. }
  1379. #endif // __UNUSED__
  1380. // skip first byte (AR, SF, NAD)
  1381. memcpy(tp->ref_sta.ant_desc, &tbuf[1], nad);
  1382. tp->ref_sta.ant_desc[nad] = '\0';
  1383. tp->ref_sta.setup_id = tbuf[nad + 1];
  1384. // get serial number
  1385. if (1 == sf) {
  1386. // nas is 0 to 31. just after last ant desc byte
  1387. nas = tbuf[nad + 2] & 0x1f;
  1388. memcpy(tp->ref_sta.ant_serial, &tbuf[nad + 3], nas);
  1389. }
  1390. tp->ref_sta.ant_serial[nas] = '\0';
  1391. }
  1392. break;
  1393. case 24:
  1394. msg_name = "Antenna Reference Point (arp)";
  1395. // WIP
  1396. unknown = false;
  1397. {
  1398. struct rtcm2_msg24 *m = &msg->msg_type.type24;
  1399. // try to prevent int overflow
  1400. long long temp;
  1401. temp = ((long long)m->w3.x_h << 8) | (m->w4.x_l);
  1402. tp->ref_sta.x = temp * EXYZ_SCALE;
  1403. temp = ((long long)m->w4.y_h << 32) |
  1404. (m->w5.y_m << 6) |
  1405. (m->w6.y_l);
  1406. tp->ref_sta.y = temp * EXYZ_SCALE;
  1407. temp = ((long long)m->w6.z_h << 22) | (m->w7.z_l);
  1408. tp->ref_sta.z = temp * EXYZ_SCALE;
  1409. if (0 == m->w7.ah) {
  1410. tp->ref_sta.ah = m->w8.ah * EDXYZ_SCALE;
  1411. }
  1412. }
  1413. break;
  1414. case 25:
  1415. FALLTHROUGH
  1416. case 26:
  1417. msg_name = "Undefined";
  1418. break;
  1419. case 27:
  1420. msg_name = "Radio Beacon Almanac";
  1421. break;
  1422. case 28:
  1423. FALLTHROUGH
  1424. case 29:
  1425. FALLTHROUGH
  1426. case 30:
  1427. msg_name = "Undefined";
  1428. break;
  1429. case 31:
  1430. FALLTHROUGH
  1431. case 34:
  1432. {
  1433. // 32 and 34 are the same, except 31 is all sats, 34 is some sats
  1434. struct glonass_correction_t *m = &msg->msg_type.type31.corrections[0];
  1435. msg_name = "Differential GLONASS corrections";
  1436. unknown = false;
  1437. while (len >= 0) {
  1438. if (len >= 2) {
  1439. tp->glonass_ranges.sat[n].ident = m->w3.satident1;
  1440. tp->glonass_ranges.sat[n].udre = m->w3.udre1;
  1441. tp->glonass_ranges.sat[n].change = (bool)m->w4.change1;
  1442. tp->glonass_ranges.sat[n].tod = m->w4.tod1;
  1443. tp->glonass_ranges.sat[n].prc = m->w3.prc1 *
  1444. (m->w3.scale1 ? PRCLARGE : PRCSMALL);
  1445. tp->glonass_ranges.sat[n].rrc = m->w4.rrc1 *
  1446. (m->w3.scale1 ? RRLARGE : RRSMALL);
  1447. n++;
  1448. }
  1449. if (len >= 4) {
  1450. tp->glonass_ranges.sat[n].ident = m->w4.satident2;
  1451. tp->glonass_ranges.sat[n].udre = m->w4.udre2;
  1452. tp->glonass_ranges.sat[n].change = (bool)m->w6.change2;
  1453. tp->glonass_ranges.sat[n].tod = m->w6.tod2;
  1454. tp->glonass_ranges.sat[n].prc = m->w5.prc2 *
  1455. (m->w4.scale2 ? PRCLARGE : PRCSMALL);
  1456. tp->glonass_ranges.sat[n].rrc = m->w5.rrc2 *
  1457. (m->w4.scale2 ? RRLARGE : RRSMALL);
  1458. n++;
  1459. }
  1460. if (len >= 5) {
  1461. tp->glonass_ranges.sat[n].ident = m->w6.satident3;
  1462. tp->glonass_ranges.sat[n].udre = m->w6.udre3;
  1463. tp->glonass_ranges.sat[n].change = (bool)m->w7.change3;
  1464. tp->glonass_ranges.sat[n].tod = m->w7.tod3;
  1465. tp->glonass_ranges.sat[n].prc =
  1466. ((m->w6.prc3_h << 8) | (m->w7.prc3_l)) *
  1467. (m->w6.scale3 ? PRCLARGE : PRCSMALL);
  1468. tp->glonass_ranges.sat[n].rrc =
  1469. m->w7.rrc3 * (m->w6.scale3 ? RRLARGE : RRSMALL);
  1470. n++;
  1471. }
  1472. len -= 5;
  1473. m++;
  1474. }
  1475. tp->glonass_ranges.nentries = n;
  1476. }
  1477. break;
  1478. case 32:
  1479. msg_name = "Reference Station Parameters (GLONASS)";
  1480. break;
  1481. case 33:
  1482. msg_name = "Constellation health (GLONASS)";
  1483. break;
  1484. // case 34 is above with 31
  1485. case 35:
  1486. msg_name = "Beacon Almanac (GLONASS)";
  1487. break;
  1488. case 36:
  1489. msg_name = "Special Message (GLONASS)";
  1490. break;
  1491. case 37:
  1492. msg_name = "GNSS System Time Offset";
  1493. break;
  1494. case 59:
  1495. msg_name = "Proprietary Message";
  1496. break;
  1497. default:
  1498. msg_name = "Huh?";
  1499. break;
  1500. }
  1501. if (unknown) {
  1502. memcpy(tp->words, msg->msg_type.rtcm2_msgunk,
  1503. tp->length * sizeof(isgps30bits_t));
  1504. if (NULL == msg_name) {
  1505. GPSD_LOG(LOG_PROG, &session->context->errout,
  1506. "RTCM2: type %d (unknown), length %d\n",
  1507. tp->type, tp->length + 2);
  1508. } else {
  1509. GPSD_LOG(LOG_PROG, &session->context->errout,
  1510. "RTCM2: type %d (%s), length %d\n",
  1511. tp->type, msg_name,tp->length + 2);
  1512. }
  1513. } else {
  1514. GPSD_LOG(LOG_PROG, &session->context->errout,
  1515. "RTCM2: type %d (%s), length %d\n",
  1516. tp->type, msg_name, tp->length + 2);
  1517. }
  1518. GPSD_LOG(LOG_RAW, &session->context->errout,
  1519. "RTCM 2: type %d (%s) length %d words "
  1520. "from %zd bytes: %s\n",
  1521. tp->type,
  1522. msg_name,
  1523. tp->length + 2,
  1524. session->lexer.isgps.buflen,
  1525. gpsd_hexdump(session->msgbuf, sizeof(session->msgbuf),
  1526. (char *)session->lexer.isgps.buf,
  1527. (tp->length + 2) * sizeof(isgps30bits_t)));
  1528. }
  1529. static bool preamble_match(isgps30bits_t * w)
  1530. {
  1531. return (((struct rtcm2_msghw1 *)w)->preamble == PREAMBLE_PATTERN);
  1532. }
  1533. static bool length_check(struct gps_lexer_t *lexer)
  1534. {
  1535. return lexer->isgps.bufindex >= 2
  1536. && lexer->isgps.bufindex >=
  1537. ((struct rtcm2_msg_t *)lexer->isgps.buf)->w2.frmlen + 2u;
  1538. }
  1539. enum isgpsstat_t rtcm2_decode(struct gps_lexer_t *lexer, unsigned int c)
  1540. {
  1541. return isgps_decode(lexer,
  1542. preamble_match, length_check, RTCM2_WORDS_MAX, c);
  1543. }
  1544. #endif /* RTCM104V2_ENABLE */
  1545. // vim: set expandtab shiftwidth=4