set time with python lib (#157)

* set time with python lib

* more logging
This commit is contained in:
Adeeb Shihadeh
2023-07-09 17:46:47 -07:00
committed by GitHub
parent 86354b243b
commit f094909978
2 changed files with 38 additions and 15 deletions
+9
View File
@@ -147,6 +147,15 @@ COPY ./userspace/usr/share/fonts/* /usr/share/fonts/
COPY ./userspace/libs/* /usr/lib/aarch64-linux-gnu/
COPY ./userspace/libs32/* /usr/lib/arm-linux-gnueabihf/
# copy of panda python lib
RUN cd /tmp/ && \
git clone https://github.com/commaai/panda.git && \
cd panda && \
git fetch --all && \
git checkout 8ba568388176b8fc6443c124ad9ecd4e33174642 && \
mkdir /usr/comma/panda/ && \
mv __init__.py python/ /usr/comma/panda/
# kernel headers for the AGNOS kernel (built on device)
COPY ./userspace/files/linux-headers-4.9.103+_4.9.103+-1_arm64.deb /tmp/
RUN dpkg -i /tmp/linux-headers-4.9.103+_4.9.103+-1_arm64.deb
+29 -15
View File
@@ -1,28 +1,42 @@
#!/usr/bin/env python3
import datetime
import os
import struct
import usb1
import sys
from pathlib import Path
REQUEST_IN = usb1.ENDPOINT_IN | usb1.TYPE_VENDOR | usb1.RECIPIENT_DEVICE
here = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, here)
from panda import Panda
# Systemd pushes the system time to the systemd build time if no time is set
MIN_DATE = datetime.datetime.fromtimestamp(Path("/lib/systemd/systemd").stat().st_mtime)
MIN_DATE += datetime.timedelta(days=1)
if __name__ == "__main__":
ctx = usb1.USBContext()
dev = ctx.openByVendorIDAndProductID(0xbbaa, 0xddcc)
if dev is None:
print("No panda found")
sys_time = datetime.datetime.today()
print(f"System time: {sys_time}")
if sys_time > MIN_DATE:
print("System time valid")
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])
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}'")
ps = Panda.list()
if len(ps) == 0:
print("Failed to set time, no pandas found")
exit()
for s in ps:
with Panda(serial=s) as p:
if not p.is_internal():
continue
# Set system time from panda RTC time
panda_time = p.get_datetime()
print(f"panda time: {panda_time}")
if panda_time > MIN_DATE:
print(f"adjusting time from '{sys_time}' to '{panda_time}'")
os.system(f"TZ=UTC date -s '{panda_time}'")
else:
print("panda time invalid")
break
else:
print("No internal pandas found")