2013-02-28 16:17:58 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
/*
|
|
|
|
DataFlash logging - file oriented variant
|
|
|
|
|
|
|
|
This uses posix file IO to create log files called logNN.dat in the
|
|
|
|
given directory
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DataFlash_File_h
|
|
|
|
#define DataFlash_File_h
|
|
|
|
|
2015-08-08 03:13:38 -03:00
|
|
|
#if HAL_OS_POSIX_IO
|
|
|
|
|
2015-06-25 10:53:20 -03:00
|
|
|
#include "DataFlash_Backend.h"
|
|
|
|
|
|
|
|
class DataFlash_File : public DataFlash_Backend
|
2013-02-28 16:17:58 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// constructor
|
2015-11-09 18:14:22 -04:00
|
|
|
DataFlash_File(DataFlash_Class &front,
|
|
|
|
DFMessageWriter_DFLogStart *,
|
|
|
|
const char *log_directory);
|
2013-02-28 16:17:58 -04:00
|
|
|
|
|
|
|
// initialisation
|
2015-11-09 18:14:22 -04:00
|
|
|
void Init() override;
|
2013-02-28 16:17:58 -04:00
|
|
|
bool CardInserted(void);
|
|
|
|
|
|
|
|
// erase handling
|
|
|
|
void EraseAll();
|
|
|
|
|
2015-08-08 03:13:38 -03:00
|
|
|
// possibly time-consuming preparation handling:
|
|
|
|
bool NeedPrep();
|
|
|
|
void Prep();
|
|
|
|
|
2013-02-28 16:17:58 -04:00
|
|
|
/* Write a block of data at current offset */
|
2015-08-06 09:18:28 -03:00
|
|
|
bool WritePrioritisedBlock(const void *pBuffer, uint16_t size, bool is_critical);
|
|
|
|
uint16_t bufferspace_available();
|
2013-02-28 16:17:58 -04:00
|
|
|
|
|
|
|
// high level interface
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t find_last_log() override;
|
2013-04-17 08:32:53 -03:00
|
|
|
void get_log_boundaries(uint16_t log_num, uint16_t & start_page, uint16_t & end_page);
|
2013-12-15 03:57:49 -04:00
|
|
|
void get_log_info(uint16_t log_num, uint32_t &size, uint32_t &time_utc);
|
|
|
|
int16_t get_log_data(uint16_t log_num, uint16_t page, uint32_t offset, uint16_t len, uint8_t *data);
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t get_num_logs() override;
|
2015-11-09 18:14:22 -04:00
|
|
|
uint16_t start_new_log(void) override;
|
2015-10-20 07:32:31 -03:00
|
|
|
void LogReadProcess(const uint16_t log_num,
|
2013-04-19 04:49:16 -03:00
|
|
|
uint16_t start_page, uint16_t end_page,
|
2015-05-12 03:59:55 -03:00
|
|
|
print_mode_fn print_mode,
|
2013-04-19 04:49:16 -03:00
|
|
|
AP_HAL::BetterStream *port);
|
2013-02-28 16:17:58 -04:00
|
|
|
void DumpPageInfo(AP_HAL::BetterStream *port);
|
|
|
|
void ShowDeviceInfo(AP_HAL::BetterStream *port);
|
2013-04-17 08:32:53 -03:00
|
|
|
void ListAvailableLogs(AP_HAL::BetterStream *port);
|
2013-02-28 16:17:58 -04:00
|
|
|
|
2015-06-18 22:57:01 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
|
|
|
void flush(void);
|
|
|
|
#endif
|
2015-08-06 09:18:28 -03:00
|
|
|
void periodic_fullrate(const uint32_t now);
|
|
|
|
|
2013-02-28 16:17:58 -04:00
|
|
|
private:
|
2013-09-28 03:29:58 -03:00
|
|
|
int _write_fd;
|
2013-02-28 16:17:58 -04:00
|
|
|
int _read_fd;
|
2013-12-15 03:57:49 -04:00
|
|
|
uint16_t _read_fd_log_num;
|
2013-04-17 08:32:53 -03:00
|
|
|
uint32_t _read_offset;
|
2014-01-12 23:25:16 -04:00
|
|
|
uint32_t _write_offset;
|
2013-09-28 03:29:58 -03:00
|
|
|
volatile bool _initialised;
|
2014-12-20 22:31:27 -04:00
|
|
|
volatile bool _open_error;
|
2013-02-28 16:17:58 -04:00
|
|
|
const char *_log_directory;
|
|
|
|
|
2015-11-10 23:51:03 -04:00
|
|
|
uint16_t _cached_oldest_log;
|
|
|
|
|
2013-04-19 21:25:10 -03:00
|
|
|
/*
|
|
|
|
read a block
|
|
|
|
*/
|
2015-11-09 18:14:22 -04:00
|
|
|
bool ReadBlock(void *pkt, uint16_t size) override;
|
2013-04-19 21:25:10 -03:00
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t _log_num_from_list_entry(const uint16_t list_entry);
|
|
|
|
|
2015-08-08 03:13:38 -03:00
|
|
|
// possibly time-consuming preparations handling
|
|
|
|
void Prep_MinSpace();
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t find_oldest_log();
|
|
|
|
int64_t disk_space_avail();
|
|
|
|
int64_t disk_space();
|
2015-08-08 03:13:38 -03:00
|
|
|
float avail_space_percent();
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
bool file_exists(const char *filename) const;
|
|
|
|
bool log_exists(const uint16_t lognum) const;
|
|
|
|
|
2015-08-08 03:13:38 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
|
|
|
// I always seem to have less than 10% free space on my laptop:
|
|
|
|
const float min_avail_space_percent = 0.1f;
|
|
|
|
#else
|
|
|
|
const float min_avail_space_percent = 10.0f;
|
|
|
|
#endif
|
2013-02-28 16:17:58 -04:00
|
|
|
// write buffer
|
2013-09-28 03:29:58 -03:00
|
|
|
uint8_t *_writebuf;
|
2015-12-03 07:01:53 -04:00
|
|
|
uint32_t _writebuf_size; // in bytes; may be == 65536, thus 32-bits
|
2014-01-12 23:25:16 -04:00
|
|
|
const uint16_t _writebuf_chunk;
|
2013-09-28 03:29:58 -03:00
|
|
|
volatile uint16_t _writebuf_head;
|
|
|
|
volatile uint16_t _writebuf_tail;
|
|
|
|
uint32_t _last_write_time;
|
2013-02-28 16:17:58 -04:00
|
|
|
|
|
|
|
/* construct a file name given a log number. Caller must free. */
|
2015-10-20 07:32:31 -03:00
|
|
|
char *_log_file_name(const uint16_t log_num) const;
|
|
|
|
char *_lastlog_file_name() const;
|
|
|
|
uint32_t _get_log_size(const uint16_t log_num) const;
|
|
|
|
uint32_t _get_log_time(const uint16_t log_num) const;
|
2013-04-17 08:32:53 -03:00
|
|
|
|
2013-12-27 23:24:28 -04:00
|
|
|
void stop_logging(void);
|
|
|
|
|
2013-09-28 03:29:58 -03:00
|
|
|
void _io_timer(void);
|
2014-01-12 23:25:16 -04:00
|
|
|
|
2015-08-06 09:18:28 -03:00
|
|
|
uint16_t critical_message_reserved_space() const {
|
|
|
|
// possibly make this a proportional to buffer size?
|
2015-12-03 07:01:53 -04:00
|
|
|
uint16_t ret = 1024;
|
|
|
|
if (ret > _writebuf_size) {
|
|
|
|
// in this case you will only get critical messages
|
|
|
|
ret = _writebuf_size;
|
|
|
|
}
|
|
|
|
return ret;
|
2015-08-06 09:18:28 -03:00
|
|
|
};
|
2015-09-17 07:58:35 -03:00
|
|
|
uint16_t non_messagewriter_message_reserved_space() const {
|
|
|
|
// possibly make this a proportional to buffer size?
|
2015-12-03 07:01:53 -04:00
|
|
|
uint16_t ret = 1024;
|
|
|
|
if (ret >= _writebuf_size) {
|
|
|
|
// need to allow messages out from the messagewriters. In
|
|
|
|
// this case while you have a messagewriter you won't get
|
|
|
|
// any other messages. This should be a corner case!
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
2015-09-17 07:58:35 -03:00
|
|
|
};
|
2015-08-06 09:18:28 -03:00
|
|
|
|
2014-01-12 23:25:16 -04:00
|
|
|
// performance counters
|
2015-10-20 02:49:32 -03:00
|
|
|
AP_HAL::Util::perf_counter_t _perf_write;
|
|
|
|
AP_HAL::Util::perf_counter_t _perf_fsync;
|
|
|
|
AP_HAL::Util::perf_counter_t _perf_errors;
|
|
|
|
AP_HAL::Util::perf_counter_t _perf_overruns;
|
2013-02-28 16:17:58 -04:00
|
|
|
};
|
|
|
|
|
2015-08-08 03:13:38 -03:00
|
|
|
#endif // HAL_OS_POSIX_IO
|
2013-02-28 16:17:58 -04:00
|
|
|
|
|
|
|
#endif // DataFlash_File_h
|