From 247028053637846b8aa27f30c83194c422487a0a Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Tue, 27 Oct 2026 05:14:35 -0647 Subject: [PATCH] omamobile-shell 0.2.32: the NFC tile really switches NFC The tile blocks NFC through rfkill, but neard isn't told when the kernel takes the radio down, and the kernel refused to take it down at all while polling (kernel PR #71). Now omamobile-nfc-open follows the rfkill switch: a block powers the adapter off through neard, an unblock restarts neard so it powers the adapter again and polling resumes, and the service starts from a fresh neard. The service pulls neard in with Wants=, not Requires=: with Requires= its own neard restart would restart it too, and the new instance would restart neard again, until neard hit its start limit. The package test fails if Requires=neard comes back. --- .../omamobile-shell/bin/omamobile-nfc-open | 69 ++++++++++++++++++- .../systemd/omamobile-nfc-open.service | 4 +- packages/omamobile-shell/test/all | 3 + packages/omamobile-shell/version | 2 +- 4 files changed, 74 insertions(+), 4 deletions(-) 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