183 lines
6.8 KiB
Python
183 lines
6.8 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Host test for the kernel-owned 2720 Flip cover panel path.
|
||
|
|
|
||
|
|
The daemon runs in SPI simulation mode against a fake /dev/fb0, fake main
|
||
|
|
backlight and a logged DPMS helper. It verifies that the mux is only claimed
|
||
|
|
while closed, that a wake forces a kernel DPMS re-init and renders a frame,
|
||
|
|
that blanking powers the panel down, that opening restores the mux, and that
|
||
|
|
a lit main backlight blocks the whole path.
|
||
|
|
"""
|
||
|
|
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"
|
||
|
|
CONF = BASE / "overlay/rootfs/etc/bananarchy/cover-display.conf"
|
||
|
|
HELPER = BASE / "overlay/rootfs/usr/bin/bananarchy-cover-dpms.sh"
|
||
|
|
|
||
|
|
|
||
|
|
class Daemon:
|
||
|
|
def __init__(self, root, exe, log, fb, bl, config):
|
||
|
|
self.log = log
|
||
|
|
self.fb = fb
|
||
|
|
self.bl = bl
|
||
|
|
self.sock = root / "cover.sock"
|
||
|
|
self.client_path = root / "client.sock"
|
||
|
|
env = dict(os.environ)
|
||
|
|
env.update({
|
||
|
|
"BANANARCHY_COVER_LOCK": str(root / "daemon.lock"),
|
||
|
|
"BANANARCHY_COVER_MAIN_BLANK_CFG": str(root / "idle_blank"),
|
||
|
|
"BANANARCHY_COVER_SPI_SIM": str(log),
|
||
|
|
"BANANARCHY_COVER_MAIN_FB": str(fb),
|
||
|
|
"BANANARCHY_COVER_DPMS_HELPER": "/bin/true",
|
||
|
|
"BANANARCHY_COVER_BL_PATH": str(bl),
|
||
|
|
"BANANARCHY_COVER_DPMS_PATH": str(root / "dpms"),
|
||
|
|
})
|
||
|
|
(root / "dpms").write_text("Off\n")
|
||
|
|
self.proc = subprocess.Popen([str(exe), "--config", str(config)],
|
||
|
|
env=env, stdout=subprocess.DEVNULL,
|
||
|
|
stderr=subprocess.DEVNULL)
|
||
|
|
self.client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
||
|
|
self.client.bind(str(self.client_path))
|
||
|
|
self.client.settimeout(2)
|
||
|
|
end = time.time() + 3
|
||
|
|
while time.time() < end and not self.sock.exists():
|
||
|
|
time.sleep(0.05)
|
||
|
|
|
||
|
|
def send(self, command):
|
||
|
|
self.client.sendto(command.encode(), str(self.sock))
|
||
|
|
try:
|
||
|
|
return self.client.recv(512).decode().strip()
|
||
|
|
except socket.timeout:
|
||
|
|
return ""
|
||
|
|
|
||
|
|
def events(self):
|
||
|
|
return self.log.read_text(encoding="utf-8", errors="replace").splitlines() \
|
||
|
|
if self.log.is_file() else []
|
||
|
|
|
||
|
|
def wait(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()
|
||
|
|
|
||
|
|
|
||
|
|
def fail(message):
|
||
|
|
sys.stderr.write("FAIL: %s\n" % message)
|
||
|
|
return 1
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
if not SRC.is_file() or not CONF.is_file() or not HELPER.is_file():
|
||
|
|
return fail("missing cover daemon source, config or DPMS helper")
|
||
|
|
conf = CONF.read_text(encoding="utf-8")
|
||
|
|
for token in ("backend = kernel", "main_fb_device = /dev/fb0",
|
||
|
|
"kernel_dpms_helper = /usr/bin/bananarchy-cover-dpms.sh"):
|
||
|
|
if token not in conf:
|
||
|
|
return fail("shipping config lost %s" % token)
|
||
|
|
helper = HELPER.read_text(encoding="utf-8")
|
||
|
|
if "dpms force off" not in helper or "dpms force on" not in helper:
|
||
|
|
return fail("the DPMS helper no longer performs an off/on re-init")
|
||
|
|
source = SRC.read_text(encoding="utf-8")
|
||
|
|
for token in ("cover_kernel_draw", "cover_kernel_frame",
|
||
|
|
"cover_main_backlight_is_off", "reinit"):
|
||
|
|
if token not in source:
|
||
|
|
return fail("cover daemon lost the kernel backend: %s" % token)
|
||
|
|
print("[ok] kernel backend wiring is present")
|
||
|
|
with tempfile.TemporaryDirectory(prefix="bananarchy-cover-kernel-") 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")
|
||
|
|
fb = root / "fb0"
|
||
|
|
fb.write_bytes(b"\x00" * (240 * 320 * 4))
|
||
|
|
bl = root / "brightness"
|
||
|
|
bl.write_text("0\n")
|
||
|
|
log = root / "spi.log"
|
||
|
|
(root / "idle_blank").write_text("300\n")
|
||
|
|
config = root / "cover.conf"
|
||
|
|
config.write_text(
|
||
|
|
"socket_path = %s\nfb_device = %s\nbacklight_path = %s\n"
|
||
|
|
"blank_timeout = 2\nbackend = kernel\nmain_fb_device = %s\n"
|
||
|
|
"kernel_dpms_helper = /bin/true\nmux_closed_level = 1\n"
|
||
|
|
"mux_open_level = 0\nreset_assert_level = 0\nreset_idle_level = 1\n" %
|
||
|
|
(root / "cover.sock", root / "no-fb1", root / "backlight", fb))
|
||
|
|
daemon = Daemon(root, exe, log, fb, bl, config)
|
||
|
|
try:
|
||
|
|
time.sleep(0.3)
|
||
|
|
if daemon.events():
|
||
|
|
return fail("an open flip claimed the mux or touched DPMS: %s" %
|
||
|
|
daemon.events()[:4])
|
||
|
|
print("[ok] open flip: no mux claim, no DPMS, no frame")
|
||
|
|
|
||
|
|
daemon.send("LID CLOSED")
|
||
|
|
if not daemon.wait("dpms off"):
|
||
|
|
return fail("closing the flip did not blank the cover")
|
||
|
|
if not any(e == "gpio 1 1" for e in daemon.events()):
|
||
|
|
return fail("the mux was not switched to the external panel")
|
||
|
|
print("[ok] closed flip: mux to cover, panel blanked")
|
||
|
|
|
||
|
|
daemon.send("BUTTON")
|
||
|
|
if not daemon.wait("dpms reinit") or not daemon.wait("kernel frame lit=1"):
|
||
|
|
return fail("BUTTON did not force a kernel re-init and draw a frame")
|
||
|
|
if not any(fb.read_bytes()):
|
||
|
|
return fail("the cover UI was not rendered into the main framebuffer")
|
||
|
|
if "kernel" not in daemon.send("STATUS"):
|
||
|
|
return fail("STATUS does not report the kernel backend")
|
||
|
|
print("[ok] BUTTON: DPMS re-init and cover UI rendered to fbdev")
|
||
|
|
|
||
|
|
daemon.send("BLANK")
|
||
|
|
if not daemon.wait("dpms off"):
|
||
|
|
return fail("blanking did not power the cover down")
|
||
|
|
daemon.send("LID OPEN")
|
||
|
|
if not daemon.wait("release"):
|
||
|
|
return fail("opening the flip did not release the mux")
|
||
|
|
if not any(e == "gpio 0 1" for e in daemon.events()):
|
||
|
|
return fail("the mux was not restored to the internal panel")
|
||
|
|
print("[ok] blank and open: panel off, mux restored")
|
||
|
|
|
||
|
|
# A lit main backlight must block the whole kernel path.
|
||
|
|
tail = len(daemon.events())
|
||
|
|
daemon.send("LID CLOSED")
|
||
|
|
bl.write_text("14\n")
|
||
|
|
daemon.send("BUTTON")
|
||
|
|
time.sleep(0.3)
|
||
|
|
if "pending" not in daemon.send("STATUS"):
|
||
|
|
return fail("a lit main backlight did not block the kernel backend")
|
||
|
|
if any(e.startswith("kernel frame") or e.startswith("dpms reinit")
|
||
|
|
for e in daemon.events()[tail:]):
|
||
|
|
return fail("the kernel backend drew with the main backlight lit")
|
||
|
|
print("[ok] lit main backlight blocks the kernel backend")
|
||
|
|
finally:
|
||
|
|
daemon.close()
|
||
|
|
print("kernel-owned cover panel: all checks passed")
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|