1 | \
|
---|
2 | /* -------------------------------------------------------------------------
|
---|
3 | * BKG NTRIP Client
|
---|
4 | * -------------------------------------------------------------------------
|
---|
5 | *
|
---|
6 | * Class: gpssDecoder
|
---|
7 | *
|
---|
8 | * Purpose: Decode Data in GPSS Format
|
---|
9 | *
|
---|
10 | * Author: L. Mervart
|
---|
11 | *
|
---|
12 | * Created: 20-Dec-2008
|
---|
13 | *
|
---|
14 | * Changes:
|
---|
15 | *
|
---|
16 | * -----------------------------------------------------------------------*/
|
---|
17 |
|
---|
18 | #include <iostream>
|
---|
19 |
|
---|
20 | #include "gpssDecoder.h"
|
---|
21 | #include "bncapp.h"
|
---|
22 |
|
---|
23 | #define MODE_SEARCH 0
|
---|
24 | #define MODE_TYPE 1
|
---|
25 | #define MODE_EPOCH 2
|
---|
26 | #define MODE_EPOCH_BODY 3
|
---|
27 | #define MODE_EPOCH_CRC 4
|
---|
28 | #define MODE_EPOCH_ETX 5
|
---|
29 | #define MODE_EPH 6
|
---|
30 | #define MODE_EPH_BODY 7
|
---|
31 | #define MODE_EPH_CRC 8
|
---|
32 | #define MODE_EPH_ETX 9
|
---|
33 |
|
---|
34 | using namespace std;
|
---|
35 |
|
---|
36 | typedef struct epochHeader {
|
---|
37 | double t_epoch;
|
---|
38 | int n_svs;
|
---|
39 | } EPOCHHEADER;
|
---|
40 |
|
---|
41 | // Cyclic Redundancy Check
|
---|
42 | ////////////////////////////////////////////////////////////////////////////
|
---|
43 | unsigned short cal_crc(unsigned char *buf, int num) {
|
---|
44 | unsigned short polynomial = 0x8408;
|
---|
45 | unsigned short crc = 0;
|
---|
46 | int i;
|
---|
47 | while ( num-- ) {
|
---|
48 | crc = ( crc & 0xFF00 ) | ( *buf++^( crc & 0x00FF ) );
|
---|
49 | for( i=0; i<8; i++ ){
|
---|
50 | if( crc & 0x0001 ){
|
---|
51 | crc >>= 1;
|
---|
52 | crc ^= polynomial;
|
---|
53 | }
|
---|
54 | else{
|
---|
55 | crc >>= 1;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | return (crc);
|
---|
60 | }
|
---|
61 |
|
---|
62 | // Constructor
|
---|
63 | ////////////////////////////////////////////////////////////////////////////
|
---|
64 | gpssDecoder::gpssDecoder() : GPSDecoder() {
|
---|
65 | _mode = MODE_SEARCH;
|
---|
66 |
|
---|
67 | connect(this, SIGNAL(newGPSEph(gpsephemeris*)),
|
---|
68 | (bncApp*) qApp, SLOT(slotNewGPSEph(gpsephemeris*)));
|
---|
69 | }
|
---|
70 |
|
---|
71 | // Destructor
|
---|
72 | ////////////////////////////////////////////////////////////////////////////
|
---|
73 | gpssDecoder::~gpssDecoder() {
|
---|
74 | }
|
---|
75 |
|
---|
76 | //
|
---|
77 | ////////////////////////////////////////////////////////////////////////////
|
---|
78 | t_irc gpssDecoder::Decode(char* data, int dataLen, vector<string>& errmsg) {
|
---|
79 |
|
---|
80 | errmsg.clear();
|
---|
81 |
|
---|
82 | _buffer += QByteArray(data, dataLen);
|
---|
83 |
|
---|
84 | cout << "Decode: buffer length = " << _buffer.length() << endl;
|
---|
85 |
|
---|
86 | int iBeg;
|
---|
87 | while ( (iBeg = _buffer.indexOf(0x02)) != -1) {
|
---|
88 | _buffer = _buffer.mid(iBeg);
|
---|
89 |
|
---|
90 | int recordSize;
|
---|
91 | int crc;
|
---|
92 |
|
---|
93 | // Observations
|
---|
94 | // ------------
|
---|
95 | if (char(_buffer[1]) == 0x00) {
|
---|
96 |
|
---|
97 | int reqLength = 2 + sizeof(recordSize) + sizeof(EPOCHHEADER);
|
---|
98 |
|
---|
99 | if (_buffer.length() >= reqLength) {
|
---|
100 | EPOCHHEADER epochHdr;
|
---|
101 | memcpy(&epochHdr, _buffer.data() + 2 + sizeof(recordSize),
|
---|
102 | sizeof(epochHdr));
|
---|
103 |
|
---|
104 | reqLength += epochHdr.n_svs * sizeof(t_obsInternal) + sizeof(crc) + 1;
|
---|
105 |
|
---|
106 | if (_buffer.length() >= reqLength) {
|
---|
107 |
|
---|
108 | int checkLen = 2 + sizeof(recordSize) + sizeof(EPOCHHEADER) +
|
---|
109 | epochHdr.n_svs * sizeof(t_obsInternal);
|
---|
110 | memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
|
---|
111 | int crdCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
|
---|
112 |
|
---|
113 | cout << "Obs: " << crc << " " << crdCal << endl;
|
---|
114 |
|
---|
115 | for (int is = 0; is < epochHdr.n_svs; is++) {
|
---|
116 | t_obs* obs = new t_obs();
|
---|
117 | memcpy(&(obs->_o), _buffer.data() + 2 + sizeof(recordSize) +
|
---|
118 | sizeof(epochHdr) + is * sizeof(t_obsInternal),
|
---|
119 | sizeof(t_obsInternal));
|
---|
120 | _obsList.push_back(obs);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | _buffer = _buffer.mid(reqLength);
|
---|
125 | }
|
---|
126 |
|
---|
127 | // Ephemeris
|
---|
128 | // ---------
|
---|
129 | else if (char(_buffer[1]) == 0x01) {
|
---|
130 | int reqLength = 2 + sizeof(recordSize) + sizeof(gpsephemeris) +
|
---|
131 | sizeof(crc) + 1;
|
---|
132 |
|
---|
133 | if (_buffer.length() >= reqLength) {
|
---|
134 |
|
---|
135 | int checkLen = 2 + sizeof(recordSize) + sizeof(gpsephemeris);
|
---|
136 | memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
|
---|
137 | int crdCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
|
---|
138 |
|
---|
139 | cout << "Eph: " << crc << " " << crdCal << endl;
|
---|
140 |
|
---|
141 | gpsephemeris* gpsEph = new gpsephemeris;
|
---|
142 | memcpy(gpsEph, _buffer.data() + 2 + sizeof(recordSize),
|
---|
143 | sizeof(gpsephemeris));
|
---|
144 | emit newGPSEph(gpsEph);
|
---|
145 | }
|
---|
146 | _buffer = _buffer.mid(reqLength);
|
---|
147 | }
|
---|
148 |
|
---|
149 | else {
|
---|
150 | _buffer == _buffer.mid(1);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | return success;
|
---|
155 | }
|
---|