-
Notifications
You must be signed in to change notification settings - Fork 17k
/
msp.cpp
321 lines (278 loc) · 10.8 KB
/
msp.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
#include <AP_Math/AP_Math.h>
#include <AP_SerialManager/AP_SerialManager.h>
#include "msp.h"
#include <stdint.h>
#if HAL_MSP_ENABLED
/*
ported from betaflight/src/main/msp/msp_serial.c
*/
uint8_t MSP::msp_serial_checksum_buf(uint8_t checksum, const uint8_t *data, uint32_t len)
{
while (len-- > 0) {
checksum ^= *data++;
}
return checksum;
}
/*
ported from betaflight/src/main/msp/msp_serial.c
*/
uint32_t MSP::msp_serial_send_frame(msp_port_t *msp, const uint8_t * hdr, uint32_t hdr_len, const uint8_t * data, uint32_t data_len, const uint8_t * crc, uint32_t crc_len)
{
if (msp->uart == nullptr) {
return 0;
}
// We are allowed to send out the response if
// a) TX buffer is completely empty (we are talking to well-behaving party that follows request-response scheduling;
// this allows us to transmit jumbo frames bigger than TX buffer (serialWriteBuf will block, but for jumbo frames we don't care)
// b) Response fits into TX buffer
const uint32_t total_frame_length = hdr_len + data_len + crc_len;
if (!msp->uart->tx_pending() && (msp->uart->txspace() < total_frame_length)) {
return 0;
}
// Transmit frame
msp->uart->write(hdr, hdr_len);
msp->uart->write(data, data_len);
msp->uart->write(crc, crc_len);
return total_frame_length;
}
/*
ported from betaflight/src/main/msp/msp_serial.c
*/
uint32_t MSP::msp_serial_encode(msp_port_t *msp, msp_packet_t *packet, msp_version_e msp_version, bool is_request)
{
static const uint8_t msp_magic[MSP_VERSION_COUNT] = MSP_VERSION_MAGIC_INITIALIZER;
/*
Note: after calling sbuf_switch_to_reader() sbuf_bytes_remaining() returns the size of the packet
*/
const uint16_t data_len = sbuf_bytes_remaining(&packet->buf);
uint8_t code;
if (is_request) {
code = '<';
} else if (packet->result == MSP_RESULT_ERROR) {
code = '!';
} else {
code = '>';
}
const uint8_t hdr_buf[16] = { '$', msp_magic[msp_version], code };
uint8_t crc_buf[2];
uint32_t hdr_len = 3;
uint32_t crc_len = 0;
#define V1_CHECKSUM_STARTPOS 3
if (msp_version == MSP_V1) {
msp_header_v1_t * hdr_v1 = (msp_header_v1_t *)&hdr_buf[hdr_len];
hdr_len += sizeof(msp_header_v1_t);
hdr_v1->cmd = packet->cmd;
// Add JUMBO-frame header if necessary
if (data_len >= JUMBO_FRAME_SIZE_LIMIT) {
msp_header_jumbo_t * hdr_jumbo = (msp_header_jumbo_t *)&hdr_buf[hdr_len];
hdr_len += sizeof(msp_header_jumbo_t);
hdr_v1->size = JUMBO_FRAME_SIZE_LIMIT;
hdr_jumbo->size = data_len;
} else {
hdr_v1->size = data_len;
}
// Pre-calculate CRC
crc_buf[crc_len] = msp_serial_checksum_buf(0, hdr_buf + V1_CHECKSUM_STARTPOS, hdr_len - V1_CHECKSUM_STARTPOS);
crc_buf[crc_len] = msp_serial_checksum_buf(crc_buf[crc_len], sbuf_ptr(&packet->buf), data_len);
crc_len++;
} else if (msp_version == MSP_V2_OVER_V1) {
msp_header_v1_t * hdr_v1 = (msp_header_v1_t *)&hdr_buf[hdr_len];
hdr_len += sizeof(msp_header_v1_t);
msp_header_v2_t * hdr_v2 = (msp_header_v2_t *)&hdr_buf[hdr_len];
hdr_len += sizeof(msp_header_v2_t);
const uint32_t v1_payload_size = sizeof(msp_header_v2_t) + data_len + 1; // MSPv2 header + data payload + MSPv2 checksum
hdr_v1->cmd = MSP_V2_FRAME_ID;
// Add JUMBO-frame header if necessary
if (v1_payload_size >= JUMBO_FRAME_SIZE_LIMIT) {
msp_header_jumbo_t * hdr_jumbo = (msp_header_jumbo_t *)&hdr_buf[hdr_len];
hdr_len += sizeof(msp_header_jumbo_t);
hdr_v1->size = JUMBO_FRAME_SIZE_LIMIT;
hdr_jumbo->size = v1_payload_size;
} else {
hdr_v1->size = v1_payload_size;
}
// Fill V2 header
hdr_v2->flags = packet->flags;
hdr_v2->cmd = packet->cmd;
hdr_v2->size = data_len;
// V2 CRC: only V2 header + data payload
crc_buf[crc_len] = crc8_dvb_s2_update(0, (uint8_t *)hdr_v2, sizeof(msp_header_v2_t));
crc_buf[crc_len] = crc8_dvb_s2_update(crc_buf[crc_len], sbuf_ptr(&packet->buf), data_len);
crc_len++;
// V1 CRC: All headers + data payload + V2 CRC byte
crc_buf[crc_len] = msp_serial_checksum_buf(0, hdr_buf + V1_CHECKSUM_STARTPOS, hdr_len - V1_CHECKSUM_STARTPOS);
crc_buf[crc_len] = msp_serial_checksum_buf(crc_buf[crc_len], sbuf_ptr(&packet->buf), data_len);
crc_buf[crc_len] = msp_serial_checksum_buf(crc_buf[crc_len], crc_buf, crc_len);
crc_len++;
} else if (msp_version == MSP_V2_NATIVE) {
msp_header_v2_t * hdr_v2 = (msp_header_v2_t *)&hdr_buf[hdr_len];
hdr_len += sizeof(msp_header_v2_t);
hdr_v2->flags = packet->flags;
hdr_v2->cmd = packet->cmd;
hdr_v2->size = data_len;
crc_buf[crc_len] = crc8_dvb_s2_update(0, (uint8_t *)hdr_v2, sizeof(msp_header_v2_t));
crc_buf[crc_len] = crc8_dvb_s2_update(crc_buf[crc_len], sbuf_ptr(&packet->buf), data_len);
crc_len++;
} else {
// Shouldn't get here
return 0;
}
// Send the frame
return msp_serial_send_frame(msp, hdr_buf, hdr_len, sbuf_ptr(&packet->buf), data_len, crc_buf, crc_len);
}
/*
ported from betaflight/src/main/msp/msp_serial.c
*/
bool MSP::msp_parse_received_data(msp_port_t *msp, uint8_t c)
{
switch (msp->c_state) {
default:
case MSP_IDLE: // Waiting for '$' character
if (c == '$') {
msp->msp_version = MSP_V1;
msp->c_state = MSP_HEADER_START;
} else {
return false;
}
break;
case MSP_HEADER_START: // Waiting for 'M' (MSPv1 / MSPv2_over_v1) or 'X' (MSPv2 native)
switch (c) {
case 'M':
msp->c_state = MSP_HEADER_M;
break;
case 'X':
msp->c_state = MSP_HEADER_X;
break;
default:
msp->c_state = MSP_IDLE;
break;
}
break;
case MSP_HEADER_M: // Waiting for '<'
if (c == '<') {
msp->offset = 0;
msp->checksum1 = 0;
msp->checksum2 = 0;
msp->c_state = MSP_HEADER_V1;
} else {
msp->c_state = MSP_IDLE;
}
break;
case MSP_HEADER_X:
if (c == '<') {
msp->offset = 0;
msp->checksum2 = 0;
msp->msp_version = MSP_V2_NATIVE;
msp->c_state = MSP_HEADER_V2_NATIVE;
} else {
msp->c_state = MSP_IDLE;
}
break;
case MSP_HEADER_V1: // Now receive v1 header (size/cmd), this is already checksummable
msp->in_buf[msp->offset++] = c;
msp->checksum1 ^= c;
if (msp->offset == sizeof(msp_header_v1_t)) {
msp_header_v1_t * hdr = (msp_header_v1_t *)&msp->in_buf[0];
// Check incoming buffer size limit
if (hdr->size > MSP_PORT_INBUF_SIZE) {
msp->c_state = MSP_IDLE;
} else if (hdr->cmd == MSP_V2_FRAME_ID) {
// MSPv1 payload must be big enough to hold V2 header + extra checksum
if (hdr->size >= sizeof(msp_header_v2_t) + 1) {
msp->msp_version = MSP_V2_OVER_V1;
msp->c_state = MSP_HEADER_V2_OVER_V1;
} else {
msp->c_state = MSP_IDLE;
}
} else {
msp->data_size = hdr->size;
msp->cmd_msp = hdr->cmd;
msp->cmd_flags = 0;
msp->offset = 0; // re-use buffer
msp->c_state = msp->data_size > 0 ? MSP_PAYLOAD_V1 : MSP_CHECKSUM_V1; // If no payload - jump to checksum byte
}
}
break;
case MSP_PAYLOAD_V1:
msp->in_buf[msp->offset++] = c;
msp->checksum1 ^= c;
if (msp->offset == msp->data_size) {
msp->c_state = MSP_CHECKSUM_V1;
}
break;
case MSP_CHECKSUM_V1:
if (msp->checksum1 == c) {
msp->c_state = MSP_COMMAND_RECEIVED;
} else {
msp->c_state = MSP_IDLE;
}
break;
case MSP_HEADER_V2_OVER_V1: // V2 header is part of V1 payload - we need to calculate both checksums now
msp->in_buf[msp->offset++] = c;
msp->checksum1 ^= c;
msp->checksum2 = crc8_dvb_s2(msp->checksum2, c);
if (msp->offset == (sizeof(msp_header_v2_t) + sizeof(msp_header_v1_t))) {
msp_header_v2_t * hdrv2 = (msp_header_v2_t *)&msp->in_buf[sizeof(msp_header_v1_t)];
msp->data_size = hdrv2->size;
// Check for potential buffer overflow
if (hdrv2->size > MSP_PORT_INBUF_SIZE) {
msp->c_state = MSP_IDLE;
} else {
msp->cmd_msp = hdrv2->cmd;
msp->cmd_flags = hdrv2->flags;
msp->offset = 0; // re-use buffer
msp->c_state = msp->data_size > 0 ? MSP_PAYLOAD_V2_OVER_V1 : MSP_CHECKSUM_V2_OVER_V1;
}
}
break;
case MSP_PAYLOAD_V2_OVER_V1:
msp->checksum2 = crc8_dvb_s2(msp->checksum2, c);
msp->checksum1 ^= c;
msp->in_buf[msp->offset++] = c;
if (msp->offset == msp->data_size) {
msp->c_state = MSP_CHECKSUM_V2_OVER_V1;
}
break;
case MSP_CHECKSUM_V2_OVER_V1:
msp->checksum1 ^= c;
if (msp->checksum2 == c) {
msp->c_state = MSP_CHECKSUM_V1; // Checksum 2 correct - verify v1 checksum
} else {
msp->c_state = MSP_IDLE;
}
break;
case MSP_HEADER_V2_NATIVE:
msp->in_buf[msp->offset++] = c;
msp->checksum2 = crc8_dvb_s2(msp->checksum2, c);
if (msp->offset == sizeof(msp_header_v2_t)) {
msp_header_v2_t * hdrv2 = (msp_header_v2_t *)&msp->in_buf[0];
// Check for potential buffer overflow
if (hdrv2->size > MSP_PORT_INBUF_SIZE) {
msp->c_state = MSP_IDLE;
} else {
msp->data_size = hdrv2->size;
msp->cmd_msp = hdrv2->cmd;
msp->cmd_flags = hdrv2->flags;
msp->offset = 0; // re-use buffer
msp->c_state = msp->data_size > 0 ? MSP_PAYLOAD_V2_NATIVE : MSP_CHECKSUM_V2_NATIVE;
}
}
break;
case MSP_PAYLOAD_V2_NATIVE:
msp->checksum2 = crc8_dvb_s2(msp->checksum2, c);
msp->in_buf[msp->offset++] = c;
if (msp->offset == msp->data_size) {
msp->c_state = MSP_CHECKSUM_V2_NATIVE;
}
break;
case MSP_CHECKSUM_V2_NATIVE:
if (msp->checksum2 == c) {
msp->c_state = MSP_COMMAND_RECEIVED;
} else {
msp->c_state = MSP_IDLE;
}
break;
}
return true;
}
#endif //HAL_MSP_ENABLED