-
Notifications
You must be signed in to change notification settings - Fork 1
/
GnssNavTimeUTCParser.cpp
193 lines (163 loc) · 5.71 KB
/
GnssNavTimeUTCParser.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
/*
* 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 "GnssHALNavTimeUTCParser"
#define LOG_NDEBUG 1
#include <log/log.h>
#include <ctime>
#include "GnssNavTimeUTCParser.h"
static const uint16_t blockSize = 20;
static const int xxCentury = 1900;
static const int january = 1;
static const int millenium = 100; //xxCentury + millenium corresponds to 2000 year in tm.tm_year value range
static const int64_t secondToNanoMultiplier = 1000 * 1000 * 1000;
// Using offsets according to the protocol description of UBX-NAV-TIMEUTC
enum NavTimeUTCOffsets : uint8_t {
iTow = 0,
timeAccuracy = 4,
nanoSecondFraction = 8,
year = 12,
month = 14,
day = 15,
hour = 16,
minute = 17,
second = 18,
flags = 19,
};
GnssNavTimeUTCParser::GnssNavTimeUTCParser(const uint8_t* payload, uint16_t payloadLen) :
mPayload(payload),
mPayloadLen(payloadLen)
{
parseNavTimeUTCMsg();
mPayload = nullptr;
mPayloadLen = 0;
}
void GnssNavTimeUTCParser::parseSingleBlock()
{
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
data.iTow = getValue<uint32_t>(&mPayload[NavTimeUTCOffsets::iTow]);
data.timeAccuracy = getValue<uint32_t>(&mPayload[NavTimeUTCOffsets::timeAccuracy]);
data.nanoSecondFraction = getValue<int32_t>(&mPayload[NavTimeUTCOffsets::nanoSecondFraction]);
data.year = getValue<uint16_t>(&mPayload[NavTimeUTCOffsets::year]);
data.month = mPayload[NavTimeUTCOffsets::month];
data.day = mPayload[NavTimeUTCOffsets::day];
data.hour = mPayload[NavTimeUTCOffsets::hour];
data.minute = mPayload[NavTimeUTCOffsets::minute];
data.second = mPayload[NavTimeUTCOffsets::second];
data.flags = mPayload[NavTimeUTCOffsets::flags];
ALOGV("[%s, line %d] Exit", __func__, __LINE__);
}
void GnssNavTimeUTCParser::parseNavTimeUTCMsg()
{
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
if (nullptr == mPayload || mPayloadLen != blockSize) {
ALOGV("[%s, line %d] Payload is not valid", __func__, __LINE__);
return;
}
parseSingleBlock();
if (checkFlags()) {
mValid = setTimeNano();
}
ALOGV("[%s, line %d] Exit", __func__, __LINE__);
}
bool GnssNavTimeUTCParser::setTimeNano()
{
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
tm recTime {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 0,
.tm_mon = 0,
.tm_year = 0,
.tm_wday = 0,
.tm_yday = 0,
.tm_isdst = -1,
.tm_gmtoff = 0,
.tm_zone = nullptr,
};
int curYear = static_cast<int>(data.year) - xxCentury;
if (curYear < millenium) {
ALOGV("[%s, line %d] Invalid year %d", __func__, __LINE__, curYear);
return false;
}
int curMonth = static_cast<int>(data.month) - january;
// 0 - for January, 11 - for December
if (curMonth < 0 || curMonth > 11) {
ALOGV("[%s, line %d] Invalid month %d", __func__, __LINE__, curMonth);
return false;
}
recTime.tm_year = curYear;
recTime.tm_mon = curMonth;
recTime.tm_mday = static_cast<int>(data.day);
recTime.tm_hour = static_cast<int>(data.hour);
recTime.tm_min = static_cast<int>(data.minute);
recTime.tm_sec = static_cast<int>(data.second);
time_t time = std::mktime(&recTime);
if (time < 0) {
ALOGV("Invalid time");
return false;
}
timeNano = static_cast<int64_t>(time * secondToNanoMultiplier);
timeNano += data.nanoSecondFraction;
ALOGV("[%s, line %d] Exit", __func__, __LINE__);
return true;
}
uint8_t GnssNavTimeUTCParser::retrieveSvInfo(MeasurementCb::GnssData &gnssData __unused)
{
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
return NotReady;
}
void GnssNavTimeUTCParser::dumpDebug()
{
hexdump("/data/app/NavTimeUTCParser",(void*)&data, sizeof(data));
ALOGV("[%s, line %d] iTOW %u, year %u, month %u, day %u, hour %u, minute %u, second %u, nanosec %u, timeAcc %u,"
" flags %u", __func__, __LINE__, data.iTow, data.year, data.month, data.day, data.hour, data.minute,
data.second, data.nanoSecondFraction, data.timeAccuracy, data.flags);
}
static bool isValidFlag(const uint8_t flags, const uint8_t expFlag)
{
uint8_t val = (flags & expFlag);
if (expFlag == val) {
return true;
}
return false;
}
static uint8_t getUtcStandard(const uint8_t flag)
{
const uint8_t defRet = 15; // default for Unknown standard
const uint8_t rifghtShift = 4;
uint8_t res = (flag >> rifghtShift);
if (res < defRet) {
return res;
}
return defRet;
}
bool GnssNavTimeUTCParser::checkFlags()
{
ALOGV("[%s, line %d] Entry", __func__, __LINE__);
const uint8_t validITowMask = 0x01;
const uint8_t validWeekMask = 0x02;
const uint8_t validUtcMask = 0x04;
uint8_t UTCStandardId = getUtcStandard(data.flags);
bool validITow = isValidFlag(data.flags, validITowMask);
bool validWeek = isValidFlag(data.flags, validWeekMask);
bool validUtc = isValidFlag(data.flags, validUtcMask);
(void)UTCStandardId;
(void)validITow;
ALOGV("[%s, line %d] flags: tow %u, week %u, utc %u, standard %u",
__func__, __LINE__, validITow, validWeek, validUtc, UTCStandardId);
return (validWeek && validUtc);
}