Files
sunnypilot/openpilot/system/ui/lib/cellular_manager.py
T
Trey Moen 25d9d41c90 esim: comma four profile management UI (#37844)
* esim: MICI eSIM profile management UI

* esim: align rename button position regardless of delete button

* esim: skip profile UI when SIM is not an eUICC

Probe is_euicc() on the first profile poll and cache it; non-eUICC SIMs
avoid list_profiles/process_notifications (which hang on a plain SIM) and
the eSIM button shows ICCID/MCC-MNC metadata instead of opening the
profile management screen.

* esim: satisfy ruff E731 in action_pressed helper

* esim: get modem info from modem.py state instead of shelling out

* esim: simplify switch lifecycle, drop active flag and settle window

* esim: only show 'switching...' on the target profile

* esim: read cell strength directly from HARDWARE

* esim: hide checkmark and dim cell icon during switch; keep rename available

* esim: disable active profile button (rename still clickable as overlay)

* esim: stop rename/delete buttons and labels from flashing during operations

* esim: show 'comma prime' on network button for comma profile

* esim: move display_name and is_comma onto Profile dataclass

* esim: anchor rename button to rightmost slot to prevent shift

* esim: include iccid prefix check in Profile.is_comma

* esim: drop process_notifications from cellular manager

* esim: disable network button when SIM isn't an eUICC

* esim: rename ESim* classes to Esim*

* esim: sort imports

* esim: default to 'loading...' instead of 'no active profile'

* esim: rename PROFILE_POLL_INTERVAL to PROFILE_POLL_INTERVAL_S

* esim: slim EsimNetworkButton

* esim: use DEFAULT_TEXT_COLOR; load delete dialog texture in __init__

* esim: revert cell-icon index trick, name each NetworkStrength explicitly

* esim: tighten delete/rename spacing so 'switch' label fits on one line

* esim: shrink delete/rename buttons by 25%

* esim: keep rename button at full size, only delete shrinks

* Revert "disable modem.py for now"

This reverts commit 1eeba86ec1.

* Revert "modem.py is disabled"

This reverts commit d238a1ccc4.

* lpa: inline comma iccid prefix

* ui/cellular_manager: clear switching state when LPA returns

* ui/cellular_manager: lock callback queue, drop poll log noise

* esim: sort NetworkStrength/NetworkType imports

* esim: drop switching_iccid concept

* esim: show 'switching...' on the clicked profile button

* esim: optimistic switch, skip post-switch list_profiles to avoid flicker

* esim: only style profile button from local op flags, not global busy

* esim: remove deleting/switching state, collapse profile button branches

* esim: show GSM settings on full prime when non-comma profile is active

* esim: expose CellularManager.active_profile, dedup callers

* esim: apply comma prime defaults on switch (roaming, metered, no APN)

* esim: restore re_sort on show_event to avoid first-frame jank

* lpa: apply comma prime defaults inside TiciLPA.switch_profile

* lpa: lazy-import Params to break circular import

* lpa: treat +CME ERROR 13 (SIM failure) as non-eUICC in is_euicc

* esim: show 'obtaining IP...' on non-eUICC path while connecting

* Revert "lpa: treat +CME ERROR 13 (SIM failure) as non-eUICC in is_euicc"

This reverts commit 475ca448ea914ff8f9892b4cbf4525d961d7618b.

* esim: poll profiles every 5s

* lpa: tighten Profile.is_comma to require both Webbing provider and comma BIN

* lpa: fall back to '<unnamed>' instead of iccid prefix in display_name

* esim: re-probe is_euicc each poll for runtime SIM swaps

* esim_ui: rename EsimUIMici to EsimUI

* esim: simplify cellular manager and profile UI

- optimistic profile switch at click time so the active button no longer bounces back
- unify LPA worker threads, refresh_profiles resets the poll timer
- deterministic profile ordering on show
- reuse wifi ForgetButton for delete, drop DeleteButton
- construct CellularManager inside NetworkLayoutMici
- drop comma prime param defaults from LPA.switch_profile (moved to a separate PR)

* esim: drop eUICC state change log

* ui: gate cellular settings on prime subscription

* ui: disable eSIM management for full prime

* ui: keep eSIM management disabled for full prime

* ui: unify eSIM profile action buttons

* ui: tighten eSIM profile action spacing

* ui: place rename before delete in eSIM actions

* esim: restrict individual profiles and process switch notifications

* esim: defer poll after operations and confirm eUICC loss before clearing profiles

* ui: allow cellular settings for unregistered and unpaired devices

* ui: disable all profile switching for full prime

* ui: simplify cellular access to not full prime

* ui: make profile poll interval a CellularManager constant

* ui: read modem state on the profile poll cadence

* ui: scope eSIM profile colors to their class

* ui: inline modem state read in cellular polling

* ui: rely on hardware modem state fallback
2026-09-07 21:55:28 -07:00

152 lines
4.9 KiB
Python

import time
import threading
from collections.abc import Callable
from dataclasses import replace
from openpilot.common.hardware import HARDWARE
from openpilot.common.swaglog import cloudlog
from openpilot.common.esim.base import LPABase, Profile
from openpilot.common.esim.esim import execute_and_process_notifications
class CellularManager:
PROFILE_POLL_INTERVAL_S = 5.0
def __init__(self):
self._lpa: LPABase | None = None
self._profiles: list[Profile] = []
self._busy: bool = False
# re-probed every poll; SIM may be swapped at runtime on tray-accessible devices
self._is_euicc: bool | None = None
self._modem_state: dict = {}
self._lock = threading.Lock()
self._callback_lock = threading.Lock()
self._callback_queue: list[Callable] = []
self.on_profiles_updated: Callable[[], None] | None = None
self.on_operation_error: Callable[[str], None] | None = None
self._last_profile_poll: float = 0.0
self._polling: bool = False
@property
def modem_state(self) -> dict:
return self._modem_state
def process_callbacks(self):
with self._callback_lock:
to_run, self._callback_queue = self._callback_queue, []
for cb in to_run:
cb()
if not self._busy and not self._polling and time.monotonic() - self._last_profile_poll >= self.PROFILE_POLL_INTERVAL_S:
self._last_profile_poll = time.monotonic()
self._modem_state = HARDWARE.get_modem_state()
self._poll_profiles()
@property
def profiles(self) -> list[Profile]:
return self._profiles
@property
def active_profile(self) -> Profile | None:
return next((p for p in self._profiles if p.enabled), None)
@property
def busy(self) -> bool:
return self._busy
@property
def is_euicc(self) -> bool | None:
return self._is_euicc
def _ensure_lpa(self) -> LPABase:
if self._lpa is None:
self._lpa = HARDWARE.get_sim_lpa()
return self._lpa
def _enqueue(self, cb: Callable):
with self._callback_lock:
self._callback_queue.append(cb)
def _stop_polling(self):
self._polling = False
def _set_profiles(self, profiles: list[Profile]):
self._profiles = profiles
if self.on_profiles_updated:
self.on_profiles_updated()
def _finish(self, profiles: list[Profile] | None = None, error: str | None = None):
self._busy = False
# defer the next poll a full interval; the eUICC can briefly report stale state after an operation
self._last_profile_poll = time.monotonic()
if profiles is not None:
self._set_profiles(profiles)
if error is not None:
self.refresh_profiles()
if self.on_operation_error:
self.on_operation_error(error)
def _run_operation(self, fn: Callable[[LPABase], None], error_msg: str, relist: bool = True):
self._busy = True
def worker():
try:
with self._lock:
lpa = self._ensure_lpa()
fn(lpa)
profiles = lpa.list_profiles() if relist else None
self._enqueue(lambda: self._finish(profiles=profiles))
except Exception as e:
cloudlog.exception(error_msg)
err = str(e)
self._enqueue(lambda: self._finish(error=err))
threading.Thread(target=worker, daemon=True).start()
def refresh_profiles(self):
# next process_callbacks tick polls, respecting busy/polling guards
self._last_profile_poll = 0.0
def _poll_profiles(self):
self._polling = True
def worker():
try:
with self._lock:
lpa = self._ensure_lpa()
is_euicc = lpa.is_euicc()
profiles = lpa.list_profiles() if is_euicc else []
self._enqueue(lambda: self._finish_poll(is_euicc, profiles))
except Exception:
cloudlog.exception("Failed to poll eSIM profiles")
self._enqueue(self._stop_polling)
threading.Thread(target=worker, daemon=True).start()
def _finish_poll(self, is_euicc: bool, profiles: list[Profile]):
self._polling = False
if self._busy:
return
if not is_euicc and self._is_euicc:
# is_euicc() is False on any AT error (e.g. SIM busy during a profile refresh); confirm on the next poll
self._is_euicc = None
return
self._is_euicc = is_euicc
self._set_profiles(profiles)
def switch_profile(self, iccid: str):
def switch(lpa: LPABase):
execute_and_process_notifications(lpa, lambda: lpa.switch_profile(iccid))
# optimistic: list_profiles() can briefly return stale enabled state after a switch
self._set_profiles([replace(p, enabled=(p.iccid == iccid)) for p in self._profiles])
self._run_operation(switch, "Failed to switch eSIM profile", relist=False)
def delete_profile(self, iccid: str):
self._run_operation(lambda lpa: lpa.delete_profile(iccid), "Failed to delete eSIM profile")
def nickname_profile(self, iccid: str, nickname: str):
self._run_operation(lambda lpa: lpa.nickname_profile(iccid, nickname), "Failed to update eSIM profile nickname")