diff --git a/packages/omamobile-shell/bin/omamobile-nfc-open b/packages/omamobile-shell/bin/omamobile-nfc-open index 8e414ba66..734ea024f 100644 --- a/packages/omamobile-shell/bin/omamobile-nfc-open +++ b/packages/omamobile-shell/bin/omamobile-nfc-open @@ -9,7 +9,14 @@ in the active graphical session through that user's systemd manager (which has the session's WAYLAND_DISPLAY), with omamobile-launch: on the browser's own screen, as every launch on the phone. A tag that stays on the phone is read once; neard reads it again only after it is lifted and tapped again. + +It also follows NFC's rfkill switch (the quick-settings tile). neard isn't +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. """ +import glob import subprocess import sys import time @@ -101,12 +108,70 @@ def start_polling(bus): return True +def nfc_blocked(): + """True, False, or None when there is no NFC rfkill switch.""" + for path in glob.glob("/sys/class/rfkill/rfkill*"): + try: + with open(f"{path}/type") as f: + if f.read().strip() != "nfc": + continue + with open(f"{path}/soft") as f: + return f.read().strip() == "1" + except OSError: + continue + return None + + +def set_powered(bus, powered): + 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(): + if ADAPTER not in interfaces: + continue + try: + bus.call_sync(NEARD, path, "org.freedesktop.DBus.Properties", "Set", + GLib.Variant("(ssv)", (ADAPTER, "Powered", GLib.Variant("b", powered))), + None, Gio.DBusCallFlags.NONE, -1, None) + except GLib.Error as error: + log(f"{path}: Powered={powered}: {error.message}") + + +rfkill = {"blocked": None} + + +def follow_rfkill(bus): + blocked = nfc_blocked() + if blocked is None or blocked == rfkill["blocked"]: + return True + previous, rfkill["blocked"] = rfkill["blocked"], blocked + if blocked: + log("NFC switched off") + set_powered(bus, False) + elif previous is not None: + log("NFC switched on: restarting neard") + restart_neard(bus) + return True + + +def restart_neard(bus): + subprocess.run(["systemctl", "restart", "neard"], check=False) + GLib.timeout_add_seconds(2, start_polling, bus) + + def main(): bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None) bus.signal_subscribe(NEARD, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded", None, None, Gio.DBusSignalFlags.NONE, on_interfaces_added, None) - if start_polling(bus): - GLib.timeout_add_seconds(2, start_polling, bus) + rfkill["blocked"] = nfc_blocked() + if not rfkill["blocked"]: + # neard's view may be stale (a block before this service ran, or an + # earlier run of it): start from a fresh neard. + restart_neard(bus) + GLib.timeout_add_seconds(2, follow_rfkill, bus) GLib.MainLoop().run() diff --git a/packages/omamobile-shell/systemd/omamobile-nfc-open.service b/packages/omamobile-shell/systemd/omamobile-nfc-open.service index 4b958fe40..3b4efed71 100644 --- a/packages/omamobile-shell/systemd/omamobile-nfc-open.service +++ b/packages/omamobile-shell/systemd/omamobile-nfc-open.service @@ -1,7 +1,9 @@ # SPDX-License-Identifier: MIT [Unit] Description=Open web links from NFC tags in the user's browser -Requires=neard.service +# Wants=, not Requires=: this service restarts neard, and with Requires= that +# restart would restart this service too, over and over. +Wants=neard.service After=neard.service [Service] diff --git a/packages/omamobile-shell/test/all b/packages/omamobile-shell/test/all index 8627102da..37dc8b355 100755 --- a/packages/omamobile-shell/test/all +++ b/packages/omamobile-shell/test/all @@ -375,6 +375,9 @@ grep -c 'Quickshell.execDetached(HomeModel.commandLaunchArgv(argv))' plugins/oma grep -c 'Quickshell.execDetached(HomeModel.urlLaunchArgv(text))' plugins/omamobile.home/WidgetHost.qml >/dev/null || fail "widgets' host.openUrl goes through omamobile-launch" 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 '"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" grep -c '"omamobile-launch", "show-with"' plugins/omamobile.switcher/Switcher.qml >/dev/null || fail "the switcher shows apps together" grep -c '"omamobile-launch", "own-screen"' plugins/omamobile.switcher/Switcher.qml >/dev/null || fail "the switcher moves apps to their own screen" diff --git a/packages/omamobile-shell/version b/packages/omamobile-shell/version index f2ba2e8fa..a8498651b 100644 --- a/packages/omamobile-shell/version +++ b/packages/omamobile-shell/version @@ -1 +1 @@ -0.2.30 +0.2.32