diff --git a/openpilot/common/hardware/comma/modem.py b/openpilot/common/hardware/comma/modem.py index 6f22770831..210bdf92a2 100755 --- a/openpilot/common/hardware/comma/modem.py +++ b/openpilot/common/hardware/comma/modem.py @@ -19,6 +19,7 @@ from ipaddress import IPv4Address, AddressValueError from enum import Enum from openpilot.common.esim.base import WEBBING_ICCID_PREFIX +from openpilot.common.hardware.comma.hardware import get_device_type from openpilot.common.swaglog import cloudlog logging.basicConfig( @@ -229,6 +230,7 @@ class Modem: def __init__(self): self._ppp = PPPSession() self._sim_change = False + self._modem_restarting = False self._apn = "" # blank = network-provided via PCO self._roaming_allowed = True self.running = True @@ -330,8 +332,7 @@ class Modem: # clear initial EPS bearer APN (some carriers reject the default) 'AT+CGDCONT=0,"IP",""', - # SIM hot swap - 'AT+QSIMDET=1,0', + # SIM hot swap (detection itself is set per device in _do_initializing) 'AT+QSIMSTAT=1', # configure modem as data-centric @@ -377,6 +378,7 @@ class Modem: def _do_initializing(self): if not glob.glob("/dev/ttyUSB*"): + self._modem_restarting = False # off USB, so a requested restart has happened return State.INITIALIZING logging.info("port found, initializing") self._ppp.kill() @@ -389,6 +391,16 @@ class Modem: cloudlog.error("modem: AT channel initialization failed, retrying") return State.INITIALIZING + # tici's SIM detect pin reads "removed"; the modem stores this setting and applies it only after a restart + want = {"tizi": "1,0", "tici": "0,0"}.get(get_device_type()) + if want and (have := self._atv("AT+QSIMDET?", "+QSIMDET:")) and have != want: + cloudlog.warning(f"modem: SIM detection {have} -> {want}, restarting modem") + self._at(f"AT+QSIMDET={want}") + self._modem_restarting = True + if self._modem_restarting: + self._at("AT+CFUN=1,1") # resent each pass until the modem drops off USB: _at skips silently while LPA holds the port + return State.INITIALIZING + identity = self._read_identity() if not identity["iccid"] or not identity["imei"]: cloudlog.error("modem: identity read incomplete: imei_present=%s iccid_present=%s, retrying", diff --git a/openpilot/common/hardware/comma/tests/test_modem_port_recovery.py b/openpilot/common/hardware/comma/tests/test_modem_port_recovery.py index 70d664d57a..131b6deddf 100644 --- a/openpilot/common/hardware/comma/tests/test_modem_port_recovery.py +++ b/openpilot/common/hardware/comma/tests/test_modem_port_recovery.py @@ -1,8 +1,52 @@ +from contextlib import contextmanager from unittest import mock +import pytest + from openpilot.common.hardware.comma import modem as m +@contextmanager +def _at_channel_ready(modem, device, have): + at = mock.Mock(side_effect=lambda cmd: [f"+QSIMDET: {have}"] if cmd == "AT+QSIMDET?" else []) + with mock.patch.object(m.glob, 'glob', return_value=['/dev/ttyUSB2', '/dev/ttyUSB3']) as ttys, \ + mock.patch.object(m, 'get_device_type', return_value=device), \ + mock.patch.object(modem, '_ppp'), mock.patch.object(modem, '_sync_ports', return_value=True), \ + mock.patch.object(modem, '_init_at_channel', return_value=True), mock.patch.object(modem, '_at', at), \ + mock.patch.object(modem, '_read_identity', return_value={"imei": "", "iccid": ""}) as identity: + yield at, ttys, identity + + +@pytest.mark.parametrize("device, have, sent", [ + ("tici", "1,0", ["AT+QSIMDET?", "AT+QSIMDET=0,0", "AT+CFUN=1,1"]), + ("tici", "0,0", ["AT+QSIMDET?"]), + ("tizi", "0,0", ["AT+QSIMDET?", "AT+QSIMDET=1,0", "AT+CFUN=1,1"]), + ("tizi", "1,0", ["AT+QSIMDET?"]), + ("mici", "1,0", []), +]) +def test_sim_detection_is_set_per_device_before_identity(device, have, sent): + modem = m.Modem() + with _at_channel_ready(modem, device, have) as (at, _, identity): + assert modem._do_initializing() == m.State.INITIALIZING + assert [c.args[0] for c in at.call_args_list] == sent + assert identity.called != ("AT+CFUN=1,1" in sent) + + +def test_lost_restart_is_resent_until_modem_drops_off_usb(): + modem = m.Modem() + modem._modem_restarting = True # QSIMDET already written, CFUN skipped while LPA held the port + with _at_channel_ready(modem, "tici", "0,0") as (at, ttys, identity): + modem._do_initializing() + assert at.call_args_list[-1] == mock.call("AT+CFUN=1,1") + ttys.return_value = [] + modem._do_initializing() + ttys.return_value = ['/dev/ttyUSB2', '/dev/ttyUSB3'] + at.reset_mock() + modem._do_initializing() + assert mock.call("AT+CFUN=1,1") not in at.call_args_list + identity.assert_called_once() + + def test_initialization_resyncs_ports_before_at_commands(): modem = m.Modem() with mock.patch.object(m.glob, 'glob', return_value=['/dev/ttyUSB2', '/dev/ttyUSB3']), \