Skip to content

Commit

Permalink
athena: use dongle ID on /persist/ when available (#33259)
Browse files Browse the repository at this point in the history
* athena: use dongle ID on /persist/ when available

* comment

* test

* cleanup
old-commit-hash: 91db49b
  • Loading branch information
adeebshihadeh authored Sep 1, 2024
1 parent 1876f80 commit 333c666
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 14 additions & 2 deletions system/athena/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,32 @@

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

IMEI = params.get("IMEI", encoding='utf8')
HardwareSerial = params.get("HardwareSerial", encoding='utf8')
dongle_id: str | None = params.get("DongleId", encoding='utf8')
needs_registration = None in (IMEI, HardwareSerial, dongle_id)
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()

needs_registration = None in (IMEI, HardwareSerial, dongle_id)
pubkey = Path(Paths.persist_root()+"/comma/id_rsa.pub")
if not pubkey.is_file():
dongle_id = UNREGISTERED_DONGLE_ID
Expand Down
16 changes: 10 additions & 6 deletions system/athena/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class TestRegistration:
def setup_method(self):
# clear params and setup key paths
self.params = Params()
self.params.clear_all()

persist_dir = Path(Paths.persist_root()) / "comma"
persist_dir.mkdir(parents=True, exist_ok=True)

self.priv_key = persist_dir / "id_rsa"
self.pub_key = persist_dir / "id_rsa.pub"
self.dongle_id = persist_dir / "dongle_id"

def _generate_keys(self):
self.pub_key.touch()
Expand All @@ -30,16 +30,20 @@ def _generate_keys(self):
f.write(k.publickey().export_key())

def test_valid_cache(self, mocker):
# if all params are written, return the cached dongle id
# if all params are written, return the cached dongle id.
# should work with a dongle ID on either /persist/ or normal params
self.params.put("IMEI", "imei")
self.params.put("HardwareSerial", "serial")
self._generate_keys()

m = mocker.patch("openpilot.system.athena.registration.api_get", autospec=True)
dongle = "DONGLE_ID_123"
self.params.put("DongleId", dongle)
assert register() == dongle
assert not m.called
m = mocker.patch("openpilot.system.athena.registration.api_get", autospec=True)
for persist, params in [(True, True), (True, False), (False, True)]:
self.params.put("DongleId", dongle if params else "")
with open(self.dongle_id, "w") as f:
f.write(dongle if persist else "")
assert register() == dongle
assert not m.called

def test_no_keys(self, mocker):
# missing pubkey
Expand Down

0 comments on commit 333c666

Please sign in to comment.