mirror of
https://github.com/infiniteCable2/openpilot.git
synced 2026-08-05 08:16:03 +08:00
14318d2f09
# Conflicts: # .github/labeler.yaml # .github/workflows/auto_pr_review.yaml # .github/workflows/docs.yaml # .github/workflows/release.yaml # .github/workflows/repo-maintenance.yaml # .github/workflows/tests.yaml # .github/workflows/ui_preview.yaml # README.md # SConstruct # conftest.py # docs/CARS.md # msgq_repo # opendbc_repo # openpilot/cereal/messaging/tests/validate_sp_cereal_upstream.py # openpilot/common/api.py # openpilot/common/hardware/hw.py # openpilot/common/version.py # openpilot/selfdrive/assets/fonts/Audiowide-Regular.ttf # openpilot/selfdrive/assets/sounds/prompt_single_high.wav # openpilot/selfdrive/assets/sounds/prompt_single_low.wav # openpilot/selfdrive/car/card.py # openpilot/selfdrive/car/helpers.py # openpilot/selfdrive/car/tests/test_car_interfaces.py # openpilot/selfdrive/car/tests/test_cruise_speed.py # openpilot/selfdrive/controls/lib/desire_helper.py # openpilot/selfdrive/controls/lib/longcontrol.py # openpilot/selfdrive/controls/plannerd.py # openpilot/selfdrive/controls/radard.py # openpilot/selfdrive/controls/tests/test_longcontrol.py # openpilot/selfdrive/modeld/compile_warp.py # openpilot/selfdrive/modeld/modeld.py # openpilot/selfdrive/monitoring/policy.py # openpilot/selfdrive/monitoring/test_monitoring.py # openpilot/selfdrive/selfdrived/events.py # openpilot/selfdrive/selfdrived/selfdrived.py # openpilot/selfdrive/ui/layouts/onboarding.py # openpilot/selfdrive/ui/mici/layouts/onboarding.py # openpilot/selfdrive/ui/soundd.py # openpilot/selfdrive/ui/tests/diff/replay.py # openpilot/selfdrive/ui/translations/app.pot # openpilot/system/athena/athenad.py # openpilot/system/athena/manage_athenad.py # openpilot/system/hardware/hardwared.py # openpilot/system/loggerd/config.py # openpilot/system/manager/github_runner.sh # openpilot/system/manager/manager.py # openpilot/system/updated/updated.py # panda # pyproject.toml # scripts/lint/lint.sh # system/manager/process_config.py # system/statsd.py # tinygrad_repo # tools/release/build_release.sh # tools/release/build_stripped.sh # uv.lock
106 lines
3.6 KiB
Python
Executable File
106 lines
3.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import time
|
|
import json
|
|
import jwt
|
|
from typing import cast
|
|
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.common.hardware import HARDWARE, PC
|
|
from openpilot.common.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()
|
|
imei: str | None = None
|
|
while imei is None:
|
|
try:
|
|
imei = HARDWARE.get_imei()
|
|
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: {imei}")
|
|
|
|
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)},
|
|
cast(str, private_key), algorithm=jwt_algo)
|
|
cloudlog.info("getting pilotauth")
|
|
cloudlog.info("getting pilotauth")
|
|
resp = api_get("v2/pilotauth/", method='POST', timeout=15,
|
|
imei=imei, 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: {imei}")
|
|
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, block=True)
|
|
set_offroad_alert("Offroad_UnregisteredHardware", (dongle_id == UNREGISTERED_DONGLE_ID) and not PC)
|
|
return dongle_id
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(register())
|