diff --git a/packages/omamobile-modem/README.md b/packages/omamobile-modem/README.md index 30405c55d..ca3b9cade 100644 --- a/packages/omamobile-modem/README.md +++ b/packages/omamobile-modem/README.md @@ -32,3 +32,15 @@ configuration cannot reassign MARK/HOLD or redefine refuse. The normal marker, unclean-stop and hold decisions remain unchanged. Device-specific configuration does not authorize starting a modem or replace its watchdog/backup/recovery qualification. The maintainer allocates the package version at merge. + +# On or off from the shell + +`omamobile-modem-switch` (in `/usr/lib/omamobile-vendor/`) backs the Modem +switch on the shell's Mobile network page. It is offered only where +something starts `omamobile-modem-boot.target` at boot (the Pixel 7 Pro's +device package links it into `multi-user.target`) or where the switch has +masked it; elsewhere it refuses. "Off" masks the target and stops nothing in +this boot; "on" unmasks it and starts the stack only if this boot's modem +never started. The polkit action `org.omamobile.modem.switch` lets the user +of the active local session switch it without a password; anyone else is +refused. diff --git a/packages/omamobile-modem/install b/packages/omamobile-modem/install index 047e54656..78bda0ad3 100755 --- a/packages/omamobile-modem/install +++ b/packages/omamobile-modem/install @@ -19,6 +19,8 @@ v=$d/usr/lib/omamobile-vendor install -Dm755 lib/omamobile-modem-start "$v/omamobile-modem-start" install -Dm755 lib/omamobile-modem-cp "$v/omamobile-modem-cp" install -Dm755 lib/omamobile-modem-nv-overlay "$v/omamobile-modem-nv-overlay" +install -Dm755 lib/omamobile-modem-switch "$v/omamobile-modem-switch" +install -Dm644 polkit/org.omamobile.modem.policy "$d/usr/share/polkit-1/actions/org.omamobile.modem.policy" install -Dm755 bin/omamobile-modem "$d/usr/bin/omamobile-modem" install -Dm644 profiles/modem.conf "$v/profiles/modem.conf" install -Dm644 modem/props.conf "$v/modem/props.conf" diff --git a/packages/omamobile-modem/lib/omamobile-modem-switch b/packages/omamobile-modem/lib/omamobile-modem-switch new file mode 100755 index 000000000..eda569c26 --- /dev/null +++ b/packages/omamobile-modem/lib/omamobile-modem-switch @@ -0,0 +1,114 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# omamobile-modem-switch: the modem stack on or off at boot, for the shell's +# Mobile network page (omamobile-mobile-network modem on|off, under pkexec: +# org.omamobile.modem.switch, the active local user). +# +# omamobile-modem-switch status one JSON object, any user (reads only) +# omamobile-modem-switch on unmask omamobile-modem-boot.target and, +# if the modem hasn't started this boot, +# start the stack now +# omamobile-modem-switch off mask omamobile-modem-boot.target: the +# stack stays off from the next boot +# +# The switch is offered only where something starts the target at boot: +# it is wanted by multi-user.target (the 7 Pro's device-cheetah vendor +# link, or an /etc link) or masked by this switch. Elsewhere (the 9a has +# its own modem start, the 6 Pro none at boot) status says +# "available": false, the page hides the switch, and "on" and "off" refuse, +# so the shell never starts a modem outside that phone's own starter. +# +# "off" stops nothing in this boot. Once the modem's drivers have loaded, +# its sleep inhibitor stays until the reboot whatever happens to the modem +# unit (omamobile-modem-inhibit; only a start refused before any driver +# loads releases it), and a modem powered off can't be started again before +# a reboot (the profile's once-per-boot mark), so a stop now would cost the +# phone its modem for the boot and still not let it sleep; and every modem +# power-off is followed by one recovered UFS error-handler pass (MODEM-PLAN +# M2.6). The page offers the restart instead. "on" starts the stack only +# when this boot's modem has never started (no +# /run/omamobile-modem/profile-started): the same start as at boot, through +# the target, never a second CP power-on. +# +# status: +# {"available": true, "enabled": true, "running": true, "started": true, +# "provisioned": true, "sleep_blocked": true, "restart_needed": false} +# available: the target is wanted at boot or masked (above). enabled: +# available and not masked (it starts at boot). running: the modem unit is +# active. started: the modem profile started this boot. provisioned: the +# three files tools/phone-provision puts there exist (without them the +# units skip themselves). sleep_blocked: the modem's sleep inhibitor is +# held. restart_needed: what the switch says differs from this boot (off +# but started, or on but started and stopped). +set -euo pipefail + +TARGET=omamobile-modem-boot.target +MARK=${OMAMOBILE_MODEM_BOOT_MARK:-/run/omamobile-modem/profile-started} +SYSTEMCTL=${OMAMOBILE_SYSTEMCTL:-systemctl} +PROVISION=(/usr/lib/omamobile-vendor/modem/root /etc/omamobile/modem-nv.sha256 /var/lib/omamobile-vendor/modem/props.conf) +[[ -n ${OMAMOBILE_MODEM_PROVISION:-} ]] && read -ra PROVISION <<< "$OMAMOBILE_MODEM_PROVISION" + +die() { echo "omamobile-modem-switch: $*" >&2; exit 1; } + +masked() { [[ $("$SYSTEMCTL" is-enabled "$TARGET" 2>/dev/null || true) == masked* ]]; } +# WantedBy sees both the vendor link under /usr/lib and links under /etc; +# `is-enabled` reports a vendor link as "static" or "disabled". +wanted() { [[ " $("$SYSTEMCTL" show -p WantedBy --value "$TARGET" 2>/dev/null || true) " == *" multi-user.target "* ]]; } +available() { wanted || masked; } +enabled() { available && ! masked; } +sleep_blocked() { "$SYSTEMCTL" is-active --quiet omamobile-modem-inhibit.service; } +running() { "$SYSTEMCTL" is-active --quiet omamobile-modem.service; } +started() { [[ -e $MARK ]]; } +provisioned() { + local p + for p in "${PROVISION[@]}"; do + [[ -e $p ]] && continue + # status runs as the user, and props.conf sits in a root-only folder + # (the phone's board id): a folder that is there but can't be searched + # counts as provisioned here; "on" runs as root and sees the file. + [[ -d ${p%/*} && ! -x ${p%/*} ]] && continue + return 1 + done +} +json_bool() { if "$@"; then echo true; else echo false; fi; } + +status() { + local a e r s p b need=false + a=$(json_bool available); e=$(json_bool enabled); r=$(json_bool running); s=$(json_bool started) + p=$(json_bool provisioned); b=$(json_bool sleep_blocked) + if [[ $a == false ]]; then + need=false + elif [[ $e == false && $s == true ]] || [[ $e == true && $s == true && $r == false ]]; then + need=true + fi + printf '{"available": %s, "enabled": %s, "running": %s, "started": %s, "provisioned": %s, "sleep_blocked": %s, "restart_needed": %s}\n' \ + "$a" "$e" "$r" "$s" "$p" "$b" "$need" +} + +case ${1:-} in + status) + [[ $# -eq 1 ]] || die "usage: omamobile-modem-switch status|on|off" + status + ;; + on|off) + [[ $# -eq 1 ]] || die "usage: omamobile-modem-switch status|on|off" + [[ ${OMAMOBILE_MODEM_SWITCH_UID:-$(id -u)} -eq 0 ]] || die "run as root (the shell uses pkexec)" + available || die "nothing starts the modem at boot on this phone ($TARGET isn't wanted by multi-user.target): no switch here" + if [[ $1 == off ]]; then + "$SYSTEMCTL" mask "$TARGET" >/dev/null + else + "$SYSTEMCTL" unmask "$TARGET" >/dev/null + # Started at boot or by an earlier "on": leave it (the unit itself + # refuses a second CP power-on). Not provisioned: the units would skip + # themselves anyway. Not wanted once unmasked (a hand mask on a phone + # without the boot link): nothing to start here. + if wanted && ! started && ! running && provisioned; then + "$SYSTEMCTL" start --no-block "$TARGET" + fi + fi + status + ;; + *) + die "usage: omamobile-modem-switch status|on|off" + ;; +esac diff --git a/packages/omamobile-modem/polkit/org.omamobile.modem.policy b/packages/omamobile-modem/polkit/org.omamobile.modem.policy new file mode 100644 index 000000000..5b5da1eb0 --- /dev/null +++ b/packages/omamobile-modem/polkit/org.omamobile.modem.policy @@ -0,0 +1,22 @@ + + + + + + Omarchy Mobile + https://github.com/derekross/omarchy-mobile + + Turn the modem on or off + Authentication is required to turn the modem on or off + + no + no + yes + + /usr/lib/omamobile-vendor/omamobile-modem-switch + + diff --git a/packages/omamobile-modem/test/all b/packages/omamobile-modem/test/all index 4b0f8abb4..01ed90180 100755 --- a/packages/omamobile-modem/test/all +++ b/packages/omamobile-modem/test/all @@ -10,10 +10,11 @@ pass() { printf 'ok - %s\n' "$1"; } export PYTHONDONTWRITEBYTECODE=1 cp=lib/omamobile-modem-cp -bash -n install && bash -n lib/omamobile-modem-start && bash -n profiles/modem.conf || fail "shell syntax" +bash -n install && bash -n lib/omamobile-modem-start && bash -n profiles/modem.conf && + bash -n lib/omamobile-modem-switch || fail "shell syntax" python3 -c 'import ast, sys; ast.parse(open(sys.argv[1]).read())' "$cp" || fail "$cp parses" if command -v shellcheck >/dev/null; then - shellcheck -S warning lib/omamobile-modem-start install || fail "shellcheck" + shellcheck -S warning lib/omamobile-modem-start lib/omamobile-modem-switch install || fail "shellcheck" fi pass "scripts parse" python3 test/test_link.py || fail "device link module parameters and load order" @@ -140,7 +141,8 @@ for f in usr/share/doc/omamobile-modem/README.md usr/lib/omamobile-vendor/omamob usr/lib/omamobile-vendor/profiles/modem.conf usr/lib/omamobile-vendor/modem/props.conf \ usr/lib/systemd/system/omamobile-modem.service usr/lib/udev/rules.d/60-omamobile-modem-nv-ro.rules \ usr/lib/omamobile-vendor/omamobile-modem-nv-overlay usr/bin/omamobile-modem \ - usr/lib/systemd/system/omamobile-modem-nv-overlay.service usr/lib/systemd/system/omamobile-modem-nv-overlay.timer; do + usr/lib/systemd/system/omamobile-modem-nv-overlay.service usr/lib/systemd/system/omamobile-modem-nv-overlay.timer \ + usr/lib/omamobile-vendor/omamobile-modem-switch usr/share/polkit-1/actions/org.omamobile.modem.policy; do [[ -e $stage/$f ]] || fail "staged $f" done grep -qx 'persist.vendor.modem.efs.clear.action=none' modem/props.conf || @@ -449,5 +451,94 @@ printf 'ro.boot.cdt_hwid=TESTID\n' > "$t/p4"; cdt "$t/p4" || fail "the persist c cdt "$t/missing" && fail "a missing props file is refused" pass "board id required" +# The on/off switch (the shell's Mobile network page): a fake systemctl +# records what it's asked. "off" only masks; "on" unmasks and starts the +# target only when this boot's modem never started and the phone is +# provisioned; status reads without root. The switch exists only where the +# target is wanted at boot (SW_WANTED: the 7 Pro's vendor link) or masked. +sw=lib/omamobile-modem-switch +mkdir -p "$t/sw/bin" "$t/sw/prov" +cat > "$t/sw/bin/systemctl" <<'SH' +#!/bin/bash +echo "$*" >> "$SW_LOG" +case $1 in + is-enabled) if [[ -e $SW_MASKED ]]; then echo masked; else echo disabled; fi ;; + is-active) if [[ $3 == omamobile-modem-inhibit.service ]]; then [[ -e $SW_INHIBIT ]]; else [[ -e $SW_ACTIVE ]]; fi ;; + show) [[ $2 == -p && $3 == WantedBy ]] || exit 1; [[ -e $SW_WANTED ]] && echo "multi-user.target" || echo ;; + mask) touch "$SW_MASKED" ;; + unmask) rm -f "$SW_MASKED" ;; +esac +SH +chmod +x "$t/sw/bin/systemctl" +: > "$t/sw/prov/a"; : > "$t/sw/wanted" +swrun() { + SW_LOG=$t/sw/log SW_MASKED=$t/sw/masked SW_ACTIVE=$t/sw/active SW_WANTED=$t/sw/wanted SW_INHIBIT=$t/sw/inhibit \ + OMAMOBILE_SYSTEMCTL=$t/sw/bin/systemctl OMAMOBILE_MODEM_BOOT_MARK=$t/sw/mark \ + OMAMOBILE_MODEM_PROVISION="${SW_PROV:-$t/sw/prov/a}" "$@" +} +swfield() { python3 -c 'import json,sys; print(str(json.loads(sys.stdin.read())[sys.argv[1]]).lower())' "$1"; } +[[ $(swrun "$sw" status | swfield enabled) == true ]] || fail "switch: on by default" +[[ $(swrun "$sw" status | swfield available) == true ]] || fail "switch: available where the target is wanted at boot" +[[ $(swrun "$sw" status | swfield sleep_blocked) == false ]] || fail "switch: no inhibitor, no sleep block" +: > "$t/sw/inhibit" +[[ $(swrun "$sw" status | swfield sleep_blocked) == true ]] || fail "switch: the inhibitor reads as the sleep block" +rm -f "$t/sw/inhibit" +OMAMOBILE_MODEM_SWITCH_UID=1000 swrun "$sw" off 2>/dev/null && fail "switch: off needs root" +rm -f "$t/sw/log" +out=$(OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" off) +[[ $(swfield enabled <<< "$out") == false ]] || fail "switch: off masks" +grep -c -e stop -e start "$t/sw/log" >/dev/null && fail "switch: off stops or starts nothing" +# Off while this boot's modem runs: a restart is needed. +date > "$t/sw/mark"; : > "$t/sw/active" +[[ $(swrun "$sw" status | swfield restart_needed) == true ]] || fail "switch: off with the modem up needs a restart" +# On again, the modem already started: unmasked, nothing started. +rm -f "$t/sw/log" +out=$(OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on) +[[ $(swfield enabled <<< "$out") == true && $(swfield restart_needed <<< "$out") == false ]] || fail "switch: on again with the modem up" +grep -c start "$t/sw/log" >/dev/null && fail "switch: never a second start in a boot" +# Started and stopped this boot, switched on: a restart, not a start. +rm -f "$t/sw/active" "$t/sw/log" +out=$(OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on) +grep -c start "$t/sw/log" >/dev/null && fail "switch: a stopped modem is not started again in the boot" +[[ $(swfield restart_needed <<< "$out") == true ]] || fail "switch: a stopped modem needs a restart" +# Never started this boot (masked at boot), switched on: started now. +rm -f "$t/sw/mark" "$t/sw/log" +OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" off >/dev/null +OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on >/dev/null +grep -qx 'start --no-block omamobile-modem-boot.target' "$t/sw/log" || fail "switch: on starts a modem that never started this boot" +# Not provisioned: unmasked, not started. +rm -f "$t/sw/log" "$t/sw/prov/a" +OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on >/dev/null +grep -c start "$t/sw/log" >/dev/null && fail "switch: an unprovisioned phone is not started" +[[ $(swrun "$sw" status | swfield provisioned) == false ]] || fail "switch: provisioned reads the files" +# props.conf sits in a root-only folder on the phone, and status runs as +# the user: a folder that can't be searched counts, a missing one doesn't. +mkdir -p "$t/sw/priv"; : > "$t/sw/priv/props.conf"; chmod 000 "$t/sw/priv" +[[ $(SW_PROV="$t/sw/priv/props.conf" swrun "$sw" status | swfield provisioned) == true ]] || + fail "switch: a file in a root-only folder reads as provisioned" +[[ $(SW_PROV="$t/sw/none/props.conf" swrun "$sw" status | swfield provisioned) == false ]] || + fail "switch: a missing folder is not provisioned" +chmod 700 "$t/sw/priv" +[[ $(SW_PROV="$t/sw/prov/a $t/sw/none/x" swrun "$sw" status | swfield provisioned) == false ]] || + fail "switch: every file in the override list counts" +swrun "$sw" reboot 2>/dev/null && fail "switch: unknown commands refused" +# Not masked and not wanted at boot (the 9a, the 6 Pro): no switch, and +# "on" and "off" refuse without touching systemd. +: > "$t/sw/prov/a"; rm -f "$t/sw/wanted" "$t/sw/masked" "$t/sw/mark" "$t/sw/active" "$t/sw/log" +out=$(swrun "$sw" status) +[[ $(swfield available <<< "$out") == false && $(swfield enabled <<< "$out") == false ]] || + fail "switch: not masked, not wanted reads as not available and not on" +OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on 2>/dev/null && fail "switch: on refused where nothing starts the modem at boot" +OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" off 2>/dev/null && fail "switch: off refused where nothing starts the modem at boot" +grep -c -e mask -e start "$t/sw/log" >/dev/null && fail "switch: a refusal touches nothing" +# Masked by hand on such a phone: "on" unmasks, and starts nothing. +: > "$t/sw/masked"; rm -f "$t/sw/log" +OMAMOBILE_MODEM_SWITCH_UID=0 swrun "$sw" on >/dev/null +grep -c start "$t/sw/log" >/dev/null && fail "switch: on starts nothing that isn't wanted at boot" +[[ $(swrun "$sw" status | swfield available) == false ]] || fail "switch: unmasked and not wanted is not available" +grep -q 'exec.path">/usr/lib/omamobile-vendor/omamobile-modem-switch<' polkit/org.omamobile.modem.policy || + fail "switch: the polkit action names the helper" +pass "on/off switch: off masks only, on starts only a modem that never started this boot" + # The NV overlay: its own tests (synthetic NV-like files only). test/nv-overlay diff --git a/packages/omamobile-modem/version b/packages/omamobile-modem/version index 790629964..5a48b6be2 100644 --- a/packages/omamobile-modem/version +++ b/packages/omamobile-modem/version @@ -1 +1 @@ -0.1.21 +0.1.24 diff --git a/packages/omamobile-shell/bin/omamobile-mobile-network b/packages/omamobile-shell/bin/omamobile-mobile-network index f98458301..76b6d28b8 100755 --- a/packages/omamobile-shell/bin/omamobile-mobile-network +++ b/packages/omamobile-shell/bin/omamobile-mobile-network @@ -8,6 +8,9 @@ SIM line (omamobile-onboarding). omamobile-mobile-network roaming on|off data while roaming omamobile-mobile-network apn auto|APN the internet APN (applies after a restart) omamobile-mobile-network attach-apn auto|APN the APN the modem registers with (after a restart) + omamobile-mobile-network modem on|off the modem stack at boot (omamobile-modem-switch): + off from the next boot; on starts it now if this + boot's modem never started, else after a restart Reads omamobile-sitd's world-readable status (/run/omamobile-sit/state.json), its settings (/etc/omamobile/modem-sit.conf, 0644) and, when it runs, @@ -23,10 +26,14 @@ status: "roaming": {"now": false, "allowed": true}, "apn": {"in_use": "...", "source": "carrier-database", "configured": "auto", "attach_in_use": "...", "attach_source": "same", "attach_configured": "auto"}, - "voice": true, "sms": true} + "voice": true, "sms": true, + "modem": {"available": true, "enabled": true, "running": true, "started": true, + "provisioned": true, "sleep_blocked": true, "restart_needed": false}} installed: the modem stack (omamobile-modem-sit) is on this phone; without it the page shows "No modem". signal is 0-100, or -1 when not reported. +modem: omamobile-modem-switch status (read without root), or null when the +switch isn't installed. """ import configparser import json @@ -38,6 +45,7 @@ import sys RUN = os.environ.get('OMAMOBILE_SIT_RUN', '/run/omamobile-sit') CONFIG = os.environ.get('OMAMOBILE_SIT_CONFIG', '/etc/omamobile/modem-sit.conf') HELPER = os.environ.get('OMAMOBILE_SIT_CONFIG_HELPER', '/usr/lib/omamobile-sit/omamobile-sit-config') +SWITCH = os.environ.get('OMAMOBILE_MODEM_SWITCH', '/usr/lib/omamobile-vendor/omamobile-modem-switch') def settings(path=CONFIG): @@ -52,9 +60,10 @@ def settings(path=CONFIG): return dict(apn=apn, attach_apn=attach, roaming=roaming) -def describe(installed, state, conf, data_active, mm=None): +def describe(installed, state, conf, data_active, mm=None, switch=None): """The page's view from sitd's public state (or None), its settings, - whether the data service runs and ModemManager's modem (or None).""" + whether the data service runs, ModemManager's modem (or None) and the + modem switch's status (or None).""" modem = (state or {}).get('modem') or {} sim = modem.get('sim') or {} apn = modem.get('apn') or {} @@ -88,9 +97,34 @@ def describe(installed, state, conf, data_active, mm=None): attach_source=apn.get('attach_source', '') if isinstance(apn, dict) else '', attach_configured=conf.get('attach_apn', 'auto')), voice=bool(modem.get('voice')), sms=bool(modem.get('sms')), + modem=switch_view(switch), ) +def switch_view(switch): + if not isinstance(switch, dict): + return None + keys = ('available', 'enabled', 'running', 'started', 'provisioned', 'sleep_blocked', 'restart_needed') + return {k: bool(switch.get(k)) for k in keys} + + +def switch_status(): + """omamobile-modem-switch status (it reads only, so no pkexec), or None.""" + if not os.access(SWITCH, os.X_OK): + return None + try: + out = subprocess.run([SWITCH, 'status'], capture_output=True, text=True, timeout=10) + return json.loads(out.stdout) if out.returncode == 0 else None + except (OSError, subprocess.SubprocessError, ValueError): + return None + + +def switch(on): + helper = [SWITCH, 'on' if on else 'off'] + command = helper if os.geteuid() == 0 else ['pkexec'] + helper + return subprocess.run(command, stdout=subprocess.DEVNULL).returncode + + def public_state(): try: with open(os.path.join(RUN, 'state.json')) as f: @@ -145,8 +179,10 @@ def main(argv): if argv == ['status']: state = public_state() print(json.dumps(describe(installed(), state, settings(), data_active(), - modemmanager() if state is not None else None))) + modemmanager() if state is not None else None, switch_status()))) return 0 + if len(argv) == 2 and argv[0] == 'modem' and argv[1] in ('on', 'off'): + return switch(argv[1] == 'on') if len(argv) == 2 and argv[0] == 'data' and argv[1] in ('on', 'off'): return subprocess.run(['omamobile-sit', 'data', argv[1]]).returncode if len(argv) == 2 and argv[0] == 'roaming' and argv[1] in ('on', 'off'): diff --git a/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetwork.qml b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetwork.qml index 273392bcb..da845ba7e 100644 --- a/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetwork.qml +++ b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetwork.qml @@ -9,8 +9,9 @@ import qs.Ui import "MobileNetworkModel.js" as Model import "Readable.js" as Readable -// The Mobile network page: the carrier, signal and registration, mobile -// data and data roaming switches, the APN in use with a field to type one, +// The Mobile network page: the carrier, signal and registration, the +// Modem switch (on or off at boot), the mobile data and data roaming +// switches, the APN in use with a field to type one, // the attach APN (the one the modem registers with) under Advanced, and // whether the SIM's PIN lock is on. Opened from Setup > Mobile network // in Omarchy's menu, the Mobile data tile's chevron, or @@ -19,7 +20,8 @@ import "Readable.js" as Readable // Everything goes through omamobile-mobile-network (bin/): `status` while // the page is open (every 3 s), `data on|off` (omamobile-sit data), // `roaming on|off`, `apn auto|APN` and `attach-apn auto|APN` -// (omamobile-sit-config under pkexec, +// (omamobile-sit-config under pkexec, and `modem on|off` through +// omamobile-modem-switch under pkexec; // polkit: the active user). An APN applies when the modem service next // starts, which needs a restart of the phone: the page says so and offers // one. Without the modem stack (the release image has none yet) the page @@ -76,6 +78,13 @@ Item { else root.close() } + // Back, as on the Wi-Fi and Bluetooth pages: this page closes and quick + // settings, which it was opened from, comes back. + function goBack() { + dismiss() + Quickshell.execDetached(["omarchy-shell", "shell", "summon", "omamobile.quicksettings", "{}"]) + } + // `shell call omamobile.mobilenetwork snapshot ''`: what the page shows // (no numbers or SIM identifiers exist in it). function snapshot() { @@ -106,6 +115,15 @@ Item { actionProc.running = true } + // The modem stack at boot (omamobile-modem-switch through pkexec): off + // from the next boot, on now if this boot's modem never started. + function setModem(on) { + if (actionProc.running) return + setOptimistic("modem", on) + actionProc.command = Model.modemArgv(on) + actionProc.running = true + } + function setRoaming(on) { if (actionProc.running) return setOptimistic("roaming", on) @@ -211,7 +229,7 @@ Item { padding: root.pad focus: root.opened - Keys.onEscapePressed: root.dismiss() + Keys.onEscapePressed: root.goBack() MouseArea { anchors.fill: parent } @@ -229,8 +247,18 @@ Item { anchors.right: parent.right height: root.touchSize - Text { + GlyphTap { + id: backButton + objectName: "mobileNetworkBack" anchors.left: parent.left + anchors.verticalCenter: parent.verticalCenter + glyph: "󰁍" + onTapped: root.goBack() + } + + Text { + anchors.left: backButton.right + anchors.leftMargin: Style.spacing.lg anchors.right: closeButton.left anchors.verticalCenter: parent.verticalCenter textFormat: Text.PlainText @@ -282,9 +310,12 @@ Item { font.pixelSize: Style.font.iconLarge * 1.5 } + // Centred on the glyph while it is the taller of the two; a + // title and body taller than the glyph start at the top, not + // above it, where the list clips them. Column { width: parent.width - bigGlyph.width - parent.spacing - anchors.verticalCenter: bigGlyph.verticalCenter + y: Math.max(0, (bigGlyph.height - height) / 2) Text { width: parent.width objectName: "mobileNetworkTitle" @@ -318,6 +349,23 @@ Item { } // ---------- Switches + SettingRow { + objectName: "setting-modem" + visible: (root.page.state === "ready" || root.page.state === "off") + && !!root.page.modem && root.page.modem.shown === true + title: "Modem" + note: root.page.modem ? (root.page.modem.note || "") : "" + checked: root.value("modem", !!root.page.modem && root.page.modem.on === true) + onToggled: root.setModem(!checked) + } + + SheetAction { + width: parent.width + visible: !!root.page.modem && root.page.modem.restart === true + label: "Restart now" + onTapped: Quickshell.execDetached(Model.REBOOT_ARGV) + } + SettingRow { objectName: "setting-mobileData" visible: root.page.state === "ready" diff --git a/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetworkModel.js b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetworkModel.js index 119a663ad..59d316573 100644 --- a/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetworkModel.js +++ b/packages/omamobile-shell/plugins/omamobile.mobilenetwork/MobileNetworkModel.js @@ -20,6 +20,27 @@ function dataArgv(on) { return ["omamobile-mobile-network", "data", on ? "on" : function roamingArgv(on) { return ["omamobile-mobile-network", "roaming", on ? "on" : "off"] } function apnArgv(value) { return ["omamobile-mobile-network", "apn", value] } function attachArgv(value) { return ["omamobile-mobile-network", "attach-apn", value] } +function modemArgv(on) { return ["omamobile-mobile-network", "modem", on ? "on" : "off"] } + +// The Modem switch (omamobile-modem-switch's status in s.modem, or null when +// it isn't installed): whether the stack starts at boot, and what this boot +// does about it. Shown only where something starts the modem at boot +// (available). Off never stops a running modem (its sleep block lasts until +// the reboot anyway), so off with the modem up says "after a restart", and +// "can't sleep" follows the sleep block itself. +function modemSwitch(m) { + if (!m || m.available !== true) return { shown: false } + var on = m.enabled === true + var note + if (!m.provisioned) note = "Not set up on this phone yet" + else if (on && m.running) note = "On" + else if (on && m.started) note = "On after a restart" + else if (on) note = "Starting" + else if (m.sleep_blocked) note = "Off after a restart. Until then the phone can't sleep." + else if (m.running || m.started) note = "Off after a restart." + else note = "Off. The phone can sleep; no calls, texts or mobile data." + return { shown: true, on: on, note: note, restart: m.restart_needed === true } +} // What the override field accepts: "" or "auto" (from the SIM), or a plain // carrier APN (the helper checks again as root). @@ -50,9 +71,21 @@ function view(s) { if (!s) return { state: "loading", title: "Mobile network", body: "Reading the modem..." } if (!s.installed) return { state: "none", title: "No modem", body: "This phone's modem software isn't installed." } + var modem = modemSwitch(s.modem) + // Switched on, and the modem or its phone service still coming up: say + // so, rather than the off text, until the service reports (about two + // minutes from the switch, as at boot). + var m = s.modem || {} + if (!s.running && modem.shown && modem.on && m.provisioned && (m.running || !m.started)) + return { state: "off", title: "Starting the modem", + body: "Registering with the network. This takes a minute or two.", + modem: modem } if (!s.running) return { state: "off", title: "Mobile network is off", - body: "The modem service isn't running. It starts with the phone once the modem is set up." } + body: modem.shown && !modem.on + ? "The modem is turned off." + : "The modem service isn't running. It starts with the phone once the modem is set up.", + modem: modem } var sim = s.sim || {} var simProblem = SIM[sim.state] !== undefined ? SIM[sim.state] : "Reading the SIM" var registered = s.registration === "home" || s.registration === "roaming" @@ -83,7 +116,8 @@ function view(s) { apnConfigured: apn.configured && apn.configured !== "auto" ? apn.configured : "", attachText: attachText(apn), attachConfigured: apn.attach_configured && apn.attach_configured !== "auto" ? apn.attach_configured : "", - pinText: pinText(sim) + pinText: pinText(sim), + modem: modem } } @@ -124,6 +158,7 @@ function apnSavedText(value, attach) { if (typeof module !== "undefined") { module.exports = { STATUS_ARGV: STATUS_ARGV, REBOOT_ARGV: REBOOT_ARGV, parseJson: parseJson, dataArgv: dataArgv, roamingArgv: roamingArgv, apnArgv: apnArgv, attachArgv: attachArgv, apnValue: apnValue, + modemArgv: modemArgv, modemSwitch: modemSwitch, attachText: attachText, view: view, signalText: signalText, pinText: pinText, apnSavedText: apnSavedText } } diff --git a/packages/omamobile-shell/plugins/omamobile.quicksettings/QuickSettings.qml b/packages/omamobile-shell/plugins/omamobile.quicksettings/QuickSettings.qml index 46c1d6757..a7e21db5e 100644 --- a/packages/omamobile-shell/plugins/omamobile.quicksettings/QuickSettings.qml +++ b/packages/omamobile-shell/plugins/omamobile.quicksettings/QuickSettings.qml @@ -1061,8 +1061,10 @@ Item { function toggle() { if (available) root.runTileAction("mobile-data", !enabled) } // The chevron (and a long press): the Mobile network page // (omamobile.mobilenetwork), through the CLI, since a plugin only - // summons its own panels. Shown with a modem only. - readonly property bool details: available + // summons its own panels. Shown with a modem, and with one the owner + // switched off on that page, which is where it is switched back on. + readonly property bool details: available || detailsUnavailable + readonly property bool detailsUnavailable: !available && root.probed.data_switch === "off" function openDetails() { root.dismiss() Quickshell.execDetached(["omarchy-shell", "shell", "summon", "omamobile.mobilenetwork", "{}"]) @@ -2113,6 +2115,7 @@ Item { available: !!backend && backend.available busy: !!backend && backend.busy === true details: !!backend && backend.details === true + detailsUnavailable: !!backend && backend.detailsUnavailable === true longPress: !!backend && backend.longPress === true onClicked: if (backend) backend.toggle() onDetailsRequested: if (backend && typeof backend.openDetails === "function") backend.openDetails() diff --git a/packages/omamobile-shell/plugins/omamobile.quicksettings/Tile.qml b/packages/omamobile-shell/plugins/omamobile.quicksettings/Tile.qml index 2f092e0ce..a6c42f1af 100644 --- a/packages/omamobile-shell/plugins/omamobile.quicksettings/Tile.qml +++ b/packages/omamobile-shell/plugins/omamobile.quicksettings/Tile.qml @@ -28,6 +28,8 @@ BorderSurface { property bool available: true property bool busy: false property bool details: false + // The chevron opens the page even while the tile can't toggle. + property bool detailsUnavailable: false property bool longPress: false property color foreground: Commons.Color.popups.text @@ -119,10 +121,12 @@ BorderSurface { } // The chevron sits above the toggle's TapHandler and takes its own presses. + // It is dimmed and disabled with the tile, except for a tile whose page is + // where it is turned back on (detailsUnavailable: a modem switched off). Item { id: chevron visible: root.details - opacity: root._dim + opacity: root.detailsUnavailable ? 1 : root._dim anchors.top: parent.top anchors.bottom: parent.bottom anchors.right: parent.right @@ -156,7 +160,7 @@ BorderSurface { MouseArea { id: chevronTouch anchors.fill: parent - enabled: root.available + enabled: root.available || root.detailsUnavailable cursorShape: Qt.PointingHandCursor onClicked: root.detailsRequested() } diff --git a/packages/omamobile-shell/plugins/omamobile.quicksettings/tile-state.sh b/packages/omamobile-shell/plugins/omamobile.quicksettings/tile-state.sh index b45b8707d..aa2ed2a15 100755 --- a/packages/omamobile-shell/plugins/omamobile.quicksettings/tile-state.sh +++ b/packages/omamobile-shell/plugins/omamobile.quicksettings/tile-state.sh @@ -24,6 +24,10 @@ # the modem this boot (a stale marker from a hang, a phone # not yet provisioned), so the tile can say why # data_signal the signal, 0-100, while registered; -1 otherwise +# data_switch off when the modem's switch (omamobile-modem-switch, the +# Mobile network page) has the modem off for this boot: the +# tile stays dimmed but keeps its chevron, so the page that +# turns it back on can be reached; absent otherwise # dictation voxtype: on while recording, busy while transcribing, off # when idle; down when its user service isn't running, none # without voxtype (in the root image; Remove > Dictation @@ -140,8 +144,24 @@ print("data_status=" + " ".join(words)) else echo "data=none" refused=${OMAMOBILE_MODEM_REFUSED:-/run/omamobile/modem-refused} + why= if [ -r "$refused" ]; then why=$(head -n1 "$refused" 2>/dev/null | head -c 48) - if [ -n "$why" ]; then echo "data_status=$why"; fi fi + # A modem the owner switched off is not "no modem": its page has the + # switch that turns it back on, so the tile keeps its chevron. + switch=${OMAMOBILE_MODEM_SWITCH:-/usr/lib/omamobile-vendor/omamobile-modem-switch} + if [ -x "$switch" ] && have python3 && + "$switch" status 2>/dev/null | python3 -I -c ' +import json, sys +try: + s = json.load(sys.stdin) +except ValueError: + sys.exit(1) +sys.exit(0 if isinstance(s, dict) and s.get("available") and not s.get("running") else 1) +'; then + echo "data_switch=off" + [ -n "$why" ] || why="Modem off" + fi + if [ -n "$why" ]; then echo "data_status=$why"; fi fi diff --git a/packages/omamobile-shell/test/mobile-network-model.js b/packages/omamobile-shell/test/mobile-network-model.js index f35014826..e88270a6d 100644 --- a/packages/omamobile-shell/test/mobile-network-model.js +++ b/packages/omamobile-shell/test/mobile-network-model.js @@ -22,6 +22,31 @@ test("no modem stack: the page says so and nothing else", () => { assert.strictEqual(M.view({ installed: true, running: false }).state, "off") }) +test("modem switch: hidden without the helper, states and restarts", () => { + assert.strictEqual(M.view(ready()).modem.shown, false) + const sw = (o) => Object.assign({ available: true, enabled: true, running: true, started: true, provisioned: true, + sleep_blocked: true, restart_needed: false }, o) + // Where nothing starts the modem at boot (the 9a, the 6 Pro): no switch. + assert.strictEqual(M.modemSwitch(sw({ available: false, enabled: false })).shown, false) + assert.strictEqual(M.modemSwitch({ enabled: true, running: true }).shown, false) + const noOff = M.view({ installed: true, running: false, modem: sw({ available: false, enabled: false, running: false, started: false }) }) + assert.strictEqual(noOff.modem.shown, false); assert.notStrictEqual(noOff.body, "The modem is turned off.") + assert.deepStrictEqual(M.modemSwitch(sw()), { shown: true, on: true, note: "On", restart: false }) + const offUp = M.modemSwitch(sw({ enabled: false, restart_needed: true })) + assert.strictEqual(offUp.on, false); assert.strictEqual(offUp.restart, true) + assert.ok(offUp.note.indexOf("after a restart") >= 0 && offUp.note.indexOf("can't sleep") >= 0) + // A start refused before any driver loaded released the sleep block. + assert.strictEqual(M.modemSwitch(sw({ enabled: false, running: false, sleep_blocked: false, restart_needed: true })).note, + "Off after a restart.") + assert.ok(M.modemSwitch(sw({ enabled: false, running: false, started: false, sleep_blocked: false })).note.indexOf("can sleep") >= 0) + assert.strictEqual(M.modemSwitch(sw({ running: false, started: false })).note, "Starting") + assert.strictEqual(M.modemSwitch(sw({ running: false, restart_needed: true })).note, "On after a restart") + assert.strictEqual(M.modemSwitch(sw({ provisioned: false })).note, "Not set up on this phone yet") + const off = M.view({ installed: true, running: false, modem: sw({ enabled: false, running: false, started: false, sleep_blocked: false }) }) + assert.strictEqual(off.state, "off"); assert.strictEqual(off.modem.shown, true); assert.strictEqual(off.body, "The modem is turned off.") + assert.deepStrictEqual(M.modemArgv(false), ["omamobile-mobile-network", "modem", "off"]) +}) + test("registered: carrier, LTE, data on, APN from the SIM", () => { const v = M.view(ready()) assert.strictEqual(v.title, "Example") @@ -80,6 +105,25 @@ test("attach APN: same as internet, or the carrier's", () => { assert.strictEqual(M.view(ready({ apn: { attach_configured: "ims", attach_in_use: "ims", attach_source: "manual" } })).attachConfigured, "ims") }) +test("modem switched on: the page says it is starting until the phone service reports", () => { + const base = { installed: true, running: false } + const sw = (m) => Object.assign({ available: true, enabled: true, provisioned: true, sleep_blocked: true, restart_needed: false }, m) + // Just switched on: the modem unit runs, the phone service doesn't yet + let v = M.view(Object.assign({}, base, { modem: sw({ running: true, started: true }) })) + assert.strictEqual(v.title, "Starting the modem") + // Switched on, the start not yet begun + v = M.view(Object.assign({}, base, { modem: sw({ running: false, started: false }) })) + assert.strictEqual(v.title, "Starting the modem") + // On, but the modem started and stopped this boot: not starting, the off text + v = M.view(Object.assign({}, base, { modem: sw({ running: false, started: true }) })) + assert.strictEqual(v.title, "Mobile network is off") + // Switched off, and not set up: the off texts as before + v = M.view(Object.assign({}, base, { modem: sw({ enabled: false, running: false, started: false, sleep_blocked: false }) })) + assert.strictEqual(v.body, "The modem is turned off.") + v = M.view(Object.assign({}, base, { modem: sw({ provisioned: false, running: false, started: false }) })) + assert.strictEqual(v.title, "Mobile network is off") +}) + let failed = 0 for (const [name, fn] of tests) { try { fn(); console.log("ok - " + name) } catch (e) { failed++; console.log("not ok - " + name + "\n " + e.message) } diff --git a/packages/omamobile-shell/test/mobile-network.py b/packages/omamobile-shell/test/mobile-network.py index c857ae860..7639ad7a9 100644 --- a/packages/omamobile-shell/test/mobile-network.py +++ b/packages/omamobile-shell/test/mobile-network.py @@ -58,6 +58,16 @@ def test_unregistered_shows_no_carrier_and_no_apn_means_unavailable(self): self.assertEqual((v['carrier'], v['tech'], v['data']['available'], v['roaming']['allowed']), ('', '', False, False)) + def test_modem_switch(self): + self.assertIsNone(mn.describe(True, None, CONF, False)['modem']) + sw = dict(available=True, enabled=False, running=True, started=True, provisioned=True, + sleep_blocked=True, restart_needed=True, extra='x') + want = dict(sw) + del want['extra'] + self.assertEqual(mn.describe(True, state(), CONF, True, switch=sw)['modem'], want) + # An older helper without "available": the page hides the switch. + self.assertFalse(mn.describe(True, state(), CONF, True, switch=dict(enabled=True))['modem']['available']) + def test_no_identifiers_in_the_view(self): text = json.dumps(mn.describe(True, state(), CONF, True)) self.assertNotIn('00101', text) @@ -80,9 +90,14 @@ def setUp(self): self.helper.write_text('#!/bin/sh\n') self.helper.chmod(0o755) (d / 'modem-sit.conf').write_text('[network]\napn = internet.example\n[data]\nroaming = no\n') + self.switch = d / 'omamobile-modem-switch' + self.switch.write_text('#!/bin/sh\n[ "$1" = status ] && echo \'{"available": true, "enabled": false, ' + '"running": false, "started": false, "provisioned": true, "sleep_blocked": false, ' + '"restart_needed": false}\'\n') + self.switch.chmod(0o755) self.env = dict(os.environ, OMAMOBILE_SIT_RUN=str(d / 'run'), OMAMOBILE_SIT_CONFIG=str(d / 'modem-sit.conf'), OMAMOBILE_SIT_CONFIG_HELPER=str(self.helper), OMAMOBILE_MM='none', - PATH=str(d / 'bin')) + OMAMOBILE_MODEM_SWITCH=str(self.switch), PATH=str(d / 'bin')) self.d = d def tearDown(self): @@ -97,6 +112,9 @@ def test_status(self): v = json.loads(self.run_tool('status').stdout) self.assertEqual((v['installed'], v['running'], v['data']['enabled']), (True, True, False)) self.assertEqual((v['apn']['configured'], v['roaming']['allowed']), ('internet.example', False)) + self.assertEqual((v['modem']['enabled'], v['modem']['provisioned']), (False, True)) + self.switch.unlink() + self.assertIsNone(json.loads(self.run_tool('status').stdout)['modem']) def test_not_installed(self): self.helper.unlink() @@ -110,12 +128,17 @@ def test_changes(self): self.run_tool('roaming', 'off') self.run_tool('apn', 'internet.example') self.run_tool('attach-apn', 'ims') + self.run_tool('modem', 'off') + self.run_tool('modem', 'on') self.assertEqual(self.log.read_text().splitlines(), [ 'omamobile-sit data on', f'pkexec {self.helper} set roaming off', f'pkexec {self.helper} set apn internet.example', - f'pkexec {self.helper} set attach_apn ims']) + f'pkexec {self.helper} set attach_apn ims', + f'pkexec {self.switch} off', + f'pkexec {self.switch} on']) self.assertEqual(self.run_tool('data', 'maybe').returncode, 2) + self.assertEqual(self.run_tool('modem', 'restart').returncode, 2) if __name__ == '__main__': diff --git a/packages/omamobile-shell/test/tile-scripts.sh b/packages/omamobile-shell/test/tile-scripts.sh index 9fba2c135..0bb324e2f 100755 --- a/packages/omamobile-shell/test/tile-scripts.sh +++ b/packages/omamobile-shell/test/tile-scripts.sh @@ -138,6 +138,30 @@ expect "$out" "data_status=Off after a hang or reset" "the start script's reason printf '%s\n' "$(printf 'x%.0s' $(seq 60))" > "$tmp/state/refused" out=$(no_modem) expect "$out" "data_status=$(printf 'x%.0s' $(seq 48))" "the reason is cut to 48 characters" +# The modem switched off for this boot on the Mobile network page: still +# no modem for the tile, but data_switch=off keeps the chevron to the page +# that turns it back on, and the status says so. A switch that reports the +# modem running, not available, or nothing readable adds nothing. +rm -f "$tmp/state/refused" +mkdir -p "$tmp/switch" +switch_says() { printf '#!/bin/sh\necho %s\n' "'$1'" > "$tmp/switch/omamobile-modem-switch"; chmod +x "$tmp/switch/omamobile-modem-switch"; } +switched() { env -i HOME="$tmp/home" XDG_RUNTIME_DIR="$tmp/run" OMAMOBILE_SIT_STATE="$tmp/state/missing.json" \ + OMAMOBILE_MODEM_REFUSED="$tmp/state/refused" OMAMOBILE_MODEM_SWITCH="$tmp/switch/omamobile-modem-switch" \ + PATH="$fake:$basic" sh "$dir/tile-state.sh"; } +switch_says '{"available": true, "enabled": false, "running": false, "started": false}' +out=$(switched) +expect "$out" "data=none" "a modem switched off is still no modem for the tile" +expect "$out" "data_switch=off" "a modem switched off keeps the page's chevron" +expect "$out" "data_status=Modem off" "a modem switched off says so" +printf 'Off after a hang or reset\n' > "$tmp/state/refused" +out=$(switched) +expect "$out" "data_status=Off after a hang or reset" "a refused start's reason wins over the switch's" +rm -f "$tmp/state/refused" +for says in '{"available": true, "running": true}' '{"available": false, "running": false}' 'not json'; do + switch_says "$says" + out=$(switched) + [[ $(grep -c '^data_' <<<"$out") -eq 0 ]] || fail "the switch saying $says adds nothing (got: $(tr '\n' ' ' <<<"$out"))" +done : > "$tmp/state/refused" out=$(no_modem) [[ $(grep -c '^data_' <<<"$out") -eq 0 ]] || fail "an empty reason file prints no status (got: $(tr '\n' ' ' <<<"$out"))" diff --git a/packages/omamobile-shell/version b/packages/omamobile-shell/version index 4f079a5e5..97d931a2b 100644 --- a/packages/omamobile-shell/version +++ b/packages/omamobile-shell/version @@ -1 +1 @@ -0.2.127 +0.2.128