diff --git a/src/modules/logger/logger.cpp b/src/modules/logger/logger.cpp index 99ec75898c..00ffc2fbd2 100644 --- a/src/modules/logger/logger.cpp +++ b/src/modules/logger/logger.cpp @@ -1990,6 +1990,11 @@ void Logger::write_info(LogType type, const char *name, uint32_t value) write_info_template(type, name, value, "uint32_t"); } +void Logger::write_info(LogType type, const char *name, uint64_t value) +{ + write_info_template(type, name, value, "uint64_t"); +} + template void Logger::write_info_template(LogType type, const char *name, T value, const char *type_str) @@ -2122,6 +2127,12 @@ void Logger::write_version(LogType type) write_info(type, "time_ref_utc", _param_sdlog_utc_offset.get() * 60); + uint64_t boot_time_utc_us; + + if (util::get_log_time(boot_time_utc_us, _param_sdlog_utc_offset.get() * 60, true)) { + write_info(type, "boot_time_utc_us", boot_time_utc_us); + } + if (_replay_file_name) { write_info(type, "replay", _replay_file_name); } diff --git a/src/modules/logger/logger.h b/src/modules/logger/logger.h index 44499c71b5..a24debd79e 100644 --- a/src/modules/logger/logger.h +++ b/src/modules/logger/logger.h @@ -267,6 +267,7 @@ private: void write_info_multiple(LogType type, const char *name, int fd); void write_info(LogType type, const char *name, int32_t value); void write_info(LogType type, const char *name, uint32_t value); + void write_info(LogType type, const char *name, uint64_t value); /** generic common template method for write_info variants */ template diff --git a/src/modules/logger/util.cpp b/src/modules/logger/util.cpp index efff34af6a..5fdd576bcf 100644 --- a/src/modules/logger/util.cpp +++ b/src/modules/logger/util.cpp @@ -72,20 +72,19 @@ bool file_exist(const char *filename) return stat(filename, &buffer) == 0; } -bool get_log_time(struct tm *tt, int utc_offset_sec, bool boot_time) +bool get_log_time(uint64_t &utc_time_usec, int utc_offset_sec, bool boot_time) { uORB::Subscription vehicle_gps_position_sub{ORB_ID(vehicle_gps_position)}; - time_t utc_time_sec; bool use_clock_time = true; /* Get the latest GPS publication */ sensor_gps_s gps_pos; if (vehicle_gps_position_sub.copy(&gps_pos)) { - utc_time_sec = gps_pos.time_utc_usec / 1e6; + utc_time_usec = gps_pos.time_utc_usec; - if (gps_pos.fix_type >= 2 && utc_time_sec >= GPS_EPOCH_SECS) { + if (gps_pos.fix_type >= 2 && utc_time_usec >= (uint64_t) GPS_EPOCH_SECS * 1000000ULL) { use_clock_time = false; } } @@ -94,22 +93,30 @@ bool get_log_time(struct tm *tt, int utc_offset_sec, bool boot_time) /* take clock time if there's no fix (yet) */ struct timespec ts = {}; px4_clock_gettime(CLOCK_REALTIME, &ts); - utc_time_sec = ts.tv_sec + (ts.tv_nsec / 1e9); + utc_time_usec = ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000ULL; - if (utc_time_sec < GPS_EPOCH_SECS) { + if (utc_time_usec < (uint64_t) GPS_EPOCH_SECS * 1000000ULL) { return false; } } /* strip the time elapsed since boot */ if (boot_time) { - utc_time_sec -= hrt_absolute_time() / 1e6; + utc_time_usec -= hrt_absolute_time(); } /* apply utc offset */ - utc_time_sec += utc_offset_sec; + utc_time_usec += (int64_t) utc_offset_sec * 1000000LL; - return gmtime_r(&utc_time_sec, tt) != nullptr; + return true; +} + +bool get_log_time(struct tm *tt, int utc_offset_sec, bool boot_time) +{ + uint64_t utc_time_usec; + bool result = get_log_time(utc_time_usec, utc_offset_sec, boot_time); + time_t utc_time_sec = static_cast(utc_time_usec / 1000000ULL); + return result && gmtime_r(&utc_time_sec, tt) != nullptr; } int check_free_space(const char *log_root_dir, int32_t max_log_dirs_to_keep, orb_advert_t &mavlink_log_pub, diff --git a/src/modules/logger/util.h b/src/modules/logger/util.h index 535815c7fb..f2939b0794 100644 --- a/src/modules/logger/util.h +++ b/src/modules/logger/util.h @@ -75,6 +75,16 @@ bool file_exist(const char *filename); int check_free_space(const char *log_root_dir, int32_t max_log_dirs_to_keep, orb_advert_t &mavlink_log_pub, int &sess_dir_index); + +/** + * Utility for fetching UTC time in microseconds from sensor_gps or CLOCK_REALTIME + * @param utc_time_usec returned microseconds + * @param utc_offset_sec UTC time offset [s] + * @param boot_time use time when booted instead of current time + * @return true on success, false otherwise (eg. if no gps) + */ +bool get_log_time(uint64_t &utc_time_usec, int utc_offset_sec, bool boot_time); + /** * Get the time for log file name * @param tt returned time