forked from tomojitakasu/RTKLIB
-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathublox.c
More file actions
1891 lines (1758 loc) · 90.3 KB
/
Copy pathublox.c
File metadata and controls
1891 lines (1758 loc) · 90.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*------------------------------------------------------------------------------
* ublox.c : ublox receiver dependent functions
*
* Copyright (C) 2007-2020 by T.TAKASU, All rights reserved.
* Copyright (C) 2014 by T.SUZUKI, All rights reserved.
*
* reference :
* [1] ublox-AG, GPS.G3-X-03002-D, ANTARIS Positioning Engine NMEA and UBX
* Protocol Specification, Version 5.00, 2003
* [2] ublox-AG, UBX-13003221-R03, u-blox M8 Receiver Description including
* Protocol Specification V5, Dec 20, 2013
* [3] ublox-AG, UBX-13003221-R07, u-blox M8 Receiver Description including
* Protocol Specification V15.00-17.00, Nov 3, 2014
* [4] ublox-AG, UBX-13003221-R09, u-blox 8 /u-blox M8 Receiver Description
* including Protocol Specification V15.00-18.00, January, 2016
* [5] ublox-AG, UBX-18010854-R08, u-blox ZED-F9P Interface Description,
* May, 2020
*
* version : $Revision: 1.2 $ $Date: 2008/07/14 00:05:05 $
* history : 2007/10/08 1.0 new
* 2008/06/16 1.1 separate common functions to rcvcmn.c
* 2009/04/01 1.2 add range check of prn number
* 2009/04/10 1.3 refactored
* 2009/09/25 1.4 add function gen_ubx()
* 2010/01/17 1.5 add time tag adjustment option -tadj sec
* 2010/10/31 1.6 fix bug on playback disabled for raw data (2.4.0_p9)
* 2011/05/27 1.7 add almanac decoding
* add -EPHALL option
* fix problem with ARM compiler
* 2013/02/23 1.8 fix memory access violation problem on arm
* change options -tadj to -TADJ, -invcp to -INVCP
* 2014/05/26 1.9 fix bug on message size of CFG-MSG
* fix bug on return code of decode_alm1()
* 2014/06/21 1.10 support message TRK-MEAS and TRK-SFRBX
* support message NAV-SOL and NAV-TIMEGPS to get time
* support message GFG-GNSS generation
* 2014/06/23 1.11 support message TRK-MEAS for beidou ephemeris
* 2014/08/11 1.12 fix bug on unable to read RXM-RAW
* fix problem on decoding glo ephemeris in TRK-SFRBX
* support message TRK-TRKD5
* 2014/08/31 1.13 suppress warning
* 2014/11/04 1.14 support message RXM-RAWX and RXM-SFRBX
* 2015/03/20 1.15 omit time adjustment for RXM-RAWX
* 2016/01/22 1.16 add time-tag in raw-message-type
* 2016/01/26 1.17 support galileo navigation data in RXM-SFRBX
* enable option -TADJ for RXM-RAWX
* 2016/05/25 1.18 fix bug on crc-buffer-overflow by decoding galileo
* navigation data
* 2016/07/04 1.19 add half-cycle vaild check for ubx-trk-meas
* 2016/07/29 1.20 support RXM-CFG-TMODE3 (0x06 0x71) for M8P
* crc24q() -> rtk_crc24q()
* check week number zero for ubx-rxm-raw and rawx
* 2016/08/20 1.21 add test of std-dev for carrier-phase valid
* 2016/08/26 1.22 add option -STD_SLIP to test slip by std-dev of cp
* fix on half-cyc valid for sbas in trkmeas
* 2017/04/11 1.23 (char *) -> (signed char *)
* fix bug on week handover in decode_trkmeas/trkd5()
* fix bug on prn for geo in decode_cnav()
* 2017/06/10 1.24 output half-cycle-subtracted flag
* 2018/10/09 1.25 support ZED-F9P according to [5]
* beidou C17 is handled as GEO (navigation D2).
* 2018/11/05 1.26 fix problem on missing QZSS L2C signal
* save signal in obs data by signal index
* suppress warning for cnav in ubx-rxm-sfrbx
* 2019/05/10 1.27 disable half-cyc-subtract flag on LLI for RXM-RAWX
* save galileo E5b data to obs index 2
* handle C17 as no-GEO (MEO/IGSO)
* 2020/11/30 1.28 update reference [5]
* support UBX-CFG-VALDEL,VALGET,VALSET
* support hex field format for ubx binary message
* add quality test for receiver time in decode_trkd5()
* add half cycle shift correction for BDS GEO
* delete receiver option -GALFNAV
* use API code2idx() and code2freq()
* support QZSS L1S (CODE_L1Z)
* CODE_L1I -> CODE_L2I for BDS B1I (RINEX 3.04)
* use integer types in stdint.h
*-----------------------------------------------------------------------------*/
#define _POSIX_C_SOURCE 200112L
#include "rtklib.h"
#define UBXSYNC1 0xB5 /* ubx message sync code 1 */
#define UBXSYNC2 0x62 /* ubx message sync code 2 */
#define UBXCFG 0x06 /* ubx message cfg-??? */
#define PREAMB_CNAV 0x8B /* cnav preamble */
#define ID_NAVSOL 0x0106 /* ubx message id: nav solution info */
#define ID_NAVTIME 0x0120 /* ubx message id: nav time gps */
#define ID_RXMRAW 0x0210 /* ubx message id: raw measurement data */
#define ID_RXMSFRB 0x0211 /* ubx message id: subframe buffer */
#define ID_RXMSFRBX 0x0213 /* ubx message id: raw subframe data */
#define ID_RXMRAWX 0x0215 /* ubx message id: multi-gnss raw meas data */
#define ID_TRKD5 0x030A /* ubx message id: trace mesurement data */
#define ID_TRKMEAS 0x0310 /* ubx message id: trace mesurement data */
#define ID_TRKSFRBX 0x030F /* ubx message id: trace subframe buffer */
#define ID_TIMTM2 0x0D03 /* ubx message id: time mark data */
#define FU1 1 /* ubx message field types */
#define FU2 2
#define FU4 3
#define FU8 4
#define FI1 5
#define FI2 6
#define FI4 7
#define FR4 8
#define FR8 9
#define FS32 10
typedef enum { false, true } bool;
#define P2_10 0.0009765625 /* 2^-10 */
/* max std-dev for valid carrier-phases */
#define MAX_CPSTD_VALID_GEN8 5 /* optimal value for Gen8 modules */
#define MAX_CPSTD_VALID_GEN9 14 /* optimal value for Gen9 modules */
#define CPSTD_SLIP 15 /* std-dev threshold for slip */
#define ROUND(x) (int)floor((x)+0.5)
/* get fields (little-endian) ------------------------------------------------*/
#define U1(p) (*((uint8_t *)(p)))
#define I1(p) (*((int8_t *)(p)))
static uint16_t U2(uint8_t *p) {uint16_t u; memcpy(&u,p,2); return u;}
static uint32_t U4(uint8_t *p) {uint32_t u; memcpy(&u,p,4); return u;}
static int32_t I4(uint8_t *p) {int32_t i; memcpy(&i,p,4); return i;}
static float R4(uint8_t *p) {float r; memcpy(&r,p,4); return r;}
static double R8(uint8_t *p) {double r; memcpy(&r,p,8); return r;}
static double I8(uint8_t *p) {return I4(p+4)*4294967296.0+U4(p);}
/* set fields (little-endian) ------------------------------------------------*/
static void setU1(uint8_t *p, uint8_t u) {*p=u;}
static void setU2(uint8_t *p, uint16_t u) {memcpy(p,&u,2);}
static void setU4(uint8_t *p, uint32_t u) {memcpy(p,&u,4);}
static void setI1(uint8_t *p, int8_t i) {*p=(uint8_t)i;}
static void setI2(uint8_t *p, int16_t i) {memcpy(p,&i,2);}
static void setI4(uint8_t *p, int32_t i) {memcpy(p,&i,4);}
static void setR4(uint8_t *p, float r) {memcpy(p,&r,4);}
static void setR8(uint8_t *p, double r) {memcpy(p,&r,8);}
/* checksum ------------------------------------------------------------------*/
static int checksum(uint8_t *buff, int len)
{
uint8_t cka=0,ckb=0;
int i;
for (i=2;i<len-2;i++) {
cka+=buff[i]; ckb+=cka;
}
return cka==buff[len-2]&&ckb==buff[len-1];
}
static void setcs(uint8_t *buff, int len)
{
uint8_t cka=0,ckb=0;
int i;
for (i=2;i<len-2;i++) {
cka+=buff[i]; ckb+=cka;
}
buff[len-2]=cka;
buff[len-1]=ckb;
}
/* UBX GNSSId to system (ref [2] 25) -----------------------------------------*/
static int ubx_sys(int gnssid)
{
switch (gnssid) {
case 0: return SYS_GPS;
case 1: return SYS_SBS;
case 2: return SYS_GAL;
case 3: return SYS_CMP;
case 5: return SYS_QZS;
case 6: return SYS_GLO;
case 7: return SYS_IRN;
}
return 0;
}
/* UBX SigId to signal (ref [5] 1.5.4) ---------------------------------------*/
static int ubx_sig(int sys, int sigid)
{
if (sys == SYS_GPS) {
if (sigid == 0) return CODE_L1C; /* L1C/A */
if (sigid==3) return CODE_L2L; /* L2CL */
if (sigid==4) return CODE_L2S; /* L2CM */
if (sigid==6) return CODE_L5I; /* L5I */
if (sigid==7) return CODE_L5Q; /* L5Q */
}
else if (sys == SYS_GLO) {
if (sigid == 0) return CODE_L1C; /* G1C/A (GLO L1 OF) */
if (sigid == 2) return CODE_L2C; /* G2C/A (GLO L2 OF) */
}
else if (sys == SYS_GAL) {
if (sigid==0) return CODE_L1C; /* E1C */
if (sigid==1) return CODE_L1B; /* E1B */
if (sigid==3) return CODE_L5I; /* E5aI */
if (sigid==4) return CODE_L5Q; /* E5aQ */
if (sigid==5) return CODE_L7I; /* E5bI */
if (sigid==6) return CODE_L7Q; /* E5bQ */
if (sigid==8) return CODE_L6B; /* E6B */
if (sigid==9) return CODE_L6C; /* E6C */
if (sigid==10) return CODE_L6A; /* E6A */
}
else if (sys == SYS_QZS) {
if (sigid==0) return CODE_L1C; /* L1C/A */
if (sigid==1) return CODE_L1Z; /* L1S */
if (sigid==4) return CODE_L2S; /* L2CM */
if (sigid==5) return CODE_L2L; /* L2CL */
if (sigid==8) return CODE_L5I; /* L5I */
if (sigid==9) return CODE_L5Q; /* L5Q */
if (sigid==12) return CODE_L1E; /* L1C/B */
}
else if (sys == SYS_CMP) {
if (sigid==0) return CODE_L2I; /* B1I D1 */
if (sigid==1) return CODE_L2I; /* B1I D2 */
if (sigid == 2) return CODE_L7I; /* B2I D1 */
if (sigid == 3) return CODE_L7I; /* B2I D2 */
if (sigid == 4) return CODE_L6I; /* B3I D1 */
if (sigid == 5) return CODE_L1P; /* B1 Cp */
if (sigid == 6) return CODE_L1D; /* B1 Cd */
if (sigid == 7) return CODE_L5P; /* B2 ap */
if (sigid == 8) return CODE_L5D; /* B2 ad */
if (sigid == 10) return CODE_L6I; /* B3I D2 */
}
else if (sys == SYS_IRN) {
if (sigid==0) return CODE_L5A; /* L5A */
}
else if (sys == SYS_SBS) {
if (sigid==0) return CODE_L1C; /* L1C/A */
}
return CODE_NONE;
}
/* UBX SigId to signal - combine codes ------------------------*/
static int ubx_sig_combined(int sys, int sigid)
{
if (sys == SYS_GPS) {
if (sigid == 0) return CODE_L1C; /* L1C/A */
if (sigid==3) return CODE_L2X; /* L2CL */
if (sigid==4) return CODE_L2X; /* L2CM */
if (sigid==6) return CODE_L5X; /* L5I */
if (sigid==7) return CODE_L5X; /* L5Q */
}
else if (sys == SYS_GLO) {
if (sigid == 0) return CODE_L1C; /* G1C/A (GLO L1 OF) */
if (sigid == 2) return CODE_L2C; /* G2C/A (GLO L2 OF) */
}
else if (sys == SYS_GAL) {
if (sigid==0) return CODE_L1X; /* E1C */
if (sigid==1) return CODE_L1X; /* E1B */
if (sigid==3) return CODE_L5X; /* E5aI */
if (sigid==4) return CODE_L5X; /* E5aQ */
if (sigid==5) return CODE_L7X; /* E5bI */
if (sigid==6) return CODE_L7X; /* E5bQ */
if (sigid==8) return CODE_L6X; /* E6B */
if (sigid==9) return CODE_L6X; /* E6C */
if (sigid==10) return CODE_L6X; /* E6A */
}
else if (sys == SYS_QZS) {
if (sigid == 0) return CODE_L1C; /* L1C/A */
if (sigid==1) return CODE_L1C; /* L1S */
if (sigid==4) return CODE_L2X; /* L2CM */
if (sigid==5) return CODE_L2X; /* L2CL */
if (sigid==8) return CODE_L5X; /* L5I */
if (sigid==9) return CODE_L5X; /* L5Q */
}
else if (sys == SYS_CMP) {
if (sigid==0) return CODE_L2I; /* B1I D1 */
if (sigid==1) return CODE_L2I; /* B1I D2 */
if (sigid == 2) return CODE_L7I; /* B2I D1 */
if (sigid == 3) return CODE_L7I; /* B2I D2 */
if (sigid == 4) return CODE_L6I; /* B3I D1 */
if (sigid == 5) return CODE_L1X; /* B1 Cp */
if (sigid == 6) return CODE_L1X; /* B1 Cd */
if (sigid == 7) return CODE_L5X; /* B2 ap */
if (sigid == 8) return CODE_L5X; /* B2 ad */
if (sigid == 10) return CODE_L6I; /* B3I D2 */
}
else if (sys == SYS_IRN) {
if (sigid==0) return CODE_L5A; /* L5A */
}
else if (sys == SYS_SBS) {
if (sigid==0) return CODE_L1C; /* L1C/A */
}
return CODE_NONE;
}
/* signal index in obs data --------------------------------------------------*/
static int sig_idx(int sys, uint8_t code)
{
int idx=code2idx(sys,code),nex=NEXOBS;
if (sys == SYS_GPS) {
if (code==CODE_L2S) return (nex<1)?-1:NFREQ; /* L2CM */
}
else if (sys == SYS_GAL) {
if (code==CODE_L1B) return (nex<1)?-1:NFREQ; /* E1B */
if (code==CODE_L7I) return (nex<2)?-1:NFREQ+1; /* E5bI */
}
else if (sys == SYS_QZS) {
if (code==CODE_L2S) return (nex<1)?-1:NFREQ; /* L2CM */
if (code==CODE_L1Z) return (nex<2)?-1:NFREQ+1; /* L1S */
}
return (idx<NFREQ)?idx:-1;
}
/* decode UBX-RXM-RAW: raw measurement data ----------------------------------*/
static int decode_rxmraw(raw_t *raw)
{
uint8_t *p=raw->buff+6;
gtime_t time;
double tow,tt,tadj=0.0,toff=0.0,tn;
int i,j,prn,sat,n=0,nsat,week;
char *q;
trace(4,"decode_rxmraw: len=%d\n",raw->len);
if (raw->outtype) {
sprintf(raw->msgtype,"UBX RXM-RAW (%4d): nsat=%d",raw->len,U1(p+6));
}
/* time tag adjustment option (-TADJ) */
if ((q=strstr(raw->opt,"-TADJ="))) {
sscanf(q,"-TADJ=%lf",&tadj);
}
nsat=U1(p+6);
if (raw->len<12+24*nsat) {
trace(2,"ubx rxmraw length error: len=%d nsat=%d\n",raw->len,nsat);
return -1;
}
tow =U4(p );
week=U2(p+4);
time=gpst2time(week,tow*0.001);
if (week==0) {
trace(3,"ubx rxmraw week=0 error: len=%d nsat=%d\n",raw->len,nsat);
return 0;
}
/* time tag adjustment */
if (tadj>0.0) {
tn=time2gpst(time,&week)/tadj;
toff=(tn-floor(tn+0.5))*tadj;
time=timeadd(time,-toff);
}
tt=timediff(time,raw->time);
for (i=0,p+=8;i<nsat&&i<MAXOBS;i++,p+=24) {
raw->obs.data[n].time=time;
raw->obs.data[n].L[0] =R8(p )-toff*FREQL1;
raw->obs.data[n].P[0] =R8(p+ 8)-toff*CLIGHT;
raw->obs.data[n].D[0] =R4(p+16);
prn =U1(p+20);
raw->obs.data[n].SNR[0]=I1(p+22);
raw->obs.data[n].LLI[0]=U1(p+23);
raw->obs.data[n].code[0]=CODE_L1C;
/* phase polarity flip option (-INVCP) */
if (strstr(raw->opt,"-INVCP")) {
raw->obs.data[n].L[0]=-raw->obs.data[n].L[0];
}
if (!(sat=satno(MINPRNSBS<=prn?SYS_SBS:SYS_GPS,prn))) {
trace(2,"ubx rxmraw sat number error: prn=%d\n",prn);
continue;
}
raw->obs.data[n].sat=sat;
if (raw->obs.data[n].LLI[0]&1) raw->lockt[sat-1][0]=0.0;
else if (tt<1.0||10.0<tt) raw->lockt[sat-1][0]=0.0;
else raw->lockt[sat-1][0]+=tt;
for (j=1;j<NFREQ+NEXOBS;j++) {
raw->obs.data[n].L[j]=raw->obs.data[n].P[j]=0.0;
raw->obs.data[n].D[j]=raw->obs.data[n].SNR[j]=0.0;
raw->obs.data[n].Lstd[j]=raw->obs.data[n].Pstd[j]=0.0;
raw->obs.data[n].LLI[j]=0;
raw->obs.data[n].code[j]=CODE_NONE;
}
n++;
}
raw->time=time;
raw->obs.n=n;
return 1;
}
/* decode UBX-RXM-RAWX: multi-GNSS raw measurement data (ref [3][4][5]) ------*/
static int decode_rxmrawx(raw_t *raw)
{
uint8_t *p=raw->buff+6;
gtime_t time;
char *q,tstr[40];
double tow,P,L,D,tn,tadj=0.0,toff=0.0;
int i,j,k,idx,sys,prn,sat,code,slip,halfv,halfc,LLI,n=0;
int week,nmeas,ver,gnss,svid,sigid,frqid,lockt,cn0,cpstd=0,prstd=0,tstat;
int multicode=0, rcvstds=0;
trace(4,"decode_rxmrawx: len=%d\n",raw->len);
if (raw->len<24) {
trace(2,"ubx rxmrawx length error: len=%d\n",raw->len);
return -1;
}
tow =R8(p ); /* rcvTow (s) */
week =U2(p+ 8); /* week */
nmeas=U1(p+11); /* numMeas */
ver =U1(p+13); /* version ([5] 5.15.3.1) */
if (raw->len<24+32*nmeas) {
trace(2,"ubx rxmrawx length error: len=%d nmeas=%d\n",raw->len,nmeas);
return -1;
}
if (week==0) {
trace(3,"ubx rxmrawx week=0 error: len=%d nmeas=%d\n",raw->len,nmeas);
return 0;
}
time=gpst2time(week,tow);
if (raw->outtype) {
time2str(time,tstr,2);
sprintf(raw->msgtype,"UBX RXM-RAWX (%4d): time=%s nmeas=%d ver=%d",
raw->len,tstr,nmeas,ver);
}
/* time tag adjustment option (-TADJ) */
if ((q=strstr(raw->opt,"-TADJ="))) {
sscanf(q,"-TADJ=%lf",&tadj);
}
/* max valid std-dev of carrier-phase (-MAX_STD_CP) */
int cpstd_valid;
if (raw->rcvtype == 1)
cpstd_valid = MAX_CPSTD_VALID_GEN9; /* F9P */
else
cpstd_valid = MAX_CPSTD_VALID_GEN8; /* M8T, M8P */
q = strstr(raw->opt, "-MAX_STD_CP=");
if (q) sscanf(q, "-MAX_STD_CP=%d", &cpstd_valid);
/* slip threshold of std-dev of carrier-phase (-STD_SLIP) */
int cpstd_slip = CPSTD_SLIP;
q = strstr(raw->opt, "-STD_SLIP=");
if (q) sscanf(q, "-STD_SLIP=%d", &cpstd_slip);
/* use multiple codes for each freq (-MULTICODE) */
if (strstr(raw->opt,"-MULTICODE")) multicode=1;
/* write rcvr stdevs to unused rinex fields */
if (strstr(raw->opt,"-RCVSTDS")) rcvstds=1;
/* time tag adjustment */
if (tadj>0.0) {
tn=time2gpst(time,&week)/tadj;
toff=(tn-floor(tn+0.5))*tadj;
time=timeadd(time,-toff);
}
for (i=0,p+=16;i<nmeas&&n<MAXOBS;i++,p+=32) {
P =R8(p ); /* prMes (m) */
L =R8(p+ 8); /* cpMes (cyc) */
D =R4(p+16); /* doMes (hz) */
gnss =U1(p+20); /* gnssId */
svid =U1(p+21); /* svId */
sigid=U1(p+22); /* sigId ([5] 5.15.3.1) */
frqid=U1(p+23); /* freqId (fcn + 7) */
lockt=U2(p+24); /* locktime (ms) */
cn0 =U1(p+26); /* cn0 (dBHz) */
prstd=U1(p+27)&15; /* pseudorange std-dev: (0.01*2^n meters) */
cpstd=U1(p+28)&15; /* cpStdev (n*0.004 m) */
tstat=U1(p+30); /* trkStat */
if (!(tstat&1)) P=0.0;
if (!(tstat&2)||L==-0.5||cpstd>cpstd_valid) L=0.0; /* invalid phase */
if (sigid>1) raw->rcvtype=1; /* flag as Gen9 receiver */
if (!(sys=ubx_sys(gnss))) {
trace(2,"ubx rxmrawx: system error gnss=%d\n", gnss);
continue;
}
prn=svid+(sys==SYS_QZS?192:0);
if (!(sat=satno(sys,prn))) {
if (sys==SYS_GLO&&prn==255) {
continue; /* suppress warning for unknown glo satellite */
}
trace(2,"ubx rxmrawx sat number error: sys=%2d prn=%2d\n",sys,prn);
continue;
}
if (sys==SYS_GLO&&!raw->nav.glo_fcn[prn-1]) {
raw->nav.glo_fcn[prn-1]=frqid-7+8;
}
if (ver>=1) {
if (multicode)
code=ubx_sig(sys,sigid);
else
code=ubx_sig_combined(sys,sigid);
}
else {
code=(sys==SYS_CMP)?CODE_L2I:((sys==SYS_GAL)?CODE_L1X:CODE_L1C);
}
/* signal index in obs data */
if ((idx=sig_idx(sys,code))<0) {
trace(2,"ubx rxmrawx signal error: sat=%2d sigid=%d\n",sat,sigid);
continue;
}
/* offset by time tag adjustment */
if (toff!=0.0) {
P-=P!=0.0?toff*CLIGHT:0.0;
L-=L!=0.0?toff*code2freq(sys,code,frqid-7):0.0;
}
/* half-cycle shift correction for BDS GEO */
if (sys==SYS_CMP&&(prn<=5||prn>=59)&&L!=0.0) {
L+=0.5;
}
if (sys==SYS_SBS)
halfv=lockt>8000?1:0; /* half-cycle valid */
else
halfv=(tstat&4)?1:0; /* half cycle valid */
halfc=(tstat&8)?1:0; /* half cycle subtracted from phase */
slip=lockt==0||lockt*1E-3<raw->lockt[sat-1][idx]||
halfc!=raw->halfc[sat-1][idx];
if (cpstd>=cpstd_slip) slip=LLI_SLIP;
if (slip) raw->lockflag[sat-1][idx]=slip;
raw->lockt[sat-1][idx]=lockt*1E-3;
/* LLI: bit0=slip,bit1=half-cycle-unresolved */
LLI=!halfv&&L!=0.0?LLI_HALFC:0;
/* half cycle adjusted */
LLI|=halfc?LLI_HALFA:0;
/* set cycle slip if half cycle subtract bit changed state */
LLI|=halfc!=raw->halfc[sat-1][idx]?LLI_SLIP:0;
raw->halfc[sat-1][idx]=halfc;
/* set cycle slip flag if first valid phase since slip */
if (L!=0.0) LLI|=raw->lockflag[sat-1][idx]>0.0?LLI_SLIP:0;
for (j=0;j<n;j++) {
if (raw->obs.data[j].sat==sat) break;
}
if (j>=n) {
raw->obs.data[n].time=time;
raw->obs.data[n].sat=sat;
raw->obs.data[n].rcv=0;
for (k=0;k<NFREQ+NEXOBS;k++) {
raw->obs.data[n].L[k]=raw->obs.data[n].P[k]=0.0;
raw->obs.data[n].D[k]=raw->obs.data[n].SNR[k]=0.0;
raw->obs.data[n].Lstd[k]=raw->obs.data[n].Pstd[k]=0.0;
raw->obs.data[n].LLI[k]=0;
raw->obs.data[n].code[k]=CODE_NONE;
}
n++;
}
raw->obs.data[j].L[idx]=L;
raw->obs.data[j].P[idx]=P;
raw->obs.data[j].Lstd[idx] = rcvstds ? cpstd * 0.004 : 0;
raw->obs.data[j].Pstd[idx] = rcvstds ? 0.01 * pow(2, prstd) : 0.0;
raw->obs.data[j].D[idx]=(float)D;
raw->obs.data[j].SNR[idx]=cn0;
raw->obs.data[j].LLI[idx]=(uint8_t)LLI;
raw->obs.data[j].code[idx]=(uint8_t)code;
if (L!=0.0) raw->lockflag[sat-1][idx]=0; /* clear slip carry-forward flag if valid phase*/
}
raw->time=time;
raw->obs.n=n;
return 1;
}
/* decode UBX-NAV-SOL: navigation solution -----------------------------------*/
static int decode_navsol(raw_t *raw)
{
uint8_t *p=raw->buff+6;
int itow,ftow,week;
trace(4,"decode_navsol: len=%d\n",raw->len);
if (raw->outtype) {
sprintf(raw->msgtype,"UBX NAV-SOL (%4d):",raw->len);
}
itow=U4(p);
ftow=I4(p+4);
week=U2(p+8);
if ((U1(p+11)&0x0C)==0x0C) {
raw->time=gpst2time(week,itow*1E-3+ftow*1E-9);
}
return 0;
}
/* decode UBX-NAV-TIMEGPS: GPS time solution ---------------------------------*/
static int decode_navtime(raw_t *raw)
{
int itow,ftow,week;
uint8_t *p=raw->buff+6;
trace(4,"decode_navtime: len=%d\n",raw->len);
if (raw->outtype) {
sprintf(raw->msgtype,"UBX NAV-TIME (%4d):",raw->len);
}
itow=U4(p);
ftow=I4(p+4);
week=U2(p+8);
if ((U1(p+11)&0x03)==0x03) {
raw->time=gpst2time(week,itow*1E-3+ftow*1E-9);
}
return 0;
}
/* decode UBX-TRK-MEAS: trace measurement data (unofficial) ------------------*/
static int decode_trkmeas(raw_t *raw)
{
static double adrs[MAXSAT]={0};
uint8_t *p=raw->buff+6;
gtime_t time;
double ts,tr=-1.0,t,tau,utc_gpst,snr,adr,dop;
int i,j,n=0,nch,sys,prn,sat,qi,frq,flag,lock1,lock2,week,fw=0;
char *q;
/* adjustment to code measurement in meters, based on GLONASS freq,
values based on difference between TRK_MEAS values and RXM-RAWX values */
const char P_adj_fw2[]={ 0, 0, 0, 0, 1, 3, 2, 0,-4,-3,-9,-8,-7,-4, 0}; /* fw 2.30 */
const char P_adj_fw3[]={11,13,13,14,14,13,12,10, 8, 6, 5, 5, 5, 7, 0}; /* fw 3.01 */
trace(4,"decode_trkmeas: len=%d\n",raw->len);
if (raw->outtype) {
sprintf(raw->msgtype,"UBX TRK-MEAS (%4d):",raw->len);
}
if (!raw->time.time) return 0;
/* trk meas code adjust (-TRKM_ADJ) */
if ((q=strstr(raw->opt,"-TRKM_ADJ="))) {
sscanf(q,"-TRKM_ADJ=%d",&fw);
}
/* number of channels */
nch=U1(p+2);
if (raw->len<112+nch*56) {
trace(2,"decode_trkmeas: length error len=%d nch=%2d\n",raw->len,nch);
return -1;
}
/* time-tag = max(transmission time + 0.08) rounded by 100 ms */
for (i=0,p=raw->buff+110;i<nch;i++,p+=56) {
if (U1(p+1)<4||ubx_sys(U1(p+4))!=SYS_GPS) continue;
if ((t=I8(p+24)*P2_32/1000.0)>tr) tr=t;
}
if (tr<0.0) return 0;
tr=ROUND((tr+0.08)/0.1)*0.1;
/* adjust week handover */
t=time2gpst(raw->time,&week);
if (tr<t-302400.0) week++;
else if (tr>t+302400.0) week--;
time=gpst2time(week,tr);
utc_gpst=timediff(gpst2utc(time),time);
int rcvstds = 0;
if (strstr(raw->opt,"-RCVSTDS")) rcvstds=1;
for (i=0,p=raw->buff+110;i<nch;i++,p+=56) {
/* quality indicator (0:idle,1:search,2:aquired,3:unusable, */
/* 4:code lock,5,6,7:code/carrier lock) */
qi=U1(p+1);
if (qi<4||7<qi) continue;
/* system and satellite number */
if (!(sys=ubx_sys(U1(p+4)))) {
trace(2,"ubx trkmeas: system error\n");
continue;
}
prn=U1(p+5)+(sys==SYS_QZS?192:0);
if (!(sat=satno(sys,prn))) {
trace(2,"ubx trkmeas sat number error: sys=%2d prn=%2d\n",sys,prn);
continue;
}
/* transmission time */
ts=I8(p+24)*P2_32/1000.0;
if (sys==SYS_CMP) ts+=14.0; /* bdt -> gpst */
else if (sys==SYS_GLO) ts-=10800.0+utc_gpst; /* glot -> gpst */
/* signal travel time */
tau=tr-ts;
if (tau<-302400.0) tau+=604800.0;
else if (tau> 302400.0) tau-=604800.0;
frq =U1(p+ 7)-7; /* frequency */
flag =U1(p+ 8); /* tracking status */
lock1=U1(p+16); /* code lock count */
lock2=U1(p+17); /* phase lock count */
snr =U2(p+20)/256.0;
adr =I8(p+32)*P2_32+(flag&0x40?0.5:0.0);
dop =I4(p+40)*P2_10*10.0;
/* set slip flag */
if (lock2==0||lock2<raw->lockt[sat-1][0]) raw->lockt[sat-1][1]=1.0;
raw->lockt[sat-1][0]=lock2;
#ifdef RTK_DISABLED /* for debug */
trace(2,"[%2d] qi=%d sys=%d prn=%3d frq=%2d flag=%02X ?=%02X %02X "
"%02X %02X %02X %02X %02X lock=%3d %3d ts=%10.3f snr=%4.1f "
"dop=%9.3f adr=%13.3f %6.3f\n",U1(p),qi,U1(p+4),prn,frq,flag,
U1(p+9),U1(p+10),U1(p+11),U1(p+12),U1(p+13),U1(p+14),U1(p+15),
lock1,lock2,ts,snr,dop,adr,
adrs[sat-1]==0.0||dop==0.0?0.0:(adr-adrs[sat-1])-dop);
#endif
adrs[sat-1]=adr;
/* check phase lock */
if (!(flag&0x20)) continue;
raw->obs.data[n].time=time;
raw->obs.data[n].sat=sat;
raw->obs.data[n].P[0]=tau*CLIGHT;
raw->obs.data[n].L[0]=-adr;
raw->obs.data[n].D[0]=(float)dop;
raw->obs.data[n].SNR[0]=snr;
raw->obs.data[n].code[0]=sys==SYS_CMP?CODE_L2I:CODE_L1C;
raw->obs.data[n].Lstd[0] = rcvstds ? (8 - qi) * 0.004 : 0;
raw->obs.data[n].LLI[0]=raw->lockt[sat-1][1]>0.0?1:0;
if (sys==SYS_SBS) { /* half-cycle valid */
raw->obs.data[n].LLI[0]|=lock2>142?0:2;
}
else {
raw->obs.data[n].LLI[0]|=flag&0x80?0:2;
}
raw->lockt[sat-1][1]=0.0;
/* adjust code measurements for GLONASS sats */
if (sys==SYS_GLO&&frq>=-7&&frq<=7) {
if (fw==2) raw->obs.data[n].P[0]+=(double)P_adj_fw2[frq+7];
if (fw==3) raw->obs.data[n].P[0]+=(double)P_adj_fw3[frq+7];
}
for (j=1;j<NFREQ+NEXOBS;j++) {
raw->obs.data[n].L[j]=raw->obs.data[n].P[j]=0.0;
raw->obs.data[n].D[j]=raw->obs.data[n].SNR[j]=0.0;
raw->obs.data[n].Lstd[j]=raw->obs.data[n].Pstd[j]=0.0;
raw->obs.data[n].LLI[j]=0;
raw->obs.data[n].code[j]=CODE_NONE;
}
n++;
}
if (n<=0) return 0;
raw->time=time;
raw->obs.n=n;
return 1;
}
/* decode UBX-TRKD5: trace measurement data (unofficial) ---------------------*/
static int decode_trkd5(raw_t *raw)
{
static double adrs[MAXSAT]={0};
gtime_t time;
double ts,tr=-1.0,t,tau,adr,dop,snr,utc_gpst;
int i,j,n=0,type,off,len,sys,prn,sat,qi,frq,flag,week;
uint8_t *p=raw->buff+6;
trace(4,"decode_trkd5: len=%d\n",raw->len);
if (raw->outtype) {
sprintf(raw->msgtype,"UBX TRK-D5 (%4d):",raw->len);
}
if (!raw->time.time) return 0;
utc_gpst=timediff(gpst2utc(raw->time),raw->time);
switch ((type=U1(p))) {
case 3 : off=86; len=56; break;
case 6 : off=86; len=64; break; /* u-blox 7 */
default: off=78; len=56; break;
}
for (i=0,p=raw->buff+off;p-raw->buff<raw->len-2;i++,p+=len) {
qi=U1(p+41)&7;
if (qi<4||7<qi) continue;
t=I8(p)*P2_32/1000.0;
if (ubx_sys(U1(p+56))==SYS_GLO) t-=10800.0+utc_gpst;
if (t>tr) {tr=t; break;}
}
if (tr<0.0) return 0;
tr=ROUND((tr+0.08)/0.1)*0.1;
/* adjust week handover */
t=time2gpst(raw->time,&week);
if (tr<t-302400.0) week++;
else if (tr>t+302400.0) week--;
time=gpst2time(week,tr);
char tstr[40];
trace(4,"time=%s\n",time2str(time,tstr,0));
for (i=0,p=raw->buff+off;p-raw->buff<raw->len-2;i++,p+=len) {
/* quality indicator */
qi =U1(p+41)&7;
if (qi<4||7<qi) continue;
if (type==6) {
if (!(sys=ubx_sys(U1(p+56)))) {
trace(2,"ubx trkd5: system error\n");
continue;
}
prn=U1(p+57)+(sys==SYS_QZS?192:0);
frq=U1(p+59)-7;
}
else {
prn=U1(p+34);
sys=prn<MINPRNSBS?SYS_GPS:SYS_SBS;
}
if (!(sat=satno(sys,prn))) {
trace(2,"ubx trkd5 sat number error: sys=%2d prn=%2d\n",sys,prn);
continue;
}
/* transmission time */
ts=I8(p)*P2_32/1000.0;
if (sys==SYS_GLO) ts-=10800.0+utc_gpst; /* glot -> gpst */
/* signal travel time */
tau=tr-ts;
if (tau<-302400.0) tau+=604800.0;
else if (tau> 302400.0) tau-=604800.0;
flag=U1(p+54); /* tracking status */
adr=qi<6?0.0:I8(p+8)*P2_32+(flag&0x01?0.5:0.0);
dop=I4(p+16)*P2_10/4.0;
snr=U2(p+32)/256.0;
if (snr<=10.0) raw->lockt[sat-1][1]=1.0;
#ifdef RTK_DISABLED /* for debug */
trace(2,"[%2d] qi=%d sys=%d prn=%3d frq=%2d flag=%02X ts=%1.3f "
"snr=%4.1f dop=%9.3f adr=%13.3f %6.3f\n",U1(p+35),qi,U1(p+56),
prn,frq,flag,ts,snr,dop,adr,
adrs[sat-1]==0.0||dop==0.0?0.0:(adr-adrs[sat-1])-dop);
#endif
adrs[sat-1]=adr;
/* check phase lock */
if (!(flag&0x08)) continue;
raw->obs.data[n].time=time;
raw->obs.data[n].sat=sat;
raw->obs.data[n].P[0]=tau*CLIGHT;
raw->obs.data[n].L[0]=-adr;
raw->obs.data[n].D[0]=(float)dop;
raw->obs.data[n].SNR[0]=snr;
raw->obs.data[n].code[0]=sys==SYS_CMP?CODE_L2I:CODE_L1C;
raw->obs.data[n].LLI[0]=raw->lockt[sat-1][1]>0.0?1:0;
raw->lockt[sat-1][1]=0.0;
for (j=1;j<NFREQ+NEXOBS;j++) {
raw->obs.data[n].L[j]=raw->obs.data[n].P[j]=0.0;
raw->obs.data[n].D[j]=raw->obs.data[n].SNR[j]=0.0;
raw->obs.data[n].LLI[j]=0;
raw->obs.data[n].code[j]=CODE_NONE;
}
n++;
}
if (n<=0) return 0;
raw->time=time;
raw->obs.n=n;
return 1;
}
/* UTC 8-bit week -> full week -----------------------------------------------*/
static void adj_utcweek(gtime_t time, double *utc)
{
int week;
time2gpst(time,&week);
utc[3]+=week/256*256;
if (utc[3]<week-127) utc[3]+=256.0;
else if (utc[3]>week+127) utc[3]-=256.0;
utc[5]+=utc[3]/256*256;
if (utc[5]<utc[3]-127) utc[5]+=256.0;
else if (utc[5]>utc[3]+127) utc[5]-=256.0;
}
/* decode GPS/QZSS ephemeris -------------------------------------------------*/
static int decode_eph(raw_t *raw, int sat)
{
eph_t eph={0};
int sys = satsys(sat, NULL);
if (!decode_frame(raw->subfrm[sat-1],sys,&eph,NULL,NULL,NULL)) return 0;
if (!strstr(raw->opt,"-EPHALL")) {
if (eph.iode==raw->nav.eph[sat-1].iode&&
eph.iodc==raw->nav.eph[sat-1].iodc&&
timediff(eph.toe,raw->nav.eph[sat-1].toe)==0.0&&
timediff(eph.toc,raw->nav.eph[sat-1].toc)==0.0) return 0;
}
eph.sat=sat;
raw->nav.eph[sat-1]=eph;
raw->ephsat=sat;
raw->ephset=0;
return 2;
}
/* decode GPS/QZSS ION/UTC parameters ----------------------------------------*/
static int decode_ionutc(raw_t *raw, int sat)
{
double ion[8],utc[8];
int sys=satsys(sat,NULL);
if (!decode_frame(raw->subfrm[sat-1],sys,NULL,NULL,ion,utc)) return 0;
adj_utcweek(raw->time,utc);
if (sys==SYS_QZS) {
matcpy(raw->nav.ion_qzs,ion,8,1);
matcpy(raw->nav.utc_qzs,utc,8,1);
}
else {
matcpy(raw->nav.ion_gps,ion,8,1);
matcpy(raw->nav.utc_gps,utc,8,1);
}
return 9;
}
/* decode GPS/QZSS navigation data -------------------------------------------*/
static int decode_nav(raw_t *raw, int sat, int off)
{
uint8_t *p=raw->buff+6+off,buff[30];
int i,id,ret;
if (raw->len<48+off) {
trace(2,"ubx rxmsfrbx nav length error: sat=%d len=%d\n",sat,raw->len);
return -1;
}
if ((U4(p)>>24)==PREAMB_CNAV) {
trace(3,"ubx rxmsfrbx nav unsupported sat=%d len=%d\n",sat,raw->len);
return 0;
}
for (i=0;i<10;i++,p+=4) { /* 24 x 10 bits w/o parity */
setbitu(buff,24*i,24,U4(p)>>6);
}
id=getbitu(buff,43,3);
if (id<1||id>5) {
trace(2,"ubx rxmsfrbx nav subframe id error: sat=%d id=%d\n",sat,id);
return -1;
}
memcpy(raw->subfrm[sat-1]+(id-1)*30,buff,30);
if (id==3) {
return decode_eph(raw,sat);
}
if (id==4||id==5) {
ret=decode_ionutc(raw,sat);
memset(raw->subfrm[sat-1]+(id-1)*30,0,30);
return ret;
}
return 0;
}
/* decode Galileo I/NAV navigation data --------------------------------------*/
static int decode_inav(raw_t *raw, int sat, int off)
{
if (strstr(raw->opt, "-GALFNAV")) return 0;
eph_t eph={0};
double ion[4]={0},utc[8]={0};
uint8_t *p=raw->buff+6+off,buff[32],crc_buff[26]={0};
int i,j,part1,page1,part2,page2,type;
if (raw->len<40+off) {
trace(2,"ubx rxmsfrbx inav length error: sat=%d len=%d\n",sat,raw->len);
return -1;
}
if (raw->len<36+off) return 0; /* E5b I/NAV */
for (i=0;i<8;i++,p+=4) {
setbitu(buff,32*i,32,U4(p));
}
part1=getbitu(buff ,0,1);
page1=getbitu(buff ,1,1);
part2=getbitu(buff,128,1);
page2=getbitu(buff,129,1);
if (part1!=0||part2!=1) {
trace(3,"ubx rxmsfrbx inav page even/odd error: sat=%d\n",sat);
return -1;
}
if (page1==1||page2==1) return 0; /* alert page */
/* test crc (4(pad) + 114 + 82 bits) */
for (i=0,j= 4;i<15;i++,j+=8) setbitu(crc_buff,j,8,getbitu(buff ,i*8,8));
for (i=0,j=118;i<11;i++,j+=8) setbitu(crc_buff,j,8,getbitu(buff,i*8+128,8));
if (rtk_crc24q(crc_buff,25)!=getbitu(buff,128+82,24)) {
trace(2,"ubx rxmsfrbx inav crc error: sat=%d\n",sat);
return -1;
}
type=getbitu(buff,2,6); /* word type */
if (type>6) return 0;
/* save 128 (112:even+16:odd) bits word */
for (i=0,j=2;i<14;i++,j+=8) {
raw->subfrm[sat-1][type*16+i]=getbitu(buff,j,8);
}
for (i=14,j=130;i<16;i++,j+=8) {
raw->subfrm[sat-1][type*16+i]=getbitu(buff,j,8);
}
if (type!=5) return 0;
if (!decode_gal_inav(raw->subfrm[sat-1],&eph,ion,utc)) return 0;
if (eph.sat!=sat) {
trace(2,"ubx rxmsfrbx inav satellite error: sat=%d %d\n",sat,eph.sat);
return -1;
}
eph.code|=(1<<0); /* data source: E1 */
adj_utcweek(raw->time,utc);
matcpy(raw->nav.ion_gal,ion,4,1);
matcpy(raw->nav.utc_gal,utc,8,1);
if (!strstr(raw->opt,"-EPHALL")) {
if (eph.iode==raw->nav.eph[sat-1].iode&&
timediff(eph.toe,raw->nav.eph[sat-1].toe)==0.0&&
timediff(eph.toc,raw->nav.eph[sat-1].toc)==0.0) return 0;
}
raw->nav.eph[sat-1]=eph;
raw->ephsat=sat;
raw->ephset=0; /* 0:I/NAV */
return 2;
}
// Decode Galileo F/NAV navigation data --------------------------------------
static int decode_fnav(raw_t *raw, int sat, int off)
{
if (strstr(raw->opt, "-GALINAV")) return 0;