From 8e58cb993df078cb01ae64e2df8e342444f055c6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 11 Jul 2019 12:16:29 +1000 Subject: [PATCH] AP_Logger: fix timestamp on log files opened before GPS lock --- libraries/AP_Logger/AP_Logger_File.cpp | 22 ++++++++++++++++++++++ libraries/AP_Logger/AP_Logger_File.h | 1 + 2 files changed, 23 insertions(+) diff --git a/libraries/AP_Logger/AP_Logger_File.cpp b/libraries/AP_Logger/AP_Logger_File.cpp index 1b829dc770..3883105350 100644 --- a/libraries/AP_Logger/AP_Logger_File.cpp +++ b/libraries/AP_Logger/AP_Logger_File.cpp @@ -883,6 +883,11 @@ uint16_t AP_Logger_File::start_new_log(void) write_fd_semaphore.give(); return 0xFFFF; } + + // remember if we had utc time when we opened the file + uint64_t utc_usec; + _need_rtc_update = !AP::rtc().get_utc_usec(utc_usec); + EXPECT_DELAY_MS(3000); #if HAL_OS_POSIX_IO _write_fd = ::open(_write_filename, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666); @@ -1058,7 +1063,24 @@ void AP_Logger_File::_io_timer(void) ::fsync(_write_fd); last_io_operation = ""; #endif + +#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS + // ChibiOS does not update mtime on writes, so if we opened + // without knowing the time we should update it later + if (_need_rtc_update) { + uint64_t utc_usec; + if (AP::rtc().get_utc_usec(utc_usec)) { + struct utimbuf t {}; + t.modtime = utc_usec / (1000UL * 1000UL); + t.actime = t.modtime; + // we ignore return on utime() as there is nothing useful we can do + (void)utime(_write_filename, &t); + _need_rtc_update = false; + } + } +#endif } + write_fd_semaphore.give(); hal.util->perf_end(_perf_write); } diff --git a/libraries/AP_Logger/AP_Logger_File.h b/libraries/AP_Logger/AP_Logger_File.h index 4d8cb66502..c8797baeb5 100644 --- a/libraries/AP_Logger/AP_Logger_File.h +++ b/libraries/AP_Logger/AP_Logger_File.h @@ -67,6 +67,7 @@ private: int _write_fd; char *_write_filename; uint32_t _last_write_ms; + bool _need_rtc_update; int _read_fd; uint16_t _read_fd_log_num;