forked from tomojitakasu/RTKLIB
-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathcomnav.c
More file actions
1142 lines (1035 loc) · 38.7 KB
/
Copy pathcomnav.c
File metadata and controls
1142 lines (1035 loc) · 38.7 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
/*------------------------------------------------------------------------------
* comnav.c : ComNav receiver functions
*
* Copyright (C) 2007-2017 by T.TAKASU, All rights reserved.
*
* reference :
* [1] ComNav, CNT-OEM-RM001, Rev 1.5 COMNAV OEM BOARD REFERENCE MANUAL, 2018
*
* history : 2007/10/08 1.0 new
*-----------------------------------------------------------------------------*/
#include "rtklib.h"
#define CNAVSYNC1 0xAA /* cnav message start sync code 1 */
#define CNAVSYNC2 0x44 /* cnav message start sync code 2 */
#define CNAVSYNC3 0x12 /* cnav message start sync code 3 */
#define CNAVHLEN 28 /* cnav message header length (bytes) */
#define ID_ALMANAC 73 /* message id: cnav decoded almanac */
#define ID_GLOALMANAC 718 /* message id: cnav glonass decoded almanac */
#define ID_GLOEPHEMERIS 723 /* message id: cnav glonass ephemeris */
#define ID_IONUTC 8 /* message id: cnav iono and utc data */
#define ID_RANGE 43 /* message id: cnav range measurement */
#define ID_RANGECMP 140 /* message id: cnav range compressed */
#define ID_RAWALM 74 /* message id: cnav raw almanac */
#define ID_RAWEPHEM 41 /* message id: cnav raw ephemeris */
#define ID_RAWWAASFRAME 287 /* message id: cnav raw waas frame */
#define ID_QZSSIONUTC 1347 /* message id: oem6 qzss ion/utc parameters */
#define ID_QZSSRAWEPHEM 1330 /* message id: oem6 qzss raw ephemeris */
#define ID_QZSSRAWSUBFRAME 1331 /* message id: oem6 qzss raw subframe */
#define ID_RAWSBASFRAME 973 /* message id: oem6 raw sbas frame */
#define ID_GALEPHEMERIS 1122 /* message id: oem6 decoded galileo ephemeris */
#define ID_GALALMANAC 1120 /* message id: oem6 decoded galileo almanac */
#define ID_GALCLOCK 1121 /* message id: oem6 galileo clockinformation */
#define ID_GALIONO 1127 /* message id: oem6 decoded galileo iono corrections */
#define ID_GALFNAVRAWPAGE 1413 /* message id: oem6 raw galileo f/nav paga data */
#define ID_GALINAVRAWWORD 1414 /* message id: oem6 raw galileo i/nav word data */
#define ID_RAWCNAVFRAME 1066 /* message id: oem6 raw cnav frame data */
#define ID_BDSEPHEMERIS 1696 /* message id: oem6 decoded bds ephemeris */
#define WL1 0.1902936727984
#define WL2 0.2442102134246
#define MAXVAL 8388608.0
#define OFF_FRQNO -7 /* F/W ver.3.620 */
/* 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 int I4(unsigned char *p) {int i; memcpy(&i,p,4); return i;}
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;}
/* extend sign ---------------------------------------------------------------*/
static int exsign(unsigned int v, int bits)
{
return (int)(v&(1<<(bits-1))?v|(~0u<<bits):v);
}
/* adjust weekly rollover of gps time ----------------------------------------*/
static gtime_t adjweek(gtime_t time, double tow)
{
double tow_p;
int week;
tow_p=time2gpst(time,&week);
if (tow<tow_p-302400.0) tow+=604800.0;
else if (tow>tow_p+302400.0) tow-=604800.0;
return gpst2time(week,tow);
}
/* get observation data index ------------------------------------------------*/
static int obsindex(obs_t *obs, gtime_t time, int sat)
{
int i,j;
if (obs->n>=MAXOBS) return -1;
for (i=0;i<obs->n;i++) {
if (obs->data[i].sat==sat) return i;
}
obs->data[i].time=time;
obs->data[i].sat=sat;
for (j=0;j<NFREQ+NEXOBS;j++) {
obs->data[i].L[j]=obs->data[i].P[j]=0.0;
obs->data[i].D[j]=obs->data[i].SNR[j]=0.0;
obs->data[i].LLI[j]=0;
obs->data[i].code[j]=CODE_NONE;
}
obs->n++;
return i;
}
/* decode cnav tracking status -------------------------------------------------
* deocode cnav tracking status
* args : unsigned int stat I tracking status field
* int *sys O system (SYS_???)
* int *code O signal code (CODE_L??)
* int *track O tracking state
* (cnav/5)
* 0=L1 idle 8=L2 idle
* 1=L1 sky search 9=L2 p-code align
* 2=L1 wide freq pull-in 10=L2 search
* 3=L1 narrow freq pull-in 11=L2 pll
* 4=L1 pll 12=L2 steering
* 5=L1 reacq
* 6=L1 steering
* 7=L1 fll
* (oem6)
* 0=idle 7=freq-lock loop
* 2=wide freq band pull-in 9=channel alignment
* 3=narrow freq band pull-in 10=code search
* 4=phase lock loop 11=aided phase lock loop
* int *plock O phase-lock flag (0=not locked, 1=locked)
* int *clock O code-lock flag (0=not locked, 1=locked)
* int *parity O parity known flag (0=not known, 1=known)
* int *halfc O phase measurement (0=half-cycle not added,
* 1=added)
* return : signal frequency (0:L1,1:L2,2:L5,3:L6,4:L7,5:L8,-1:error)
* notes : refer [1][3]
*-----------------------------------------------------------------------------*/
static int decode_trackstat(unsigned int stat, int *sys, int *code, int *track,
int *plock, int *clock, int *parity, int *halfc)
{
int satsys,sigtype,freq=0;
*track =stat&0x1F;
*plock =(stat>>10)&1;
*parity=(stat>>11)&1;
*clock =(stat>>12)&1;
satsys =(stat>>16)&7;
*halfc =(stat>>28)&1;
sigtype=(stat>>21)&0x1F;
switch (satsys) {
case 0: *sys=SYS_GPS; break;
case 1: *sys=SYS_GLO; break;
case 2: *sys=SYS_SBS; break;
case 3: *sys=SYS_GAL; break; /* OEM6 */
case 4: *sys=SYS_CMP; break; /* OEM6 F/W 6.400 */
case 5: *sys=SYS_QZS; break; /* OEM6 */
default:
trace(2,"cnav unknown system: sys=%d\n",satsys);
return -1;
}
if (*sys==SYS_GPS||*sys==SYS_QZS) {
switch (sigtype) {
case 0: freq=0; *code=CODE_L1C; break; /* L1C/A */
case 2: freq=2; *code=CODE_L5I; break; /* L5 */
case 5: freq=0; *code=CODE_L1P; break; /* L1P */
case 9: freq=1; *code=CODE_L2D; break; /* L2P codeless */
case 14: freq=2; *code=CODE_L5I; break; /* L5I */
case 17: freq=1; *code=CODE_L2X; break; /* L2C(M+L) */
default: freq=-1; break;
}
}
else if (*sys==SYS_GLO) {
switch (sigtype) {
case 0: freq=0; *code=CODE_L1C; break; /* L1C/A */
case 1: freq=1; *code=CODE_L2C; break; /* L2C/A (OEM6) */
case 5: freq=1; *code=CODE_L2C; break; /* L2C */
default: freq=-1; break;
}
}
else if (*sys==SYS_GAL) {
switch (sigtype) {
case 1: freq=0; *code=CODE_L1B; break; /* E1B (OEM6) */
case 2: freq=0; *code=CODE_L1X; break; /* E1C (OEM6) */
case 12: freq=2; *code=CODE_L5X; break; /* E5aQ (OEM6) */
case 17: freq=4; *code=CODE_L7X; break; /* E5bQ (OEM6) */
case 20: freq=5; *code=CODE_L8X; break; /* AltBOCQ (OEM6) */
default: freq=-1; break;
}
}
else if (*sys==SYS_CMP) {
switch (sigtype) {
case 0: freq=0; *code=CODE_L1I; break; /* B1 with D1 (OEM6) */
case 1: freq=1; *code=CODE_L7I; break; /* B2 with D1 (OEM6) */
case 4: freq=0; *code=CODE_L1I; break; /* B1 with D2 (OEM6) */
case 5: freq=1; *code=CODE_L7I; break; /* B2 with D2 (OEM6) */
default: freq=-1; break;
}
}
else if (*sys==SYS_SBS) {
switch (sigtype) {
case 0: freq=0; *code=CODE_L1C; break; /* L1C/A */
case 6: freq=2; *code=CODE_L5I; break; /* L5I (OEM6) */
default: freq=-1; break;
}
}
if (freq<0) {
trace(2,"cnav signal type error: sys=%d sigtype=%d\n",*sys,sigtype);
return -1;
}
return freq;
}
/* check code priority and return obs position -------------------------------*/
static int checkpri(const char *opt, int sys, int code, int freq)
{
int nex=NEXOBS; /* number of extended obs data */
if (sys==SYS_GPS) {
if (strstr(opt,"-GL1P")&&freq==0) return code==CODE_L1P?0:-1;
if (strstr(opt,"-GL2X")&&freq==1) return code==CODE_L2X?1:-1;
if (code==CODE_L1P) return nex<1?-1:NFREQ;
if (code==CODE_L2X) return nex<2?-1:NFREQ+1;
}
else if (sys==SYS_GLO) {
if (strstr(opt,"-RL2C")&&freq==1) return code==CODE_L2C?1:-1;
if (code==CODE_L2C) return nex<1?-1:NFREQ;
}
else if (sys==SYS_GAL) {
if (strstr(opt,"-EL1B")&&freq==0) return code==CODE_L1B?0:-1;
if (code==CODE_L1B) return nex<1?-1:NFREQ;
if (code==CODE_L7Q) return nex<2?-1:NFREQ+1;
if (code==CODE_L8Q) return nex<3?-1:NFREQ+2;
}
return freq<NFREQ?freq:-1;
}
/* decode rangecmpb ----------------------------------------------------------*/
static int decode_rangecmpb(raw_t *raw)
{
double psr,adr,adr_rolls,lockt,tt,dop,snr,wavelen;
int i,index,nobs,prn,sat,sys,code,freq,pos;
int track,plock,clock,parity,halfc,lli;
char *msg;
unsigned char *p=raw->buff+CNAVHLEN;
trace(3,"decode_rangecmpb: len=%d\n",raw->len);
nobs=U4(p);
if (raw->outtype) {
msg=raw->msgtype+strlen(raw->msgtype);
sprintf(msg," nobs=%2d",nobs);
}
if (raw->len<CNAVHLEN+4+nobs*24) {
trace(2,"cnav rangecmpb length error: len=%d nobs=%d\n",raw->len,nobs);
return -1;
}
for (i=0,p+=4;i<nobs;i++,p+=24) {
/* decode tracking status */
if ((freq=decode_trackstat(U4(p),&sys,&code,&track,&plock,&clock,
&parity,&halfc))<0) continue;
/* obs position */
if ((pos=checkpri(raw->opt,sys,code,freq))<0) continue;
prn=U1(p+17);
if (sys==SYS_GLO) prn-=37;
if (!(sat=satno(sys,prn))) {
trace(3,"cnav rangecmpb satellite number error: sys=%d,prn=%d\n",sys,prn);
continue;
}
if (sys==SYS_GLO&&!parity) continue; /* invalid if GLO parity unknown */
dop=exsign(U4(p+4)&0xFFFFFFF,28)/256.0;
psr=(U4(p+7)>>4)/128.0+U1(p+11)*2097152.0;
if ((wavelen=satwavelen(sat,freq,&raw->nav))<=0.0) {
if (sys==SYS_GLO) wavelen=CLIGHT/(freq==0?FREQ1_GLO:FREQ2_GLO);
else wavelen=lam_carr[freq];
}
adr=I4(p+12)/256.0;
adr_rolls=(psr/wavelen+adr)/MAXVAL;
adr=-adr+MAXVAL*floor(adr_rolls+(adr_rolls<=0?-0.5:0.5));
lockt=(U4(p+18)&0x1FFFFF)/32.0; /* lock time */
if (lockt<2) parity=0; /* pseudo-parity */
if (raw->tobs[sat-1][pos].time!=0) {
tt=timediff(raw->time,raw->tobs[sat-1][pos]);
lli=(lockt<65535.968&&lockt-raw->lockt[sat-1][pos]+0.05<=tt)?LLI_SLIP:0;
}
else {
lli=0;
}
if (!parity) lli|=LLI_HALFC;
#ifdef RTK_DISABLED
if (halfc ) lli|=LLI_HALFA;
#else
if (halfc!=raw->halfc[sat-1][pos]) lli|=LLI_SLIP;
#endif
raw->tobs [sat-1][pos]=raw->time;
raw->lockt[sat-1][pos]=lockt;
raw->halfc[sat-1][pos]=halfc;
snr=((U2(p+20)&0x3FF)>>5)+20.0;
if ((sys!=SYS_GAL&&!clock)||(sys==SYS_GAL&&!plock)) psr=0.0; /* code unlock */
if (!plock) adr=dop=0.0; /* phase unlock */
if (fabs(timediff(raw->obs.data[0].time,raw->time))>1E-9) {
raw->obs.n=0;
}
if ((index=obsindex(&raw->obs,raw->time,sat))>=0) {
raw->obs.data[index].L [pos]=adr;
raw->obs.data[index].P [pos]=psr;
raw->obs.data[index].D [pos]=(float)dop;
raw->obs.data[index].SNR[pos]=0.0<=snr&&snr<255.0?snr:0;
raw->obs.data[index].LLI[pos]=(unsigned char)lli;
raw->obs.data[index].code[pos]=code;
#ifdef RTK_DISABLED
/* L2C phase shift correction (L2C->L2P) */
if (code==CODE_L2X) {
raw->obs.data[index].L[pos]+=0.25;
trace(3,"cnav L2C phase shift corrected: prn=%2d\n",prn);
}
#endif
}
}
return 1;
}
/* decode rangeb -------------------------------------------------------------*/
static int decode_rangeb(raw_t *raw)
{
double psr,adr,dop,snr,lockt,tt;
char *msg;
int i,index,nobs,prn,sat,sys,code,freq,pos;
int track,plock,clock,parity,halfc,lli,gfrq;
unsigned char *p=raw->buff+CNAVHLEN;
trace(3,"decode_rangeb: len=%d\n",raw->len);
nobs=U4(p);
if (raw->outtype) {
msg=raw->msgtype+strlen(raw->msgtype);
sprintf(msg," nobs=%2d",nobs);
}
if (raw->len<CNAVHLEN+4+nobs*44) {
trace(2,"cnav rangeb length error: len=%d nobs=%d\n",raw->len,nobs);
return -1;
}
for (i=0,p+=4;i<nobs;i++,p+=44) {
/* decode tracking status */
if ((freq=decode_trackstat(U4(p+40),&sys,&code,&track,&plock,&clock,
&parity,&halfc))<0) continue;
/* obs position */
if ((pos=checkpri(raw->opt,sys,code,freq))<0) continue;
prn=U2(p);
if (sys==SYS_GLO) prn-=37;
if (!(sat=satno(sys,prn))) {
trace(3,"cnav rangeb satellite number error: sys=%d,prn=%d\n",sys,prn);
continue;
}
if (sys==SYS_GLO&&!parity) continue; /* invalid if GLO parity unknown */
gfrq =U2(p+ 2);
psr =R8(p+ 4);
adr =R8(p+16);
dop =R4(p+28);
snr =R4(p+32);
lockt=R4(p+36);
if (lockt<2) parity=0; /* pseudo-parity */
/* set glonass frequency channel number */
if (sys==SYS_GLO&&raw->nav.geph[prn-1].sat!=sat) {
raw->nav.geph[prn-1].frq=gfrq-7;
}
if (raw->tobs[sat-1][pos].time!=0) {
tt=timediff(raw->time,raw->tobs[sat-1][pos]);
lli=lockt-raw->lockt[sat-1][pos]+0.05<=tt?LLI_SLIP:0;
}
else {
lli=0;
}
if (!parity) lli|=LLI_HALFC;
if (halfc ) lli|=LLI_HALFA;
raw->tobs [sat-1][pos]=raw->time;
raw->lockt[sat-1][pos]=lockt;
raw->halfc[sat-1][pos]=halfc;
if (!clock) psr=0.0; /* code unlock */
if (!plock) adr=dop=0.0; /* phase unlock */
if (fabs(timediff(raw->obs.data[0].time,raw->time))>1E-9) {
raw->obs.n=0;
}
if ((index=obsindex(&raw->obs,raw->time,sat))>=0) {
raw->obs.data[index].L [pos]=-adr;
raw->obs.data[index].P [pos]=psr;
raw->obs.data[index].D [pos]=(float)dop;
raw->obs.data[index].SNR[pos]=0.0<=snr&&snr<255.0?(float)snr:0.0f;
raw->obs.data[index].LLI[pos]=(unsigned char)lli;
raw->obs.data[index].code[pos]=code;
#ifdef RTK_DISABLED
/* L2C phase shift correction */
if (code==CODE_L2X) {
raw->obs.data[index].L[pos]+=0.25;
trace(3,"cnav L2C phase shift corrected: prn=%2d\n",prn);
}
#endif
}
}
return 1;
}
/* decode rawephemb ----------------------------------------------------------*/
static int decode_rawephemb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
eph_t eph={0};
int prn,sat;
trace(3,"decode_rawephemb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+102) {
trace(2,"cnav rawephemb length error: len=%d\n",raw->len);
return -1;
}
prn=U4(p);
if (!(sat=satno(SYS_GPS,prn))) {
trace(2,"cnav rawephemb satellite number error: prn=%d\n",prn);
return -1;
}
if (decode_frame(p+ 12,&eph,NULL,NULL,NULL,NULL)!=1||
decode_frame(p+ 42,&eph,NULL,NULL,NULL,NULL)!=2||
decode_frame(p+ 72,&eph,NULL,NULL,NULL,NULL)!=3) {
trace(2,"cnav rawephemb subframe error: prn=%d\n",prn);
return -1;
}
if (!strstr(raw->opt,"-EPHALL")) {
if (eph.iode==raw->nav.eph[sat-1].iode) return 0; /* unchanged */
}
eph.sat=sat;
raw->nav.eph[sat-1]=eph;
raw->ephsat=sat;
trace(4,"decode_rawephemb: sat=%2d\n",sat);
return 2;
}
/* decode ionutcb ------------------------------------------------------------*/
static int decode_ionutcb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
int i;
trace(3,"decode_ionutcb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+108) {
trace(2,"cnav ionutcb length error: len=%d\n",raw->len);
return -1;
}
for (i=0;i<8;i++) raw->nav.ion_gps[i]=R8(p+i*8);
raw->nav.utc_gps[0]=R8(p+72);
raw->nav.utc_gps[1]=R8(p+80);
raw->nav.utc_gps[2]=U4(p+68);
raw->nav.utc_gps[3]=U4(p+64);
raw->nav.leaps =I4(p+96);
return 9;
}
/* decode rawwaasframeb ------------------------------------------------------*/
static int decode_rawwaasframeb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
int i,prn;
trace(3,"decode_rawwaasframeb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+48) {
trace(2,"cnav rawwaasframeb length error: len=%d\n",raw->len);
return -1;
}
prn=U4(p+4);
if (MINPRNQZS_S<=prn&&prn<=MAXPRNQZS_S) {
prn+=10; /* QZSS SAIF PRN -> QZSS PRN */
}
else if (prn<MINPRNSBS||MAXPRNSBS<prn) return 0;
raw->sbsmsg.tow=(int)time2gpst(raw->time,&raw->sbsmsg.week);
raw->sbsmsg.prn=prn;
for (i=0,p+=12;i<29;i++,p++) raw->sbsmsg.msg[i]=*p;
return 3;
}
/* decode rawsbasframeb ------------------------------------------------------*/
static int decode_rawsbasframeb(raw_t *raw)
{
trace(3,"decode_rawsbasframeb: len=%d\n",raw->len);
/* format same as rawwaasframeb */
return decode_rawwaasframeb(raw);
}
/* decode gloephemerisb ------------------------------------------------------*/
static int decode_gloephemerisb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
geph_t geph={0};
char *msg;
double tow,tof,toff;
int prn,sat,week;
trace(3,"decode_gloephemerisb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+144) {
trace(2,"cnav gloephemerisb length error: len=%d\n",raw->len);
return -1;
}
prn =U2(p)-37;
if (raw->outtype) {
msg=raw->msgtype+strlen(raw->msgtype);
sprintf(msg," prn=%3d",prn);
}
if (!(sat=satno(SYS_GLO,prn))) {
trace(2,"cnav gloephemerisb prn error: prn=%d\n",prn);
return -1;
}
geph.frq =U2(p+ 2)+OFF_FRQNO;
week =U2(p+ 6);
tow =floor(U4(p+8)/1000.0+0.5); /* rounded to integer sec */
toff =U4(p+ 12);
geph.iode =U4(p+ 20)&0x7F;
geph.svh =U4(p+ 24);
geph.pos[0]=R8(p+ 28);
geph.pos[1]=R8(p+ 36);
geph.pos[2]=R8(p+ 44);
geph.vel[0]=R8(p+ 52);
geph.vel[1]=R8(p+ 60);
geph.vel[2]=R8(p+ 68);
geph.acc[0]=R8(p+ 76);
geph.acc[1]=R8(p+ 84);
geph.acc[2]=R8(p+ 92);
geph.taun =R8(p+100);
geph.gamn =R8(p+116);
tof =U4(p+124)-toff; /* glonasst->gpst */
geph.age =U4(p+136);
geph.toe=gpst2time(week,tow);
tof+=floor(tow/86400.0)*86400;
if (tof<tow-43200.0) tof+=86400.0;
else if (tof>tow+43200.0) tof-=86400.0;
geph.tof=gpst2time(week,tof);
if (!strstr(raw->opt,"-EPHALL")) {
if (fabs(timediff(geph.toe,raw->nav.geph[prn-1].toe))<1.0&&
geph.svh==raw->nav.geph[prn-1].svh) return 0; /* unchanged */
}
geph.sat=sat;
raw->nav.geph[prn-1]=geph;
raw->ephsat=sat;
return 2;
}
/* decode qzss rawephemb -----------------------------------------------------*/
static int decode_qzssrawephemb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN,*q;
eph_t eph={0};
char *msg;
int i,prn,id,sat;
trace(3,"decode_qzssrawephemb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+44) {
trace(2,"cnav qzssrawephemb length error: len=%d\n",raw->len);
return -1;
}
prn=U4(p);
id =U4(p+4);
if (raw->outtype) {
msg=raw->msgtype+strlen(raw->msgtype);
sprintf(msg," prn=%3d id=%d",prn,id);
}
if (!(sat=satno(SYS_QZS,prn))) {
trace(2,"cnav qzssrawephemb satellite number error: prn=%d\n",prn);
return -1;
}
if (id<1||3<id) return 0;
q=raw->subfrm[sat-1]+(id-1)*30;
for (i=0;i<30;i++) *q++=p[8+i];
if (id<3) return 0;
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.iodc==raw->nav.eph[sat-1].iodc&&
eph.iode==raw->nav.eph[sat-1].iode) return 0; /* unchanged */
}
eph.sat=sat;
raw->nav.eph[sat-1]=eph;
raw->ephsat=sat;
trace(4,"decode_qzssrawephemb: sat=%2d\n",sat);
return 2;
}
/* decode qzss rawsubframeb --------------------------------------------------*/
static int decode_qzssrawsubframeb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
eph_t eph={0};
char *msg;
int prn,sat;
trace(3,"decode_qzssrawsubframeb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+44) {
trace(2,"cnav qzssrawsubframeb length error: len=%d\n",raw->len);
return -1;
}
prn=U4(p);
if (raw->outtype) {
msg=raw->msgtype+strlen(raw->msgtype);
sprintf(msg," prn=%3d",prn);
}
if (!(sat=satno(SYS_QZS,prn))) {
trace(2,"cnav qzssrawephemb satellite number error: prn=%d\n",prn);
return -1;
}
if (decode_frame(p+12,&eph,NULL,NULL,NULL,NULL)!=1||
decode_frame(p+42,&eph,NULL,NULL,NULL,NULL)!=2||
decode_frame(p+72,&eph,NULL,NULL,NULL,NULL)!=3) {
return 0;
}
if (!strstr(raw->opt,"-EPHALL")) {
if (eph.iodc==raw->nav.eph[sat-1].iodc&&
eph.iode==raw->nav.eph[sat-1].iode) return 0; /* unchanged */
}
eph.sat=sat;
raw->nav.eph[sat-1]=eph;
raw->ephsat=sat;
trace(4,"decode_qzssrawsubframeb: sat=%2d\n",sat);
return 2;
}
/* decode qzssionutcb --------------------------------------------------------*/
static int decode_qzssionutcb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
int i;
trace(3,"decode_qzssionutcb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+108) {
trace(2,"cnav qzssionutcb length error: len=%d\n",raw->len);
return -1;
}
for (i=0;i<8;i++) raw->nav.ion_qzs[i]=R8(p+i*8);
raw->nav.utc_qzs[0]=R8(p+72);
raw->nav.utc_qzs[1]=R8(p+80);
raw->nav.utc_qzs[2]=U4(p+68);
raw->nav.utc_qzs[3]=U4(p+64);
raw->nav.leaps =I4(p+96);
return 9;
}
/* decode galephemerisb ------------------------------------------------------*/
static int decode_galephemerisb(raw_t *raw)
{
eph_t eph={0};
unsigned char *p=raw->buff+CNAVHLEN;
double tow,sqrtA,af0_fnav,af1_fnav,af2_fnav,af0_inav,af1_inav,af2_inav,tt;
char *msg;
int prn,rcv_fnav,rcv_inav,svh_e1b,svh_e5a,svh_e5b,dvs_e1b,dvs_e5a,dvs_e5b;
int toc_fnav,toc_inav,week,sel_nav=0;
trace(3,"decode_galephemerisb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+220) {
trace(2,"cnav galephemrisb length error: len=%d\n",raw->len);
return -1;
}
prn =U4(p); p+=4;
rcv_fnav =U4(p)&1; p+=4;
rcv_inav =U4(p)&1; p+=4;
svh_e1b =U1(p)&3; p+=1;
svh_e5a =U1(p)&3; p+=1;
svh_e5b =U1(p)&3; p+=1;
dvs_e1b =U1(p)&1; p+=1;
dvs_e5a =U1(p)&1; p+=1;
dvs_e5b =U1(p)&1; p+=1;
eph.sva =U1(p); p+=1+1; /* SISA */
eph.iode =U4(p); p+=4; /* IODNav */
eph.toes =U4(p); p+=4;
sqrtA =R8(p); p+=8;
eph.deln =R8(p); p+=8;
eph.M0 =R8(p); p+=8;
eph.e =R8(p); p+=8;
eph.omg =R8(p); p+=8;
eph.cuc =R8(p); p+=8;
eph.cus =R8(p); p+=8;
eph.crc =R8(p); p+=8;
eph.crs =R8(p); p+=8;
eph.cic =R8(p); p+=8;
eph.cis =R8(p); p+=8;
eph.i0 =R8(p); p+=8;
eph.idot =R8(p); p+=8;
eph.OMG0 =R8(p); p+=8;
eph.OMGd =R8(p); p+=8;
toc_fnav =U4(p); p+=4;
af0_fnav =R8(p); p+=8;
af1_fnav =R8(p); p+=8;
af2_fnav =R8(p); p+=8;
toc_inav =U4(p); p+=4;
af0_inav =R8(p); p+=8;
af1_inav =R8(p); p+=8;
af2_inav =R8(p); p+=8;
eph.tgd[0]=R8(p); p+=8; /* BGD: E5A-E1 (s) */
eph.tgd[1]=R8(p); /* BGD: E5B-E1 (s) */
eph.iodc =eph.iode;
eph.svh =(svh_e5b<<7)|(dvs_e5b<<6)|(svh_e5a<<4)|(dvs_e5a<<3)|
(svh_e1b<<1)|dvs_e1b;
/* ephemeris selection (0:INAV,1:FNAV) */
if (strstr(raw->opt,"-GALINAV")) sel_nav=0;
else if (strstr(raw->opt,"-GALFNAV")) sel_nav=1;
else if (!rcv_inav&&rcv_fnav) sel_nav=1;
eph.A =sqrtA*sqrtA;
eph.f0 =sel_nav?af0_fnav:af0_inav;
eph.f1 =sel_nav?af1_fnav:af1_inav;
eph.f2 =sel_nav?af2_fnav:af2_inav;
eph.code =sel_nav?2:1; /* data source 1:I/NAV E1B,2:F/NAV E5a-I */
if (raw->outtype) {
msg=raw->msgtype+strlen(raw->msgtype);
sprintf(msg," prn=%3d iod=%3d toes=%6.0f",prn,eph.iode,eph.toes);
}
if (!(eph.sat=satno(SYS_GAL,prn))) {
trace(2,"oemv galephemeris satellite error: prn=%d\n",prn);
return -1;
}
tow=time2gpst(raw->time,&week);
eph.week=week; /* gps-week = gal-week */
eph.toe=gpst2time(eph.week,eph.toes);
/* for week-handover problem */
tt=timediff(eph.toe,raw->time);
if (tt<-302400.0) eph.week++;
else if (tt> 302400.0) eph.week--;
eph.toe=gpst2time(eph.week,eph.toes);
eph.toc=adjweek(eph.toe,sel_nav?toc_fnav:toc_inav);
eph.ttr=adjweek(eph.toe,tow);
if (!strstr(raw->opt,"-EPHALL")) {
if (raw->nav.eph[eph.sat-1].iode==eph.iode&&
raw->nav.eph[eph.sat-1].code==eph.code) return 0; /* unchanged */
}
raw->nav.eph[eph.sat-1]=eph;
raw->ephsat=eph.sat;
return 2;
}
/* decode galalmanacb --------------------------------------------------------*/
static int decode_galalmanacb(raw_t *raw)
{
alm_t alm={0};
unsigned char *p=raw->buff+CNAVHLEN;
double dsqrtA,sqrtA=sqrt(29601297.0);
int prn,rcv_fnav,rcv_inav,svh_e1b,svh_e5a,svh_e5b,ioda;
trace(3,"decode_galalmanacb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+100) {
trace(2,"cnav galephemrisb length error: len=%d\n",raw->len);
return -1;
}
prn =U4(p); p+=4;
rcv_fnav=U4(p)&1; p+=4;
rcv_inav=U4(p)&1; p+=4;
svh_e1b =U1(p)&3; p+=1;
svh_e5a =U1(p)&3; p+=1;
svh_e5b =U1(p)&3; p+=1+1;
ioda =U4(p); p+=4;
alm.week=U4(p); p+=4; /* gst week */
alm.toas=U4(p); p+=4;
alm.e =R8(p); p+=8;
alm.OMGd=R8(p); p+=8;
alm.OMG0=R8(p); p+=8;
alm.omg =R8(p); p+=8;
alm.M0 =R8(p); p+=8;
alm.f0 =R8(p); p+=8;
alm.f1 =R8(p); p+=8;
dsqrtA =R8(p); p+=8;
alm.i0 =(R8(p)+56.0)*D2R;
alm.svh =(svh_e5b<<7)|(svh_e5a<<4)|(svh_e1b<<1);
alm.A =(sqrtA+dsqrtA)*(sqrtA+dsqrtA);
if (!(alm.sat=satno(SYS_GAL,prn))) {
trace(2,"oemv galalmanac satellite error: prn=%d\n",prn);
return -1;
}
alm.toa=gst2time(alm.week,alm.toas);
raw->nav.alm[alm.sat-1]=alm;
return 0;
}
/* decode galclockb ----------------------------------------------------------*/
static int decode_galclockb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
double a0,a1,a0g,a1g;
int leaps,tot,wnt,wnlsf,dn,dtlsf,t0g,wn0g;
trace(3,"decode_galclockb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+64) {
trace(2,"cnav galclockb length error: len=%d\n",raw->len);
return -1;
}
a0 =R8(p); p+=8;
a1 =R8(p); p+=8;
leaps=I4(p); p+=4;
tot =U4(p); p+=4;
wnt =U4(p); p+=4;
wnlsf=U4(p); p+=4;
dn =U4(p); p+=4;
dtlsf=U4(p); p+=4;
a0g =R8(p); p+=8;
a1g =R8(p); p+=8;
t0g =U4(p); p+=4;
wn0g =U4(p);
raw->nav.utc_gal[0]=a0;
raw->nav.utc_gal[1]=a1;
raw->nav.utc_gal[2]=tot; /* utc reference tow (s) */
raw->nav.utc_gal[3]=wnt; /* utc reference week */
return 9;
}
/* decode galionob -----------------------------------------------------------*/
static int decode_galionob(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
double ai[3];
int i,sf[5];
trace(3,"decode_galionob: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+29) {
trace(2,"cnav galionob length error: len=%d\n",raw->len);
return -1;
}
ai[0]=R8(p); p+=8;
ai[1]=R8(p); p+=8;
ai[2]=R8(p); p+=8;
sf[0]=U1(p); p+=1;
sf[1]=U1(p); p+=1;
sf[2]=U1(p); p+=1;
sf[3]=U1(p); p+=1;
sf[4]=U1(p);
for (i=0;i<3;i++) raw->nav.ion_gal[i]=ai[i];
return 9;
}
/* decode galfnavrawpageb ----------------------------------------------------*/
static int decode_galfnavrawpageb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
unsigned char buff[27];
int i,sigch,satid,page;
trace(3,"decode_galfnavrawpageb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+35) {
trace(2,"cnav galfnavrawpageb length error: len=%d\n",raw->len);
return -1;
}
sigch=U4(p); p+=4;
satid=U4(p); p+=4;
for (i=0;i<27;i++) {
buff[i]=U1(p); p+=1;
}
page=getbitu(buff,0,6);
char tstr[40];
trace(3,"%s E%2d FNAV (%2d) ",time2str(raw->time,tstr,0),satid,page);
traceb(3,buff,27);
return 0;
}
/* decode galinavrawwordb ----------------------------------------------------*/
static int decode_galinavrawwordb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
unsigned char buff[16];
gtime_t time=raw->time;
char *sig;
int i,sigch,satid,sigtype,type,week=0,tow=0;
trace(3,"decode_galinavrawwordb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+28) {
trace(2,"cnav galinavrawwordb length error: len=%d\n",raw->len);
return -1;
}
sigch =U4(p); p+=4;
satid =U4(p); p+=4;
sigtype=U4(p); p+=4;
switch (sigtype) {
case 10433: sig="E1 "; break;
case 10466: sig="E5A"; break;
case 10499: sig="E5B"; break;
default: sig="???" ; break;
}
for (i=0;i<16;i++) {
buff[i]=U1(p); p+=1;
}
type=getbitu(buff,0,6);
if (type==0&&getbitu(buff,6,2)==2) {
week=getbitu(buff, 96,12); /* gst week */
tow =getbitu(buff,108,20);
time=gst2time(week,tow);
}
char tstr[40];
trace(3,"%s E%2d INAV-%s (%2d) ",time2str(time,tstr,0),satid,sig,type);
traceb(3,buff,16);
return 0;
}
/* decode rawcnavframeb ------------------------------------------------------*/
static int decode_rawcnavframeb(raw_t *raw)
{
unsigned char *p=raw->buff+CNAVHLEN;
unsigned char buff[38];
int i,sigch,prn,frmid;
trace(3,"decode_rawcnavframeb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+50) {
trace(2,"cnav rawcnavframeb length error: len=%d\n",raw->len);
return -1;
}
sigch=U4(p); p+=4;
prn =U4(p); p+=4;
frmid=U4(p); p+=4;
for (i=0;i<38;i++) {
buff[i]=U1(p); p+=1;
}
char tstr[40];
trace(3,"%s PRN=%3d FRMID=%2d ",time2str(raw->time,tstr,0),prn,frmid);
traceb(3,buff,38);
return 0;
}
/* decode bdsephemerisb ------------------------------------------------------*/
static int decode_bdsephemerisb(raw_t *raw)
{
eph_t eph={0};
unsigned char *p=raw->buff+CNAVHLEN;
double ura,sqrtA;
char *msg;
int prn,toc;
trace(3,"decode_bdsephemerisb: len=%d\n",raw->len);
if (raw->len<CNAVHLEN+196) {
trace(2,"cnav bdsephemrisb length error: len=%d\n",raw->len);
return -1;
}
prn =U4(p); p+=4;
eph.week =U4(p); p+=4;
ura =R8(p); p+=8;
eph.svh =U4(p)&1; p+=4;
eph.tgd[0]=R8(p); p+=8; /* TGD1 for B1 (s) */
eph.tgd[1]=R8(p); p+=8; /* TGD2 for B2 (s) */
eph.iodc =U4(p); p+=4; /* AODC */
toc =U4(p); p+=4;
eph.f0 =R8(p); p+=8;
eph.f1 =R8(p); p+=8;
eph.f2 =R8(p); p+=8;
eph.iode =U4(p); p+=4; /* AODE */
eph.toes =U4(p); p+=4;
sqrtA =R8(p); p+=8;
eph.e =R8(p); p+=8;
eph.omg =R8(p); p+=8;
eph.deln =R8(p); p+=8;
eph.M0 =R8(p); p+=8;
eph.OMG0 =R8(p); p+=8;
eph.OMGd =R8(p); p+=8;
eph.i0 =R8(p); p+=8;
eph.idot =R8(p); p+=8;
eph.cuc =R8(p); p+=8;
eph.cus =R8(p); p+=8;
eph.crc =R8(p); p+=8;
eph.crs =R8(p); p+=8;
eph.cic =R8(p); p+=8;
eph.cis =R8(p);
eph.A =sqrtA*sqrtA;
eph.sva =uraindex(ura);
if (raw->outtype) {
msg=raw->msgtype+strlen(raw->msgtype);
sprintf(msg," prn=%3d iod=%3d toes=%6.0f",prn,eph.iode,eph.toes);
}
if (!(eph.sat=satno(SYS_CMP,prn))) {
trace(2,"oemv bdsephemeris satellite error: prn=%d\n",prn);
return -1;
}
eph.toe=bdt2gpst(bdt2time(eph.week,eph.toes)); /* bdt -> gpst */
eph.toc=bdt2gpst(bdt2time(eph.week,toc)); /* bdt -> gpst */
eph.ttr=raw->time;
if (!strstr(raw->opt,"-EPHALL")) {
if (timediff(raw->nav.eph[eph.sat-1].toe,eph.toe)==0.0&&