2013-04-17 08:32:53 -03:00
|
|
|
/*
|
|
|
|
DataFlash logging - file oriented variant
|
|
|
|
|
2013-04-23 02:03:10 -03:00
|
|
|
This uses posix file IO to create log files called logs/NN.bin in the
|
2013-04-17 08:32:53 -03:00
|
|
|
given directory
|
2015-10-20 07:32:31 -03:00
|
|
|
|
|
|
|
SD Card Rates on PixHawk:
|
|
|
|
- deletion rate seems to be ~50 files/second.
|
|
|
|
- stat seems to be ~150/second
|
|
|
|
- readdir loop of 511 entry directory ~62,000 microseconds
|
2013-04-17 08:32:53 -03:00
|
|
|
*/
|
|
|
|
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-04-17 08:32:53 -03:00
|
|
|
|
2018-01-05 03:16:47 -04:00
|
|
|
#if HAL_OS_POSIX_IO || HAL_OS_FATFS_IO
|
2015-08-06 09:18:28 -03:00
|
|
|
#include "DataFlash_File.h"
|
|
|
|
|
2015-12-08 19:53:20 -04:00
|
|
|
#include <AP_Common/AP_Common.h>
|
2018-01-05 03:16:47 -04:00
|
|
|
|
|
|
|
#if HAL_OS_POSIX_IO
|
2013-04-17 08:32:53 -03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
2013-10-23 09:27:09 -03:00
|
|
|
#include <time.h>
|
2014-01-13 21:39:49 -04:00
|
|
|
#include <dirent.h>
|
2016-07-07 11:34:47 -03:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
2015-09-09 00:41:04 -03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
2018-05-11 09:26:52 -03:00
|
|
|
#else
|
2015-08-08 03:13:38 -03:00
|
|
|
#include <sys/statfs.h>
|
2015-09-09 00:41:04 -03:00
|
|
|
#endif
|
2018-01-05 03:16:47 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAL_OS_FATFS_IO
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
#define MAX_LOG_FILES 500U
|
|
|
|
#define DATAFLASH_PAGE_SIZE 1024UL
|
|
|
|
|
|
|
|
/*
|
|
|
|
constructor
|
|
|
|
*/
|
2015-11-09 18:14:22 -04:00
|
|
|
DataFlash_File::DataFlash_File(DataFlash_Class &front,
|
|
|
|
DFMessageWriter_DFLogStart *writer,
|
|
|
|
const char *log_directory) :
|
|
|
|
DataFlash_Backend(front, writer),
|
2013-09-28 03:29:58 -03:00
|
|
|
_write_fd(-1),
|
2013-04-17 08:32:53 -03:00
|
|
|
_read_fd(-1),
|
2013-09-28 03:29:58 -03:00
|
|
|
_log_directory(log_directory),
|
2016-07-29 21:52:21 -03:00
|
|
|
_writebuf(0),
|
2014-03-31 14:50:58 -03:00
|
|
|
#if defined(CONFIG_ARCH_BOARD_PX4FMU_V1)
|
2014-02-19 15:55:35 -04:00
|
|
|
// V1 gets IO errors with larger than 512 byte writes
|
|
|
|
_writebuf_chunk(512),
|
2014-05-30 17:58:34 -03:00
|
|
|
#elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V45)
|
2014-03-31 14:50:58 -03:00
|
|
|
_writebuf_chunk(512),
|
2014-05-30 17:58:34 -03:00
|
|
|
#elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V51)
|
|
|
|
_writebuf_chunk(512),
|
2014-12-30 06:33:42 -04:00
|
|
|
#elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V52)
|
|
|
|
_writebuf_chunk(512),
|
2018-02-03 10:20:41 -04:00
|
|
|
#elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V52E)
|
|
|
|
_writebuf_chunk(512),
|
2014-05-30 17:58:34 -03:00
|
|
|
#elif defined(CONFIG_ARCH_BOARD_VRUBRAIN_V51)
|
|
|
|
_writebuf_chunk(512),
|
2014-12-30 06:33:42 -04:00
|
|
|
#elif defined(CONFIG_ARCH_BOARD_VRUBRAIN_V52)
|
|
|
|
_writebuf_chunk(512),
|
2014-05-30 17:58:34 -03:00
|
|
|
#elif defined(CONFIG_ARCH_BOARD_VRHERO_V10)
|
2014-03-31 14:50:58 -03:00
|
|
|
_writebuf_chunk(512),
|
2018-02-03 10:01:47 -04:00
|
|
|
#elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V54)
|
|
|
|
_writebuf_chunk(512),
|
2014-02-19 15:55:35 -04:00
|
|
|
#else
|
2014-01-13 21:39:49 -04:00
|
|
|
_writebuf_chunk(4096),
|
2014-02-19 15:55:35 -04:00
|
|
|
#endif
|
2015-10-20 02:49:32 -03:00
|
|
|
_perf_write(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "DF_write")),
|
|
|
|
_perf_fsync(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "DF_fsync")),
|
|
|
|
_perf_errors(hal.util->perf_alloc(AP_HAL::Util::PC_COUNT, "DF_errors")),
|
|
|
|
_perf_overruns(hal.util->perf_alloc(AP_HAL::Util::PC_COUNT, "DF_overruns"))
|
2017-08-25 04:18:26 -03:00
|
|
|
{
|
|
|
|
df_stats_clear();
|
|
|
|
}
|
2013-04-17 08:32:53 -03:00
|
|
|
|
|
|
|
|
2015-11-09 18:14:22 -04:00
|
|
|
void DataFlash_File::Init()
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
2015-11-09 18:14:22 -04:00
|
|
|
DataFlash_Backend::Init();
|
2013-04-17 08:32:53 -03:00
|
|
|
// create the log directory if need be
|
|
|
|
int ret;
|
2013-12-14 00:32:39 -04:00
|
|
|
struct stat st;
|
2014-01-13 21:39:49 -04:00
|
|
|
|
2015-12-16 18:23:14 -04:00
|
|
|
semaphore = hal.util->new_semaphore();
|
|
|
|
if (semaphore == nullptr) {
|
2015-12-08 19:53:20 -04:00
|
|
|
AP_HAL::panic("Failed to create DataFlash_File semaphore");
|
2015-12-16 18:23:14 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-09-12 22:46:34 -03:00
|
|
|
write_fd_semaphore = hal.util->new_semaphore();
|
|
|
|
if (write_fd_semaphore == nullptr) {
|
|
|
|
AP_HAL::panic("Failed to create DataFlash_File write_fd_semaphore");
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 21:52:21 -03:00
|
|
|
|
2018-03-22 16:42:47 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4 || CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN
|
2014-01-13 21:39:49 -04:00
|
|
|
// try to cope with an existing lowercase log directory
|
|
|
|
// name. NuttX does not handle case insensitive VFAT well
|
|
|
|
DIR *d = opendir("/fs/microsd/APM");
|
2016-10-30 02:24:21 -03:00
|
|
|
if (d != nullptr) {
|
2014-01-13 21:39:49 -04:00
|
|
|
for (struct dirent *de=readdir(d); de; de=readdir(d)) {
|
|
|
|
if (strcmp(de->d_name, "logs") == 0) {
|
|
|
|
rename("/fs/microsd/APM/logs", "/fs/microsd/APM/OLDLOGS");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-28 16:12:34 -03:00
|
|
|
const char* custom_dir = hal.util->get_custom_log_directory();
|
2016-10-30 02:24:21 -03:00
|
|
|
if (custom_dir != nullptr){
|
2015-06-28 16:12:34 -03:00
|
|
|
_log_directory = custom_dir;
|
|
|
|
}
|
|
|
|
|
2013-12-14 00:32:39 -04:00
|
|
|
ret = stat(_log_directory, &st);
|
|
|
|
if (ret == -1) {
|
|
|
|
ret = mkdir(_log_directory, 0777);
|
|
|
|
}
|
|
|
|
if (ret == -1) {
|
2018-01-05 03:16:47 -04:00
|
|
|
printf("Failed to create log directory %s : %s\n", _log_directory, strerror(errno));
|
2013-04-17 08:32:53 -03:00
|
|
|
return;
|
|
|
|
}
|
2014-09-09 04:32:35 -03:00
|
|
|
|
2015-12-03 07:01:53 -04:00
|
|
|
// determine and limit file backend buffersize
|
2016-07-29 21:52:21 -03:00
|
|
|
uint32_t bufsize = _front._params.file_bufsize;
|
2015-12-03 07:01:53 -04:00
|
|
|
if (bufsize > 64) {
|
2016-07-29 21:52:21 -03:00
|
|
|
bufsize = 64; // PixHawk has DMA limitations.
|
2015-12-03 07:01:53 -04:00
|
|
|
}
|
2016-07-29 21:52:21 -03:00
|
|
|
bufsize *= 1024;
|
2015-12-03 07:01:53 -04:00
|
|
|
|
2016-07-29 21:52:21 -03:00
|
|
|
// If we can't allocate the full size, try to reduce it until we can allocate it
|
|
|
|
while (!_writebuf.set_size(bufsize) && bufsize >= _writebuf_chunk) {
|
|
|
|
hal.console->printf("DataFlash_File: Couldn't set buffer size to=%u\n", (unsigned)bufsize);
|
|
|
|
bufsize >>= 1;
|
2014-09-09 04:32:35 -03:00
|
|
|
}
|
2016-07-29 21:52:21 -03:00
|
|
|
|
|
|
|
if (!_writebuf.get_size()) {
|
2014-09-09 04:32:35 -03:00
|
|
|
hal.console->printf("Out of memory for logging\n");
|
2016-07-29 21:52:21 -03:00
|
|
|
return;
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
2016-07-29 21:52:21 -03:00
|
|
|
|
|
|
|
hal.console->printf("DataFlash_File: buffer size=%u\n", (unsigned)bufsize);
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
_initialised = true;
|
2015-05-24 20:24:11 -03:00
|
|
|
hal.scheduler->register_io_process(FUNCTOR_BIND_MEMBER(&DataFlash_File::_io_timer, void));
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
bool DataFlash_File::file_exists(const char *filename) const
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(filename, &st) == -1) {
|
|
|
|
// hopefully errno==ENOENT. If some error occurs it is
|
|
|
|
// probably better to assume this file exists.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataFlash_File::log_exists(const uint16_t lognum) const
|
|
|
|
{
|
|
|
|
char *filename = _log_file_name(lognum);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (filename == nullptr) {
|
2015-10-20 07:32:31 -03:00
|
|
|
return false; // ?!
|
|
|
|
}
|
|
|
|
bool ret = file_exists(filename);
|
|
|
|
free(filename);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-06 02:09:39 -04:00
|
|
|
void DataFlash_File::periodic_1Hz(const uint32_t now)
|
|
|
|
{
|
|
|
|
if (!io_thread_alive()) {
|
2017-04-17 07:42:30 -03:00
|
|
|
if (io_thread_warning_decimation_counter == 0) {
|
2017-07-27 23:07:35 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_CRITICAL, "No IO Thread Heartbeat (%s)", last_io_operation);
|
2017-04-17 07:42:30 -03:00
|
|
|
}
|
|
|
|
if (io_thread_warning_decimation_counter++ > 57) {
|
|
|
|
io_thread_warning_decimation_counter = 0;
|
|
|
|
}
|
2016-11-06 02:09:39 -04:00
|
|
|
// If you try to close the file here then it will almost
|
|
|
|
// certainly block. Since this is the main thread, this is
|
|
|
|
// likely to cause a crash.
|
2017-09-12 22:46:34 -03:00
|
|
|
|
|
|
|
// semaphore_write_fd not taken here as if the io thread is
|
|
|
|
// dead it may not release lock...
|
2016-11-06 02:09:39 -04:00
|
|
|
_write_fd = -1;
|
|
|
|
_initialised = false;
|
|
|
|
}
|
2017-08-25 04:18:26 -03:00
|
|
|
df_stats_log();
|
2016-11-06 02:09:39 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 09:18:28 -03:00
|
|
|
void DataFlash_File::periodic_fullrate(const uint32_t now)
|
|
|
|
{
|
|
|
|
DataFlash_Backend::push_log_blocks();
|
|
|
|
}
|
|
|
|
|
2016-07-29 21:52:21 -03:00
|
|
|
uint32_t DataFlash_File::bufferspace_available()
|
2015-08-06 09:18:28 -03:00
|
|
|
{
|
2016-07-29 21:52:21 -03:00
|
|
|
const uint32_t space = _writebuf.space();
|
|
|
|
const uint32_t crit = critical_message_reserved_space();
|
2016-09-12 23:32:01 -03:00
|
|
|
|
|
|
|
return (space > crit) ? space - crit : 0;
|
2015-08-06 09:18:28 -03:00
|
|
|
}
|
|
|
|
|
2016-07-29 21:52:21 -03:00
|
|
|
// return true for CardInserted() if we successfully initialized
|
2017-06-14 22:19:54 -03:00
|
|
|
bool DataFlash_File::CardInserted(void) const
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
2014-12-20 22:31:27 -04:00
|
|
|
return _initialised && !_open_error;
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
// returns the amount of disk space available in _log_directory (in bytes)
|
|
|
|
// returns -1 on error
|
|
|
|
int64_t DataFlash_File::disk_space_avail()
|
2015-08-08 03:13:38 -03:00
|
|
|
{
|
2018-05-11 09:26:52 -03:00
|
|
|
#if HAL_OS_POSIX_IO
|
2017-08-25 04:18:26 -03:00
|
|
|
struct statfs _stats;
|
|
|
|
if (statfs(_log_directory, &_stats) < 0) {
|
2015-08-08 03:13:38 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2017-08-25 04:18:26 -03:00
|
|
|
return (((int64_t)_stats.f_bavail) * _stats.f_bsize);
|
2018-01-05 03:16:47 -04:00
|
|
|
#elif HAL_OS_FATFS_IO
|
|
|
|
return fs_getfree();
|
2015-12-08 19:53:20 -04:00
|
|
|
#else
|
|
|
|
// return a fake disk space size
|
|
|
|
return 100*1000*1000UL;
|
|
|
|
#endif
|
2015-08-08 03:13:38 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
// returns the total amount of disk space (in use + available) in
|
|
|
|
// _log_directory (in bytes).
|
|
|
|
// returns -1 on error
|
|
|
|
int64_t DataFlash_File::disk_space()
|
2015-08-08 03:13:38 -03:00
|
|
|
{
|
2018-05-11 09:26:52 -03:00
|
|
|
#if HAL_OS_POSIX_IO
|
2017-08-25 04:18:26 -03:00
|
|
|
struct statfs _stats;
|
|
|
|
if (statfs(_log_directory, &_stats) < 0) {
|
2015-08-08 03:13:38 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2017-08-25 04:18:26 -03:00
|
|
|
return (((int64_t)_stats.f_blocks) * _stats.f_bsize);
|
2018-01-05 03:16:47 -04:00
|
|
|
#elif HAL_OS_FATFS_IO
|
|
|
|
return fs_gettotal();
|
2015-12-08 19:53:20 -04:00
|
|
|
#else
|
|
|
|
// return fake disk space size
|
|
|
|
return 200*1000*1000UL;
|
|
|
|
#endif
|
2015-08-08 03:13:38 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
// returns the available space in _log_directory as a percentage
|
|
|
|
// returns -1.0f on error
|
2015-08-08 03:13:38 -03:00
|
|
|
float DataFlash_File::avail_space_percent()
|
|
|
|
{
|
2015-10-20 07:32:31 -03:00
|
|
|
int64_t avail = disk_space_avail();
|
|
|
|
if (avail == -1) {
|
|
|
|
return -1.0f;
|
|
|
|
}
|
|
|
|
int64_t space = disk_space();
|
|
|
|
if (space == -1) {
|
|
|
|
return -1.0f;
|
|
|
|
}
|
2015-08-08 03:13:38 -03:00
|
|
|
|
|
|
|
return (avail/(float)space) * 100;
|
|
|
|
}
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
// find_oldest_log - find oldest log in _log_directory
|
2015-08-08 03:13:38 -03:00
|
|
|
// returns 0 if no log was found
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t DataFlash_File::find_oldest_log()
|
2015-08-08 03:13:38 -03:00
|
|
|
{
|
2015-11-10 23:51:03 -04:00
|
|
|
if (_cached_oldest_log != 0) {
|
|
|
|
return _cached_oldest_log;
|
|
|
|
}
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t last_log_num = find_last_log();
|
|
|
|
if (last_log_num == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t current_oldest_log = 0; // 0 is invalid
|
|
|
|
|
2015-08-08 03:13:38 -03:00
|
|
|
// We could count up to find_last_log(), but if people start
|
|
|
|
// relying on the min_avail_space_percent feature we could end up
|
|
|
|
// doing a *lot* of asprintf()s and stat()s
|
|
|
|
DIR *d = opendir(_log_directory);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (d == nullptr) {
|
2017-08-25 09:03:40 -03:00
|
|
|
internal_error();
|
2015-08-08 03:13:38 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we only remove files which look like xxx.BIN
|
|
|
|
for (struct dirent *de=readdir(d); de; de=readdir(d)) {
|
|
|
|
uint8_t length = strlen(de->d_name);
|
|
|
|
if (length < 5) {
|
|
|
|
// not long enough for \d+[.]BIN
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp(&de->d_name[length-4], ".BIN", 4)) {
|
|
|
|
// doesn't end in .BIN
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
uint16_t thisnum = strtoul(de->d_name, nullptr, 10);
|
2015-10-20 07:32:31 -03:00
|
|
|
if (thisnum > MAX_LOG_FILES) {
|
|
|
|
// ignore files above our official maximum...
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (current_oldest_log == 0) {
|
|
|
|
current_oldest_log = thisnum;
|
|
|
|
} else {
|
|
|
|
if (current_oldest_log <= last_log_num) {
|
|
|
|
if (thisnum > last_log_num) {
|
|
|
|
current_oldest_log = thisnum;
|
|
|
|
} else if (thisnum < current_oldest_log) {
|
|
|
|
current_oldest_log = thisnum;
|
|
|
|
}
|
|
|
|
} else { // current_oldest_log > last_log_num
|
|
|
|
if (thisnum > last_log_num) {
|
|
|
|
if (thisnum < current_oldest_log) {
|
|
|
|
current_oldest_log = thisnum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-08 03:13:38 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
2015-11-10 23:51:03 -04:00
|
|
|
_cached_oldest_log = current_oldest_log;
|
2015-08-08 03:13:38 -03:00
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
return current_oldest_log;
|
2015-08-08 03:13:38 -03:00
|
|
|
}
|
2013-04-17 08:32:53 -03:00
|
|
|
|
2015-08-08 03:13:38 -03:00
|
|
|
void DataFlash_File::Prep_MinSpace()
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
2015-10-20 07:32:31 -03:00
|
|
|
const uint16_t first_log_to_remove = find_oldest_log();
|
|
|
|
if (first_log_to_remove == 0) {
|
2015-08-08 03:13:38 -03:00
|
|
|
// no files to remove
|
|
|
|
return;
|
|
|
|
}
|
2015-10-20 07:32:31 -03:00
|
|
|
|
2015-11-10 23:51:03 -04:00
|
|
|
_cached_oldest_log = 0;
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t log_to_remove = first_log_to_remove;
|
|
|
|
|
2015-08-08 03:13:38 -03:00
|
|
|
uint16_t count = 0;
|
2015-10-20 07:32:31 -03:00
|
|
|
do {
|
|
|
|
float avail = avail_space_percent();
|
|
|
|
if (is_equal(avail, -1.0f)) {
|
2017-08-25 09:03:40 -03:00
|
|
|
internal_error();
|
2015-10-20 07:32:31 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (avail >= min_avail_space_percent) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (count++ > MAX_LOG_FILES+10) {
|
2015-08-08 03:13:38 -03:00
|
|
|
// *way* too many deletions going on here. Possible internal error.
|
2017-08-25 09:03:40 -03:00
|
|
|
internal_error();
|
2015-08-08 03:13:38 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
char *filename_to_remove = _log_file_name(log_to_remove);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (filename_to_remove == nullptr) {
|
2017-08-25 09:03:40 -03:00
|
|
|
internal_error();
|
2015-08-08 03:13:38 -03:00
|
|
|
break;
|
|
|
|
}
|
2015-10-20 07:32:31 -03:00
|
|
|
if (file_exists(filename_to_remove)) {
|
|
|
|
hal.console->printf("Removing (%s) for minimum-space requirements (%.2f%% < %.0f%%)\n",
|
2015-11-24 16:30:40 -04:00
|
|
|
filename_to_remove, (double)avail, (double)min_avail_space_percent);
|
2015-10-20 07:32:31 -03:00
|
|
|
if (unlink(filename_to_remove) == -1) {
|
|
|
|
hal.console->printf("Failed to remove %s: %s\n", filename_to_remove, strerror(errno));
|
|
|
|
free(filename_to_remove);
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
// corruption - should always have a continuous
|
|
|
|
// sequence of files... however, there may be still
|
|
|
|
// files out there, so keep going.
|
|
|
|
} else {
|
2017-08-25 09:03:40 -03:00
|
|
|
internal_error();
|
2015-08-08 03:13:38 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-20 07:32:31 -03:00
|
|
|
free(filename_to_remove);
|
2015-08-08 03:13:38 -03:00
|
|
|
}
|
|
|
|
}
|
2015-10-20 07:32:31 -03:00
|
|
|
log_to_remove++;
|
|
|
|
if (log_to_remove > MAX_LOG_FILES) {
|
|
|
|
log_to_remove = 1;
|
|
|
|
}
|
|
|
|
} while (log_to_remove != first_log_to_remove);
|
2015-08-08 03:13:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlash_File::Prep() {
|
2017-06-12 10:05:02 -03:00
|
|
|
if (!NeedPrep()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-08 03:13:38 -03:00
|
|
|
if (hal.util->get_soft_armed()) {
|
|
|
|
// do not want to do any filesystem operations while we are e.g. flying
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Prep_MinSpace();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataFlash_File::NeedPrep()
|
|
|
|
{
|
|
|
|
if (!CardInserted()) {
|
|
|
|
// should not have been called?!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avail_space_percent() < min_avail_space_percent) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
construct a log file name given a log number.
|
2017-02-10 22:39:50 -04:00
|
|
|
The number in the log filename will *not* be zero-padded.
|
2013-04-17 08:32:53 -03:00
|
|
|
Note: Caller must free.
|
|
|
|
*/
|
2017-02-10 22:39:50 -04:00
|
|
|
char *DataFlash_File::_log_file_name_short(const uint16_t log_num) const
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
char *buf = nullptr;
|
2017-02-13 22:25:32 -04:00
|
|
|
if (asprintf(&buf, "%s/%u.BIN", _log_directory, (unsigned)log_num) == -1) {
|
2016-10-30 02:24:21 -03:00
|
|
|
return nullptr;
|
2015-05-04 04:27:15 -03:00
|
|
|
}
|
2013-04-17 08:32:53 -03:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:39:50 -04:00
|
|
|
/*
|
|
|
|
construct a log file name given a log number.
|
|
|
|
The number in the log filename will be zero-padded.
|
|
|
|
Note: Caller must free.
|
|
|
|
*/
|
|
|
|
char *DataFlash_File::_log_file_name_long(const uint16_t log_num) const
|
|
|
|
{
|
|
|
|
char *buf = nullptr;
|
|
|
|
if (asprintf(&buf, "%s/%08u.BIN", _log_directory, (unsigned)log_num) == -1) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
return a log filename appropriate for the supplied log_num if a
|
|
|
|
filename exists with the short (not-zero-padded name) then it is the
|
|
|
|
appropirate name, otherwise the long (zero-padded) version is.
|
|
|
|
Note: Caller must free.
|
|
|
|
*/
|
|
|
|
char *DataFlash_File::_log_file_name(const uint16_t log_num) const
|
|
|
|
{
|
|
|
|
char *filename = _log_file_name_short(log_num);
|
|
|
|
if (filename == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (file_exists(filename)) {
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
free(filename);
|
|
|
|
return _log_file_name_long(log_num);
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
/*
|
|
|
|
return path name of the lastlog.txt marker file
|
|
|
|
Note: Caller must free.
|
|
|
|
*/
|
2015-10-20 07:32:31 -03:00
|
|
|
char *DataFlash_File::_lastlog_file_name(void) const
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
char *buf = nullptr;
|
2017-02-13 22:25:32 -04:00
|
|
|
if (asprintf(&buf, "%s/LASTLOG.TXT", _log_directory) == -1) {
|
2016-10-30 02:24:21 -03:00
|
|
|
return nullptr;
|
2015-05-04 04:27:15 -03:00
|
|
|
}
|
2013-04-17 08:32:53 -03:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// remove all log files
|
|
|
|
void DataFlash_File::EraseAll()
|
|
|
|
{
|
|
|
|
uint16_t log_num;
|
2016-09-18 09:31:29 -03:00
|
|
|
const bool was_logging = (_write_fd != -1);
|
2013-12-28 23:59:35 -04:00
|
|
|
stop_logging();
|
2018-05-11 09:26:52 -03:00
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
for (log_num=1; log_num<=MAX_LOG_FILES; log_num++) {
|
2013-04-17 08:32:53 -03:00
|
|
|
char *fname = _log_file_name(log_num);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (fname == nullptr) {
|
2013-04-17 08:32:53 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
unlink(fname);
|
|
|
|
free(fname);
|
|
|
|
}
|
|
|
|
char *fname = _lastlog_file_name();
|
2016-10-30 02:24:21 -03:00
|
|
|
if (fname != nullptr) {
|
2013-04-17 08:32:53 -03:00
|
|
|
unlink(fname);
|
|
|
|
free(fname);
|
|
|
|
}
|
2018-05-11 09:26:52 -03:00
|
|
|
|
2015-11-10 23:51:03 -04:00
|
|
|
_cached_oldest_log = 0;
|
2016-09-18 09:31:29 -03:00
|
|
|
|
|
|
|
if (was_logging) {
|
|
|
|
start_new_log();
|
|
|
|
}
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
|
|
|
|
2017-06-08 22:36:18 -03:00
|
|
|
bool DataFlash_File::WritesOK() const
|
|
|
|
{
|
|
|
|
if (_write_fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_open_error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-30 08:09:20 -03:00
|
|
|
|
|
|
|
bool DataFlash_File::StartNewLogOK() const
|
|
|
|
{
|
|
|
|
if (_open_error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return DataFlash_Backend::StartNewLogOK();
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
/* Write a block of data at current offset */
|
2017-06-30 08:09:20 -03:00
|
|
|
bool DataFlash_File::_WritePrioritisedBlock(const void *pBuffer, uint16_t size, bool is_critical)
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
2015-08-06 09:18:28 -03:00
|
|
|
if (! WriteBlockCheckStartupMessages()) {
|
|
|
|
_dropped++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-28 18:09:45 -04:00
|
|
|
if (!semaphore->take(1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-30 15:13:12 -03:00
|
|
|
uint32_t space = _writebuf.space();
|
2015-08-06 09:18:28 -03:00
|
|
|
|
2015-09-17 07:58:35 -03:00
|
|
|
if (_writing_startup_messages &&
|
2015-11-09 18:14:22 -04:00
|
|
|
_startup_messagewriter->fmt_done()) {
|
2015-09-17 07:58:35 -03:00
|
|
|
// the state machine has called us, and it has finished
|
|
|
|
// writing format messages out. It can always get back to us
|
|
|
|
// with more messages later, so let's leave room for other
|
|
|
|
// things:
|
|
|
|
if (space < non_messagewriter_message_reserved_space()) {
|
|
|
|
// this message isn't dropped, it will be sent again...
|
2015-12-16 18:23:14 -04:00
|
|
|
semaphore->give();
|
2015-09-17 07:58:35 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we reserve some amount of space for critical messages:
|
|
|
|
if (!is_critical && space < critical_message_reserved_space()) {
|
|
|
|
_dropped++;
|
2015-12-16 18:23:14 -04:00
|
|
|
semaphore->give();
|
2015-09-17 07:58:35 -03:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-06 09:18:28 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// if no room for entire message - drop it:
|
2013-04-17 08:32:53 -03:00
|
|
|
if (space < size) {
|
2015-10-20 02:49:32 -03:00
|
|
|
hal.util->perf_count(_perf_overruns);
|
2015-08-06 09:18:28 -03:00
|
|
|
_dropped++;
|
2015-12-16 18:23:14 -04:00
|
|
|
semaphore->give();
|
2015-08-06 09:18:28 -03:00
|
|
|
return false;
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
|
|
|
|
2016-07-29 21:52:21 -03:00
|
|
|
_writebuf.write((uint8_t*)pBuffer, size);
|
2017-08-25 04:18:26 -03:00
|
|
|
df_stats_gather(size);
|
2015-12-16 18:23:14 -04:00
|
|
|
semaphore->give();
|
2015-08-06 09:18:28 -03:00
|
|
|
return true;
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
find the highest log number
|
|
|
|
*/
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t DataFlash_File::find_last_log()
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
|
|
|
unsigned ret = 0;
|
|
|
|
char *fname = _lastlog_file_name();
|
2016-10-30 02:24:21 -03:00
|
|
|
if (fname == nullptr) {
|
2013-04-17 08:32:53 -03:00
|
|
|
return ret;
|
|
|
|
}
|
2016-10-30 10:42:48 -03:00
|
|
|
int fd = open(fname, O_RDONLY|O_CLOEXEC);
|
2013-04-17 08:32:53 -03:00
|
|
|
free(fname);
|
2015-12-08 19:53:20 -04:00
|
|
|
if (fd != -1) {
|
2013-04-17 08:32:53 -03:00
|
|
|
char buf[10];
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2015-12-08 19:53:20 -04:00
|
|
|
if (read(fd, buf, sizeof(buf)-1) > 0) {
|
2018-01-05 03:16:47 -04:00
|
|
|
#if HAL_OS_POSIX_IO
|
|
|
|
sscanf(buf, "%u", &ret);
|
|
|
|
#else
|
|
|
|
ret = strtol(buf, NULL, 10);
|
|
|
|
#endif
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
2015-12-08 19:53:20 -04:00
|
|
|
close(fd);
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
uint32_t DataFlash_File::_get_log_size(const uint16_t log_num) const
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
|
|
|
char *fname = _log_file_name(log_num);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (fname == nullptr) {
|
2013-04-17 08:32:53 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-13 21:32:44 -03:00
|
|
|
if (_write_fd != -1 && write_fd_semaphore->take_nonblocking()) {
|
|
|
|
if (_write_filename != nullptr && strcmp(_write_filename, fname) == 0) {
|
|
|
|
// it is the file we are currently writing
|
|
|
|
free(fname);
|
|
|
|
write_fd_semaphore->give();
|
|
|
|
return _write_offset;
|
|
|
|
}
|
|
|
|
write_fd_semaphore->give();
|
|
|
|
}
|
2013-04-17 08:32:53 -03:00
|
|
|
struct stat st;
|
|
|
|
if (::stat(fname, &st) != 0) {
|
2018-01-05 03:16:47 -04:00
|
|
|
printf("Unable to fetch Log File Size: %s\n", strerror(errno));
|
2013-04-17 08:32:53 -03:00
|
|
|
free(fname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free(fname);
|
|
|
|
return st.st_size;
|
|
|
|
}
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
uint32_t DataFlash_File::_get_log_time(const uint16_t log_num) const
|
2013-12-15 03:57:49 -04:00
|
|
|
{
|
|
|
|
char *fname = _log_file_name(log_num);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (fname == nullptr) {
|
2013-12-15 03:57:49 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-13 21:32:44 -03:00
|
|
|
if (_write_fd != -1 && write_fd_semaphore->take_nonblocking()) {
|
|
|
|
if (_write_filename != nullptr && strcmp(_write_filename, fname) == 0) {
|
|
|
|
// it is the file we are currently writing
|
|
|
|
free(fname);
|
|
|
|
write_fd_semaphore->give();
|
2018-06-14 02:30:54 -03:00
|
|
|
uint64_t utc_usec;
|
|
|
|
if (!AP::rtc().get_utc_usec(utc_usec)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return utc_usec / 1000000U;
|
2018-06-13 21:32:44 -03:00
|
|
|
}
|
|
|
|
write_fd_semaphore->give();
|
|
|
|
}
|
2013-12-15 03:57:49 -04:00
|
|
|
struct stat st;
|
|
|
|
if (::stat(fname, &st) != 0) {
|
|
|
|
free(fname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free(fname);
|
|
|
|
return st.st_mtime;
|
|
|
|
}
|
|
|
|
|
2015-10-20 07:32:31 -03:00
|
|
|
/*
|
|
|
|
convert a list entry number back into a log number (which can then
|
|
|
|
be converted into a filename). A "list entry number" is a sequence
|
|
|
|
where the oldest log has a number of 1, the second-from-oldest 2,
|
|
|
|
and so on. Thus the highest list entry number is equal to the
|
|
|
|
number of logs.
|
|
|
|
*/
|
|
|
|
uint16_t DataFlash_File::_log_num_from_list_entry(const uint16_t list_entry)
|
|
|
|
{
|
|
|
|
uint16_t oldest_log = find_oldest_log();
|
|
|
|
if (oldest_log == 0) {
|
|
|
|
// We don't have any logs...
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t log_num = oldest_log + list_entry - 1;
|
|
|
|
if (log_num > MAX_LOG_FILES) {
|
|
|
|
log_num -= MAX_LOG_FILES;
|
|
|
|
}
|
|
|
|
return (uint16_t)log_num;
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
/*
|
|
|
|
find the number of pages in a log
|
|
|
|
*/
|
2015-10-20 07:32:31 -03:00
|
|
|
void DataFlash_File::get_log_boundaries(const uint16_t list_entry, uint16_t & start_page, uint16_t & end_page)
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
2015-10-20 07:32:31 -03:00
|
|
|
const uint16_t log_num = _log_num_from_list_entry(list_entry);
|
|
|
|
if (log_num == 0) {
|
|
|
|
// that failed - probably no logs
|
|
|
|
start_page = 0;
|
|
|
|
end_page = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
start_page = 0;
|
|
|
|
end_page = _get_log_size(log_num) / DATAFLASH_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2013-12-15 03:57:49 -04:00
|
|
|
/*
|
2015-11-10 23:51:03 -04:00
|
|
|
retrieve data from a log file
|
2013-12-15 03:57:49 -04:00
|
|
|
*/
|
2015-10-20 07:32:31 -03:00
|
|
|
int16_t DataFlash_File::get_log_data(const uint16_t list_entry, const uint16_t page, const uint32_t offset, const uint16_t len, uint8_t *data)
|
2013-12-15 03:57:49 -04:00
|
|
|
{
|
2014-12-20 22:31:27 -04:00
|
|
|
if (!_initialised || _open_error) {
|
2013-12-15 03:57:49 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-20 07:32:31 -03:00
|
|
|
|
|
|
|
const uint16_t log_num = _log_num_from_list_entry(list_entry);
|
|
|
|
if (log_num == 0) {
|
|
|
|
// that failed - probably no logs
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-15 03:57:49 -04:00
|
|
|
if (_read_fd != -1 && log_num != _read_fd_log_num) {
|
|
|
|
::close(_read_fd);
|
|
|
|
_read_fd = -1;
|
|
|
|
}
|
|
|
|
if (_read_fd == -1) {
|
|
|
|
char *fname = _log_file_name(log_num);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (fname == nullptr) {
|
2013-12-15 03:57:49 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2013-12-27 23:24:28 -04:00
|
|
|
stop_logging();
|
2016-10-30 10:42:48 -03:00
|
|
|
_read_fd = ::open(fname, O_RDONLY|O_CLOEXEC);
|
2013-12-15 03:57:49 -04:00
|
|
|
if (_read_fd == -1) {
|
2014-12-20 22:31:27 -04:00
|
|
|
_open_error = true;
|
|
|
|
int saved_errno = errno;
|
|
|
|
::printf("Log read open fail for %s - %s\n",
|
|
|
|
fname, strerror(saved_errno));
|
|
|
|
hal.console->printf("Log read open fail for %s - %s\n",
|
|
|
|
fname, strerror(saved_errno));
|
|
|
|
free(fname);
|
2013-12-15 03:57:49 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-12-20 22:31:27 -04:00
|
|
|
free(fname);
|
2013-12-15 03:57:49 -04:00
|
|
|
_read_offset = 0;
|
|
|
|
_read_fd_log_num = log_num;
|
|
|
|
}
|
|
|
|
uint32_t ofs = page * (uint32_t)DATAFLASH_PAGE_SIZE + offset;
|
2014-02-14 03:21:57 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
this rather strange bit of code is here to work around a bug
|
|
|
|
in file offsets in NuttX. Every few hundred blocks of reads
|
|
|
|
(starting at around 350k into a file) NuttX will get the
|
|
|
|
wrong offset for sequential reads. The offset it gets is
|
|
|
|
typically 128k earlier than it should be. It turns out that
|
|
|
|
calling lseek() with 0 offset and SEEK_CUR works around the
|
|
|
|
bug. We can remove this once we find the real bug.
|
|
|
|
*/
|
|
|
|
if (ofs / 4096 != (ofs+len) / 4096) {
|
2014-03-24 23:31:21 -03:00
|
|
|
off_t seek_current = ::lseek(_read_fd, 0, SEEK_CUR);
|
2015-12-08 08:43:02 -04:00
|
|
|
if (seek_current == (off_t)-1) {
|
|
|
|
close(_read_fd);
|
|
|
|
_read_fd = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-24 23:31:21 -03:00
|
|
|
if (seek_current != (off_t)_read_offset) {
|
2015-12-08 08:43:02 -04:00
|
|
|
if (::lseek(_read_fd, _read_offset, SEEK_SET) == (off_t)-1) {
|
|
|
|
close(_read_fd);
|
|
|
|
_read_fd = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-14 03:21:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-15 03:57:49 -04:00
|
|
|
if (ofs != _read_offset) {
|
2015-12-08 08:43:02 -04:00
|
|
|
if (::lseek(_read_fd, ofs, SEEK_SET) == (off_t)-1) {
|
|
|
|
close(_read_fd);
|
|
|
|
_read_fd = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-14 03:21:57 -04:00
|
|
|
_read_offset = ofs;
|
2013-12-15 03:57:49 -04:00
|
|
|
}
|
|
|
|
int16_t ret = (int16_t)::read(_read_fd, data, len);
|
|
|
|
if (ret > 0) {
|
|
|
|
_read_offset += ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
find size and date of a log
|
|
|
|
*/
|
2015-10-20 07:32:31 -03:00
|
|
|
void DataFlash_File::get_log_info(const uint16_t list_entry, uint32_t &size, uint32_t &time_utc)
|
2013-12-15 03:57:49 -04:00
|
|
|
{
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t log_num = _log_num_from_list_entry(list_entry);
|
|
|
|
if (log_num == 0) {
|
|
|
|
// that failed - probably no logs
|
|
|
|
size = 0;
|
|
|
|
time_utc = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-15 03:57:49 -04:00
|
|
|
size = _get_log_size(log_num);
|
|
|
|
time_utc = _get_log_time(log_num);
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
get the number of logs - note that the log numbers must be consecutive
|
|
|
|
*/
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t DataFlash_File::get_num_logs()
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t ret = 0;
|
2013-04-17 08:32:53 -03:00
|
|
|
uint16_t high = find_last_log();
|
2015-10-20 07:32:31 -03:00
|
|
|
uint16_t i;
|
|
|
|
for (i=high; i>0; i--) {
|
|
|
|
if (! log_exists(i)) {
|
2013-04-17 08:32:53 -03:00
|
|
|
break;
|
|
|
|
}
|
2015-10-20 07:32:31 -03:00
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
if (i == 0) {
|
|
|
|
for (i=MAX_LOG_FILES; i>high; i--) {
|
|
|
|
if (! log_exists(i)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret++;
|
|
|
|
}
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-27 23:24:28 -04:00
|
|
|
stop logging
|
2013-04-17 08:32:53 -03:00
|
|
|
*/
|
2013-12-27 23:24:28 -04:00
|
|
|
void DataFlash_File::stop_logging(void)
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
2017-09-12 22:46:34 -03:00
|
|
|
// best-case effort to avoid annoying the IO thread
|
|
|
|
const bool have_sem = write_fd_semaphore->take(1);
|
2013-04-17 08:32:53 -03:00
|
|
|
if (_write_fd != -1) {
|
|
|
|
int fd = _write_fd;
|
|
|
|
_write_fd = -1;
|
|
|
|
::close(fd);
|
|
|
|
}
|
2017-09-12 22:46:34 -03:00
|
|
|
if (have_sem) {
|
|
|
|
write_fd_semaphore->give();
|
|
|
|
} else {
|
|
|
|
_internal_errors++;
|
|
|
|
}
|
2013-12-27 23:24:28 -04:00
|
|
|
}
|
|
|
|
|
2017-07-06 22:28:42 -03:00
|
|
|
void DataFlash_File::PrepForArming()
|
|
|
|
{
|
|
|
|
if (logging_started()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
start_new_log();
|
|
|
|
}
|
2013-12-27 23:24:28 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
start writing to a new log file
|
|
|
|
*/
|
|
|
|
uint16_t DataFlash_File::start_new_log(void)
|
|
|
|
{
|
|
|
|
stop_logging();
|
|
|
|
|
2016-04-15 06:53:54 -03:00
|
|
|
start_new_log_reset_variables();
|
2015-11-09 18:14:22 -04:00
|
|
|
|
2014-12-20 22:31:27 -04:00
|
|
|
if (_open_error) {
|
|
|
|
// we have previously failed to open a file - don't try again
|
|
|
|
// to prevent us trying to open files while in flight
|
|
|
|
return 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2013-12-15 03:57:49 -04:00
|
|
|
if (_read_fd != -1) {
|
|
|
|
::close(_read_fd);
|
|
|
|
_read_fd = -1;
|
|
|
|
}
|
2013-04-17 08:32:53 -03:00
|
|
|
|
2016-07-06 05:58:47 -03:00
|
|
|
if (disk_space_avail() < _free_space_min_avail) {
|
|
|
|
hal.console->printf("Out of space for logging\n");
|
|
|
|
_open_error = true;
|
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
uint16_t log_num = find_last_log();
|
|
|
|
// re-use empty logs if possible
|
2013-04-19 21:41:04 -03:00
|
|
|
if (_get_log_size(log_num) > 0 || log_num == 0) {
|
2013-04-17 08:32:53 -03:00
|
|
|
log_num++;
|
|
|
|
}
|
|
|
|
if (log_num > MAX_LOG_FILES) {
|
|
|
|
log_num = 1;
|
|
|
|
}
|
2018-06-13 21:32:44 -03:00
|
|
|
if (!write_fd_semaphore->take(1)) {
|
2017-06-30 08:09:20 -03:00
|
|
|
_open_error = true;
|
2015-12-08 08:07:43 -04:00
|
|
|
return 0xFFFF;
|
|
|
|
}
|
2018-06-13 21:32:44 -03:00
|
|
|
if (_write_filename) {
|
|
|
|
free(_write_filename);
|
|
|
|
_write_filename = nullptr;
|
|
|
|
}
|
|
|
|
_write_filename = _log_file_name(log_num);
|
|
|
|
if (_write_filename == nullptr) {
|
2017-09-12 22:46:34 -03:00
|
|
|
_open_error = true;
|
2018-06-13 21:32:44 -03:00
|
|
|
write_fd_semaphore->give();
|
2017-09-12 22:46:34 -03:00
|
|
|
return 0xFFFF;
|
|
|
|
}
|
2018-01-05 03:16:47 -04:00
|
|
|
#if HAL_OS_POSIX_IO
|
2018-06-13 21:32:44 -03:00
|
|
|
_write_fd = ::open(_write_filename, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666);
|
2018-01-05 03:16:47 -04:00
|
|
|
#else
|
|
|
|
//TODO add support for mode flags
|
2018-06-13 21:32:44 -03:00
|
|
|
_write_fd = ::open(_write_filename, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC);
|
2018-01-05 03:16:47 -04:00
|
|
|
#endif
|
2015-11-10 23:51:03 -04:00
|
|
|
_cached_oldest_log = 0;
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
if (_write_fd == -1) {
|
|
|
|
_initialised = false;
|
2014-12-20 22:31:27 -04:00
|
|
|
_open_error = true;
|
2017-09-12 22:46:34 -03:00
|
|
|
write_fd_semaphore->give();
|
2014-12-20 22:31:27 -04:00
|
|
|
int saved_errno = errno;
|
|
|
|
::printf("Log open fail for %s - %s\n",
|
2018-06-13 21:32:44 -03:00
|
|
|
_write_filename, strerror(saved_errno));
|
2014-12-20 22:31:27 -04:00
|
|
|
hal.console->printf("Log open fail for %s - %s\n",
|
2018-06-13 21:32:44 -03:00
|
|
|
_write_filename, strerror(saved_errno));
|
2013-04-17 08:32:53 -03:00
|
|
|
return 0xFFFF;
|
|
|
|
}
|
2018-06-13 21:32:44 -03:00
|
|
|
_last_write_ms = AP_HAL::millis();
|
2014-01-12 23:25:16 -04:00
|
|
|
_write_offset = 0;
|
2016-07-29 21:52:21 -03:00
|
|
|
_writebuf.clear();
|
2017-09-12 22:46:34 -03:00
|
|
|
write_fd_semaphore->give();
|
2013-04-17 08:32:53 -03:00
|
|
|
|
|
|
|
// now update lastlog.txt with the new log number
|
2018-06-13 21:32:44 -03:00
|
|
|
char *fname = _lastlog_file_name();
|
2015-12-08 19:53:20 -04:00
|
|
|
|
|
|
|
// we avoid fopen()/fprintf() here as it is not available on as many
|
2018-05-11 09:26:52 -03:00
|
|
|
// systems as open/write
|
2018-01-05 03:16:47 -04:00
|
|
|
#if HAL_OS_POSIX_IO
|
2016-10-30 10:42:48 -03:00
|
|
|
int fd = open(fname, O_WRONLY|O_CREAT|O_CLOEXEC, 0644);
|
2018-01-05 03:16:47 -04:00
|
|
|
#else
|
|
|
|
int fd = open(fname, O_WRONLY|O_CREAT|O_CLOEXEC);
|
|
|
|
#endif
|
2015-12-08 19:53:20 -04:00
|
|
|
free(fname);
|
|
|
|
if (fd == -1) {
|
2017-06-30 08:09:20 -03:00
|
|
|
_open_error = true;
|
2015-12-08 08:18:37 -04:00
|
|
|
return 0xFFFF;
|
|
|
|
}
|
2015-12-08 19:53:20 -04:00
|
|
|
|
|
|
|
char buf[30];
|
|
|
|
snprintf(buf, sizeof(buf), "%u\r\n", (unsigned)log_num);
|
2016-08-25 00:57:58 -03:00
|
|
|
const ssize_t to_write = strlen(buf);
|
|
|
|
const ssize_t written = write(fd, buf, to_write);
|
2015-12-08 19:53:20 -04:00
|
|
|
close(fd);
|
2013-04-19 04:49:16 -03:00
|
|
|
|
2016-08-25 00:57:58 -03:00
|
|
|
if (written < to_write) {
|
2017-06-30 08:09:20 -03:00
|
|
|
_open_error = true;
|
2016-08-25 00:57:58 -03:00
|
|
|
return 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
return log_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-18 22:57:01 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
|
|
|
void DataFlash_File::flush(void)
|
2018-05-13 19:45:48 -03:00
|
|
|
#if APM_BUILD_TYPE(APM_BUILD_Replay) || APM_BUILD_TYPE(APM_BUILD_UNKNOWN)
|
2015-06-18 22:57:01 -03:00
|
|
|
{
|
2016-11-06 01:26:31 -04:00
|
|
|
uint32_t tnow = AP_HAL::millis();
|
2016-07-29 21:52:21 -03:00
|
|
|
while (_write_fd != -1 && _initialised && !_open_error && _writebuf.available()) {
|
2015-06-18 22:57:01 -03:00
|
|
|
// convince the IO timer that it really is OK to write out
|
|
|
|
// less than _writebuf_chunk bytes:
|
2016-11-06 01:26:31 -04:00
|
|
|
if (tnow > 2001) { // avoid resetting _last_write_time to 0
|
|
|
|
_last_write_time = tnow - 2001;
|
2016-04-26 09:12:19 -03:00
|
|
|
}
|
2015-06-18 22:57:01 -03:00
|
|
|
_io_timer();
|
|
|
|
}
|
2017-09-12 22:46:34 -03:00
|
|
|
if (write_fd_semaphore->take(1)) {
|
|
|
|
if (_write_fd != -1) {
|
|
|
|
::fsync(_write_fd);
|
|
|
|
}
|
|
|
|
write_fd_semaphore->give();
|
|
|
|
} else {
|
|
|
|
_internal_errors++;
|
2015-06-18 22:57:01 -03:00
|
|
|
}
|
|
|
|
}
|
2018-05-13 19:45:48 -03:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
// flush is for replay and examples only
|
|
|
|
}
|
|
|
|
#endif // APM_BUILD_TYPE(APM_BUILD_Replay) || APM_BUILD_TYPE(APM_BUILD_UNKNOWN)
|
2015-06-18 22:57:01 -03:00
|
|
|
#endif
|
2013-04-17 08:32:53 -03:00
|
|
|
|
2013-09-28 03:29:58 -03:00
|
|
|
void DataFlash_File::_io_timer(void)
|
2013-04-17 08:32:53 -03:00
|
|
|
{
|
2016-11-06 02:09:39 -04:00
|
|
|
uint32_t tnow = AP_HAL::millis();
|
|
|
|
_io_timer_heartbeat = tnow;
|
2014-12-20 22:31:27 -04:00
|
|
|
if (_write_fd == -1 || !_initialised || _open_error) {
|
2013-04-17 08:32:53 -03:00
|
|
|
return;
|
|
|
|
}
|
2014-01-13 21:39:49 -04:00
|
|
|
|
2016-07-29 21:52:21 -03:00
|
|
|
uint32_t nbytes = _writebuf.available();
|
2013-04-17 08:32:53 -03:00
|
|
|
if (nbytes == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-12 23:25:16 -04:00
|
|
|
if (nbytes < _writebuf_chunk &&
|
2016-11-06 01:26:31 -04:00
|
|
|
tnow - _last_write_time < 2000UL) {
|
2015-07-19 21:41:14 -03:00
|
|
|
// write in _writebuf_chunk-sized chunks, but always write at
|
|
|
|
// least once per 2 seconds if data is available
|
2013-04-17 08:32:53 -03:00
|
|
|
return;
|
|
|
|
}
|
2016-07-06 05:58:47 -03:00
|
|
|
if (tnow - _free_space_last_check_time > _free_space_check_interval) {
|
|
|
|
_free_space_last_check_time = tnow;
|
2017-07-27 23:07:35 -03:00
|
|
|
last_io_operation = "disk_space_avail";
|
2016-07-06 05:58:47 -03:00
|
|
|
if (disk_space_avail() < _free_space_min_avail) {
|
|
|
|
hal.console->printf("Out of space for logging\n");
|
|
|
|
stop_logging();
|
|
|
|
_open_error = true; // prevent logging starting again
|
2017-07-27 23:07:35 -03:00
|
|
|
last_io_operation = "";
|
2016-07-06 05:58:47 -03:00
|
|
|
return;
|
|
|
|
}
|
2017-07-27 23:07:35 -03:00
|
|
|
last_io_operation = "";
|
2016-07-06 05:58:47 -03:00
|
|
|
}
|
2014-01-12 23:25:16 -04:00
|
|
|
|
2015-10-20 02:49:32 -03:00
|
|
|
hal.util->perf_begin(_perf_write);
|
2014-01-12 23:25:16 -04:00
|
|
|
|
2013-04-17 08:32:53 -03:00
|
|
|
_last_write_time = tnow;
|
2014-01-12 23:25:16 -04:00
|
|
|
if (nbytes > _writebuf_chunk) {
|
2013-04-17 08:32:53 -03:00
|
|
|
// be kind to the FAT PX4 filesystem
|
2014-01-12 23:25:16 -04:00
|
|
|
nbytes = _writebuf_chunk;
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
2014-01-12 23:25:16 -04:00
|
|
|
|
2016-07-29 21:52:21 -03:00
|
|
|
uint32_t size;
|
|
|
|
const uint8_t *head = _writebuf.readptr(size);
|
|
|
|
nbytes = MIN(nbytes, size);
|
|
|
|
|
|
|
|
// try to align writes on a 512 byte boundary to avoid filesystem reads
|
2014-01-12 23:25:16 -04:00
|
|
|
if ((nbytes + _write_offset) % 512 != 0) {
|
|
|
|
uint32_t ofs = (nbytes + _write_offset) % 512;
|
|
|
|
if (ofs < nbytes) {
|
|
|
|
nbytes -= ofs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 23:07:35 -03:00
|
|
|
last_io_operation = "write";
|
2017-09-12 22:46:34 -03:00
|
|
|
if (!write_fd_semaphore->take(1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_write_fd == -1) {
|
|
|
|
write_fd_semaphore->give();
|
|
|
|
return;
|
|
|
|
}
|
2016-07-29 21:52:21 -03:00
|
|
|
ssize_t nwritten = ::write(_write_fd, head, nbytes);
|
2017-07-27 23:07:35 -03:00
|
|
|
last_io_operation = "";
|
2013-04-17 08:32:53 -03:00
|
|
|
if (nwritten <= 0) {
|
2018-06-13 21:32:44 -03:00
|
|
|
if (tnow - _last_write_ms > 2000) {
|
|
|
|
// if we can't write for 2 seconds we give up and close
|
|
|
|
// the file. This allows us to cope with temporary write
|
|
|
|
// failures caused by directory listing
|
|
|
|
hal.util->perf_count(_perf_errors);
|
|
|
|
last_io_operation = "close";
|
|
|
|
close(_write_fd);
|
|
|
|
last_io_operation = "";
|
|
|
|
_write_fd = -1;
|
|
|
|
_initialised = false;
|
|
|
|
printf("Failed to write to File: %s\n", strerror(errno));
|
|
|
|
}
|
2013-04-17 08:32:53 -03:00
|
|
|
} else {
|
2018-06-13 21:32:44 -03:00
|
|
|
_last_write_ms = tnow;
|
2014-01-12 23:25:16 -04:00
|
|
|
_write_offset += nwritten;
|
2016-07-29 21:52:21 -03:00
|
|
|
_writebuf.advance(nwritten);
|
2014-01-13 21:39:49 -04:00
|
|
|
/*
|
2016-07-29 21:52:21 -03:00
|
|
|
the best strategy for minimizing corruption on microSD cards
|
2014-01-13 21:39:49 -04:00
|
|
|
seems to be to write in 4k chunks and fsync the file on each
|
|
|
|
chunk, ensuring the directory entry is updated after each
|
|
|
|
write.
|
|
|
|
*/
|
2018-05-11 09:26:52 -03:00
|
|
|
#if CONFIG_HAL_BOARD != HAL_BOARD_SITL && CONFIG_HAL_BOARD_SUBTYPE != HAL_BOARD_SUBTYPE_LINUX_NONE
|
2017-07-27 23:07:35 -03:00
|
|
|
last_io_operation = "fsync";
|
2014-01-13 21:39:49 -04:00
|
|
|
::fsync(_write_fd);
|
2017-07-27 23:07:35 -03:00
|
|
|
last_io_operation = "";
|
2014-01-13 21:40:41 -04:00
|
|
|
#endif
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
2017-09-12 22:46:34 -03:00
|
|
|
write_fd_semaphore->give();
|
2015-10-20 02:49:32 -03:00
|
|
|
hal.util->perf_end(_perf_write);
|
2013-04-17 08:32:53 -03:00
|
|
|
}
|
|
|
|
|
2016-07-07 04:12:27 -03:00
|
|
|
// this sensor is enabled if we should be logging at the moment
|
|
|
|
bool DataFlash_File::logging_enabled() const
|
|
|
|
{
|
|
|
|
if (hal.util->get_soft_armed() ||
|
|
|
|
_front.log_while_disarmed()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-06 02:09:39 -04:00
|
|
|
bool DataFlash_File::io_thread_alive() const
|
|
|
|
{
|
|
|
|
// if the io thread hasn't had a heartbeat in a full second then it is dead
|
2018-05-13 05:33:58 -03:00
|
|
|
return (AP_HAL::millis() - _io_timer_heartbeat) < 1000;
|
2016-11-06 02:09:39 -04:00
|
|
|
}
|
|
|
|
|
2016-07-07 04:12:27 -03:00
|
|
|
bool DataFlash_File::logging_failed() const
|
|
|
|
{
|
2017-09-11 08:28:04 -03:00
|
|
|
if (!_initialised) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-07 04:12:27 -03:00
|
|
|
if (_open_error) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-06 02:09:39 -04:00
|
|
|
if (!io_thread_alive()) {
|
|
|
|
// No heartbeat in a second. IO thread is dead?! Very Not
|
|
|
|
// Good.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-07 04:12:27 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-28 02:58:20 -03:00
|
|
|
void DataFlash_File::vehicle_was_disarmed()
|
|
|
|
{
|
|
|
|
if (_front._params.file_disarm_rot) {
|
|
|
|
// rotate our log. Closing the current one and letting the
|
|
|
|
// logging restart naturally based on log_disarmed should do
|
|
|
|
// the trick:
|
|
|
|
stop_logging();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 04:18:26 -03:00
|
|
|
void DataFlash_File::Log_Write_DataFlash_Stats_File(const struct df_stats &_stats)
|
|
|
|
{
|
|
|
|
struct log_DSF pkt = {
|
|
|
|
LOG_PACKET_HEADER_INIT(LOG_DF_FILE_STATS),
|
|
|
|
time_us : AP_HAL::micros64(),
|
|
|
|
dropped : _dropped,
|
|
|
|
internal_errors : _internal_errors,
|
|
|
|
blocks : _stats.blocks,
|
|
|
|
bytes : _stats.bytes,
|
|
|
|
buf_space_min : _stats.buf_space_min,
|
|
|
|
buf_space_max : _stats.buf_space_max,
|
|
|
|
buf_space_avg : (_stats.blocks) ? (_stats.buf_space_sigma / _stats.blocks) : 0,
|
|
|
|
|
|
|
|
};
|
|
|
|
WriteBlock(&pkt, sizeof(pkt));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlash_File::df_stats_gather(const uint16_t bytes_written) {
|
|
|
|
const uint32_t space_remaining = _writebuf.space();
|
|
|
|
if (space_remaining < stats.buf_space_min) {
|
|
|
|
stats.buf_space_min = space_remaining;
|
|
|
|
}
|
|
|
|
if (space_remaining > stats.buf_space_max) {
|
|
|
|
stats.buf_space_max = space_remaining;
|
|
|
|
}
|
|
|
|
stats.buf_space_sigma += space_remaining;
|
|
|
|
stats.bytes += bytes_written;
|
|
|
|
stats.blocks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlash_File::df_stats_clear() {
|
|
|
|
memset(&stats, '\0', sizeof(stats));
|
|
|
|
stats.buf_space_min = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlash_File::df_stats_log() {
|
|
|
|
Log_Write_DataFlash_Stats_File(stats);
|
|
|
|
df_stats_clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-30 18:30:50 -04:00
|
|
|
#endif // HAL_OS_POSIX_IO
|