forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAP_Logger_MAVLinkLogTransfer.cpp
332 lines (284 loc) · 9.34 KB
/
AP_Logger_MAVLinkLogTransfer.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
/*
MAVLink logfile transfer functions
*/
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <GCS_MAVLink/GCS_config.h>
#include <AP_Logger/AP_Logger_config.h>
#if HAL_LOGGING_ENABLED && HAL_GCS_ENABLED
#include <AP_HAL/AP_HAL.h>
#include <AP_Logger/AP_Logger.h>
#include <GCS_MAVLink/GCS.h> // for LOG_ENTRY
extern const AP_HAL::HAL& hal;
/**
handle all types of log download requests from the GCS
*/
void AP_Logger::handle_log_message(GCS_MAVLINK &link, const mavlink_message_t &msg)
{
if (!WritesEnabled()) {
// this is currently used as a proxy for "in_mavlink_delay"
return;
}
if (vehicle_is_armed()) {
if (!_warned_log_disarm) {
link.send_text(MAV_SEVERITY_ERROR, "Disarm for log download");
_warned_log_disarm = true;
}
return;
}
_warned_log_disarm = false;
_last_mavlink_log_transfer_message_handled_ms = AP_HAL::millis();
switch (msg.msgid) {
case MAVLINK_MSG_ID_LOG_REQUEST_LIST:
handle_log_request_list(link, msg);
break;
case MAVLINK_MSG_ID_LOG_REQUEST_DATA:
handle_log_request_data(link, msg);
break;
case MAVLINK_MSG_ID_LOG_ERASE:
handle_log_request_erase(link, msg);
break;
case MAVLINK_MSG_ID_LOG_REQUEST_END:
handle_log_request_end(link, msg);
break;
}
}
/**
handle all types of log download requests from the GCS
*/
void AP_Logger::handle_log_request_list(GCS_MAVLINK &link, const mavlink_message_t &msg)
{
WITH_SEMAPHORE(_log_send_sem);
if (_log_sending_link != nullptr) {
link.send_text(MAV_SEVERITY_INFO, "Log download in progress");
return;
}
mavlink_log_request_list_t packet;
mavlink_msg_log_request_list_decode(&msg, &packet);
_log_num_logs = get_num_logs();
if (_log_num_logs == 0) {
_log_next_list_entry = 0;
_log_last_list_entry = 0;
} else {
_log_next_list_entry = packet.start;
_log_last_list_entry = packet.end;
if (_log_last_list_entry > _log_num_logs) {
_log_last_list_entry = _log_num_logs;
}
if (_log_next_list_entry < 1) {
_log_next_list_entry = 1;
}
}
transfer_activity = TransferActivity::LISTING;
_log_sending_link = &link;
handle_log_send_listing();
}
/**
handle request for log data
*/
void AP_Logger::handle_log_request_data(GCS_MAVLINK &link, const mavlink_message_t &msg)
{
WITH_SEMAPHORE(_log_send_sem);
if (_log_sending_link != nullptr) {
// some GCS (e.g. MAVProxy) attempt to stream request_data
// messages when they're filling gaps in the downloaded logs.
// This channel check avoids complaining to them, at the cost
// of silently dropping any repeated attempts to start logging
if (_log_sending_link->get_chan() != link.get_chan()) {
link.send_text(MAV_SEVERITY_INFO, "Log download in progress");
}
return;
}
mavlink_log_request_data_t packet;
mavlink_msg_log_request_data_decode(&msg, &packet);
// consider opening or switching logs:
if (transfer_activity != TransferActivity::SENDING || _log_num_data != packet.id) {
uint16_t num_logs = get_num_logs();
if (packet.id > num_logs || packet.id < 1) {
// request for an invalid log; cancel any current download
transfer_activity = TransferActivity::IDLE;
return;
}
uint32_t time_utc, size;
get_log_info(packet.id, size, time_utc);
_log_num_data = packet.id;
_log_data_size = size;
uint32_t end;
get_log_boundaries(packet.id, _log_data_page, end);
}
_log_data_offset = packet.ofs;
if (_log_data_offset >= _log_data_size) {
_log_data_remaining = 0;
} else {
_log_data_remaining = _log_data_size - _log_data_offset;
}
if (_log_data_remaining > packet.count) {
_log_data_remaining = packet.count;
}
transfer_activity = TransferActivity::SENDING;
_log_sending_link = &link;
handle_log_send();
}
/**
handle request to erase log data
*/
void AP_Logger::handle_log_request_erase(GCS_MAVLINK &link, const mavlink_message_t &msg)
{
// mavlink_log_erase_t packet;
// mavlink_msg_log_erase_decode(&msg, &packet);
EraseAll();
}
/**
handle request to stop transfer and resume normal logging
*/
void AP_Logger::handle_log_request_end(GCS_MAVLINK &link, const mavlink_message_t &msg)
{
WITH_SEMAPHORE(_log_send_sem);
mavlink_log_request_end_t packet;
mavlink_msg_log_request_end_decode(&msg, &packet);
transfer_activity = TransferActivity::IDLE;
_log_sending_link = nullptr;
}
/**
trigger sending of log messages if there are some pending
*/
void AP_Logger::handle_log_send()
{
WITH_SEMAPHORE(_log_send_sem);
if (_log_sending_link == nullptr) {
return;
}
if (hal.util->get_soft_armed()) {
// might be flying
return;
}
switch (transfer_activity) {
case TransferActivity::IDLE:
break;
case TransferActivity::LISTING:
handle_log_send_listing();
break;
case TransferActivity::SENDING:
handle_log_sending();
break;
}
}
void AP_Logger::handle_log_sending()
{
WITH_SEMAPHORE(_log_send_sem);
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
// assume USB speeds in SITL for the purposes of log download
const uint8_t num_sends = 40;
#else
uint8_t num_sends = 1;
if (_log_sending_link->is_high_bandwidth() && hal.gpio->usb_connected()) {
// when on USB we can send a lot more data
num_sends = 250;
} else if (_log_sending_link->have_flow_control()) {
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
num_sends = 80;
#else
num_sends = 10;
#endif
}
#endif
for (uint8_t i=0; i<num_sends; i++) {
if (transfer_activity != TransferActivity::SENDING) {
// may have completed sending data
break;
}
if (!handle_log_send_data()) {
break;
}
}
}
/**
trigger sending of log messages if there are some pending
*/
void AP_Logger::handle_log_send_listing()
{
WITH_SEMAPHORE(_log_send_sem);
if (!HAVE_PAYLOAD_SPACE(_log_sending_link->get_chan(), LOG_ENTRY)) {
// no space
return;
}
if (AP_HAL::millis() - _log_sending_link->get_last_heartbeat_time() > 3000) {
// give a heartbeat a chance
return;
}
uint32_t size, time_utc;
if (_log_next_list_entry == 0) {
size = 0;
time_utc = 0;
} else {
get_log_info(_log_next_list_entry, size, time_utc);
}
mavlink_msg_log_entry_send(_log_sending_link->get_chan(),
_log_next_list_entry,
_log_num_logs,
_log_last_list_entry,
time_utc,
size);
if (_log_next_list_entry == _log_last_list_entry) {
transfer_activity = TransferActivity::IDLE;
_log_sending_link = nullptr;
} else {
_log_next_list_entry++;
}
}
/**
trigger sending of log data if there are some pending
*/
bool AP_Logger::handle_log_send_data()
{
WITH_SEMAPHORE(_log_send_sem);
if (!HAVE_PAYLOAD_SPACE(_log_sending_link->get_chan(), LOG_DATA)) {
// no space
return false;
}
if (AP_HAL::millis() - _log_sending_link->get_last_heartbeat_time() > 3000) {
// give a heartbeat a chance
return false;
}
int16_t nbytes = 0;
uint32_t len = _log_data_remaining;
mavlink_log_data_t packet;
if (len > MAVLINK_MSG_LOG_DATA_FIELD_DATA_LEN) {
len = MAVLINK_MSG_LOG_DATA_FIELD_DATA_LEN;
}
nbytes = get_log_data(_log_num_data, _log_data_page, _log_data_offset, len, packet.data);
if (nbytes < 0) {
// report as EOF on error
nbytes = 0;
}
if (nbytes < MAVLINK_MSG_LOG_DATA_FIELD_DATA_LEN) {
memset(&packet.data[nbytes], 0, MAVLINK_MSG_LOG_DATA_FIELD_DATA_LEN-nbytes);
}
packet.ofs = _log_data_offset;
packet.id = _log_num_data;
packet.count = nbytes;
_mav_finalize_message_chan_send(_log_sending_link->get_chan(),
MAVLINK_MSG_ID_LOG_DATA,
(const char *)&packet,
MAVLINK_MSG_ID_LOG_DATA_MIN_LEN,
MAVLINK_MSG_ID_LOG_DATA_LEN,
MAVLINK_MSG_ID_LOG_DATA_CRC);
_log_data_offset += nbytes;
_log_data_remaining -= nbytes;
if (nbytes < MAVLINK_MSG_LOG_DATA_FIELD_DATA_LEN || _log_data_remaining == 0) {
transfer_activity = TransferActivity::IDLE;
_log_sending_link = nullptr;
}
return true;
}
#endif // HAL_LOGGING_ENABLED && HAL_GCS_ENABLED