#!/usr/bin/env python3 """Drive tudor_cli enrollment over a pty. Phases: 1. Init (~3-4 min). Prints phase markers; traces are filtered out. 2. Enroll command is sent automatically (identity + finger from argv). 3. Full output from then on. Touch the sensor when the driver asks. 4. Auto-shuts down when enrollment succeeds and saves the template. Press Ctrl-C to abort safely. Usage: enroll.py [identity] [finger] """ import os import pty import select import sys import time BUILD = "/home/nebulon/projects/fingerprint/work/synaTudor/build" CLI = os.path.join(BUILD, "cli", "tudor_cli") DATASTORE = "/home/nebulon/projects/fingerprint/work/debug/datastore.bin" IDENTITY = sys.argv[1] if len(sys.argv) > 1 else "1" FINGER = sys.argv[2] if len(sys.argv) > 2 else "1" os.chdir(BUILD) pid, fd = pty.fork() if pid == 0: os.execv(CLI, [CLI, DATASTORE]) buf = b"" sent_enroll = False enrolled = False init_deadline = time.time() + 600 KEEP = ( "[INF]", "[PIPE]", "[WRN]", "[ERR]", "Initializing", "Opening", "Loading", "Commands:", "Enter identity", "Enter finger", "Put your", "Successfully", "Capturing", "Driver requested", "Retrying", "sensor status", "session ended", "shutting down", "Error", "Enrollment", "not calibrated", "> ", "DEVCTRL", "about to ioctl", ) def interesting(line): return any(k in line for k in KEEP) def pump(timeout=1.0): global buf try: r, _, _ = select.select([fd], [], [], timeout) except (OSError, ValueError): return False if not r: return True try: data = os.read(fd, 65536) except OSError: return False if not data: return False buf += data return True def drain_lines(show_all): global buf text = buf.decode("utf-8", "replace") lines = text.split("\n") buf = lines[-1].encode() for line in lines[:-1]: if show_all or interesting(line): print(line, flush=True) def child_alive(): try: got, status = os.waitpid(pid, os.WNOHANG) if got == pid: if os.WIFEXITED(status): print(f"\n=== child exited normally with code {os.WEXITSTATUS(status)} ===") elif os.WIFSIGNALED(status): print(f"\n=== child killed by signal {os.WTERMSIG(status)} ===") return False except ChildProcessError: return False return True print("=== waiting for driver init (~3-4 minutes)... ===", flush=True) init_start = time.time() while True: if not pump(1.0): print("\n=== output stream closed ===") break if not child_alive(): break #Wait for the real command prompt, not early boot output that also #contains '> ' if not sent_enroll and "Commands:" in buf.decode("utf-8", "replace"): drain_lines(False) sent_enroll = True time.sleep(0.3) os.write(fd, b"e\n") time.sleep(0.3) os.write(fd, f"{IDENTITY}\n".encode()) time.sleep(0.3) os.write(fd, f"{FINGER}\n".encode()) print(f"\n=== init done in {int(time.time()-init_start)}s; enroll sent for identity {IDENTITY}, finger {FINGER} ===", flush=True) print("=== TOUCH AND HOLD the sensor now; keep re-touching until it says enrolled ===\n", flush=True) break if time.time() > init_deadline: print("\n=== init timed out ===") try: os.write(fd, b"s\n") except OSError: pass break # Full output during capture, with a heartbeat so you know it is still waiting capture_deadline = time.time() + 1800 last_beat = time.time() while sent_enroll and not enrolled: if not pump(1.0): print("\n=== output stream closed during capture ===") break if not child_alive(): break text = buf.decode("utf-8", "replace") drain_lines(True) if "Successfully enrolled finger" in text: enrolled = True print("\n=== ENROLLED! saving and shutting down... ===", flush=True) time.sleep(1) os.write(fd, b"s\n") time.sleep(4) break if time.time() - last_beat > 30: last_beat = time.time() print(f" ... {int(time.time() - (capture_deadline - 1800))}s elapsed, still waiting for a finger touch", flush=True) if time.time() > capture_deadline: print("\n=== capture timed out after 30 min ===") try: os.write(fd, b"s\n") time.sleep(2) except OSError: pass break # Drain remaining output briefly end = time.time() + 15 while time.time() < end: if not pump(1.0): break drain_lines(True) print("\n=== session ended ===") try: os.close(fd) except OSError: pass try: os.waitpid(pid, 0) except ChildProcessError: pass