forked from mavlink-router/mavlink-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm.cpp
453 lines (378 loc) · 11.8 KB
/
comm.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* This file is part of the MAVLink Router project
*
* Copyright (C) 2016 Intel Corporation. All rights reserved.
*
* 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.
*/
#include "comm.h"
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <mavlink.h>
#include "log.h"
#include "util.h"
#define RX_BUF_MAX_SIZE (MAVLINK_MAX_PACKET_LEN * 4)
#define TX_BUF_MAX_SIZE (8U * 1024U)
/*
* mavlink 2.0 packet in its wire format
*
* Packet size:
* sizeof(mavlink_router_mavlink2_header)
* + payload length
* + 2 (checksum)
* + signature (0 if not signed)
*/
struct _packed_ mavlink_router_mavlink2_header {
uint8_t magic;
uint8_t payload_len;
uint8_t incompat_flags;
uint8_t compat_flags;
uint8_t seq;
uint8_t sysid;
uint8_t compid;
uint32_t msgid : 24;
};
/*
* mavlink 1.0 packet in its wire format
*
* Packet size:
* sizeof(mavlink_router_mavlink1_header)
* + payload length
* + 2 (checksum)
*/
struct _packed_ mavlink_router_mavlink1_header {
uint8_t magic;
uint8_t payload_len;
uint8_t seq;
uint8_t sysid;
uint8_t compid;
uint8_t msgid;
};
Endpoint::Endpoint(const char *name, bool crc_check_enabled)
: _name{name}
, _crc_check_enabled{crc_check_enabled}
{
rx_buf.data = (uint8_t *) malloc(RX_BUF_MAX_SIZE);
rx_buf.len = 0;
tx_buf.data = (uint8_t *) malloc(TX_BUF_MAX_SIZE);
tx_buf.len = 0;
assert(rx_buf.data);
assert(tx_buf.data);
}
Endpoint::~Endpoint()
{
if (fd >= 0) {
::close(fd);
}
}
int Endpoint::read_msg(struct buffer *pbuf)
{
bool should_read_more = true;
if (fd < 0) {
log_error("Trying to read invalid fd");
return -EINVAL;
}
if (_last_packet_len != 0) {
/*
* read_msg() should be called in a loop after writting to each
* output. However we don't want to keep busy looping on a single
* endpoint reading more data. If we left data behind, move them
* to the beginning and check we have a complete packet, but don't
* read more data right now - it will be handled on next
* iteration when more data is available
*/
should_read_more = false;
/* see TODO below about using bigger buffers: we could just walk on
* the buffer rather than moving bytes */
rx_buf.len -= _last_packet_len;
if (rx_buf.len > 0) {
memmove(rx_buf.data, rx_buf.data + _last_packet_len, rx_buf.len);
}
_last_packet_len = 0;
}
if (should_read_more) {
ssize_t r = _read_msg(rx_buf.data + rx_buf.len, RX_BUF_MAX_SIZE - rx_buf.len);
if (r <= 0)
return r;
log_debug("%s: Got %zd bytes", _name, r);
rx_buf.len += r;
}
bool mavlink2 = rx_buf.data[0] == MAVLINK_STX;
bool mavlink1 = rx_buf.data[0] == MAVLINK_STX_MAVLINK1;
/*
* Find magic byte as the start byte:
*
* we either enter here due to new bytes being written to the
* beginning of the buffer or due to _last_packet_len not being 0
* above, which means we moved some bytes we read previously
*/
if (!mavlink1 && !mavlink2) {
unsigned int stx_pos = 0;
for (unsigned int i = 1; i < (unsigned int) rx_buf.len; i++) {
if (rx_buf.data[i] == MAVLINK_STX)
mavlink2 = true;
else if (rx_buf.data[i] == MAVLINK_STX_MAVLINK1)
mavlink1 = true;
if (mavlink1 || mavlink2) {
stx_pos = i;
break;
}
}
/* Discarding data since we don't have a marker */
if (stx_pos == 0) {
rx_buf.len = 0;
return 0;
}
/*
* TODO: a larger buffer would allow to avoid the memmove in case a
* new message would still fit in our buffer
*/
rx_buf.len -= stx_pos;
memmove(rx_buf.data, rx_buf.data + stx_pos, rx_buf.len);
}
const uint8_t checksum_len = 2;
size_t expected_size;
if (mavlink2) {
struct mavlink_router_mavlink2_header *hdr =
(struct mavlink_router_mavlink2_header *)rx_buf.data;
if (rx_buf.len < sizeof(*hdr))
return 0;
expected_size = sizeof(*hdr);
expected_size += hdr->payload_len;
expected_size += checksum_len;
if (hdr->incompat_flags & MAVLINK_IFLAG_SIGNED)
expected_size += MAVLINK_SIGNATURE_BLOCK_LEN;
} else {
struct mavlink_router_mavlink1_header *hdr =
(struct mavlink_router_mavlink1_header *)rx_buf.data;
if (rx_buf.len < sizeof(*hdr))
return 0;
expected_size = sizeof(*hdr);
expected_size += hdr->payload_len;
expected_size += checksum_len;
}
/* check if we have a valid mavlink packet */
if (rx_buf.len < expected_size)
return 0;
/* We always want to transmit one packet at a time; record the number
* of bytes read in addition to the expected size and leave them for
* the next iteration */
_last_packet_len = expected_size;
_read_total++;
if (_crc_check_enabled && !_check_crc())
return 0;
pbuf->data = rx_buf.data;
pbuf->len = expected_size;
return 1;
}
bool Endpoint::_check_crc()
{
const bool mavlink2 = rx_buf.data[0] == MAVLINK_STX;
uint32_t msg_id;
uint16_t crc_msg, crc_calc;
uint8_t payload_len, header_len, *payload;
const mavlink_msg_entry_t *msg_entry;
if (mavlink2) {
struct mavlink_router_mavlink2_header *hdr =
(struct mavlink_router_mavlink2_header *)rx_buf.data;
payload = rx_buf.data + sizeof(*hdr);
msg_id = hdr->msgid;
header_len = sizeof(*hdr);
payload_len = hdr->payload_len;
} else {
struct mavlink_router_mavlink1_header *hdr =
(struct mavlink_router_mavlink1_header *)rx_buf.data;
payload = rx_buf.data + sizeof(*hdr);
msg_id = hdr->msgid;
header_len = sizeof(*hdr);
payload_len = hdr->payload_len;
}
msg_entry = mavlink_get_msg_entry(msg_id);
if (!msg_entry) {
/*
* It is accepting and forwarding unknown messages ids because
* it can be a new MAVLink message implemented only in
* Ground Station and Flight Stack. Although it can also be a
* corrupted message is better forward than silent drop it.
*/
return true;
}
crc_msg = payload[payload_len] | (payload[payload_len + 1] << 8);
crc_calc = crc_calculate(&rx_buf.data[1], header_len + payload_len - 1);
crc_accumulate(msg_entry->crc_extra, &crc_calc);
if (crc_calc != crc_msg) {
_read_crc_errors++;
return false;
}
return true;
}
void Endpoint::print_statistics()
{
printf("Endpoint {"
"\n\tname: %s" \
"\n\tmessages read: %u" \
"\n\tmessages read with CRC error: %u %f%%" \
"\n\tmessages written: %u" \
"\n}" \
"\n",
_name, _read_total, _read_crc_errors,
(_read_crc_errors * 100.0f) / (_read_total == 0 ? 1 : _read_total),
_write_total);
}
int UartEndpoint::open(const char *path, speed_t baudrate)
{
struct termios2 tc;
fd = ::open(path, O_RDWR|O_NONBLOCK|O_CLOEXEC|O_NOCTTY);
if (fd < 0) {
log_error_errno(errno, "Could not open %s (%m)", path);
return -1;
}
bzero(&tc, sizeof(tc));
if (ioctl(fd, TCGETS2, &tc) == -1) {
log_error_errno(errno, "Could not get termios2 (%m)");
goto fail;
}
tc.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
tc.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OPOST);
tc.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG | TOSTOP);
tc.c_cflag &= ~(CSIZE | PARENB | CBAUD | CRTSCTS);
tc.c_cflag |= CS8 | BOTHER;
tc.c_cc[VMIN] = 0;
tc.c_cc[VTIME] = 0;
tc.c_ispeed = baudrate;
tc.c_ospeed = baudrate;
if (ioctl(fd, TCSETS2, &tc) == -1) {
log_error_errno(errno, "Could not set terminal attributes (%m)");
goto fail;
}
if (ioctl(fd, TCFLSH, TCIOFLUSH) == -1) {
log_error_errno(errno, "Could not flush terminal (%m)");
goto fail;
}
return fd;
fail:
::close(fd);
fd = -1;
return -1;
}
ssize_t UartEndpoint::_read_msg(uint8_t *buf, size_t len)
{
ssize_t r = ::read(fd, buf, len);
if ((r == -1 && errno == EAGAIN) || r == 0)
return 0;
if (r == -1)
return -errno;
return r;
}
int UartEndpoint::write_msg(const struct buffer *pbuf)
{
if (fd < 0) {
log_error("Trying to write invalid fd");
return -EINVAL;
}
/* TODO: send any pending data */
if (tx_buf.len > 0) {
;
}
ssize_t r = ::write(fd, pbuf->data, pbuf->len);
if (r == -1 && errno == EAGAIN)
return -EAGAIN;
_write_total++;
/* Incomplete packet, we warn and discard the rest */
if (r != (ssize_t) pbuf->len) {
log_warning("Discarding packet, incomplete write %zd but len=%u",
r, pbuf->len);
}
log_debug("UART: wrote %zd bytes", r);
return r;
}
UdpEndpoint::UdpEndpoint()
: Endpoint{"UDP", false}
{
bzero(&sockaddr, sizeof(sockaddr));
}
int UdpEndpoint::open(const char *ip, unsigned long port)
{
const int broadcast_val = 1;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1) {
log_error_errno(errno, "Could not create socket (%m)");
return -1;
}
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = inet_addr(ip);
sockaddr.sin_port = htons(port);
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &broadcast_val, sizeof(broadcast_val))) {
log_error_errno(errno, "Error enabling broadcast in socket (%m)");
goto fail;
}
if (fcntl(fd, F_SETFL, O_NONBLOCK | FASYNC) < 0) {
log_error_errno(errno, "Error setting socket fd as non-blocking (%m)");
goto fail;
}
log_info("Open %s:%lu", ip, port);
return fd;
fail:
if (fd >= 0) {
::close(fd);
fd = -1;
}
return -1;
}
ssize_t UdpEndpoint::_read_msg(uint8_t *buf, size_t len)
{
socklen_t addrlen = sizeof(sockaddr);
ssize_t r = ::recvfrom(fd, buf, len, 0,
(struct sockaddr *)&sockaddr, &addrlen);
if (r == -1 && errno == EAGAIN)
return 0;
if (r == -1)
return -errno;
return r;
}
int UdpEndpoint::write_msg(const struct buffer *pbuf)
{
if (fd < 0) {
log_error("Trying to write invalid fd");
return -EINVAL;
}
/* TODO: send any pending data */
if (tx_buf.len > 0) {
;
}
ssize_t r = ::sendto(fd, pbuf->data, pbuf->len, 0,
(struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (r == -1) {
if (errno != EAGAIN && errno != ECONNREFUSED)
log_error_errno(errno, "Error sending udp packet (%m)");
return -errno;
};
_write_total++;
/* Incomplete packet, we warn and discard the rest */
if (r != (ssize_t) pbuf->len) {
log_warning("Discarding packet, incomplete write %zd but len=%u",
r, pbuf->len);
}
log_debug("UDP: wrote %zd bytes", r);
return r;
}