From a96d3f46bf6de9943fcfb63204efc8be445b1689 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Mon, 29 Nov 2021 18:19:38 +0800 Subject: [PATCH] boardd: new function sync_time (#23033) * sync_time * add direction * Update selfdrive/boardd/boardd.cc Co-authored-by: Willem Melching * enum class * rename * caps * lambda get_time_str * Revert "lambda get_time_str" This reverts commit 5eb6e19c5130b28963e9555f6c73835ac30d817d. * static Co-authored-by: Willem Melching old-commit-hash: 35c0319f6c846c900098b5280652ab16424658c6 --- selfdrive/boardd/boardd.cc | 62 ++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/selfdrive/boardd/boardd.cc b/selfdrive/boardd/boardd.cc index bbaf28ca6..3f66efc9f 100644 --- a/selfdrive/boardd/boardd.cc +++ b/selfdrive/boardd/boardd.cc @@ -62,7 +62,7 @@ std::atomic pigeon_active(false); ExitHandler do_exit; -std::string get_time_str(const struct tm &time) { +static std::string get_time_str(const struct tm &time) { char s[30] = {'\0'}; std::strftime(s, std::size(s), "%Y-%m-%d %H:%M:%S", &time); return s; @@ -78,6 +78,35 @@ bool check_all_connected(const std::vector &pandas) { return true; } +enum class SyncTimeDir { TO_PANDA, FROM_PANDA }; + +void sync_time(Panda *panda, SyncTimeDir dir) { + if (!panda->has_rtc) return; + + setenv("TZ", "UTC", 1); + struct tm sys_time = util::get_time(); + struct tm rtc_time = panda->get_rtc(); + + if (dir == SyncTimeDir::TO_PANDA) { + if (util::time_valid(sys_time)) { + // Write time to RTC if it looks reasonable + double seconds = difftime(mktime(&rtc_time), mktime(&sys_time)); + if (std::abs(seconds) > 1.1) { + panda->set_rtc(sys_time); + LOGW("Updating panda RTC. dt = %.2f System: %s RTC: %s", + seconds, get_time_str(sys_time).c_str(), get_time_str(rtc_time).c_str()); + } + } + } else if (dir == SyncTimeDir::FROM_PANDA) { + if (!util::time_valid(sys_time) && util::time_valid(rtc_time)) { + const struct timeval tv = {mktime(&rtc_time), 0}; + settimeofday(&tv, 0); + LOGE("System time wrong, setting from RTC. System: %s RTC: %s", + get_time_str(sys_time).c_str(), get_time_str(rtc_time).c_str()); + } + } +} + bool safety_setter_thread(std::vector pandas) { LOGD("Starting safety setter thread"); @@ -171,19 +200,7 @@ Panda *usb_connect(std::string serial="", uint32_t index=0) { std::call_once(connected_once, &Panda::set_usb_power_mode, panda, cereal::PeripheralState::UsbPowerMode::CDP); #endif - if (panda->has_rtc) { - setenv("TZ","UTC",1); - struct tm sys_time = util::get_time(); - struct tm rtc_time = panda->get_rtc(); - - if (!util::time_valid(sys_time) && util::time_valid(rtc_time)) { - LOGE("System time wrong, setting from RTC. System: %s RTC: %s", - get_time_str(sys_time).c_str(), get_time_str(rtc_time).c_str()); - const struct timeval tv = {mktime(&rtc_time), 0}; - settimeofday(&tv, 0); - } - } - + sync_time(panda.get(), SyncTimeDir::FROM_PANDA); return panda.release(); } @@ -516,21 +533,8 @@ void peripheral_control_thread(Panda *panda) { } // Write to rtc once per minute when no ignition present - if ((panda->has_rtc) && !ignition && (cnt % 120 == 1)) { - // Write time to RTC if it looks reasonable - setenv("TZ","UTC",1); - struct tm sys_time = util::get_time(); - - if (util::time_valid(sys_time)) { - struct tm rtc_time = panda->get_rtc(); - double seconds = difftime(mktime(&rtc_time), mktime(&sys_time)); - - if (std::abs(seconds) > 1.1) { - panda->set_rtc(sys_time); - LOGW("Updating panda RTC. dt = %.2f System: %s RTC: %s", - seconds, get_time_str(sys_time).c_str(), get_time_str(rtc_time).c_str()); - } - } + if (!ignition && (cnt % 120 == 1)) { + sync_time(panda, SyncTimeDir::TO_PANDA); } } }