diff --git a/device/cheetah/MODEM-CLIENT.md b/device/cheetah/MODEM-CLIENT.md index a1f504861..95a809fb2 100644 --- a/device/cheetah/MODEM-CLIENT.md +++ b/device/cheetah/MODEM-CLIENT.md @@ -112,10 +112,15 @@ registration indication), unless `ims_required = yes`. - **Outgoing SMS** carry a durable 32-hex request id; an interrupted or uncertain send is marked `unknown`, never repeated automatically. - **Calls**: a hangup whose release isn't confirmed within 12 s, or a call - left over from a previous run, stops the modem unit (CP off, then rfsd): - the only way to be sure the radio isn't still carrying a call. That is a - CP power-off (one UFS handler pass) and is the one place the RIL powers - the modem off. + left over from a previous run, turns the radio off (best effort, AT+CFUN=0) + and stops sitd with the call still recorded as its own, for an attended + reboot. The journal says "Call release not confirmed: ", the radio + and CP states, and "attended reboot required". The RIL no longer stops the + modem unit or powers the CP off: an in-place power-off reset the 9a once, + and on the 7 Pro every power-off costs a UFS error-handler pass. The unit + has `Restart=no`, so a manual restart in the same boot finds the recorded + call, turns the radio off again and exits. This path has run only in the + simulator. - **Storage**: received SMS in `/var/lib/omamobile-sit` (0700, files 0600): numbers and text, never to be copied into the repository. diff --git a/device/cheetah/MODEM-PLAN.md b/device/cheetah/MODEM-PLAN.md index d160beeda..a736265ab 100644 --- a/device/cheetah/MODEM-PLAN.md +++ b/device/cheetah/MODEM-PLAN.md @@ -1527,7 +1527,8 @@ tools/phone-ssh 'omamobile-sit hangup; sleep 2; omamobile-sit status; # "AoC call routes restored". Then the human calls the phone: `omamobile-sit answer`, talk, hangup # stop: no audio either way (record the journal: "voice PCM ... failed" means the AoC card; check that # PipeWire does not hold PCM 4/11), distorted or loud audio (hang up at once), a release that is -# never confirmed (the RIL stops the modem unit by design after 12 s: one UFS pass; reboot after) +# never confirmed (after 12 s the RIL turns the radio off and exits: "Call release not confirmed", +# "attended reboot required"; reboot, which powers the CP off: one UFS pass) # rollback: omamobile-sit hangup # M4.6 at boot: superseded by "M4.6: the modem stack at boot" at the end of section 3 (one switch, @@ -2638,8 +2639,8 @@ tools/phone-ssh 'omamobile-sit answer; sleep 10; omamobile-sit status | grep -E # outgoing, to the human's other phone (international form); answer it there, hang up there after ~10 s: tools/phone-ssh 'omamobile-sit dial ; sleep 30; omamobile-sit status | grep -E "^call"' # pass: the other phone rings and connects (silent), then "terminated" after the remote hang-up -# stop: "Call release was not confirmed; modem stopped" (the RIL powered the CP off by design: one UFS -# pass; reboot before anything else); a CP crash; any sound from the phone's speakers (stop at once: +# stop: "Call release not confirmed" and "attended reboot required" (the RIL turned the radio off and +# exited, leaving the CP on; reboot before anything else, one UFS pass on the way down); a CP crash; any sound from the phone's speakers (stop at once: # nothing should route audio: record `amixer -c cget name="TDM_0_RX Mixer EP5"`) # rollback: omamobile-sit hangup # Never dial an emergency number to test (refused anyway; TELEPHONY-APPS-PLAN 3.3) diff --git a/device/tegu/MODEM-PLAN.md b/device/tegu/MODEM-PLAN.md index 1ad0c50db..0aecea8ff 100644 --- a/device/tegu/MODEM-PLAN.md +++ b/device/tegu/MODEM-PLAN.md @@ -1935,9 +1935,10 @@ tools/phone-ssh 'omamobile-sit sms send "omamobile test"' # SIM-10 calls: NOT in this test. No audio route exists on the 9a (table above). A signalling-only call # (ring, answer, hang up, silence both ways, as cheetah's S10) is its own later step, after this runbook passes. -# Nobody calls the palmtop's number during the test: an incoming call that sitd can't release makes its -# safety stop target omamobile-modem.service, which is masked on the 9a, so it would only time out. If the -# journal says a call release wasn't confirmed ("call release not confirmed" or the stop timing out): don't +# Nobody calls the palmtop's number during the test: an incoming call that sitd can't release makes it +# turn the radio off and exit for an attended reboot (since #364; before it, its safety stop targeted +# omamobile-modem.service, masked on the 9a). If the journal says "Call release not confirmed" or +# "attended reboot required": don't # stop anything by hand; "omamobile-sit radio off", record the journal lines (states and codes), and end # the session with SIM-11's attended reboot. diff --git a/packages/omamobile-modem-sit/lib/cp.py b/packages/omamobile-modem-sit/lib/cp.py index 65108b2ea..50f9f3b7f 100644 --- a/packages/omamobile-modem-sit/lib/cp.py +++ b/packages/omamobile-modem-sit/lib/cp.py @@ -1,16 +1,12 @@ # SPDX-License-Identifier: MIT -"""The CP (modem) state from CPIF, and the one way this stack powers it off. +"""Read the CP state without stopping or restarting the modem. -The modem stays on once it is up (device/cheetah/MODEM-PLAN.md M2.6: every -modem power-off costs one recovered UFS error-handler pass). The RIL powers -it off only when a call's release can't be confirmed: then stopping the -modem unit is the only way to be sure the radio isn't carrying a call. The -unit's stop powers the CP off before rfsd goes (omamobile-modem profile). +An in-place modem stop has reset tegu. Unconfirmed calls are handled by +service.py with best-effort radio-off and an attended-reboot requirement; +the CP and its NV server stay running until shutdown. """ import fcntl import os -import subprocess -import time from paths import BOOT @@ -19,7 +15,6 @@ 5: 'nv-rebuilding', 6: 'loader-done', 7: 'sim-attach', 8: 'sim-detach', 9: 'crash-watchdog'} ONLINE = 4 -MODEM_UNIT = 'omamobile-modem.service' # One open file for every status request. On the Pixel 9a each open and @@ -49,19 +44,3 @@ def state(): def online(): return state() == ONLINE - - -def stop_modem(reason): - """Stop the modem unit (CP off, then rfsd) and wait for OFFLINE.""" - print('Stopping the modem:', reason, flush=True) - if BOOT == 'none': - return - subprocess.run(['systemctl', 'stop', '--no-block', MODEM_UNIT], - stdin=subprocess.DEVNULL, capture_output=True, timeout=20) - deadline = time.monotonic() + 30 - while time.monotonic() < deadline: - if state() == 0: - print('CP OFFLINE', flush=True) - return - time.sleep(.25) - raise RuntimeError('CP shutdown not confirmed; reboot the phone') diff --git a/packages/omamobile-modem-sit/lib/data_call.py b/packages/omamobile-modem-sit/lib/data_call.py index 328ed1479..6c9f8b3f4 100644 --- a/packages/omamobile-modem-sit/lib/data_call.py +++ b/packages/omamobile-modem-sit/lib/data_call.py @@ -219,8 +219,10 @@ def setup_payload(cid, radio_technology, pdu_session, apn, *, ims=False, ip_type # ApnSetting's supported types (vendor.radio.base # ApnSetting::SetSupportedTypesBitmask, member +0x160; the 983-byte # builder writes it at 0xf1). DEFAULT for the internet bearer, IMS for - # the IMS one. Until 0.3.6 this was sent as 0 (taken for an MTU), and - # the 9a's modem answered every internet setup with status 36. + # the IMS one. Until 0.3.6 this was sent as 0 (taken for an MTU). It + # wasn't the cause of the 9a's status 36: 0.3.7 sent the bitmap and + # still got 36, and 0.3.8's wait after the data permission fixed it + # (device/tegu MODEM-PLAN, "SIM test results"). # apn_types: the whole bitmask of the APN's data profile, as stock's # builder copies it (ApnSetting +0x160 -> 0xf1); Verizon's IMS APN is # ims|ia (0x140), which stock's IMS setup carries. diff --git a/packages/omamobile-modem-sit/lib/service.py b/packages/omamobile-modem-sit/lib/service.py index 5e1a65adb..ab181f61c 100644 --- a/packages/omamobile-modem-sit/lib/service.py +++ b/packages/omamobile-modem-sit/lib/service.py @@ -92,6 +92,37 @@ def signal_percent(rsrp): SUBSCRIBERS = 8 +class UnconfirmedRelease(RuntimeError): + """Fatal call ownership failure; never turn this into a socket reply.""" + + +def unconfirmed_release(client, reason): + """Stop serving an uncertain call without power-cycling the CP. + + In-place modem stops have reset tegu. Radio-off is best effort: even + an accepted command does not establish that the call was released. + Keep call ownership and the modem/NV server for an attended reboot. + """ + print('Call release not confirmed:', reason, flush=True) + try: + radio(False) + print('Recovery radio-off command accepted', flush=True) + except (OSError, RuntimeError, ValueError) as error: + print('Recovery radio-off failed:', type(error).__name__, flush=True) + try: + payload = client.request(0x0801, timeout=5) + state = RADIO.get(struct.unpack(' self.hangup_deadline: - cp.stop_modem('a call release was not confirmed') - raise RuntimeError('Call release was not confirmed; modem stopped') + self.release_unconfirmed = True + unconfirmed_release(self.client, 'hangup deadline expired') def send_sms(self, request): key = request.get('request_id', '') @@ -783,6 +815,9 @@ def serve_one(service, connection, clients): return True result = service.dispatch(request) response = dict(ok=True, result=result) + except UnconfirmedRelease: + connection.close() + raise except (ValueError, KeyError, TypeError, RuntimeError, OSError) as error: # Errors from wire data must never expose raw values. response = dict(ok=False, error=str(error) if isinstance(error, RuntimeError) @@ -856,8 +891,7 @@ def stop(*_): try: marker = paths.RUN / 'active.json' if marker.exists() and json.loads(marker.read_text()): - cp.stop_modem('a call from the previous service run was never released') - raise RuntimeError('Previous call ownership unresolved; modem stopped') + unconfirmed_release(client, 'a call from the previous service run was never released') service = Service(client, database(paths.STATE / 'apps.db'), settings) service.publish() for sms in paths.INBOX.glob('*.sit'): @@ -904,7 +938,7 @@ def stop(*_): service.broadcast() finally: try: - if service and service.live(): + if service and service.live() and not service.release_unconfirmed: for call in service.live(): if call['modem_id'] is not None: try: @@ -917,17 +951,20 @@ def stop(*_): if packet: service.event(packet) if service.live(): - cp.stop_modem('a call release was not confirmed at service stop') - if service: - service.audio_stop() + service.release_unconfirmed = True + unconfirmed_release(client, 'a call release was not confirmed at service stop') finally: - if service: - for connection in list(service.subscribers): - service.drop(connection) - client.close() - server.close() - SOCKET.unlink(missing_ok=True) - (paths.RUN / 'state.json').unlink(missing_ok=True) + try: + if service: + service.audio_stop() + finally: + if service: + for connection in list(service.subscribers): + service.drop(connection) + client.close() + server.close() + SOCKET.unlink(missing_ok=True) + (paths.RUN / 'state.json').unlink(missing_ok=True) if __name__ == '__main__': diff --git a/packages/omamobile-modem-sit/test/test_service.py b/packages/omamobile-modem-sit/test/test_service.py index 7fbeaec10..5a8e2f5a1 100644 --- a/packages/omamobile-modem-sit/test/test_service.py +++ b/packages/omamobile-modem-sit/test/test_service.py @@ -1,8 +1,10 @@ # SPDX-License-Identifier: MIT import env # noqa: F401 (paths and sys.path first) import json +import os from pathlib import Path import struct +import socket import tempfile import unittest from unittest.mock import Mock, MagicMock, patch @@ -62,6 +64,29 @@ def test_restart_does_not_retry_pending_sms(self): self.db.execute('INSERT INTO outgoing VALUES (?,?,?,?,?)',('a'*32,'digest','sending','',0)); self.db.commit() other=app_service.database(self.root/'db') self.assertEqual(other.execute('SELECT state FROM outgoing').fetchone()[0],'unknown'); other.close() + + def test_failed_radio_off_still_requires_reboot_without_claiming_release(self): + self.client.request.side_effect = TimeoutError() + with patch.object(app_service, 'radio', side_effect=OSError()), \ + patch.object(app_service.cp, 'state', side_effect=OSError()), \ + patch('builtins.print') as printed: + with self.assertRaisesRegex(RuntimeError, 'attended reboot required'): + app_service.unconfirmed_release(self.client, 'hangup deadline expired') + log = ' '.join(' '.join(map(str, call.args)) for call in printed.call_args_list) + self.assertIn('hangup deadline expired', log) + self.assertIn('radio-off failed: OSError', log) + self.assertIn('radio unavailable (TimeoutError) CP unavailable', log) + self.assertIn('leaving the modem running', log) + + def test_unconfirmed_release_during_socket_request_is_fatal(self): + mine, theirs = socket.socketpair() + self.addCleanup(mine.close) + self.addCleanup(theirs.close) + theirs.sendall(b'{"action":"status"}\n') + self.service.dispatch = Mock(side_effect=app_service.UnconfirmedRelease('attended reboot required')) + with self.assertRaises(app_service.UnconfirmedRelease): + app_service.serve_one(self.service, mine, {os.getuid()}) + self.assertEqual(mine.fileno(), -1) def test_ring_does_not_autoanswer_and_release_restores_audio(self): n=b'+12025550123'; body=struct.pack('