diff --git a/packages/omamobile-shell/bin/omamobile-nfc-open b/packages/omamobile-shell/bin/omamobile-nfc-open index 734ea024f..cd9311e03 100644 --- a/packages/omamobile-shell/bin/omamobile-nfc-open +++ b/packages/omamobile-shell/bin/omamobile-nfc-open @@ -15,6 +15,11 @@ told when the kernel blocks the radio, so on a block the adapter is powered off through neard (on a kernel that already did, neard's view stays stale), and on an unblock neard is restarted: it finds the adapter again, powers it (DefaultPowered), and a new poll loop starts. + +And it polls only while the screen is lit, as stock Android does: with every +display connector off (DPMS) for SCREEN_OFF_SECONDS, the poll loop stops, +and it starts again when the screen comes on. A poll loop is the radio +searching for cards, which costs the Pixel 9a about 60 mA. """ import glob import subprocess @@ -30,6 +35,7 @@ NEARD = "org.neard" RECORD = "org.neard.Record" ADAPTER = "org.neard.Adapter" REPEAT_SECONDS = 3.0 +SCREEN_OFF_SECONDS = 4 last = {"uri": None, "time": 0.0} @@ -80,7 +86,10 @@ def on_interfaces_added(_conn, _sender, _path, _iface, _signal, params, _data): def start_polling(bus): """Power the first adapter and start a poll loop (neard's ConstantPoll - keeps it going after each tag).""" + keeps it going after each tag). True means "try again": neard isn't + answering, has no adapter yet, or refused the power-on.""" + if rfkill["blocked"]: + return False # switched off meanwhile; the next unblock restarts neard try: objects = bus.call_sync(NEARD, "/", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects", None, None, @@ -93,10 +102,18 @@ def start_polling(bus): if adapter is None: continue if not adapter.get("Powered"): - bus.call_sync(NEARD, path, "org.freedesktop.DBus.Properties", "Set", - GLib.Variant("(ssv)", (ADAPTER, "Powered", GLib.Variant("b", True))), - None, Gio.DBusCallFlags.NONE, -1, None) - if not adapter.get("Polling"): + # This runs from GLib timeouts too: an exception would remove the + # timer for good, so a failed power-on is logged and retried. + try: + bus.call_sync(NEARD, path, "org.freedesktop.DBus.Properties", "Set", + GLib.Variant("(ssv)", (ADAPTER, "Powered", GLib.Variant("b", True))), + None, Gio.DBusCallFlags.NONE, -1, None) + except GLib.Error as error: + log(f"{path}: Powered=True: {error.message}") + return True + if not adapter.get("Polling") and not screen["lit"]: + log(f"screen off: {path} powered, polling waits for the screen") + elif not adapter.get("Polling"): try: bus.call_sync(NEARD, path, ADAPTER, "StartPollLoop", GLib.Variant("(s)", ("Initiator",)), None, @@ -141,6 +158,65 @@ def set_powered(bus, powered): rfkill = {"blocked": None} +screen = {"lit": True, "dark_for": 0, "start": False} + + +def screen_lit(drm="/sys/class/drm"): + """True unless every connected display connector reports DPMS off.""" + states = [] + for path in glob.glob(f"{drm}/card*-*"): + try: + with open(f"{path}/status") as f: + if f.read().strip() != "connected": + continue + with open(f"{path}/dpms") as f: + states.append(f.read().strip()) + except OSError: + continue + return not states or any(state == "On" for state in states) + + +def stop_polling(bus): + try: + objects = bus.call_sync(NEARD, "/", "org.freedesktop.DBus.ObjectManager", + "GetManagedObjects", None, None, + Gio.DBusCallFlags.NONE, -1, None).unpack()[0] + except GLib.Error: + return + for path, interfaces in objects.items(): + adapter = interfaces.get(ADAPTER) + if adapter is None or not adapter.get("Polling"): + continue + try: + bus.call_sync(NEARD, path, ADAPTER, "StopPollLoop", None, None, + Gio.DBusCallFlags.NONE, -1, None) + log(f"screen off: polling stopped on {path}") + except GLib.Error as error: + log(f"could not stop polling on {path}: {error.message}") + + +def follow_screen(bus): + """Every 2 s: once the screen has been off for a while, stop any poll loop + (each time, so one that neard, a restart or the sleep hook started while + the screen was dark stops too); start it again when the screen lights + (unless NFC is switched off).""" + lit = screen_lit() + if lit: + screen["dark_for"] = 0 + if not screen["lit"]: + screen["lit"] = True + screen["start"] = not rfkill["blocked"] + if screen["start"]: + log("screen on: polling again") + if screen["start"]: + screen["start"] = start_polling(bus) + return True + screen["dark_for"] += 2 + if screen["dark_for"] >= SCREEN_OFF_SECONDS: + screen["lit"] = False + if not rfkill["blocked"]: + stop_polling(bus) + return True def follow_rfkill(bus): @@ -172,6 +248,7 @@ def main(): # earlier run of it): start from a fresh neard. restart_neard(bus) GLib.timeout_add_seconds(2, follow_rfkill, bus) + GLib.timeout_add_seconds(2, follow_screen, bus) GLib.MainLoop().run() diff --git a/packages/omamobile-shell/test/all b/packages/omamobile-shell/test/all index bc9f6533a..b0253eb06 100755 --- a/packages/omamobile-shell/test/all +++ b/packages/omamobile-shell/test/all @@ -469,6 +469,10 @@ grep -c 'Quickshell.execDetached(HomeModel.urlLaunchArgv(text))' plugins/omamobi grep -c 'HomeModel.commandLaunchArgv(\["sh", "-c", tap\])' plugins/omamobile.home/CommandWidget.qml >/dev/null || fail "the text widget's tap goes through omamobile-launch" grep -c '"omamobile-launch", "url", uri' bin/omamobile-nfc-open >/dev/null || fail "NFC links open through omamobile-launch" grep -c 'GLib.timeout_add_seconds(2, follow_rfkill, bus)' bin/omamobile-nfc-open >/dev/null || fail "the NFC service follows the rfkill switch" +grep -c 'GLib.timeout_add_seconds(2, follow_screen, bus)' bin/omamobile-nfc-open >/dev/null || fail "the NFC service stops polling with the screen off" +python3 -B test/nfc-screen.py >/dev/null || { python3 -B test/nfc-screen.py | grep -v '^ok'; fail "screen_lit reads the connectors"; } +grep -c 'polling waits for the screen' bin/omamobile-nfc-open >/dev/null || fail "start_polling leaves the loop off while the screen is dark" +grep -c 'timeout_add_seconds(4' bin/omamobile-nfc-open >/dev/null && fail "the one-shot stop is gone: the screen check stops polling every time" grep -c '"systemctl", "restart", "neard"' bin/omamobile-nfc-open >/dev/null || fail "an NFC unblock restarts neard" grep -c '^Requires=neard' systemd/omamobile-nfc-open.service >/dev/null && fail "omamobile-nfc-open restarts neard, so it must not Require= it (the restart would loop)" grep -c '"omamobile-launch", "empty"' bin/omamobile-widget >/dev/null || fail "the widget agent's terminal gets an empty workspace" diff --git a/packages/omamobile-shell/test/nfc-screen.py b/packages/omamobile-shell/test/nfc-screen.py new file mode 100644 index 000000000..0c0f63010 --- /dev/null +++ b/packages/omamobile-shell/test/nfc-screen.py @@ -0,0 +1,31 @@ +# SPDX-License-Identifier: MIT +# omamobile-nfc-open's screen_lit() against temporary sysfs trees: connected +# and disconnected connectors, DPMS On and Off, and no connectors at all. +import importlib.machinery, importlib.util, os, sys, tempfile, types +sys.dont_write_bytecode = True # never a __pycache__ next to bin/omamobile-nfc-open + +gi = types.ModuleType("gi"); gi.require_version = lambda *a: None +repo = types.ModuleType("gi.repository"); repo.Gio = repo.GLib = types.SimpleNamespace() +sys.modules.update({"gi": gi, "gi.repository": repo}) +loader = importlib.machinery.SourceFileLoader("nfc_open", os.path.join(os.path.dirname(__file__), "..", "bin", "omamobile-nfc-open")) +nfc = importlib.util.module_from_spec(importlib.util.spec_from_loader("nfc_open", loader)); loader.exec_module(nfc) + +fails = 0 +def case(name, connectors, want): + global fails + with tempfile.TemporaryDirectory() as drm: + for conn, (status, dpms) in connectors.items(): + os.makedirs(f"{drm}/{conn}") + open(f"{drm}/{conn}/status", "w").write(status + "\n") + open(f"{drm}/{conn}/dpms", "w").write(dpms + "\n") + got = nfc.screen_lit(drm) + print(("ok" if got == want else "not ok") + f" - {name}") + fails += got != want + +case("panel on", {"card1-DSI-1": ("connected", "On")}, True) +case("panel off", {"card1-DSI-1": ("connected", "Off")}, False) +case("panel off, a disconnected output on", {"card1-DSI-1": ("connected", "Off"), "card1-DP-1": ("disconnected", "On")}, False) +case("two connected, one on", {"card1-DSI-1": ("connected", "Off"), "card1-HDMI-A-1": ("connected", "On")}, True) +case("only a disconnected output", {"card1-DP-1": ("disconnected", "Off")}, True) +case("no connectors at all", {}, True) +sys.exit(1 if fails else 0) diff --git a/packages/omamobile-shell/version b/packages/omamobile-shell/version index 16c9e92a2..16eb842d8 100644 --- a/packages/omamobile-shell/version +++ b/packages/omamobile-shell/version @@ -1 +1 @@ -0.2.45 +0.2.48