-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathublox.c
2232 lines (2013 loc) · 71.2 KB
/
ublox.c
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-2018 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-R04, u-blox ZED-F9P Interface Description,
* September, 2018
*
* 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)
*-----------------------------------------------------------------------------*/
#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_NAVATT 0x0105 /* Attitude */
#define ID_NAVTIME 0x0120 /* ubx message id: nav time gps */
#define ID_NAVPVT 0x0107 /* ubx message id: nav position velocity time */
#define ID_ESFMEAS 0x1002 /* ubx message id: esf external sensor fusion */
#define ID_ESFRAW 0x1003 /* ubx message id: raw sensor measurements */
#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 ID_HNRPVT 0x2800 /* ubx message id: hnr pvt data */
#define ID_HNRINS 0x2802 /* ubx message id: hnr ins data */
#define FU1 1 /* ubx message field types */
#define FU2 2
#define FU4 3
#define FI1 4
#define FI2 5
#define FI4 6
#define FR4 7
#define FR8 8
#define FS32 9
#define P2_10 0.0009765625 /* 2^-10 */
#define CPSTD_VALID 5 /* std-dev threshold of carrier-phase valid */
#define ROUND(x) (int)floor((x)+0.5)
/* get fields (little-endian) ------------------------------------------------*/
#define U1(p) (*((unsigned char *)(p)))
#define I1(p) (*((signed char *)(p)))
static unsigned short U2(unsigned char *p) { unsigned short u; memcpy(&u, p, 2); return u; }
static unsigned int U4(unsigned char *p) { unsigned int u; memcpy(&u, p, 4); return u; }
static short I2(unsigned char* p) { short u; memcpy(&u, p, 2); return u; }
static int I4(unsigned char *p) { int u; memcpy(&u, p, 4); return u; }
static float R4(unsigned char *p) { float r; memcpy(&r, p, 4); return r; }
static double R8(unsigned char *p) { double r; memcpy(&r, p, 8); return r; }
static double I8(unsigned char *p) { return I4(p + 4)*4294967296.0 + U4(p); }
/* set fields (little-endian) ------------------------------------------------*/
static void setU1(unsigned char *p, unsigned char u) { *p = u; }
static void setU2(unsigned char *p, unsigned short u) { memcpy(p, &u, 2); }
static void setU4(unsigned char *p, unsigned int u) { memcpy(p, &u, 4); }
static void setI1(unsigned char *p, signed char i) { *p = (unsigned char)i; }
static void setI2(unsigned char *p, short i) { memcpy(p, &i, 2); }
static void setI4(unsigned char *p, int i) { memcpy(p, &i, 4); }
static void setR4(unsigned char *p, float r) { memcpy(p, &r, 4); }
static void setR8(unsigned char *p, double r) { memcpy(p, &r, 8); }
//static FILE* fpimu = NULL;
/* checksum ------------------------------------------------------------------*/
static int checksum(unsigned char *buff, int len)
{
unsigned char 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(unsigned char *buff, int len)
{
unsigned char 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;
}
return 0;
}
/* ubx sigid to signal ([5] Appendix B) --------------------------------------*/
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_L2M; /* L2CM */
}
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 == 5) return CODE_L7I; /* E5bI */
if (sigid == 6) return CODE_L7Q; /* E5bQ */
}
else if (sys == SYS_QZS) {
if (sigid == 0) return CODE_L1C; /* L1C/A */
if (sigid == 5) return CODE_L2L; /* L2CL (not specified in [5]) */
}
else if (sys == SYS_CMP) {
if (sigid == 0) return CODE_L2I; /* B1I D1 (rinex 3.03) */
if (sigid == 1) return CODE_L2I; /* B1I D2 (rinex 3.03) */
if (sigid == 2) return CODE_L7I; /* B2I D1 */
if (sigid == 3) return CODE_L7I; /* B2I D2 */
}
else if (sys == SYS_SBS) {
return CODE_L1C; /* L1C/A (not in [5]) */
}
return CODE_NONE;
}
/* signal index in obs data --------------------------------------------------*/
static int sig_idx(int sys, int code)
{
if (sys == SYS_GPS) {
if (code == CODE_L1C) return 1;
if (code == CODE_L2L) return 2;
if (code == CODE_L2M) return 2;
}
else if (sys == SYS_GLO) {
if (code == CODE_L1C) return 1;
if (code == CODE_L2C) return 2;
}
else if (sys == SYS_GAL) {
if (code == CODE_L1C) return 1;
if (code == CODE_L1B) return 1;
if (code == CODE_L7I) return 2; /* E5bI */
if (code == CODE_L7Q) return 2; /* E5bQ */
}
else if (sys == SYS_QZS) {
if (code == CODE_L1C) return 1;
if (code == CODE_L2L) return 2;
}
else if (sys == SYS_CMP) {
if (code == CODE_L1I || code == CODE_L2I) return 1;
if (code == CODE_L7I) return 2;
}
else if (sys == SYS_SBS) {
if (code == CODE_L1C) return 1;
}
return 0;
}
/* freq index to frequency ---------------------------------------------------*/
static double sig_freq(int sys, int f, int fcn)
{
static const double freq_glo[8] = { FREQ1_GLO,FREQ2_GLO,FREQ3_GLO };
static const double dfrq_glo[8] = { DFRQ1_GLO,DFRQ2_GLO };
static const double freq_bds[8] = { FREQ1_CMP,FREQ2_CMP,FREQ3_CMP };
if (sys == SYS_GLO) {
return freq_glo[f - 1] + dfrq_glo[f - 1] * fcn;
}
else if (sys == SYS_CMP) {
return freq_bds[f - 1];
}
return CLIGHT / lam_carr[f - 1];
}
/* 8-bit week -> full week ---------------------------------------------------*/
static void adj_utcweek(gtime_t time, double *utc)
{
int week;
if (utc[3] >= 256.0) return;
time2gpst(time, &week);
utc[3] += week / 256 * 256;
if (utc[3] < week - 128) utc[3] += 256.0;
else if (utc[3] > week + 128) utc[3] -= 256.0;
}
/* decode ubx-rxm-raw: raw measurement data ----------------------------------*/
static int decode_rxmraw(raw_t *raw)
{
gtime_t time;
double tow, tt, tadj = 0.0, toff = 0.0, tn;
int i, j, prn, sat, n = 0, nsat, week;
unsigned char *p = raw->buff + 6;
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] = (unsigned char)(I1(p + 22)*4.0 + 0.5);
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] = 0.0;
raw->obs.data[n].SNR[j] = raw->obs.data[n].LLI[j] = 0;
raw->obs.data[n].qualL[j] = raw->obs.data[n].qualP[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)
{
gtime_t time;
unsigned char *p = raw->buff + 6;
char *q, tstr[64];
double tow, P, L, D, tn, tadj = 0.0, toff = 0.0;
int i, j, k, f, sys, prn, sat, code, slip, halfv, halfc, LLI, n = 0, std_slip = 0;
int week, nmeas, ver, gnss, svid, sigid, frqid, lockt, cn0, cpstd, prstd, tstat;
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);
}
/* slip threshold of std-dev of carrier-phase (-STD_SLIP) */
if ((q = strstr(raw->opt, "-STD_SLIP="))) {
sscanf(q, "-STD_SLIP=%d", &std_slip);
}
/* 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 */
cpstd = U1(p + 28) & 15; /* cpStdev (m) */
prstd = 1 << (prstd >= 5 ? prstd - 5 : 0); /* prstd=2^(x-5) */
prstd = prstd <= 9 ? prstd : 9; /* limit to 9 to fit RINEX format */
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 (!(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 (ver >= 1) {
code = ubx_sig(sys, sigid);
}
else {
code = (sys == SYS_CMP) ? CODE_L2I : ((sys == SYS_GAL) ? CODE_L1X : CODE_L1C);
}
/* signal index in obs data */
f = sig_idx(sys, code);
if (f == 0 || f > NFREQ + NEXOBS) {
trace(2, "ubx rxmrawx signal error: sat=%2d sigid=%d\n", sat, sigid);
continue;
}
/* offset by time tag adjustment */
if (toff != 0.0&&L != 0.0) {
P -= toff * CLIGHT;
L -= toff * sig_freq(sys, f, frqid - 7);
}
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][f - 1] ||
halfc != raw->halfc[sat - 1][f - 1];
if (std_slip > 0 && cpstd >= std_slip) slip = LLI_SLIP;
if (slip) raw->lockflag[sat - 1][f - 1] = slip;
raw->lockt[sat - 1][f - 1] = lockt * 1E-3;
raw->halfc[sat - 1][f - 1] = halfc;
/* LLI: bit1=slip,bit2=half-cycle-invalid */
LLI = !halfv&&L != 0.0 ? LLI_HALFC : 0;
LLI |= halfc != raw->halfc[sat - 1][f - 1] ? 1 : 0;
if (L != 0.0) LLI |= raw->lockflag[sat - 1][f - 1] > 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].qualL[k] = raw->obs.data[n].qualP[k] = 0.0;
raw->obs.data[n].D[k] = 0.0;
raw->obs.data[n].SNR[k] = raw->obs.data[n].LLI[k] = 0;
raw->obs.data[n].code[k] = CODE_NONE;
}
n++;
}
raw->obs.data[j].L[f - 1] = L;
raw->obs.data[j].P[f - 1] = P;
raw->obs.data[j].qualL[f - 1] = cpstd <= 7 ? cpstd : 0;
raw->obs.data[j].qualP[f - 1] = prstd;
raw->obs.data[j].D[f - 1] = (float)D;
raw->obs.data[j].SNR[f - 1] = (unsigned char)(cn0 * 4);
raw->obs.data[j].LLI[f - 1] = (unsigned char)LLI;
raw->obs.data[j].code[f - 1] = (unsigned char)code;
if (L != 0.0) raw->lockflag[sat - 1][f - 1] = 0;
}
raw->time = time;
raw->obs.n = n;
return 1;
}
/* save subframe -------------------------------------------------------------*/
static int save_subfrm(int sat, raw_t *raw)
{
unsigned char *p = raw->buff + 6, *q;
int i, j, n, id = (U4(p + 6) >> 2) & 0x7;
trace(4, "save_subfrm: sat=%2d id=%d\n", sat, id);
if (id < 1 || 5 < id) return 0;
q = raw->subfrm[sat - 1] + (id - 1) * 30;
for (i = n = 0, p += 2; i < 10; i++, p += 4) {
for (j = 23; j >= 0; j--) {
*q = (*q << 1) + ((U4(p) >> j) & 1); if (++n % 8 == 0) q++;
}
}
return id;
}
/* decode ephemeris ----------------------------------------------------------*/
static int decode_ephem(int sat, raw_t *raw)
{
eph_t eph = { 0 };
trace(4, "decode_ephem: sat=%2d\n", sat);
if (decode_frame(raw->subfrm[sat - 1], &eph, NULL, NULL, NULL, NULL) != 1 ||
decode_frame(raw->subfrm[sat - 1] + 30, &eph, NULL, NULL, NULL, NULL) != 2 ||
decode_frame(raw->subfrm[sat - 1] + 60, &eph, NULL, NULL, NULL, NULL) != 3) return 0;
if (!strstr(raw->opt, "-EPHALL")) {
if (eph.iode == raw->nav.eph[sat - 1].iode&&
eph.iodc == raw->nav.eph[sat - 1].iodc) return 0; /* unchanged */
}
eph.sat = sat;
raw->nav.eph[sat - 1] = eph;
raw->ephsat = sat;
return 2;
}
/* decode almanac and ion/utc ------------------------------------------------*/
static int decode_alm1(int sat, raw_t *raw)
{
int sys = satsys(sat, NULL);
trace(4, "decode_alm1 : sat=%2d\n", sat);
if (sys == SYS_GPS) {
decode_frame(raw->subfrm[sat - 1] + 90, NULL, raw->nav.alm, raw->nav.ion_gps,
raw->nav.utc_gps, &raw->nav.leaps);
adj_utcweek(raw->time, raw->nav.utc_gps);
}
else if (sys == SYS_QZS) {
decode_frame(raw->subfrm[sat - 1] + 90, NULL, raw->nav.alm, raw->nav.ion_qzs,
raw->nav.utc_qzs, &raw->nav.leaps);
adj_utcweek(raw->time, raw->nav.utc_qzs);
}
return 9;
}
/* decode almanac ------------------------------------------------------------*/
static int decode_alm2(int sat, raw_t *raw)
{
int sys = satsys(sat, NULL);
trace(4, "decode_alm2 : sat=%2d\n", sat);
if (sys == SYS_GPS) {
decode_frame(raw->subfrm[sat - 1] + 120, NULL, raw->nav.alm, NULL, NULL, NULL);
}
else if (sys == SYS_QZS) {
decode_frame(raw->subfrm[sat - 1] + 120, NULL, raw->nav.alm, raw->nav.ion_qzs,
raw->nav.utc_qzs, &raw->nav.leaps);
adj_utcweek(raw->time, raw->nav.utc_qzs);
}
return 0;
}
/* decode ubx-rxm-sfrb: subframe buffer --------------------------------------*/
static int decode_rxmsfrb(raw_t *raw)
{
unsigned int words[10];
int i, prn, sat, sys, id;
unsigned char *p = raw->buff + 6;
trace(4, "decode_rxmsfrb: len=%d\n", raw->len);
if (raw->outtype) {
sprintf(raw->msgtype, "UBX RXM-SFRB (%4d): prn=%2d", raw->len, U1(p + 1));
}
if (raw->len < 42) {
trace(2, "ubx rxmsfrb length error: len=%d\n", raw->len);
return -1;
}
prn = U1(p + 1);
if (!(sat = satno(MINPRNSBS <= prn ? SYS_SBS : SYS_GPS, prn))) {
trace(2, "ubx rxmsfrb satellite number error: prn=%d\n", prn);
return -1;
}
sys = satsys(sat, &prn);
if (sys == SYS_GPS) {
id = save_subfrm(sat, raw);
if (id == 3) return decode_ephem(sat, raw);
if (id == 4) return decode_alm1(sat, raw);
if (id == 5) return decode_alm2(sat, raw);
return 0;
}
else if (sys == SYS_SBS) {
for (i = 0, p += 2; i < 10; i++, p += 4) words[i] = U4(p);
return sbsdecodemsg(raw->time, prn, words, &raw->sbsmsg) ? 3 : 0;
}
return 0;
}
/* decode ubx-nav-sol: navigation solution -----------------------------------*/
static int decode_navsol(raw_t *raw)
{
int itow, ftow, week;
unsigned char *p = raw->buff + 6;
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;
unsigned char *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 -------------------------------*/
static int decode_trkmeas(raw_t *raw)
{
static double adrs[MAXSAT] = { 0 };
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;
unsigned char *p = raw->buff + 6;
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);
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;
#if 0 /* 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] = (unsigned char)(snr*4.0);
raw->obs.data[n].code[0] = sys == SYS_CMP ? CODE_L2I : CODE_L1C;
raw->obs.data[n].qualL[0] = 8 - qi;
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] = 0.0;
raw->obs.data[n].SNR[j] = raw->obs.data[n].LLI[j] = 0;
raw->obs.data[n].qualL[j] = raw->obs.data[n].qualP[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 ----------------------------------*/
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;
unsigned char *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) {
if (U1(p + 41) < 4) 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;
}
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);
trace(4, "time=%s\n", time_str(time, 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;
#if 0 /* 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] = (unsigned char)(snr*4.0);
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] = 0.0;
raw->obs.data[n].SNR[j] = 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 gps and qzss navigation data ---------------------------------------*/
static int decode_nav(raw_t *raw, int sat, int off)
{
unsigned int words[10];
int i, id;
unsigned char *p = raw->buff + 6 + off;
if (raw->len < 48 + off) {
trace(2, "ubx rawsfrbx length error: sat=%d len=%d\n", sat, raw->len);
return -1;
}
if ((U4(p) >> 24) == PREAMB_CNAV) {
trace(3, "ubx rawsfrbx cnav not supported sat=%d prn=%d\n", sat,
(U4(p) >> 18) & 0x3F);
return 0;
}
for (i = 0; i < 10; i++, p += 4) words[i] = U4(p) >> 6; /* 24 bits without parity */
id = (words[1] >> 2) & 7;
if (id < 1 || 5 < id) {
trace(2, "ubx rawsfrbx subfrm id error: sat=%2d id=%d len=%d\n", sat, id,
raw->len);
return -1;
}
for (i = 0; i < 10; i++) {
setbitu(raw->subfrm[sat - 1] + (id - 1) * 30, i * 24, 24, words[i]);
}
if (id == 3) return decode_ephem(sat, raw);
if (id == 4) return decode_alm1(sat, raw);
if (id == 5) return decode_alm2(sat, raw);
return 0;
}
/* decode galileo navigation data --------------------------------------------*/
static int decode_enav(raw_t *raw, int sat, int off)
{
eph_t eph = { 0 };
unsigned char *p = raw->buff + 6 + off, buff[32], crc_buff[26] = { 0 };
int i, j, k, part1, page1, part2, page2, type;
if (raw->len < 44 + off) {
trace(2, "ubx rawsfrbx length error: sat=%d len=%d\n", sat, raw->len);
return -1;
}
for (i = k = 0; i < 8; i++, p += 4) for (j = 0; j < 4; j++) {
buff[k++] = p[3 - j];
}
part1 = getbitu(buff, 0, 1);
page1 = getbitu(buff, 1, 1);
part2 = getbitu(buff + 16, 0, 1);
page2 = getbitu(buff + 16, 1, 1);
/* skip alert page */
if (page1 == 1 || page2 == 1) return 0;
/* test even-odd parts */
if (part1 != 0 || part2 != 1) {
trace(2, "ubx rawsfrbx gal page even/odd error: sat=%2d\n", sat);
return -1;
}
/* 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 + 16, i * 8, 8));
if (rtk_crc24q(crc_buff, 25) != getbitu(buff + 16, 82, 24)) {
trace(2, "ubx rawsfrbx gal page crc error: sat=%2d\n", sat);
return -1;
}
type = getbitu(buff, 2, 6); /* word type */
/* skip word except for ephemeris, iono, utc parameters */
if (type > 6) return 0;
/* clear word 0-6 flags */
if (type == 2) raw->subfrm[sat - 1][112] = 0;
/* save page data (112 + 16 bits) to frame buffer */
k = type * 16;
for (i = 0, j = 2; i < 14; i++, j += 8) raw->subfrm[sat - 1][k++] = getbitu(buff, j, 8);
for (i = 0, j = 2; i < 2; i++, j += 8) raw->subfrm[sat - 1][k++] = getbitu(buff + 16, j, 8);
/* test word 0-6 flags */
raw->subfrm[sat - 1][112] |= (1 << type);
if (raw->subfrm[sat - 1][112] != 0x7F) return 0;
if (strstr(raw->opt, "-GALFNAV")) {
return 0;
}
/* decode galileo inav ephemeris */
if (!decode_gal_inav(raw->subfrm[sat - 1], &eph)) {
return 0;
}
/* test svid consistency */
if (eph.sat != sat) {
trace(2, "ubx rawsfrbx gal svid error: sat=%2d %2d\n", sat, eph.sat);
return -1;
}
if (!strstr(raw->opt, "-EPHALL")) {
if (eph.iode == raw->nav.eph[sat - 1].iode&& /* unchanged */
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;
return 2;
}
/* decode beidou navigation data ---------------------------------------------*/
static int decode_cnav(raw_t *raw, int sat, int off)
{
eph_t eph = { 0 };
unsigned int words[10];
int i, id, pgn, prn;
unsigned char *p = raw->buff + 6 + off;
if (raw->len < 48 + off) {
trace(2, "ubx rawsfrbx length error: sat=%d len=%d\n", sat, raw->len);
return -1;
}
for (i = 0; i < 10; i++, p += 4) words[i] = U4(p) & 0x3FFFFFFF; /* 30 bits */
satsys(sat, &prn);
id = (words[0] >> 12) & 0x07; /* subframe id (3bit) */
if (id < 1 || 5 < id) {