From d8e4669e07237b9ec62218e367c48f16e59c9fe2 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Tue, 23 Nov 2021 13:40:39 +1100 Subject: [PATCH] AP_HAL_Linux: tidy set/get of hw RTC --- libraries/AP_HAL_Linux/Util.cpp | 19 +++++++++++++++---- libraries/AP_HAL_Linux/Util.h | 4 +++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/libraries/AP_HAL_Linux/Util.cpp b/libraries/AP_HAL_Linux/Util.cpp index 350455e02d..5492fc99ee 100644 --- a/libraries/AP_HAL_Linux/Util.cpp +++ b/libraries/AP_HAL_Linux/Util.cpp @@ -62,12 +62,23 @@ void Util::commandline_arguments(uint8_t &argc, char * const *&argv) argv = saved_argv; } +uint64_t Util::get_hw_rtc() const +{ + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + const uint64_t seconds = ts.tv_sec; + const uint64_t nanoseconds = ts.tv_nsec; + return (seconds * 1000000ULL + nanoseconds/1000ULL); +} + void Util::set_hw_rtc(uint64_t time_utc_usec) { +// don't reset the HW clock time on people's laptops. #if CONFIG_HAL_BOARD_SUBTYPE != HAL_BOARD_SUBTYPE_LINUX_NONE - // call superclass method to set time. We've guarded this so we - // don't reset the HW clock time on people's laptops. - AP_HAL::Util::set_hw_rtc(time_utc_usec); + timespec ts; + ts.tv_sec = time_utc_usec/1000000ULL; + ts.tv_nsec = (time_utc_usec % 1000000ULL) * 1000ULL; + clock_settime(CLOCK_REALTIME, &ts); #endif } @@ -342,4 +353,4 @@ bool Util::parse_cpu_set(const char *str, cpu_set_t *cpu_set) const } while (*endptr != '\0'); return true; -} \ No newline at end of file +} diff --git a/libraries/AP_HAL_Linux/Util.h b/libraries/AP_HAL_Linux/Util.h index b86592044f..98b6440d19 100644 --- a/libraries/AP_HAL_Linux/Util.h +++ b/libraries/AP_HAL_Linux/Util.h @@ -37,9 +37,11 @@ public: void commandline_arguments(uint8_t &argc, char * const *&argv) override; /* - set system clock in UTC microseconds + get/set system clock in UTC microseconds */ void set_hw_rtc(uint64_t time_utc_usec) override; + uint64_t get_hw_rtc() const override; + const char *get_custom_log_directory() const override final { return custom_log_directory; } const char *get_custom_terrain_directory() const override final { return custom_terrain_directory; } const char *get_custom_storage_directory() const override final { return custom_storage_directory; }