-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacket_utils.cpp
408 lines (367 loc) · 13.6 KB
/
packet_utils.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Functions for working with packets
*/
#include "packet_utils.h"
#include "basic.h"
#include "software_crc.h"
#include <stdio.h> // fwrite
#include <string.h> // memmove
#include <unistd.h> // write
#ifdef HOST_APP
#define println(format, ...) printf(format "\n", ##__VA_ARGS__)
#else
#define println(format, ...) (void)0
#endif
/*
* Helper-function for pre-populating some packet fields.
* Sets:
* - origin
* - ID
* - Minimum length
* - Must increment after adding data to variable-length field.
*/
void initializePacket(Packet& packet, PacketID id)
{
packet.origin = PacketOrigin::Internal;
packet.id = id;
packet.length = packetSizeFromID(id);
}
// A version of initializePacket() that just sets id and length fields
void setPacketIdAndLength(Packet& packet, PacketID id)
{
packet.id = id;
packet.length = packetSizeFromID(id);
}
/*
* Sets wrapper fields
*/
WrappedPacket& setPacketWrapper(WrappedPacket& wrap)
{
wrap.magicStart = startWord;
wrap.crc = crc32(reinterpret_cast<const uint8_t*>(&wrap.packet), wrap.packet.length);
return wrap;
}
/*
* More helper functions for populating packets
*/
void fillFreqPacket(WrappedPacket& wrap, uint32_t seq, uint32_t node, uint32_t freq)
{
initializePacket(wrap.packet, PacketID::VfdSetFrequency);
wrap.packet.sequenceNum = seq;
wrap.packet.body.vfdSetFrequency.node = node;
wrap.packet.body.vfdSetFrequency.frequency = freq;
setPacketWrapper(wrap);
}
void fillLengthErrorPacket(WrappedPacket& wrap, uint32_t len)
{
initializePacket(wrap.packet, PacketID::ParsingErrorInvalidLength);
wrap.packet.body.parsingError.invalidLength = len;
setPacketWrapper(wrap);
}
void fillDropErrorPacket(WrappedPacket& wrap, uint32_t drop)
{
initializePacket(wrap.packet, PacketID::ParsingErrorDroppedBytes);
wrap.packet.body.parsingError.droppedBytes = drop;
setPacketWrapper(wrap);
}
// Same as memcpy, but returns length of bytes written
uint32_t mymemcpy(void* dst, const void* src, uint32_t len)
{
memcpy(dst, src, len);
return len;
}
// Writes the entire wrapped packet to a file.
// Returns 1 if successfully written.
size_t fwriteWrapped(FILE* fp, WrappedPacket& wrap)
{
return fwrite(&wrap, wrapperLength + wrap.packet.length, 1, fp);
}
// Writes the entire wrapped packet to a file.
// Returns -1 for error.
ssize_t writeWrapped(int fd, WrappedPacket& wrap)
{
return write(fd, &wrap, wrapperLength + wrap.packet.length);
}
// Copies the entire wrapped packet to a buffer
uint32_t copyWrapped(void* buf, WrappedPacket& wrap)
{
return mymemcpy(buf, &wrap, wrapperLength + wrap.packet.length);
}
// Copies just the inner unwrapped packet to a buffer
uint32_t copyInner(void* buf, WrappedPacket& wrap)
{
return mymemcpy(buf, &wrap.packet, wrap.packet.length);
}
/*
* Takes a buffer and its length.
* Looks for any complete packets.
* Sends these complete packets to processPacket callback.
* Moves remaining unprocessed bytes to beginning of buf.
* Returns index of next free spot in buf, for example:
* - Returns 0 if ALL bytes processed.
* - Returns len if NO bytes processed.
* - Returns n where n is the number of trailing bytes
* (shifted to the start of the buffer) that have
* not yet formed a completed Packet.
* If parsing errors occur, will generate and send one or more
* of the following error packets to processPacket callback.
* These packets will have sequence number 0, which indicates they are
* generated internally.
*
* Todos to think about:
* Additional error packet allocated locally.
* If we made a packetExtractor object, could statically allocate
* the errorPacket.
* Could have sequence number history too, to enable that error message.
* There will likely need to be more than one of these extractors, so
* can't just do non-object static.
*/
uint32_t PacketParser::extractPackets(void* bufArg, uint32_t len)
{
uint32_t offset = 0;
uint32_t skippedBytes = 0;
uint8_t* buf = (uint8_t*)bufArg;
// Keep checking while there are still enough bytes to read
while (len >= offset + minWrappedPacketLength) {
// Check if this is a valid packet
auto wrap = (WrappedPacket*)(buf + offset);
Packet& packet = wrap->packet;
// Check for start word
if (wrap->magicStart != startWord) {
// Mismatch, try next byte
offset++;
skippedBytes++;
continue;
}
// Check if length is reasonable
if (packet.length < minPacketLength || //
packet.length > sizeof(Packet)) {
// Report length mismatch
initializePacket(errorPacket, PacketID::ParsingErrorInvalidLength);
errorPacket.body.parsingError.invalidLength = packet.length;
processer.processPacket(errorPacket);
// Mismatch, try next byte
offset++;
skippedBytes++;
continue;
}
// Check if id is reasonable
if (packet.id >= PacketID::NumIDs) {
// Report id mismatch
initializePacket(errorPacket, PacketID::ParsingErrorInvalidID);
errorPacket.body.parsingError.invalidID = static_cast<uint32_t>(packet.id);
processer.processPacket(errorPacket);
// Mismatch, try next byte
offset++;
skippedBytes++;
continue;
}
// Check if we have enough bytes of data for this packet
if (len < offset + wrapperLength + packet.length) {
// This is just an informative print, rather than an error.
// println("Waiting for remaining bytes of packet. Have %d, need %d.",
// len, offset + wrapperLength + packet.length);
// We likely have an incomplete packet.
// Need more data to be sure, so done with parsing for now.
// Should hopefully receive the rest of the packet on next parsing call.
break;
}
// Check crc
uint32_t calculatedCRC = crc32(reinterpret_cast<const uint8_t*>(&packet), packet.length);
if (calculatedCRC != wrap->crc) {
// Report crc mismatch
initializePacket(errorPacket, PacketID::ParsingErrorInvalidCRC);
errorPacket.body.parsingError.invalidCRC.provided = wrap->crc;
errorPacket.body.parsingError.invalidCRC.calculated = calculatedCRC;
processer.processPacket(errorPacket);
// Mismatch, try next byte
offset++;
skippedBytes++;
continue;
}
// We have a valid packet
// First, report how many bytes we had to skip (if any)
if (skippedBytes) {
// Report skipped bytes
initializePacket(errorPacket, PacketID::ParsingErrorDroppedBytes);
errorPacket.body.parsingError.droppedBytes = skippedBytes;
processer.processPacket(errorPacket);
// Reset skipped bytes
skippedBytes = 0;
}
// Check if sequence number is out of order
if (packet.sequenceNum != lastSeqNum + 1) {
// Report unexpected sequence number
initializePacket(errorPacket, PacketID::ParsingErrorInvalidSequence);
errorPacket.body.parsingError.invalidSequence.provided = packet.sequenceNum;
errorPacket.body.parsingError.invalidSequence.expected = lastSeqNum + 1;
processer.processPacket(errorPacket);
}
lastSeqNum = packet.sequenceNum;
// Send valid packet to callback
processer.processPacket(packet);
// Adjust offset
offset += wrapperLength + packet.length;
}
// Do a final reporting of skipped bytes
if (skippedBytes) {
// Report skipped bytes
initializePacket(errorPacket, PacketID::ParsingErrorDroppedBytes);
errorPacket.body.parsingError.droppedBytes = skippedBytes;
processer.processPacket(errorPacket);
}
// Shift out any consumed bytes
if (offset) {
len -= offset;
// Move leftover bytes to beginning of buffer
memmove(buf, buf + offset, len);
}
// Return number of leftover bytes
return len;
}
/*
* Returns rewrapped packet with updated sequence number.
* Updates internally-tracked sequence number.
*/
WrappedPacket& PacketSequencer::rewrap(WrappedPacket& wrap)
{
wrap.packet.sequenceNum = num++;
return setPacketWrapper(wrap);
}
/*
* Generates human-readable string for packets
*/
uint32_t snprintPacket(char* buf, uint32_t len, const Packet& packet)
{
uint32_t n = 0;
n += snprintf(buf + n,
len - n, //
"Sequence %u, Origin %u %s, ID %u %s: ",
packet.sequenceNum,
static_cast<uint32_t>(packet.origin),
packetOriginToString(packet.origin),
static_cast<uint32_t>(packet.id),
packetIdToString(packet.id));
switch (packet.id) {
case PacketID::LogMessage: {
return snprintf(buf + n,
len - n, //
"%s",
packet.body.logMessage.msg);
}
case PacketID::Heartbeat: return n;
case PacketID::ParsingErrorInvalidLength: {
return n + snprintf(buf + n,
len - n, //
"%u",
packet.body.parsingError.invalidLength);
}
case PacketID::ParsingErrorInvalidCRC: {
return n + snprintf(buf + n,
len - n, //
"provided 0x%08X, calculated 0x%08X",
packet.body.parsingError.invalidCRC.provided,
packet.body.parsingError.invalidCRC.calculated);
}
case PacketID::ParsingErrorInvalidID: {
return n + snprintf(buf + n,
len - n, //
"%u",
packet.body.parsingError.invalidID);
}
case PacketID::ParsingErrorInvalidSequence: {
return n + snprintf(buf + n,
len - n, //
"provided %u, expected %u",
packet.body.parsingError.invalidSequence.provided,
packet.body.parsingError.invalidSequence.expected);
}
case PacketID::ParsingErrorDroppedBytes: {
return n + snprintf(buf + n,
len - n, //
"%u",
packet.body.parsingError.droppedBytes);
}
case PacketID::WatchdogTimeout: {
n += snprintf(buf + n, //
min<size_t>(len - n, sizeof(WatchdogTimeout::name) + 1),
"%s",
packet.body.watchdogTimeout.name);
return n + snprintf(buf + n, //
len - n,
" unresponsive for %d ticks",
packet.body.watchdogTimeout.unresponsiveTicks);
}
case PacketID::VfdSetFrequency: {
return n + snprintf(buf + n,
len - n, //
"node %u, frequency %u.%u Hz",
packet.body.vfdSetFrequency.node,
packet.body.vfdSetFrequency.frequency / 10,
packet.body.vfdSetFrequency.frequency % 10);
}
case PacketID::VfdStatus: {
// todo conversions and improve readability
return n + snprintf(buf + n,
len - n, //
"node %u"
" error 0x%04X,"
" state 0x%04X,"
" freqCmd %u.%u Hz,"
" freqOut %u.%u Hz,"
" currentOut %u A,"
" dcBusVoltage %u.%u V,"
" motorOutputVoltage %u.%u V,"
" rpm %u",
packet.body.vfdStatus.nodeAddress,
packet.body.vfdStatus.payload.error,
packet.body.vfdStatus.payload.state,
packet.body.vfdStatus.payload.freqCmd / 10,
packet.body.vfdStatus.payload.freqCmd % 10,
packet.body.vfdStatus.payload.freqOut / 10,
packet.body.vfdStatus.payload.freqOut % 10,
packet.body.vfdStatus.payload.currentOut,
packet.body.vfdStatus.payload.dcBusVoltage / 10,
packet.body.vfdStatus.payload.dcBusVoltage % 10,
packet.body.vfdStatus.payload.motorOutputVoltage / 10,
packet.body.vfdStatus.payload.motorOutputVoltage % 10,
packet.body.vfdStatus.payload.rpm);
}
case PacketID::ModbusError: {
n += snprintf(buf + n, //
len - n,
"node %u, cmd 0x%x, %s: ",
packet.body.modbusError.node,
(uint8_t)packet.body.modbusError.command,
modbusErrorIdToString(packet.body.modbusError.id));
switch (packet.body.modbusError.id) {
case ModbusErrorID::BadEchoNotEnoughBytes:
case ModbusErrorID::BadResponseNotEnoughBytes:
case ModbusErrorID::ExtraBytes:
return n + snprintf(buf + n, //
len - n,
"actual %u, expected %u",
packet.body.modbusError.bytes.actual,
packet.body.modbusError.bytes.expected);
case ModbusErrorID::ResponseException:
return n + snprintf(buf + n, //
len - n,
"0x%x %s",
(uint8_t)packet.body.modbusError.exceptionCode,
exceptionCodeToString(packet.body.modbusError.exceptionCode));
case ModbusErrorID::BadEchoMismatchedContents:
case ModbusErrorID::BadResponseMalformedPacket: return n;
}
}
case PacketID::DummyPacket: {
return n + snprintf(buf + n,
len - n, //
"%u",
packet.body.dummy.outId);
}
// This should not be called
case PacketID::NumIDs: return n;
}
// This is never reached
return n;
}