#!/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" 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", ) EVENT = BASE / "overlay/rootfs/usr/bin/bananarchy-cover-event.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 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") 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: return fail("the End/Power key is not watched while the flip is closed") 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("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 devices are 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())