From 2a9af52a24b16033df775824ef85d2c53dd779f3 Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Thu, 29 Oct 2026 04:02:37 -0647 Subject: [PATCH 1/2] omamobile-modem 0.1.9: poll the CP state every 5 s, on one open file On the palmtop (Pixel 9a) every open and close of umts_boot0 woke the CP's PCIe link for about 100 ms. The watcher opened the file for each 0.5 s poll, so it woke the link about 120 times a minute. It now holds one file open for the whole watch and polls every 5 s, which cuts the link-ups to about 5 a minute. Whether the status ioctl on the held file wakes the link too is being measured. The watcher was one source of link cycling, and the hang seen while gnssd reloaded Kepler is still open: it happened with this watcher in place too. A crash still shows within seconds. The interval, OMAMOBILE_MODEM_WATCH_SECONDS, is read inside watch() and checked against 0.2-60 s, with the default used otherwise, so a bad value can't break the other subcommands, power-off at the profile's stop included. The package test covers it. --- .../omamobile-modem/lib/omamobile-modem-cp | 48 ++++++++++++++++++- packages/omamobile-modem/test/all | 13 +++++ packages/omamobile-modem/version | 2 +- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/packages/omamobile-modem/lib/omamobile-modem-cp b/packages/omamobile-modem/lib/omamobile-modem-cp index c2510e167..657fa743c 100755 --- a/packages/omamobile-modem/lib/omamobile-modem-cp +++ b/packages/omamobile-modem/lib/omamobile-modem-cp @@ -34,6 +34,18 @@ BOOT = os.environ.get('OMAMOBILE_MODEM_BOOT', '/dev/umts_boot0') ALLOW = Path('/sys/module/cpif/parameters/allow_poweroff') CRASH_LOG = Path(os.environ.get('OMAMOBILE_MODEM_CRASH_LOG', '/var/log/omamobile/modem-crashes.log')) RUN = Path(os.environ.get('OMAMOBILE_MODEM_RUN', '/run/omamobile-modem')) +# watch's poll interval (OMAMOBILE_MODEM_WATCH_SECONDS overrides it; read in +# watch(), so a bad value can't break the other subcommands). On the Pixel +# 9a each open and close of umts_boot0 woke the CP's PCIe link for about +# 100 ms: the old 0.5 s poll, one open per poll, woke it about 120 times a +# minute, and the file held open for the whole watch cuts that to about 5. +# Whether the status ioctl on a held file wakes the link too is being +# measured. The watcher was one source of link cycling; the hang seen while +# gnssd reloaded Kepler (device/tegu/MODEM-PLAN.md G3) is still open, it +# happened with this watcher in place too. A crash shows within seconds +# either way. +WATCH_SECONDS_DEFAULT = 5.0 +WATCH_SECONDS_RANGE = (0.2, 60.0) IOCTL_POWER_OFF = 0x6f20 # _IO('o', 0x20) IOCTL_GET_CP_STATUS = 0x6f27 # _IO('o', 0x27) @@ -128,11 +140,43 @@ def record_crash(): os.fsync(out.fileno()) +def watch_seconds(): + """OMAMOBILE_MODEM_WATCH_SECONDS within WATCH_SECONDS_RANGE, else the + default (with a note), so a typo there can't stop the watch.""" + value = os.environ.get('OMAMOBILE_MODEM_WATCH_SECONDS') + if value is None: + return WATCH_SECONDS_DEFAULT + try: + seconds = float(value) + except ValueError: + seconds = float('nan') + low, high = WATCH_SECONDS_RANGE + if not low <= seconds <= high: + say(f'OMAMOBILE_MODEM_WATCH_SECONDS={value!r} is not {low:g}-{high:g} s: ' + f'polling every {WATCH_SECONDS_DEFAULT:g} s') + return WATCH_SECONDS_DEFAULT + return seconds + + def watch(marker=None): + seconds = watch_seconds() was_online = False crashed = False + fd = None while True: - now = state() + # one open file for the whole watch, so each poll is a single ioctl + # (the open and close woke the 9a's CP link; see WATCH_SECONDS_DEFAULT) + try: + if fd is None: + fd = os.open(BOOT, os.O_RDWR | os.O_CLOEXEC) + now = fcntl.ioctl(fd, IOCTL_GET_CP_STATUS) + except FileNotFoundError: + now = 0 + except OSError: + if fd is not None: + os.close(fd) + fd = None + now = state() if now == ONLINE and not was_online: was_online = True say('CP ONLINE') @@ -146,7 +190,7 @@ def watch(marker=None): (RUN / 'crashed').write_text(STATES.get(now, str(now)) + '\n') say('the CP left ONLINE: crash recorded; nothing restarts it. ' 'Reboot to recover (the reboot powers it off).') - time.sleep(.5) + time.sleep(seconds) def baseband_version(path): diff --git a/packages/omamobile-modem/test/all b/packages/omamobile-modem/test/all index 34d989298..013446169 100755 --- a/packages/omamobile-modem/test/all +++ b/packages/omamobile-modem/test/all @@ -59,6 +59,19 @@ python3 "$cp" baseband-version "$t/noinfo.bin" 2>/dev/null && fail "a TOC withou python3 "$cp" baseband-version "$t/notoc.bin" 2>/dev/null && fail "a file without a TOC is refused" pass "baseband version" +# watch's poll interval is read inside watch(): a value outside the range, +# or not a number, falls back to the default, and never stops another +# subcommand (power-off at the profile's stop runs with the same environment). +ws() { + OMAMOBILE_MODEM_WATCH_SECONDS=$1 python3 -c ' +import sys; g = {"__name__": "cp"}; exec(open(sys.argv[1]).read(), g); print(g["watch_seconds"]())' "$cp" 2>/dev/null +} +[[ $(ws 0.5) == 0.5 && $(ws 60) == 60.0 && $(ws five) == 5.0 && $(ws 0) == 5.0 && $(ws 600) == 5.0 && $(ws nan) == 5.0 ]] || + fail "watch interval: $(ws 0.5) $(ws 60) $(ws five) $(ws 0) $(ws 600) $(ws nan)" +[[ $(OMAMOBILE_MODEM_WATCH_SECONDS=five python3 "$cp" baseband-version "$t/modem.bin" 2>/dev/null) == test-version-001 ]] || + fail "a bad watch interval leaves other subcommands alone" +pass "watch interval" + mkdir -p "$t/efs" "$t/efs_backup" for f in efs/nv_normal.bin efs/nv_protected.bin efs_backup/nv_protected.bak; do printf 'made-up %s' "$f" > "$t/$f" diff --git a/packages/omamobile-modem/version b/packages/omamobile-modem/version index 699c6c6d4..1a030947e 100644 --- a/packages/omamobile-modem/version +++ b/packages/omamobile-modem/version @@ -1 +1 @@ -0.1.8 +0.1.9 From babf987a8605cb96ce3759a2ddd40a201c3f990d Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Thu, 29 Oct 2026 10:17:14 -0647 Subject: [PATCH 2/2] omamobile-modem 0.1.9: the held file is the fix, so the interval stays at 0.5 s Measured on the palmtop with the modem ONLINE: the profile's held-file watcher made 0 link-ups in a minute, and a second held-file watcher at 0.5 s added 0 in its minute. So it is the open and close of umts_boot0 that wakes the CP's PCIe link, not the status ioctl, and the default goes back to 0.5 s with the file held open; crash timing on the other phones stays as it was. --- packages/omamobile-modem/lib/omamobile-modem-cp | 16 ++++++++-------- packages/omamobile-modem/test/all | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/omamobile-modem/lib/omamobile-modem-cp b/packages/omamobile-modem/lib/omamobile-modem-cp index 657fa743c..078271d4d 100755 --- a/packages/omamobile-modem/lib/omamobile-modem-cp +++ b/packages/omamobile-modem/lib/omamobile-modem-cp @@ -37,14 +37,14 @@ RUN = Path(os.environ.get('OMAMOBILE_MODEM_RUN', '/run/omamobile-modem')) # watch's poll interval (OMAMOBILE_MODEM_WATCH_SECONDS overrides it; read in # watch(), so a bad value can't break the other subcommands). On the Pixel # 9a each open and close of umts_boot0 woke the CP's PCIe link for about -# 100 ms: the old 0.5 s poll, one open per poll, woke it about 120 times a -# minute, and the file held open for the whole watch cuts that to about 5. -# Whether the status ioctl on a held file wakes the link too is being -# measured. The watcher was one source of link cycling; the hang seen while -# gnssd reloaded Kepler (device/tegu/MODEM-PLAN.md G3) is still open, it -# happened with this watcher in place too. A crash shows within seconds -# either way. -WATCH_SECONDS_DEFAULT = 5.0 +# 100 ms: the old watch, one open per 0.5 s poll, woke it about 120 times a +# minute. The status ioctl on a file held open doesn't wake it at all (0 +# link-ups in a minute at 0.5 s, measured 2026-10-29), so the file is held +# for the whole watch and the interval stays at 0.5 s. The watcher was one +# source of link cycling; the hang seen while gnssd reloaded Kepler +# (device/tegu/MODEM-PLAN.md G3) is still open, it happened with the held +# file too. A crash shows within a second. +WATCH_SECONDS_DEFAULT = 0.5 WATCH_SECONDS_RANGE = (0.2, 60.0) IOCTL_POWER_OFF = 0x6f20 # _IO('o', 0x20) diff --git a/packages/omamobile-modem/test/all b/packages/omamobile-modem/test/all index 013446169..ff74e22c8 100755 --- a/packages/omamobile-modem/test/all +++ b/packages/omamobile-modem/test/all @@ -66,7 +66,7 @@ ws() { OMAMOBILE_MODEM_WATCH_SECONDS=$1 python3 -c ' import sys; g = {"__name__": "cp"}; exec(open(sys.argv[1]).read(), g); print(g["watch_seconds"]())' "$cp" 2>/dev/null } -[[ $(ws 0.5) == 0.5 && $(ws 60) == 60.0 && $(ws five) == 5.0 && $(ws 0) == 5.0 && $(ws 600) == 5.0 && $(ws nan) == 5.0 ]] || +[[ $(ws 5) == 5.0 && $(ws 60) == 60.0 && $(ws five) == 0.5 && $(ws 0) == 0.5 && $(ws 600) == 0.5 && $(ws nan) == 0.5 ]] || fail "watch interval: $(ws 0.5) $(ws 60) $(ws five) $(ws 0) $(ws 600) $(ws nan)" [[ $(OMAMOBILE_MODEM_WATCH_SECONDS=five python3 "$cp" baseband-version "$t/modem.bin" 2>/dev/null) == test-version-001 ]] || fail "a bad watch interval leaves other subcommands alone"