#!/usr/bin/env python3 """Host test for the 2720 Flip closed-flip-only external panel backend. Compiles bananarchy-cover-idle.c on the host and runs it in SPI simulation mode (BANANARCHY_COVER_SPI_SIM). The shared QUP5 bus must be untouched while the flip is open, claimed only while closed, released before Xorg can flush another main-panel frame, and the external firmware must be replayed before the first frame. Polarity knobs and the /dev/fb1 precedence are also checked. """ import os import pathlib import socket import subprocess import sys import tempfile import time BASE = pathlib.Path(__file__).resolve().parent.parent SRC = BASE / "src/bananarchy-daemons/bananarchy-cover-idle.c" FIRMWARE = BASE / "devices/nokia-beatles/nokia,beatles-gc9305-ext-panel.bin" CONFIG = BASE / "overlay/rootfs/etc/bananarchy/cover-display.conf" class Cover: def __init__(self, root, exe, config, log, env_extra=None): self.root = root self.log = log self.sock_path = root / "cover.sock" self.client_path = root / "client.sock" self.env = dict(os.environ) self.env.update({ "BANANARCHY_COVER_LOCK": str(root / "daemon.lock"), "BANANARCHY_COVER_MAIN_BLANK_CFG": str(root / "idle_blank_timeout"), "BANANARCHY_COVER_SPI_SIM": str(log), }) if env_extra: self.env.update(env_extra) self.proc = subprocess.Popen([str(exe), "--config", str(config)], env=self.env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) if self.client_path.exists(): self.client_path.unlink() self.client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) self.client.bind(str(self.client_path)) self.client.settimeout(2) deadline = time.time() + 3 while time.time() < deadline and not self.sock_path.exists(): time.sleep(0.05) def send(self, command): self.client.sendto(command.encode(), str(self.sock_path)) try: return self.client.recv(512).decode().strip() except socket.timeout: return "" def events(self): if not self.log.is_file(): return [] return self.log.read_text(encoding="utf-8", errors="replace").splitlines() def wait_event(self, text, timeout=3.0): end = time.time() + timeout while time.time() < end: if any(text in line for line in self.events()): return True time.sleep(0.05) return False def close(self): try: self.send("QUIT") except OSError: pass self.client.close() try: self.proc.wait(timeout=3) except subprocess.TimeoutExpired: self.proc.kill() self.proc.wait() if self.client_path.exists(): self.client_path.unlink() def write_config(path, root, extra): lines = [ "socket_path = %s" % (root / "cover.sock"), "fb_device = %s" % (root / "no-fb1"), "backlight_path = %s" % (root / "backlight"), "brightness = 160", "blank_timeout = 2", "backend = spidev", "spi_device = /dev/spidev0.1", "gpiochip_device = /dev/gpiochip0", "panel_firmware = %s" % FIRMWARE, "spi_mux_gpio = 13", "spi_dc_gpio = 21", "spi_reset_gpio = 23", "mux_closed_level = 1", "mux_open_level = 0", "dc_command_level = 0", "dc_data_level = 1", "reset_assert_level = 0", "reset_idle_level = 1", "spi_speed_hz = 25000000", ] lines.extend("%s = %s" % item for item in extra.items()) path.write_text("\n".join(lines) + "\n", encoding="utf-8") def check(ok, message): if not ok: sys.stderr.write("FAIL: %s\n" % message) return False return True def run_case(root, exe, extra, prepare=None, env_extra=None, dpms="Off"): case = root / ("case%d" % len(list(root.glob("case*")))) case.mkdir() log = case / "spi.log" conf = case / "cover.conf" (case / "idle_blank_timeout").write_text("300\n") (case / "dpms").write_text(dpms + "\n") write_config(conf, case, extra) if prepare: prepare(case) env = {"BANANARCHY_COVER_DPMS_PATH": str(case / "dpms")} if env_extra: env.update(env_extra) return Cover(case, exe, conf, log, env), case def main(): if not SRC.is_file() or not FIRMWARE.is_file() or not CONFIG.is_file(): sys.stderr.write("FAIL: missing cover sources or firmware\n") return 1 source = SRC.read_text(encoding="utf-8") for token in ("cover_hw_release", "cover_spi_draw", "hw_init_panel", "mux_closed_level", "spi_speed_hz", "FBIOBLANK", "BANANARCHY_COVER_SPI_SIM", "cover_main_panel_is_off", "cover_wake", "pending_until", "probe_cases", "spi_word_bits", "0x28", "0x29"): if token not in source: sys.stderr.write("FAIL: cover daemon lost %s\n" % token) return 1 conf = CONFIG.read_text(encoding="utf-8") for token in ("backend = auto", "spi_device = /dev/spidev0.1", "spi_mux_gpio", "spi_dc_gpio", "spi_reset_gpio", "mux_closed_level", "spi_speed_hz = 25000000", "dc_gpio = -1"): if token not in conf: sys.stderr.write("FAIL: cover config lost %s\n" % token) return 1 print("[ok] closed-flip spidev wiring and config are present") with tempfile.TemporaryDirectory(prefix="bananarchy-cover-spi-") as tmp: root = pathlib.Path(tmp) exe = root / "cover-idle" build = subprocess.run( ["gcc", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", "-I" + str(SRC.parent), str(SRC), "-o", str(exe)], capture_output=True, text=True) if build.returncode: sys.stderr.write(build.stderr) return 1 print("[ok] host build of the compiled cover daemon") daemon, case = run_case(root, exe, {}) try: if "pong" not in daemon.send("PING"): sys.stderr.write("FAIL: daemon did not answer PING\n") return 1 time.sleep(0.3) if daemon.events(): return not check(False, "an open flip touched the shared SPI bus: %s" % daemon.events()[:4]) status = daemon.send("STATUS") if "spi=1" not in status or "mux=free" not in status: return not check(False, "STATUS does not report the idle bus: %s" % status) print("[ok] open flip: no GPIO claim, no SPI transfer") daemon.send("LID CLOSED") if not daemon.wait_event("claim"): return not check(False, "closing the flip did not claim the mux") if not daemon.wait_event("cmd fe 0 ") or not daemon.wait_event("cmd 29 0 "): return not check(False, "external panel firmware was not replayed") if not daemon.wait_event("frame lit=0 size=115200"): return not check(False, "no dark frame after closing") events = daemon.events() mux_closed = [i for i, line in enumerate(events) if line == "gpio 1"] first_cmd = next((i for i, line in enumerate(events) if line.startswith("cmd ")), len(events)) if not mux_closed or mux_closed[0] >= first_cmd: return not check(False, "mux was not switched before the first SPI command") if "backend=spidev" not in daemon.send("STATUS") or \ "mux=closed" not in daemon.send("STATUS"): return not check(False, "STATUS does not report the spidev backend") print("[ok] closed flip: mux, reset, firmware init and dark frame") daemon.send("BUTTON") if not daemon.wait_event("frame lit=1 size=115200"): return not check(False, "BUTTON did not light the external panel") if not any(e.startswith("cmd 29 0 ") for e in daemon.events()): return not check(False, "display-on command missing") print("[ok] external button: lit RGB565 frame on the cover") daemon.send("BLANK") if not daemon.wait_event("cmd 28 0"): return not check(False, "display-off command missing when dark") print("[ok] blank: external panel is powered down again") daemon.send("LID OPEN") if not daemon.wait_event("release"): return not check(False, "opening the flip did not release the panel lines") events = daemon.events() last_release = max(i for i, line in enumerate(events) if line == "release") tail = events[last_release + 1:] if any(line.startswith(("cmd ", "frame ", "claim")) for line in tail): return not check(False, "SPI activity continued after LID OPEN: %s" % tail) if "gpio 0" not in events[:last_release + 1]: return not check(False, "mux was not restored to the internal panel") if "mux=open" not in daemon.send("STATUS"): return not check(False, "STATUS does not report the restored mux") print("[ok] LID OPEN: mux restored, panel lines released, bus idle") finally: daemon.close() # Polarity knobs must be honoured without a rebuild. rev, _ = run_case(root, exe, {"mux_closed_level": "0", "mux_open_level": "1"}) try: rev.send("LID CLOSED") if not rev.wait_event("claim"): return not check(False, "inverted-polarity daemon did not activate") rev.send("LID OPEN") if not rev.wait_event("release"): return not check(False, "inverted-polarity daemon did not release") events = rev.events() if events.count("gpio 0") < 1 or events.count("gpio 1") < 1: return not check(False, "inverted mux polarity was ignored: %s" % events) print("[ok] mux polarity is a config knob, not a rebuild") finally: rev.close() # The opt-in /dev/fb1 DT keeps precedence over the userspace backend. fb, fbcase = run_case(root, exe, {"backend": "auto"}, prepare=lambda d: (d / "no-fb1").write_bytes( b"\x00" * (240 * 240 * 4)), env_extra={"BANANARCHY_COVER_FB_GEOM": "240,240,32"}) try: fb.send("LID CLOSED") fb.send("BUTTON") time.sleep(0.3) if any(line.startswith(("claim", "cmd ", "frame ")) for line in fb.events()): return not check(False, "spidev backend overrode the /dev/fb1 path") if "backend=fb" not in fb.send("STATUS"): return not check(False, "fbdev backend not selected when /dev/fb1 exists") print("[ok] /dev/fb1 takes precedence when the opt-in DT is installed") finally: fb.close() # Explicitly disabled backend must stay completely inert. off, _ = run_case(root, exe, {"backend": "none"}) try: off.send("LID CLOSED") off.send("BUTTON") time.sleep(0.3) if off.events(): return not check(False, "backend=none still touched the bus: %s" % off.events()[:4]) if "spi=0" not in off.send("STATUS"): return not check(False, "backend=none did not disable spidev") print("[ok] backend=none keeps the shared bus untouched") finally: off.close() # A forwarded main-panel wake lights the outer panel only. wake, _ = run_case(root, exe, {}) try: wake.send("LID CLOSED") if "visible=1" in wake.send("STATUS"): return not check(False, "a closed flip lit the cover without an event") wake.send("WAKE") if not wake.wait_event("frame lit=1 size=115200"): return not check(False, "WAKE did not light the cover panel") if "visible=1" not in wake.send("STATUS"): return not check(False, "WAKE did not mark the cover visible") # The lid daemon rebroadcasts LID CLOSED every 2 s; it must not undo it. lit = [e for e in wake.events() if e.startswith("frame lit=1")] wake.send("LID CLOSED") time.sleep(0.3) if "visible=0" in wake.send("STATUS"): return not check(False, "a repeated LID CLOSED cleared the cover wake") if len([e for e in wake.events() if e.startswith("frame lit=1")]) != len(lit): return not check(False, "a repeated LID CLOSED redrew the cover") print("[ok] WAKE lights the outer panel; repeated LID CLOSED is ignored") finally: wake.close() # A pending DPMS-off must be retried without another event. pend, pendcase = run_case(root, exe, {}, dpms="On") try: pend.send("LID CLOSED") pend.send("WAKE") if "backend=pending" not in pend.send("STATUS"): return not check(False, "a live main panel did not defer the cover") (pendcase / "dpms").write_text("Off\n") if not pend.wait_event("frame lit=1 size=115200", timeout=1.5): return not check(False, "a deferred cover draw was not retried") if "backend=spidev" not in pend.send("STATUS"): return not check(False, "the retried draw did not switch to spidev") print("[ok] deferred cover draw is retried once DPMS goes off") finally: pend.close() # PROBE walks the hardware sign-off cases and 9-bit words are framed. probe, _ = run_case(root, exe, {}) try: probe.send("LID CLOSED") for i in range(12): probe.send("PROBE") if "probe=case %d/12" % (i + 1) not in probe.send("STATUS"): return not check(False, "PROBE case %d was not reported" % (i + 1)) events = probe.events() if not any(e.startswith("cmd fe 0 bits=9") for e in events): return not check(False, "PROBE never exercised 9-bit MIPI DBI") if not any(e.startswith("frame lit=1 size=230400") for e in events) and \ not any(e.startswith("frame lit=1") for e in events): return not check(False, "PROBE did not draw the test bars") print("[ok] PROBE cycles polarity/word-bit/speed cases with test bars") finally: probe.close() # A transient lid flap must never steal the bus from a live main panel. live, _ = run_case(root, exe, {}, dpms="On") try: live.send("LID CLOSED") live.send("BUTTON") time.sleep(0.3) if any(e.startswith(("claim", "cmd ", "frame ")) for e in live.events()): return not check(False, "cover took the bus while the main panel was On") if "backend=pending" not in live.send("STATUS"): return not check(False, "STATUS does not report the DPMS gate") print("[ok] main panel DPMS-On blocks the closed-flip bus takeover") finally: live.close() # Runtime SET knobs: polarity re-init and a one-command rollback. knobs, _ = run_case(root, exe, {}) try: knobs.send("LID CLOSED") if not knobs.wait_event("cmd fe 0 "): return not check(False, "runtime-knob daemon did not init the panel") before = len([e for e in knobs.events() if e.startswith("cmd fe 0 ")]) knobs.send("SET mux_closed_level 0") if not knobs.wait_event("release"): return not check(False, "SET polarity did not release the panel lines") if not check(any(e == "gpio 0" for e in knobs.events()), "SET mux_closed_level was ignored"): return 1 after = len([e for e in knobs.events() if e.startswith("cmd fe 0 ")]) if after <= before: return not check(False, "SET polarity did not re-init the panel") knobs.send("SET backend none") if "spi=0" not in knobs.send("STATUS"): return not check(False, "SET backend none did not disable spidev") tail_start = len(knobs.events()) knobs.send("BUTTON") time.sleep(0.3) if any(e.startswith("frame ") for e in knobs.events()[tail_start:]): return not check(False, "SET backend none still drew a frame") print("[ok] SET polarity and SET backend none are live sign-off knobs") finally: knobs.close() print("closed-flip external panel backend: all checks passed") return 0 if __name__ == "__main__": sys.exit(main())