-
Notifications
You must be signed in to change notification settings - Fork 1
/
GnssRxmMeasxParser.cpp
394 lines (323 loc) · 12 KB
/
GnssRxmMeasxParser.cpp
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
/*
* Copyright (C) 2019 GlobalLogic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "GnssHALRxmMeasxParser"
#define LOG_NDEBUG 1
#include <log/log.h>
#include "GnssRxmMeasxParser.h"
static const uint8_t maxSvsNum = 64;
static const uint16_t repeatedBlockSize = 24;
static const uint16_t singleBlockSize = 44;
static const int64_t towAccScaleDown = 16;
static const double pseudorangeRateScaleUp = 0.04;
static const int64_t msToNsMultiplier = 1000000;
// Satellite vehicle numbering according to documentation
static const uint8_t gpsFirst = 1;
static const uint8_t gpsLast = 32;
static const uint8_t sbasOneFirst = 120;
static const uint8_t sbasOneLast = 151;
static const uint8_t sbasTwoFirst = 183;
static const uint8_t sbasTwoLast = 192;
static const uint8_t galileoFirst = 1;
static const uint8_t galileoLast = 36;
static const uint8_t qzssFirst = 193;
static const uint8_t qzssLast = 200;
static const uint8_t bdFirst = 1;
static const uint8_t bdLast = 37;
static const uint8_t glonassFcnFirst = 93;
static const uint8_t glonassFcnLast = 106;
static const uint8_t glonassFirst = 1;
static const uint8_t glonassLast = 24;
// Using offsets according to the protocol description of UBX-RXM-MEASX
enum RxmMeasxOffsets : uint8_t {
//first block offsets
version = 0,
gpsTOW = 4,
glonassTOW = 8,
bdsTOW = 12,
qzssTOW = 20,
gpsTOWacc = 24,
glonassTOWacc = 26,
bdsTOWacc = 28,
qzssTOWacc = 32,
numSvs = 34,
TOWset = 35,
//repeated block offsets
gnssIdOfst = 0,
svId = 1,
cn0 = 2,
multipath = 3,
pseudorangeRate = 4,
};
GnssRxmMeasxParser::GnssRxmMeasxParser(const uint8_t* payload,
uint16_t payloadLen) :
mPayload(payload),
mPayloadLen(payloadLen) {
parseRxmMeasMsg();
mPayload = nullptr;
mPayloadLen = 0;;
}
void GnssRxmMeasxParser::parseSingleBlock(const uint8_t* msg) {
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
if (nullptr != msg) {
meta.version = msg[RxmMeasxOffsets::version];
meta.numSvs = msg[RxmMeasxOffsets::numSvs];
meta.gpsTOW = getValue<uint32_t>(&msg[RxmMeasxOffsets::gpsTOW]);
meta.glonassTOW = getValue<uint32_t>(&msg[RxmMeasxOffsets::glonassTOW]);
meta.bdsTOW = getValue<uint32_t>(&msg[RxmMeasxOffsets::bdsTOW]);
meta.qzssTOW = getValue<uint32_t>(&msg[RxmMeasxOffsets::qzssTOW]);
meta.gpsTOWacc = getValue<uint16_t>(&msg[RxmMeasxOffsets::gpsTOWacc]);
meta.glonassTOWacc = getValue<uint16_t>(&msg[RxmMeasxOffsets::glonassTOWacc]);
meta.bdsTOWacc = getValue<uint16_t>(&msg[RxmMeasxOffsets::bdsTOWacc]);
meta.qzssTOWacc = getValue<uint16_t>(&msg[RxmMeasxOffsets::qzssTOWacc]);
meta.TOWset = msg[RxmMeasxOffsets::TOWset];
ALOGV("[%s, line %d] Exit", __func__, __LINE__);
}
}
void GnssRxmMeasxParser::parseRepeatedBlock(const uint8_t* msg) {
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
if (nullptr != msg) {
repeatedBlock_t block;
block.gnssId = msg[RxmMeasxOffsets::gnssIdOfst];
block.svId = msg[RxmMeasxOffsets::svId];
block.cn0 = msg[RxmMeasxOffsets::cn0];
block.multipath = msg[RxmMeasxOffsets::multipath];
block.pseudoRangeRate = getValue<int32_t>
(&msg[RxmMeasxOffsets::pseudorangeRate]);
data.push_back(block);
ALOGV("[%s, line %d] Exit", __func__, __LINE__);
}
}
void GnssRxmMeasxParser::parseRepeatedBlocks(const uint8_t* msg,
const uint16_t msgLen) {
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
if (nullptr == msg) {
ALOGV("[%s, line %d] nullptr input parameter", __func__, __LINE__);
return;
}
uint16_t offset = 0;
for (uint8_t i = 0; i < meta.numSvs; i++) {
if (offset + repeatedBlockSize <= msgLen) {
parseRepeatedBlock(msg + offset);
offset += repeatedBlockSize;
mValid = true;
}
}
ALOGV("[%s, line %d] Exit", __func__, __LINE__);
}
void GnssRxmMeasxParser::parseRxmMeasMsg() {
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
if (nullptr == mPayload
|| ((maxSvsNum * repeatedBlockSize) + singleBlockSize) < mPayloadLen) {
ALOGV("[%s, line %d] mPayload is null", __func__, __LINE__);
return;
}
if (mPayloadLen >= singleBlockSize + repeatedBlockSize) {
parseSingleBlock(mPayload);
if (meta.numSvs <= maxSvsNum) {
parseRepeatedBlocks(mPayload + singleBlockSize, mPayloadLen - singleBlockSize);
}
ALOGV("[%s, line %d] Exit", __func__, __LINE__);
}
}
CnstlType GnssRxmMeasxParser::getConstellationFromGnssId(const uint8_t gnssId) {
switch (gnssId) {
case UbxGnssId::GPS:
return CnstlType::GPS;
case UbxGnssId::GLONASS:
return CnstlType::GLONASS;
case UbxGnssId::GALILEO:
return CnstlType::GALILEO;
case UbxGnssId::QZSS:
return CnstlType::QZSS;
case UbxGnssId::BEIDOU:
return CnstlType::BEIDOU;
case UbxGnssId::SBAS:
return CnstlType::SBAS;
default:
return CnstlType::UNKNOWN;
}
}
uint32_t GnssRxmMeasxParser::getTOWforGnssId(const uint8_t gnssId) {
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
if (0 == meta.TOWset) {
mTOWstate = MeasurementState::STATE_UNKNOWN;
ALOGV("[%s, line %d] TOW unknown", __func__, __LINE__);
return 0;
}
uint32_t result = 0;
mTOWstate = MeasurementState::STATE_TOW_DECODED;
switch (gnssId) {
case UbxGnssId::GPS:
result = meta.gpsTOW;
break;
case UbxGnssId::GLONASS:
result = meta.glonassTOW;
break;
case UbxGnssId::QZSS:
result = meta.qzssTOW;
break;
case UbxGnssId::BEIDOU:
result = meta.bdsTOW;
break;
case UbxGnssId::SBAS:
case UbxGnssId::GALILEO:
mTOWstate = MeasurementState::STATE_TOW_KNOWN;
result = meta.gpsTOW; //TODO: find better solution
break;
}
if (0 == result) {
mTOWstate = MeasurementState::STATE_UNKNOWN;
} else {
mTOWstate = static_cast<MeasurementState>(mTOWstate |
MeasurementState::STATE_BIT_SYNC | MeasurementState::STATE_SUBFRAME_SYNC);
}
return result;
}
int64_t GnssRxmMeasxParser::getTOWaccForGnssId(const uint8_t gnssId) {
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
if (0 == meta.TOWset || MeasurementState::STATE_UNKNOWN == mTOWstate) {
ALOGV("[%s, line %d] TOW unknown", __func__, __LINE__);
return 0;
}
uint16_t towAccMs = 0;
switch (gnssId) {
case UbxGnssId::GPS:
towAccMs = meta.gpsTOWacc;
break;
case UbxGnssId::GLONASS:
towAccMs = meta.glonassTOWacc;
break;
case UbxGnssId::QZSS:
towAccMs = meta.qzssTOWacc;
break;
case UbxGnssId::BEIDOU:
towAccMs = meta.bdsTOWacc;
break;
case UbxGnssId::SBAS:
case UbxGnssId::GALILEO:
towAccMs = meta.gpsTOWacc; //TODO: find better solution
break;
default:
mTOWstate = MeasurementState::STATE_UNKNOWN;
return 0;
}
int64_t result = scaleUp(scaleDown(towAccMs, towAccScaleDown),
msToNsMultiplier);
return ((result > 0) ? result : 1);
}
uint8_t GnssRxmMeasxParser::getValidSvidForGnssId(const uint8_t gnssId,
const uint8_t svid) {
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
uint8_t resultSvid = svid;
switch (gnssId) {
case UbxGnssId::GPS:
inRange(gpsFirst, gpsLast, resultSvid);
break;
case UbxGnssId::SBAS:
inRanges(sbasOneFirst, sbasOneLast, sbasTwoFirst, sbasTwoLast, resultSvid);
break;
case UbxGnssId::GALILEO:
inRange(galileoFirst, galileoLast, resultSvid);
break;
case UbxGnssId::QZSS:
inRange(qzssFirst, qzssLast, resultSvid);
break;
case UbxGnssId::BEIDOU:
inRange(bdFirst, bdLast, resultSvid);
break;
case UbxGnssId::GLONASS:
inRanges(glonassFirst, glonassLast, glonassFcnFirst, glonassFcnLast,
resultSvid);
break;
}
ALOGV("[%s, line %d] svid = %u", __func__, __LINE__, resultSvid);
return resultSvid;
}
float GnssRxmMeasxParser::getCarrierFrequencyFromGnssId(const uint8_t gnssId) {
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
const float L1BandFrequency = 1575.42f;
const float B1BandFrequency = 1561.098f;
const float L1GlonassBandFrequency = 1602.562f;
const float scale = 1000000.0;
switch (gnssId) {
case UbxGnssId::GPS:
case UbxGnssId::SBAS:
case UbxGnssId::GALILEO:
case UbxGnssId::QZSS:
return scaleUp(L1BandFrequency, scale);
case UbxGnssId::BEIDOU:
return scaleUp(B1BandFrequency, scale);
case UbxGnssId::GLONASS:
return scaleUp(L1GlonassBandFrequency, scale);
default:
return 0;
}
}
void GnssRxmMeasxParser::getGnssMeasurement(MeasurementCb::GnssMeasurement&
instance,
const repeatedBlock_t& block) {
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
instance.carrierFrequencyHz = getCarrierFrequencyFromGnssId(block.gnssId);
instance.flags = 0;
if (instance.carrierFrequencyHz > 0.0f) {
instance.flags = static_cast<uint32_t>(MeasurementFlags::HAS_CARRIER_FREQUENCY);
}
instance.svid = getValidSvidForGnssId(block.gnssId, block.svId);
instance.constellation = getConstellationFromGnssId(block.gnssId);
instance.receivedSvTimeInNs = scaleUp(getTOWforGnssId(block.gnssId),
msToNsMultiplier);
instance.receivedSvTimeUncertaintyInNs = getTOWaccForGnssId(block.gnssId);
instance.state = static_cast<uint32_t>(mTOWstate);
instance.cN0DbHz = static_cast<double>(block.cn0);
instance.multipathIndicator = (block.multipath == 0) ?
MultipathId::INDICATIOR_NOT_PRESENT : MultipathId::INDICATOR_PRESENT;
instance.pseudorangeRateMps = scaleUp(block.pseudoRangeRate,
pseudorangeRateScaleUp);
instance.pseudorangeRateUncertaintyMps =
0.075; // TODO: set real value, at moment it is pure magic.
instance.accumulatedDeltaRangeState = static_cast<uint16_t>
(AccumulatedDeltaRangeState::ADR_STATE_UNKNOWN);
ALOGV("[%s, line %d] Exit", __func__, __LINE__);
}
uint8_t GnssRxmMeasxParser::retrieveSvInfo(MeasurementCb::GnssData& gnssData) {
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
if (mValid) {
uint32_t measurementCount = 0;
for (auto& block : data) {
if (measurementCount < maxSvsNum) {
getGnssMeasurement(gnssData.measurements[measurementCount], block);
++measurementCount;
}
}
gnssData.measurementCount = measurementCount;
ALOGV("[%s, line %d] Exit Done", __func__, __LINE__);
return RxmDone;
}
return NotReady;
}
void GnssRxmMeasxParser::dumpDebug() {
ALOGV("[%s, line %d] ", __func__, __LINE__);
const char* file = "/data/app/RxmMeasxParser_dump";
hexdump(file, (void*)&meta, sizeof(meta));
for (auto block : data) {
hexdump(file, (void*)&block, sizeof(repeatedBlock_t));
ALOGV("[%s, line %d] svid: %u, constel: %u, state: %u, cN0DbHz: %u, mpath: %u, pRangeRate: %f",
__func__, __LINE__, block.svId,
(uint16_t)getConstellationFromGnssId(block.gnssId),
(uint32_t)mTOWstate, block.cn0, (uint16_t)block.multipath,
scaleUp(block.pseudoRangeRate, pseudorangeRateScaleUp));
}
}