source: ntrip/trunk/rtcm3torinex/lib/rtcm3torinex.c@ 5359

Last change on this file since 5359 was 5359, checked in by stoecker, 8 years ago

unify code

  • Property svn:keywords set to Id Revision Date
File size: 132.5 KB
Line 
1/*
2 Converter for RTCM3 data to RINEX.
3 $Id: rtcm3torinex.c 5359 2013-08-26 08:58:35Z stoecker $
4 Copyright (C) 2005-2012 by Dirk Stöcker <stoecker@alberding.eu>
5
6 This software is a complete NTRIP-RTCM3 to RINEX converter as well as
7 a module of the BNC tool for multiformat conversion. Contact Dirk
8 Stöcker for suggestions and bug reports related to the RTCM3 to RINEX
9 conversion problems and the author of BNC for all the other problems.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 or read http://www.gnu.org/licenses/gpl.txt
25*/
26
27#include <ctype.h>
28#include <errno.h>
29#include <math.h>
30#include <signal.h>
31#include <stdarg.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/types.h>
36#include <time.h>
37#include <unistd.h>
38
39#ifndef NO_RTCM3_MAIN
40#include <getopt.h>
41#include <netdb.h>
42#include <netinet/in.h>
43#include <sys/socket.h>
44#endif
45
46#ifndef sparc
47#include <stdint.h>
48#endif
49
50#ifndef isinf
51#define isinf(x) 0
52#endif
53
54#include "rtcm3torinex.h"
55
56/* CVS revision and version */
57static char revisionstr[] = "$Revision: 5359 $";
58
59#ifndef COMPILEDATE
60#define COMPILEDATE " built " __DATE__
61#endif
62
63static uint32_t CRC24(long size, const unsigned char *buf)
64{
65 uint32_t crc = 0;
66 int i;
67
68 while(size--)
69 {
70 crc ^= (*buf++) << (16);
71 for(i = 0; i < 8; i++)
72 {
73 crc <<= 1;
74 if(crc & 0x1000000)
75 crc ^= 0x01864cfb;
76 }
77 }
78 return crc;
79}
80
81static int GetMessage(struct RTCM3ParserData *handle)
82{
83 unsigned char *m, *e;
84 int i;
85
86 m = handle->Message+handle->SkipBytes;
87 e = handle->Message+handle->MessageSize;
88 handle->NeedBytes = handle->SkipBytes = 0;
89 while(e-m >= 3)
90 {
91 if(m[0] == 0xD3)
92 {
93 handle->size = ((m[1]&3)<<8)|m[2];
94 if(e-m >= handle->size+6)
95 {
96 if((uint32_t)((m[3+handle->size]<<16)|(m[3+handle->size+1]<<8)
97 |(m[3+handle->size+2])) == CRC24(handle->size+3, m))
98 {
99 handle->SkipBytes = handle->size;
100 break;
101 }
102 else
103 ++m;
104 }
105 else
106 {
107 handle->NeedBytes = handle->size+6;
108 break;
109 }
110 }
111 else
112 ++m;
113 }
114 if(e-m < 3)
115 handle->NeedBytes = 3;
116
117 /* copy buffer to front */
118 i = m - handle->Message;
119 if(i && m < e)
120 memmove(handle->Message, m, (size_t)(handle->MessageSize-i));
121 handle->MessageSize -= i;
122
123 return !handle->NeedBytes;
124}
125
126#define LOADBITS(a) \
127{ \
128 while((a) > numbits) \
129 { \
130 if(!size--) break; \
131 bitfield = (bitfield<<8)|*(data++); \
132 numbits += 8; \
133 } \
134}
135
136/* extract bits from data stream
137 b = variable to store result, a = number of bits */
138#define GETBITS64(b, a) \
139{ \
140 if(((a) > 56) && ((a)-56) > numbits) \
141 { \
142 uint64_t x; \
143 GETBITS(x, 56) \
144 LOADBITS((a)-56) \
145 b = ((x<<((a)-56)) | (bitfield<<(sizeof(bitfield)*8-numbits)) \
146 >>(sizeof(bitfield)*8-((a)-56))); \
147 numbits -= ((a)-56); \
148 } \
149 else \
150 { \
151 GETBITS(b, a) \
152 } \
153}
154
155/* extract bits from data stream
156 b = variable to store result, a = number of bits */
157#define GETBITS(b, a) \
158{ \
159 LOADBITS(a) \
160 b = (bitfield<<(64-numbits))>>(64-(a)); \
161 numbits -= (a); \
162}
163
164/* extract bits from data stream
165 b = variable to store result, a = number of bits */
166#define GETBITSFACTOR(b, a, c) \
167{ \
168 LOADBITS(a) \
169 b = ((bitfield<<(sizeof(bitfield)*8-numbits))>>(sizeof(bitfield)*8-(a)))*(c); \
170 numbits -= (a); \
171}
172
173/* extract floating value from data stream
174 b = variable to store result, a = number of bits */
175#define GETFLOAT(b, a, c) \
176{ \
177 LOADBITS(a) \
178 b = ((double)((bitfield<<(64-numbits))>>(64-(a))))*(c); \
179 numbits -= (a); \
180}
181
182/* extract signed floating value from data stream
183 b = variable to store result, a = number of bits */
184#define GETFLOATSIGN(b, a, c) \
185{ \
186 LOADBITS(a) \
187 b = ((double)(((int64_t)(bitfield<<(64-numbits)))>>(64-(a))))*(c); \
188 numbits -= (a); \
189}
190
191/* extract bits from data stream
192 b = variable to store result, a = number of bits */
193#define GETBITSSIGN(b, a) \
194{ \
195 LOADBITS(a) \
196 b = ((int64_t)(bitfield<<(64-numbits)))>>(64-(a)); \
197 numbits -= (a); \
198}
199
200#define GETFLOATSIGNM(b, a, c) \
201{ int l; \
202 LOADBITS(a) \
203 l = (bitfield<<(64-numbits))>>(64-1); \
204 b = ((double)(((bitfield<<(64-(numbits-1))))>>(64-(a-1))))*(c); \
205 numbits -= (a); \
206 if(l) b *= -1.0; \
207}
208
209#define SKIPBITS(b) { LOADBITS(b) numbits -= (b); }
210
211/* extract byte-aligned byte from data stream,
212 b = variable to store size, s = variable to store string pointer */
213#define GETSTRING(b, s) \
214{ \
215 b = *(data++); \
216 s = (char *) data; \
217 data += b; \
218 size -= b+1; \
219}
220
221struct leapseconds { /* specify the day of leap second */
222 int day; /* this is the day, where 23:59:59 exists 2 times */
223 int month; /* not the next day! */
224 int year;
225 int taicount;
226};
227static const int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
228static const struct leapseconds leap[] = {
229/*{31, 12, 1971, 10},*/
230/*{30, 06, 1972, 11},*/
231/*{31, 12, 1972, 12},*/
232/*{31, 12, 1973, 13},*/
233/*{31, 12, 1974, 14},*/
234/*{31, 12, 1975, 15},*/
235/*{31, 12, 1976, 16},*/
236/*{31, 12, 1977, 17},*/
237/*{31, 12, 1978, 18},*/
238/*{31, 12, 1979, 19},*/
239{30, 06, 1981,20},
240{30, 06, 1982,21},
241{30, 06, 1983,22},
242{30, 06, 1985,23},
243{31, 12, 1987,24},
244{31, 12, 1989,25},
245{31, 12, 1990,26},
246{30, 06, 1992,27},
247{30, 06, 1993,28},
248{30, 06, 1994,29},
249{31, 12, 1995,30},
250{30, 06, 1997,31},
251{31, 12, 1998,32},
252{31, 12, 2005,33},
253{31, 12, 2008,34},
254{30, 06, 2012,35},
255{0,0,0,0} /* end marker */
256};
257#define LEAPSECONDS 16 /* only needed for approx. time */
258#define GPSLEAPSTART 19 /* 19 leap seconds existed at 6.1.1980 */
259
260static int longyear(int year, int month)
261{
262 if(!(year % 4) && (!(year % 400) || (year % 100)))
263 {
264 if(!month || month == 2)
265 return 1;
266 }
267 return 0;
268}
269
270int gnumleap(int year, int month, int day)
271{
272 int ls = 0;
273 const struct leapseconds *l;
274
275 for(l = leap; l->taicount && year >= l->year; ++l)
276 {
277 if(year > l->year || month > l->month || (month == l->month && day > l->day))
278 ls = l->taicount - GPSLEAPSTART;
279 }
280 return ls;
281}
282
283/* Convert Moscow time into UTC (fixnumleap == 1) or GPS (fixnumleap == 0) */
284void updatetime(int *week, int *secOfWeek, int mSecOfWeek, int fixnumleap)
285{
286 int y,m,d,k,l, nul;
287 unsigned int j = *week*(7*24*60*60) + *secOfWeek + 5*24*60*60+3*60*60;
288 int glo_daynumber = 0, glo_timeofday;
289 for(y = 1980; j >= (unsigned int)(k = (l = (365+longyear(y,0)))*24*60*60)
290 + gnumleap(y+1,1,1); ++y)
291 {
292 j -= k; glo_daynumber += l;
293 }
294 for(m = 1; j >= (unsigned int)(k = (l = months[m]+longyear(y, m))*24*60*60)
295 + gnumleap(y, m+1, 1); ++m)
296 {
297 j -= k; glo_daynumber += l;
298 }
299 for(d = 1; j >= 24UL*60UL*60UL + gnumleap(y, m, d+1); ++d)
300 j -= 24*60*60;
301 glo_daynumber -= 16*365+4-d;
302 nul = gnumleap(y, m, d);
303 glo_timeofday = j-nul;
304
305 // original version
306 // if(mSecOfWeek < 5*60*1000 && glo_timeofday > 23*60*60)
307 // *secOfWeek += 24*60*60;
308 // else if(glo_timeofday < 5*60 && mSecOfWeek > 23*60*60*1000)
309 // *secOfWeek -= 24*60*60;
310
311 // new version
312 if(mSecOfWeek < 4*60*60*1000 && glo_timeofday > 20*60*60)
313 *secOfWeek += 24*60*60;
314 else if(glo_timeofday < 4*60*60 && mSecOfWeek > 20*60*60*1000)
315 *secOfWeek -= 24*60*60;
316
317 *secOfWeek += mSecOfWeek/1000-glo_timeofday;
318 if(fixnumleap)
319 *secOfWeek -= nul;
320 if(*secOfWeek < 0) {*secOfWeek += 24*60*60*7; --*week; }
321 if(*secOfWeek >= 24*60*60*7) {*secOfWeek -= 24*60*60*7; ++*week; }
322}
323
324int RTCM3Parser(struct RTCM3ParserData *handle)
325{
326 int ret=0;
327
328#ifdef NO_RTCM3_MAIN
329 if(GetMessage(handle)) /* don't repeat */
330#else
331 while(!ret && GetMessage(handle))
332#endif /* NO_RTCM3_MAIN */
333 {
334 /* using 64 bit integer types, as it is much easier than handling
335 the long datatypes in 32 bit */
336 uint64_t numbits = 0, bitfield = 0;
337 int size = handle->size, type;
338 int syncf, old = 0;
339 unsigned char *data = handle->Message+3;
340
341 GETBITS(type,12)
342#ifdef NO_RTCM3_MAIN
343 handle->blocktype = type;
344#endif /* NO_RTCM3_MAIN */
345 switch(type)
346 {
347#ifdef NO_RTCM3_MAIN
348 default:
349 ret = type;
350 break;
351 case 1005: case 1006:
352 {
353 SKIPBITS(22)
354 GETBITSSIGN(handle->antX, 38)
355 SKIPBITS(2)
356 GETBITSSIGN(handle->antY, 38)
357 SKIPBITS(2)
358 GETBITSSIGN(handle->antZ, 38)
359 if(type == 1006)
360 GETBITS(handle->antH, 16)
361 ret = type;
362 }
363 break;
364 case 1007: case 1008: case 1033:
365 {
366 char *antenna;
367 int antnum;
368
369 SKIPBITS(12)
370 GETSTRING(antnum,antenna)
371 memcpy(handle->antenna, antenna, antnum);
372 handle->antenna[antnum] = 0;
373 ret = type;
374 }
375 break;
376 case 1013:
377 {
378 SKIPBITS(12);
379 GETBITS(handle->modjulday, 16);
380 GETBITS(handle->secofday, 17);
381 SKIPBITS(5);
382 GETBITS(handle->leapsec, 8);
383 ret = 1013;
384 }
385 break;
386#endif /* NO_RTCM3_MAIN */
387 case 1019:
388 {
389 struct gpsephemeris *ge;
390 int sv, i;
391
392 ge = &handle->ephemerisGPS;
393 memset(ge, 0, sizeof(*ge));
394
395 GETBITS(sv, 6)
396 ge->satellite = (sv < 40 ? sv : sv+80);
397 GETBITS(ge->GPSweek, 10)
398 ge->GPSweek += 1024;
399 GETBITS(ge->URAindex, 4)
400 GETBITS(sv, 2)
401 if(sv & 1)
402 ge->flags |= GPSEPHF_L2PCODE;
403 if(sv & 2)
404 ge->flags |= GPSEPHF_L2CACODE;
405 GETFLOATSIGN(ge->IDOT, 14, R2R_PI/(double)(1<<30)/(double)(1<<13))
406 GETBITS(ge->IODE, 8)
407 GETBITS(ge->TOC, 16)
408 ge->TOC <<= 4;
409 GETFLOATSIGN(ge->clock_driftrate, 8, 1.0/(double)(1<<30)/(double)(1<<25))
410 GETFLOATSIGN(ge->clock_drift, 16, 1.0/(double)(1<<30)/(double)(1<<13))
411 GETFLOATSIGN(ge->clock_bias, 22, 1.0/(double)(1<<30)/(double)(1<<1))
412 GETBITS(ge->IODC, 10)
413 GETFLOATSIGN(ge->Crs, 16, 1.0/(double)(1<<5))
414 GETFLOATSIGN(ge->Delta_n, 16, R2R_PI/(double)(1<<30)/(double)(1<<13))
415 GETFLOATSIGN(ge->M0, 32, R2R_PI/(double)(1<<30)/(double)(1<<1))
416 GETFLOATSIGN(ge->Cuc, 16, 1.0/(double)(1<<29))
417 GETFLOAT(ge->e, 32, 1.0/(double)(1<<30)/(double)(1<<3))
418 GETFLOATSIGN(ge->Cus, 16, 1.0/(double)(1<<29))
419 GETFLOAT(ge->sqrt_A, 32, 1.0/(double)(1<<19))
420 GETBITS(ge->TOE, 16)
421 ge->TOE <<= 4;
422
423 GETFLOATSIGN(ge->Cic, 16, 1.0/(double)(1<<29))
424 GETFLOATSIGN(ge->OMEGA0, 32, R2R_PI/(double)(1<<30)/(double)(1<<1))
425 GETFLOATSIGN(ge->Cis, 16, 1.0/(double)(1<<29))
426 GETFLOATSIGN(ge->i0, 32, R2R_PI/(double)(1<<30)/(double)(1<<1))
427 GETFLOATSIGN(ge->Crc, 16, 1.0/(double)(1<<5))
428 GETFLOATSIGN(ge->omega, 32, R2R_PI/(double)(1<<30)/(double)(1<<1))
429 GETFLOATSIGN(ge->OMEGADOT, 24, R2R_PI/(double)(1<<30)/(double)(1<<13))
430 GETFLOATSIGN(ge->TGD, 8, 1.0/(double)(1<<30)/(double)(1<<1))
431 GETBITS(ge->SVhealth, 6)
432 GETBITS(sv, 1)
433 if(sv)
434 ge->flags |= GPSEPHF_L2PCODEDATA;
435 GETBITS(sv, 1)
436 if(sv)
437 ge->flags |= GPSEPHF_6HOURSFIT;
438
439 i = ((int)ge->GPSweek - (int)handle->GPSWeek)*7*24*60*60
440 + ((int)ge->TOE - (int)handle->GPSTOW) - 2*60*60;
441 if(i > 5*60*60 && i < 8*60*60)
442 {
443 handle->GPSTOW = ge->TOE;
444 handle->GPSWeek = ge->GPSweek;
445 }
446 ge->TOW = 0.9999E9;
447 ret = 1019;
448 }
449 break;
450 case 1045: case 1046:
451 {
452 struct galileoephemeris *ge;
453 int sv;
454
455 ge = &handle->ephemerisGALILEO;
456 memset(ge, 0, sizeof(*ge));
457
458 GETBITS(sv, 6)
459 ge->satellite = sv;
460 GETBITS(ge->Week, 12)
461 GETBITS(ge->IODnav, 10)
462 GETBITS(ge->SISA, 8)
463 GETFLOATSIGN(ge->IDOT, 14, R2R_PI/(double)(1<<30)/(double)(1<<13))
464 GETBITSFACTOR(ge->TOC, 14, 60)
465 GETFLOATSIGN(ge->clock_driftrate, 6, 1.0/(double)(1<<30)/(double)(1<<29))
466 GETFLOATSIGN(ge->clock_drift, 21, 1.0/(double)(1<<30)/(double)(1<<16))
467 GETFLOATSIGN(ge->clock_bias, 31, 1.0/(double)(1<<30)/(double)(1<<4))
468 GETFLOATSIGN(ge->Crs, 16, 1.0/(double)(1<<5))
469 GETFLOATSIGN(ge->Delta_n, 16, R2R_PI/(double)(1<<30)/(double)(1<<13))
470 GETFLOATSIGN(ge->M0, 32, R2R_PI/(double)(1<<30)/(double)(1<<1))
471 GETFLOATSIGN(ge->Cuc, 16, 1.0/(double)(1<<29))
472 GETFLOAT(ge->e, 32, 1.0/(double)(1<<30)/(double)(1<<3))
473 GETFLOATSIGN(ge->Cus, 16, 1.0/(double)(1<<29))
474 GETFLOAT(ge->sqrt_A, 32, 1.0/(double)(1<<19))
475 GETBITSFACTOR(ge->TOE, 14, 60)
476 GETFLOATSIGN(ge->Cic, 16, 1.0/(double)(1<<29))
477 GETFLOATSIGN(ge->OMEGA0, 32, R2R_PI/(double)(1<<30)/(double)(1<<1))
478 GETFLOATSIGN(ge->Cis, 16, 1.0/(double)(1<<29))
479 GETFLOATSIGN(ge->i0, 32, R2R_PI/(double)(1<<30)/(double)(1<<1))
480 GETFLOATSIGN(ge->Crc, 16, 1.0/(double)(1<<5))
481 GETFLOATSIGN(ge->omega, 32, R2R_PI/(double)(1<<30)/(double)(1<<1))
482 GETFLOATSIGN(ge->OMEGADOT, 24, R2R_PI/(double)(1<<30)/(double)(1<<13))
483 GETFLOATSIGN(ge->BGD_1_5A, 10, 1.0/(double)(1<<30)/(double)(1<<2))
484 if(type == 1046)
485 {
486 GETFLOATSIGN(ge->BGD_1_5B, 10, 1.0/(double)(1<<30)/(double)(1<<2))
487 GETBITS(ge->E5aHS, 2)
488 GETBITS(sv, 1)
489 if(sv)
490 ge->flags |= GALEPHF_E5ADINVALID;
491 GETFLOATSIGN(ge->BGD_1_5B, 10, 1.0/(double)(1<<30)/(double)(1<<2))
492 }
493 else
494 {
495 GETBITS(ge->E5bHS, 2)
496 GETBITS(sv, 1)
497 if(sv)
498 ge->flags |= GALEPHF_E5BDINVALID;
499 }
500 ret = type;
501 }
502 break;
503 case 1020:
504 {
505 struct glonassephemeris *ge;
506 int i;
507
508 ge = &handle->ephemerisGLONASS;
509 memset(ge, 0, sizeof(*ge));
510
511 ge->flags |= GLOEPHF_PAVAILABLE;
512 GETBITS(ge->almanac_number, 6)
513 GETBITS(i, 5)
514 ge->frequency_number = i-7;
515 if(ge->almanac_number >= 1 && ge->almanac_number <= PRN_GLONASS_NUM)
516 handle->GLOFreq[ge->almanac_number-1] = 100+ge->frequency_number;
517 GETBITS(i, 1)
518 if(i)
519 ge->flags |= GLOEPHF_ALMANACHEALTHY;
520 GETBITS(i, 1)
521 if(i)
522 ge->flags |= GLOEPHF_ALMANACHEALTHOK;
523 GETBITS(i, 2)
524 if(i & 1)
525 ge->flags |= GLOEPHF_P10TRUE;
526 if(i & 2)
527 ge->flags |= GLOEPHF_P11TRUE;
528 GETBITS(i, 5)
529 ge->tk = i*60*60;
530 GETBITS(i, 6)
531 ge->tk += i*60;
532 GETBITS(i, 1)
533 ge->tk += i*30;
534 GETBITS(i, 1)
535 if(i)
536 ge->flags |= GLOEPHF_UNHEALTHY;
537 GETBITS(i, 1)
538 if(i)
539 ge->flags |= GLOEPHF_P2TRUE;
540 GETBITS(i, 7)
541 ge->tb = i*15*60;
542 GETFLOATSIGNM(ge->x_velocity, 24, 1.0/(double)(1<<20))
543 GETFLOATSIGNM(ge->x_pos, 27, 1.0/(double)(1<<11))
544 GETFLOATSIGNM(ge->x_acceleration, 5, 1.0/(double)(1<<30))
545 GETFLOATSIGNM(ge->y_velocity, 24, 1.0/(double)(1<<20))
546 GETFLOATSIGNM(ge->y_pos, 27, 1.0/(double)(1<<11))
547 GETFLOATSIGNM(ge->y_acceleration, 5, 1.0/(double)(1<<30))
548 GETFLOATSIGNM(ge->z_velocity, 24, 1.0/(double)(1<<20))
549 GETFLOATSIGNM(ge->z_pos, 27, 1.0/(double)(1<<11))
550 GETFLOATSIGNM(ge->z_acceleration, 5, 1.0/(double)(1<<30))
551 GETBITS(i, 1)
552 if(i)
553 ge->flags |= GLOEPHF_P3TRUE;
554 GETFLOATSIGNM(ge->gamma, 11, 1.0/(double)(1<<30)/(double)(1<<10))
555 SKIPBITS(3) /* GLONASS-M P, GLONASS-M ln (third string) */
556 GETFLOATSIGNM(ge->tau, 22, 1.0/(double)(1<<30)) /* GLONASS tau n(tb) */
557 SKIPBITS(5) /* GLONASS-M delta tau n(tb) */
558 GETBITS(ge->E, 5)
559 /* GETBITS(b, 1) / * GLONASS-M P4 */
560 /* GETBITS(b, 4) / * GLONASS-M Ft */
561 /* GETBITS(b, 11) / * GLONASS-M Nt */
562 /* GETBITS(b, 2) / * GLONASS-M M */
563 /* GETBITS(b, 1) / * GLONASS-M The Availability of Additional Data */
564 /* GETBITS(b, 11) / * GLONASS-M Na */
565 /* GETFLOATSIGNM(b, 32, 1.0/(double)(1<<30)/(double)(1<<1)) / * GLONASS tau c */
566 /* GETBITS(b, 5) / * GLONASS-M N4 */
567 /* GETFLOATSIGNM(b, 22, 1.0/(double)(1<<30)/(double)(1<<1)) / * GLONASS-M tau GPS */
568 /* GETBITS(b, 1) / * GLONASS-M ln (fifth string) */
569 ge->GPSWeek = handle->GPSWeek;
570 ge->GPSTOW = handle->GPSTOW;
571 ret = 1020;
572 }
573 break;
574 case 1001: case 1002: case 1003: case 1004:
575 if(handle->GPSWeek)
576 {
577 int lastlockl1[64];
578 int lastlockl2[64];
579 struct gnssdata *gnss;
580 int i, numsats, wasamb=0;
581
582 for(i = 0; i < 64; ++i)
583 lastlockl1[i] = lastlockl2[i] = 0;
584
585 gnss = &handle->DataNew;
586
587 SKIPBITS(12) /* id */
588 GETBITS(i,30)
589 if(i/1000 < (int)handle->GPSTOW - 86400)
590 ++handle->GPSWeek;
591 handle->GPSTOW = i/1000;
592 if(gnss->week && (gnss->timeofweek != i || gnss->week
593 != handle->GPSWeek))
594 {
595 handle->Data = *gnss;
596 memset(gnss, 0, sizeof(*gnss));
597 old = 1;
598 }
599 gnss->timeofweek = i;
600 gnss->week = handle->GPSWeek;
601
602 GETBITS(syncf,1) /* sync */
603 GETBITS(numsats,5)
604 SKIPBITS(4) /* smind, smint */
605
606 while(numsats-- && gnss->numsats < GNSS_MAXSATS)
607 {
608 int sv, code, l1range, c,l,s,ce,le,se,amb=0;
609 int fullsat, num;
610
611 GETBITS(sv, 6)
612 fullsat = sv < 40 ? sv : sv+80;
613 for(num = 0; num < gnss->numsats
614 && fullsat != gnss->satellites[num]; ++num)
615 ;
616
617 if(num == gnss->numsats)
618 gnss->satellites[gnss->numsats++] = fullsat;
619
620 /* L1 */
621 GETBITS(code, 1);
622 if(code)
623 {
624 c = GNSSDF_P1DATA; ce = GNSSENTRY_P1DATA;
625 l = GNSSDF_L1PDATA; le = GNSSENTRY_L1PDATA;
626 s = GNSSDF_S1PDATA; se = GNSSENTRY_S1PDATA;
627 gnss->codetype[num][se] =
628 gnss->codetype[num][ce] = gnss->codetype[num][le] = "1W";
629 }
630 else
631 {
632 c = GNSSDF_C1DATA; ce = GNSSENTRY_C1DATA;
633 l = GNSSDF_L1CDATA; le = GNSSENTRY_L1CDATA;
634 s = GNSSDF_S1CDATA; se = GNSSENTRY_S1CDATA;
635 gnss->codetype[num][se] =
636 gnss->codetype[num][ce] = gnss->codetype[num][le] = "1C";
637 }
638 if(!handle->info[RTCM3_MSM_GPS].type[ce])
639 {
640 handle->info[RTCM3_MSM_GPS].type[ce] =
641 handle->info[RTCM3_MSM_GPS].type[le] =
642 handle->info[RTCM3_MSM_GPS].type[se] = gnss->codetype[num][ce][1];
643 }
644 GETBITS(l1range, 24);
645 GETBITSSIGN(i, 20);
646 if((i&((1<<20)-1)) != 0x80000)
647 {
648 gnss->dataflags[num] |= (c|l);
649 gnss->measdata[num][ce] = l1range*0.02;
650 gnss->measdata[num][le] = l1range*0.02+i*0.0005;
651 }
652 GETBITS(i, 7);
653 lastlockl1[sv] = i;
654 if(handle->lastlockGPSl1[sv] > i || i == 0)
655 gnss->dataflags2[num] |= GNSSDF2_LOCKLOSSL1;
656 if(type == 1002 || type == 1004)
657 {
658 GETBITS(amb,8);
659 if(amb && (gnss->dataflags[num] & c))
660 {
661 gnss->measdata[num][ce] += amb*299792.458;
662 gnss->measdata[num][le] += amb*299792.458;
663 ++wasamb;
664 }
665 GETBITS(i, 8);
666 if(i)
667 {
668 gnss->dataflags[num] |= s;
669 gnss->measdata[num][se] = i*0.25;
670 i /= 4*4;
671 if(i > 9) i = 9;
672 else if(i < 1) i = 1;
673 gnss->snrL1[num] = i;
674 }
675 }
676 gnss->measdata[num][le] /= GPS_WAVELENGTH_L1;
677 if(type == 1003 || type == 1004)
678 {
679 /* L2 */
680 GETBITS(code,2);
681 if(code)
682 {
683 c = GNSSDF_P2DATA; ce = GNSSENTRY_P2DATA;
684 l = GNSSDF_L2PDATA; le = GNSSENTRY_L2PDATA;
685 s = GNSSDF_S2PDATA; se = GNSSENTRY_S2PDATA;
686 if(code >= 2)
687 {
688 gnss->codetype[num][se] =
689 gnss->codetype[num][ce] = gnss->codetype[num][le] = "2W";
690 gnss->dataflags2[num] |= GNSSDF2_XCORRL2;
691 }
692 else
693 {
694 gnss->codetype[num][se] =
695 gnss->codetype[num][ce] = gnss->codetype[num][le] = "2P";
696 }
697 }
698 else
699 {
700 c = GNSSDF_C2DATA; ce = GNSSENTRY_C2DATA;
701 l = GNSSDF_L2CDATA; le = GNSSENTRY_L2CDATA;
702 s = GNSSDF_S2CDATA; se = GNSSENTRY_S2CDATA;
703 gnss->codetype[num][se] =
704 gnss->codetype[num][ce] = gnss->codetype[num][le] = "2 ";
705 }
706 if(!handle->info[RTCM3_MSM_GPS].type[ce])
707 {
708 handle->info[RTCM3_MSM_GPS].type[ce] =
709 handle->info[RTCM3_MSM_GPS].type[le] =
710 handle->info[RTCM3_MSM_GPS].type[se] = gnss->codetype[num][ce][1];
711 }
712 GETBITSSIGN(i,14);
713 if((i&((1<<14)-1)) != 0x2000)
714 {
715 gnss->dataflags[num] |= c;
716 gnss->measdata[num][ce] = l1range*0.02+i*0.02
717 +amb*299792.458;
718 }
719 GETBITSSIGN(i,20);
720 if((i&((1<<20)-1)) != 0x80000)
721 {
722 gnss->dataflags[num] |= l;
723 gnss->measdata[num][le] = l1range*0.02+i*0.0005
724 +amb*299792.458;
725 }
726 GETBITS(i,7);
727 lastlockl2[sv] = i;
728 if(handle->lastlockGPSl2[sv] > i || i == 0)
729 gnss->dataflags2[num] |= GNSSDF2_LOCKLOSSL2;
730 if(type == 1004)
731 {
732 GETBITS(i, 8);
733 if(i)
734 {
735 gnss->dataflags[num] |= s;
736 gnss->measdata[num][se] = i*0.25;
737 i /= 4*4;
738 if(i > 9) i = 9;
739 else if(i < 1) i = 1;
740 gnss->snrL2[num] = i;
741 }
742 }
743 gnss->measdata[num][le] /= GPS_WAVELENGTH_L2;
744 }
745 }
746 for(i = 0; i < 64; ++i)
747 {
748 handle->lastlockGPSl1[i] = lastlockl1[i];
749 handle->lastlockGPSl2[i] = lastlockl2[i];
750 }
751 if(!syncf && !old)
752 {
753 handle->Data = *gnss;
754 memset(gnss, 0, sizeof(*gnss));
755 }
756 if(!syncf || old)
757 {
758 if(wasamb) /* not RINEX compatible without */
759 ret = 1;
760 else
761 ret = 2;
762 }
763#ifdef NO_RTCM3_MAIN
764 else
765 ret = type;
766#endif /* NO_RTCM3_MAIN */
767 }
768 break;
769 case 1009: case 1010: case 1011: case 1012:
770 {
771 int lastlockl1[64];
772 int lastlockl2[64];
773 struct gnssdata *gnss;
774 int i, numsats;
775 int wasamb=0;
776
777 for(i = 0; i < 64; ++i)
778 lastlockl1[i] = lastlockl2[i] = 0;
779
780 gnss = &handle->DataNew;
781
782 SKIPBITS(12) /* id */;
783 GETBITS(i,27) /* tk */
784
785 updatetime(&handle->GPSWeek, &handle->GPSTOW, i, 0); /* Moscow -> GPS */
786 i = handle->GPSTOW*1000;
787 if(gnss->week && (gnss->timeofweek != i || gnss->week
788 != handle->GPSWeek))
789 {
790 handle->Data = *gnss;
791 memset(gnss, 0, sizeof(*gnss));
792 old = 1;
793 }
794
795 gnss->timeofweek = i;
796 gnss->week = handle->GPSWeek;
797
798 GETBITS(syncf,1) /* sync */
799 GETBITS(numsats,5)
800
801 SKIPBITS(4) /* smind, smint */
802
803 while(numsats-- && gnss->numsats < GNSS_MAXSATS)
804 {
805 int sv, code, l1range, c,l,s,ce,le,se,amb=0;
806 int freq;
807 int fullsat, num;
808
809 GETBITS(sv, 6)
810 fullsat = sv-1 + PRN_GLONASS_START;
811 for(num = 0; num < gnss->numsats
812 && fullsat != gnss->satellites[num]; ++num)
813 ;
814
815 if(num == gnss->numsats)
816 gnss->satellites[gnss->numsats++] = fullsat;
817
818 /* L1 */
819 GETBITS(code, 1)
820 GETBITS(freq, 5)
821
822 if(sv >= 1 && sv <= PRN_GLONASS_NUM)
823 handle->GLOFreq[sv-1] = 100+freq-7;
824
825 if(code)
826 {
827 c = GNSSDF_P1DATA; ce = GNSSENTRY_P1DATA;
828 l = GNSSDF_L1PDATA; le = GNSSENTRY_L1PDATA;
829 s = GNSSDF_S1PDATA; se = GNSSENTRY_S1PDATA;
830 gnss->codetype[num][se] =
831 gnss->codetype[num][ce] = gnss->codetype[num][le] = "1P";
832 }
833 else
834 {
835 c = GNSSDF_C1DATA; ce = GNSSENTRY_C1DATA;
836 l = GNSSDF_L1CDATA; le = GNSSENTRY_L1CDATA;
837 s = GNSSDF_S1CDATA; se = GNSSENTRY_S1CDATA;
838 gnss->codetype[num][se] =
839 gnss->codetype[num][ce] = gnss->codetype[num][le] = "1C";
840 }
841 if(!handle->info[RTCM3_MSM_GLONASS].type[ce])
842 {
843 handle->info[RTCM3_MSM_GLONASS].type[ce] =
844 handle->info[RTCM3_MSM_GLONASS].type[le] =
845 handle->info[RTCM3_MSM_GLONASS].type[se] = gnss->codetype[num][ce][1];
846 }
847 GETBITS(l1range, 25)
848 GETBITSSIGN(i, 20)
849 if((i&((1<<20)-1)) != 0x80000)
850 {
851 /* Handle this like GPS. Actually for GLONASS L1 range is always
852 valid. To be on the save side, we handle it as invalid like we
853 do for GPS and also remove range in case of 0x80000. */
854 gnss->dataflags[num] |= (c|l);
855 gnss->measdata[num][ce] = l1range*0.02;
856 gnss->measdata[num][le] = l1range*0.02+i*0.0005;
857 }
858 GETBITS(i, 7)
859 lastlockl1[sv] = i;
860 if(handle->lastlockGLOl1[sv] > i || i == 0)
861 gnss->dataflags2[num] |= GNSSDF2_LOCKLOSSL1;
862 if(type == 1010 || type == 1012)
863 {
864 GETBITS(amb,7)
865 if(amb && (gnss->dataflags[num] & c))
866 {
867 gnss->measdata[num][ce] += amb*599584.916;
868 gnss->measdata[num][le] += amb*599584.916;
869 ++wasamb;
870 }
871 GETBITS(i, 8)
872 if(i)
873 {
874 gnss->dataflags[num] |= s;
875 gnss->measdata[num][se] = i*0.25;
876 i /= 4*4;
877 if(i > 9) i = 9;
878 else if(i < 1) i = 1;
879 gnss->snrL1[num] = i;
880 }
881 }
882 gnss->measdata[num][le] /= GLO_WAVELENGTH_L1(freq-7);
883 if(type == 1011 || type == 1012)
884 {
885 /* L2 */
886 GETBITS(code,2)
887 if(code)
888 {
889 c = GNSSDF_P2DATA; ce = GNSSENTRY_P2DATA;
890 l = GNSSDF_L2PDATA; le = GNSSENTRY_L2PDATA;
891 s = GNSSDF_S2PDATA; se = GNSSENTRY_S2PDATA;
892 gnss->codetype[num][se] =
893 gnss->codetype[num][ce] = gnss->codetype[num][le] = "2P";
894 }
895 else
896 {
897 c = GNSSDF_C2DATA; ce = GNSSENTRY_C2DATA;
898 l = GNSSDF_L2CDATA; le = GNSSENTRY_L2CDATA;
899 s = GNSSDF_S2CDATA; se = GNSSENTRY_S2CDATA;
900 gnss->codetype[num][se] =
901 gnss->codetype[num][ce] = gnss->codetype[num][le] = "2C";
902 }
903 if(!handle->info[RTCM3_MSM_GLONASS].type[ce])
904 {
905 handle->info[RTCM3_MSM_GLONASS].type[ce] =
906 handle->info[RTCM3_MSM_GLONASS].type[le] =
907 handle->info[RTCM3_MSM_GLONASS].type[se] = gnss->codetype[num][ce][1];
908 }
909 GETBITSSIGN(i,14)
910 if((i&((1<<14)-1)) != 0x2000)
911 {
912 gnss->dataflags[num] |= c;
913 gnss->measdata[num][ce] = l1range*0.02+i*0.02
914 +amb*599584.916;
915 }
916 GETBITSSIGN(i,20)
917 if((i&((1<<20)-1)) != 0x80000)
918 {
919 gnss->dataflags[num] |= l;
920 gnss->measdata[num][le] = l1range*0.02+i*0.0005
921 +amb*599584.916;
922 }
923 GETBITS(i,7)
924 lastlockl2[sv] = i;
925 if(handle->lastlockGLOl2[sv] > i || i == 0)
926 gnss->dataflags2[num] |= GNSSDF2_LOCKLOSSL2;
927 if(type == 1012)
928 {
929 GETBITS(i, 8)
930 if(i)
931 {
932 gnss->dataflags[num] |= s;
933 gnss->measdata[num][se] = i*0.25;
934 i /= 4*4;
935 if(i > 9) i = 9;
936 else if(i < 1) i = 1;
937 gnss->snrL2[num] = i;
938 }
939 }
940 gnss->measdata[num][le] /= GLO_WAVELENGTH_L2(freq-7);
941 }
942 if(!sv || sv > 24) /* illegal, remove it again */
943 --gnss->numsats;
944 }
945 for(i = 0; i < 64; ++i)
946 {
947 handle->lastlockGLOl1[i] = lastlockl1[i];
948 handle->lastlockGLOl2[i] = lastlockl2[i];
949 }
950 if(!syncf && !old)
951 {
952 handle->Data = *gnss;
953 memset(gnss, 0, sizeof(*gnss));
954 }
955 if(!syncf || old)
956 {
957 if(wasamb) /* not RINEX compatible without */
958 ret = 1;
959 else
960 ret = 2;
961 }
962#ifdef NO_RTCM3_MAIN
963 else
964 ret = type;
965#endif /* NO_RTCM3_MAIN */
966 }
967 break;
968 case 1071: case 1081: case 1091: case 1101: case 1111: case 1121:
969 case 1072: case 1082: case 1092: case 1102: case 1112: case 1122:
970 case 1073: case 1083: case 1093: case 1103: case 1113: case 1123:
971 case 1074: case 1084: case 1094: case 1104: case 1114: case 1124:
972 case 1075: case 1085: case 1095: case 1105: case 1115: case 1125:
973 case 1076: case 1086: case 1096: case 1106: case 1116: case 1126:
974 case 1077: case 1087: case 1097: case 1107: case 1117: case 1127:
975 if(handle->GPSWeek)
976 {
977 struct CodeData {
978 int typeR;
979 int typeP;
980 int typeD;
981 int typeS;
982 int lock;
983 double wl;
984 const char *code; /* currently unused */
985 };
986 struct CodeData gps[RTCM3_MSM_NUMSIG] =
987 {
988 {0,0,0,0,0,0,0},
989 {GNSSENTRY_C1DATA,GNSSENTRY_L1CDATA,GNSSENTRY_D1CDATA,
990 GNSSENTRY_S1CDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1C"},
991 {GNSSENTRY_P1DATA,GNSSENTRY_L1PDATA,GNSSENTRY_D1PDATA,
992 GNSSENTRY_S1PDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1P"},
993 {GNSSENTRY_P1DATA,GNSSENTRY_L1PDATA,GNSSENTRY_D1PDATA,
994 GNSSENTRY_S1PDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1W"},
995 {0,0,0,0,0,0,0}/*{GNSSENTRY_P1DATA,GNSSENTRY_L1PDATA,GNSSENTRY_D1PDATA,
996 GNSSENTRY_S1PDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1Y"}*/,
997 {0,0,0,0,0,0,0},
998 {0,0,0,0,0,0,0},
999 {GNSSENTRY_C2DATA,GNSSENTRY_L2CDATA,GNSSENTRY_D2CDATA,
1000 GNSSENTRY_S2CDATA,GNSSDF2_LOCKLOSSL2,GPS_WAVELENGTH_L2,"2C"},
1001 {GNSSENTRY_P2DATA,GNSSENTRY_L2PDATA,GNSSENTRY_D2PDATA,
1002 GNSSENTRY_S2PDATA,GNSSDF2_LOCKLOSSL2,GPS_WAVELENGTH_L2,"2P"},
1003 {GNSSENTRY_P2DATA,GNSSENTRY_L2PDATA,GNSSENTRY_D2PDATA,
1004 GNSSENTRY_S2PDATA,GNSSDF2_LOCKLOSSL2,GPS_WAVELENGTH_L2,"2W"},
1005 {0,0,0,0,0,0,0}/*{GNSSENTRY_P2DATA,GNSSENTRY_L2PDATA,GNSSENTRY_D2PDATA,
1006 GNSSENTRY_S2PDATA,GNSSDF2_LOCKLOSSL2,GPS_WAVELENGTH_L2,"2Y"}*/,
1007 {0,0,0,0,0,0,0},
1008 {0,0,0,0,0,0,0},
1009 {0,0,0,0,0,0,0},
1010 {GNSSENTRY_C2DATA,GNSSENTRY_L2CDATA,GNSSENTRY_D2CDATA,
1011 GNSSENTRY_S2CDATA,GNSSDF2_LOCKLOSSL2,GPS_WAVELENGTH_L2,"2S"},
1012 {GNSSENTRY_C2DATA,GNSSENTRY_L2CDATA,GNSSENTRY_D2CDATA,
1013 GNSSENTRY_S2CDATA,GNSSDF2_LOCKLOSSL2,GPS_WAVELENGTH_L2,"2L"},
1014 {GNSSENTRY_C2DATA,GNSSENTRY_L2CDATA,GNSSENTRY_D2CDATA,
1015 GNSSENTRY_S2CDATA,GNSSDF2_LOCKLOSSL2,GPS_WAVELENGTH_L2,"2X"},
1016 {0,0,0,0,0,0,0},
1017 {0,0,0,0,0,0,0},
1018 {0,0,0,0,0,0,0},
1019 {0,0,0,0,0,0,0},
1020 {GNSSENTRY_C5DATA,GNSSENTRY_L5DATA,GNSSENTRY_D5DATA,
1021 GNSSENTRY_S5DATA,GNSSDF2_LOCKLOSSL5,GPS_WAVELENGTH_L5,"5I"},
1022 {GNSSENTRY_C5DATA,GNSSENTRY_L5DATA,GNSSENTRY_D5DATA,
1023 GNSSENTRY_S5DATA,GNSSDF2_LOCKLOSSL5,GPS_WAVELENGTH_L5,"5Q"},
1024 {GNSSENTRY_C5DATA,GNSSENTRY_L5DATA,GNSSENTRY_D5DATA,
1025 GNSSENTRY_S5DATA,GNSSDF2_LOCKLOSSL5,GPS_WAVELENGTH_L5,"5X"},
1026 {0,0,0,0,0,0,0},
1027 {0,0,0,0,0,0,0},
1028 {0,0,0,0,0,0,0},
1029 {0,0,0,0,0,0,0},
1030 {0,0,0,0,0,0,0},
1031 {GNSSENTRY_C1NDATA,GNSSENTRY_L1NDATA,GNSSENTRY_D1NDATA,
1032 GNSSENTRY_S1NDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1S"},
1033 {GNSSENTRY_C1NDATA,GNSSENTRY_L1NDATA,GNSSENTRY_D1NDATA,
1034 GNSSENTRY_S1NDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1L"},
1035 {GNSSENTRY_C1NDATA,GNSSENTRY_L1NDATA,GNSSENTRY_D1NDATA,
1036 GNSSENTRY_S1NDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1X"}
1037 };
1038 /* NOTE: Uses 0.0, 1.0 for wavelength as sat index dependence is done later! */
1039 struct CodeData glo[RTCM3_MSM_NUMSIG] =
1040 {
1041 {0,0,0,0,0,0,0},
1042 {GNSSENTRY_C1DATA,GNSSENTRY_L1CDATA,GNSSENTRY_D1CDATA,
1043 GNSSENTRY_S1CDATA,GNSSDF2_LOCKLOSSL1,0.0,"1C"},
1044 {GNSSENTRY_P1DATA,GNSSENTRY_L1PDATA,GNSSENTRY_D1PDATA,
1045 GNSSENTRY_S1PDATA,GNSSDF2_LOCKLOSSL1,0.0,"1P"},
1046 {0,0,0,0,0,0,0},
1047 {0,0,0,0,0,0,0},
1048 {0,0,0,0,0,0,0},
1049 {0,0,0,0,0,0,0},
1050 {GNSSENTRY_C2DATA,GNSSENTRY_L2CDATA,GNSSENTRY_D2CDATA,
1051 GNSSENTRY_S2CDATA,GNSSDF2_LOCKLOSSL2,1.0,"2C"},
1052 {GNSSENTRY_P2DATA,GNSSENTRY_L2PDATA,GNSSENTRY_D2PDATA,
1053 GNSSENTRY_S2PDATA,GNSSDF2_LOCKLOSSL2,1.0,"2P"},
1054 {0,0,0,0,0,0,0},
1055 {0,0,0,0,0,0,0},
1056 {0,0,0,0,0,0,0},
1057 {0,0,0,0,0,0,0},
1058 {0,0,0,0,0,0,0},
1059 {0,0,0,0,0,0,0},
1060 {0,0,0,0,0,0,0},
1061 {0,0,0,0,0,0,0},
1062 {0,0,0,0,0,0,0},
1063 {0,0,0,0,0,0,0},
1064 {0,0,0,0,0,0,0},
1065 {0,0,0,0,0,0,0},
1066 {0,0,0,0,0,0,0},
1067 {0,0,0,0,0,0,0},
1068 {0,0,0,0,0,0,0},
1069 {0,0,0,0,0,0,0},
1070 {0,0,0,0,0,0,0},
1071 {0,0,0,0,0,0,0},
1072 {0,0,0,0,0,0,0},
1073 {0,0,0,0,0,0,0},
1074 {0,0,0,0,0,0,0},
1075 {0,0,0,0,0,0,0},
1076 {0,0,0,0,0,0,0}
1077 };
1078 struct CodeData gal[RTCM3_MSM_NUMSIG] =
1079 {
1080 {0,0,0,0,0,0,0},
1081 {GNSSENTRY_C1DATA,GNSSENTRY_L1CDATA,GNSSENTRY_D1CDATA,
1082 GNSSENTRY_S1CDATA,GNSSDF2_LOCKLOSSL1,GAL_WAVELENGTH_E1,"1C"},
1083 {GNSSENTRY_C1DATA,GNSSENTRY_L1CDATA,GNSSENTRY_D1CDATA,
1084 GNSSENTRY_S1CDATA,GNSSDF2_LOCKLOSSL1,GAL_WAVELENGTH_E1,"1A"},
1085 {GNSSENTRY_C1DATA,GNSSENTRY_L1CDATA,GNSSENTRY_D1CDATA,
1086 GNSSENTRY_S1CDATA,GNSSDF2_LOCKLOSSL1,GAL_WAVELENGTH_E1,"1B"},
1087 {GNSSENTRY_C1DATA,GNSSENTRY_L1CDATA,GNSSENTRY_D1CDATA,
1088 GNSSENTRY_S1CDATA,GNSSDF2_LOCKLOSSL1,GAL_WAVELENGTH_E1,"1X"},
1089 {GNSSENTRY_C1DATA,GNSSENTRY_L1CDATA,GNSSENTRY_D1CDATA,
1090 GNSSENTRY_S1CDATA,GNSSDF2_LOCKLOSSL1,GAL_WAVELENGTH_E1,"1Z"},
1091 {0,0,0,0,0,0,0},
1092 {GNSSENTRY_C6DATA,GNSSENTRY_L6DATA,GNSSENTRY_D6DATA,
1093 GNSSENTRY_S6DATA,GNSSDF2_LOCKLOSSE6,GAL_WAVELENGTH_E6,"6C"},
1094 {GNSSENTRY_C6DATA,GNSSENTRY_L6DATA,GNSSENTRY_D6DATA,
1095 GNSSENTRY_S6DATA,GNSSDF2_LOCKLOSSE6,GAL_WAVELENGTH_E6,"6A"},
1096 {GNSSENTRY_C6DATA,GNSSENTRY_L6DATA,GNSSENTRY_D6DATA,
1097 GNSSENTRY_S6DATA,GNSSDF2_LOCKLOSSE6,GAL_WAVELENGTH_E6,"6B"},
1098 {GNSSENTRY_C6DATA,GNSSENTRY_L6DATA,GNSSENTRY_D6DATA,
1099 GNSSENTRY_S6DATA,GNSSDF2_LOCKLOSSE6,GAL_WAVELENGTH_E6,"6X"},
1100 {GNSSENTRY_C6DATA,GNSSENTRY_L6DATA,GNSSENTRY_D6DATA,
1101 GNSSENTRY_S6DATA,GNSSDF2_LOCKLOSSE6,GAL_WAVELENGTH_E6,"6Z"},
1102 {0,0,0,0,0,0,0},
1103 {GNSSENTRY_C5BDATA,GNSSENTRY_L5BDATA,GNSSENTRY_D5BDATA,
1104 GNSSENTRY_S5BDATA,GNSSDF2_LOCKLOSSE5B,GAL_WAVELENGTH_E5B,"7I"},
1105 {GNSSENTRY_C5BDATA,GNSSENTRY_L5BDATA,GNSSENTRY_D5BDATA,
1106 GNSSENTRY_S5BDATA,GNSSDF2_LOCKLOSSE5B,GAL_WAVELENGTH_E5B,"7Q"},
1107 {GNSSENTRY_C5BDATA,GNSSENTRY_L5BDATA,GNSSENTRY_D5BDATA,
1108 GNSSENTRY_S5BDATA,GNSSDF2_LOCKLOSSE5B,GAL_WAVELENGTH_E5B,"7X"},
1109 {0,0,0,0,0,0,0},
1110 {GNSSENTRY_C5ABDATA,GNSSENTRY_L5ABDATA,GNSSENTRY_D5ABDATA,
1111 GNSSENTRY_S5ABDATA,GNSSDF2_LOCKLOSSE5AB,GAL_WAVELENGTH_E5AB,"8I"},
1112 {GNSSENTRY_C5ABDATA,GNSSENTRY_L5ABDATA,GNSSENTRY_D5ABDATA,
1113 GNSSENTRY_S5ABDATA,GNSSDF2_LOCKLOSSE5AB,GAL_WAVELENGTH_E5AB,"8Q"},
1114 {GNSSENTRY_C5ABDATA,GNSSENTRY_L5ABDATA,GNSSENTRY_D5ABDATA,
1115 GNSSENTRY_S5ABDATA,GNSSDF2_LOCKLOSSE5AB,GAL_WAVELENGTH_E5AB,"8X"},
1116 {0,0,0,0,0,0,0},
1117 {GNSSENTRY_C5DATA,GNSSENTRY_L5DATA,GNSSENTRY_D5DATA,
1118 GNSSENTRY_S5DATA,GNSSDF2_LOCKLOSSL5,GAL_WAVELENGTH_E5A,"5I"},
1119 {GNSSENTRY_C5DATA,GNSSENTRY_L5DATA,GNSSENTRY_D5DATA,
1120 GNSSENTRY_S5DATA,GNSSDF2_LOCKLOSSL5,GAL_WAVELENGTH_E5A,"5Q"},
1121 {GNSSENTRY_C5DATA,GNSSENTRY_L5DATA,GNSSENTRY_D5DATA,
1122 GNSSENTRY_S5DATA,GNSSDF2_LOCKLOSSL5,GAL_WAVELENGTH_E5A,"5X"},
1123 {0,0,0,0,0,0,0},
1124 {0,0,0,0,0,0,0},
1125 {0,0,0,0,0,0,0},
1126 {0,0,0,0,0,0,0},
1127 {0,0,0,0,0,0,0},
1128 {0,0,0,0,0,0,0},
1129 {0,0,0,0,0,0,0},
1130 {0,0,0,0,0,0,0},
1131 };
1132 struct CodeData qzss[RTCM3_MSM_NUMSIG] =
1133 {
1134 {0,0,0,0,0,0,0},
1135 {GNSSENTRY_C1DATA,GNSSENTRY_L1CDATA,GNSSENTRY_D1CDATA,
1136 GNSSENTRY_S1CDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1C"},
1137 {0,0,0,0,0,0,0},
1138 {0,0,0,0,0,0,0},
1139 {0,0,0,0,0,0,0},
1140 {GNSSENTRY_CSAIFDATA,GNSSENTRY_LSAIFDATA,GNSSENTRY_DSAIFDATA,
1141 GNSSENTRY_SSAIFDATA,GNSSDF2_LOCKLOSSSAIF,GPS_WAVELENGTH_L1,"1Z"},
1142 {0,0,0,0,0,0,0},
1143 {0,0,0,0,0,0,0},
1144 {GNSSENTRY_CLEXDATA,GNSSENTRY_LLEXDATA,GNSSENTRY_DLEXDATA,
1145 GNSSENTRY_SLEXDATA,GNSSDF2_LOCKLOSSLEX,QZSS_WAVELENGTH_LEX,"6S"},
1146 {GNSSENTRY_CLEXDATA,GNSSENTRY_LLEXDATA,GNSSENTRY_DLEXDATA,
1147 GNSSENTRY_SLEXDATA,GNSSDF2_LOCKLOSSLEX,QZSS_WAVELENGTH_LEX,"6L"},
1148 {GNSSENTRY_CLEXDATA,GNSSENTRY_LLEXDATA,GNSSENTRY_DLEXDATA,
1149 GNSSENTRY_SLEXDATA,GNSSDF2_LOCKLOSSLEX,QZSS_WAVELENGTH_LEX,"6X"},
1150 {0,0,0,0,0,0,0},
1151 {0,0,0,0,0,0,0},
1152 {0,0,0,0,0,0,0},
1153 {GNSSENTRY_C2DATA,GNSSENTRY_L2CDATA,GNSSENTRY_D2CDATA,
1154 GNSSENTRY_S2CDATA,GNSSDF2_LOCKLOSSL2,GPS_WAVELENGTH_L2,"2S"},
1155 {GNSSENTRY_C2DATA,GNSSENTRY_L2CDATA,GNSSENTRY_D2CDATA,
1156 GNSSENTRY_S2CDATA,GNSSDF2_LOCKLOSSL2,GPS_WAVELENGTH_L2,"2L"},
1157 {GNSSENTRY_C2DATA,GNSSENTRY_L2CDATA,GNSSENTRY_D2CDATA,
1158 GNSSENTRY_S2CDATA,GNSSDF2_LOCKLOSSL2,GPS_WAVELENGTH_L2,"2X"},
1159 {0,0,0,0,0,0,0},
1160 {0,0,0,0,0,0,0},
1161 {0,0,0,0,0,0,0},
1162 {0,0,0,0,0,0,0},
1163 {GNSSENTRY_C5DATA,GNSSENTRY_L5DATA,GNSSENTRY_D5DATA,
1164 GNSSENTRY_S5DATA,GNSSDF2_LOCKLOSSL5,GPS_WAVELENGTH_L5,"5I"},
1165 {GNSSENTRY_C5DATA,GNSSENTRY_L5DATA,GNSSENTRY_D5DATA,
1166 GNSSENTRY_S5DATA,GNSSDF2_LOCKLOSSL5,GPS_WAVELENGTH_L5,"5Q"},
1167 {GNSSENTRY_C5DATA,GNSSENTRY_L5DATA,GNSSENTRY_D5DATA,
1168 GNSSENTRY_S5DATA,GNSSDF2_LOCKLOSSL5,GPS_WAVELENGTH_L5,"5X"},
1169 {0,0,0,0,0,0,0},
1170 {0,0,0,0,0,0,0},
1171 {0,0,0,0,0,0,0},
1172 {0,0,0,0,0,0,0},
1173 {0,0,0,0,0,0,0},
1174 {GNSSENTRY_C1NDATA,GNSSENTRY_L1NDATA,GNSSENTRY_D1NDATA,
1175 GNSSENTRY_S1NDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1D"},
1176 {GNSSENTRY_C1NDATA,GNSSENTRY_L1NDATA,GNSSENTRY_D1NDATA,
1177 GNSSENTRY_S1NDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1P"},
1178 {GNSSENTRY_C1NDATA,GNSSENTRY_L1NDATA,GNSSENTRY_D1NDATA,
1179 GNSSENTRY_S1NDATA,GNSSDF2_LOCKLOSSL1,GPS_WAVELENGTH_L1,"1X"}
1180 };
1181 struct CodeData compass[RTCM3_MSM_NUMSIG] =
1182 {
1183 {0,0,0,0,0,0,0},
1184 {GNSSENTRY_CB1DATA,GNSSENTRY_LB1DATA,GNSSENTRY_DB1DATA,
1185 GNSSENTRY_SB1DATA,GNSSDF2_LOCKLOSSB1,COMPASS_WAVELENGTH_B1,"2I"},
1186 {0,0,0,0,0,0,0},
1187 {0,0,0,0,0,0,0},
1188 {0,0,0,0,0,0,0},
1189 {0,0,0,0,0,0,0},
1190 {0,0,0,0,0,0,0},
1191 {GNSSENTRY_CB3DATA,GNSSENTRY_LB3DATA,GNSSENTRY_DB3DATA,
1192 GNSSENTRY_SB3DATA,GNSSDF2_LOCKLOSSB3,COMPASS_WAVELENGTH_B3,"6I"},
1193 {0,0,0,0,0,0,0},
1194 {0,0,0,0,0,0,0},
1195 {0,0,0,0,0,0,0},
1196 {0,0,0,0,0,0,0},
1197 {0,0,0,0,0,0,0},
1198 {GNSSENTRY_CB2DATA,GNSSENTRY_LB2DATA,GNSSENTRY_DB2DATA,
1199 GNSSENTRY_SB2DATA,GNSSDF2_LOCKLOSSB2,COMPASS_WAVELENGTH_B2,"7I"},
1200 {0,0,0,0,0,0,0},
1201 {0,0,0,0,0,0,0},
1202 {0,0,0,0,0,0,0},
1203 {0,0,0,0,0,0,0},
1204 {0,0,0,0,0,0,0},
1205 {0,0,0,0,0,0,0},
1206 {0,0,0,0,0,0,0},
1207 {0,0,0,0,0,0,0},
1208 {0,0,0,0,0,0,0},
1209 {0,0,0,0,0,0,0},
1210 {0,0,0,0,0,0,0},
1211 {0,0,0,0,0,0,0},
1212 {0,0,0,0,0,0,0},
1213 {0,0,0,0,0,0,0},
1214 {0,0,0,0,0,0,0},
1215 {0,0,0,0,0,0,0},
1216 {0,0,0,0,0,0,0},
1217 {0,0,0,0,0,0,0},
1218 };
1219
1220 int sys = RTCM3_MSM_GPS, i=0, count, j, old = 0, wasnoamb = 0,
1221 start=PRN_GPS_START;
1222 int syncf, sigmask, numsat = 0, numsig = 0, numcells;
1223 uint64_t satmask, cellmask, ui;
1224 double rrmod[RTCM3_MSM_NUMSAT];
1225 int rrint[RTCM3_MSM_NUMSAT], rdop[RTCM3_MSM_NUMSAT],
1226 extsat[RTCM3_MSM_NUMSAT];
1227 int ll[RTCM3_MSM_NUMCELLS]/*, hc[RTCM3_MSM_NUMCELLS]*/;
1228 double cnr[RTCM3_MSM_NUMCELLS];
1229 double cp[RTCM3_MSM_NUMCELLS], psr[RTCM3_MSM_NUMCELLS],
1230 dop[RTCM3_MSM_NUMCELLS];
1231 struct gnssdata *gnss = &handle->DataNew;
1232
1233 SKIPBITS(12)
1234 if(type >= 1121)
1235 {
1236 sys = RTCM3_MSM_COMPASS;
1237 start = PRN_COMPASS_START;
1238 }
1239 else if(type >= 1111)
1240 {
1241 sys = RTCM3_MSM_QZSS;
1242 start = PRN_QZSS_START;
1243 }
1244 else if(type >= 1101)
1245 {
1246 sys = RTCM3_MSM_SBAS;
1247 start = PRN_SBAS_START;
1248 }
1249 else if(type >= 1091)
1250 {
1251 sys = RTCM3_MSM_GALILEO;
1252 start = PRN_GALILEO_START;
1253 }
1254 else if(type >= 1081)
1255 {
1256 sys = RTCM3_MSM_GLONASS;
1257 start = PRN_GLONASS_START;
1258 }
1259
1260 for(i = 0; i < RTCM3_MSM_NUMSAT; ++i)
1261 extsat[i] = 15;
1262
1263 switch(sys)
1264 {
1265 case RTCM3_MSM_COMPASS:
1266 GETBITS(i,30)
1267 i += 14000;
1268 if(i >= 7*24*60*60*1000)
1269 i -= 7*24*60*60*1000;
1270 if(i/1000 < (int)handle->GPSTOW - 86400)
1271 ++handle->GPSWeek;
1272 handle->GPSTOW = i/1000;
1273 break;
1274 case RTCM3_MSM_GALILEO: /* use DF004 instead of DF248 */
1275 case RTCM3_MSM_QZSS:
1276 case RTCM3_MSM_SBAS:
1277 case RTCM3_MSM_GPS:
1278 GETBITS(i,30)
1279 if(i/1000 < (int)handle->GPSTOW - 86400)
1280 ++handle->GPSWeek;
1281 handle->GPSTOW = i/1000;
1282 break;
1283 case RTCM3_MSM_GLONASS:
1284 SKIPBITS(3)
1285 GETBITS(i,27) /* tk */
1286
1287 updatetime(&handle->GPSWeek, &handle->GPSTOW, i, 0); /* Moscow -> GPS */
1288 i = handle->GPSTOW*1000;
1289 break;
1290 }
1291
1292 if(gnss->week && (gnss->timeofweek != i || gnss->week
1293 != handle->GPSWeek))
1294 {
1295 handle->Data = *gnss;
1296 memset(gnss, 0, sizeof(*gnss));
1297 old = 1;
1298 }
1299 gnss->timeofweek = i;
1300 gnss->week = handle->GPSWeek;
1301
1302 GETBITS(syncf, 1)
1303 SKIPBITS(3+7+2+2+1+3)
1304 GETBITS64(satmask, RTCM3_MSM_NUMSAT)
1305
1306 /* http://gurmeetsingh.wordpress.com/2008/08/05/fast-bit-counting-routines/ */
1307 for(ui = satmask; ui; ui &= (ui - 1) /* remove rightmost bit */)
1308 ++numsat;
1309 GETBITS(sigmask, RTCM3_MSM_NUMSIG)
1310 for(i = sigmask; i; i &= (i - 1) /* remove rightmost bit */)
1311 ++numsig;
1312 i = numsat*numsig;
1313 GETBITS64(cellmask, (unsigned)i)
1314
1315 switch(type % 10)
1316 {
1317 case 1: case 2: case 3:
1318 ++wasnoamb;
1319 for(j = numsat; j--;)
1320 GETFLOAT(rrmod[j], 10, 1.0/1024.0)
1321 break;
1322 case 4: case 6:
1323 for(j = numsat; j--;)
1324 GETBITS(rrint[j], 8)
1325 for(j = numsat; j--;)
1326 GETFLOAT(rrmod[j], 10, 1.0/1024.0)
1327 break;
1328 case 5: case 7:
1329 for(j = numsat; j--;)
1330 GETBITS(rrint[j], 8)
1331 for(j = numsat; j--;)
1332 GETBITS(extsat[j], 4)
1333 for(j = numsat; j--;)
1334 GETFLOAT(rrmod[j], 10, 1.0/1024.0)
1335 for(j = numsat; j--;)
1336 GETBITSSIGN(rdop[j], 14)
1337 break;
1338 }
1339
1340 numcells = numsat*numsig;
1341 if(numcells <= RTCM3_MSM_NUMCELLS)
1342 {
1343 switch(type % 10)
1344 {
1345 case 1:
1346 for(count = numcells; count--;)
1347 if(cellmask & (UINT64(1)<<count))
1348 GETFLOATSIGN(psr[count], 15, 1.0/(1<<24))
1349 break;
1350 case 2:
1351 for(count = numcells; count--;)
1352 if(cellmask & (UINT64(1)<<count))
1353 GETFLOATSIGN(cp[count], 22, 1.0/(1<<29))
1354 for(count = numcells; count--;)
1355 if(cellmask & (UINT64(1)<<count))
1356 GETBITS(ll[count], 4)
1357 for(count = numcells; count--;)
1358 if(cellmask & (UINT64(1)<<count))
1359 SKIPBITS(1)/*GETBITS(hc[count], 1)*/
1360 break;
1361 case 3:
1362 for(count = numcells; count--;)
1363 if(cellmask & (UINT64(1)<<count))
1364 GETFLOATSIGN(psr[count], 15, 1.0/(1<<24))
1365 for(count = numcells; count--;)
1366 if(cellmask & (UINT64(1)<<count))
1367 GETFLOATSIGN(cp[count], 22, 1.0/(1<<29))
1368 for(count = numcells; count--;)
1369 if(cellmask & (UINT64(1)<<count))
1370 GETBITS(ll[count], 4)
1371 for(count = numcells; count--;)
1372 if(cellmask & (UINT64(1)<<count))
1373 SKIPBITS(1)/*GETBITS(hc[count], 1)*/
1374 break;
1375 case 4:
1376 for(count = numcells; count--;)
1377 if(cellmask & (UINT64(1)<<count))
1378 GETFLOATSIGN(psr[count], 15, 1.0/(1<<24))
1379 for(count = numcells; count--;)
1380 if(cellmask & (UINT64(1)<<count))
1381 GETFLOATSIGN(cp[count], 22, 1.0/(1<<29))
1382 for(count = numcells; count--;)
1383 if(cellmask & (UINT64(1)<<count))
1384 GETBITS(ll[count], 4)
1385 for(count = numcells; count--;)
1386 if(cellmask & (UINT64(1)<<count))
1387 SKIPBITS(1)/*GETBITS(hc[count], 1)*/
1388 for(count = numcells; count--;)
1389 if(cellmask & (UINT64(1)<<count))
1390 GETBITS(cnr[count], 6)
1391 break;
1392 case 5:
1393 for(count = numcells; count--;)
1394 if(cellmask & (UINT64(1)<<count))
1395 GETFLOATSIGN(psr[count], 15, 1.0/(1<<24))
1396 for(count = numcells; count--;)
1397 if(cellmask & (UINT64(1)<<count))
1398 GETFLOATSIGN(cp[count], 22, 1.0/(1<<29))
1399 for(count = numcells; count--;)
1400 if(cellmask & (UINT64(1)<<count))
1401 GETBITS(ll[count], 4)
1402 for(count = numcells; count--;)
1403 if(cellmask & (UINT64(1)<<count))
1404 SKIPBITS(1)/*GETBITS(hc[count], 1)*/
1405 for(count = numcells; count--;)
1406 if(cellmask & (UINT64(1)<<count))
1407 GETFLOAT(cnr[count], 6, 1.0)
1408 for(count = numcells; count--;)
1409 if(cellmask & (UINT64(1)<<count))
1410 GETFLOATSIGN(dop[count], 15, 0.0001)
1411 break;
1412 case 6:
1413 for(count = numcells; count--;)
1414 if(cellmask & (UINT64(1)<<count))
1415 GETFLOATSIGN(psr[count], 20, 1.0/(1<<29))
1416 for(count = numcells; count--;)
1417 if(cellmask & (UINT64(1)<<count))
1418 GETFLOATSIGN(cp[count], 24, 1.0/(1U<<31))
1419 for(count = numcells; count--;)
1420 if(cellmask & (UINT64(1)<<count))
1421 GETBITS(ll[count], 10)
1422 for(count = numcells; count--;)
1423 if(cellmask & (UINT64(1)<<count))
1424 SKIPBITS(1)/*GETBITS(hc[count], 1)*/
1425 for(count = numcells; count--;)
1426 if(cellmask & (UINT64(1)<<count))
1427 GETFLOAT(cnr[count], 10, 1.0/(1<<4))
1428 break;
1429 case 7:
1430 for(count = numcells; count--;)
1431 if(cellmask & (UINT64(1)<<count))
1432 GETFLOATSIGN(psr[count], 20, 1.0/(1<<29))
1433 for(count = numcells; count--;)
1434 if(cellmask & (UINT64(1)<<count))
1435 GETFLOATSIGN(cp[count], 24, 1.0/(1U<<31))
1436 for(count = numcells; count--;)
1437 if(cellmask & (UINT64(1)<<count))
1438 GETBITS(ll[count], 10)
1439 for(count = numcells; count--;)
1440 if(cellmask & (UINT64(1)<<count))
1441 SKIPBITS(1)/*GETBITS(hc[count], 1)*/
1442 for(count = numcells; count--;)
1443 if(cellmask & (UINT64(1)<<count))
1444 GETFLOAT(cnr[count], 10, 1.0/(1<<4))
1445 for(count = numcells; count--;)
1446 if(cellmask & (UINT64(1)<<count))
1447 GETFLOATSIGN(dop[count], 15, 0.0001)
1448 break;
1449 }
1450 i = RTCM3_MSM_NUMSAT;
1451 j = -1;
1452 for(count = numcells; count--;)
1453 {
1454 while(j >= 0 && !(sigmask&(1<<--j)))
1455 ;
1456 if(j < 0)
1457 {
1458 while(!(satmask&(UINT64(1)<<(--i)))) /* next satellite */
1459 ;
1460 j = RTCM3_MSM_NUMSIG;
1461 while(!(sigmask&(1<<--j)))
1462 ;
1463 --numsat;
1464 }
1465 if(cellmask & (UINT64(1)<<count))
1466 {
1467 struct CodeData cd = {0,0,0,0,0,0,0};
1468 double wl = 0.0;
1469 switch(sys)
1470 {
1471 case RTCM3_MSM_QZSS:
1472 cd = qzss[RTCM3_MSM_NUMSIG-j-1];
1473 wl = cd.wl;
1474 break;
1475 case RTCM3_MSM_COMPASS:
1476 cd = compass[RTCM3_MSM_NUMSIG-j-1];
1477 wl = cd.wl;
1478 break;
1479 case RTCM3_MSM_GPS: case RTCM3_MSM_SBAS:
1480 cd = gps[RTCM3_MSM_NUMSIG-j-1];
1481 wl = cd.wl;
1482 break;
1483 case RTCM3_MSM_GLONASS: cd = glo[RTCM3_MSM_NUMSIG-j-1];
1484 {
1485 int k = handle->GLOFreq[RTCM3_MSM_NUMSAT-i-1];
1486 if(!k && extsat[count] < 14)
1487 {
1488 k = handle->GLOFreq[RTCM3_MSM_NUMSAT-i-1]
1489 = 100+extsat[count]-7;
1490 }
1491 if(k)
1492 {
1493 if(cd.wl == 0.0)
1494 wl = GLO_WAVELENGTH_L1(k-100);
1495 else if(cd.wl == 1.0)
1496 wl = GLO_WAVELENGTH_L2(k-100);
1497 }
1498 }
1499 break;
1500 case RTCM3_MSM_GALILEO: cd = gal[RTCM3_MSM_NUMSIG-j-1];
1501 wl = cd.wl;
1502 break;
1503 }
1504 if(cd.lock && wl) /* lock cannot have a valid zero value */
1505 {
1506 int fullsat = RTCM3_MSM_NUMSAT-i-1, num;
1507
1508 if(sys == RTCM3_MSM_GALILEO && fullsat >= 50 && fullsat <= 51)
1509 fullsat += PRN_GIOVE_START-50;
1510 else
1511 fullsat += start;
1512
1513 for(num = 0; num < gnss->numsats
1514 && fullsat != gnss->satellites[num]; ++num)
1515 ;
1516
1517 if(num == gnss->numsats)
1518 gnss->satellites[gnss->numsats++] = fullsat;
1519
1520 gnss->codetype[num][cd.typeR] =
1521 gnss->codetype[num][cd.typeP] =
1522 gnss->codetype[num][cd.typeD] =
1523 gnss->codetype[num][cd.typeS] = cd.code;
1524 if(!handle->info[sys].type[cd.typeR])
1525 {
1526 handle->info[sys].type[cd.typeR] =
1527 handle->info[sys].type[cd.typeP] =
1528 handle->info[sys].type[cd.typeD] =
1529 handle->info[sys].type[cd.typeS] = cd.code[1];
1530 }
1531
1532 switch(type % 10)
1533 {
1534 case 1:
1535 if(psr[count] > -1.0/(1<<10))
1536 {
1537 gnss->measdata[num][cd.typeR] = psr[count]*LIGHTSPEED/1000.0
1538 +(rrmod[numsat])*LIGHTSPEED/1000.0;
1539 gnss->dataflags[num] |= (1LL<<cd.typeR);
1540 }
1541 break;
1542 case 2:
1543 if(wl && cp[count] > -1.0/(1<<8))
1544 {
1545 gnss->measdata[num][cd.typeP] = cp[count]*LIGHTSPEED/1000.0/wl
1546 +(rrmod[numsat])*LIGHTSPEED/1000.0/wl;
1547 if(handle->lastlockmsm[j][i] != ll[count])
1548 {
1549 gnss->dataflags2[num] |= cd.lock;
1550 handle->lastlockmsm[j][i] = ll[count];
1551 }
1552 gnss->dataflags[num] |= (1LL<<cd.typeP);
1553 }
1554 break;
1555 case 3:
1556 if(psr[count] > -1.0/(1<<10))
1557 {
1558 gnss->measdata[num][cd.typeR] = psr[count]*LIGHTSPEED/1000.0
1559 +(rrmod[numsat])*LIGHTSPEED/1000.0;
1560 gnss->dataflags[num] |= (1LL<<cd.typeR);
1561 }
1562
1563 if(wl && cp[count] > -1.0/(1<<8))
1564 {
1565 gnss->measdata[num][cd.typeP] = cp[count]*LIGHTSPEED/1000.0/wl
1566 +(rrmod[numsat]+rrint[numsat])*LIGHTSPEED/1000.0/wl;
1567 if(handle->lastlockmsm[j][i] != ll[count])
1568 {
1569 gnss->dataflags2[num] |= cd.lock;
1570 handle->lastlockmsm[j][i] = ll[count];
1571 }
1572 gnss->dataflags[num] |= (1LL<<cd.typeP);
1573 }
1574 break;
1575 case 4:
1576 if(psr[count] > -1.0/(1<<10))
1577 {
1578 gnss->measdata[num][cd.typeR] = psr[count]*LIGHTSPEED/1000.0
1579 +(rrmod[numsat]+rrint[numsat])*LIGHTSPEED/1000.0;
1580 gnss->dataflags[num] |= (1LL<<cd.typeR);
1581 }
1582
1583 if(wl && cp[count] > -1.0/(1<<8))
1584 {
1585 gnss->measdata[num][cd.typeP] = cp[count]*LIGHTSPEED/1000.0/wl
1586 +(rrmod[numsat]+rrint[numsat])*LIGHTSPEED/1000.0/wl;
1587 if(handle->lastlockmsm[j][i] != ll[count])
1588 {
1589 gnss->dataflags2[num] |= cd.lock;
1590 handle->lastlockmsm[j][i] = ll[count];
1591 }
1592 gnss->dataflags[num] |= (1LL<<cd.typeP);
1593 }
1594
1595 gnss->measdata[num][cd.typeS] = cnr[count];
1596 gnss->dataflags[num] |= (1LL<<cd.typeS);
1597 break;
1598 case 5:
1599 if(psr[count] > -1.0/(1<<10))
1600 {
1601 gnss->measdata[num][cd.typeR] = psr[count]*LIGHTSPEED/1000.0
1602 +(rrmod[numsat]+rrint[numsat])*LIGHTSPEED/1000.0;
1603 gnss->dataflags[num] |= (1LL<<cd.typeR);
1604 }
1605
1606 if(wl && cp[count] > -1.0/(1<<8))
1607 {
1608 gnss->measdata[num][cd.typeP] = cp[count]*LIGHTSPEED/1000.0/wl
1609 +(rrmod[numsat]+rrint[numsat])*LIGHTSPEED/1000.0/wl;
1610 if(handle->lastlockmsm[j][i] != ll[count])
1611 {
1612 gnss->dataflags2[num] |= cd.lock;
1613 handle->lastlockmsm[j][i] = ll[count];
1614 }
1615 gnss->dataflags[num] |= (1LL<<cd.typeP);
1616 }
1617
1618 gnss->measdata[num][cd.typeS] = cnr[count];
1619 gnss->dataflags[num] |= (1<<cd.typeS);
1620
1621 if(dop[count] > -1.6384)
1622 {
1623 gnss->measdata[num][cd.typeD] = -(dop[count]
1624 +rdop[numsat])/wl;
1625 gnss->dataflags[num] |= (1LL<<cd.typeD);
1626 }
1627 break;
1628 case 6:
1629 if(psr[count] > -1.0/(1<<10))
1630 {
1631 gnss->measdata[num][cd.typeR] = psr[count]*LIGHTSPEED/1000.0
1632 +(rrmod[numsat]+rrint[numsat])*LIGHTSPEED/1000.0;
1633 gnss->dataflags[num] |= (1LL<<cd.typeR);
1634 }
1635
1636 if(wl && cp[count] > -1.0/(1<<8))
1637 {
1638 gnss->measdata[num][cd.typeP] = cp[count]*LIGHTSPEED/1000.0/wl
1639 +(rrmod[numsat]+rrint[numsat])*LIGHTSPEED/1000.0/wl;
1640 if(handle->lastlockmsm[j][i] != ll[count])
1641 {
1642 gnss->dataflags2[num] |= cd.lock;
1643 handle->lastlockmsm[j][i] = ll[count];
1644 }
1645 gnss->dataflags[num] |= (1LL<<cd.typeP);
1646 }
1647
1648 gnss->measdata[num][cd.typeS] = cnr[count];
1649 gnss->dataflags[num] |= (1LL<<cd.typeS);
1650 break;
1651 case 7:
1652 if(psr[count] > -1.0/(1<<10))
1653 {
1654 gnss->measdata[num][cd.typeR] = psr[count]*LIGHTSPEED/1000.0
1655 +(rrmod[numsat]+rrint[numsat])*LIGHTSPEED/1000.0;
1656 gnss->dataflags[num] |= (1LL<<cd.typeR);
1657 }
1658
1659 if(wl && cp[count] > -1.0/(1<<8))
1660 {
1661 gnss->measdata[num][cd.typeP] = cp[count]*LIGHTSPEED/1000.0/wl
1662 +(rrmod[numsat]+rrint[numsat])*LIGHTSPEED/1000.0/wl;
1663 if(handle->lastlockmsm[j][i] != ll[count])
1664 {
1665 gnss->dataflags2[num] |= cd.lock;
1666 handle->lastlockmsm[j][i] = ll[count];
1667 }
1668 gnss->dataflags[num] |= (1LL<<cd.typeP);
1669 }
1670
1671 gnss->measdata[num][cd.typeS] = cnr[count];
1672 gnss->dataflags[num] |= (1LL<<cd.typeS);
1673
1674 if(dop[count] > -1.6384)
1675 {
1676 gnss->measdata[num][cd.typeD] = -(dop[count]
1677 +rdop[numsat])/wl;
1678 gnss->dataflags[num] |= (1LL<<cd.typeD);
1679 }
1680 break;
1681 }
1682 }
1683 }
1684 }
1685 }
1686 if(!syncf && !old)
1687 {
1688 handle->Data = *gnss;
1689 memset(gnss, 0, sizeof(*gnss));
1690 }
1691 if(!syncf || old)
1692 {
1693 if(!wasnoamb) /* not RINEX compatible without */
1694 ret = 1;
1695 else
1696 ret = 2;
1697 }
1698#ifdef NO_RTCM3_MAIN
1699 else
1700 ret = type;
1701#endif /* NO_RTCM3_MAIN */
1702 }
1703 break;
1704 }
1705 }
1706 return ret;
1707}
1708
1709struct Header
1710{
1711 const char *version;
1712 const char *pgm;
1713 const char *marker;
1714 const char *markertype;
1715 const char *observer;
1716 const char *receiver;
1717 const char *antenna;
1718 const char *position;
1719 const char *antennaposition;
1720 const char *wavelength;
1721 const char *typesofobs; /* should not be modified outside */
1722 const char *typesofobsG; /* should not be modified outside */
1723 const char *typesofobsR; /* should not be modified outside */
1724 const char *typesofobsS; /* should not be modified outside */
1725 const char *typesofobsE; /* should not be modified outside */
1726 const char *typesofobsC; /* should not be modified outside */
1727 const char *typesofobsJ; /* should not be modified outside */
1728 const char *timeoffirstobs; /* should not be modified outside */
1729};
1730
1731#define MAXHEADERLINES 50
1732#define MAXHEADERBUFFERSIZE 4096
1733struct HeaderData
1734{
1735 union
1736 {
1737 struct Header named;
1738 const char *unnamed[MAXHEADERLINES];
1739 } data;
1740 int numheaders;
1741};
1742
1743void converttime(struct converttimeinfo *c, int week, int tow)
1744{
1745 int i, k, doy, j; /* temporary variables */
1746 j = week*(7*24*60*60) + tow + 5*24*60*60;
1747 for(i = 1980; j >= (k = (365+longyear(i,0))*24*60*60); ++i)
1748 j -= k;
1749 c->year = i;
1750 doy = 1+ (j / (24*60*60));
1751 j %= (24*60*60);
1752 c->hour = j / (60*60);
1753 j %= (60*60);
1754 c->minute = j / 60;
1755 c->second = j % 60;
1756 j = 0;
1757 for(i = 1; j + (k = months[i] + longyear(c->year,i)) < doy; ++i)
1758 j += k;
1759 c->month = i;
1760 c->day = doy - j;
1761}
1762
1763#ifndef NO_RTCM3_MAIN
1764void RTCM3Error(const char *fmt, ...)
1765{
1766 va_list v;
1767 va_start(v, fmt);
1768 vfprintf(stderr, fmt, v);
1769 va_end(v);
1770}
1771#endif
1772
1773void RTCM3Text(const char *fmt, ...)
1774{
1775 va_list v;
1776 va_start(v, fmt);
1777 vprintf(fmt, v);
1778 va_end(v);
1779}
1780
1781static void fixrevision(void)
1782{
1783 if(revisionstr[0] == '$')
1784 {
1785 char *a;
1786 int i=sizeof(RTCM3TORINEX_VERSION); /* set version to 1.<revision> */
1787 strcpy(revisionstr, RTCM3TORINEX_VERSION ".");
1788 for(a = revisionstr+11; *a && *a != ' '; ++a)
1789 revisionstr[i++] = *a;
1790 revisionstr[i] = 0;
1791 }
1792}
1793
1794static int HandleRunBy(char *buffer, int buffersize, const char **u,
1795int rinex3)
1796{
1797 const char *user;
1798 time_t t;
1799 struct tm * t2;
1800
1801#ifdef NO_RTCM3_MAIN
1802 fixrevision();
1803#endif
1804
1805 user= getenv("USER");
1806 if(!user) user = "";
1807 t = time(&t);
1808 t2 = gmtime(&t);
1809 if(u) *u = user;
1810 return 1+snprintf(buffer, buffersize,
1811 rinex3 ?
1812 "RTCM3TORINEX %-7.7s%-20.20s%04d%02d%02d %02d%02d%02d UTC "
1813 "PGM / RUN BY / DATE" :
1814 "RTCM3TORINEX %-7.7s%-20.20s%04d-%02d-%02d %02d:%02d "
1815 "PGM / RUN BY / DATE", revisionstr, user, 1900+t2->tm_year,
1816 t2->tm_mon+1, t2->tm_mday, t2->tm_hour, t2->tm_min, t2->tm_sec);
1817}
1818
1819#ifdef NO_RTCM3_MAIN
1820#define NUMSTARTSKIP 1
1821#else
1822#define NUMSTARTSKIP 3
1823#endif
1824
1825int HandleObsHeader(struct RTCM3ParserData *Parser, char *buffer,
1826size_t buffersize, struct HeaderData *hdata)
1827{
1828 int buffersizeold = buffersize;
1829 int i, modified = 0;
1830
1831 if(Parser->rinex3)
1832 {
1833 int flags;
1834#define CHECKFLAGSNEW(a, b, c) \
1835 if(flags & GNSSDF_##b##DATA) \
1836 { \
1837 int new = hdata ? 1 : 0; \
1838 if(!hdata) /* check if already known */ \
1839 { \
1840 int ic; \
1841 for(ic = 0; ic < Parser->info[RTCM3_MSM_##a].numtypes \
1842 && Parser->info[RTCM3_MSM_##a].flags[ic] != GNSSDF_##b##DATA; ++ic) \
1843 ; \
1844 if(ic == Parser->info[RTCM3_MSM_##a].numtypes) \
1845 new = 1; \
1846 } \
1847 if(new) \
1848 { \
1849 Parser->info[RTCM3_MSM_##a].flags[Parser->info[RTCM3_MSM_##a].numtypes] \
1850 = GNSSDF_##b##DATA; \
1851 Parser->info[RTCM3_MSM_##a].pos[Parser->info[RTCM3_MSM_##a].numtypes] \
1852 = GNSSENTRY_##b##DATA; \
1853 if(Parser->info[RTCM3_MSM_##a].type[GNSSENTRY_##b##DATA]) \
1854 { \
1855 snprintf(Parser->fieldbuffer##a+4*Parser->info[RTCM3_MSM_##a].numtypes, \
1856 sizeof(Parser->fieldbuffer##a)-4*Parser->info[RTCM3_MSM_##a].numtypes, \
1857 " %-2.2s%c", #c, Parser->info[RTCM3_MSM_##a].type[GNSSENTRY_##b##DATA]); \
1858 } \
1859 else \
1860 { \
1861 snprintf(Parser->fieldbuffer##a+4*Parser->info[RTCM3_MSM_##a].numtypes, \
1862 sizeof(Parser->fieldbuffer##a)-4*Parser->info[RTCM3_MSM_##a].numtypes, \
1863 " %-3s", #c); \
1864 } \
1865 ++Parser->info[RTCM3_MSM_##a].numtypes; \
1866 ++modified; \
1867 } \
1868 }
1869
1870#define INITFLAGS(a) \
1871 flags = Parser->startflags; \
1872 modified = 0; \
1873 for(i = 0; i < Parser->Data.numsats; ++i) \
1874 { \
1875 if(Parser->Data.satellites[i] >= PRN_##a##_START \
1876 && Parser->Data.satellites[i] <= PRN_##a##_END) \
1877 flags |= Parser->Data.dataflags[i]; \
1878 }
1879
1880 INITFLAGS(SBAS)
1881 CHECKFLAGSNEW(SBAS, C1, C1C)
1882 CHECKFLAGSNEW(SBAS, L1C, L1C)
1883 CHECKFLAGSNEW(SBAS, D1C, D1C)
1884 CHECKFLAGSNEW(SBAS, S1C, S1C)
1885 CHECKFLAGSNEW(SBAS, C5, C5)
1886 CHECKFLAGSNEW(SBAS, L5, L5)
1887 CHECKFLAGSNEW(SBAS, D5, D5)
1888 CHECKFLAGSNEW(SBAS, S5, S5)
1889
1890 if(modified)
1891 {
1892 if(hdata)
1893 hdata->data.named.typesofobsS = buffer;
1894 i = 1+snprintf(buffer, buffersize,
1895 "S %3d%-52.52s SYS / # / OBS TYPES",
1896 Parser->info[RTCM3_MSM_SBAS].numtypes, Parser->fieldbufferSBAS);
1897 buffer += i; buffersize -= i;
1898 }
1899
1900 INITFLAGS(GPS)
1901 CHECKFLAGSNEW(GPS, C1, C1C)
1902 CHECKFLAGSNEW(GPS, L1C, L1C)
1903 CHECKFLAGSNEW(GPS, D1C, D1C)
1904 CHECKFLAGSNEW(GPS, S1C, S1C)
1905 CHECKFLAGSNEW(GPS, P1, C1W)
1906 CHECKFLAGSNEW(GPS, L1P, L1W)
1907 CHECKFLAGSNEW(GPS, D1P, D1W)
1908 CHECKFLAGSNEW(GPS, S1P, S1W)
1909 CHECKFLAGSNEW(GPS, C5, C5)
1910 CHECKFLAGSNEW(GPS, L5, L5)
1911 CHECKFLAGSNEW(GPS, D5, D5)
1912 CHECKFLAGSNEW(GPS, S5, S5)
1913 CHECKFLAGSNEW(GPS, P2, C2W)
1914 CHECKFLAGSNEW(GPS, L2P, L2W)
1915 CHECKFLAGSNEW(GPS, D2P, D2W)
1916 CHECKFLAGSNEW(GPS, S2P, S2W)
1917 CHECKFLAGSNEW(GPS, C2, C2)
1918 CHECKFLAGSNEW(GPS, L2C, L2)
1919 CHECKFLAGSNEW(GPS, D2C, D2)
1920 CHECKFLAGSNEW(GPS, S2C, S2)
1921 CHECKFLAGSNEW(GPS, C1N, C1)
1922 CHECKFLAGSNEW(GPS, L1N, L1)
1923 CHECKFLAGSNEW(GPS, D1N, D1)
1924 CHECKFLAGSNEW(GPS, S1N, S1)
1925
1926 if(modified)
1927 {
1928 if(hdata)
1929 hdata->data.named.typesofobsG = buffer;
1930 i = 1+snprintf(buffer, buffersize,
1931 "G %3d%-52.52s SYS / # / OBS TYPES",
1932 Parser->info[RTCM3_MSM_GPS].numtypes, Parser->fieldbufferGPS);
1933 if(Parser->info[RTCM3_MSM_GPS].numtypes>13)
1934 {
1935 i += snprintf(buffer+i-1, buffersize,
1936 "\n %-52.52s SYS / # / OBS TYPES", Parser->fieldbufferGPS+13*4);
1937 }
1938 buffer += i; buffersize -= i;
1939 }
1940
1941 INITFLAGS(GLONASS)
1942 CHECKFLAGSNEW(GLONASS, C1, C1C)
1943 CHECKFLAGSNEW(GLONASS, L1C, L1C)
1944 CHECKFLAGSNEW(GLONASS, D1C, D1C)
1945 CHECKFLAGSNEW(GLONASS, S1C, S1C)
1946 CHECKFLAGSNEW(GLONASS, P1, C1P)
1947 CHECKFLAGSNEW(GLONASS, L1P, L1P)
1948 CHECKFLAGSNEW(GLONASS, D1P, D1P)
1949 CHECKFLAGSNEW(GLONASS, S1P, S1P)
1950 CHECKFLAGSNEW(GLONASS, P2, C2P)
1951 CHECKFLAGSNEW(GLONASS, L2P, L2P)
1952 CHECKFLAGSNEW(GLONASS, D2P, D2P)
1953 CHECKFLAGSNEW(GLONASS, S2P, S2P)
1954 CHECKFLAGSNEW(GLONASS, C2, C2C)
1955 CHECKFLAGSNEW(GLONASS, L2C, L2C)
1956 CHECKFLAGSNEW(GLONASS, D2C, D2C)
1957 CHECKFLAGSNEW(GLONASS, S2C, S2C)
1958
1959 if(modified)
1960 {
1961 if(hdata)
1962 hdata->data.named.typesofobsR = buffer;
1963 i = 1+snprintf(buffer, buffersize,
1964 "R %3d%-52.52s SYS / # / OBS TYPES",
1965 Parser->info[RTCM3_MSM_GLONASS].numtypes, Parser->fieldbufferGLONASS);
1966 if(Parser->info[RTCM3_MSM_GLONASS].numtypes>13)
1967 {
1968 i += snprintf(buffer+i-1, buffersize,
1969 "\n %-52.52s SYS / # / OBS TYPES", Parser->fieldbufferGLONASS+13*4);
1970 }
1971 buffer += i; buffersize -= i;
1972 }
1973
1974 INITFLAGS(GALGIO)
1975 CHECKFLAGSNEW(GALILEO, C1, C1)
1976 CHECKFLAGSNEW(GALILEO, L1C, L1)
1977 CHECKFLAGSNEW(GALILEO, D1C, D1)
1978 CHECKFLAGSNEW(GALILEO, S1C, S1)
1979 CHECKFLAGSNEW(GALILEO, C6, C6)
1980 CHECKFLAGSNEW(GALILEO, L6, L6)
1981 CHECKFLAGSNEW(GALILEO, D6, D6)
1982 CHECKFLAGSNEW(GALILEO, S6, S6)
1983 CHECKFLAGSNEW(GALILEO, C5, C5)
1984 CHECKFLAGSNEW(GALILEO, L5, L5)
1985 CHECKFLAGSNEW(GALILEO, D5, D5)
1986 CHECKFLAGSNEW(GALILEO, S5, S5)
1987 CHECKFLAGSNEW(GALILEO, C5B, C7)
1988 CHECKFLAGSNEW(GALILEO, L5B, L7)
1989 CHECKFLAGSNEW(GALILEO, D5B, D7)
1990 CHECKFLAGSNEW(GALILEO, S5B, S7)
1991 CHECKFLAGSNEW(GALILEO, C5AB, C8)
1992 CHECKFLAGSNEW(GALILEO, L5AB, L8)
1993 CHECKFLAGSNEW(GALILEO, D5AB, D8)
1994 CHECKFLAGSNEW(GALILEO, S5AB, S8)
1995
1996 if(modified)
1997 {
1998 if(hdata)
1999 hdata->data.named.typesofobsE = buffer;
2000 i = 1+snprintf(buffer, buffersize,
2001 "E %3d%-52.52s SYS / # / OBS TYPES",
2002 Parser->info[RTCM3_MSM_GALILEO].numtypes, Parser->fieldbufferGALILEO);
2003 if(Parser->info[RTCM3_MSM_GALILEO].numtypes>13)
2004 {
2005 i += snprintf(buffer+i-1, buffersize,
2006 "\n %-52.52s SYS / # / OBS TYPES", Parser->fieldbufferGALILEO+13*4);
2007 }
2008 buffer += i; buffersize -= i;
2009 }
2010
2011 INITFLAGS(COMPASS)
2012 CHECKFLAGSNEW(COMPASS, CB1, C2I)
2013 CHECKFLAGSNEW(COMPASS, LB1, L2I)
2014 CHECKFLAGSNEW(COMPASS, DB1, D2I)
2015 CHECKFLAGSNEW(COMPASS, SB1, S2I)
2016 CHECKFLAGSNEW(COMPASS, CB2, C7I)
2017 CHECKFLAGSNEW(COMPASS, LB2, L7I)
2018 CHECKFLAGSNEW(COMPASS, DB2, D7I)
2019 CHECKFLAGSNEW(COMPASS, SB2, S7I)
2020 CHECKFLAGSNEW(COMPASS, CB3, C6I)
2021 CHECKFLAGSNEW(COMPASS, LB3, L6I)
2022 CHECKFLAGSNEW(COMPASS, DB3, D6I)
2023 CHECKFLAGSNEW(COMPASS, SB3, S6I)
2024
2025 if(modified)
2026 {
2027 if(hdata)
2028 hdata->data.named.typesofobsC = buffer;
2029 i = 1+snprintf(buffer, buffersize,
2030 "C %3d%-52.52s SYS / # / OBS TYPES",
2031 Parser->info[RTCM3_MSM_COMPASS].numtypes, Parser->fieldbufferCOMPASS);
2032 if(Parser->info[RTCM3_MSM_COMPASS].numtypes>13)
2033 {
2034 i += snprintf(buffer+i-1, buffersize,
2035 "\n %-52.52s SYS / # / OBS TYPES", Parser->fieldbufferCOMPASS+13*4);
2036 }
2037 buffer += i; buffersize -= i;
2038 }
2039
2040 INITFLAGS(QZSS)
2041
2042 CHECKFLAGSNEW(QZSS, C1, C1C)
2043 CHECKFLAGSNEW(QZSS, L1C, L1C)
2044 CHECKFLAGSNEW(QZSS, D1C, D1C)
2045 CHECKFLAGSNEW(QZSS, S1C, S1C)
2046
2047 CHECKFLAGSNEW(QZSS, CSAIF, C1Z)
2048 CHECKFLAGSNEW(QZSS, LSAIF, L1Z)
2049 CHECKFLAGSNEW(QZSS, DSAIF, D1Z)
2050 CHECKFLAGSNEW(QZSS, SSAIF, S1Z)
2051
2052 CHECKFLAGSNEW(QZSS, C1N, C1)
2053 CHECKFLAGSNEW(QZSS, L1N, L1)
2054 CHECKFLAGSNEW(QZSS, D1N, D1)
2055 CHECKFLAGSNEW(QZSS, S1N, S1)
2056
2057 CHECKFLAGSNEW(QZSS, C6, C6)
2058 CHECKFLAGSNEW(QZSS, L6, L6)
2059 CHECKFLAGSNEW(QZSS, D6, D6)
2060 CHECKFLAGSNEW(QZSS, S6, S6)
2061
2062 CHECKFLAGSNEW(QZSS, C2, C2)
2063 CHECKFLAGSNEW(QZSS, L2C, L2)
2064 CHECKFLAGSNEW(QZSS, D2C, D2)
2065 CHECKFLAGSNEW(QZSS, S2C, S2)
2066
2067 CHECKFLAGSNEW(QZSS, C5, C5)
2068 CHECKFLAGSNEW(QZSS, L5, L5)
2069 CHECKFLAGSNEW(QZSS, D5, D5)
2070 CHECKFLAGSNEW(QZSS, S5, S5)
2071
2072 if(modified)
2073 {
2074 if(hdata)
2075 hdata->data.named.typesofobsJ = buffer;
2076 i = 1+snprintf(buffer, buffersize,
2077 "J %3d%-52.52s SYS / # / OBS TYPES",
2078 Parser->info[RTCM3_MSM_QZSS].numtypes, Parser->fieldbufferQZSS);
2079 if(Parser->info[RTCM3_MSM_QZSS].numtypes>13)
2080 {
2081 i += snprintf(buffer+i-1, buffersize,
2082 "\n %-52.52s SYS / # / OBS TYPES", Parser->fieldbufferQZSS+13*4);
2083 }
2084 buffer += i; buffersize -= i;
2085 }
2086 }
2087 else
2088 {
2089#define CHECKFLAGS(a, b) \
2090 if(flags & GNSSDF_##a##DATA) \
2091 { \
2092 if(Parser->datafields[RINEXENTRY_##b##DATA]) \
2093 { \
2094 Parser->info[RTCM3_MSM_GPS].flags[Parser->datafields[\
2095 RINEXENTRY_##b##DATA]-1] = GNSSDF_##a##DATA; \
2096 Parser->info[RTCM3_MSM_GPS].pos[Parser->datafields[\
2097 RINEXENTRY_##b##DATA]-1] = GNSSENTRY_##a##DATA; \
2098 } \
2099 else \
2100 { \
2101 Parser->flags[Parser->info[RTCM3_MSM_GPS].numtypes] = GNSSDF_##a##DATA; \
2102 Parser->pos[Parser->info[RTCM3_MSM_GPS].numtypes] = GNSSENTRY_##a##DATA; \
2103 Parser->datafields[RINEXENTRY_##b##DATA] = \
2104 ++Parser->info[RTCM3_MSM_GPS].numtypes; \
2105 snprintf(Parser->fieldbuffer+6*Parser->numdatafields, \
2106 sizeof(Parser->fieldbuffer)-6*Parser->numdatafields, " "#b); \
2107 ++Parser->numdatafields; \
2108 ++modified; \
2109 } \
2110 }
2111
2112 int flags = Parser->startflags;
2113 for(i = 0; i < Parser->Data.numsats; ++i)
2114 flags |= Parser->Data.dataflags[i];
2115
2116 CHECKFLAGS(C1,C1)
2117 CHECKFLAGS(C2,C2)
2118 CHECKFLAGS(P1,P1)
2119 CHECKFLAGS(P2,P2)
2120 CHECKFLAGS(L1C,L1)
2121 CHECKFLAGS(L1P,L1)
2122 CHECKFLAGS(L2C,L2)
2123 CHECKFLAGS(L2P,L2)
2124 CHECKFLAGS(D1C,D1)
2125 CHECKFLAGS(D1P,D1)
2126 CHECKFLAGS(D2C,D2)
2127 CHECKFLAGS(D2P,D2)
2128 CHECKFLAGS(S1C,S1)
2129 CHECKFLAGS(S1P,S1)
2130 CHECKFLAGS(S2C,S2)
2131 CHECKFLAGS(S2P,S2)
2132 CHECKFLAGS(C5,C5)
2133 CHECKFLAGS(L5,L5)
2134 CHECKFLAGS(D5,D5)
2135 CHECKFLAGS(S5,S5)
2136 CHECKFLAGS(C5AB,C8)
2137 CHECKFLAGS(L5AB,L8)
2138 CHECKFLAGS(D5AB,D8)
2139 CHECKFLAGS(S5AB,S8)
2140 CHECKFLAGS(C5B,C7)
2141 CHECKFLAGS(L5B,L7)
2142 CHECKFLAGS(D5B,D7)
2143 CHECKFLAGS(S5B,S7)
2144 CHECKFLAGS(C6,C6)
2145 CHECKFLAGS(L6,L6)
2146 CHECKFLAGS(D6,D6)
2147 CHECKFLAGS(S6,S6)
2148 /* Skip C1N and SAIF for RINEX2! */
2149
2150 if(hdata)
2151 hdata->data.named.typesofobs = buffer;
2152 if(modified)
2153 {
2154 i = 1+snprintf(buffer, buffersize,
2155 "%6d%-54.54s# / TYPES OF OBSERV", Parser->info[RTCM3_MSM_GPS].numtypes,
2156 Parser->fieldbuffer);
2157 if(Parser->info[RTCM3_MSM_GPS].numtypes>9)
2158 {
2159 i += snprintf(buffer+i-1, buffersize,
2160 "\n %-54.54s# / TYPES OF OBSERV", Parser->fieldbuffer+9*6);
2161 }
2162 if(Parser->info[RTCM3_MSM_GPS].numtypes>18)
2163 {
2164 i += snprintf(buffer+i-1, buffersize,
2165 "\n %-54.54s# / TYPES OF OBSERV", Parser->fieldbuffer+18*6);
2166 }
2167 buffer += i; buffersize -= i;
2168 }
2169 }
2170 return buffersizeold - buffersize;
2171}
2172
2173void HandleHeader(struct RTCM3ParserData *Parser)
2174{
2175#ifdef NO_RTCM3_MAIN
2176 int i;
2177 if(Parser->allflags == 0)
2178 Parser->allflags = ~0;
2179 if(Parser->rinex3)
2180 {
2181 int flags = Parser->allflags;
2182 CHECKFLAGSNEW(GPS, C1, C1C)
2183 CHECKFLAGSNEW(GPS, L1C, L1C)
2184 CHECKFLAGSNEW(GPS, D1C, D1C)
2185 CHECKFLAGSNEW(GPS, S1C, S1C)
2186 CHECKFLAGSNEW(GPS, P1, C1P)
2187 CHECKFLAGSNEW(GPS, L1P, L1P)
2188 CHECKFLAGSNEW(GPS, D1P, D1P)
2189 CHECKFLAGSNEW(GPS, S1P, S1P)
2190 CHECKFLAGSNEW(GPS, P2, C2P)
2191 CHECKFLAGSNEW(GPS, L2P, L2P)
2192 CHECKFLAGSNEW(GPS, D2P, D2P)
2193 CHECKFLAGSNEW(GPS, S2P, S2P)
2194 CHECKFLAGSNEW(GPS, C2, C2X)
2195 CHECKFLAGSNEW(GPS, L2C, L2X)
2196 CHECKFLAGSNEW(GPS, D2C, D2X)
2197 CHECKFLAGSNEW(GPS, S2C, S2X)
2198 CHECKFLAGSNEW(GLONASS, C1, C1C)
2199 CHECKFLAGSNEW(GLONASS, L1C, L1C)
2200 CHECKFLAGSNEW(GLONASS, D1C, D1C)
2201 CHECKFLAGSNEW(GLONASS, S1C, S1C)
2202 CHECKFLAGSNEW(GLONASS, P1, C1P)
2203 CHECKFLAGSNEW(GLONASS, L1P, L1P)
2204 CHECKFLAGSNEW(GLONASS, D1P, D1P)
2205 CHECKFLAGSNEW(GLONASS, S1P, S1P)
2206 CHECKFLAGSNEW(GLONASS, P2, C2P)
2207 CHECKFLAGSNEW(GLONASS, L2P, L2P)
2208 CHECKFLAGSNEW(GLONASS, D2P, D2P)
2209 CHECKFLAGSNEW(GLONASS, S2P, S2P)
2210 CHECKFLAGSNEW(GLONASS, C2, C2C)
2211 CHECKFLAGSNEW(GLONASS, L2C, L2C)
2212 CHECKFLAGSNEW(GLONASS, D2C, D2C)
2213 CHECKFLAGSNEW(GLONASS, S2C, S2C)
2214 }
2215 else
2216 {
2217 int flags = Parser->allflags;
2218 int data[RINEXENTRY_NUMBER];
2219 for(i = 0; i < RINEXENTRY_NUMBER; ++i) data[i] = 0;
2220
2221 CHECKFLAGS(C1,C1)
2222 CHECKFLAGS(C2,C2)
2223 CHECKFLAGS(P1,P1)
2224 CHECKFLAGS(P2,P2)
2225 CHECKFLAGS(L1C,L1)
2226 CHECKFLAGS(L1P,L1)
2227 CHECKFLAGS(L2C,L2)
2228 CHECKFLAGS(L2P,L2)
2229 CHECKFLAGS(D1C,D1)
2230 CHECKFLAGS(D1P,D1)
2231 CHECKFLAGS(D2C,D2)
2232 CHECKFLAGS(D2P,D2)
2233 CHECKFLAGS(S1C,S1)
2234 CHECKFLAGS(S1P,S1)
2235 CHECKFLAGS(S2C,S2)
2236 CHECKFLAGS(S2P,S2)
2237 CHECKFLAGS(C5,C5)
2238 CHECKFLAGS(L5,L5)
2239 CHECKFLAGS(D5,D5)
2240 CHECKFLAGS(S5,S5)
2241 CHECKFLAGS(C5AB,C8)
2242 CHECKFLAGS(L5AB,L8)
2243 CHECKFLAGS(D5AB,D8)
2244 CHECKFLAGS(S5AB,S8)
2245 CHECKFLAGS(C5B,C7)
2246 CHECKFLAGS(L5B,L7)
2247 CHECKFLAGS(D5B,D7)
2248 CHECKFLAGS(S5B,S7)
2249 CHECKFLAGS(C6,C6)
2250 CHECKFLAGS(L6,L6)
2251 CHECKFLAGS(D6,D6)
2252 CHECKFLAGS(S6,S6)
2253 }
2254#else /* NO_RTCM3_MAIN */
2255 struct HeaderData hdata;
2256 char thebuffer[MAXHEADERBUFFERSIZE];
2257 char *buffer = thebuffer;
2258 size_t buffersize = sizeof(thebuffer);
2259 int i;
2260
2261 memset(&hdata, 0, sizeof(hdata));
2262
2263 hdata.data.named.version = buffer;
2264 i = 1+snprintf(buffer, buffersize,
2265 "%9.2f OBSERVATION DATA M (Mixed)"
2266 " RINEX VERSION / TYPE", Parser->rinex3 ? 3.0 : 2.11);
2267 buffer += i; buffersize -= i;
2268
2269 {
2270 const char *str;
2271 hdata.data.named.pgm = buffer;
2272 i = HandleRunBy(buffer, buffersize, &str, Parser->rinex3);
2273 buffer += i; buffersize -= i;
2274 hdata.data.named.observer = buffer;
2275 i = 1+snprintf(buffer, buffersize,
2276 "%-20.20s "
2277 "OBSERVER / AGENCY", str);
2278 buffer += i; buffersize -= i;
2279 }
2280
2281 hdata.data.named.marker =
2282 "RTCM3TORINEX "
2283 "MARKER NAME";
2284
2285 hdata.data.named.markertype = !Parser->rinex3 ? 0 :
2286 "GEODETIC "
2287 "MARKER TYPE";
2288
2289 hdata.data.named.receiver =
2290 " "
2291 "REC # / TYPE / VERS";
2292
2293 hdata.data.named.antenna =
2294 " "
2295 "ANT # / TYPE";
2296
2297 hdata.data.named.position =
2298 " .0000 .0000 .0000 "
2299 "APPROX POSITION XYZ";
2300
2301 hdata.data.named.antennaposition =
2302 " .0000 .0000 .0000 "
2303 "ANTENNA: DELTA H/E/N";
2304
2305 hdata.data.named.wavelength = Parser->rinex3 ? 0 :
2306 " 1 1 "
2307 "WAVELENGTH FACT L1/2";
2308
2309 hdata.numheaders = 18;
2310
2311 i = HandleObsHeader(Parser, buffer, buffersize, &hdata);
2312 buffer += i; buffersize -= i;
2313
2314 {
2315 struct converttimeinfo cti;
2316 converttime(&cti, Parser->Data.week,
2317 (int)floor(Parser->Data.timeofweek/1000.0));
2318 hdata.data.named.timeoffirstobs = buffer;
2319 i = 1+snprintf(buffer, buffersize,
2320 " %4d %2d %2d %2d %2d %10.7f GPS "
2321 "TIME OF FIRST OBS", cti.year, cti.month, cti.day, cti.hour,
2322 cti.minute, cti.second + fmod(Parser->Data.timeofweek/1000.0,1.0));
2323
2324 buffer += i; buffersize -= i;
2325 }
2326
2327 if(Parser->headerfile)
2328 {
2329 FILE *fh;
2330 if((fh = fopen(Parser->headerfile, "r")))
2331 {
2332 size_t siz;
2333 char *lastblockstart;
2334 if((siz = fread(buffer, 1, buffersize-1, fh)) > 0)
2335 {
2336 buffer[siz] = '\n';
2337 if(siz == buffersize)
2338 {
2339 RTCM3Error("Header file is too large. Only %d bytes read.",
2340 (int)siz);
2341 }
2342 /* scan the file line by line and enter the entries in the list */
2343 /* warn for "# / TYPES OF OBSERV" and "TIME OF FIRST OBS" */
2344 /* overwrites entries, except for comments */
2345 lastblockstart = buffer;
2346 for(i = 0; i < (int)siz; ++i)
2347 {
2348 if(buffer[i] == '\n')
2349 { /* we found a line */
2350 char *end;
2351 while(buffer[i+1] == '\r')
2352 ++i; /* skip \r in case there are any */
2353 end = buffer+i;
2354 while(*end == '\t' || *end == ' ' || *end == '\r' || *end == '\n')
2355 *(end--) = 0;
2356 if(end-lastblockstart < 60+5) /* short line */
2357 RTCM3Error("Short Header line '%s' ignored.\n", lastblockstart);
2358 else
2359 {
2360 int pos;
2361 if(!strcmp("COMMENT", lastblockstart+60))
2362 pos = hdata.numheaders;
2363 else
2364 {
2365 for(pos = 0; pos < hdata.numheaders; ++pos)
2366 {
2367 if(!strcmp(hdata.data.unnamed[pos]+60, lastblockstart+60))
2368 break;
2369 }
2370 if(!strcmp("# / TYPES OF OBSERV", lastblockstart+60)
2371 || !strcmp("TIME OF FIRST OBS", lastblockstart+60))
2372 {
2373 RTCM3Error("Overwriting header '%s' is dangerous.\n",
2374 lastblockstart+60);
2375 }
2376 }
2377 if(pos >= MAXHEADERLINES)
2378 {
2379 RTCM3Error("Maximum number of header lines of %d reached.\n",
2380 MAXHEADERLINES);
2381 }
2382 else if(!strcmp("END OF HEADER", lastblockstart+60))
2383 {
2384 RTCM3Error("End of header ignored.\n");
2385 }
2386 else
2387 {
2388 hdata.data.unnamed[pos] = lastblockstart;
2389 if(pos == hdata.numheaders)
2390 ++hdata.numheaders;
2391 }
2392 }
2393 lastblockstart = buffer+i+1;
2394 }
2395 }
2396 }
2397 else
2398 {
2399 RTCM3Error("Could not read data from headerfile '%s'.\n",
2400 Parser->headerfile);
2401 }
2402 fclose(fh);
2403 }
2404 else
2405 {
2406 RTCM3Error("Could not open header datafile '%s'.\n",
2407 Parser->headerfile);
2408 }
2409 }
2410
2411 for(i = 0; i < hdata.numheaders; ++i)
2412 {
2413 if(hdata.data.unnamed[i] && hdata.data.unnamed[i][0])
2414 RTCM3Text("%s\n", hdata.data.unnamed[i]);
2415 }
2416 RTCM3Text(" "
2417 "END OF HEADER\n");
2418#endif
2419}
2420
2421static void ConvLine(FILE *file, const char *fmt, ...)
2422{
2423 char buffer[100], *b;
2424 va_list v;
2425 va_start(v, fmt);
2426 vsnprintf(buffer, sizeof(buffer), fmt, v);
2427 for(b = buffer; *b; ++b)
2428 {
2429 if(*b == 'e') *b = 'D';
2430 }
2431 fprintf(file, "%s", buffer);
2432 va_end(v);
2433}
2434
2435void HandleByte(struct RTCM3ParserData *Parser, unsigned int byte)
2436{
2437 Parser->Message[Parser->MessageSize++] = byte;
2438 if(Parser->MessageSize >= Parser->NeedBytes)
2439 {
2440 int r;
2441 while((r = RTCM3Parser(Parser)))
2442 {
2443 if(r == 1020 || r == 1019)
2444 {
2445 FILE *file = 0;
2446
2447 if(Parser->rinex3 && !(file = Parser->gpsfile))
2448 {
2449 const char *n = Parser->gpsephemeris ? Parser->gpsephemeris : Parser->glonassephemeris;
2450 if(n)
2451 {
2452 if(!(Parser->gpsfile = fopen(n, "w")))
2453 {
2454 RTCM3Error("Could not open ephemeris output file.\n");
2455 }
2456 else
2457 {
2458 char buffer[100];
2459 fprintf(Parser->gpsfile,
2460 "%9.2f%11sN: GNSS NAV DATA M: Mixed%12sRINEX VERSION / TYPE\n", 3.0, "", "");
2461 HandleRunBy(buffer, sizeof(buffer), 0, Parser->rinex3);
2462 fprintf(Parser->gpsfile, "%s\n%60sEND OF HEADER\n", buffer, "");
2463 }
2464 Parser->gpsephemeris = 0;
2465 Parser->glonassephemeris = 0;
2466 file = Parser->gpsfile;
2467 }
2468 }
2469 else
2470 {
2471 if(r == 1020)
2472 {
2473 if(Parser->glonassephemeris)
2474 {
2475 if(!(Parser->glonassfile = fopen(Parser->glonassephemeris, "w")))
2476 {
2477 RTCM3Error("Could not open GLONASS ephemeris output file.\n");
2478 }
2479 else
2480 {
2481 char buffer[100];
2482 fprintf(Parser->glonassfile,
2483 "%9.2f%11sG: GLONASS NAV DATA%21sRINEX VERSION / TYPE\n", 2.1, "", "");
2484 HandleRunBy(buffer, sizeof(buffer), 0, Parser->rinex3);
2485 fprintf(Parser->glonassfile, "%s\n%60sEND OF HEADER\n", buffer, "");
2486 }
2487 Parser->glonassephemeris = 0;
2488 }
2489 file = Parser->glonassfile;
2490 }
2491 else if(r == 1019)
2492 {
2493 if(Parser->gpsephemeris)
2494 {
2495 if(!(Parser->gpsfile = fopen(Parser->gpsephemeris, "w")))
2496 {
2497 RTCM3Error("Could not open GPS ephemeris output file.\n");
2498 }
2499 else
2500 {
2501 char buffer[100];
2502 fprintf(Parser->gpsfile,
2503 "%9.2f%11sN: GPS NAV DATA%25sRINEX VERSION / TYPE\n", 2.1, "", "");
2504 HandleRunBy(buffer, sizeof(buffer), 0, Parser->rinex3);
2505 fprintf(Parser->gpsfile, "%s\n%60sEND OF HEADER\n", buffer, "");
2506 }
2507 Parser->gpsephemeris = 0;
2508 }
2509 file = Parser->gpsfile;
2510 }
2511 }
2512 if(file)
2513 {
2514 if(r == 1020)
2515 {
2516 struct glonassephemeris *e = &Parser->ephemerisGLONASS;
2517 int w = e->GPSWeek, tow = e->GPSTOW, i;
2518 struct converttimeinfo cti;
2519
2520 updatetime(&w, &tow, e->tb*1000, 1); /* Moscow - > UTC */
2521 converttime(&cti, w, tow);
2522
2523 i = e->tk-3*60*60; if(i < 0) i += 86400;
2524
2525 if(Parser->rinex3)
2526 ConvLine(file, "R%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e\n",
2527 e->almanac_number, cti.year, cti.month, cti.day, cti.hour, cti.minute,
2528 cti.second, -e->tau, e->gamma, (double) i);
2529 else
2530 ConvLine(file, "%02d %02d %02d %02d %02d %02d%5.1f%19.12e%19.12e%19.12e\n",
2531 e->almanac_number, cti.year%100, cti.month, cti.day, cti.hour, cti.minute,
2532 (double) cti.second, -e->tau, e->gamma, (double) i);
2533 ConvLine(file, " %19.12e%19.12e%19.12e%19.12e\n", e->x_pos,
2534 e->x_velocity, e->x_acceleration, (e->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
2535 ConvLine(file, " %19.12e%19.12e%19.12e%19.12e\n", e->y_pos,
2536 e->y_velocity, e->y_acceleration, (double) e->frequency_number);
2537 ConvLine(file, " %19.12e%19.12e%19.12e%19.12e\n", e->z_pos,
2538 e->z_velocity, e->z_acceleration, (double) e->E);
2539 }
2540 else /* if(r == 1019) */
2541 {
2542 struct gpsephemeris *e = &Parser->ephemerisGPS;
2543 double d; /* temporary variable */
2544 unsigned long int i; /* temporary variable */
2545 struct converttimeinfo cti;
2546 converttime(&cti, e->GPSweek, e->TOC);
2547
2548 if(Parser->rinex3)
2549 ConvLine(file, "G%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e\n",
2550 e->satellite, cti.year, cti.month, cti.day, cti.hour,
2551 cti.minute, cti.second, e->clock_bias, e->clock_drift,
2552 e->clock_driftrate);
2553 else
2554 ConvLine(file, "%02d %02d %02d %02d %02d %02d%05.1f%19.12e%19.12e%19.12e\n",
2555 e->satellite, cti.year%100, cti.month, cti.day, cti.hour,
2556 cti.minute, (double) cti.second, e->clock_bias, e->clock_drift,
2557 e->clock_driftrate);
2558 ConvLine(file, " %19.12e%19.12e%19.12e%19.12e\n", (double)e->IODE,
2559 e->Crs, e->Delta_n, e->M0);
2560 ConvLine(file, " %19.12e%19.12e%19.12e%19.12e\n", e->Cuc,
2561 e->e, e->Cus, e->sqrt_A);
2562 ConvLine(file, " %19.12e%19.12e%19.12e%19.12e\n",
2563 (double) e->TOE, e->Cic, e->OMEGA0, e->Cis);
2564 ConvLine(file, " %19.12e%19.12e%19.12e%19.12e\n", e->i0,
2565 e->Crc, e->omega, e->OMEGADOT);
2566 d = 0;
2567 i = e->flags;
2568 if(i & GPSEPHF_L2CACODE)
2569 d += 2.0;
2570 if(i & GPSEPHF_L2PCODE)
2571 d += 1.0;
2572 ConvLine(file, " %19.12e%19.12e%19.12e%19.12e\n", e->IDOT, d,
2573 (double) e->GPSweek, i & GPSEPHF_L2PCODEDATA ? 1.0 : 0.0);
2574 if(e->URAindex <= 6) /* URA index */
2575 d = ceil(10.0*pow(2.0, 1.0+((double)e->URAindex)/2.0))/10.0;
2576 else
2577 d = ceil(10.0*pow(2.0, ((double)e->URAindex)/2.0))/10.0;
2578 /* 15 indicates not to use satellite. We can't handle this special
2579 case, so we create a high "non"-accuracy value. */
2580 ConvLine(file, " %19.12e%19.12e%19.12e%19.12e\n", d,
2581 ((double) e->SVhealth), e->TGD, ((double) e->IODC));
2582
2583 ConvLine(file, " %19.12e%19.12e\n", ((double)e->TOW),
2584 i & GPSEPHF_6HOURSFIT ? 6.0 : 4.0);
2585 /* TOW */
2586 }
2587 }
2588 }
2589 else if (r == 1 || r == 2)
2590 {
2591 int i, j, o, nh=0;
2592 char newheader[512];
2593 struct converttimeinfo cti;
2594
2595 /* skip first epochs to detect correct data types */
2596 if(Parser->init < (Parser->changeobs ? 1 : NUMSTARTSKIP))
2597 {
2598 ++Parser->init;
2599
2600 if(Parser->init == (Parser->changeobs ? 1 : NUMSTARTSKIP))
2601 HandleHeader(Parser);
2602 else
2603 {
2604 for(i = 0; i < Parser->Data.numsats; ++i)
2605 Parser->startflags |= Parser->Data.dataflags[i];
2606 continue;
2607 }
2608 }
2609 if(r == 2 && !Parser->validwarning)
2610 {
2611 RTCM3Text("No valid RINEX! All values are modulo 299792.458!"
2612 " COMMENT\n");
2613 Parser->validwarning = 1;
2614 }
2615
2616 converttime(&cti, Parser->Data.week,
2617 (int)floor(Parser->Data.timeofweek/1000.0));
2618 newheader[0] = 0;
2619 if(Parser->changeobs)
2620 {
2621 nh = HandleObsHeader(Parser, newheader, sizeof(newheader), 0);
2622 }
2623 if(Parser->rinex3)
2624 {
2625 RTCM3Text("> %04d %02d %02d %02d %02d%11.7f %d%3d\n",
2626 cti.year, cti.month, cti.day, cti.hour, cti.minute, cti.second
2627 + fmod(Parser->Data.timeofweek/1000.0,1.0), nh ? 4 : 0,
2628 Parser->Data.numsats);
2629 if(nh)
2630 {
2631 RTCM3Text("%s\n "
2632 " END OF HEADER\n", newheader);
2633 }
2634 for(i = 0; i < Parser->Data.numsats; ++i)
2635 {
2636 int sys[RTCM3_MSM_NUMSYS] = {0,0,0,0,0,0};
2637 if(Parser->Data.satellites[i] <= PRN_GPS_END)
2638 {
2639 RTCM3Text("G%02d", Parser->Data.satellites[i]);
2640 sys[RTCM3_MSM_GPS] = 1;
2641 }
2642 else if(Parser->Data.satellites[i] >= PRN_GLONASS_START
2643 && Parser->Data.satellites[i] <= PRN_GLONASS_END)
2644 {
2645 RTCM3Text("R%02d", Parser->Data.satellites[i] - (PRN_GLONASS_START-1));
2646 sys[RTCM3_MSM_GLONASS] = 1;
2647 }
2648 else if(Parser->Data.satellites[i] >= PRN_GALILEO_START
2649 && Parser->Data.satellites[i] <= PRN_GALILEO_END)
2650 {
2651 RTCM3Text("E%02d", Parser->Data.satellites[i] - (PRN_GALILEO_START-1));
2652 sys[RTCM3_MSM_GALILEO] = 1;
2653 }
2654 else if(Parser->Data.satellites[i] >= PRN_GIOVE_START
2655 && Parser->Data.satellites[i] <= PRN_GIOVE_END)
2656 {
2657 RTCM3Text("E%02d", Parser->Data.satellites[i] - (PRN_GIOVE_START-PRN_GIOVE_OFFSET));
2658 sys[RTCM3_MSM_GALILEO] = 1;
2659 }
2660 else if(Parser->Data.satellites[i] >= PRN_QZSS_START
2661 && Parser->Data.satellites[i] <= PRN_QZSS_END)
2662 {
2663 RTCM3Text("J%02d", Parser->Data.satellites[i] - (PRN_QZSS_START-1));
2664 sys[RTCM3_MSM_QZSS] = 1;
2665 }
2666 else if(Parser->Data.satellites[i] >= PRN_COMPASS_START
2667 && Parser->Data.satellites[i] <= PRN_COMPASS_END)
2668 {
2669 RTCM3Text("C%02d", Parser->Data.satellites[i] - (PRN_COMPASS_START-1));
2670 sys[RTCM3_MSM_COMPASS] = 1;
2671 }
2672 else if(Parser->Data.satellites[i] >= PRN_SBAS_START
2673 && Parser->Data.satellites[i] <= PRN_SBAS_END)
2674 {
2675 RTCM3Text("S%02d", Parser->Data.satellites[i] - PRN_SBAS_START+20);
2676 sys[RTCM3_MSM_SBAS] = 1;
2677 }
2678 else
2679 {
2680 RTCM3Text("%3d", Parser->Data.satellites[i]);
2681 }
2682
2683 if(sys[RTCM3_MSM_GLONASS])
2684 {
2685 for(j = 0; j < Parser->info[RTCM3_MSM_GLONASS].numtypes; ++j)
2686 {
2687 long long df = Parser->info[RTCM3_MSM_GLONASS].flags[j];
2688 int pos = Parser->info[RTCM3_MSM_GLONASS].pos[j];
2689 if((Parser->Data.dataflags[i] & df)
2690 && !isnan(Parser->Data.measdata[i][pos])
2691 && !isinf(Parser->Data.measdata[i][pos])
2692 && (Parser->Data.codetype[i][pos]
2693 && Parser->info[RTCM3_MSM_GLONASS].type[pos]
2694 && Parser->info[RTCM3_MSM_GLONASS].type[pos]
2695 == Parser->Data.codetype[i][pos][1]))
2696 {
2697 char lli = ' ';
2698 char snr = ' ';
2699 if(df & (GNSSDF_L1CDATA|GNSSDF_L1PDATA))
2700 {
2701 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL1)
2702 lli = '1';
2703 snr = '0'+Parser->Data.snrL1[i];
2704 }
2705 if(df & (GNSSDF_L2CDATA|GNSSDF_L2PDATA))
2706 {
2707 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL2)
2708 lli = '1';
2709 snr = '0'+Parser->Data.snrL2[i];
2710 }
2711 RTCM3Text("%14.3f%c%c",
2712 Parser->Data.measdata[i][pos],lli,snr);
2713 }
2714 else
2715 { /* no or illegal data */
2716 RTCM3Text(" ");
2717 }
2718 }
2719 }
2720 else if(sys[RTCM3_MSM_GALILEO])
2721 {
2722 for(j = 0; j < Parser->info[RTCM3_MSM_GALILEO].numtypes; ++j)
2723 {
2724 long long df = Parser->info[RTCM3_MSM_GALILEO].flags[j];
2725 int pos = Parser->info[RTCM3_MSM_GALILEO].pos[j];
2726 if((Parser->Data.dataflags[i] & df)
2727 && !isnan(Parser->Data.measdata[i][pos])
2728 && !isinf(Parser->Data.measdata[i][pos])
2729 && (Parser->Data.codetype[i][pos]
2730 && Parser->info[RTCM3_MSM_GALILEO].type[pos]
2731 && Parser->info[RTCM3_MSM_GALILEO].type[pos]
2732 == Parser->Data.codetype[i][pos][1]))
2733 {
2734 char lli = ' ';
2735 char snr = ' ';
2736 if(df & (GNSSDF_L1CDATA|GNSSDF_L1PDATA))
2737 {
2738 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL1)
2739 lli = '1';
2740 snr = '0'+Parser->Data.snrL1[i];
2741 }
2742 if(df & GNSSDF_L6DATA)
2743 {
2744 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSE6)
2745 lli = '1';
2746 snr = ' ';
2747 }
2748 if(df & GNSSDF_L5DATA)
2749 {
2750 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL5)
2751 lli = '1';
2752 snr = ' ';
2753 }
2754 if(df & GNSSDF_L5BDATA)
2755 {
2756 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSE5B)
2757 lli = '1';
2758 snr = ' ';
2759 }
2760 if(df & GNSSDF_L5ABDATA)
2761 {
2762 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSE5AB)
2763 lli = '1';
2764 snr = ' ';
2765 }
2766 RTCM3Text("%14.3f%c%c",
2767 Parser->Data.measdata[i][pos],lli,snr);
2768 }
2769 else
2770 { /* no or illegal data */
2771 RTCM3Text(" ");
2772 }
2773 }
2774 }
2775 else if(sys[RTCM3_MSM_COMPASS])
2776 {
2777 for(j = 0; j < Parser->info[RTCM3_MSM_COMPASS].numtypes; ++j)
2778 {
2779 long long df = Parser->info[RTCM3_MSM_COMPASS].flags[j];
2780 int pos = Parser->info[RTCM3_MSM_COMPASS].pos[j];
2781 if((Parser->Data.dataflags[i] & df)
2782 && !isnan(Parser->Data.measdata[i][pos])
2783 && !isinf(Parser->Data.measdata[i][pos])
2784 && (Parser->Data.codetype[i][pos]
2785 && Parser->info[RTCM3_MSM_COMPASS].type[pos]
2786 && Parser->info[RTCM3_MSM_COMPASS].type[pos]
2787 == Parser->Data.codetype[i][pos][1]))
2788 {
2789 char lli = ' ';
2790 char snr = ' ';
2791 if(df & GNSSDF_LB1DATA)
2792 {
2793 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSB1)
2794 lli = '1';
2795 }
2796 if(df & GNSSDF_LB2DATA)
2797 {
2798 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSB2)
2799 lli = '1';
2800 }
2801 if(df & GNSSDF_LB3DATA)
2802 {
2803 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSB3)
2804 lli = '1';
2805 }
2806 RTCM3Text("%14.3f%c%c",
2807 Parser->Data.measdata[i][pos],lli,snr);
2808 }
2809 else
2810 { /* no or illegal data */
2811 RTCM3Text(" ");
2812 }
2813 }
2814 }
2815 else if(sys[RTCM3_MSM_QZSS])
2816 {
2817 for(j = 0; j < Parser->info[RTCM3_MSM_QZSS].numtypes; ++j)
2818 {
2819 long long df = Parser->info[RTCM3_MSM_QZSS].flags[j];
2820 int pos = Parser->info[RTCM3_MSM_QZSS].pos[j];
2821 if((Parser->Data.dataflags[i] & df)
2822 && !isnan(Parser->Data.measdata[i][pos])
2823 && !isinf(Parser->Data.measdata[i][pos])
2824 && (Parser->Data.codetype[i][pos]
2825 && Parser->info[RTCM3_MSM_QZSS].type[pos]
2826 && Parser->info[RTCM3_MSM_QZSS].type[pos]
2827 == Parser->Data.codetype[i][pos][1]))
2828 {
2829 char lli = ' ';
2830 char snr = ' ';
2831 if(df & GNSSDF_L1CDATA)
2832 {
2833 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL1)
2834 lli = '1';
2835 snr = '0'+Parser->Data.snrL1[i];
2836 }
2837 if(df & (GNSSDF_L2CDATA|GNSSDF_L2PDATA))
2838 {
2839 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL2)
2840 lli = '1';
2841 snr = '0'+Parser->Data.snrL2[i];
2842 }
2843 if(df & GNSSDF_L5DATA)
2844 {
2845 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL5)
2846 lli = '1';
2847 snr = ' ';
2848 }
2849 RTCM3Text("%14.3f%c%c",
2850 Parser->Data.measdata[i][pos],lli,snr);
2851 }
2852 else
2853 { /* no or illegal data */
2854 RTCM3Text(" ");
2855 }
2856 }
2857 }
2858 else if(sys[RTCM3_MSM_SBAS])
2859 {
2860 for(j = 0; j < Parser->info[RTCM3_MSM_SBAS].numtypes; ++j)
2861 {
2862 long long df = Parser->info[RTCM3_MSM_SBAS].flags[j];
2863 int pos = Parser->info[RTCM3_MSM_SBAS].pos[j];
2864 if((Parser->Data.dataflags[i] & df)
2865 && !isnan(Parser->Data.measdata[i][pos])
2866 && !isinf(Parser->Data.measdata[i][pos])
2867 && (Parser->Data.codetype[i][pos]
2868 && Parser->info[RTCM3_MSM_SBAS].type[pos]
2869 && Parser->info[RTCM3_MSM_SBAS].type[pos]
2870 == Parser->Data.codetype[i][pos][1]))
2871 {
2872 char lli = ' ';
2873 char snr = ' ';
2874 if(df & (GNSSDF_L1CDATA|GNSSDF_L1PDATA))
2875 {
2876 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL1)
2877 lli = '1';
2878 snr = '0'+Parser->Data.snrL1[i];
2879 }
2880 if(df & GNSSDF_L5DATA)
2881 {
2882 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL5)
2883 lli = '1';
2884 snr = ' ';
2885 }
2886 RTCM3Text("%14.3f%c%c",
2887 Parser->Data.measdata[i][pos],lli,snr);
2888 }
2889 else
2890 { /* no or illegal data */
2891 RTCM3Text(" ");
2892 }
2893 }
2894 }
2895 else
2896 {
2897 for(j = 0; j < Parser->info[RTCM3_MSM_GPS].numtypes; ++j)
2898 {
2899 long long df = Parser->info[RTCM3_MSM_GPS].flags[j];
2900 int pos = Parser->info[RTCM3_MSM_GPS].pos[j];
2901 if((Parser->Data.dataflags[i] & df)
2902 && !isnan(Parser->Data.measdata[i][pos])
2903 && !isinf(Parser->Data.measdata[i][pos])
2904 && (Parser->Data.codetype[i][pos]
2905 && Parser->info[RTCM3_MSM_GPS].type[pos]
2906 && Parser->info[RTCM3_MSM_GPS].type[pos]
2907 == Parser->Data.codetype[i][pos][1]))
2908 {
2909 char lli = ' ';
2910 char snr = ' ';
2911 if(df & (GNSSDF_L1CDATA|GNSSDF_L1PDATA))
2912 {
2913 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL1)
2914 lli = '1';
2915 snr = '0'+Parser->Data.snrL1[i];
2916 }
2917 if(df & (GNSSDF_L2CDATA|GNSSDF_L2PDATA))
2918 {
2919 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL2)
2920 lli = '1';
2921 snr = '0'+Parser->Data.snrL2[i];
2922 }
2923 if(df & GNSSDF_L5DATA)
2924 {
2925 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL5)
2926 lli = '1';
2927 snr = ' ';
2928 }
2929 RTCM3Text("%14.3f%c%c",
2930 Parser->Data.measdata[i][pos],lli,snr);
2931 }
2932 else
2933 { /* no or illegal data */
2934 RTCM3Text(" ");
2935 }
2936 }
2937 }
2938 RTCM3Text("\n");
2939 }
2940 }
2941 else
2942 {
2943 RTCM3Text(" %02d %2d %2d %2d %2d %10.7f %d%3d",
2944 cti.year%100, cti.month, cti.day, cti.hour, cti.minute, cti.second
2945 + fmod(Parser->Data.timeofweek/1000.0,1.0), nh ? 4 : 0,
2946 Parser->Data.numsats);
2947 for(i = 0; i < 12 && i < Parser->Data.numsats; ++i)
2948 {
2949 if(Parser->Data.satellites[i] <= PRN_GPS_END)
2950 RTCM3Text("G%02d", Parser->Data.satellites[i]);
2951 else if(Parser->Data.satellites[i] >= PRN_GLONASS_START
2952 && Parser->Data.satellites[i] <= PRN_GLONASS_END)
2953 RTCM3Text("R%02d", Parser->Data.satellites[i]
2954 - (PRN_GLONASS_START-1));
2955 else if(Parser->Data.satellites[i] >= PRN_SBAS_START
2956 && Parser->Data.satellites[i] <= PRN_SBAS_END)
2957 RTCM3Text("S%02d", Parser->Data.satellites[i]
2958 - PRN_SBAS_START+20);
2959 else if(Parser->Data.satellites[i] >= PRN_GALILEO_START
2960 && Parser->Data.satellites[i] <= PRN_GALILEO_END)
2961 RTCM3Text("E%02d", Parser->Data.satellites[i]
2962 - (PRN_GALILEO_START-1));
2963 else if(Parser->Data.satellites[i] >= PRN_GIOVE_START
2964 && Parser->Data.satellites[i] <= PRN_GIOVE_END)
2965 RTCM3Text("E%02d", Parser->Data.satellites[i]
2966 - (PRN_GIOVE_START-PRN_GIOVE_OFFSET));
2967 else if(Parser->Data.satellites[i] >= PRN_QZSS_START
2968 && Parser->Data.satellites[i] <= PRN_QZSS_END)
2969 RTCM3Text("J%02d", Parser->Data.satellites[i]
2970 - (PRN_QZSS_START-1));
2971 else if(Parser->Data.satellites[i] >= PRN_COMPASS_START
2972 && Parser->Data.satellites[i] <= PRN_COMPASS_END)
2973 RTCM3Text("C%02d", Parser->Data.satellites[i]
2974 - (PRN_COMPASS_START-1));
2975 else
2976 RTCM3Text("%3d", Parser->Data.satellites[i]);
2977 }
2978 RTCM3Text("\n");
2979 o = 12;
2980 j = Parser->Data.numsats - 12;
2981 while(j > 0)
2982 {
2983 RTCM3Text(" ");
2984 for(i = o; i < o+12 && i < Parser->Data.numsats; ++i)
2985 {
2986 if(Parser->Data.satellites[i] <= PRN_GPS_END)
2987 RTCM3Text("G%02d", Parser->Data.satellites[i]);
2988 else if(Parser->Data.satellites[i] >= PRN_GLONASS_START
2989 && Parser->Data.satellites[i] <= PRN_GLONASS_END)
2990 RTCM3Text("R%02d", Parser->Data.satellites[i]
2991 - (PRN_GLONASS_START-1));
2992 else if(Parser->Data.satellites[i] >= PRN_SBAS_START
2993 && Parser->Data.satellites[i] <= PRN_SBAS_END)
2994 RTCM3Text("S%02d", Parser->Data.satellites[i]
2995 - PRN_SBAS_START+20);
2996 else if(Parser->Data.satellites[i] >= PRN_GALILEO_START
2997 && Parser->Data.satellites[i] <= PRN_GALILEO_END)
2998 RTCM3Text("E%02d", Parser->Data.satellites[i]
2999 - (PRN_GALILEO_START-1));
3000 else if(Parser->Data.satellites[i] >= PRN_GIOVE_START
3001 && Parser->Data.satellites[i] <= PRN_GIOVE_END)
3002 RTCM3Text("E%02d", Parser->Data.satellites[i]
3003 - (PRN_GIOVE_START-PRN_GIOVE_OFFSET));
3004 else if(Parser->Data.satellites[i] >= PRN_QZSS_START
3005 && Parser->Data.satellites[i] <= PRN_QZSS_END)
3006 RTCM3Text("J%02d", Parser->Data.satellites[i]
3007 - (PRN_QZSS_START-1));
3008 else if(Parser->Data.satellites[i] >= PRN_COMPASS_START
3009 && Parser->Data.satellites[i] <= PRN_COMPASS_END)
3010 RTCM3Text("C%02d", Parser->Data.satellites[i]
3011 - (PRN_COMPASS_START-1));
3012 else
3013 RTCM3Text("%3d", Parser->Data.satellites[i]);
3014 }
3015 RTCM3Text("\n");
3016 j -= 12;
3017 o += 12;
3018 }
3019 if(nh)
3020 {
3021 RTCM3Text("%s\n "
3022 " END OF HEADER\n", newheader);
3023 }
3024 for(i = 0; i < Parser->Data.numsats; ++i)
3025 {
3026 for(j = 0; j < Parser->info[RTCM3_MSM_GPS].numtypes; ++j)
3027 {
3028 int v = 0;
3029 long long df = Parser->flags[j];
3030 int pos = Parser->pos[j];
3031 if((Parser->Data.dataflags[i] & df)
3032 && !isnan(Parser->Data.measdata[i][pos])
3033 && !isinf(Parser->Data.measdata[i][pos]))
3034 {
3035 v = 1;
3036 }
3037 else
3038 {
3039 df = Parser->info[RTCM3_MSM_GPS].flags[j];
3040 pos = Parser->info[RTCM3_MSM_GPS].pos[j];
3041
3042 if((Parser->Data.dataflags[i] & df)
3043 && !isnan(Parser->Data.measdata[i][pos])
3044 && !isinf(Parser->Data.measdata[i][pos]))
3045 {
3046 v = 1;
3047 }
3048 }
3049
3050 if(!v)
3051 { /* no or illegal data */
3052 RTCM3Text(" ");
3053 }
3054 else
3055 {
3056 char lli = ' ';
3057 char snr = ' ';
3058 if(df & (GNSSDF_L1CDATA|GNSSDF_L1PDATA))
3059 {
3060 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL1)
3061 lli = '1';
3062 snr = '0'+Parser->Data.snrL1[i];
3063 }
3064 if(df & (GNSSDF_L2CDATA|GNSSDF_L2PDATA))
3065 {
3066 if(Parser->Data.dataflags2[i]
3067 & (GNSSDF2_LOCKLOSSL2|GNSSDF2_XCORRL2))
3068 {
3069 lli = '0';
3070 if(Parser->Data.dataflags2[i] & GNSSDF2_LOCKLOSSL2)
3071 lli += 1;
3072 if(Parser->Data.dataflags2[i] & GNSSDF2_XCORRL2)
3073 lli += 4;
3074 }
3075 snr = '0'+Parser->Data.snrL2[i];
3076 }
3077 if((df & GNSSDF_P2DATA) && (Parser->Data.dataflags2[i]
3078 & GNSSDF2_XCORRL2))
3079 lli = '4';
3080 RTCM3Text("%14.3f%c%c",
3081 Parser->Data.measdata[i][pos],lli,snr);
3082 }
3083 if(j%5 == 4 || j == Parser->info[RTCM3_MSM_GPS].numtypes-1)
3084 RTCM3Text("\n");
3085 }
3086 }
3087 }
3088 }
3089 }
3090 }
3091}
3092
3093#ifndef NO_RTCM3_MAIN
3094static char datestr[] = "$Date: 2013-08-26 08:58:35 +0000 (Mon, 26 Aug 2013) $";
3095
3096/* The string, which is send as agent in HTTP request */
3097#define AGENTSTRING "NTRIP NtripRTCM3ToRINEX"
3098
3099#define MAXDATASIZE 1000 /* max number of bytes we can get at once */
3100
3101static const char encodingTable [64] = {
3102 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
3103 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
3104 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
3105 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
3106};
3107
3108/* does not buffer overrun, but breaks directly after an error */
3109/* returns the number of required bytes */
3110static int encode(char *buf, int size, const char *user, const char *pwd)
3111{
3112 unsigned char inbuf[3];
3113 char *out = buf;
3114 int i, sep = 0, fill = 0, bytes = 0;
3115
3116 while(*user || *pwd)
3117 {
3118 i = 0;
3119 while(i < 3 && *user) inbuf[i++] = *(user++);
3120 if(i < 3 && !sep) {inbuf[i++] = ':'; ++sep; }
3121 while(i < 3 && *pwd) inbuf[i++] = *(pwd++);
3122 while(i < 3) {inbuf[i++] = 0; ++fill; }
3123 if(out-buf < size-1)
3124 *(out++) = encodingTable[(inbuf [0] & 0xFC) >> 2];
3125 if(out-buf < size-1)
3126 *(out++) = encodingTable[((inbuf [0] & 0x03) << 4)
3127 | ((inbuf [1] & 0xF0) >> 4)];
3128 if(out-buf < size-1)
3129 {
3130 if(fill == 2)
3131 *(out++) = '=';
3132 else
3133 *(out++) = encodingTable[((inbuf [1] & 0x0F) << 2)
3134 | ((inbuf [2] & 0xC0) >> 6)];
3135 }
3136 if(out-buf < size-1)
3137 {
3138 if(fill >= 1)
3139 *(out++) = '=';
3140 else
3141 *(out++) = encodingTable[inbuf [2] & 0x3F];
3142 }
3143 bytes += 4;
3144 }
3145 if(out-buf < size)
3146 *out = 0;
3147 return bytes;
3148}
3149
3150static int stop = 0;
3151
3152struct Args
3153{
3154 const char *server;
3155 const char *port;
3156 int mode;
3157 int timeout;
3158 int rinex3;
3159 int changeobs;
3160 const char *user;
3161 const char *password;
3162 const char *proxyhost;
3163 const char *proxyport;
3164 const char *nmea;
3165 const char *data;
3166 const char *headerfile;
3167 const char *gpsephemeris;
3168 const char *glonassephemeris;
3169};
3170
3171/* option parsing */
3172#ifdef NO_LONG_OPTS
3173#define LONG_OPT(a)
3174#else
3175#define LONG_OPT(a) a
3176static struct option opts[] = {
3177{ "data", required_argument, 0, 'd'},
3178{ "server", required_argument, 0, 's'},
3179{ "password", required_argument, 0, 'p'},
3180{ "port", required_argument, 0, 'r'},
3181{ "timeout", required_argument, 0, 't'},
3182{ "header", required_argument, 0, 'f'},
3183{ "user", required_argument, 0, 'u'},
3184{ "gpsephemeris", required_argument, 0, 'E'},
3185{ "glonassephemeris", required_argument, 0, 'G'},
3186{ "rinex3", no_argument, 0, '3'},
3187{ "changeobs", no_argument, 0, 'O'},
3188{ "proxyport", required_argument, 0, 'R'},
3189{ "proxyhost", required_argument, 0, 'S'},
3190{ "nmea", required_argument, 0, 'n'},
3191{ "mode", required_argument, 0, 'M'},
3192{ "help", no_argument, 0, 'h'},
3193{0,0,0,0}};
3194#endif
3195#define ARGOPT "-d:s:p:r:t:f:u:E:G:M:S:R:n:h3O"
3196
3197enum MODE { HTTP = 1, RTSP = 2, NTRIP1 = 3, AUTO = 4, END };
3198
3199static const char *geturl(const char *url, struct Args *args)
3200{
3201 static char buf[1000];
3202 static char *Buffer = buf;
3203 static char *Bufend = buf+sizeof(buf);
3204
3205 if(strncmp("ntrip:", url, 6))
3206 return "URL must start with 'ntrip:'.";
3207 url += 6; /* skip ntrip: */
3208
3209 if(*url != '@' && *url != '/')
3210 {
3211 /* scan for mountpoint */
3212 args->data = Buffer;
3213 while(*url && *url != '@' && *url != ';' &&*url != '/' && Buffer != Bufend)
3214 *(Buffer++) = *(url++);
3215 if(Buffer == args->data)
3216 return "Mountpoint required.";
3217 else if(Buffer >= Bufend-1)
3218 return "Parsing buffer too short.";
3219 *(Buffer++) = 0;
3220 }
3221
3222 if(*url == '/') /* username and password */
3223 {
3224 ++url;
3225 args->user = Buffer;
3226 while(*url && *url != '@' && *url != ';' && *url != ':' && Buffer != Bufend)
3227 *(Buffer++) = *(url++);
3228 if(Buffer == args->user)
3229 return "Username cannot be empty.";
3230 else if(Buffer >= Bufend-1)
3231 return "Parsing buffer too short.";
3232 *(Buffer++) = 0;
3233
3234 if(*url == ':') ++url;
3235
3236 args->password = Buffer;
3237 while(*url && *url != '@' && *url != ';' && Buffer != Bufend)
3238 *(Buffer++) = *(url++);
3239 if(Buffer == args->password)
3240 return "Password cannot be empty.";
3241 else if(Buffer >= Bufend-1)
3242 return "Parsing buffer too short.";
3243 *(Buffer++) = 0;
3244 }
3245
3246 if(*url == '@') /* server */
3247 {
3248 ++url;
3249 if(*url != '@' && *url != ':')
3250 {
3251 args->server = Buffer;
3252 while(*url && *url != '@' && *url != ':' && *url != ';' && Buffer != Bufend)
3253 *(Buffer++) = *(url++);
3254 if(Buffer == args->server)
3255 return "Servername cannot be empty.";
3256 else if(Buffer >= Bufend-1)
3257 return "Parsing buffer too short.";
3258 *(Buffer++) = 0;
3259 }
3260
3261 if(*url == ':')
3262 {
3263 ++url;
3264 args->port = Buffer;
3265 while(*url && *url != '@' && *url != ';' && Buffer != Bufend)
3266 *(Buffer++) = *(url++);
3267 if(Buffer == args->port)
3268 return "Port cannot be empty.";
3269 else if(Buffer >= Bufend-1)
3270 return "Parsing buffer too short.";
3271 *(Buffer++) = 0;
3272 }
3273
3274 if(*url == '@') /* proxy */
3275 {
3276 ++url;
3277 args->proxyhost = Buffer;
3278 while(*url && *url != ':' && *url != ';' && Buffer != Bufend)
3279 *(Buffer++) = *(url++);
3280 if(Buffer == args->proxyhost)
3281 return "Proxy servername cannot be empty.";
3282 else if(Buffer >= Bufend-1)
3283 return "Parsing buffer too short.";
3284 *(Buffer++) = 0;
3285
3286 if(*url == ':')
3287 {
3288 ++url;
3289 args->proxyport = Buffer;
3290 while(*url && *url != ';' && Buffer != Bufend)
3291 *(Buffer++) = *(url++);
3292 if(Buffer == args->proxyport)
3293 return "Proxy port cannot be empty.";
3294 else if(Buffer >= Bufend-1)
3295 return "Parsing buffer too short.";
3296 *(Buffer++) = 0;
3297 }
3298 }
3299 }
3300 if(*url == ';') /* NMEA */
3301 {
3302 args->nmea = ++url;
3303 while(*url)
3304 ++url;
3305 }
3306
3307 return *url ? "Garbage at end of server string." : 0;
3308}
3309
3310static int getargs(int argc, char **argv, struct Args *args)
3311{
3312 int res = 1;
3313 int getoptr;
3314 int help = 0;
3315 char *t;
3316
3317 args->server = "www.euref-ip.net";
3318 args->port = "2101";
3319 args->timeout = 60;
3320 args->user = "";
3321 args->password = "";
3322 args->data = 0;
3323 args->headerfile = 0;
3324 args->gpsephemeris = 0;
3325 args->glonassephemeris = 0;
3326 args->rinex3 = 0;
3327 args->nmea = 0;
3328 args->changeobs = 0;
3329 args->proxyhost = 0;
3330 args->proxyport = "2101";
3331 args->mode = AUTO;
3332 help = 0;
3333
3334 do
3335 {
3336
3337#ifdef NO_LONG_OPTS
3338 switch((getoptr = getopt(argc, argv, ARGOPT)))
3339#else
3340 switch((getoptr = getopt_long(argc, argv, ARGOPT, opts, 0)))
3341#endif
3342 {
3343 case 's': args->server = optarg; break;
3344 case 'u': args->user = optarg; break;
3345 case 'p': args->password = optarg; break;
3346 case 'd': args->data = optarg; break;
3347 case 'f': args->headerfile = optarg; break;
3348 case 'E': args->gpsephemeris = optarg; break;
3349 case 'G': args->glonassephemeris = optarg; break;
3350 case 'r': args->port = optarg; break;
3351 case '3': args->rinex3 = 1; break;
3352 case 'S': args->proxyhost = optarg; break;
3353 case 'n': args->nmea = optarg; break;
3354 case 'R': args->proxyport = optarg; break;
3355 case 'O': args->changeobs = 1; break;
3356 case 'h': help=1; break;
3357 case 'M':
3358 args->mode = 0;
3359 if (!strcmp(optarg,"n") || !strcmp(optarg,"ntrip1"))
3360 args->mode = NTRIP1;
3361 else if(!strcmp(optarg,"h") || !strcmp(optarg,"http"))
3362 args->mode = HTTP;
3363 else if(!strcmp(optarg,"r") || !strcmp(optarg,"rtsp"))
3364 args->mode = RTSP;
3365 else if(!strcmp(optarg,"a") || !strcmp(optarg,"auto"))
3366 args->mode = AUTO;
3367 else args->mode = atoi(optarg);
3368 if((args->mode == 0) || (args->mode >= END))
3369 {
3370 fprintf(stderr, "Mode %s unknown\n", optarg);
3371 res = 0;
3372 }
3373 break;
3374 case 't':
3375 args->timeout = strtoul(optarg, &t, 10);
3376 if((t && *t) || args->timeout < 0)
3377 res = 0;
3378 break;
3379
3380 case 1:
3381 {
3382 const char *err;
3383 if((err = geturl(optarg, args)))
3384 {
3385 RTCM3Error("%s\n\n", err);
3386 res = 0;
3387 }
3388 }
3389 break;
3390 case -1: break;
3391 }
3392 } while(getoptr != -1 || !res);
3393
3394 datestr[0] = datestr[7];
3395 datestr[1] = datestr[8];
3396 datestr[2] = datestr[9];
3397 datestr[3] = datestr[10];
3398 datestr[5] = datestr[12];
3399 datestr[6] = datestr[13];
3400 datestr[8] = datestr[15];
3401 datestr[9] = datestr[16];
3402 datestr[4] = datestr[7] = '-';
3403 datestr[10] = 0;
3404
3405 if(args->gpsephemeris && args->glonassephemeris && args->rinex3)
3406 {
3407 RTCM3Error("RINEX3 produces a combined ephemeris file, but 2 files were specified.\n"
3408 "Please specify only one navigation file.\n");
3409 res = 0;
3410 }
3411 else if(!res || help)
3412 {
3413 RTCM3Error("Version %s (%s) GPL" COMPILEDATE
3414 "\nUsage: %s -s server -u user ...\n"
3415 " -d " LONG_OPT("--data ") "the requested data set\n"
3416 " -f " LONG_OPT("--headerfile ") "file for RINEX header information\n"
3417 " -s " LONG_OPT("--server ") "the server name or address\n"
3418 " -p " LONG_OPT("--password ") "the login password\n"
3419 " -r " LONG_OPT("--port ") "the server port number (default 2101)\n"
3420 " -t " LONG_OPT("--timeout ") "timeout in seconds (default 60)\n"
3421 " -u " LONG_OPT("--user ") "the user name\n"
3422 " -E " LONG_OPT("--gpsephemeris ") "output file for GPS ephemeris data\n"
3423 " -G " LONG_OPT("--glonassephemeris ") "output file for GLONASS ephemeris data\n"
3424 " -3 " LONG_OPT("--rinex3 ") "output RINEX type 3 data\n"
3425 " -S " LONG_OPT("--proxyhost ") "proxy name or address\n"
3426 " -R " LONG_OPT("--proxyport ") "proxy port, optional (default 2101)\n"
3427 " -n " LONG_OPT("--nmea ") "NMEA string for sending to server\n"
3428 " -O " LONG_OPT("--changeobs ") "Add observation type change header lines\n"
3429 " -M " LONG_OPT("--mode ") "mode for data request\n"
3430 " Valid modes are:\n"
3431 " 1, h, http NTRIP Version 2.0 Caster in TCP/IP mode\n"
3432 " 2, r, rtsp NTRIP Version 2.0 Caster in RTSP/RTP mode\n"
3433 " 3, n, ntrip1 NTRIP Version 1.0 Caster\n"
3434 " 4, a, auto automatic detection (default)\n"
3435 "or using an URL:\n%s ntrip:data[/user[:password]][@[server][:port][@proxyhost[:proxyport]]][;nmea]\n"
3436 , revisionstr, datestr, argv[0], argv[0]);
3437 exit(1);
3438 }
3439 return res;
3440}
3441
3442/* let the output complete a block if necessary */
3443static void signalhandler(int sig)
3444{
3445 if(!stop)
3446 {
3447 RTCM3Error("Stop signal number %d received. "
3448 "Trying to terminate gentle.\n", sig);
3449 stop = 1;
3450 alarm(1);
3451 }
3452}
3453
3454#ifndef WINDOWSVERSION
3455static void WaitMicro(int mic)
3456{
3457 struct timeval tv;
3458 tv.tv_sec = mic/1000000;
3459 tv.tv_usec = mic%1000000;
3460#ifdef DEBUG
3461 fprintf(stderr, "Waiting %d micro seconds\n", mic);
3462#endif
3463 select(0, 0, 0, 0, &tv);
3464}
3465#else /* WINDOWSVERSION */
3466void WaitMicro(int mic)
3467{
3468 Sleep(mic/1000);
3469}
3470#endif /* WINDOWSVERSION */
3471
3472#define ALARMTIME (2*60)
3473
3474/* for some reason we had to abort hard (maybe waiting for data */
3475#ifdef __GNUC__
3476static __attribute__ ((noreturn)) void signalhandler_alarm(
3477int sig __attribute__((__unused__)))
3478#else /* __GNUC__ */
3479static void signalhandler_alarm(int sig)
3480#endif /* __GNUC__ */
3481{
3482 RTCM3Error("Programm forcefully terminated.\n");
3483 exit(1);
3484}
3485
3486int main(int argc, char **argv)
3487{
3488 struct Args args;
3489 struct RTCM3ParserData Parser;
3490
3491 setbuf(stdout, 0);
3492 setbuf(stdin, 0);
3493 setbuf(stderr, 0);
3494
3495 fixrevision();
3496
3497 signal(SIGINT, signalhandler);
3498 signal(SIGALRM,signalhandler_alarm);
3499 signal(SIGQUIT,signalhandler);
3500 signal(SIGTERM,signalhandler);
3501 signal(SIGPIPE,signalhandler);
3502 memset(&Parser, 0, sizeof(Parser));
3503 {
3504 time_t tim;
3505 tim = time(0) - ((10*365+2+5)*24*60*60+LEAPSECONDS);
3506 Parser.GPSWeek = tim/(7*24*60*60);
3507 Parser.GPSTOW = tim%(7*24*60*60);
3508 }
3509
3510 if(getargs(argc, argv, &args))
3511 {
3512 int sockfd, numbytes;
3513 char buf[MAXDATASIZE];
3514 struct sockaddr_in their_addr; /* connector's address information */
3515 struct hostent *he;
3516 struct servent *se;
3517 const char *server, *port, *proxyserver = 0;
3518 char proxyport[6];
3519 char *b;
3520 long i;
3521 struct timeval tv;
3522
3523 alarm(ALARMTIME);
3524
3525 Parser.headerfile = args.headerfile;
3526 Parser.glonassephemeris = args.glonassephemeris;
3527 Parser.gpsephemeris = args.gpsephemeris;
3528 Parser.rinex3 = args.rinex3;
3529 Parser.changeobs = args.changeobs;
3530
3531 if(args.proxyhost)
3532 {
3533 int p;
3534 if((i = strtol(args.port, &b, 10)) && (!b || !*b))
3535 p = i;
3536 else if(!(se = getservbyname(args.port, 0)))
3537 {
3538 RTCM3Error("Can't resolve port %s.", args.port);
3539 exit(1);
3540 }
3541 else
3542 {
3543 p = ntohs(se->s_port);
3544 }
3545 snprintf(proxyport, sizeof(proxyport), "%d", p);
3546 port = args.proxyport;
3547 proxyserver = args.server;
3548 server = args.proxyhost;
3549 }
3550 else
3551 {
3552 server = args.server;
3553 port = args.port;
3554 }
3555
3556 memset(&their_addr, 0, sizeof(struct sockaddr_in));
3557 if((i = strtol(port, &b, 10)) && (!b || !*b))
3558 their_addr.sin_port = htons(i);
3559 else if(!(se = getservbyname(port, 0)))
3560 {
3561 RTCM3Error("Can't resolve port %s.", port);
3562 exit(1);
3563 }
3564 else
3565 {
3566 their_addr.sin_port = se->s_port;
3567 }
3568 if(!(he=gethostbyname(server)))
3569 {
3570 RTCM3Error("Server name lookup failed for '%s'.\n", server);
3571 exit(1);
3572 }
3573 if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
3574 {
3575 perror("socket");
3576 exit(1);
3577 }
3578
3579 tv.tv_sec = args.timeout;
3580 tv.tv_usec = 0;
3581 if(setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval) ) == -1)
3582 {
3583 RTCM3Error("Function setsockopt: %s\n", strerror(errno));
3584 exit(1);
3585 }
3586
3587 their_addr.sin_family = AF_INET;
3588 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
3589
3590 if(args.data && args.mode == RTSP)
3591 {
3592 struct sockaddr_in local;
3593 int sockudp, localport;
3594 int cseq = 1;
3595 socklen_t len;
3596
3597 if((sockudp = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
3598 {
3599 perror("socket");
3600 exit(1);
3601 }
3602 /* fill structure with local address information for UDP */
3603 memset(&local, 0, sizeof(local));
3604 local.sin_family = AF_INET;
3605 local.sin_port = htons(0);
3606 local.sin_addr.s_addr = htonl(INADDR_ANY);
3607 len = sizeof(local);
3608 /* bind() in order to get a random RTP client_port */
3609 if((bind(sockudp, (struct sockaddr *)&local, len)) < 0)
3610 {
3611 perror("bind");
3612 exit(1);
3613 }
3614 if((getsockname(sockudp, (struct sockaddr*)&local, &len)) != -1)
3615 {
3616 localport = ntohs(local.sin_port);
3617 }
3618 else
3619 {
3620 perror("local access failed");
3621 exit(1);
3622 }
3623 if(connect(sockfd, (struct sockaddr *)&their_addr,
3624 sizeof(struct sockaddr)) == -1)
3625 {
3626 perror("connect");
3627 exit(1);
3628 }
3629 i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
3630 "SETUP rtsp://%s%s%s/%s RTSP/1.0\r\n"
3631 "CSeq: %d\r\n"
3632 "Ntrip-Version: Ntrip/2.0\r\n"
3633 "Ntrip-Component: Ntripclient\r\n"
3634 "User-Agent: %s/%s\r\n"
3635 "Transport: RTP/GNSS;unicast;client_port=%u\r\n"
3636 "Authorization: Basic ",
3637 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
3638 args.data, cseq++, AGENTSTRING, revisionstr, localport);
3639 if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
3640 {
3641 RTCM3Error("Requested data too long\n");
3642 exit(1);
3643 }
3644 i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
3645 if(i > MAXDATASIZE-4)
3646 {
3647 RTCM3Error("Username and/or password too long\n");
3648 exit(1);
3649 }
3650 buf[i++] = '\r';
3651 buf[i++] = '\n';
3652 buf[i++] = '\r';
3653 buf[i++] = '\n';
3654 if(args.nmea)
3655 {
3656 int j = snprintf(buf+i, MAXDATASIZE-i, "%s\r\n", args.nmea);
3657 if(j >= 0 && j < MAXDATASIZE-i)
3658 i += j;
3659 else
3660 {
3661 RTCM3Error("NMEA string too long\n");
3662 exit(1);
3663 }
3664 }
3665 if(send(sockfd, buf, (size_t)i, 0) != i)
3666 {
3667 perror("send");
3668 exit(1);
3669 }
3670 if((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
3671 {
3672 if(numbytes >= 17 && !strncmp(buf, "RTSP/1.0 200 OK\r\n", 17))
3673 {
3674 int serverport = 0, session = 0;
3675 const char *portcheck = "server_port=";
3676 const char *sessioncheck = "session: ";
3677 int l = strlen(portcheck)-1;
3678 int j=0;
3679 for(i = 0; j != l && i < numbytes-l; ++i)
3680 {
3681 for(j = 0; j < l && tolower(buf[i+j]) == portcheck[j]; ++j)
3682 ;
3683 }
3684 if(i == numbytes-l)
3685 {
3686 RTCM3Error("No server port number found\n");
3687 exit(1);
3688 }
3689 else
3690 {
3691 i+=l;
3692 while(i < numbytes && buf[i] >= '0' && buf[i] <= '9')
3693 serverport = serverport * 10 + buf[i++]-'0';
3694 if(buf[i] != '\r' && buf[i] != ';')
3695 {
3696 RTCM3Error("Could not extract server port\n");
3697 exit(1);
3698 }
3699 }
3700 l = strlen(sessioncheck)-1;
3701 j=0;
3702 for(i = 0; j != l && i < numbytes-l; ++i)
3703 {
3704 for(j = 0; j < l && tolower(buf[i+j]) == sessioncheck[j]; ++j)
3705 ;
3706 }
3707 if(i == numbytes-l)
3708 {
3709 RTCM3Error("No session number found\n");
3710 exit(1);
3711 }
3712 else
3713 {
3714 i+=l;
3715 while(i < numbytes && buf[i] >= '0' && buf[i] <= '9')
3716 session = session * 10 + buf[i++]-'0';
3717 if(buf[i] != '\r')
3718 {
3719 RTCM3Error("Could not extract session number\n");
3720 exit(1);
3721 }
3722 }
3723
3724 i = snprintf(buf, MAXDATASIZE,
3725 "PLAY rtsp://%s%s%s/%s RTSP/1.0\r\n"
3726 "CSeq: %d\r\n"
3727 "Session: %d\r\n"
3728 "\r\n",
3729 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
3730 args.data, cseq++, session);
3731
3732 if(i > MAXDATASIZE || i < 0) /* second check for old glibc */
3733 {
3734 RTCM3Error("Requested data too long\n");
3735 exit(1);
3736 }
3737 if(send(sockfd, buf, (size_t)i, 0) != i)
3738 {
3739 perror("send");
3740 exit(1);
3741 }
3742 if((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
3743 {
3744 if(numbytes >= 17 && !strncmp(buf, "RTSP/1.0 200 OK\r\n", 17))
3745 {
3746 struct sockaddr_in addrRTP;
3747 /* fill structure with caster address information for UDP */
3748 memset(&addrRTP, 0, sizeof(addrRTP));
3749 addrRTP.sin_family = AF_INET;
3750 addrRTP.sin_port = htons(serverport);
3751 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
3752 len = sizeof(addrRTP);
3753 int ts = 0;
3754 int sn = 0;
3755 int ssrc = 0;
3756 int init = 0;
3757 int u, v, w;
3758 while(!stop && (i = recvfrom(sockudp, buf, 1526, 0,
3759 (struct sockaddr*) &addrRTP, &len)) > 0)
3760 {
3761 alarm(ALARMTIME);
3762 if(i >= 12+1 && (unsigned char)buf[0] == (2 << 6) && buf[1] == 0x60)
3763 {
3764 u= ((unsigned char)buf[2]<<8)+(unsigned char)buf[3];
3765 v = ((unsigned char)buf[4]<<24)+((unsigned char)buf[5]<<16)
3766 +((unsigned char)buf[6]<<8)+(unsigned char)buf[7];
3767 w = ((unsigned char)buf[8]<<24)+((unsigned char)buf[9]<<16)
3768 +((unsigned char)buf[10]<<8)+(unsigned char)buf[11];
3769
3770 if(init)
3771 {
3772 int z;
3773 if(u < -30000 && sn > 30000) sn -= 0xFFFF;
3774 if(ssrc != w || ts > v)
3775 {
3776 RTCM3Error("Illegal UDP data received.\n");
3777 exit(1);
3778 }
3779 if(u > sn) /* don't show out-of-order packets */
3780 for(z = 12; z < i && !stop; ++z)
3781 HandleByte(&Parser, (unsigned int) buf[z]);
3782 }
3783 sn = u; ts = v; ssrc = w; init = 1;
3784 }
3785 else
3786 {
3787 RTCM3Error("Illegal UDP header.\n");
3788 exit(1);
3789 }
3790 }
3791 }
3792 i = snprintf(buf, MAXDATASIZE,
3793 "TEARDOWN rtsp://%s%s%s/%s RTSP/1.0\r\n"
3794 "CSeq: %d\r\n"
3795 "Session: %d\r\n"
3796 "\r\n",
3797 args.server, proxyserver ? ":" : "", proxyserver ? args.port : "",
3798 args.data, cseq++, session);
3799
3800 if(i > MAXDATASIZE || i < 0) /* second check for old glibc */
3801 {
3802 RTCM3Error("Requested data too long\n");
3803 exit(1);
3804 }
3805 if(send(sockfd, buf, (size_t)i, 0) != i)
3806 {
3807 perror("send");
3808 exit(1);
3809 }
3810 }
3811 else
3812 {
3813 RTCM3Error("Could not start data stream.\n");
3814 exit(1);
3815 }
3816 }
3817 else
3818 {
3819 RTCM3Error("Could not setup initial control connection.\n");
3820 exit(1);
3821 }
3822 }
3823 else
3824 {
3825 perror("recv");
3826 exit(1);
3827 }
3828 }
3829 else
3830 {
3831 if(connect(sockfd, (struct sockaddr *)&their_addr,
3832 sizeof(struct sockaddr)) == -1)
3833 {
3834 perror("connect");
3835 exit(1);
3836 }
3837 if(!args.data)
3838 {
3839 i = snprintf(buf, MAXDATASIZE,
3840 "GET %s%s%s%s/ HTTP/1.0\r\n"
3841 "Host: %s\r\n%s"
3842 "User-Agent: %s/%s\r\n"
3843 "Connection: close\r\n"
3844 "\r\n"
3845 , proxyserver ? "http://" : "", proxyserver ? proxyserver : "",
3846 proxyserver ? ":" : "", proxyserver ? proxyport : "",
3847 args.server, args.mode == NTRIP1 ? "" : "Ntrip-Version: Ntrip/2.0\r\n",
3848 AGENTSTRING, revisionstr);
3849 }
3850 else
3851 {
3852 i=snprintf(buf, MAXDATASIZE-40, /* leave some space for login */
3853 "GET %s%s%s%s/%s HTTP/1.0\r\n"
3854 "Host: %s\r\n%s"
3855 "User-Agent: %s/%s\r\n"
3856 "Connection: close\r\n"
3857 "Authorization: Basic "
3858 , proxyserver ? "http://" : "", proxyserver ? proxyserver : "",
3859 proxyserver ? ":" : "", proxyserver ? proxyport : "",
3860 args.data, args.server,
3861 args.mode == NTRIP1 ? "" : "Ntrip-Version: Ntrip/2.0\r\n",
3862 AGENTSTRING, revisionstr);
3863 if(i > MAXDATASIZE-40 || i < 0) /* second check for old glibc */
3864 {
3865 RTCM3Error("Requested data too long\n");
3866 exit(1);
3867 }
3868 i += encode(buf+i, MAXDATASIZE-i-4, args.user, args.password);
3869 if(i > MAXDATASIZE-4)
3870 {
3871 RTCM3Error("Username and/or password too long\n");
3872 exit(1);
3873 }
3874 buf[i++] = '\r';
3875 buf[i++] = '\n';
3876 buf[i++] = '\r';
3877 buf[i++] = '\n';
3878 if(args.nmea)
3879 {
3880 int j = snprintf(buf+i, MAXDATASIZE-i, "%s\r\n", args.nmea);
3881 if(j >= 0 && j < MAXDATASIZE-i)
3882 i += j;
3883 else
3884 {
3885 RTCM3Error("NMEA string too long\n");
3886 exit(1);
3887 }
3888 }
3889 }
3890 if(send(sockfd, buf, (size_t)i, 0) != i)
3891 {
3892 perror("send");
3893 exit(1);
3894 }
3895 if(args.data)
3896 {
3897 int k = 0;
3898 int chunkymode = 0;
3899 int totalbytes = 0;
3900 int chunksize = 0;
3901
3902 while(!stop && (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) != -1)
3903 {
3904 if(numbytes > 0)
3905 alarm(ALARMTIME);
3906 else
3907 {
3908 WaitMicro(100);
3909 continue;
3910 }
3911 if(!k)
3912 {
3913 if(numbytes > 17 && (!strncmp(buf, "HTTP/1.1 200 OK\r\n", 17)
3914 || !strncmp(buf, "HTTP/1.0 200 OK\r\n", 17)))
3915 {
3916 const char *datacheck = "Content-Type: gnss/data\r\n";
3917 const char *chunkycheck = "Transfer-Encoding: chunked\r\n";
3918 int l = strlen(datacheck)-1;
3919 int j=0;
3920 for(i = 0; j != l && i < numbytes-l; ++i)
3921 {
3922 for(j = 0; j < l && buf[i+j] == datacheck[j]; ++j)
3923 ;
3924 }
3925 if(i == numbytes-l)
3926 {
3927 RTCM3Error("No 'Content-Type: gnss/data' found\n");
3928 exit(1);
3929 }
3930 l = strlen(chunkycheck)-1;
3931 j=0;
3932 for(i = 0; j != l && i < numbytes-l; ++i)
3933 {
3934 for(j = 0; j < l && buf[i+j] == chunkycheck[j]; ++j)
3935 ;
3936 }
3937 if(i < numbytes-l)
3938 chunkymode = 1;
3939 }
3940 else if(numbytes < 12 || strncmp("ICY 200 OK\r\n", buf, 12))
3941 {
3942 RTCM3Error("Could not get the requested data: ");
3943 for(k = 0; k < numbytes && buf[k] != '\n' && buf[k] != '\r'; ++k)
3944 {
3945 RTCM3Error("%c", isprint(buf[k]) ? buf[k] : '.');
3946 }
3947 RTCM3Error("\n");
3948 exit(1);
3949 }
3950 else if(args.mode != NTRIP1)
3951 {
3952 if(args.mode != AUTO)
3953 {
3954 RTCM3Error("NTRIP version 2 HTTP connection failed%s.\n",
3955 args.mode == AUTO ? ", falling back to NTRIP1" : "");
3956 }
3957 if(args.mode == HTTP)
3958 exit(1);
3959 }
3960 ++k;
3961 }
3962 else
3963 {
3964 if(chunkymode)
3965 {
3966 int stop = 0;
3967 int pos = 0;
3968 while(!stop && pos < numbytes)
3969 {
3970 switch(chunkymode)
3971 {
3972 case 1: /* reading number starts */
3973 chunksize = 0;
3974 ++chunkymode; /* no break */
3975 case 2: /* during reading number */
3976 i = buf[pos++];
3977 if(i >= '0' && i <= '9') chunksize = chunksize*16+i-'0';
3978 else if(i >= 'a' && i <= 'f') chunksize = chunksize*16+i-'a'+10;
3979 else if(i >= 'A' && i <= 'F') chunksize = chunksize*16+i-'A'+10;
3980 else if(i == '\r') ++chunkymode;
3981 else if(i == ';') chunkymode = 5;
3982 else stop = 1;
3983 break;
3984 case 3: /* scanning for return */
3985 if(buf[pos++] == '\n') chunkymode = chunksize ? 4 : 1;
3986 else stop = 1;
3987 break;
3988 case 4: /* output data */
3989 i = numbytes-pos;
3990 if(i > chunksize) i = chunksize;
3991 {
3992 int z;
3993 for(z = 0; z < i && !stop; ++z)
3994 HandleByte(&Parser, (unsigned int) buf[pos+z]);
3995 }
3996 totalbytes += i;
3997 chunksize -= i;
3998 pos += i;
3999 if(!chunksize)
4000 chunkymode = 1;
4001 break;
4002 case 5:
4003 if(i == '\r') chunkymode = 3;
4004 break;
4005 }
4006 }
4007 if(stop)
4008 {
4009 RTCM3Error("Error in chunky transfer encoding\n");
4010 break;
4011 }
4012 }
4013 else
4014 {
4015 totalbytes += numbytes;
4016 {
4017 int z;
4018 for(z = 0; z < numbytes && !stop; ++z)
4019 HandleByte(&Parser, (unsigned int) buf[z]);
4020 }
4021 }
4022 if(totalbytes < 0) /* overflow */
4023 {
4024 totalbytes = 0;
4025 }
4026 }
4027 }
4028 }
4029 else
4030 {
4031 while(!stop && (numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) > 0)
4032 {
4033 alarm(ALARMTIME);
4034 fwrite(buf, (size_t)numbytes, 1, stdout);
4035 }
4036 }
4037 close(sockfd);
4038 }
4039 }
4040 return 0;
4041}
4042#endif /* NO_RTCM3_MAIN */
Note: See TracBrowser for help on using the repository browser.