-
Notifications
You must be signed in to change notification settings - Fork 0
/
UbxParser.cpp
212 lines (169 loc) · 6.96 KB
/
UbxParser.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
/*
MIT License
Copyright (c) 2024 Pavel Slama
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "UbxParser.h"
UbxParser::UbxParser() {}
UbxParser::~UbxParser(void) {
while (_oobs) {
struct oob_t *oob = _oobs;
_oobs = oob->next;
delete oob;
}
}
uint16_t UbxParser::buildMessage(char *buffer, UbxClassId class_id, char id, const void *payload,
uint16_t payload_len) {
uint8_t checksum[2] = {0};
buffer[0] = UBX_SYNC_CHAR1;
buffer[1] = UBX_SYNC_CHAR2;
buffer[2] = class_id;
buffer[3] = id;
buffer[4] = payload_len & UCHAR_MAX;
buffer[5] = payload_len >> 8;
if (payload_len > 0 && payload != nullptr) {
memcpy(buffer + UBX_HEADER_LEN, payload, payload_len);
}
for (uint16_t i = 2; i < (UBX_HEADER_LEN + payload_len); i++) { // exclude SYNC
checksum[0] += buffer[i];
checksum[1] += checksum[0];
}
memcpy(buffer + UBX_HEADER_LEN + payload_len, checksum, sizeof(checksum));
return UBX_HEADER_LEN + payload_len + UBX_CHECKSUM_LEN;
}
void UbxParser::oob(UbxClassId class_id, char id, Callback<void()> cb) {
// In case we have it already in, remove it
remove_oob(class_id, id);
ubx_debug("Registering oob CLASS: %02hhX, ID: %02hhX", class_id, id);
struct oob_t *oob = new struct oob_t;
oob->class_id = class_id;
oob->id = id;
oob->cb = cb;
oob->next = _oobs;
_oobs = oob;
}
void UbxParser::remove_oob(UbxClassId class_id, char id) {
struct oob_t *prev = nullptr;
struct oob_t *oob = _oobs;
while (oob) {
if (oob->class_id == class_id && oob->id == id) {
if (prev) {
prev->next = oob->next;
} else {
_oobs = oob->next;
}
ubx_debug("Removing oob CLASS: %02hhX, ID: %02hhX", class_id, id);
delete oob;
return;
}
prev = oob;
oob = oob->next;
}
}
void UbxParser::parse(const char *buffer, uint16_t length) {
static StateMachine state = StateMachine::SYNC_SEARCH_1;
static uint16_t data_index = 0;
// ubx_debug("Read: %s", tr_array(reinterpret_cast<const uint8_t *>(buffer), length));
for (uint16_t i = 0; i < length; i++) {
switch (state) {
case StateMachine::SYNC_SEARCH_1:
if (buffer[i] == UBX_SYNC_CHAR1) {
state = StateMachine::SYNC_SEARCH_2;
}
break;
case StateMachine::SYNC_SEARCH_2:
if (buffer[i] == UBX_SYNC_CHAR2) {
state = StateMachine::CLASS_READ;
} else {
state = StateMachine::SYNC_SEARCH_1;
}
break;
case StateMachine::CLASS_READ:
msg.classId = buffer[i];
state = StateMachine::ID_READ;
add_checksum(buffer[i]);
break;
case StateMachine::ID_READ: {
bool process = false;
msg.id = buffer[i];
for (struct oob_t *oob = _oobs; oob; oob = oob->next) {
if (oob->class_id == msg.classId && (oob->class_id == UBX_ACK || oob->id == msg.id)) {
ubx_debug("This is a packet we are looking for, class: %02X, id: %02X", msg.classId, msg.id);
process = true;
break;
}
}
if (process) {
state = StateMachine::LENGTH_READ_1;
add_checksum(buffer[i]);
} else {
ubx_warning("Skipping class: %02X, id: %02X", msg.classId, msg.id);
state = StateMachine::SYNC_SEARCH_1;
memset(_checksum_calc, 0, sizeof(_checksum_calc)); // reset checksum
}
break;
}
case StateMachine::LENGTH_READ_1:
msg.length = buffer[i];
state = StateMachine::LENGTH_READ_2;
add_checksum(buffer[i]);
break;
case StateMachine::LENGTH_READ_2:
msg.length |= ((uint16_t)buffer[i] << 8);
msg.data = std::make_unique<uint8_t[]>(msg.length);
data_index = 0;
state = StateMachine::DATA_READ;
add_checksum(buffer[i]);
ubx_debug("Message length: %u", msg.length);
break;
case StateMachine::DATA_READ:
msg.data[data_index++] = buffer[i];
add_checksum(buffer[i]);
if (data_index >= msg.length) {
state = StateMachine::CHECKSUM_READ_1;
}
break;
case StateMachine::CHECKSUM_READ_1:
msg.checksum = ((uint16_t)buffer[i] << 8);
state = StateMachine::CHECKSUM_READ_2;
break;
case StateMachine::CHECKSUM_READ_2:
msg.checksum |= buffer[i];
uint16_t calculatedChecksum = (_checksum_calc[0] << 8) | _checksum_calc[1];
if (msg.checksum == calculatedChecksum) {
ubx_debug("Checksum OK");
for (struct oob_t *oob = _oobs; oob; oob = oob->next) {
if (oob->class_id == msg.classId && (oob->class_id == UBX_ACK || oob->id == msg.id)) {
oob->cb.call();
break;
}
}
} else {
ubx_error("Wrong checksum");
}
msg = message_t(); // resets automatically due to unique_ptr
state = StateMachine::SYNC_SEARCH_1; // reset to initial state
data_index = 0;
memset(_checksum_calc, 0, sizeof(_checksum_calc)); // reset checksum
break;
}
}
}
void UbxParser::add_checksum(char b) {
_checksum_calc[0] += +b;
_checksum_calc[1] += _checksum_calc[0];
}