77a8919349
* UV+DTR model * DTR model.. again. * fix naviGPS * fix radar... * fix.. * test * fix.. * carrot serv * fix.. * fix.. fleet * fix.. radar * fix atc * Steam Powered model.. * fix.. radarLatFactor range.. 200->500 * fix.. dbc.. * side * SP v2 * brake light * fix brakelight * fix.. * add datetime... * fix.. * fix.. * fix.. * fix.. * blind spot * fix tz * fix.. * ff * radarLatFactor * fix.. bsd * Revert "fix.. bsd" This reverts commit 1d0d1434470e1b92c65eaffaeb8dd7cd779f85ee. * fix.. bsd side.. * test * fix.. e2e conditions * Revert "test" This reverts commit 0ce791dbd66c17260366ed1a4df2626c602dbb7d. * TR16 * fix cut-in detect threshold 3.4 -> 2.6 * fix.. jerk_l limit 5->10 * fix.. * fix.. gm * fix.. OPTIMA_H mass * fix.. radar.. * fix radar.. * fix.. * Radar... * fix.. * fix.. * fix.. * fix.. radartrack 3 * fix.. * fix.. * fix.. * merge.. * fix.. canfd * fix.. * fix.. * fix.. * fix.. radard * new cut_in * Revert "new cut_in" This reverts commit b9b6e9b33318fe1ce7d626468139b17848efcdcd. * fix.. * new cut_in detect... * fix.. disp.. * fix.. * fix.. * fix.. center radar.. * fix.. radar y_sane.. * fix.. * fix.. * hkg jerk 10 -> 5 * fix.. * fix.. * fix.. radar dbc.. * fix.. * fix.. jLead filter.. * test new radar interface.. * fix.. * fix.. * test time... * Revert "test time..." This reverts commit 63e9187736985c4dc4b4f3736674ba7cda6adc3f. * fix radar.. * fix.. * FireHose model.. * tinygrad * Update interface.py * fix.. * fix.. nff toyota corolla_tss2 * fix.. * fix.. * fix.. radar * fix.. * fix.. radar, y_gate * fix.. radar.. * fix.. for clone.. * scc radar enable at low speed.. * fix.. settings.. * fix. * fix.. * fix.. radarTimeStep. * TR16 model again.. * RELEASE.md * fix cut-in detection... * fix.. registeration timeout 15sec.. * fix.. * fix.. radar processing. * fix.. * fix.. * fix.. * fix.. * fix.. * fix..
113 lines
3.9 KiB
Python
Executable File
113 lines
3.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import time
|
|
import json
|
|
import jwt
|
|
from pathlib import Path
|
|
|
|
from datetime import datetime, timedelta, UTC
|
|
from openpilot.common.api import api_get
|
|
from openpilot.common.params import Params
|
|
from openpilot.common.spinner import Spinner
|
|
from openpilot.selfdrive.selfdrived.alertmanager import set_offroad_alert
|
|
from openpilot.system.hardware import HARDWARE, PC
|
|
from openpilot.system.hardware.hw import Paths
|
|
from openpilot.common.swaglog import cloudlog
|
|
|
|
|
|
UNREGISTERED_DONGLE_ID = "UnregisteredDevice"
|
|
|
|
def is_registered_device() -> bool:
|
|
dongle = Params().get("DongleId", encoding='utf-8')
|
|
return dongle not in (None, UNREGISTERED_DONGLE_ID)
|
|
|
|
|
|
def register(show_spinner=False) -> str | None:
|
|
"""
|
|
All devices built since March 2024 come with all
|
|
info stored in /persist/. This is kept around
|
|
only for devices built before then.
|
|
|
|
With a backend update to take serial number instead
|
|
of dongle ID to some endpoints, this can be removed
|
|
entirely.
|
|
"""
|
|
params = Params()
|
|
|
|
|
|
#return UNREGISTERED_DONGLE_ID # for c3lite, clone
|
|
dongle_id: str | None = params.get("DongleId", encoding='utf8')
|
|
if dongle_id is None and Path(Paths.persist_root()+"/comma/dongle_id").is_file():
|
|
# not all devices will have this; added early in comma 3X production (2/28/24)
|
|
with open(Paths.persist_root()+"/comma/dongle_id") as f:
|
|
dongle_id = f.read().strip()
|
|
|
|
pubkey = Path(Paths.persist_root()+"/comma/id_rsa.pub")
|
|
if not pubkey.is_file():
|
|
dongle_id = UNREGISTERED_DONGLE_ID
|
|
cloudlog.warning(f"missing public key: {pubkey}")
|
|
elif dongle_id is None:
|
|
if show_spinner:
|
|
spinner = Spinner()
|
|
spinner.update("registering device")
|
|
|
|
# Create registration token, in the future, this key will make JWTs directly
|
|
with open(Paths.persist_root()+"/comma/id_rsa.pub") as f1, open(Paths.persist_root()+"/comma/id_rsa") as f2:
|
|
public_key = f1.read()
|
|
private_key = f2.read()
|
|
|
|
# Block until we get the imei
|
|
serial = HARDWARE.get_serial()
|
|
start_time = time.monotonic()
|
|
imei1: str | None = None
|
|
imei2: str | None = None
|
|
while imei1 is None and imei2 is None:
|
|
try:
|
|
imei1, imei2 = HARDWARE.get_imei(0), HARDWARE.get_imei(1)
|
|
except Exception:
|
|
cloudlog.exception("Error getting imei, trying again...")
|
|
time.sleep(1)
|
|
|
|
if time.monotonic() - start_time > 60 and show_spinner:
|
|
spinner.update(f"registering device - serial: {serial}, IMEI: ({imei1}, {imei2})")
|
|
|
|
backoff = 0
|
|
start_time = time.monotonic()
|
|
while True:
|
|
try:
|
|
register_token = jwt.encode({'register': True, 'exp': datetime.now(UTC).replace(tzinfo=None) + timedelta(hours=1)}, private_key, algorithm='RS256')
|
|
cloudlog.info("getting pilotauth")
|
|
resp = api_get("v2/pilotauth/", method='POST', timeout=15,
|
|
imei=imei1, imei2=imei2, serial=serial, public_key=public_key, register_token=register_token)
|
|
|
|
if resp.status_code in (402, 403):
|
|
cloudlog.info(f"Unable to register device, got {resp.status_code}")
|
|
dongle_id = UNREGISTERED_DONGLE_ID
|
|
else:
|
|
dongleauth = json.loads(resp.text)
|
|
dongle_id = dongleauth["dongle_id"]
|
|
break
|
|
except Exception:
|
|
cloudlog.exception("failed to authenticate")
|
|
backoff = min(backoff + 1, 15)
|
|
time.sleep(backoff)
|
|
|
|
if time.monotonic() - start_time > 14:
|
|
cloudlog.error("pilotauth timed out; continuing as UNREGISTERED")
|
|
dongle_id = UNREGISTERED_DONGLE_ID
|
|
break
|
|
|
|
if time.monotonic() - start_time > 60 and show_spinner:
|
|
spinner.update(f"registering device - serial: {serial}, IMEI: ({imei1}, {imei2})")
|
|
|
|
if show_spinner:
|
|
spinner.close()
|
|
|
|
if dongle_id:
|
|
params.put("DongleId", dongle_id)
|
|
set_offroad_alert("Offroad_UnofficialHardware", (dongle_id == UNREGISTERED_DONGLE_ID) and not PC)
|
|
return dongle_id
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(register())
|