mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-06-27 22:32:07 +08:00
08e85808c5
# Conflicts: # .github/workflows/ci_weekly_run.yaml # .github/workflows/raylib_ui_preview.yaml # .github/workflows/tests.yaml # .gitmodules # README.md # SConstruct # common/api.py # common/params_keys.h # docs/CARS.md # msgq_repo # opendbc_repo # panda # selfdrive/car/tests/test_car_interfaces.py # selfdrive/controls/controlsd.py # selfdrive/controls/lib/latcontrol.py # selfdrive/controls/lib/latcontrol_angle.py # selfdrive/controls/lib/latcontrol_pid.py # selfdrive/controls/lib/latcontrol_torque.py # selfdrive/controls/tests/test_latcontrol.py # selfdrive/monitoring/helpers.py # selfdrive/ui/SConscript # selfdrive/ui/main.cc # selfdrive/ui/qt/body.h # selfdrive/ui/qt/home.cc # selfdrive/ui/qt/home.h # selfdrive/ui/qt/network/networking.cc # selfdrive/ui/qt/network/networking.h # selfdrive/ui/qt/network/wifi_manager.cc # selfdrive/ui/qt/offroad/developer_panel.cc # selfdrive/ui/qt/offroad/developer_panel.h # selfdrive/ui/qt/offroad/experimental_mode.cc # selfdrive/ui/qt/offroad/firehose.cc # selfdrive/ui/qt/offroad/firehose.h # selfdrive/ui/qt/offroad/onboarding.cc # selfdrive/ui/qt/offroad/onboarding.h # selfdrive/ui/qt/offroad/settings.cc # selfdrive/ui/qt/offroad/settings.h # selfdrive/ui/qt/offroad/software_settings.cc # selfdrive/ui/qt/onroad/alerts.cc # selfdrive/ui/qt/onroad/annotated_camera.h # selfdrive/ui/qt/onroad/buttons.cc # selfdrive/ui/qt/onroad/buttons.h # selfdrive/ui/qt/onroad/driver_monitoring.cc # selfdrive/ui/qt/onroad/hud.cc # selfdrive/ui/qt/onroad/hud.h # selfdrive/ui/qt/onroad/model.cc # selfdrive/ui/qt/onroad/model.h # selfdrive/ui/qt/onroad/onroad_home.cc # selfdrive/ui/qt/onroad/onroad_home.h # selfdrive/ui/qt/request_repeater.h # selfdrive/ui/qt/sidebar.cc # selfdrive/ui/qt/sidebar.h # selfdrive/ui/qt/util.cc # selfdrive/ui/qt/widgets/cameraview.h # selfdrive/ui/qt/widgets/controls.cc # selfdrive/ui/qt/widgets/controls.h # selfdrive/ui/qt/widgets/input.cc # selfdrive/ui/qt/widgets/input.h # selfdrive/ui/qt/widgets/prime.cc # selfdrive/ui/qt/widgets/prime.h # selfdrive/ui/qt/widgets/ssh_keys.h # selfdrive/ui/qt/widgets/toggle.h # selfdrive/ui/qt/widgets/wifi.cc # selfdrive/ui/qt/widgets/wifi.h # selfdrive/ui/qt/window.cc # selfdrive/ui/qt/window.h # selfdrive/ui/tests/cycle_offroad_alerts.py # selfdrive/ui/tests/test_ui/run.py # selfdrive/ui/translations/main_ar.ts # selfdrive/ui/translations/main_de.ts # selfdrive/ui/translations/main_es.ts # selfdrive/ui/translations/main_fr.ts # selfdrive/ui/translations/main_ja.ts # selfdrive/ui/translations/main_ko.ts # selfdrive/ui/translations/main_nl.ts # selfdrive/ui/translations/main_pl.ts # selfdrive/ui/translations/main_pt-BR.ts # selfdrive/ui/translations/main_th.ts # selfdrive/ui/translations/main_tr.ts # selfdrive/ui/translations/main_zh-CHS.ts # selfdrive/ui/translations/main_zh-CHT.ts # selfdrive/ui/ui.cc # selfdrive/ui/ui.h # system/manager/build.py # system/version.py
104 lines
3.5 KiB
Python
Executable File
104 lines
3.5 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, get_key_pair
|
|
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")
|
|
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()
|
|
|
|
dongle_id: str | None = params.get("DongleId")
|
|
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()
|
|
|
|
# Create registration token, in the future, this key will make JWTs directly
|
|
jwt_algo, private_key, public_key = get_key_pair()
|
|
|
|
if not public_key:
|
|
dongle_id = UNREGISTERED_DONGLE_ID
|
|
cloudlog.warning("missing public key")
|
|
elif dongle_id is None:
|
|
if show_spinner:
|
|
spinner = Spinner()
|
|
spinner.update("registering device")
|
|
|
|
# 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=jwt_algo)
|
|
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 > 60 and show_spinner:
|
|
spinner.update(f"registering device - serial: {serial}, IMEI: ({imei1}, {imei2})")
|
|
return UNREGISTERED_DONGLE_ID # hotfix to prevent an infinite wait for registration
|
|
|
|
if show_spinner:
|
|
spinner.close()
|
|
|
|
if dongle_id:
|
|
params.put("DongleId", dongle_id)
|
|
set_offroad_alert("Offroad_UnregisteredHardware", (dongle_id == UNREGISTERED_DONGLE_ID) and not PC)
|
|
return dongle_id
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(register())
|