-
Notifications
You must be signed in to change notification settings - Fork 586
/
rtcm.h
1597 lines (1296 loc) · 60.5 KB
/
rtcm.h
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
/*!
* \file rtcm.h
* \brief Interface for the RTCM 3.2 Standard
* \author Carles Fernandez-Prades, 2015. cfernandez(at)cttc.es
*
* -----------------------------------------------------------------------------
*
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
* This file is part of GNSS-SDR.
*
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
* SPDX-License-Identifier: GPL-3.0-or-later
*
* -----------------------------------------------------------------------------
*/
#ifndef GNSS_SDR_RTCM_H
#define GNSS_SDR_RTCM_H
#include "concurrent_queue.h"
#include "galileo_ephemeris.h"
#include "galileo_has_data.h"
#include "glonass_gnav_ephemeris.h"
#include "glonass_gnav_utc_model.h"
#include "gnss_synchro.h"
#include "gps_cnav_ephemeris.h"
#include "gps_ephemeris.h"
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <algorithm> // for std::max, std::min, std::copy_n
#include <array>
#include <bitset>
#include <cstddef> // for size_t
#include <cstdint>
#include <deque>
#include <iomanip> // for std::setw
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream> // for std::stringstream
#include <string>
#include <thread>
#include <utility>
#include <vector>
#if USE_GLOG_AND_GFLAGS
#include <glog/logging.h>
#else
#include <absl/log/log.h>
#endif
/** \addtogroup PVT
* \{ */
/** \addtogroup PVT_libs
* \{ */
#if USE_BOOST_ASIO_IO_CONTEXT
using b_io_context = boost::asio::io_context;
#else
using b_io_context = boost::asio::io_service;
#endif
/*!
* \brief This class implements the generation and reading of some Message Types
* defined in the RTCM 3.2 Standard, plus some utilities to handle messages.
*
* Generation of the following Message Types:
* 1001, 1002, 1003, 1004, 1005, 1006, 1008, 1019, 1020, 1029, 1045
*
* Decoding of the following Message Types:
* 1019, 1045
*
* Generation of the following Multiple Signal Messages:
* MSM1 (message types 1071, 1091)
* MSM2 (message types 1072, 1092)
* MSM3 (message types 1073, 1093)
* MSM4 (message types 1074, 1094)
* MSM5 (message types 1075, 1095)
* MSM6 (message types 1076, 1096)
* MSM7 (message types 1077, 1097)
*
* RTCM 3 message format (size in bits):
* +----------+--------+-----------+--------------------+----------+
* | preamble | 000000 | length | data message | parity |
* +----------+--------+-----------+--------------------+----------+
* |<-- 8 --->|<- 6 -->|<-- 10 --->|<--- length x 8 --->|<-- 24 -->|
* +----------+--------+-----------+--------------------+----------+
*
*
* (C) Carles Fernandez-Prades, 2015. cfernandez(at)cttc.es
*/
class Rtcm
{
public:
explicit Rtcm(uint16_t port = 2101); //!< Default constructor that sets TCP port of the RTCM message server and RTCM Station ID. 2101 is the standard RTCM port according to the Internet Assigned Numbers Authority (IANA). See https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml
~Rtcm();
/*!
* \brief Prints message type 1001 (L1-Only GPS RTK Observables)
*/
std::string print_MT1001(const Gps_Ephemeris& gps_eph, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables, uint16_t station_id);
/*!
* \brief Prints message type 1002 (Extended L1-Only GPS RTK Observables)
*/
std::string print_MT1002(const Gps_Ephemeris& gps_eph, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables, uint16_t station_id);
/*!
* \brief Prints message type 1003 (L1 & L2 GPS RTK Observables)
*/
std::string print_MT1003(const Gps_Ephemeris& ephL1, const Gps_CNAV_Ephemeris& ephL2, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables, uint16_t station_id);
/*!
* \brief Prints message type 1004 (Extended L1 & L2 GPS RTK Observables)
*/
std::string print_MT1004(const Gps_Ephemeris& ephL1, const Gps_CNAV_Ephemeris& ephL2, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables, uint16_t station_id);
/*!
* \brief Prints message type 1005 (Stationary Antenna Reference Point)
*/
std::string print_MT1005(uint32_t ref_id, double ecef_x, double ecef_y, double ecef_z, bool gps, bool glonass, bool galileo, bool non_physical, bool single_oscillator, uint32_t quarter_cycle_indicator);
/*!
* \brief Verifies and reads messages of type 1005 (Stationary Antenna Reference Point). Returns 1 if anything goes wrong, 0 otherwise.
*/
int32_t read_MT1005(const std::string& message, uint32_t& ref_id, double& ecef_x, double& ecef_y, double& ecef_z, bool& gps, bool& glonass, bool& galileo);
/*!
* \brief Prints message type 1006 (Stationary Antenna Reference Point, with Height Information)
*/
std::string print_MT1006(uint32_t ref_id, double ecef_x, double ecef_y, double ecef_z, bool gps, bool glonass, bool galileo, bool non_physical, bool single_oscillator, uint32_t quarter_cycle_indicator, double height);
std::string print_MT1005_test(); //!< For testing purposes
/*!
* \brief Prints message type 1008 (Antenna Descriptor & Serial Number)
*/
std::string print_MT1008(uint32_t ref_id, const std::string& antenna_descriptor, uint32_t antenna_setup_id, const std::string& antenna_serial_number);
/*!
* \brief Prints L1-Only GLONASS RTK Observables
* \details This GLONASS message type is not generally used or supported; type 1012 is to be preferred.
* \note Code added as part of GSoC 2017 program
* \param glonass_gnav_eph GLONASS GNAV Broadcast Ephemeris
* \param obs_time Time of observation at the moment of printing
* \param observables Set of observables as defined by the platform
* \return string with message contents
*/
std::string print_MT1009(const Glonass_Gnav_Ephemeris& glonass_gnav_eph, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables, uint16_t station_id);
/*!
* \brief Prints Extended L1-Only GLONASS RTK Observables
* \details This GLONASS message type is used when only L1 data is present and bandwidth is very tight, often 1012 is used in such cases.
* \note Code added as part of GSoC 2017 program
* \param glonass_gnav_eph GLONASS GNAV Broadcast Ephemeris
* \param obs_time Time of observation at the moment of printing
* \param observables Set of observables as defined by the platform
* \return string with message contents
*/
std::string print_MT1010(const Glonass_Gnav_Ephemeris& glonass_gnav_eph, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables, uint16_t station_id);
/*!
* \brief Prints L1&L2 GLONASS RTK Observables
* \details This GLONASS message type is not generally used or supported; type 1012 is to be preferred
* \note Code added as part of GSoC 2017 program
* \param glonass_gnav_eph GLONASS GNAV Broadcast Ephemeris
* \param obs_time Time of observation at the moment of printing
* \param observables Set of observables as defined by the platform
* \return string with message contents
*/
std::string print_MT1011(const Glonass_Gnav_Ephemeris& glonass_gnav_ephL1, const Glonass_Gnav_Ephemeris& glonass_gnav_ephL2, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables, uint16_t station_id);
/*!
* \brief Prints Extended L1&L2 GLONASS RTK Observables
* \details This GLONASS message type is the most common observational message type, with L1/L2/SNR content. This is one of the most common messages found.
* \note Code added as part of GSoC 2017 program
* \param glonass_gnav_eph GLONASS GNAV Broadcast Ephemeris
* \param obs_time Time of observation at the moment of printing
* \param observables Set of observables as defined by the platform
* \return string with message contents
*/
std::string print_MT1012(const Glonass_Gnav_Ephemeris& glonass_gnav_ephL1, const Glonass_Gnav_Ephemeris& glonass_gnav_ephL2, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables, uint16_t station_id);
/*!
* \brief Prints message type 1019 (GPS Ephemeris), should be broadcast in the event that
* the IODC does not match the IODE, and every 2 minutes.
*/
std::string print_MT1019(const Gps_Ephemeris& gps_eph);
/*!
* \brief Verifies and reads messages of type 1019 (GPS Ephemeris). Returns 1 if anything goes wrong, 0 otherwise.
*/
int32_t read_MT1019(const std::string& message, Gps_Ephemeris& gps_eph) const;
/*!
* \brief Prints message type 1020 (GLONASS Ephemeris).
* \note Code added as part of GSoC 2017 program
* \param glonass_gnav_eph GLONASS GNAV Broadcast Ephemeris
* \param glonass_gnav_utc_model GLONASS GNAV Clock Information
* \return Returns message type as a string type
*/
std::string print_MT1020(const Glonass_Gnav_Ephemeris& glonass_gnav_eph, const Glonass_Gnav_Utc_Model& glonass_gnav_utc_model);
/*!
* \brief Verifies and reads messages of type 1020 (GLONASS Ephemeris).
* \note Code added as part of GSoC 2017 program
* \param message Message to read as a string type
* \param glonass_gnav_eph GLONASS GNAV Broadcast Ephemeris
* \param glonass_gnav_utc_model GLONASS GNAV Clock Information
* \return Returns 1 if anything goes wrong, 0 otherwise.
*/
int32_t read_MT1020(const std::string& message, Glonass_Gnav_Ephemeris& glonass_gnav_eph, Glonass_Gnav_Utc_Model& glonass_gnav_utc_model) const;
/*!
* \brief Prints message type 1029 (Unicode Text String)
*/
std::string print_MT1029(uint32_t ref_id, const Gps_Ephemeris& gps_eph, double obs_time, const std::string& message);
/*!
* \brief Prints message type 1045 (Galileo Ephemeris), should be broadcast every 2 minutes
*/
std::string print_MT1045(const Galileo_Ephemeris& gal_eph);
/*!
* \brief Verifies and reads messages of type 1045 (Galileo Ephemeris). Returns 1 if anything goes wrong, 0 otherwise.
*/
int32_t read_MT1045(const std::string& message, Galileo_Ephemeris& gal_eph) const;
/*!
* \brief Prints messages of type MSM1 (Compact GNSS observables)
*/
std::string print_MSM_1(const Gps_Ephemeris& gps_eph,
const Gps_CNAV_Ephemeris& gps_cnav_eph,
const Galileo_Ephemeris& gal_eph,
const Glonass_Gnav_Ephemeris& glo_gnav_eph,
double obs_time,
const std::map<int32_t, Gnss_Synchro>& observables,
uint32_t ref_id,
uint32_t clock_steering_indicator,
uint32_t external_clock_indicator,
int32_t smooth_int,
bool divergence_free,
bool more_messages);
/*!
* \brief Prints messages of type MSM2 (Compact GNSS phaseranges)
*/
std::string print_MSM_2(const Gps_Ephemeris& gps_eph,
const Gps_CNAV_Ephemeris& gps_cnav_eph,
const Galileo_Ephemeris& gal_eph,
const Glonass_Gnav_Ephemeris& glo_gnav_eph,
double obs_time,
const std::map<int32_t, Gnss_Synchro>& observables,
uint32_t ref_id,
uint32_t clock_steering_indicator,
uint32_t external_clock_indicator,
int32_t smooth_int,
bool divergence_free,
bool more_messages);
/*!
* \brief Prints messages of type MSM3 (Compact GNSS pseudoranges and phaseranges)
*/
std::string print_MSM_3(const Gps_Ephemeris& gps_eph,
const Gps_CNAV_Ephemeris& gps_cnav_eph,
const Galileo_Ephemeris& gal_eph,
const Glonass_Gnav_Ephemeris& glo_gnav_eph,
double obs_time,
const std::map<int32_t, Gnss_Synchro>& observables,
uint32_t ref_id,
uint32_t clock_steering_indicator,
uint32_t external_clock_indicator,
int32_t smooth_int,
bool divergence_free,
bool more_messages);
/*!
* \brief Prints messages of type MSM4 (Full GNSS pseudoranges and phaseranges plus CNR)
*/
std::string print_MSM_4(const Gps_Ephemeris& gps_eph,
const Gps_CNAV_Ephemeris& gps_cnav_eph,
const Galileo_Ephemeris& gal_eph,
const Glonass_Gnav_Ephemeris& glo_gnav_eph,
double obs_time,
const std::map<int32_t, Gnss_Synchro>& observables,
uint32_t ref_id,
uint32_t clock_steering_indicator,
uint32_t external_clock_indicator,
int32_t smooth_int,
bool divergence_free,
bool more_messages);
/*!
* \brief Prints messages of type MSM5 (Full GNSS pseudoranges, phaseranges, phaserange rate and CNR)
*/
std::string print_MSM_5(const Gps_Ephemeris& gps_eph,
const Gps_CNAV_Ephemeris& gps_cnav_eph,
const Galileo_Ephemeris& gal_eph,
const Glonass_Gnav_Ephemeris& glo_gnav_eph,
double obs_time,
const std::map<int32_t, Gnss_Synchro>& observables,
uint32_t ref_id,
uint32_t clock_steering_indicator,
uint32_t external_clock_indicator,
int32_t smooth_int,
bool divergence_free,
bool more_messages);
/*!
* \brief Prints messages of type MSM6 (Full GNSS pseudoranges and phaseranges plus CNR, high resolution)
*/
std::string print_MSM_6(const Gps_Ephemeris& gps_eph,
const Gps_CNAV_Ephemeris& gps_cnav_eph,
const Galileo_Ephemeris& gal_eph,
const Glonass_Gnav_Ephemeris& glo_gnav_eph,
double obs_time,
const std::map<int32_t, Gnss_Synchro>& observables,
uint32_t ref_id,
uint32_t clock_steering_indicator,
uint32_t external_clock_indicator,
int32_t smooth_int,
bool divergence_free,
bool more_messages);
/*!
* \brief Prints messages of type MSM7 (Full GNSS pseudoranges, phaseranges, phaserange rate and CNR, high resolution)
*/
std::string print_MSM_7(const Gps_Ephemeris& gps_eph,
const Gps_CNAV_Ephemeris& gps_cnav_eph,
const Galileo_Ephemeris& gal_eph,
const Glonass_Gnav_Ephemeris& glo_gnav_eph,
double obs_time,
const std::map<int32_t, Gnss_Synchro>& observables,
uint32_t ref_id,
uint32_t clock_steering_indicator,
uint32_t external_clock_indicator,
int32_t smooth_int,
bool divergence_free,
bool more_messages);
/*!
* \brief Prints messages of type IGM01 (SSR Orbit Correction)
*/
std::vector<std::string> print_IGM01(const Galileo_HAS_data& has_data);
/*!
* \brief Prints messages of type IGM02 (SSR Clock Correction)
*/
std::vector<std::string> print_IGM02(const Galileo_HAS_data& has_data);
/*!
* \brief Prints messages of type IGM03 (SSR Combined Orbit and Clock Correction)
*/
std::vector<std::string> print_IGM03(const Galileo_HAS_data& has_data);
/*!
* \brief Prints messages of type IGM05 (SSR Bias Correction)
*/
std::vector<std::string> print_IGM05(const Galileo_HAS_data& has_data);
uint32_t lock_time(const Gps_Ephemeris& eph, double obs_time, const Gnss_Synchro& gnss_synchro); //!< Returns the time period in which GPS L1 signals have been continually tracked.
uint32_t lock_time(const Gps_CNAV_Ephemeris& eph, double obs_time, const Gnss_Synchro& gnss_synchro); //!< Returns the time period in which GPS L2 signals have been continually tracked.
uint32_t lock_time(const Galileo_Ephemeris& eph, double obs_time, const Gnss_Synchro& gnss_synchro); //!< Returns the time period in which Galileo signals have been continually tracked.
/*!
* \brief Locks time period in which GLONASS signals have been continually tracked.
* \note Code added as part of GSoC 2017 program
* \param eph GLONASS GNAV Broadcast Ephemeris
* \param obs_time Time of observation at the moment of printing
* \param observables Set of observables as defined by the platform
* \return Returns the time period in which GLONASS signals have been continually tracked.
*/
uint32_t lock_time(const Glonass_Gnav_Ephemeris& eph, double obs_time, const Gnss_Synchro& gnss_synchro);
std::string bin_to_hex(const std::string& s) const; //!< Returns a string of hexadecimal symbols from a string of binary symbols
std::string hex_to_bin(const std::string& s) const; //!< Returns a string of binary symbols from a string of hexadecimal symbols
std::string bin_to_binary_data(const std::string& s) const; //!< Returns a string of binary data from a string of binary symbols
std::string binary_data_to_bin(const std::string& s) const; //!< Returns a string of binary symbols from a string of binary data
uint32_t bin_to_uint(const std::string& s) const; //!< Returns an uint32_t from a string of binary symbols
int32_t bin_to_int(const std::string& s) const;
double bin_to_double(const std::string& s) const; //!< Returns double from a string of binary symbols
int32_t bin_to_sint(const std::string& s) const;
uint64_t hex_to_uint(const std::string& s) const; //!< Returns an uint64_t from a string of hexadecimal symbols
int64_t hex_to_int(const std::string& s) const; //!< Returns a int64_t from a string of hexadecimal symbols
bool check_CRC(const std::string& message) const; //!< Checks that the CRC of a RTCM package is correct
void run_server(); //!< Starts running the server
void stop_server(); //!< Stops the server
void send_message(const std::string& msg); //!< Sends a message through the server to all connected clients
bool is_server_running() const; //!< Returns true if the server is running, false otherwise
private:
//
// Generation of messages content
//
std::bitset<64> get_MT1001_4_header(uint32_t msg_number,
double obs_time,
const std::map<int32_t, Gnss_Synchro>& observables,
uint32_t ref_id,
uint32_t smooth_int,
bool sync_flag,
bool divergence_free);
std::bitset<58> get_MT1001_sat_content(const Gps_Ephemeris& eph, double obs_time, const Gnss_Synchro& gnss_synchro);
std::bitset<74> get_MT1002_sat_content(const Gps_Ephemeris& eph, double obs_time, const Gnss_Synchro& gnss_synchro);
std::bitset<101> get_MT1003_sat_content(const Gps_Ephemeris& ephL1, const Gps_CNAV_Ephemeris& ephL2, double obs_time, const Gnss_Synchro& gnss_synchroL1, const Gnss_Synchro& gnss_synchroL2);
std::bitset<125> get_MT1004_sat_content(const Gps_Ephemeris& ephL1, const Gps_CNAV_Ephemeris& ephL2, double obs_time, const Gnss_Synchro& gnss_synchroL1, const Gnss_Synchro& gnss_synchroL2);
std::bitset<152> get_MT1005_test();
/*!
* \brief Generates contents of message header for types 1009, 1010, 1011 and 1012. GLONASS RTK Message
* \note Code added as part of GSoC 2017 program
* \param msg_number Message type number, acceptable options include 1009 to 1012
* \param obs_time Time of observation at the moment of printing
* \param observables Set of observables as defined by the platform
* \param ref_id
* \param smooth_int
* \param divergence_free
* \return Returns the message header content as set of bits
*/
std::bitset<61> get_MT1009_12_header(uint32_t msg_number,
double obs_time,
const std::map<int32_t, Gnss_Synchro>& observables,
uint32_t ref_id,
uint32_t smooth_int,
bool sync_flag,
bool divergence_free);
/*!
* \brief Get the contents of the satellite specific portion of a type 1009 Message (GLONASS Basic RTK, L1 Only)
* \details Contents generated for each satellite. See table 3.5-11
* \note Code added as part of GSoC 2017 program
* \param ephGNAV Ephemeris for GLONASS GNAV in L1 satellites
* \param obs_time Time of observation at the moment of printing
* \param gnss_synchro Information generated by channels while processing the satellite
* \return Returns the message content as set of bits
*/
std::bitset<64> get_MT1009_sat_content(const Glonass_Gnav_Ephemeris& ephGNAV, double obs_time, const Gnss_Synchro& gnss_synchro);
/*!
* \brief Get the contents of the satellite specific portion of a type 1010 Message (GLONASS Extended RTK, L1 Only)
* \details Contents generated for each satellite. See table 3.5-12
* \note Code added as part of GSoC 2017 program
* \param ephGNAV Ephemeris for GLONASS GNAV in L1 satellites
* \param obs_time Time of observation at the moment of printing
* \param gnss_synchro Information generated by channels while processing the satellite
* \return Returns the message content as set of bits
*/
std::bitset<79> get_MT1010_sat_content(const Glonass_Gnav_Ephemeris& ephGNAV, double obs_time, const Gnss_Synchro& gnss_synchro);
/*!
* \brief Get the contents of the satellite specific portion of a type 1011 Message (GLONASS Basic RTK, L1 & L2)
* \details Contents generated for each satellite. See table 3.5-13
* \note Code added as part of GSoC 2017 program
* \param ephGNAVL1 Ephemeris for GLONASS GNAV in L1 satellites
* \param ephGNAVL2 Ephemeris for GLONASS GNAV in L2 satellites
* \param obs_time Time of observation at the moment of printing
* \param gnss_synchroL1 Information generated by channels while processing the GLONASS GNAV L1 satellite
* \param gnss_synchroL2 Information generated by channels while processing the GLONASS GNAV L2 satellite
* \return Returns the message content as set of bits
*/
std::bitset<107> get_MT1011_sat_content(const Glonass_Gnav_Ephemeris& ephL1, const Glonass_Gnav_Ephemeris& ephL2, double obs_time, const Gnss_Synchro& gnss_synchroL1, const Gnss_Synchro& gnss_synchroL2);
/*!
* \brief Get the contents of the satellite specific portion of a type 1012 Message (GLONASS Extended RTK, L1 & L2)
* \details Contents generated for each satellite. See table 3.5-14
* \note Code added as part of GSoC 2017 program
* \param ephGNAVL1 Ephemeris for GLONASS GNAV in L1 satellites
* \param ephGNAVL2 Ephemeris for GLONASS GNAV in L2 satellites
* \param obs_time Time of observation at the moment of printing
* \param gnss_synchroL1 Information generated by channels while processing the GLONASS GNAV L1 satellite
* \param gnss_synchroL2 Information generated by channels while processing the GLONASS GNAV L2 satellite
* \return Returns the message content as set of bits
*/
std::bitset<130> get_MT1012_sat_content(const Glonass_Gnav_Ephemeris& ephL1, const Glonass_Gnav_Ephemeris& ephL2, double obs_time, const Gnss_Synchro& gnss_synchroL1, const Gnss_Synchro& gnss_synchroL2);
std::string get_MSM_header(uint32_t msg_number,
double obs_time,
const std::map<int32_t, Gnss_Synchro>& observables,
uint32_t ref_id,
uint32_t clock_steering_indicator,
uint32_t external_clock_indicator,
int32_t smooth_int,
bool divergence_free,
bool more_messages);
std::string get_MSM_1_content_sat_data(const std::map<int32_t, Gnss_Synchro>& observables);
std::string get_MSM_4_content_sat_data(const std::map<int32_t, Gnss_Synchro>& observables);
std::string get_MSM_5_content_sat_data(const std::map<int32_t, Gnss_Synchro>& observables);
std::string get_MSM_1_content_signal_data(const std::map<int32_t, Gnss_Synchro>& observables);
std::string get_MSM_2_content_signal_data(const Gps_Ephemeris& ephNAV, const Gps_CNAV_Ephemeris& ephCNAV, const Galileo_Ephemeris& ephFNAV, const Glonass_Gnav_Ephemeris& ephGNAV, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables);
std::string get_MSM_3_content_signal_data(const Gps_Ephemeris& ephNAV, const Gps_CNAV_Ephemeris& ephCNAV, const Galileo_Ephemeris& ephFNAV, const Glonass_Gnav_Ephemeris& ephGNAV, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables);
std::string get_MSM_4_content_signal_data(const Gps_Ephemeris& ephNAV, const Gps_CNAV_Ephemeris& ephCNAV, const Galileo_Ephemeris& ephFNAV, const Glonass_Gnav_Ephemeris& ephGNAV, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables);
std::string get_MSM_5_content_signal_data(const Gps_Ephemeris& ephNAV, const Gps_CNAV_Ephemeris& ephCNAV, const Galileo_Ephemeris& ephFNAV, const Glonass_Gnav_Ephemeris& ephGNAV, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables);
std::string get_MSM_6_content_signal_data(const Gps_Ephemeris& ephNAV, const Gps_CNAV_Ephemeris& ephCNAV, const Galileo_Ephemeris& ephFNAV, const Glonass_Gnav_Ephemeris& ephGNAV, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables);
std::string get_MSM_7_content_signal_data(const Gps_Ephemeris& ephNAV, const Gps_CNAV_Ephemeris& ephCNAV, const Galileo_Ephemeris& ephFNAV, const Glonass_Gnav_Ephemeris& ephGNAV, double obs_time, const std::map<int32_t, Gnss_Synchro>& observables);
std::string get_IGM01_header(const Galileo_HAS_data& has_data, uint8_t nsys, bool ssr_multiple_msg_indicator);
std::string get_IGM01_content_sat(const Galileo_HAS_data& has_data, uint8_t nsys_index);
std::string get_IGM02_header(const Galileo_HAS_data& has_data, uint8_t nsys, bool ssr_multiple_msg_indicator);
std::string get_IGM02_content_sat(const Galileo_HAS_data& has_data, uint8_t nsys_index);
std::string get_IGM03_header(const Galileo_HAS_data& has_data, uint8_t nsys, bool ssr_multiple_msg_indicator);
std::string get_IGM03_content_sat(const Galileo_HAS_data& has_data, uint8_t nsys_index);
std::string get_IGM05_header(const Galileo_HAS_data& has_data, uint8_t nsys, bool ssr_multiple_msg_indicator);
std::string get_IGM05_content_sat(const Galileo_HAS_data& has_data, uint8_t nsys_index);
//
// Utilities
//
static std::map<std::string, int> galileo_signal_map;
static std::map<std::string, int> gps_signal_map;
std::vector<std::pair<int32_t, Gnss_Synchro>> sort_by_signal(const std::vector<std::pair<int32_t, Gnss_Synchro>>& synchro_map) const;
std::vector<std::pair<int32_t, Gnss_Synchro>> sort_by_PRN_mask(const std::vector<std::pair<int32_t, Gnss_Synchro>>& synchro_map) const;
boost::posix_time::ptime compute_GPS_time(const Gps_Ephemeris& eph, double obs_time) const;
boost::posix_time::ptime compute_GPS_time(const Gps_CNAV_Ephemeris& eph, double obs_time) const;
boost::posix_time::ptime compute_Galileo_time(const Galileo_Ephemeris& eph, double obs_time) const;
boost::posix_time::ptime compute_GLONASS_time(const Glonass_Gnav_Ephemeris& eph, double obs_time) const;
boost::posix_time::ptime gps_L1_last_lock_time[64];
boost::posix_time::ptime gps_L2_last_lock_time[64];
boost::posix_time::ptime gal_E1_last_lock_time[64];
boost::posix_time::ptime gal_E5_last_lock_time[64];
boost::posix_time::ptime glo_L1_last_lock_time[64];
boost::posix_time::ptime glo_L2_last_lock_time[64];
uint32_t lock_time_indicator(uint32_t lock_time_period_s);
uint32_t msm_lock_time_indicator(uint32_t lock_time_period_s);
uint32_t msm_extended_lock_time_indicator(uint32_t lock_time_period_s);
// SSR utilities
uint8_t ssr_update_interval(uint16_t validity_seconds) const;
//
// Classes for TCP communication
//
uint16_t RTCM_port;
// uint16_t RTCM_Station_ID;
class Rtcm_Message
{
public:
static const std::size_t header_length = 6;
static const std::size_t max_body_length = 1029;
Rtcm_Message()
: body_length_(0)
{
}
const char* data() const
{
return data_.data();
}
char* data()
{
return data_.data();
}
inline std::size_t length() const
{
return header_length + body_length_;
}
const char* body() const
{
return data_.data() + header_length;
}
char* body()
{
return data_.data() + header_length;
}
std::size_t body_length() const
{
return body_length_;
}
void body_length(std::size_t new_length)
{
body_length_ = new_length;
if (body_length_ > max_body_length)
{
body_length_ = max_body_length;
}
}
inline bool decode_header()
{
std::string header(data_.data(), header_length);
if (header[0] != 'G' || header[1] != 'S')
{
return false;
}
auto header2 = header.substr(2);
try
{
body_length_ = std::stoi(header2);
}
catch (const std::exception& e)
{
// invalid stoi conversion
body_length_ = 0;
return false;
}
if (body_length_ == 0)
{
return false;
}
if (body_length_ > max_body_length)
{
body_length_ = 0;
return false;
}
return true;
}
inline void encode_header()
{
std::stringstream ss;
ss << "GS" << std::setw(4) << std::max(std::min(static_cast<int>(body_length_), static_cast<int>(max_body_length)), 0);
std::string header = ss.str();
header.resize(header_length, ' ');
std::copy(header.begin(), header.end(), data_.begin());
}
private:
std::array<char, header_length + max_body_length> data_{};
std::size_t body_length_;
};
class RtcmListener
{
public:
virtual ~RtcmListener() = default;
virtual void deliver(const Rtcm_Message& msg) = 0;
};
class Rtcm_Listener_Room
{
public:
inline void join(const std::shared_ptr<RtcmListener>& participant)
{
participants_.insert(participant);
for (const auto& msg : recent_msgs_)
{
participant->deliver(msg);
}
}
inline void leave(const std::shared_ptr<RtcmListener>& participant)
{
participants_.erase(participant);
}
inline void deliver(const Rtcm_Message& msg)
{
recent_msgs_.push_back(msg);
while (recent_msgs_.size() > max_recent_msgs)
{
recent_msgs_.pop_front();
}
for (const auto& participant : participants_)
{
participant->deliver(msg);
}
}
private:
std::set<std::shared_ptr<RtcmListener>> participants_;
enum
{
max_recent_msgs = 1
};
std::deque<Rtcm_Message> recent_msgs_;
};
class Rtcm_Session
: public RtcmListener,
public std::enable_shared_from_this<Rtcm_Session>
{
public:
Rtcm_Session(boost::asio::ip::tcp::socket socket, Rtcm_Listener_Room& room) : socket_(std::move(socket)), room_(room) {}
inline void start()
{
room_.join(shared_from_this());
do_read_message_header();
}
inline void deliver(const Rtcm_Message& msg)
{
bool write_in_progress = !write_msgs_.empty();
write_msgs_.push_back(msg);
if (!write_in_progress)
{
do_write();
}
}
private:
inline void do_read_message_header()
{
auto self(shared_from_this());
boost::asio::async_read(socket_,
boost::asio::buffer(read_msg_.data(), Rtcm_Message::header_length),
[this, self](boost::system::error_code ec, std::size_t /*length*/) {
if (!ec and read_msg_.decode_header())
{
do_read_message_body();
}
else if (!ec and !read_msg_.decode_header())
{
client_says += read_msg_.data();
bool first = true;
while (client_says.length() >= 80)
{
if (first == true)
{
DLOG(INFO) << "Client says:";
first = false;
}
DLOG(INFO) << client_says;
client_says = client_says.substr(80, client_says.length() - 80);
}
do_read_message_header();
}
else
{
std::cout << "Closing connection with RTCM client\n";
room_.leave(shared_from_this());
}
});
}
inline void do_read_message_body()
{
auto self(shared_from_this());
boost::asio::async_read(socket_,
boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
[this, self](boost::system::error_code ec, std::size_t /*length*/) {
if (!ec)
{
room_.deliver(read_msg_);
// std::cout << "Delivered message (session): ";
// std::cout.write(read_msg_.body(), read_msg_.body_length());
// std::cout << '\n';
do_read_message_header();
}
else
{
std::cout << "Closing connection with RTCM client\n";
room_.leave(shared_from_this());
}
});
}
inline void do_write()
{
auto self(shared_from_this());
boost::asio::async_write(socket_,
boost::asio::buffer(write_msgs_.front().body(), write_msgs_.front().body_length()),
[this, self](boost::system::error_code ec, std::size_t /*length*/) {
if (!ec)
{
write_msgs_.pop_front();
if (!write_msgs_.empty())
{
do_write();
}
}
else
{
std::cout << "Closing connection with RTCM client\n";
room_.leave(shared_from_this());
}
});
}
boost::asio::ip::tcp::socket socket_;
Rtcm_Listener_Room& room_;
Rtcm_Message read_msg_;
std::deque<Rtcm_Message> write_msgs_;
std::string client_says;
};
class Tcp_Internal_Client
: public std::enable_shared_from_this<Tcp_Internal_Client>
{
public:
Tcp_Internal_Client(b_io_context& io_context,
boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
: io_context_(io_context), socket_(io_context)
{
do_connect(std::move(endpoint_iterator));
}
inline void close()
{
io_context_.post([this]() { socket_.close(); });
}
inline void write(const Rtcm_Message& msg)
{
io_context_.post(
[this, &msg]() {
bool write_in_progress = !write_msgs_.empty();
write_msgs_.push_back(msg);
if (!write_in_progress)
{
do_write();
}
});
}
private:
inline void do_connect(boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{
boost::asio::async_connect(socket_, std::move(endpoint_iterator),
[this](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator) {
if (!ec)
{
do_read_message();
}
else
{
std::cout << "Server is down.\n";
}
});
}
inline void do_read_message()
{
boost::asio::async_read(socket_,
boost::asio::buffer(read_msg_.data(), 1029),
[this](boost::system::error_code ec, std::size_t /*length*/) {
if (!ec)
{
do_read_message();
}
else
{
std::cout << "Error in client\n";
socket_.close();
}
});
}
inline void do_write()
{
boost::asio::async_write(socket_,
boost::asio::buffer(write_msgs_.front().data(), write_msgs_.front().length()),
[this](boost::system::error_code ec, std::size_t /*length*/) {
if (!ec)
{
write_msgs_.pop_front();
if (!write_msgs_.empty())
{
do_write();
}
}
else
{
socket_.close();
}
});
}
b_io_context& io_context_;
boost::asio::ip::tcp::socket socket_;
Rtcm_Message read_msg_;
std::deque<Rtcm_Message> write_msgs_;
};
class Queue_Reader
{
public:
Queue_Reader(b_io_context& io_context, std::shared_ptr<Concurrent_Queue<std::string>>& queue, int32_t port) : queue_(queue)
{
boost::asio::ip::tcp::resolver resolver(io_context);
std::string host("localhost");
std::string port_str = std::to_string(port);
auto queue_endpoint_iterator = resolver.resolve({host.c_str(), port_str.c_str()});
c = std::make_shared<Tcp_Internal_Client>(io_context, queue_endpoint_iterator);
}
inline void do_read_queue()
{
for (;;)
{
std::string message;
Rtcm_Message msg;
queue_->wait_and_pop(message); // message += '\n';
if (message == "Goodbye")
{
break;
}
const char* char_msg = message.c_str();
msg.body_length(message.length());
std::copy_n(char_msg, msg.body_length(), msg.body());
msg.encode_header();
c->write(msg);
}
}
private:
std::shared_ptr<Tcp_Internal_Client> c;
std::shared_ptr<Concurrent_Queue<std::string>>& queue_;
};
class Tcp_Server
{
public:
Tcp_Server(b_io_context& io_context, const boost::asio::ip::tcp::endpoint& endpoint)
: acceptor_(io_context), socket_(io_context)
{
acceptor_.open(endpoint.protocol());
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen();
do_accept();
}
inline void close_server()
{
socket_.close();
acceptor_.close();
}
private:
inline void do_accept()
{
acceptor_.async_accept(socket_, [this](boost::system::error_code ec) {
if (!ec)
{
if (first_client)
{
std::cout << "The TCP/IP server of RTCM messages is up and running. Accepting connections ...\n";
first_client = false;
}
else
{
std::cout << "Starting RTCM TCP/IP server session...\n";
boost::system::error_code ec2;
boost::asio::ip::tcp::endpoint endpoint = socket_.remote_endpoint(ec2);
if (ec2)
{
// Error creating remote_endpoint
std::cout << "Error getting remote IP address, closing session.\n";
LOG(INFO) << "Error getting remote IP address";
start_session = false;
}
else
{
std::string remote_addr = endpoint.address().to_string();
std::cout << "Serving client from " << remote_addr << '\n';
LOG(INFO) << "Serving client from " << remote_addr;
}
}
if (start_session)
{
std::make_shared<Rtcm_Session>(std::move(socket_), room_)->start();
}
}
else
{
std::cout << "Error when invoking a RTCM session. " << ec << '\n';
}
start_session = true;
do_accept();
});
}
boost::asio::ip::tcp::acceptor acceptor_;
boost::asio::ip::tcp::socket socket_;
Rtcm_Listener_Room room_;
bool first_client = true;
bool start_session = true;
};
b_io_context io_context;
std::shared_ptr<Concurrent_Queue<std::string>> rtcm_message_queue;
std::thread t;
std::thread tq;
std::list<Rtcm::Tcp_Server> servers;