2015-11-10 02:34:31 -04:00
|
|
|
/*
|
2019-01-18 00:23:42 -04:00
|
|
|
AP_Logger Remote(via MAVLink) logging
|
2015-11-10 02:34:31 -04:00
|
|
|
*/
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
#include "AP_Logger_MAVLink.h"
|
2015-11-10 02:34:31 -04:00
|
|
|
|
2021-01-06 22:04:09 -04:00
|
|
|
#if HAL_LOGGING_MAVLINK_ENABLED
|
2015-11-10 02:34:31 -04:00
|
|
|
|
|
|
|
#include "LogStructure.h"
|
|
|
|
|
|
|
|
#define REMOTE_LOG_DEBUGGING 0
|
|
|
|
|
|
|
|
#if REMOTE_LOG_DEBUGGING
|
|
|
|
#include <stdio.h>
|
2022-01-03 04:51:30 -04:00
|
|
|
# define Debug(fmt, args ...) do {fprintf(stderr, "%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); hal.scheduler->delay(1); } while(0)
|
2015-11-10 02:34:31 -04:00
|
|
|
#else
|
|
|
|
# define Debug(fmt, args ...)
|
|
|
|
#endif
|
|
|
|
|
2018-08-07 07:22:21 -03:00
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
2016-05-16 02:11:38 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
|
2015-11-10 02:34:31 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
|
|
|
|
// initialisation
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::Init()
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
_blocks = nullptr;
|
2015-11-10 02:34:31 -04:00
|
|
|
while (_blockcount >= 8) { // 8 is a *magic* number
|
2018-04-23 01:35:29 -03:00
|
|
|
_blocks = (struct dm_block *) calloc(_blockcount, sizeof(struct dm_block));
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_blocks != nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
_blockcount /= 2;
|
|
|
|
}
|
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_blocks == nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_all_blocks();
|
|
|
|
stats_init();
|
|
|
|
|
|
|
|
_initialised = true;
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
bool AP_Logger_MAVLink::logging_failed() const
|
2016-07-07 04:12:27 -03:00
|
|
|
{
|
|
|
|
return !_sending_to_client;
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
uint32_t AP_Logger_MAVLink::bufferspace_available() {
|
2015-11-10 02:34:31 -04:00
|
|
|
return (_blockcount_free * 200 + remaining_space_in_current_block());
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:26:29 -04:00
|
|
|
uint8_t AP_Logger_MAVLink::remaining_space_in_current_block() const {
|
2015-11-10 02:34:31 -04:00
|
|
|
// note that _current_block *could* be NULL ATM.
|
|
|
|
return (MAVLINK_MSG_REMOTE_LOG_DATA_BLOCK_FIELD_DATA_LEN - _latest_block_len);
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::enqueue_block(dm_block_queue_t &queue, struct dm_block *block)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
if (queue.youngest != nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
queue.youngest->next = block;
|
|
|
|
} else {
|
|
|
|
queue.oldest = block;
|
|
|
|
}
|
|
|
|
queue.youngest = block;
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
struct AP_Logger_MAVLink::dm_block *AP_Logger_MAVLink::dequeue_seqno(AP_Logger_MAVLink::dm_block_queue_t &queue, uint32_t seqno)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
struct dm_block *prev = nullptr;
|
|
|
|
for (struct dm_block *block=queue.oldest; block != nullptr; block=block->next) {
|
2015-11-10 02:34:31 -04:00
|
|
|
if (block->seqno == seqno) {
|
2016-10-30 02:24:21 -03:00
|
|
|
if (prev == nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
if (queue.youngest == queue.oldest) {
|
2016-10-30 02:24:21 -03:00
|
|
|
queue.oldest = nullptr;
|
|
|
|
queue.youngest = nullptr;
|
2015-11-10 02:34:31 -04:00
|
|
|
} else {
|
|
|
|
queue.oldest = block->next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (queue.youngest == block) {
|
|
|
|
queue.youngest = prev;
|
|
|
|
}
|
|
|
|
prev->next = block->next;
|
|
|
|
}
|
2016-10-30 02:24:21 -03:00
|
|
|
block->next = nullptr;
|
2015-11-10 02:34:31 -04:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
prev = block;
|
|
|
|
}
|
2016-10-30 02:24:21 -03:00
|
|
|
return nullptr;
|
2015-11-10 02:34:31 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
bool AP_Logger_MAVLink::free_seqno_from_queue(uint32_t seqno, dm_block_queue_t &queue)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
|
|
|
struct dm_block *block = dequeue_seqno(queue, seqno);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (block != nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
block->next = _blocks_free;
|
|
|
|
_blocks_free = block;
|
|
|
|
_blockcount_free++; // comment me out to expose a bug!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-08 22:36:18 -03:00
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
bool AP_Logger_MAVLink::WritesOK() const
|
2017-06-08 22:36:18 -03:00
|
|
|
{
|
|
|
|
if (!_sending_to_client) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-10 02:34:31 -04:00
|
|
|
/* Write a block of data at current offset */
|
|
|
|
|
|
|
|
// DM_write: 70734 events, 0 overruns, 167806us elapsed, 2us avg, min 1us max 34us 0.620us rms
|
2019-01-18 00:23:42 -04:00
|
|
|
bool AP_Logger_MAVLink::_WritePrioritisedBlock(const void *pBuffer, uint16_t size, bool is_critical)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2018-10-11 20:35:04 -03:00
|
|
|
if (!semaphore.take_nonblocking()) {
|
2017-08-25 09:03:40 -03:00
|
|
|
_dropped++;
|
2017-04-05 07:03:18 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-10 02:34:31 -04:00
|
|
|
if (! WriteBlockCheckStartupMessages()) {
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2015-11-10 02:34:31 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bufferspace_available() < size) {
|
|
|
|
if (_startup_messagewriter->finished()) {
|
|
|
|
// do not count the startup packets as being dropped...
|
2017-08-25 09:03:40 -03:00
|
|
|
_dropped++;
|
2015-11-10 02:34:31 -04:00
|
|
|
}
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2015-11-10 02:34:31 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t copied = 0;
|
|
|
|
|
|
|
|
while (copied < size) {
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_current_block == nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
_current_block = next_block();
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_current_block == nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
// should not happen - there's a sanity check above
|
2020-04-29 21:40:46 -03:00
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::logger_bad_current_block);
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2015-11-10 02:34:31 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint16_t remaining_to_copy = size - copied;
|
|
|
|
uint16_t _curr_remaining = remaining_space_in_current_block();
|
|
|
|
uint16_t to_copy = (remaining_to_copy > _curr_remaining) ? _curr_remaining : remaining_to_copy;
|
|
|
|
memcpy(&(_current_block->buf[_latest_block_len]), &((const uint8_t *)pBuffer)[copied], to_copy);
|
|
|
|
copied += to_copy;
|
|
|
|
_latest_block_len += to_copy;
|
|
|
|
if (_latest_block_len == MAVLINK_MSG_REMOTE_LOG_DATA_BLOCK_FIELD_DATA_LEN) {
|
|
|
|
//block full, mark it to be sent:
|
|
|
|
enqueue_block(_blocks_pending, _current_block);
|
|
|
|
_current_block = next_block();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2015-11-10 02:34:31 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get a free block
|
2019-01-18 00:23:42 -04:00
|
|
|
struct AP_Logger_MAVLink::dm_block *AP_Logger_MAVLink::next_block()
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2019-01-18 00:23:42 -04:00
|
|
|
AP_Logger_MAVLink::dm_block *ret = _blocks_free;
|
2016-10-30 02:24:21 -03:00
|
|
|
if (ret != nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
_blocks_free = ret->next;
|
|
|
|
_blockcount_free--;
|
|
|
|
ret->seqno = _next_seq_num++;
|
|
|
|
ret->last_sent = 0;
|
2016-10-30 02:24:21 -03:00
|
|
|
ret->next = nullptr;
|
2015-11-10 02:34:31 -04:00
|
|
|
_latest_block_len = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::free_all_blocks()
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
_blocks_free = nullptr;
|
|
|
|
_current_block = nullptr;
|
2015-11-10 02:34:31 -04:00
|
|
|
|
|
|
|
_blocks_pending.sent_count = 0;
|
2016-10-30 02:24:21 -03:00
|
|
|
_blocks_pending.oldest = _blocks_pending.youngest = nullptr;
|
2015-11-10 02:34:31 -04:00
|
|
|
_blocks_retry.sent_count = 0;
|
2016-10-30 02:24:21 -03:00
|
|
|
_blocks_retry.oldest = _blocks_retry.youngest = nullptr;
|
2015-11-10 02:34:31 -04:00
|
|
|
_blocks_sent.sent_count = 0;
|
2016-10-30 02:24:21 -03:00
|
|
|
_blocks_sent.oldest = _blocks_sent.youngest = nullptr;
|
2015-11-10 02:34:31 -04:00
|
|
|
|
|
|
|
// add blocks to the free stack:
|
|
|
|
for(uint8_t i=0; i < _blockcount; i++) {
|
|
|
|
_blocks[i].next = _blocks_free;
|
|
|
|
_blocks_free = &_blocks[i];
|
|
|
|
// this value doesn't really matter, but it stops valgrind
|
|
|
|
// complaining when acking blocks (we check seqno before
|
|
|
|
// state). Also, when we receive ACKs we check seqno, and we
|
|
|
|
// want to ack the *real* block zero!
|
|
|
|
_blocks[i].seqno = 9876543;
|
|
|
|
}
|
|
|
|
_blockcount_free = _blockcount;
|
|
|
|
|
|
|
|
_latest_block_len = 0;
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::stop_logging()
|
2016-04-22 10:33:40 -03:00
|
|
|
{
|
|
|
|
if (_sending_to_client) {
|
|
|
|
_sending_to_client = false;
|
|
|
|
_last_response_time = AP_HAL::millis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 21:08:23 -03:00
|
|
|
void AP_Logger_MAVLink::handle_ack(const GCS_MAVLINK &link,
|
2019-04-30 07:22:48 -03:00
|
|
|
const mavlink_message_t &msg,
|
2015-11-10 02:34:31 -04:00
|
|
|
uint32_t seqno)
|
|
|
|
{
|
|
|
|
if (!_initialised) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(seqno == MAV_REMOTE_LOG_DATA_BLOCK_STOP) {
|
|
|
|
Debug("Received stop-logging packet");
|
2016-04-22 10:33:40 -03:00
|
|
|
stop_logging();
|
2015-11-10 02:34:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(seqno == MAV_REMOTE_LOG_DATA_BLOCK_START) {
|
|
|
|
if (!_sending_to_client) {
|
|
|
|
Debug("Starting New Log");
|
|
|
|
free_all_blocks();
|
|
|
|
// _current_block = next_block();
|
2016-10-30 02:24:21 -03:00
|
|
|
// if (_current_block == nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
// Debug("No free blocks?!!!\n");
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
stats_init();
|
|
|
|
_sending_to_client = true;
|
2019-04-30 07:22:48 -03:00
|
|
|
_target_system_id = msg.sysid;
|
|
|
|
_target_component_id = msg.compid;
|
2019-07-31 21:08:23 -03:00
|
|
|
_link = &link;
|
2015-11-10 02:34:31 -04:00
|
|
|
_next_seq_num = 0;
|
2016-04-15 06:53:54 -03:00
|
|
|
start_new_log_reset_variables();
|
2015-11-10 02:34:31 -04:00
|
|
|
_last_response_time = AP_HAL::millis();
|
|
|
|
Debug("Target: (%u/%u)", _target_system_id, _target_component_id);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check SENT blocks (VERY likely to be first on the list):
|
|
|
|
if (free_seqno_from_queue(seqno, _blocks_sent)) {
|
|
|
|
// celebrate
|
|
|
|
_last_response_time = AP_HAL::millis();
|
|
|
|
} else if(free_seqno_from_queue(seqno, _blocks_retry)) {
|
|
|
|
// party
|
|
|
|
_last_response_time = AP_HAL::millis();
|
|
|
|
} else {
|
|
|
|
// probably acked already and put on the free list.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 21:08:23 -03:00
|
|
|
void AP_Logger_MAVLink::remote_log_block_status_msg(const GCS_MAVLINK &link,
|
2019-04-30 07:22:48 -03:00
|
|
|
const mavlink_message_t& msg)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
|
|
|
mavlink_remote_log_block_status_t packet;
|
2019-04-30 07:22:48 -03:00
|
|
|
mavlink_msg_remote_log_block_status_decode(&msg, &packet);
|
2018-10-11 20:35:04 -03:00
|
|
|
if (!semaphore.take_nonblocking()) {
|
2017-04-11 00:57:23 -03:00
|
|
|
return;
|
|
|
|
}
|
2021-08-19 21:11:27 -03:00
|
|
|
switch ((MAV_REMOTE_LOG_DATA_BLOCK_STATUSES)packet.status) {
|
|
|
|
case MAV_REMOTE_LOG_DATA_BLOCK_NACK:
|
|
|
|
handle_retry(packet.seqno);
|
|
|
|
break;
|
|
|
|
case MAV_REMOTE_LOG_DATA_BLOCK_ACK:
|
|
|
|
handle_ack(link, msg, packet.seqno);
|
|
|
|
break;
|
|
|
|
// we apparently have to handle an END enum entry, just drop it so we catch future additions
|
|
|
|
case MAV_REMOTE_LOG_DATA_BLOCK_STATUSES_ENUM_END:
|
|
|
|
break;
|
2015-11-10 02:34:31 -04:00
|
|
|
}
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2015-11-10 02:34:31 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::handle_retry(uint32_t seqno)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
|
|
|
if (!_initialised || !_sending_to_client) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dm_block *victim = dequeue_seqno(_blocks_sent, seqno);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (victim != nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
_last_response_time = AP_HAL::millis();
|
|
|
|
enqueue_block(_blocks_retry, victim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::stats_init() {
|
2017-08-25 09:03:40 -03:00
|
|
|
_dropped = 0;
|
2015-11-10 02:34:31 -04:00
|
|
|
stats.resends = 0;
|
|
|
|
stats_reset();
|
|
|
|
}
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::stats_reset() {
|
2015-11-10 02:34:31 -04:00
|
|
|
stats.state_free = 0;
|
|
|
|
stats.state_free_min = -1; // unsigned wrap
|
|
|
|
stats.state_free_max = 0;
|
|
|
|
stats.state_pending = 0;
|
|
|
|
stats.state_pending_min = -1; // unsigned wrap
|
|
|
|
stats.state_pending_max = 0;
|
|
|
|
stats.state_retry = 0;
|
|
|
|
stats.state_retry_min = -1; // unsigned wrap
|
|
|
|
stats.state_retry_max = 0;
|
|
|
|
stats.state_sent = 0;
|
|
|
|
stats.state_sent_min = -1; // unsigned wrap
|
|
|
|
stats.state_sent_max = 0;
|
|
|
|
stats.collection_count = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-11 04:38:01 -04:00
|
|
|
void AP_Logger_MAVLink::Write_logger_MAV(AP_Logger_MAVLink &logger_mav)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2019-02-11 04:38:01 -04:00
|
|
|
if (logger_mav.stats.collection_count == 0) {
|
2015-11-10 02:34:31 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-07-02 06:31:02 -03:00
|
|
|
const struct log_MAV_Stats pkt{
|
2019-02-11 04:38:01 -04:00
|
|
|
LOG_PACKET_HEADER_INIT(LOG_MAV_STATS),
|
2020-04-03 22:37:36 -03:00
|
|
|
timestamp : AP_HAL::micros64(),
|
2019-02-11 04:38:01 -04:00
|
|
|
seqno : logger_mav._next_seq_num-1,
|
|
|
|
dropped : logger_mav._dropped,
|
|
|
|
retries : logger_mav._blocks_retry.sent_count,
|
|
|
|
resends : logger_mav.stats.resends,
|
|
|
|
state_free_avg : (uint8_t)(logger_mav.stats.state_free/logger_mav.stats.collection_count),
|
|
|
|
state_free_min : logger_mav.stats.state_free_min,
|
|
|
|
state_free_max : logger_mav.stats.state_free_max,
|
|
|
|
state_pending_avg : (uint8_t)(logger_mav.stats.state_pending/logger_mav.stats.collection_count),
|
|
|
|
state_pending_min : logger_mav.stats.state_pending_min,
|
|
|
|
state_pending_max : logger_mav.stats.state_pending_max,
|
|
|
|
state_sent_avg : (uint8_t)(logger_mav.stats.state_sent/logger_mav.stats.collection_count),
|
|
|
|
state_sent_min : logger_mav.stats.state_sent_min,
|
|
|
|
state_sent_max : logger_mav.stats.state_sent_max,
|
2015-11-10 02:34:31 -04:00
|
|
|
};
|
|
|
|
WriteBlock(&pkt,sizeof(pkt));
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::stats_log()
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2018-05-07 00:34:31 -03:00
|
|
|
if (!_initialised) {
|
2015-11-10 02:34:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (stats.collection_count == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-11 04:38:01 -04:00
|
|
|
Write_logger_MAV(*this);
|
2015-11-10 02:34:31 -04:00
|
|
|
#if REMOTE_LOG_DEBUGGING
|
2018-08-07 07:22:21 -03:00
|
|
|
printf("D:%d Retry:%d Resent:%d SF:%d/%d/%d SP:%d/%d/%d SS:%d/%d/%d SR:%d/%d/%d\n",
|
2022-01-03 04:54:29 -04:00
|
|
|
_dropped,
|
2015-11-10 02:34:31 -04:00
|
|
|
_blocks_retry.sent_count,
|
|
|
|
stats.resends,
|
|
|
|
stats.state_free_min,
|
|
|
|
stats.state_free_max,
|
|
|
|
stats.state_free/stats.collection_count,
|
|
|
|
stats.state_pending_min,
|
|
|
|
stats.state_pending_max,
|
|
|
|
stats.state_pending/stats.collection_count,
|
|
|
|
stats.state_sent_min,
|
|
|
|
stats.state_sent_max,
|
|
|
|
stats.state_sent/stats.collection_count,
|
|
|
|
stats.state_retry_min,
|
|
|
|
stats.state_retry_max,
|
|
|
|
stats.state_retry/stats.collection_count
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
stats_reset();
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
uint8_t AP_Logger_MAVLink::stack_size(struct dm_block *stack)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
|
|
|
uint8_t ret = 0;
|
2016-10-30 02:24:21 -03:00
|
|
|
for (struct dm_block *block=stack; block != nullptr; block=block->next) {
|
2015-11-10 02:34:31 -04:00
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2019-01-18 00:23:42 -04:00
|
|
|
uint8_t AP_Logger_MAVLink::queue_size(dm_block_queue_t queue)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
|
|
|
return stack_size(queue.oldest);
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::stats_collect()
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2018-05-07 00:34:31 -03:00
|
|
|
if (!_initialised) {
|
2015-11-10 02:34:31 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-10-11 20:35:04 -03:00
|
|
|
if (!semaphore.take_nonblocking()) {
|
2017-04-11 00:57:23 -03:00
|
|
|
return;
|
|
|
|
}
|
2015-11-10 02:34:31 -04:00
|
|
|
uint8_t pending = queue_size(_blocks_pending);
|
|
|
|
uint8_t sent = queue_size(_blocks_sent);
|
|
|
|
uint8_t retry = queue_size(_blocks_retry);
|
|
|
|
uint8_t sfree = stack_size(_blocks_free);
|
|
|
|
|
|
|
|
if (sfree != _blockcount_free) {
|
2020-04-29 21:40:46 -03:00
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::logger_blockcount_mismatch);
|
2015-11-10 02:34:31 -04:00
|
|
|
}
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2017-04-11 00:57:23 -03:00
|
|
|
|
2015-11-10 02:34:31 -04:00
|
|
|
stats.state_pending += pending;
|
|
|
|
stats.state_sent += sent;
|
|
|
|
stats.state_free += sfree;
|
|
|
|
stats.state_retry += retry;
|
|
|
|
|
|
|
|
if (pending < stats.state_pending_min) {
|
|
|
|
stats.state_pending_min = pending;
|
|
|
|
}
|
|
|
|
if (pending > stats.state_pending_max) {
|
|
|
|
stats.state_pending_max = pending;
|
|
|
|
}
|
|
|
|
if (retry < stats.state_retry_min) {
|
|
|
|
stats.state_retry_min = retry;
|
|
|
|
}
|
|
|
|
if (retry > stats.state_retry_max) {
|
|
|
|
stats.state_retry_max = retry;
|
|
|
|
}
|
|
|
|
if (sent < stats.state_sent_min) {
|
|
|
|
stats.state_sent_min = sent;
|
|
|
|
}
|
|
|
|
if (sent > stats.state_sent_max) {
|
|
|
|
stats.state_sent_max = sent;
|
|
|
|
}
|
|
|
|
if (sfree < stats.state_free_min) {
|
|
|
|
stats.state_free_min = sfree;
|
|
|
|
}
|
|
|
|
if (sfree > stats.state_free_max) {
|
|
|
|
stats.state_free_max = sfree;
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.collection_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* while we "successfully" send log blocks from a queue, move them to
|
|
|
|
* the sent list. DO NOT call this for blocks already sent!
|
|
|
|
*/
|
2019-01-18 00:23:42 -04:00
|
|
|
bool AP_Logger_MAVLink::send_log_blocks_from_queue(dm_block_queue_t &queue)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
|
|
|
uint8_t sent_count = 0;
|
2016-10-30 02:24:21 -03:00
|
|
|
while (queue.oldest != nullptr) {
|
2015-11-10 02:34:31 -04:00
|
|
|
if (sent_count++ > _max_blocks_per_send_blocks) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (! send_log_block(*queue.oldest)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
queue.sent_count++;
|
2019-01-18 00:23:42 -04:00
|
|
|
struct AP_Logger_MAVLink::dm_block *tmp = dequeue_seqno(queue,queue.oldest->seqno);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (tmp != nullptr) { // should never be nullptr
|
2015-11-10 02:34:31 -04:00
|
|
|
enqueue_block(_blocks_sent, tmp);
|
|
|
|
} else {
|
2020-04-29 21:40:46 -03:00
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::logger_dequeue_failure);
|
2015-11-10 02:34:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::push_log_blocks()
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2018-05-07 00:34:31 -03:00
|
|
|
if (!_initialised || !_sending_to_client) {
|
2015-11-10 02:34:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
AP_Logger_Backend::WriteMoreStartupMessages();
|
2015-11-10 02:34:31 -04:00
|
|
|
|
2018-10-11 20:35:04 -03:00
|
|
|
if (!semaphore.take_nonblocking()) {
|
2017-04-05 07:03:18 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-10 02:34:31 -04:00
|
|
|
if (! send_log_blocks_from_queue(_blocks_retry)) {
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2015-11-10 02:34:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! send_log_blocks_from_queue(_blocks_pending)) {
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2015-11-10 02:34:31 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2015-11-10 02:34:31 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::do_resends(uint32_t now)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2018-05-07 00:34:31 -03:00
|
|
|
if (!_initialised || !_sending_to_client) {
|
2015-11-10 02:34:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t count_to_send = 5;
|
|
|
|
if (_blockcount < count_to_send) {
|
|
|
|
count_to_send = _blockcount;
|
|
|
|
}
|
|
|
|
uint32_t oldest = now - 100; // 100 milliseconds before resend. Hmm.
|
|
|
|
while (count_to_send-- > 0) {
|
2018-10-11 20:35:04 -03:00
|
|
|
if (!semaphore.take_nonblocking()) {
|
2017-04-11 00:57:23 -03:00
|
|
|
return;
|
|
|
|
}
|
2016-10-30 02:24:21 -03:00
|
|
|
for (struct dm_block *block=_blocks_sent.oldest; block != nullptr; block=block->next) {
|
2015-11-10 02:34:31 -04:00
|
|
|
// only want to send blocks every now-and-then:
|
|
|
|
if (block->last_sent < oldest) {
|
|
|
|
if (! send_log_block(*block)) {
|
|
|
|
// failed to send the block; try again later....
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2015-11-10 02:34:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
stats.resends++;
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 20:35:04 -03:00
|
|
|
semaphore.give();
|
2015-11-10 02:34:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 01:02:35 -03:00
|
|
|
// NOTE: any functions called from these periodic functions MUST
|
|
|
|
// handle locking of the blocks structures by taking the semaphore
|
|
|
|
// appropriately!
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::periodic_10Hz(const uint32_t now)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
|
|
|
do_resends(now);
|
|
|
|
stats_collect();
|
|
|
|
}
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_Logger_MAVLink::periodic_1Hz()
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
2022-07-26 20:41:04 -03:00
|
|
|
if (rate_limiter == nullptr && (_front._params.mav_ratemax > 0 || _front._log_pause)) {
|
|
|
|
// setup rate limiting if log rate max > 0Hz or log pause of streaming entries is requested
|
2021-07-28 03:52:35 -03:00
|
|
|
rate_limiter = new AP_Logger_RateLimiter(_front, _front._params.mav_ratemax);
|
|
|
|
}
|
|
|
|
|
2015-11-10 02:34:31 -04:00
|
|
|
if (_sending_to_client &&
|
|
|
|
_last_response_time + 10000 < _last_send_time) {
|
|
|
|
// other end appears to have timed out!
|
|
|
|
Debug("Client timed out");
|
|
|
|
_sending_to_client = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stats_log();
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: handle full txspace properly
|
2019-01-18 00:23:42 -04:00
|
|
|
bool AP_Logger_MAVLink::send_log_block(struct dm_block &block)
|
2015-11-10 02:34:31 -04:00
|
|
|
{
|
|
|
|
if (!_initialised) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-31 21:08:23 -03:00
|
|
|
if (_link == nullptr) {
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
2015-11-10 02:34:31 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-31 21:08:23 -03:00
|
|
|
// don't completely fill buffers - and also ensure there's enough
|
|
|
|
// room to send at least one packet:
|
|
|
|
const uint16_t min_payload_space = 500;
|
|
|
|
static_assert(MAVLINK_MSG_ID_REMOTE_LOG_DATA_BLOCK_LEN <= min_payload_space,
|
|
|
|
"minimum allocated space is less than payload length");
|
|
|
|
if (_link->txspace() < min_payload_space) {
|
2015-11-10 02:34:31 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-31 21:08:23 -03:00
|
|
|
|
2016-01-20 20:41:11 -04:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2019-07-31 21:08:23 -03:00
|
|
|
// deliberately fail 10% of the time in SITL:
|
2021-08-11 05:37:26 -03:00
|
|
|
if ((rand() % 100 + 1) < 10) {
|
2015-11-10 02:34:31 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DF_MAVLINK_DISABLE_INTERRUPTS
|
2019-09-20 23:20:45 -03:00
|
|
|
void *istate = hal.scheduler->disable_interrupts_save();
|
2015-11-10 02:34:31 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// DM_packing: 267039 events, 0 overruns, 8440834us elapsed, 31us avg, min 31us max 32us 0.488us rms
|
|
|
|
|
|
|
|
mavlink_message_t msg;
|
2019-07-31 21:08:23 -03:00
|
|
|
mavlink_status_t *chan_status = mavlink_get_channel_status(_link->get_chan());
|
2015-11-10 02:34:31 -04:00
|
|
|
uint8_t saved_seq = chan_status->current_tx_seq;
|
|
|
|
chan_status->current_tx_seq = mavlink_seq++;
|
|
|
|
// Debug("Sending block (%d)", block.seqno);
|
|
|
|
mavlink_msg_remote_log_data_block_pack(mavlink_system.sysid,
|
|
|
|
MAV_COMP_ID_LOG,
|
|
|
|
&msg,
|
|
|
|
_target_system_id,
|
|
|
|
_target_component_id,
|
|
|
|
block.seqno,
|
|
|
|
block.buf);
|
|
|
|
|
|
|
|
#if DF_MAVLINK_DISABLE_INTERRUPTS
|
2019-09-20 23:20:45 -03:00
|
|
|
hal.scheduler->restore_interrupts(istate);
|
2015-11-10 02:34:31 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
block.last_sent = AP_HAL::millis();
|
|
|
|
chan_status->current_tx_seq = saved_seq;
|
|
|
|
|
|
|
|
// _last_send_time is set even if we fail to send the packet; if
|
|
|
|
// the txspace is repeatedly chockas we should not add to the
|
|
|
|
// problem and stop attempting to log
|
|
|
|
_last_send_time = AP_HAL::millis();
|
|
|
|
|
2019-07-31 21:08:23 -03:00
|
|
|
_mavlink_resend_uart(_link->get_chan(), &msg);
|
2015-11-10 02:34:31 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|