2013-12-15 03:57:15 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
2013-12-16 20:14:53 -04:00
|
|
|
MAVLink logfile transfer functions
|
|
|
|
*/
|
2013-12-15 03:57:15 -04:00
|
|
|
|
2013-12-16 20:14:53 -04:00
|
|
|
/*
|
2013-12-15 03:57:15 -04:00
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2013-12-15 18:57:28 -04:00
|
|
|
#include <AP_HAL.h>
|
2013-12-15 03:57:15 -04:00
|
|
|
#include <GCS.h>
|
|
|
|
#include <DataFlash.h>
|
2013-12-15 18:57:28 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
2013-12-15 03:57:15 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
handle all types of log download requests from the GCS
|
|
|
|
*/
|
|
|
|
void GCS_MAVLINK::handle_log_message(mavlink_message_t *msg, DataFlash_Class &dataflash)
|
|
|
|
{
|
|
|
|
switch (msg->msgid) {
|
|
|
|
case MAVLINK_MSG_ID_LOG_REQUEST_LIST:
|
|
|
|
handle_log_request_list(msg, dataflash);
|
|
|
|
break;
|
|
|
|
case MAVLINK_MSG_ID_LOG_REQUEST_DATA:
|
|
|
|
handle_log_request_data(msg, dataflash);
|
|
|
|
break;
|
2013-12-15 18:57:28 -04:00
|
|
|
case MAVLINK_MSG_ID_LOG_ERASE:
|
2014-09-26 23:43:14 -03:00
|
|
|
handle_log_request_erase(msg, dataflash);
|
2013-12-15 18:57:28 -04:00
|
|
|
break;
|
2014-01-13 22:51:20 -04:00
|
|
|
case MAVLINK_MSG_ID_LOG_REQUEST_END:
|
2014-09-26 23:43:14 -03:00
|
|
|
handle_log_request_end(msg, dataflash);
|
2014-01-13 22:51:20 -04:00
|
|
|
break;
|
2013-12-15 03:57:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
handle all types of log download requests from the GCS
|
|
|
|
*/
|
|
|
|
void GCS_MAVLINK::handle_log_request_list(mavlink_message_t *msg, DataFlash_Class &dataflash)
|
|
|
|
{
|
|
|
|
mavlink_log_request_list_t packet;
|
|
|
|
mavlink_msg_log_request_list_decode(msg, &packet);
|
2014-12-09 23:03:53 -04:00
|
|
|
|
2013-12-15 18:57:28 -04:00
|
|
|
_log_listing = false;
|
2013-12-15 03:57:15 -04:00
|
|
|
_log_sending = false;
|
|
|
|
|
|
|
|
_log_num_logs = dataflash.get_num_logs();
|
2013-12-15 18:57:28 -04:00
|
|
|
if (_log_num_logs == 0) {
|
2014-03-11 03:12:22 -03:00
|
|
|
_log_next_list_entry = 0;
|
|
|
|
_log_last_list_entry = 0;
|
|
|
|
} else {
|
2014-03-23 22:52:39 -03:00
|
|
|
uint16_t last_log_num = dataflash.find_last_log();
|
2013-12-15 03:57:15 -04:00
|
|
|
|
2014-03-11 03:12:22 -03:00
|
|
|
_log_next_list_entry = packet.start;
|
|
|
|
_log_last_list_entry = packet.end;
|
2013-12-15 03:57:15 -04:00
|
|
|
|
2014-03-11 03:12:22 -03:00
|
|
|
if (_log_last_list_entry > last_log_num) {
|
|
|
|
_log_last_list_entry = last_log_num;
|
|
|
|
}
|
|
|
|
if (_log_next_list_entry < last_log_num + 1 - _log_num_logs) {
|
|
|
|
_log_next_list_entry = last_log_num + 1 - _log_num_logs;
|
|
|
|
}
|
2013-12-15 03:57:15 -04:00
|
|
|
}
|
2013-12-15 18:57:28 -04:00
|
|
|
|
|
|
|
_log_listing = true;
|
|
|
|
handle_log_send_listing(dataflash);
|
2013-12-15 03:57:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
handle request for log data
|
|
|
|
*/
|
|
|
|
void GCS_MAVLINK::handle_log_request_data(mavlink_message_t *msg, DataFlash_Class &dataflash)
|
|
|
|
{
|
|
|
|
mavlink_log_request_data_t packet;
|
|
|
|
mavlink_msg_log_request_data_decode(msg, &packet);
|
|
|
|
|
|
|
|
_log_listing = false;
|
2013-12-29 00:00:04 -04:00
|
|
|
if (!_log_sending || _log_num_data != packet.id) {
|
|
|
|
_log_sending = false;
|
2013-12-15 03:57:15 -04:00
|
|
|
|
2013-12-29 00:00:04 -04:00
|
|
|
uint16_t num_logs = dataflash.get_num_logs();
|
2014-03-23 22:52:39 -03:00
|
|
|
uint16_t last_log_num = dataflash.find_last_log();
|
2013-12-29 00:00:04 -04:00
|
|
|
if (packet.id > last_log_num || packet.id < last_log_num + 1 - num_logs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t time_utc, size;
|
|
|
|
dataflash.get_log_info(packet.id, size, time_utc);
|
|
|
|
_log_num_data = packet.id;
|
|
|
|
_log_data_size = size;
|
|
|
|
|
|
|
|
uint16_t end;
|
|
|
|
dataflash.get_log_boundaries(packet.id, _log_data_page, end);
|
2013-12-15 03:57:15 -04:00
|
|
|
}
|
|
|
|
|
2014-02-13 22:14:07 -04:00
|
|
|
_log_data_offset = packet.ofs;
|
2013-12-29 00:00:04 -04:00
|
|
|
if (_log_data_offset >= _log_data_size) {
|
2013-12-15 03:57:15 -04:00
|
|
|
_log_data_remaining = 0;
|
|
|
|
} else {
|
2013-12-29 00:00:04 -04:00
|
|
|
_log_data_remaining = _log_data_size - _log_data_offset;
|
2013-12-15 03:57:15 -04:00
|
|
|
}
|
2013-12-15 18:57:28 -04:00
|
|
|
if (_log_data_remaining > packet.count) {
|
|
|
|
_log_data_remaining = packet.count;
|
|
|
|
}
|
2013-12-15 03:57:15 -04:00
|
|
|
_log_sending = true;
|
2013-12-15 18:57:28 -04:00
|
|
|
|
2013-12-27 23:25:07 -04:00
|
|
|
handle_log_send(dataflash);
|
2013-12-15 03:57:15 -04:00
|
|
|
}
|
|
|
|
|
2014-09-26 23:43:14 -03:00
|
|
|
/**
|
|
|
|
handle request to erase log data
|
|
|
|
*/
|
|
|
|
void GCS_MAVLINK::handle_log_request_erase(mavlink_message_t *msg, DataFlash_Class &dataflash)
|
|
|
|
{
|
2014-09-30 22:45:51 -03:00
|
|
|
mavlink_log_erase_t packet;
|
|
|
|
mavlink_msg_log_erase_decode(msg, &packet);
|
2014-09-26 23:43:14 -03:00
|
|
|
|
|
|
|
dataflash.EraseAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
handle request to stop transfer and resume normal logging
|
|
|
|
*/
|
|
|
|
void GCS_MAVLINK::handle_log_request_end(mavlink_message_t *msg, DataFlash_Class &dataflash)
|
|
|
|
{
|
2014-09-30 22:45:51 -03:00
|
|
|
mavlink_log_request_end_t packet;
|
|
|
|
mavlink_msg_log_request_end_decode(msg, &packet);
|
2014-09-26 23:43:14 -03:00
|
|
|
_log_sending = false;
|
|
|
|
}
|
|
|
|
|
2013-12-15 03:57:15 -04:00
|
|
|
/**
|
|
|
|
trigger sending of log messages if there are some pending
|
|
|
|
*/
|
|
|
|
void GCS_MAVLINK::handle_log_send(DataFlash_Class &dataflash)
|
|
|
|
{
|
|
|
|
if (_log_listing) {
|
|
|
|
handle_log_send_listing(dataflash);
|
|
|
|
}
|
2014-02-14 03:19:41 -04:00
|
|
|
if (!_log_sending) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-15 18:57:28 -04:00
|
|
|
uint8_t num_sends = 1;
|
|
|
|
if (chan == MAVLINK_COMM_0 && hal.gpio->usb_connected()) {
|
|
|
|
// when on USB we can send a lot more data
|
2013-12-28 01:01:01 -04:00
|
|
|
num_sends = 40;
|
2014-03-18 16:43:22 -03:00
|
|
|
} else if (have_flow_control()) {
|
2014-02-13 22:15:50 -04:00
|
|
|
num_sends = 10;
|
2013-12-15 18:57:28 -04:00
|
|
|
}
|
2013-12-16 20:14:53 -04:00
|
|
|
|
2015-05-04 03:17:47 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2013-12-16 20:14:53 -04:00
|
|
|
// assume USB speeds in SITL for the purposes of log download
|
2013-12-28 01:01:01 -04:00
|
|
|
num_sends = 40;
|
2013-12-16 20:14:53 -04:00
|
|
|
#endif
|
|
|
|
|
2013-12-15 03:57:15 -04:00
|
|
|
for (uint8_t i=0; i<num_sends; i++) {
|
|
|
|
if (_log_sending) {
|
2013-12-27 23:25:07 -04:00
|
|
|
if (!handle_log_send_data(dataflash)) break;
|
2013-12-15 03:57:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
trigger sending of log messages if there are some pending
|
|
|
|
*/
|
|
|
|
void GCS_MAVLINK::handle_log_send_listing(DataFlash_Class &dataflash)
|
|
|
|
{
|
2014-07-27 08:15:25 -03:00
|
|
|
uint16_t txspace = comm_get_txspace(chan);
|
|
|
|
if (txspace < MAVLINK_NUM_NON_PAYLOAD_BYTES+MAVLINK_MSG_ID_LOG_ENTRY_LEN) {
|
2013-12-15 03:57:15 -04:00
|
|
|
// no space
|
|
|
|
return;
|
|
|
|
}
|
2013-12-15 18:57:28 -04:00
|
|
|
if (hal.scheduler->millis() - last_heartbeat_time > 3000) {
|
|
|
|
// give a heartbeat a chance
|
|
|
|
return;
|
|
|
|
}
|
2013-12-15 03:57:15 -04:00
|
|
|
|
|
|
|
uint32_t size, time_utc;
|
2014-03-11 03:12:22 -03:00
|
|
|
if (_log_next_list_entry == 0) {
|
|
|
|
size = 0;
|
|
|
|
time_utc = 0;
|
|
|
|
} else {
|
|
|
|
dataflash.get_log_info(_log_next_list_entry, size, time_utc);
|
|
|
|
}
|
2013-12-15 03:57:15 -04:00
|
|
|
mavlink_msg_log_entry_send(chan, _log_next_list_entry, _log_num_logs, _log_last_list_entry, time_utc, size);
|
|
|
|
if (_log_next_list_entry == _log_last_list_entry) {
|
|
|
|
_log_listing = false;
|
|
|
|
} else {
|
|
|
|
_log_next_list_entry++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
trigger sending of log data if there are some pending
|
|
|
|
*/
|
2013-12-27 23:25:07 -04:00
|
|
|
bool GCS_MAVLINK::handle_log_send_data(DataFlash_Class &dataflash)
|
2013-12-15 03:57:15 -04:00
|
|
|
{
|
2014-07-27 08:15:25 -03:00
|
|
|
uint16_t txspace = comm_get_txspace(chan);
|
|
|
|
if (txspace < MAVLINK_NUM_NON_PAYLOAD_BYTES+MAVLINK_MSG_ID_LOG_DATA_LEN) {
|
2013-12-15 03:57:15 -04:00
|
|
|
// no space
|
2013-12-27 23:25:07 -04:00
|
|
|
return false;
|
2013-12-15 03:57:15 -04:00
|
|
|
}
|
2013-12-15 18:57:28 -04:00
|
|
|
if (hal.scheduler->millis() - last_heartbeat_time > 3000) {
|
|
|
|
// give a heartbeat a chance
|
2013-12-27 23:25:07 -04:00
|
|
|
return false;
|
2013-12-15 18:57:28 -04:00
|
|
|
}
|
2013-12-15 03:57:15 -04:00
|
|
|
|
|
|
|
int16_t ret = 0;
|
|
|
|
uint32_t len = _log_data_remaining;
|
2014-03-17 23:51:31 -03:00
|
|
|
mavlink_log_data_t packet;
|
2013-12-15 03:57:15 -04:00
|
|
|
|
|
|
|
if (len > 90) {
|
|
|
|
len = 90;
|
|
|
|
}
|
2014-03-17 23:51:31 -03:00
|
|
|
ret = dataflash.get_log_data(_log_num_data, _log_data_page, _log_data_offset, len, packet.data);
|
2013-12-15 03:57:15 -04:00
|
|
|
if (ret < 0) {
|
|
|
|
// report as EOF on error
|
|
|
|
ret = 0;
|
|
|
|
}
|
2013-12-16 20:14:53 -04:00
|
|
|
if (ret < 90) {
|
2014-03-17 23:51:31 -03:00
|
|
|
memset(&packet.data[ret], 0, 90-ret);
|
2013-12-16 20:14:53 -04:00
|
|
|
}
|
2014-03-17 23:51:31 -03:00
|
|
|
|
|
|
|
packet.ofs = _log_data_offset;
|
|
|
|
packet.id = _log_num_data;
|
|
|
|
packet.count = ret;
|
|
|
|
_mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_LOG_DATA, (const char *)&packet,
|
|
|
|
MAVLINK_MSG_ID_LOG_DATA_LEN, MAVLINK_MSG_ID_LOG_DATA_CRC);
|
|
|
|
|
2013-12-15 03:57:15 -04:00
|
|
|
_log_data_offset += len;
|
|
|
|
_log_data_remaining -= len;
|
2013-12-27 23:25:07 -04:00
|
|
|
if (ret < 90 || _log_data_remaining == 0) {
|
2013-12-15 03:57:15 -04:00
|
|
|
_log_sending = false;
|
|
|
|
}
|
2013-12-27 23:25:07 -04:00
|
|
|
return true;
|
2013-12-15 03:57:15 -04:00
|
|
|
}
|