mavlink: log handler rewrite for improved efficiency (#23421)

This commit is contained in:
Jacob Dahl
2024-08-07 07:26:12 -08:00
committed by GitHub
parent a39a3e2099
commit 086c044f47
2 changed files with 463 additions and 513 deletions
File diff suppressed because it is too large Load Diff
+65 -55
View File
@@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (c) 2014, 2020 PX4 Development Team. All rights reserved.
* Copyright (c) 2014-2024 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -33,76 +33,86 @@
#pragma once
/// @file mavlink_log_handler.h
/// @author px4dev, Gus Grubba <mavlink@grubba.com>
#include <dirent.h>
#include <queue.h>
#include <time.h>
#include <stdio.h>
#include <cstdbool>
#include <drivers/drv_hrt.h>
#include <perf/perf_counter.h>
#include "mavlink_bridge_header.h"
class Mavlink;
// MAVLink LOG_* Message Handler
class MavlinkLogHandler
{
public:
MavlinkLogHandler(Mavlink &mavlink);
~MavlinkLogHandler();
// Handle possible LOG message
void send();
void handle_message(const mavlink_message_t *msg);
/**
* Handle sending of messages. Call this regularly at a fixed frequency.
* @param t current time
*/
void send();
private:
struct LogEntry {
uint16_t id{0xffff};
uint32_t time_utc{};
uint32_t size_bytes{};
FILE *fp{nullptr};
char filepath[60];
uint32_t offset{};
};
struct LogEntryRequest {
uint16_t id{0xffff};
uint32_t start_offset{};
uint32_t byte_count{};
};
struct LogListRequest {
uint16_t first_id{0};
uint16_t last_id{0};
uint16_t current_id{0};
int file_index{0};
};
enum class LogHandlerState {
Idle,
Listing,
SendingData
};
// mavlink message handlers
void handle_log_request_list(const mavlink_message_t *msg);
void handle_log_request_data(const mavlink_message_t *msg);
void handle_log_request_end(const mavlink_message_t *msg);
void handle_log_erase(const mavlink_message_t *msg);
// state functions
void state_idle();
void state_listing();
void state_sending_data();
// Log request list
bool create_log_list_file();
void write_entries_to_file(FILE *f, const char *dir);
void send_log_entry(uint32_t size, uint32_t time_utc);
// Log request data
bool log_entry_from_id(uint16_t log_id, LogEntry *entry);
// Log erase
void delete_all_logs(const char *dir);
unsigned get_size();
private:
enum class LogHandlerState {
Inactive, //There is no active action of log handler
Idle, //The log handler is not sending list/data, but list has been sent
Listing, //File list is being send
SendingData //File Data is being send
};
void _log_message(const mavlink_message_t *msg);
void _log_request_list(const mavlink_message_t *msg);
void _log_request_data(const mavlink_message_t *msg);
void _log_request_erase(const mavlink_message_t *msg);
void _log_request_end(const mavlink_message_t *msg);
void _reset_list_helper();
void _init_list_helper();
bool _get_session_date(const char *path, const char *dir, time_t &date);
void _scan_logs(FILE *f, const char *dir, time_t &date);
bool _get_log_time_size(const char *path, const char *file, time_t &date, uint32_t &size);
static void _delete_all(const char *dir);
bool _get_entry(int idx, uint32_t &size, uint32_t &date, char *filename = 0, int filename_len = 0);
bool _open_for_transmit();
size_t _get_log_data(uint8_t len, uint8_t *buffer);
void _close_and_unlink_files();
size_t _log_send_listing();
size_t _log_send_data();
LogHandlerState _current_status{LogHandlerState::Inactive};
LogHandlerState _state{LogHandlerState::Idle};
Mavlink &_mavlink;
int _next_entry{0};
int _last_entry{0};
int _log_count{0};
// Log list
LogListRequest _list_request{};
int _num_logs{0};
bool _logs_listed{false};
uint16_t _current_log_index{UINT16_MAX};
uint32_t _current_log_size{0};
uint32_t _current_log_data_offset{0};
uint32_t _current_log_data_remaining{0};
FILE *_current_log_filep{nullptr};
char _current_log_filename[128]; //TODO: consider to allocate on runtime
// Log data
LogEntry _current_entry{};
LogEntryRequest _entry_request{};
bool _file_send_finished{};
perf_counter_t _create_file_elapsed{perf_alloc(PC_ELAPSED, MODULE_NAME": create file")};
perf_counter_t _listing_elapsed{perf_alloc(PC_ELAPSED, MODULE_NAME": listing")};
};