forked from mavlink-router/mavlink-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulog.cpp
360 lines (300 loc) · 10.1 KB
/
ulog.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
/*
* This file is part of the MAVLink Router project
*
* Copyright (C) 2017 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 "ulog.h"
#include <assert.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "log.h"
#include "util.h"
#define ULOG_HEADER_SIZE 16
#define ULOG_MAGIC \
{ \
0x55, 0x4C, 0x6F, 0x67, 0x01, 0x12, 0x35 \
}
#define NO_FIRST_MSG_OFFSET 255
struct _packed_ ulog_msg_header {
uint16_t msg_size;
uint8_t msg_type;
};
bool ULog::_start_timeout()
{
mavlink_message_t msg;
mavlink_command_long_t cmd;
bzero(&cmd, sizeof(cmd));
cmd.command = MAV_CMD_LOGGING_START;
cmd.target_component = MAV_COMP_ID_ALL;
cmd.target_system = _target_system_id;
mavlink_msg_command_long_encode(_system_id, MAV_COMP_ID_ALL, &msg, &cmd);
_send_msg(&msg, _target_system_id);
return true;
}
bool ULog::start()
{
if (!LogEndpoint::start()) {
return false;
}
_waiting_header = true;
_waiting_first_msg_offset = false;
_expected_seq = 0;
_buffer_len = 0;
_buffer_index = 0;
_buffer_partial_len = 0;
_system_id = LOG_ENDPOINT_SYSTEM_ID;
return true;
}
void ULog::stop()
{
mavlink_message_t msg;
mavlink_command_long_t cmd;
if (_file == -1) {
log_error("ULog not started");
return;
}
bzero(&cmd, sizeof(cmd));
cmd.command = MAV_CMD_LOGGING_STOP;
cmd.target_component = MAV_COMP_ID_ALL;
cmd.target_system = _target_system_id;
mavlink_msg_command_long_encode(_system_id, MAV_COMP_ID_ALL, &msg, &cmd);
_send_msg(&msg, _target_system_id);
_buffer_len = 0;
/* Write the last partial message to avoid corrupt the end of the file */
while (_buffer_partial_len) {
if (!_logging_flush())
break;
}
LogEndpoint::stop();
}
int ULog::write_msg(const struct buffer *buffer)
{
const bool mavlink2 = buffer->data[0] == MAVLINK_STX;
uint32_t msg_id;
uint16_t payload_len;
uint8_t trimmed_zeros;
if (mavlink2) {
struct mavlink_router_mavlink2_header *msg
= (struct mavlink_router_mavlink2_header *)buffer->data;
msg_id = msg->msgid;
payload_len = msg->payload_len;
} else {
struct mavlink_router_mavlink1_header *msg
= (struct mavlink_router_mavlink1_header *)buffer->data;
msg_id = msg->msgid;
payload_len = msg->payload_len;
}
/* Check if we are interested in this msg_id */
if (msg_id != MAVLINK_MSG_ID_COMMAND_ACK && msg_id != MAVLINK_MSG_ID_LOGGING_DATA_ACKED
&& msg_id != MAVLINK_MSG_ID_LOGGING_DATA) {
return buffer->len;
}
uint8_t *payload;
if (mavlink2) {
payload = buffer->data + sizeof(struct mavlink_router_mavlink2_header);
trimmed_zeros = get_trimmed_zeros(buffer);
} else {
payload = buffer->data + sizeof(struct mavlink_router_mavlink1_header);
trimmed_zeros = 0;
}
/* Handle messages */
switch (msg_id) {
case MAVLINK_MSG_ID_COMMAND_ACK: {
mavlink_command_ack_t cmd;
memcpy(&cmd, payload, payload_len);
if (trimmed_zeros)
memset(((uint8_t *)&cmd) + payload_len, 0, trimmed_zeros);
if (!_logging_start_timeout || cmd.command != MAV_CMD_LOGGING_START)
return buffer->len;
if (cmd.result == MAV_RESULT_ACCEPTED) {
_remove_start_timeout();
if (!_start_alive_timeout()) {
log_warning("Could not start liveness timeout - mavlink router log won't be able "
"to detect if flight stack stopped");
}
} else
log_error("MAV_CMD_LOGGING_START result(%u) is different than accepted", cmd.result);
break;
}
case MAVLINK_MSG_ID_LOGGING_DATA_ACKED: {
mavlink_logging_data_acked_t *ulog_data_acked = (mavlink_logging_data_acked_t *)payload;
mavlink_message_t msg;
mavlink_logging_ack_t ack;
ack.sequence = ulog_data_acked->sequence;
ack.target_component = MAV_COMP_ID_ALL;
ack.target_system = _target_system_id;
mavlink_msg_logging_ack_encode(_system_id, MAV_COMP_ID_ALL, &msg, &ack);
_send_msg(&msg, _target_system_id);
/* no break needed, message will be handled by MAVLINK_MSG_ID_LOGGING_DATA case */
}
case MAVLINK_MSG_ID_LOGGING_DATA: {
if (trimmed_zeros) {
mavlink_logging_data_t ulog_data;
memcpy(&ulog_data, payload, payload_len);
memset(((uint8_t *)&ulog_data) + payload_len, 0, trimmed_zeros);
_logging_data_process(&ulog_data);
} else {
mavlink_logging_data_t *ulog_data = (mavlink_logging_data_t *)payload;
_logging_data_process(ulog_data);
}
break;
}
}
_stat.write.total++;
_stat.write.bytes += buffer->len;
return buffer->len;
}
/*
* Return true if the message with seq should be handled.
*/
bool ULog::_logging_seq(uint16_t seq, bool *drop)
{
if (_expected_seq == seq) {
_expected_seq++;
*drop = false;
return true;
}
if (seq > _expected_seq) {
const uint16_t diff = seq - _expected_seq;
if (diff > (UINT16_MAX / 2)) {
/* _expected_seq wrapped and a re-transmission of a non-wrapped message happened */
return false;
}
} else {
const uint16_t diff = _expected_seq - seq;
if (diff < (UINT16_MAX / 2)) {
/* re-transmission */
return false;
}
}
*drop = true;
_expected_seq = seq + 1;
return true;
}
void ULog::_logging_data_process(mavlink_logging_data_t *msg)
{
bool drops = false;
if (!_logging_seq(msg->sequence, &drops))
return;
/* Waiting for ULog header? */
if (_waiting_header) {
const uint8_t magic[] = ULOG_MAGIC;
if (msg->length < ULOG_HEADER_SIZE) {
/* This should never happen */
log_error("ULog header is not complete, restarting ULog...");
stop();
start();
return;
}
if (memcmp(magic, msg->data, sizeof(magic))) {
log_error("Invalid ULog Magic number, restarting ULog...");
stop();
start();
return;
}
_buffer_partial_len = ULOG_HEADER_SIZE;
memcpy(_buffer_partial, msg->data, ULOG_HEADER_SIZE);
memmove(msg->data, &msg->data[ULOG_HEADER_SIZE], msg->length);
msg->length -= ULOG_HEADER_SIZE;
_waiting_header = false;
}
if (drops) {
_logging_flush();
_buffer_len = 0;
_buffer_index = 0;
_waiting_first_msg_offset = true;
}
/*
* Do not cause a buffer overflow, it should only happens if a ULog message
* don't fit in _msg_buffer
*/
if ((_buffer_len + msg->length) > BUFFER_LEN) {
log_warning("Buffer full, dropping everything on buffer");
_buffer_len = 0;
_waiting_first_msg_offset = true;
}
/*
* ULog message fits on _buffer but it need move all valid data to
* the being of buffer.
*/
if ((_buffer_index + _buffer_len + msg->length) > BUFFER_LEN) {
memmove(_buffer, &_buffer[_buffer_index], _buffer_len);
_buffer_index = 0;
}
uint8_t begin = 0;
if (_waiting_first_msg_offset) {
if (msg->first_message_offset == NO_FIRST_MSG_OFFSET) {
/* no useful information in this message */
return;
}
_waiting_first_msg_offset = false;
begin = msg->first_message_offset;
}
if (!msg->length)
return;
msg->length = msg->length - begin;
memcpy(&_buffer[_buffer_index + _buffer_len], &msg->data[begin], msg->length);
_buffer_len += msg->length;
_logging_flush();
}
bool ULog::_logging_flush()
{
while (_buffer_partial_len) {
const ssize_t r = write(_file, _buffer_partial, _buffer_partial_len);
if (r == 0 || (r == -1 && errno == EAGAIN))
return true;
if (r < 0) {
log_error("Unable to write to ULog file: (%m)");
return false;
}
_buffer_partial_len -= r;
memmove(_buffer_partial, &_buffer_partial[r], _buffer_partial_len);
}
while (_buffer_len >= sizeof(struct ulog_msg_header) && !_buffer_partial_len) {
struct ulog_msg_header *header = (struct ulog_msg_header *)&_buffer[_buffer_index];
const uint16_t full_msg_size = header->msg_size + sizeof(struct ulog_msg_header);
if (full_msg_size > _buffer_len) {
break;
}
const ssize_t r = write(_file, header, full_msg_size);
if (r == full_msg_size) {
_buffer_len -= full_msg_size;
_buffer_index += full_msg_size;
continue;
}
if (r == 0 || (r == -1 && errno == EAGAIN))
break;
if (r < 0) {
log_error("Unable to write to ULog file: (%m)");
return false;
}
/* Handle partial write */
_buffer_partial_len = full_msg_size - r;
if (_buffer_partial_len > sizeof(_buffer_partial)) {
_buffer_partial_len = 0;
log_error("Partial buffer is not big enough to store the "
"ULog entry(type=%c len=%u), ULog file is now corrupt.",
header->msg_type, full_msg_size);
break;
}
memcpy(_buffer_partial, &_buffer[_buffer_index + r], _buffer_partial_len);
_buffer_len -= full_msg_size;
_buffer_index += full_msg_size;
break;
}
return true;
}