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 <device@comma.ai>
albatross
Adeeb Shihadeh 2021-04-14 12:04:02 -07:00 committed by GitHub
parent 529cf08015
commit 1adf34f033
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 14 deletions

View File

@ -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:
#

View File

@ -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

View File

@ -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)

View File

@ -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()