mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-06-24 20:52:08 +08:00
boardd: new function sync_time (#23033)
* sync_time
* add direction
* Update selfdrive/boardd/boardd.cc
Co-authored-by: Willem Melching <willem.melching@gmail.com>
* enum class
* rename
* caps
* lambda get_time_str
* Revert "lambda get_time_str"
This reverts commit 5eb6e19c5130b28963e9555f6c73835ac30d817d.
* static
Co-authored-by: Willem Melching <willem.melching@gmail.com>
old-commit-hash: 35c0319f6c
This commit is contained in:
+33
-29
@@ -62,7 +62,7 @@ std::atomic<bool> 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<Panda *> &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<Panda *> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user