From 1adf34f033bc1efb592383ebb2dbaef909bac60b Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Wed, 14 Apr 2021 12:04:02 -0700 Subject: [PATCH] update time from RTC on manager init (#20678) * set time on manager init * earlier * handle exception * logging * add to release files * run in launch script too Co-authored-by: Comma Device --- launch_chffrplus.sh | 3 +++ release/files_common | 1 + selfdrive/boardd/set_time.py | 41 ++++++++++++++++++++++++------------ selfdrive/manager/manager.py | 5 +++++ 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/launch_chffrplus.sh b/launch_chffrplus.sh index c9605cba..3f9ce997 100755 --- a/launch_chffrplus.sh +++ b/launch_chffrplus.sh @@ -165,6 +165,9 @@ function launch { # Remove orphaned git lock if it exists on boot [ -f "$DIR/.git/index.lock" ] && rm -f $DIR/.git/index.lock + # Pull time from panda + $DIR/selfdrive/boardd/set_time.py + # Check to see if there's a valid overlay-based update available. Conditions # are as follows: # diff --git a/release/files_common b/release/files_common index a9bf88bd..d5b2be97 100644 --- a/release/files_common +++ b/release/files_common @@ -89,6 +89,7 @@ selfdrive/boardd/panda.cc selfdrive/boardd/panda.h selfdrive/boardd/pigeon.cc selfdrive/boardd/pigeon.h +selfdrive/boardd/set_time.py selfdrive/car/__init__.py selfdrive/car/car_helpers.py diff --git a/selfdrive/boardd/set_time.py b/selfdrive/boardd/set_time.py index b5a8f269..7d17be4d 100755 --- a/selfdrive/boardd/set_time.py +++ b/selfdrive/boardd/set_time.py @@ -7,18 +7,31 @@ import usb1 REQUEST_IN = usb1.ENDPOINT_IN | usb1.TYPE_VENDOR | usb1.RECIPIENT_DEVICE MIN_DATE = datetime.datetime(year=2021, month=4, day=1) -if __name__ == "__main__": - ctx = usb1.USBContext() - dev = ctx.openByVendorIDAndProductID(0xbbaa, 0xddcc) - if dev is None: - print("No panda found") - exit() - - # Set system time from panda RTC time - dat = dev.controlRead(REQUEST_IN, 0xa0, 0, 0, 8) - a = struct.unpack("HBBBBBB", dat) - panda_time = datetime.datetime(a[0], a[1], a[2], a[4], a[5], a[6]) +def set_time(logger): sys_time = datetime.datetime.today() - if panda_time > MIN_DATE and sys_time < MIN_DATE: - print(f"adjusting time from '{sys_time}' to '{panda_time}'") - os.system(f"TZ=UTC date -s '{panda_time}'") + if sys_time > MIN_DATE: + logger.info("System time valid") + return + + try: + ctx = usb1.USBContext() + dev = ctx.openByVendorIDAndProductID(0xbbaa, 0xddcc) + if dev is None: + logger.info("No panda found") + return + + # Set system time from panda RTC time + dat = dev.controlRead(REQUEST_IN, 0xa0, 0, 0, 8) + a = struct.unpack("HBBBBBB", dat) + panda_time = datetime.datetime(a[0], a[1], a[2], a[4], a[5], a[6]) + if panda_time > MIN_DATE: + logger.info(f"adjusting time from '{sys_time}' to '{panda_time}'") + os.system(f"TZ=UTC date -s '{panda_time}'") + except Exception: + logger.warn("Failed to fetch time from panda") + +if __name__ == "__main__": + import logging + logging.basicConfig(level=logging.INFO) + + set_time(logging) diff --git a/selfdrive/manager/manager.py b/selfdrive/manager/manager.py index 84c3ae80..46824fba 100755 --- a/selfdrive/manager/manager.py +++ b/selfdrive/manager/manager.py @@ -11,6 +11,7 @@ import selfdrive.crash as crash from common.basedir import BASEDIR from common.params import Params from common.text_window import TextWindow +from selfdrive.boardd.set_time import set_time from selfdrive.hardware import HARDWARE from selfdrive.manager.helpers import unblock_stdout from selfdrive.manager.process import ensure_running @@ -21,6 +22,10 @@ from selfdrive.version import dirty, version def manager_init(): + + # update system time from panda + set_time(cloudlog) + params = Params() params.manager_start()