#!/usr/bin/env python3 """Host test for the 2720 Flip instant lid handling and closed-lid gate. Checks the compiled lid daemon's event-driven structure, that every main-panel wake path is gated on /run/bananarchy/lid-state, and that the gate forwards a blocked wake to the cover display instead of the main panel. """ import os import pathlib import subprocess import sys import tempfile BASE = pathlib.Path(__file__).resolve().parent.parent LID_SRC = BASE / "src/bananarchy-daemons/bananarchy-lid.c" COVER_SRC = BASE / "src/bananarchy-daemons/bananarchy-cover-idle.c" LOCKER_SRC = BASE / "src/dwm-plugins/locker_plugin.cpp" CALLSCREEN_SRC = BASE / "src/dwm-plugins/callscreen_plugin.cpp" GATE = BASE / "overlay/rootfs/usr/bin/bananarchy-lid-gate.sh" LID_WRAPPER = BASE / "overlay/rootfs/usr/bin/bananarchy-lid-state.sh" COMMON = BASE / "src/bananarchy-daemons/common.h" COVER_CONF = BASE / "overlay/rootfs/etc/bananarchy/cover-display.conf" PROBE_SH = BASE / "overlay/rootfs/usr/bin/bananarchy-cover-probe.sh" WM = BASE / "overlay/rootfs/usr/bin/sxmo_wm.sh" STATE_SH = BASE / "overlay/rootfs/usr/bin/sxmo_state.sh" WAKE = BASE / "overlay/rootfs/usr/bin/bananarchy-screen-wake.sh" DPMS_SYNC = BASE / "overlay/rootfs/usr/bin/bananarchy-display-power-sync" INPUT_HOOKS = ( BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_inputhandler.sh", BASE / "overlay/rootfs/etc/skel/.config/sxmo/hooks/sxmo_hook_inputhandler.sh", BASE / "overlay/rootfs/home/user/.config/sxmo/hooks/sxmo_hook_inputhandler.sh", ) EVENT = BASE / "overlay/rootfs/usr/bin/bananarchy-cover-event.sh" LOCK_HOOK = BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_lock.sh" HOOKS = ( BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_ring.sh", BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_notification.sh", BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_missed_call.sh", BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_pickup.sh", BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_hangup.sh", ) def fail(message): sys.stderr.write("FAIL: %s\n" % message) return 1 def run_gate(profile, state, arg="", stub=None): env = dict(os.environ) env["BANANARCHY_LID_PROFILE"] = str(profile) env["BANANARCHY_LID_STATE"] = str(state) if stub: env["BANANARCHY_COVERCTL"] = str(stub) return subprocess.run([str(GATE)] + ([arg] if arg else []), env=env, capture_output=True, text=True) def main(): paths = [LID_SRC, COVER_SRC, COMMON, COVER_CONF, PROBE_SH, GATE, LID_WRAPPER, WM, STATE_SH, WAKE, DPMS_SYNC, EVENT, *INPUT_HOOKS, *HOOKS] for path in paths: if not path.is_file(): return fail("missing %s" % path.relative_to(BASE)) lid = LID_SRC.read_text(encoding="utf-8") for token in ("LID_DEBOUNCE_MS 20", "HALL_EDGE_MS 20", "LID_LOOP_MS 20", "hall_observed", "fast_until", "write_session_file", "write_lid_state(closed)", 'write_session_file(state, "screenoff")', 'write_session_file(state, "lock")', 'cover_send("OPEN")', 'locker_send("LOCK\\n")', 'run_sxmo_state("lock", "open")', "read_ready(&list, fds, nfds, &switch_mask, &edge)", "LID_GENERATION_FILE", "run_dpms", "bananarchy-lid-state.sh"): if token not in lid: return fail("lid daemon lost instant handling: %s" % token) if "unlink(LOCK_FLAG)" in lid: return fail("opening the flip still auto-unlocks the desktop") # The cover daemon must be told to release the bus before the panel is lit. if lid.index('cover_send("OPEN")') > lid.index('write_sysfs_int(BRIGHTNESS, bl)'): return fail("the SPI bus is released after the main panel is relit") lid_close = lid[lid.index('if(closed) {'):lid.index(' } else {')] if lid_close.index('write_session_file(LOCK_FLAG, "1")') > \ lid_close.index('write_text_file(BRIGHTNESS, "0")'): return fail("the screen is locked after the panel is blanked") lid_open = lid[lid.index(' } else {'):] if 'write_session_file(LOCK_FLAG, "1")' not in lid_open or \ 'write_session_file(state, "lock")' not in lid_open: return fail("opening the flip does not keep the desktop locked") if 'write_session_file(state, "lock")' in lid_open and \ lid_open.index('write_session_file(state, "lock")') > \ lid_open.index('write_sysfs_int(BRIGHTNESS, bl)'): return fail("the desktop lock is published after the panel is relit") if 'callscreen_send("RAISE\\n")' not in lid_open: return fail("opening the flip does not raise active/incoming callscreen") print("[ok] lid daemon: instant close, persistent lock and bus handover") scratch = pathlib.Path("/tmp/bantam/9064bb701d9e9f7c6412f0ea6846d88a") scratch.mkdir(parents=True, exist_ok=True) build = subprocess.run( ["gcc", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", "-I" + str(LID_SRC.parent), str(LID_SRC), "-o", str(scratch / "lid-gate-host")], capture_output=True, text=True) if build.returncode: sys.stderr.write(build.stderr) return fail("lid daemon does not compile on the host") print("[ok] host build of the compiled lid daemon") wrapper = LID_WRAPPER.read_text(encoding="utf-8") if "lid-generation" not in wrapper or "lid-state" not in wrapper: return fail("the lid hook wrapper is not guarded by lid state/generation") for path in (GATE, LID_WRAPPER, WM, STATE_SH, WAKE, DPMS_SYNC, EVENT, *INPUT_HOOKS, *HOOKS): if subprocess.run(["sh", "-n", str(path)], capture_output=True).returncode: return fail("shell syntax: %s" % path.relative_to(BASE)) for path in (WM, STATE_SH, WAKE, DPMS_SYNC, *INPUT_HOOKS): if "bananarchy-lid-gate.sh" not in path.read_text(encoding="utf-8"): return fail("%s is not gated on the flip lid" % path.relative_to(BASE)) if "bananarchy-cover-event.sh ring" not in HOOKS[0].read_text(): return fail("ring hook does not publish the call to the cover") if "bananarchy-cover-event.sh notification" not in HOOKS[1].read_text(): return fail("notification hook does not publish to the cover") event = EVENT.read_text(encoding="utf-8") if " wake)" not in event or "coverctl WAKE" not in event: return fail("cover event bridge lost the wake forwarding case") common = COMMON.read_text(encoding="utf-8") if "device_has_code(fd, EV_KEY, KEY_POWER)" not in common or \ "device_has_code(fd, EV_KEY, KEY_CAMERA)" not in common: return fail("the outer Power/Camera keys are not watched while closed") locker = LOCKER_SRC.read_text(encoding="utf-8") if "isLidClosed()" not in locker or \ "if (!visible || isLidClosed())" not in locker or \ 'runAsyncArgv("/usr/bin/bananarchy-lid-gate.sh", "wake")' not in locker: return fail("locker_plugin.cpp is not gated on /run/bananarchy/lid-state") if "if (blankDisplay && !alreadyClosedBlanked)" not in locker or \ locker.index("if (blankDisplay && !alreadyClosedBlanked)") > locker.index("HANGUP_CLOSE") or \ "if (hadActiveCall)" not in locker or \ 'sendPluginCommand("callscreen", "RAISE")' not in locker: return fail("locker_plugin.cpp still hangs up active calls on non-blank LOCK/open or closed-flip blank") if "cancel_idle_lock_timer()" not in lid: return fail("bananarchy-lid.c does not cancel pending idle-lock timers on flip transitions") lock_hook = LOCK_HOOK.read_text(encoding="utf-8") if "HANGUP_CLOSE" in lock_hook or "hangup all" in lock_hook: return fail("sxmo_hook_lock.sh unconditionally hangs up calls on lock/flip-open") callscreen = CALLSCREEN_SRC.read_text(encoding="utf-8") if "/run/bananarchy/lid-state" not in callscreen or \ 'runAsyncArgv("/usr/bin/bananarchy-lid-gate.sh", "wake")' not in callscreen or \ "isFlipClosed()" not in callscreen or \ "notifyCoverCall(" not in callscreen or \ "callState == CallState::Idle && !isCallActiveOrRingingFast()" not in callscreen or \ "lastRejectedCallId" not in callscreen or \ "syncModemCalls();" not in callscreen: return fail("callscreen_plugin.cpp is missing lid-gate, idle HANGUP_CLOSE guard, or stale-call RAISE verification") conf = COVER_CONF.read_text(encoding="utf-8") if "spi_word_bits" not in conf: return fail("the cover config lost the 8/9-bit sign-off knob") cover = COVER_SRC.read_text(encoding="utf-8") if "cover_wake" not in cover or 'strcmp(command, "WAKE")' not in cover: return fail("cover daemon lost the WAKE command") print("[ok] every main-panel wake path is gated and forwarded to the cover") with tempfile.TemporaryDirectory(prefix="bananarchy-lid-gate-") as tmp: root = pathlib.Path(tmp) profile = root / "lid-profile" state = root / "lid-state" calls = root / "coverctl.calls" stub = root / "coverctl" stub.write_text('#!/bin/sh\nprintf "%s\\n" "$*" >> "%s"\n' % ("%s", calls)) stub.chmod(0o755) profile.write_text("slider\n") state.write_text("closed\n") if run_gate(profile, state, "wake", stub).returncode != 0: return fail("nokia-argon slider device was blocked by the flip lid gate") if calls.exists() and calls.read_text().strip(): return fail("nokia-argon slider device forwarded a wake to coverctl") lid_state_out = root / "daemon-lid-state" lid_env = dict(os.environ) lid_env["BANANARCHY_LID_PROFILE"] = str(profile) lid_env["BANANARCHY_LID_STATE"] = str(lid_state_out) proc = subprocess.run([str(scratch / "lid-gate-host")], env=lid_env, capture_output=True, text=True, timeout=2) if proc.returncode != 0 or lid_state_out.exists(): return fail("compiled lid daemon stayed active on a slider device") overlay_sh = (BASE / "scripts/apply-bananarchy-overlay.sh").read_text(encoding="utf-8") for token in ('LID_PROFILE="slider"', 'LID_PROFILE="flip"', 'LID_PROFILE="none"', 'printf \'%s\\n\' "$LID_PROFILE" > "$TARGET_ROOTFS/usr/share/bananarchy/lid-profile"', '"$TARGET_ROOTFS/etc/runlevels/default/bananarchy-lid"', '"$TARGET_ROOTFS/etc/runlevels/default/bananarchy-cover-display"', '"$TARGET_ROOTFS/etc/runlevels/default/bananarchy-volume-keys"'): if token not in overlay_sh: return fail("apply-bananarchy-overlay.sh lost target lid isolation: %s" % token) if '"/usr/share/bananarchy/lid-profile"' not in locker or \ '"/usr/share/bananarchy/lid-profile"' not in callscreen or \ 'LID_PROFILE=' not in DPMS_SYNC.read_text(encoding="utf-8"): return fail("plugins or display-power-sync lost the flip lid-profile check") profile.write_text("bar\n") state.write_text("closed\n") if run_gate(profile, state, stub=stub).returncode != 0: return fail("a non-flip device was blocked by the lid gate") print("[ok] non-flip / slider devices are completely unaffected") profile.write_text("flip\n") state.write_text("open\n") if run_gate(profile, state, "wake", stub).returncode != 0: return fail("an open flip was blocked by the lid gate") if calls.exists() and calls.read_text().strip(): return fail("an open flip forwarded a wake to the cover") print("[ok] open flip: main panel allowed, no cover wake") state.write_text("closed\n") if run_gate(profile, state, stub=stub).returncode != 1: return fail("a closed flip was allowed to light the main panel") if calls.exists() and calls.read_text().strip(): return fail("a non-wake gate call forwarded to the cover") if run_gate(profile, state, "wake", stub).returncode != 1: return fail("a closed flip wake was not blocked") if "WAKE" not in calls.read_text(): return fail("a blocked main-panel wake was not forwarded to the cover") print("[ok] closed flip: main panel blocked, WAKE forwarded to cover") state.write_text("unknown\n") if run_gate(profile, state, "wake", stub).returncode != 0: return fail("a missing/unknown lid state blocked the main panel") print("[ok] unknown lid state fails open for the main panel") print("instant flip lid handling: all checks passed") return 0 if __name__ == "__main__": sys.exit(main())