-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackets.h
320 lines (289 loc) · 8.05 KB
/
packets.h
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
#pragma once
#include "basic.h"
#include "modbus_common.h"
#include <stddef.h> // size_t
#include <stdint.h>
const uint32_t startWord = 0xFEEDABBE;
enum class PacketID : uint32_t
{
LogMessage = 0,
Heartbeat,
ParsingErrorInvalidLength,
ParsingErrorInvalidCRC,
ParsingErrorInvalidID,
ParsingErrorInvalidSequence,
ParsingErrorDroppedBytes,
WatchdogTimeout,
VfdSetFrequency,
VfdStatus,
ModbusError,
DummyPacket,
NumIDs,
};
enum class PacketOrigin : uint32_t
{
Internal = 0,
HostToTarget,
TargetToHost,
HostToMonitor,
MonitorToAscii,
UnitTest,
TargetTest,
NumOrigins,
};
// -------------
const uint32_t maxLogMsgLength = 256;
struct LogMessage
{
uint32_t length;
char msg[maxLogMsgLength];
};
// ----------
union ParsingError
{
// Thrown internally if length is too small or large
uint32_t invalidLength;
// Thrown internally if CRC does not match
struct
{
uint32_t provided;
uint32_t calculated;
} invalidCRC;
// Thrown internally if packet ID is out of range
uint32_t invalidID;
/*
* Thrown whenever a sequence number is out of "sequence".
* Thrown once whenever a group of packets is skipped.
* 1
* 2
* 4 <- error
* 5
* 6
*
* Thrown multiple times whenever there's an ordering issue, since
* the new "unexpected" number becomes the "previous" number.
* 1
* 2
* 4 <- error
* 3 <- error
* 5 <- error
* 6
*
*/
struct
{
uint32_t provided;
uint32_t expected;
} invalidSequence;
// Thrown internally whenever bytes must be discarded
uint32_t droppedBytes;
};
struct WatchdogTimeout
{
uint32_t unresponsiveTicks;
// Name length should match configMAX_TASK_NAME_LEN,
// but can't include FreeRTOSConfig.h,
// so hardcoding here and asserting in watchdog_task.h
char name[16];
};
// Sent from PC to set VFD frequency
struct VfdSetFrequency
{
uint8_t node;
uint16_t frequency;
};
// Sent from uC to PC
// Contents of status registers starting at modbus address 48449
struct __attribute__((__packed__)) VfdStatus
{
struct
{
uint16_t error;
uint16_t state; // could pretty-print or just dump masked hex/bin
uint16_t freqCmd;
uint16_t freqOut;
uint16_t currentOut;
uint16_t dcBusVoltage; // Don't really care about this, should be constant
uint16_t motorOutputVoltage; // Seems like this would be constant
uint16_t rpm; // Might be different than freqOut * 60
} payload;
uint8_t nodeAddress;
};
// Dummy packet for testing
struct DummyPacket
{
uint32_t outId;
uint8_t payload[64];
};
enum class ModbusErrorID : uint16_t
{
BadEchoNotEnoughBytes,
BadEchoMismatchedContents,
BadResponseNotEnoughBytes,
BadResponseMalformedPacket,
ResponseException,
ExtraBytes,
};
struct __attribute__((__packed__)) ModbusError
{
ModbusErrorID id;
uint8_t node;
FunctionCode command;
// These additional union fields are only used for some IDs.
union
{
struct
{
uint32_t actual;
uint32_t expected;
} bytes;
ExceptionCode exceptionCode;
};
};
// Packet
struct Packet
{
uint32_t length; // The length of the entire packet, not including wrapper
uint32_t sequenceNum; // Incrementing number to check for dropped packets
// Note that sequence numbers are re-applied before transmitting,
// so numbers noted in invalidSequence error are only accurate from the
// perspective of the device generating that error packet.
PacketOrigin origin;
PacketID id;
union
{
LogMessage logMessage;
ParsingError parsingError;
WatchdogTimeout watchdogTimeout;
VfdSetFrequency vfdSetFrequency;
VfdStatus vfdStatus;
ModbusError modbusError;
DummyPacket dummy;
} body;
// Would be nicer to omit 'body' so this could be an anonymous union
// which makes it a bit more convenient to use, but then can't calculate
// minimum packet sizes.
};
// Todo - reduce field sizes and convert to architecture-independent packed struct
struct WrappedPacket
{
uint32_t magicStart; // Helps with faster re-syncing than re-calculating full crc at each new offset.
uint32_t crc;
Packet packet;
};
const uint32_t maxWrappedPacketLength = sizeof(WrappedPacket);
const uint32_t minWrappedPacketLength = sizeof(WrappedPacket) - sizeof(WrappedPacket::packet.body);
const uint32_t minPacketLength = sizeof(Packet) - sizeof(Packet::body);
const uint32_t wrapperLength = sizeof(WrappedPacket) - sizeof(WrappedPacket::packet);
// --------
// Size of just the unique packet body
constexpr uint32_t packetBodySizeFromID(PacketID id)
{
switch (id) {
case PacketID::LogMessage: {
return sizeof(Packet::body.logMessage); // maximum size
}
case PacketID::Heartbeat: {
return 0;
}
case PacketID::ParsingErrorInvalidLength: {
return sizeof(Packet::body.parsingError.invalidLength);
}
case PacketID::ParsingErrorInvalidCRC: {
return sizeof(Packet::body.parsingError.invalidCRC);
}
case PacketID::ParsingErrorInvalidID: {
return sizeof(Packet::body.parsingError.invalidID);
}
case PacketID::ParsingErrorInvalidSequence: {
return sizeof(Packet::body.parsingError.invalidSequence);
}
case PacketID::ParsingErrorDroppedBytes: {
return sizeof(Packet::body.parsingError.droppedBytes);
}
case PacketID::WatchdogTimeout: {
return sizeof(Packet::body.watchdogTimeout);
}
case PacketID::VfdSetFrequency: {
return sizeof(Packet::body.vfdSetFrequency);
}
case PacketID::VfdStatus: {
return sizeof(Packet::body.vfdStatus);
}
case PacketID::ModbusError: {
// Not attempting to optimize packet size for IDs that don't
// require additional union fields (e.g. BadResponseMalformedPacket)
return sizeof(Packet::body.modbusError);
}
case PacketID::DummyPacket: {
return sizeof(Packet::body.dummy);
}
case PacketID::NumIDs: // This should not be called
{
return -1;
}
}
// This is never reached
// but compiler is not smart enough to know that, and demands a return
return -1;
}
// Size of packet body plus common packet field, but excluding wrapper fields
constexpr uint32_t packetSizeFromID(PacketID id)
{
return minPacketLength + packetBodySizeFromID(id);
}
// Size of the entire wrapped packet
constexpr uint32_t wrappedPacketSizeFromID(PacketID id)
{
return minWrappedPacketLength + packetBodySizeFromID(id);
}
// Size of the wrapped packet (assuming length field is set correctly)
inline uint32_t wrappedPacketSize(WrappedPacket& wrap)
{
return wrapperLength + wrap.packet.length;
}
constexpr const char* packetIdToString(PacketID id)
{
switch (id) {
ENUM_STRING(PacketID, LogMessage)
ENUM_STRING(PacketID, Heartbeat)
ENUM_STRING(PacketID, ParsingErrorInvalidLength)
ENUM_STRING(PacketID, ParsingErrorInvalidCRC)
ENUM_STRING(PacketID, ParsingErrorInvalidID)
ENUM_STRING(PacketID, ParsingErrorInvalidSequence)
ENUM_STRING(PacketID, ParsingErrorDroppedBytes)
ENUM_STRING(PacketID, WatchdogTimeout)
ENUM_STRING(PacketID, VfdSetFrequency)
ENUM_STRING(PacketID, VfdStatus)
ENUM_STRING(PacketID, ModbusError)
ENUM_STRING(PacketID, DummyPacket)
ENUM_STRING(PacketID, NumIDs)
}
return "InvalidID";
}
constexpr const char* packetOriginToString(PacketOrigin origin)
{
switch (origin) {
ENUM_STRING(PacketOrigin, Internal)
ENUM_STRING(PacketOrigin, HostToTarget)
ENUM_STRING(PacketOrigin, TargetToHost)
ENUM_STRING(PacketOrigin, HostToMonitor)
ENUM_STRING(PacketOrigin, MonitorToAscii)
ENUM_STRING(PacketOrigin, UnitTest)
ENUM_STRING(PacketOrigin, TargetTest)
ENUM_STRING(PacketOrigin, NumOrigins)
}
return "InvalidOrigin";
}
constexpr const char* modbusErrorIdToString(ModbusErrorID id)
{
switch (id) {
ENUM_STRING(ModbusErrorID, BadEchoNotEnoughBytes)
ENUM_STRING(ModbusErrorID, BadEchoMismatchedContents)
ENUM_STRING(ModbusErrorID, BadResponseNotEnoughBytes)
ENUM_STRING(ModbusErrorID, BadResponseMalformedPacket)
ENUM_STRING(ModbusErrorID, ResponseException)
ENUM_STRING(ModbusErrorID, ExtraBytes)
}
return "InvalidID";
};