-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathgps.c
2948 lines (2455 loc) · 95.1 KB
/
gps.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
/**
* multi-sdr-gps-sim generates a IQ data stream on-the-fly to simulate a
* GPS L1 baseband signal using a SDR platform like HackRF or ADLAM-Pluto.
*
* This file is part of the Github project at
* https://github.com/mictronics/multi-sdr-gps-sim.git
*
* Copyright © 2021 Mictronics
* Distributed under the MIT License.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <pthread.h>
#include <errno.h>
#include <curl/curl.h>
#include <zlib.h>
#include "sdr.h"
#include "gui.h"
#include "fifo.h"
#include "almanac.h"
#include "gps-sim.h"
/**
* Note:
* Not all stations provide RINEX data with ionosphere data.
*/
/**
* Stations providing Rinex v3 format.
* 4-characters station ID
* 9-characters station ID
* Station name
* Including Ionosphere data in Rinex file
*/
const stations_t stations_v3[] = {
{"func", "FUNC00PRT", "Funchal"},
{"flrs", "FLRS00PRT", "Santa Cruz das Flore"},
{"pdel", "PDEL00PRT", "PONTA DELGADA"},
{NULL, NULL, NULL} // Always last entry
};
/**
* Stations providing Rinex v2 format.
* 4-characters station ID
* 9-characters station ID
* Station name
*/
const stations_t stations_v2[] = {
{"abmf", "ABMF00GLP", "Aeroport du Raizet"},
{"aggo", "AGGO00ARG", "AGGO"},
{"ajac", "AJAC00FRA", "Ajaccio"},
{"ankr", "ANKR00TUR", "Ankara"},
{"areg", "AREG00PER", "Arequipa"},
{"ascg", "ASCG00SHN", "Ascension"},
{"bogi", "BOGI00POL", "Borowa Gora"},
{"bor1", "BOR100POL", "Borowiec"},
{"brst", "BRST00FRA", "Brest"},
{"chpg", "CHPG00BRA", "Cachoeira Paulista"},
{"cibg", "CIBG00IDN", "Cibinong"},
{"cpvg", "CPVG00CPV", "CAP-VERT"},
{"djig", "DJIG00DJI", "Djibouti"},
{"dlf1", "DLF100NLD", "Delft"},
{"ffmj", "FFMJ00DEU", "Frankfurt/Main"},
{"ftna", "FTNA00WLF", "Futuna"},
{"gamb", "GAMB00PYF", "Rikitea"},
{"gamg", "GAMG00KOR", "Geochang"},
{"glps", "GLPS00ECU", "Galapagos Permanent Station"},
{"glsv", "GLSV00UKR", "Kiev/Golosiiv"},
{"gmsd", "GMSD00JPN", "GUTS Masda"},
{"gop6", "GOP600CZE", "Pecny, Ondrejov"},
{"gop7", "GOP700CZE", "Pecny, Ondrejov"},
{"gope", "GOPE00CZE", "Pecny, Ondrejov"},
{"grac", "GRAC00FRA", "Grasse"},
{"gras", "GRAS00FRA", "Observatoire de Calern - OCA"},
{"holb", "HOLB00CAN", "Holberg"},
{"hueg", "HUEG00DEU", "Huegelheim"},
{"ieng", "IENG00ITA", "Torino"},
{"ista", "ISTA00TUR", "Istanbul"},
{"izmi", "IZMI00TUR", "Izmir"},
{"jfng", "JFNG00CHN", "Juifeng"},
{"joz2", "JOZ200POL", "Jozefoslaw"},
{"joze", "JOZE00POL", "Jozefoslaw"},
{"kerg", "KERG00ATF", "Kerguelen Islands"},
{"kitg", "KITG00UZB", "Kitab"},
{"koug", "KOUG00GUF", "Kourou"},
{"krgg", "KRGG00ATF", "Kerguelen Islands"},
{"krs1", "KRS100TUR", "Kars"},
{"lama", "LAMA00POL", "Lamkowo"},
{"leij", "LEIJ00DEU", "Leipzig"},
{"lmmf", "LMMF00MTQ", "Aeroport Aime CESAIRE-LE LAMENTIN"},
{"lroc", "LROC00FRA", "La Rochelle"},
{"mad2", "MAD200ESP", "Madrid Deep Space Tracking Station"},
{"madr", "MADR00ESP", "Madrid Deep Space Tracking Station"},
{"mayg", "MAYG00MYT", "Dzaoudzi"},
{"mers", "MERS00TUR", "Mersin"},
{"mikl", "MIKL00UKR", "Mykolaiv"},
{"morp", "MORP00GBR", "Morpeth"},
{"nklg", "NKLG00GAB", "N'KOLTANG"},
{"nyal", "NYAL00NOR", "Ny-Alesund"},
{"nya1", "NYA100NOR", "Ny-Alesund"},
{"ohi2", "OHI200ATA", "O'Higgins"},
{"orid", "ORID00MKD", "Ohrid"},
{"owmg", "OWMG00NZL", "Chatham Island"},
{"polv", "POLV00UKR", "Poltava"},
{"ptbb", "PTBB00DEU", "Braunschweig"},
{"ptgg", "PTGG00PHL", "Manilla"},
{"rabt", "RABT00MAR", "Rabat, EMI"},
{"reun", "REUN00REU", "La Reunion - Observatoire Volcanologique"},
{"rgdg", "RGDG00ARG", "Rio Grande"},
{"riga", "RIGA00LVA", "RIGA permanent GPS"},
{"seyg", "SEYG00SYC", "Mahe"},
{"sofi", "SOFI00BGR", "Sofia"},
{"stj3", "STJ300CAN", "STJ3 CACS-GSD"},
{"sulp", "SULP00UKR", "Lviv Polytechnic"},
{"svtl", "SVTL00RUS", "Svetloe"},
{"tana", "TANA00ETH", "ILA, Bahir Dar University"},
{"thtg", "THTG00PYF", "Papeete Tahiti"},
{"thti", "THTI00PYF", "Tahiti"},
{"tit2", "TIT200DEU", "Titz / Jackerath"},
{"tlse", "TLSE00FRA", "Toulouse"},
{"tro1", "TRO100NOR", "Tromsoe"},
{"warn", "WARN00DEU", "Warnemuende"},
{"whit", "WHIT00CAN", "WHIT CACS-GSD"},
{"wroc", "WROC00POL", "Wroclaw"},
{"wtza", "WTZA00DEU", "Wettzell"},
{"yel2", "YEL200CAN", "Yellow Knife"},
{"zeck", "ZECK00RUS", "Zelenchukskaya"},
{"zim2", "ZIM200CHE", "Zimmerwald"},
{"zimm", "ZIMM00CHE", "Zimmerwald L+T 88"},
{NULL, NULL, NULL} // Always last entry
};
static char rinex_date[21];
struct ftp_file {
const char *filename;
FILE *stream;
};
const int sinTable512[] = {
2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47,
50, 53, 56, 59, 62, 65, 68, 71, 74, 77, 80, 83, 86, 89, 91, 94,
97, 100, 103, 105, 108, 111, 114, 116, 119, 122, 125, 127, 130, 132, 135, 138,
140, 143, 145, 148, 150, 153, 155, 157, 160, 162, 164, 167, 169, 171, 173, 176,
178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 205, 207,
209, 210, 212, 214, 215, 217, 218, 220, 221, 223, 224, 225, 227, 228, 229, 230,
232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 241, 242, 243, 244, 244, 245,
245, 246, 247, 247, 248, 248, 248, 249, 249, 249, 249, 250, 250, 250, 250, 250,
250, 250, 250, 250, 250, 249, 249, 249, 249, 248, 248, 248, 247, 247, 246, 245,
245, 244, 244, 243, 242, 241, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232,
230, 229, 228, 227, 225, 224, 223, 221, 220, 218, 217, 215, 214, 212, 210, 209,
207, 205, 204, 202, 200, 198, 196, 194, 192, 190, 188, 186, 184, 182, 180, 178,
176, 173, 171, 169, 167, 164, 162, 160, 157, 155, 153, 150, 148, 145, 143, 140,
138, 135, 132, 130, 127, 125, 122, 119, 116, 114, 111, 108, 105, 103, 100, 97,
94, 91, 89, 86, 83, 80, 77, 74, 71, 68, 65, 62, 59, 56, 53, 50,
47, 44, 41, 38, 35, 32, 29, 26, 23, 20, 17, 14, 11, 8, 5, 2,
-2, -5, -8, -11, -14, -17, -20, -23, -26, -29, -32, -35, -38, -41, -44, -47,
-50, -53, -56, -59, -62, -65, -68, -71, -74, -77, -80, -83, -86, -89, -91, -94,
-97, -100, -103, -105, -108, -111, -114, -116, -119, -122, -125, -127, -130, -132, -135, -138,
-140, -143, -145, -148, -150, -153, -155, -157, -160, -162, -164, -167, -169, -171, -173, -176,
-178, -180, -182, -184, -186, -188, -190, -192, -194, -196, -198, -200, -202, -204, -205, -207,
-209, -210, -212, -214, -215, -217, -218, -220, -221, -223, -224, -225, -227, -228, -229, -230,
-232, -233, -234, -235, -236, -237, -238, -239, -240, -241, -241, -242, -243, -244, -244, -245,
-245, -246, -247, -247, -248, -248, -248, -249, -249, -249, -249, -250, -250, -250, -250, -250,
-250, -250, -250, -250, -250, -249, -249, -249, -249, -248, -248, -248, -247, -247, -246, -245,
-245, -244, -244, -243, -242, -241, -241, -240, -239, -238, -237, -236, -235, -234, -233, -232,
-230, -229, -228, -227, -225, -224, -223, -221, -220, -218, -217, -215, -214, -212, -210, -209,
-207, -205, -204, -202, -200, -198, -196, -194, -192, -190, -188, -186, -184, -182, -180, -178,
-176, -173, -171, -169, -167, -164, -162, -160, -157, -155, -153, -150, -148, -145, -143, -140,
-138, -135, -132, -130, -127, -125, -122, -119, -116, -114, -111, -108, -105, -103, -100, -97,
-94, -91, -89, -86, -83, -80, -77, -74, -71, -68, -65, -62, -59, -56, -53, -50,
-47, -44, -41, -38, -35, -32, -29, -26, -23, -20, -17, -14, -11, -8, -5, -2
};
const int cosTable512[] = {
250, 250, 250, 250, 250, 249, 249, 249, 249, 248, 248, 248, 247, 247, 246, 245,
245, 244, 244, 243, 242, 241, 241, 240, 239, 238, 237, 236, 235, 234, 233, 232,
230, 229, 228, 227, 225, 224, 223, 221, 220, 218, 217, 215, 214, 212, 210, 209,
207, 205, 204, 202, 200, 198, 196, 194, 192, 190, 188, 186, 184, 182, 180, 178,
176, 173, 171, 169, 167, 164, 162, 160, 157, 155, 153, 150, 148, 145, 143, 140,
138, 135, 132, 130, 127, 125, 122, 119, 116, 114, 111, 108, 105, 103, 100, 97,
94, 91, 89, 86, 83, 80, 77, 74, 71, 68, 65, 62, 59, 56, 53, 50,
47, 44, 41, 38, 35, 32, 29, 26, 23, 20, 17, 14, 11, 8, 5, 2,
-2, -5, -8, -11, -14, -17, -20, -23, -26, -29, -32, -35, -38, -41, -44, -47,
-50, -53, -56, -59, -62, -65, -68, -71, -74, -77, -80, -83, -86, -89, -91, -94,
-97, -100, -103, -105, -108, -111, -114, -116, -119, -122, -125, -127, -130, -132, -135, -138,
-140, -143, -145, -148, -150, -153, -155, -157, -160, -162, -164, -167, -169, -171, -173, -176,
-178, -180, -182, -184, -186, -188, -190, -192, -194, -196, -198, -200, -202, -204, -205, -207,
-209, -210, -212, -214, -215, -217, -218, -220, -221, -223, -224, -225, -227, -228, -229, -230,
-232, -233, -234, -235, -236, -237, -238, -239, -240, -241, -241, -242, -243, -244, -244, -245,
-245, -246, -247, -247, -248, -248, -248, -249, -249, -249, -249, -250, -250, -250, -250, -250,
-250, -250, -250, -250, -250, -249, -249, -249, -249, -248, -248, -248, -247, -247, -246, -245,
-245, -244, -244, -243, -242, -241, -241, -240, -239, -238, -237, -236, -235, -234, -233, -232,
-230, -229, -228, -227, -225, -224, -223, -221, -220, -218, -217, -215, -214, -212, -210, -209,
-207, -205, -204, -202, -200, -198, -196, -194, -192, -190, -188, -186, -184, -182, -180, -178,
-176, -173, -171, -169, -167, -164, -162, -160, -157, -155, -153, -150, -148, -145, -143, -140,
-138, -135, -132, -130, -127, -125, -122, -119, -116, -114, -111, -108, -105, -103, -100, -97,
-94, -91, -89, -86, -83, -80, -77, -74, -71, -68, -65, -62, -59, -56, -53, -50,
-47, -44, -41, -38, -35, -32, -29, -26, -23, -20, -17, -14, -11, -8, -5, -2,
2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47,
50, 53, 56, 59, 62, 65, 68, 71, 74, 77, 80, 83, 86, 89, 91, 94,
97, 100, 103, 105, 108, 111, 114, 116, 119, 122, 125, 127, 130, 132, 135, 138,
140, 143, 145, 148, 150, 153, 155, 157, 160, 162, 164, 167, 169, 171, 173, 176,
178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 205, 207,
209, 210, 212, 214, 215, 217, 218, 220, 221, 223, 224, 225, 227, 228, 229, 230,
232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 241, 242, 243, 244, 244, 245,
245, 246, 247, 247, 248, 248, 248, 249, 249, 249, 249, 250, 250, 250, 250, 250
};
// Receiver antenna attenuation in dB for boresight angle = 0:5:180 [deg]
const double ant_pat_db[37] = {
0.00, 0.00, 0.22, 0.44, 0.67, 1.11, 1.56, 2.00, 2.44, 2.89, 3.56, 4.22,
4.89, 5.56, 6.22, 6.89, 7.56, 8.22, 8.89, 9.78, 10.67, 11.56, 12.44, 13.33,
14.44, 15.56, 16.67, 17.78, 18.89, 20.00, 21.33, 22.67, 24.00, 25.56, 27.33, 29.33,
31.56
};
// Page number to SVID conversion for subframe 4&5
// See IS-GPS-200L, p.110, table 20-V
const unsigned long sbf4_svId[25] = {
57UL, 0UL, 0UL, 0UL, 0UL, 57UL, 0UL, 0UL, 0UL, 0UL,
57UL, 62UL, 52UL, 53UL, 54UL, 57UL, 55UL, 56UL, 58UL,
59UL, 57UL, 60UL, 61UL, 62UL, 63UL
};
const unsigned long sbf5_svId[25] = {
0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL,
0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL,
0UL, 0UL, 0UL, 0UL, 51UL
};
static int allocatedSat[MAX_SAT];
/* Subtract two vectors of double
* y Result of subtraction
* x1 Minuend of subtracion
* x2 Subtrahend of subtracion
*/
static void subVect(double *y, const double *x1, const double *x2) {
y[0] = x1[0] - x2[0];
y[1] = x1[1] - x2[1];
y[2] = x1[2] - x2[2];
return;
}
/* Compute Norm of Vector
* x Input vector
* Length (Norm) of the input vector
*/
static double normVect(const double *x) {
return (sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]));
}
/* Compute dot-product of two vectors
* x1 First multiplicand
* x2 Second multiplicand
* Dot-product of both multiplicands
*/
static double dotProd(const double *x1, const double *x2) {
return (x1[0] * x2[0] + x1[1] * x2[1] + x1[2] * x2[2]);
}
/* Generate the C/A code sequence for a given Satellite Vehicle PRN
* prn PRN nuber of the Satellite Vehicle
* ca Caller-allocated integer array of 1023 bytes
*/
static void codegen(int *ca, int prn) {
int delay[] = {
5, 6, 7, 8, 17, 18, 139, 140, 141, 251,
252, 254, 255, 256, 257, 258, 469, 470, 471, 472,
473, 474, 509, 512, 513, 514, 515, 516, 859, 860,
861, 862
};
int g1[CA_SEQ_LEN], g2[CA_SEQ_LEN];
int r1[N_DWRD_SBF], r2[N_DWRD_SBF];
int c1, c2;
int i, j;
if (prn < 1 || prn > 32)
return;
for (i = 0; i < N_DWRD_SBF; i++)
r1[i] = r2[i] = -1;
for (i = 0; i < CA_SEQ_LEN; i++) {
g1[i] = r1[9];
g2[i] = r2[9];
c1 = r1[2] * r1[9];
c2 = r2[1] * r2[2] * r2[5] * r2[7] * r2[8] * r2[9];
for (j = 9; j > 0; j--) {
r1[j] = r1[j - 1];
r2[j] = r2[j - 1];
}
r1[0] = c1;
r2[0] = c2;
}
for (i = 0, j = CA_SEQ_LEN - delay[prn - 1]; i < CA_SEQ_LEN; i++, j++)
ca[i] = (1 - g1[i] * g2[j % CA_SEQ_LEN]) / 2;
return;
}
/* Convert a UTC date into a GPS date
* t input date in UTC form
* g output date in GPS form
*/
static void date2gps(const datetime_t *t, gpstime_t *g) {
int doy[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
int ye;
int de;
int lpdays;
ye = t->y - 1980;
// Compute the number of leap days since Jan 5/Jan 6, 1980.
lpdays = ye / 4 + 1;
if ((ye % 4) == 0 && t->m <= 2)
lpdays--;
// Compute the number of days elapsed since Jan 5/Jan 6, 1980.
de = ye * 365 + doy[t->m - 1] + t->d + lpdays - 6;
// Convert time to GPS weeks and seconds.
g->week = de / 7;
g->sec = (double) (de % 7) * SECONDS_IN_DAY + t->hh * SECONDS_IN_HOUR
+ t->mm * SECONDS_IN_MINUTE + t->sec;
return;
}
static void gps2date(const gpstime_t *g, datetime_t *t) {
// Convert Julian day number to calendar date
int c = (int) (7 * g->week + floor(g->sec / 86400.0) + 2444245.0) + 1537;
int d = (int) ((c - 122.1) / 365.25);
int e = 365 * d + d / 4;
int f = (int) ((c - e) / 30.6001);
t->d = c - e - (int) (30.6001 * f);
t->m = f - 1 - 12 * (f / 14);
t->y = d - 4715 - ((7 + t->m) / 10);
t->hh = ((int) (g->sec / 3600.0)) % 24;
t->mm = ((int) (g->sec / 60.0)) % 60;
t->sec = g->sec - 60.0 * floor(g->sec / 60.0);
return;
}
/* Convert Earth-centered Earth-fixed (ECEF) into Lat/Long/Height
* xyz Input Array of X, Y and Z ECEF coordinates
* llh Output Array of Latitude, Longitude and Height
*/
static void xyz2llh(const double *xyz, double *llh) {
double a, eps, e, e2;
double x, y, z;
double rho2, dz, zdz, nh, slat, n, dz_new;
a = WGS84_RADIUS;
e = WGS84_ECCENTRICITY;
eps = 1.0e-3;
e2 = e*e;
if (normVect(xyz) < eps) {
// Invalid ECEF vector
llh[0] = 0.0;
llh[1] = 0.0;
llh[2] = -a;
return;
}
x = xyz[0];
y = xyz[1];
z = xyz[2];
rho2 = x * x + y*y;
dz = e2*z;
while (1) {
zdz = z + dz;
nh = sqrt(rho2 + zdz * zdz);
slat = zdz / nh;
n = a / sqrt(1.0 - e2 * slat * slat);
dz_new = n * e2*slat;
if (fabs(dz - dz_new) < eps)
break;
dz = dz_new;
}
llh[0] = atan2(zdz, sqrt(rho2));
llh[1] = atan2(y, x);
llh[2] = nh - n;
return;
}
/* Convert Lat/Long/Height into Earth-centered Earth-fixed (ECEF)
* llh Input Array of Latitude, Longitude and Height
* xyz Output Array of X, Y and Z ECEF coordinates
*/
static void llh2xyz(const double *llh, double *xyz) {
double n;
double a;
double e;
double e2;
double clat;
double slat;
double clon;
double slon;
double d, nph;
double tmp;
a = WGS84_RADIUS;
e = WGS84_ECCENTRICITY;
e2 = e*e;
clat = cos(llh[0]);
slat = sin(llh[0]);
clon = cos(llh[1]);
slon = sin(llh[1]);
d = e*slat;
n = a / sqrt(1.0 - d * d);
nph = n + llh[2];
tmp = nph*clat;
xyz[0] = tmp*clon;
xyz[1] = tmp*slon;
xyz[2] = ((1.0 - e2) * n + llh[2]) * slat;
return;
}
/* Compute the intermediate matrix for LLH to ECEF
* llh Input position in Latitude-Longitude-Height format
* t Three-by-Three output matrix
*/
static void ltcmat(const double *llh, double t[3][3]) {
double slat, clat;
double slon, clon;
slat = sin(llh[0]);
clat = cos(llh[0]);
slon = sin(llh[1]);
clon = cos(llh[1]);
t[0][0] = -slat*clon;
t[0][1] = -slat*slon;
t[0][2] = clat;
t[1][0] = -slon;
t[1][1] = clon;
t[1][2] = 0.0;
t[2][0] = clat*clon;
t[2][1] = clat*slon;
t[2][2] = slat;
return;
}
/* Convert Earth-centered Earth-Fixed to ?
* xyz Input position as vector in ECEF format
* t Intermediate matrix computed by \ref ltcmat
* neu Output position as North-East-Up format
*/
static void ecef2neu(const double *xyz, double t[3][3], double *neu) {
neu[0] = t[0][0] * xyz[0] + t[0][1] * xyz[1] + t[0][2] * xyz[2];
neu[1] = t[1][0] * xyz[0] + t[1][1] * xyz[1] + t[1][2] * xyz[2];
neu[2] = t[2][0] * xyz[0] + t[2][1] * xyz[1] + t[2][2] * xyz[2];
return;
}
/* Convert North-Eeast-Up to Azimuth + Elevation
* neu Input position in North-East-Up format
* azel Output array of azimuth + elevation as double
*/
static void neu2azel(double *azel, const double *neu) {
double ne;
azel[0] = atan2(neu[1], neu[0]);
if (azel[0] < 0.0)
azel[0] += (2.0 * PI);
ne = sqrt(neu[0] * neu[0] + neu[1] * neu[1]);
azel[1] = atan2(neu[2], ne);
return;
}
/* Compute Satellite position, velocity and clock at given time
* eph Ephemeris data of the satellite
* g GPS time at which position is to be computed
* pos Computed position (vector)
* vel Computed velociy (vector)
* clk Computed clock
*/
static void satpos(ephem_t eph, gpstime_t g, double *pos, double *vel, double *clk) {
// Computing Satellite Velocity using the Broadcast Ephemeris
// http://www.ngs.noaa.gov/gps-toolbox/bc_velo.htm
double tk;
double mk;
double ek;
double ekold;
double ekdot;
double cek, sek;
double pk;
double pkdot;
double c2pk, s2pk;
double uk;
double ukdot;
double cuk, suk;
double ok;
double sok, cok;
double ik;
double ikdot;
double sik, cik;
double rk;
double rkdot;
double xpk, ypk;
double xpkdot, ypkdot;
double relativistic, OneMinusecosE, tmp;
tk = g.sec - eph.toe.sec;
if (tk > SECONDS_IN_HALF_WEEK)
tk -= SECONDS_IN_WEEK;
else if (tk<-SECONDS_IN_HALF_WEEK)
tk += SECONDS_IN_WEEK;
mk = eph.m0 + eph.n*tk;
ek = mk;
ekold = ek + 1.0;
OneMinusecosE = 0; // Suppress the uninitialized warning.
while (fabs(ek - ekold) > 1.0E-14) {
ekold = ek;
OneMinusecosE = 1.0 - eph.ecc * cos(ekold);
ek = ek + (mk - ekold + eph.ecc * sin(ekold)) / OneMinusecosE;
}
sek = sin(ek);
cek = cos(ek);
ekdot = eph.n / OneMinusecosE;
relativistic = -4.442807633E-10 * eph.ecc * eph.sqrta*sek;
pk = atan2(eph.sq1e2*sek, cek - eph.ecc) + eph.aop;
pkdot = eph.sq1e2 * ekdot / OneMinusecosE;
s2pk = sin(2.0 * pk);
c2pk = cos(2.0 * pk);
uk = pk + eph.cus * s2pk + eph.cuc*c2pk;
suk = sin(uk);
cuk = cos(uk);
ukdot = pkdot * (1.0 + 2.0 * (eph.cus * c2pk - eph.cuc * s2pk));
rk = eph.A * OneMinusecosE + eph.crc * c2pk + eph.crs*s2pk;
rkdot = eph.A * eph.ecc * sek * ekdot + 2.0 * pkdot * (eph.crs * c2pk - eph.crc * s2pk);
ik = eph.inc0 + eph.idot * tk + eph.cic * c2pk + eph.cis*s2pk;
sik = sin(ik);
cik = cos(ik);
ikdot = eph.idot + 2.0 * pkdot * (eph.cis * c2pk - eph.cic * s2pk);
xpk = rk*cuk;
ypk = rk*suk;
xpkdot = rkdot * cuk - ypk*ukdot;
ypkdot = rkdot * suk + xpk*ukdot;
ok = eph.omg0 + tk * eph.omgkdot - OMEGA_EARTH * eph.toe.sec;
sok = sin(ok);
cok = cos(ok);
pos[0] = xpk * cok - ypk * cik*sok;
pos[1] = xpk * sok + ypk * cik*cok;
pos[2] = ypk*sik;
tmp = ypkdot * cik - ypk * sik*ikdot;
vel[0] = -eph.omgkdot * pos[1] + xpkdot * cok - tmp*sok;
vel[1] = eph.omgkdot * pos[0] + xpkdot * sok + tmp*cok;
vel[2] = ypk * cik * ikdot + ypkdot*sik;
// Satellite clock correction
tk = g.sec - eph.toc.sec;
if (tk > SECONDS_IN_HALF_WEEK)
tk -= SECONDS_IN_WEEK;
else if (tk<-SECONDS_IN_HALF_WEEK)
tk += SECONDS_IN_WEEK;
clk[0] = eph.af0 + tk * (eph.af1 + tk * eph.af2) + relativistic - eph.tgd;
clk[1] = eph.af1 + 2.0 * tk * eph.af2;
return;
}
/* Compute Subframe from Ephemeris
* eph Ephemeris of given SV
* sbf Array of five sub-frames, 10 long words each
*/
void eph2sbf(const ephem_t eph, const ionoutc_t ionoutc, const almanac_gps_t *alm, unsigned long sbf[N_SBF_PAGE][N_DWRD_SBF]) {
unsigned long wn;
unsigned long toe;
unsigned long toc;
unsigned long iode;
unsigned long iodc;
long deltan;
long cuc;
long cus;
long cic;
long cis;
long crc;
long crs;
unsigned long ecc;
unsigned long sqrta;
long m0;
long omega0;
long inc0;
long aop;
long omegadot;
long idot;
long af0;
long af1;
long af2;
long tgd;
unsigned long ura = 0UL;
unsigned long dataId = 1UL;
unsigned long wna;
unsigned long toa;
signed long alpha0, alpha1, alpha2, alpha3;
signed long beta0, beta1, beta2, beta3;
signed long A0, A1;
signed long dtls, dtlsf;
unsigned long tot, wnt, wnlsf, dn;
int sv, i;
unsigned long svId;
signed long delta_i; // Relative to i0 = 0.30 semicircles
// FIXED: This has to be the "transmission" week number, not for the ephemeris reference time
//wn = (unsigned long)(eph.toe.week%1024);
wn = 0UL;
toe = (unsigned long) (eph.toe.sec / 16.0);
toc = (unsigned long) (eph.toc.sec / 16.0);
iode = (unsigned long) (eph.iode);
iodc = (unsigned long) (eph.iodc);
deltan = (long) (eph.deltan / POW2_M43 / PI);
cuc = (long) (eph.cuc / POW2_M29);
cus = (long) (eph.cus / POW2_M29);
cic = (long) (eph.cic / POW2_M29);
cis = (long) (eph.cis / POW2_M29);
crc = (long) (eph.crc / POW2_M5);
crs = (long) (eph.crs / POW2_M5);
ecc = (unsigned long) (eph.ecc / POW2_M33);
sqrta = (unsigned long) (eph.sqrta / POW2_M19);
m0 = (long) (eph.m0 / POW2_M31 / PI);
omega0 = (long) (eph.omg0 / POW2_M31 / PI);
inc0 = (long) (eph.inc0 / POW2_M31 / PI);
aop = (long) (eph.aop / POW2_M31 / PI);
omegadot = (long) (eph.omgdot / POW2_M43 / PI);
idot = (long) (eph.idot / POW2_M43 / PI);
af0 = (long) (eph.af0 / POW2_M31);
af1 = (long) (eph.af1 / POW2_M43);
af2 = (long) (eph.af2 / POW2_M55);
tgd = (long) (eph.tgd / POW2_M31);
alpha0 = (signed long) round(ionoutc.alpha0 / POW2_M30);
alpha1 = (signed long) round(ionoutc.alpha1 / POW2_M27);
alpha2 = (signed long) round(ionoutc.alpha2 / POW2_M24);
alpha3 = (signed long) round(ionoutc.alpha3 / POW2_M24);
beta0 = (signed long) round(ionoutc.beta0 / 2048.0);
beta1 = (signed long) round(ionoutc.beta1 / 16384.0);
beta2 = (signed long) round(ionoutc.beta2 / 65536.0);
beta3 = (signed long) round(ionoutc.beta3 / 65536.0);
A0 = (signed long) round(ionoutc.A0 / POW2_M30);
A1 = (signed long) round(ionoutc.A1 / POW2_M50);
dtls = (signed long) (ionoutc.dtls);
tot = (unsigned long) (ionoutc.tot / 4096);
wnt = (unsigned long) (ionoutc.wnt % 256);
// TO DO: Specify scheduled leap seconds in command options
// 2016/12/31 (Sat) -> WNlsf = 1929, DN = 7 (http://navigationservices.agi.com/GNSSWeb/)
// Days are counted from 1 to 7 (Sunday is 1).
wnlsf = 1929 % 256;
dn = 7;
dtlsf = 18;
// Subframe 1
sbf[0][0] = 0x8B0000UL << 6;
sbf[0][1] = 0x1UL << 8;
sbf[0][2] = ((wn & 0x3FFUL) << 20) | (ura << 14) | (((iodc >> 8)&0x3UL) << 6);
sbf[0][3] = 0UL;
sbf[0][4] = 0UL;
sbf[0][5] = 0UL;
sbf[0][6] = (tgd & 0xFFUL) << 6;
sbf[0][7] = ((iodc & 0xFFUL) << 22) | ((toc & 0xFFFFUL) << 6);
sbf[0][8] = ((af2 & 0xFFUL) << 22) | ((af1 & 0xFFFFUL) << 6);
sbf[0][9] = (af0 & 0x3FFFFFUL) << 8;
// Subframe 2
sbf[1][0] = 0x8B0000UL << 6;
sbf[1][1] = 0x2UL << 8;
sbf[1][2] = ((iode & 0xFFUL) << 22) | ((crs & 0xFFFFUL) << 6);
sbf[1][3] = ((deltan & 0xFFFFUL) << 14) | (((m0 >> 24)&0xFFUL) << 6);
sbf[1][4] = (m0 & 0xFFFFFFUL) << 6;
sbf[1][5] = ((cuc & 0xFFFFUL) << 14) | (((ecc >> 24)&0xFFUL) << 6);
sbf[1][6] = (ecc & 0xFFFFFFUL) << 6;
sbf[1][7] = ((cus & 0xFFFFUL) << 14) | (((sqrta >> 24)&0xFFUL) << 6);
sbf[1][8] = (sqrta & 0xFFFFFFUL) << 6;
sbf[1][9] = (toe & 0xFFFFUL) << 14;
// Subframe 3
sbf[2][0] = 0x8B0000UL << 6;
sbf[2][1] = 0x3UL << 8;
sbf[2][2] = ((cic & 0xFFFFUL) << 14) | (((omega0 >> 24)&0xFFUL) << 6);
sbf[2][3] = (omega0 & 0xFFFFFFUL) << 6;
sbf[2][4] = ((cis & 0xFFFFUL) << 14) | (((inc0 >> 24)&0xFFUL) << 6);
sbf[2][5] = (inc0 & 0xFFFFFFUL) << 6;
sbf[2][6] = ((crc & 0xFFFFUL) << 14) | (((aop >> 24)&0xFFUL) << 6);
sbf[2][7] = (aop & 0xFFFFFFUL) << 6;
sbf[2][8] = (omegadot & 0xFFFFFFUL) << 6;
sbf[2][9] = ((iode & 0xFFUL) << 22) | ((idot & 0x3FFFUL) << 8);
// Empty all the pages of subframes 4 and 5
for (i = 0; i < 25; i++) {
svId = 0UL; // Dummy SV
//svId = sbf4_svId[i];
sbf[3 + i * 2][0] = 0x8B0000UL << 6; // Preamble for TLM
sbf[3 + i * 2][1] = 0x4UL << 8; // Subframe ID for HOW
sbf[3 + i * 2][2] = (dataId << 28) | (svId << 22) | ((EMPTY_WORD & 0xFFFFUL) << 6);
sbf[3 + i * 2][3] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[3 + i * 2][4] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[3 + i * 2][5] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[3 + i * 2][6] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[3 + i * 2][7] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[3 + i * 2][8] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[3 + i * 2][9] = (EMPTY_WORD & 0x3FFFFFUL) << 8;
//svId = sbf5_svId[i];
sbf[4 + i * 2][0] = 0x8B0000UL << 6; // Preamble for TLM
sbf[4 + i * 2][1] = 0x5UL << 8; // Subframe ID for HOW
sbf[4 + i * 2][2] = (dataId << 28) | (svId << 22) | ((EMPTY_WORD & 0xFFFFUL) << 6);
sbf[4 + i * 2][3] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[4 + i * 2][4] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[4 + i * 2][5] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[4 + i * 2][6] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[4 + i * 2][7] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[4 + i * 2][8] = (EMPTY_WORD & 0xFFFFFFUL) << 6;
sbf[4 + i * 2][9] = (EMPTY_WORD & 0x3FFFFFUL) << 8;
}
// Subframe 4, pages 2-5 and 7-10: almanac data for PRN 25 through 32
for (sv = 24; sv < MAX_SAT; sv++) {
if (sv >= 24 && sv <= 27) // PRN 25-28
i = sv - 23; // Pages 2-5 (i = 1-4)
else if (sv >= 28 && sv < MAX_SAT) // PRN 29-32
i = sv - 22; // Page 7-10 (i = 6-9)
if (alm->sv[sv].valid != 0) {
svId = (unsigned long) (sv + 1);
ecc = (unsigned long) (alm->sv[sv].e / POW2_M21);
toa = (unsigned long) (alm->sv[sv].toa.sec / POW2_12);
delta_i = (signed long) (alm->sv[sv].delta_i / POW2_M19);
omegadot = (signed long) (alm->sv[sv].omegadot / POW2_M38);
sqrta = (unsigned long) (alm->sv[sv].sqrta / POW2_M11);
omega0 = (signed long) (alm->sv[sv].omega0 / POW2_M23);
aop = (signed long) (alm->sv[sv].aop / POW2_M23);
m0 = (signed long) (alm->sv[sv].m0 / POW2_M23);
af0 = (signed long) (alm->sv[sv].af0 / POW2_M20);
af1 = (signed long) (alm->sv[sv].af1 / POW2_M38);
sbf[3 + i * 2][0] = 0x8B0000UL << 6; // Preamble for TLM
sbf[3 + i * 2][1] = 0x4UL << 8; // Subframe ID for HOW
sbf[3 + i * 2][2] = (dataId << 28) | (svId << 22) | ((ecc & 0xFFFFUL) << 6);
sbf[3 + i * 2][3] = ((toa & 0xFFUL) << 22) | ((delta_i & 0xFFFFUL) << 6);
sbf[3 + i * 2][4] = ((omegadot & 0xFFFFUL) << 14); // SV HEALTH = 000 (ALL DATA OK)
sbf[3 + i * 2][5] = ((sqrta & 0xFFFFFFUL) << 6);
sbf[3 + i * 2][6] = ((omega0 & 0xFFFFFFUL) << 6);
sbf[3 + i * 2][7] = ((aop & 0xFFFFFFUL) << 6);
sbf[3 + i * 2][8] = ((m0 & 0xFFFFFFUL) << 6);
sbf[3 + i * 2][9] = ((af0 & 0x7F8UL) << 19) | ((af1 & 0x7FFUL) << 11) | ((af0 & 0x7UL) << 8);
}
}
// Subframe 4, page 18: ionospheric and UTC data
if (ionoutc.vflg == true) {
sbf[3 + 17 * 2][0] = 0x8B0000UL << 6;
sbf[3 + 17 * 2][1] = 0x4UL << 8;
sbf[3 + 17 * 2][2] = (dataId << 28) | (sbf4_svId[17] << 22) | ((alpha0 & 0xFFUL) << 14) | ((alpha1 & 0xFFUL) << 6);
sbf[3 + 17 * 2][3] = ((alpha2 & 0xFFUL) << 22) | ((alpha3 & 0xFFUL) << 14) | ((beta0 & 0xFFUL) << 6);
sbf[3 + 17 * 2][4] = ((beta1 & 0xFFUL) << 22) | ((beta2 & 0xFFUL) << 14) | ((beta3 & 0xFFUL) << 6);
sbf[3 + 17 * 2][5] = (A1 & 0xFFFFFFUL) << 6;
sbf[3 + 17 * 2][6] = ((A0 >> 8)&0xFFFFFFUL) << 6;
sbf[3 + 17 * 2][7] = ((A0 & 0xFFUL) << 22) | ((tot & 0xFFUL) << 14) | ((wnt & 0xFFUL) << 6);
sbf[3 + 17 * 2][8] = ((dtls & 0xFFUL) << 22) | ((wnlsf & 0xFFUL) << 14) | ((dn & 0xFFUL) << 6);
sbf[3 + 17 * 2][9] = (dtlsf & 0xFFUL) << 22;
}
// Subframe 4, page 25: SV health data for PRN 25 through 32
sbf[3 + 24 * 2][0] = 0x8B0000UL << 6;
sbf[3 + 24 * 2][1] = 0x4UL << 8;
sbf[3 + 24 * 2][2] = (dataId << 28) | (sbf4_svId[24] << 22);
sbf[3 + 24 * 2][3] = 0UL;
sbf[3 + 24 * 2][4] = 0UL;
sbf[3 + 24 * 2][5] = 0UL;
sbf[3 + 24 * 2][6] = 0UL;
sbf[3 + 24 * 2][7] = 0UL;
sbf[3 + 24 * 2][8] = 0UL;
sbf[3 + 24 * 2][9] = 0UL;
// Subframe 5, page 1-24: almanac data for PRN 1 through 24
for (sv = 0; sv < 24; sv++) {
i = sv;
if (alm->sv[sv].svid != 0) {
svId = (unsigned long) (sv + 1);
ecc = (unsigned long) (alm->sv[sv].e / POW2_M21);
toa = (unsigned long) (alm->sv[sv].toa.sec / 4096.0);
delta_i = (signed long) (alm->sv[sv].delta_i / POW2_M19);
omegadot = (signed long) (alm->sv[sv].omegadot / POW2_M38);
sqrta = (unsigned long) (alm->sv[sv].sqrta / POW2_M11);
omega0 = (signed long) (alm->sv[sv].omega0 / POW2_M23);
aop = (signed long) (alm->sv[sv].aop / POW2_M23);
m0 = (signed long) (alm->sv[sv].m0 / POW2_M23);
af0 = (signed long) (alm->sv[sv].af0 / POW2_M20);
af1 = (signed long) (alm->sv[sv].af1 / POW2_M38);
sbf[4 + i * 2][0] = 0x8B0000UL << 6; // Preamble
sbf[4 + i * 2][1] = 0x5UL << 8; // Subframe ID
sbf[4 + i * 2][2] = (dataId << 28) | (svId << 22) | ((ecc & 0xFFFFUL) << 6);
sbf[4 + i * 2][3] = ((toa & 0xFFUL) << 22) | ((delta_i & 0xFFFFUL) << 6);
sbf[4 + i * 2][4] = ((omegadot & 0xFFFFUL) << 14); // SV HEALTH = 000 (ALL DATA OK)
sbf[4 + i * 2][5] = ((sqrta & 0xFFFFFFUL) << 6);
sbf[4 + i * 2][6] = ((omega0 & 0xFFFFFFUL) << 6);
sbf[4 + i * 2][7] = ((aop & 0xFFFFFFUL) << 6);
sbf[4 + i * 2][8] = ((m0 & 0xFFFFFFUL) << 6);
sbf[4 + i * 2][9] = ((af0 & 0x7F8UL) << 19) | ((af1 & 0x7FFUL) << 11) | ((af0 & 0x7UL) << 8);
}
}
// Subframe 5, page 25: SV health data for PRN 1 through 24
wna = (unsigned long) (eph.toe.week % 256);
toa = (unsigned long) (eph.toe.sec / 4096.0);
for (sv = 0; sv < MAX_SAT; sv++) {
if (alm->sv[sv].svid != 0) // Valid almanac is availabe
{
wna = (unsigned long) (alm->sv[sv].toa.week % 256);
toa = (unsigned long) (alm->sv[sv].toa.sec / 4096.0);
break;
}
}
sbf[4 + 24 * 2][0] = 0x8B0000UL << 6;
sbf[4 + 24 * 2][1] = 0x5UL << 8;
sbf[4 + 24 * 2][2] = (dataId << 28) | (sbf5_svId[24] << 22) | ((toa & 0xFFUL) << 14) | ((wna & 0xFFUL) << 6);
sbf[4 + 24 * 2][3] = 0UL;
sbf[4 + 24 * 2][4] = 0UL;
sbf[4 + 24 * 2][5] = 0UL;
sbf[4 + 24 * 2][6] = 0UL;
sbf[4 + 24 * 2][7] = 0UL;
sbf[4 + 24 * 2][8] = 0UL;
sbf[4 + 24 * 2][9] = 0UL;
}
/* Count number of bits set to 1
* v long word in which bits are counted
* Count of bits set to 1
*/
static unsigned long countBits(unsigned long v) {
unsigned long c;
const int S[] = {1, 2, 4, 8, 16};
const unsigned long B[] = {
0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF
};
c = v;
c = ((c >> S[0]) & B[0]) + (c & B[0]);
c = ((c >> S[1]) & B[1]) + (c & B[1]);
c = ((c >> S[2]) & B[2]) + (c & B[2]);
c = ((c >> S[3]) & B[3]) + (c & B[3]);
c = ((c >> S[4]) & B[4]) + (c & B[4]);
return (c);
}
static int decode_wordN(unsigned int word) {
const unsigned int hamming[] = {
0xBB1F3480, 0x5D8F9A40, 0xAEC7CD00, 0x5763E680, 0x6BB1F340, 0x8B7A89C0
};
unsigned int parity = 0, w;
int i;
if (word & 0x40000000) word ^= 0x3FFFFFC0;
for (i = 0; i < 6; i++) {
parity <<= 1;
for (w = (word & hamming[i]) >> 6; w; w >>= 1) parity ^= w & 1;
}
if (parity != (word & 0x3F)) return 0;
//for (i=0;i<3;i++) data[i]=(unsigned char)(word>>(22-i*8));
return 1;
}
static bool validate_parityN(unsigned int W) {
// Parity stuff
static const unsigned int PARITY_25 = 0xBB1F3480;
static const unsigned int PARITY_26 = 0x5D8F9A40;
static const unsigned int PARITY_27 = 0xAEC7CD00;
static const unsigned int PARITY_28 = 0x5763E680;
static const unsigned int PARITY_29 = 0x6BB1F340;
static const unsigned int PARITY_30 = 0x8B7A89C0;
// Look-up table for parity of eight bit bytes
// (parity=0 if the number of 0s and 1s is equal, else parity=1)
static unsigned char byteParity[] = {
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
};
// Local variables
unsigned int t, w, p;
// The sign of the data is determined by the D30* parity bit
// of the previous data word. If D30* is set, invert the data
// bits D01..D24 to obtain the d01..d24 (but leave all other
// bits untouched).
w = W;
if (w & 0x40000000) w ^= 0x3FFFFFC0;
// Compute the parity of the sign corrected data bits d01..d24
// as described in the ICD-GPS-200
t = w & PARITY_25;
p = (byteParity[t & 0xff] ^ byteParity[(t >> 8) & 0xff] ^
byteParity[(t >> 16) & 0xff] ^ byteParity[(t >> 24)]);
t = w & PARITY_26;
p = (p << 1) |
(byteParity[t & 0xff] ^ byteParity[(t >> 8) & 0xff] ^
byteParity[(t >> 16) & 0xff] ^ byteParity[(t >> 24)]);
t = w & PARITY_27;
p = (p << 1) |
(byteParity[t & 0xff] ^ byteParity[(t >> 8) & 0xff] ^
byteParity[(t >> 16) & 0xff] ^ byteParity[(t >> 24)]);
t = w & PARITY_28;
p = (p << 1) |
(byteParity[t & 0xff] ^ byteParity[(t >> 8) & 0xff] ^
byteParity[(t >> 16) & 0xff] ^ byteParity[(t >> 24)]);
t = w & PARITY_29;
p = (p << 1) |
(byteParity[t & 0xff] ^ byteParity[(t >> 8) & 0xff] ^
byteParity[(t >> 16) & 0xff] ^ byteParity[(t >> 24)]);
t = w & PARITY_30;
p = (p << 1) |
(byteParity[t & 0xff] ^ byteParity[(t >> 8) & 0xff] ^
byteParity[(t >> 16) & 0xff] ^ byteParity[(t >> 24)]);
if ((W & 0x3f) != p) {
gui_status_wprintw(RED, "%d-%u ", (W & 0x3f), p);
}
if (!decode_wordN(W)) {
gui_status_wprintw(RED, "%d-%u ", (W & 0x3f), p);
}
return ((W & 0x3f) == p);