2022-01-06 17:11:32 -07:00
|
|
|
#!/usr/bin/env python3
|
2020-12-18 05:17:23 -07:00
|
|
|
import time
|
2020-01-17 12:03:22 -07:00
|
|
|
import json
|
2020-12-18 05:17:23 -07:00
|
|
|
import jwt
|
2021-12-03 23:58:34 -07:00
|
|
|
from pathlib import Path
|
2020-12-18 05:17:23 -07:00
|
|
|
|
2020-01-17 12:03:22 -07:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from common.api import api_get
|
|
|
|
from common.params import Params
|
2021-03-25 10:27:15 -06:00
|
|
|
from common.spinner import Spinner
|
2020-02-02 13:15:02 -07:00
|
|
|
from common.basedir import PERSIST
|
2021-05-11 04:18:45 -06:00
|
|
|
from selfdrive.controls.lib.alertmanager import set_offroad_alert
|
2022-01-29 19:10:41 -07:00
|
|
|
from selfdrive.hardware import HARDWARE, PC
|
2020-12-16 22:30:23 -07:00
|
|
|
from selfdrive.swaglog import cloudlog
|
2020-01-17 12:03:22 -07:00
|
|
|
|
2020-08-26 06:57:17 -06:00
|
|
|
|
2021-05-11 04:18:45 -06:00
|
|
|
UNREGISTERED_DONGLE_ID = "UnregisteredDevice"
|
|
|
|
|
|
|
|
|
2022-01-26 12:44:10 -07:00
|
|
|
def is_registered_device() -> bool:
|
|
|
|
dongle = Params().get("DongleId", encoding='utf-8')
|
|
|
|
return dongle not in (None, UNREGISTERED_DONGLE_ID)
|
|
|
|
|
|
|
|
|
2021-05-01 18:16:58 -06:00
|
|
|
def register(show_spinner=False) -> str:
|
2020-01-17 12:03:22 -07:00
|
|
|
params = Params()
|
2020-08-26 06:57:17 -06:00
|
|
|
params.put("SubscriberInfo", HARDWARE.get_subscriber_info())
|
2020-01-17 12:03:22 -07:00
|
|
|
|
2021-01-18 04:07:55 -07:00
|
|
|
IMEI = params.get("IMEI", encoding='utf8')
|
|
|
|
HardwareSerial = params.get("HardwareSerial", encoding='utf8')
|
2021-04-22 22:03:57 -06:00
|
|
|
dongle_id = params.get("DongleId", encoding='utf8')
|
|
|
|
needs_registration = None in (IMEI, HardwareSerial, dongle_id)
|
2020-12-18 05:17:23 -07:00
|
|
|
|
2021-12-03 23:58:34 -07:00
|
|
|
pubkey = Path(PERSIST+"/comma/id_rsa.pub")
|
|
|
|
if not pubkey.is_file():
|
|
|
|
dongle_id = UNREGISTERED_DONGLE_ID
|
|
|
|
cloudlog.warning(f"missing public key: {pubkey}")
|
|
|
|
elif needs_registration:
|
2021-03-25 10:27:15 -06:00
|
|
|
if show_spinner:
|
|
|
|
spinner = Spinner()
|
2020-12-18 05:17:23 -07:00
|
|
|
spinner.update("registering device")
|
2020-01-17 12:03:22 -07:00
|
|
|
|
2020-12-18 05:17:23 -07:00
|
|
|
# Create registration token, in the future, this key will make JWTs directly
|
2021-05-01 18:16:58 -06:00
|
|
|
with open(PERSIST+"/comma/id_rsa.pub") as f1, open(PERSIST+"/comma/id_rsa") as f2:
|
|
|
|
public_key = f1.read()
|
|
|
|
private_key = f2.read()
|
2020-01-17 12:03:22 -07:00
|
|
|
|
2020-12-18 05:17:23 -07:00
|
|
|
# Block until we get the imei
|
2021-07-22 00:30:02 -06:00
|
|
|
serial = HARDWARE.get_serial()
|
|
|
|
start_time = time.monotonic()
|
2020-12-18 05:17:23 -07:00
|
|
|
imei1, imei2 = 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)
|
2020-01-17 12:03:22 -07:00
|
|
|
|
2021-07-22 00:30:02 -06:00
|
|
|
if time.monotonic() - start_time > 60 and show_spinner:
|
|
|
|
spinner.update(f"registering device - serial: {serial}, IMEI: ({imei1}, {imei2})")
|
|
|
|
|
2021-01-18 04:07:55 -07:00
|
|
|
params.put("IMEI", imei1)
|
|
|
|
params.put("HardwareSerial", serial)
|
|
|
|
|
2021-04-24 12:01:58 -06:00
|
|
|
backoff = 0
|
2021-07-22 00:30:02 -06:00
|
|
|
start_time = time.monotonic()
|
2020-12-18 05:17:23 -07:00
|
|
|
while True:
|
|
|
|
try:
|
2021-04-24 12:01:58 -06:00
|
|
|
register_token = jwt.encode({'register': True, 'exp': datetime.utcnow() + timedelta(hours=1)}, private_key, algorithm='RS256')
|
2020-12-18 05:17:23 -07:00
|
|
|
cloudlog.info("getting pilotauth")
|
2021-08-03 07:33:41 -06:00
|
|
|
resp = api_get("v2/pilotauth/", method='POST', timeout=15,
|
2021-01-18 04:07:55 -07:00
|
|
|
imei=imei1, imei2=imei2, serial=serial, public_key=public_key, register_token=register_token)
|
2021-04-22 22:03:57 -06:00
|
|
|
|
2021-04-28 15:09:13 -06:00
|
|
|
if resp.status_code in (402, 403):
|
|
|
|
cloudlog.info(f"Unable to register device, got {resp.status_code}")
|
2021-05-11 04:18:45 -06:00
|
|
|
dongle_id = UNREGISTERED_DONGLE_ID
|
2021-04-22 22:03:57 -06:00
|
|
|
else:
|
|
|
|
dongleauth = json.loads(resp.text)
|
|
|
|
dongle_id = dongleauth["dongle_id"]
|
2020-12-18 05:17:23 -07:00
|
|
|
break
|
|
|
|
except Exception:
|
|
|
|
cloudlog.exception("failed to authenticate")
|
2021-04-24 12:01:58 -06:00
|
|
|
backoff = min(backoff + 1, 15)
|
|
|
|
time.sleep(backoff)
|
2020-01-17 12:03:22 -07:00
|
|
|
|
2021-07-22 00:30:02 -06:00
|
|
|
if time.monotonic() - start_time > 60 and show_spinner:
|
|
|
|
spinner.update(f"registering device - serial: {serial}, IMEI: ({imei1}, {imei2})")
|
|
|
|
|
2021-03-25 10:27:15 -06:00
|
|
|
if show_spinner:
|
|
|
|
spinner.close()
|
|
|
|
|
2021-04-28 15:09:13 -06:00
|
|
|
if dongle_id:
|
|
|
|
params.put("DongleId", dongle_id)
|
2022-01-29 19:10:41 -07:00
|
|
|
set_offroad_alert("Offroad_UnofficialHardware", (dongle_id == UNREGISTERED_DONGLE_ID) and not PC)
|
2020-12-18 05:17:23 -07:00
|
|
|
return dongle_id
|
2020-01-17 12:03:22 -07:00
|
|
|
|
2021-03-25 10:27:15 -06:00
|
|
|
|
2020-01-17 12:03:22 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
print(register())
|