enroll.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env python3
  2. """Drive tudor_cli enrollment over a pty.
  3. Phases:
  4. 1. Init (~3-4 min). Prints phase markers; traces are filtered out.
  5. 2. Enroll command is sent automatically (identity + finger from argv).
  6. 3. Full output from then on. Touch the sensor when the driver asks.
  7. 4. Auto-shuts down when enrollment succeeds and saves the template.
  8. Press Ctrl-C to abort safely.
  9. Usage: enroll.py [identity] [finger]
  10. """
  11. import os
  12. import pty
  13. import select
  14. import sys
  15. import time
  16. BUILD = "/home/nebulon/projects/fingerprint/work/synaTudor/build"
  17. CLI = os.path.join(BUILD, "cli", "tudor_cli")
  18. DATASTORE = "/home/nebulon/projects/fingerprint/work/debug/datastore.bin"
  19. IDENTITY = sys.argv[1] if len(sys.argv) > 1 else "1"
  20. FINGER = sys.argv[2] if len(sys.argv) > 2 else "1"
  21. os.chdir(BUILD)
  22. pid, fd = pty.fork()
  23. if pid == 0:
  24. os.execv(CLI, [CLI, DATASTORE])
  25. buf = b""
  26. sent_enroll = False
  27. enrolled = False
  28. init_deadline = time.time() + 600
  29. KEEP = (
  30. "[INF]", "[PIPE]", "[WRN]", "[ERR]", "Initializing", "Opening",
  31. "Loading", "Commands:", "Enter identity", "Enter finger", "Put your",
  32. "Successfully", "Capturing", "Driver requested", "Retrying",
  33. "sensor status", "session ended", "shutting down", "Error",
  34. "Enrollment", "not calibrated", "> ", "DEVCTRL", "about to ioctl",
  35. )
  36. def interesting(line):
  37. return any(k in line for k in KEEP)
  38. def pump(timeout=1.0):
  39. global buf
  40. try:
  41. r, _, _ = select.select([fd], [], [], timeout)
  42. except (OSError, ValueError):
  43. return False
  44. if not r:
  45. return True
  46. try:
  47. data = os.read(fd, 65536)
  48. except OSError:
  49. return False
  50. if not data:
  51. return False
  52. buf += data
  53. return True
  54. def drain_lines(show_all):
  55. global buf
  56. text = buf.decode("utf-8", "replace")
  57. lines = text.split("\n")
  58. buf = lines[-1].encode()
  59. for line in lines[:-1]:
  60. if show_all or interesting(line):
  61. print(line, flush=True)
  62. def child_alive():
  63. try:
  64. got, status = os.waitpid(pid, os.WNOHANG)
  65. if got == pid:
  66. if os.WIFEXITED(status):
  67. print(f"\n=== child exited normally with code {os.WEXITSTATUS(status)} ===")
  68. elif os.WIFSIGNALED(status):
  69. print(f"\n=== child killed by signal {os.WTERMSIG(status)} ===")
  70. return False
  71. except ChildProcessError:
  72. return False
  73. return True
  74. print("=== waiting for driver init (~3-4 minutes)... ===", flush=True)
  75. init_start = time.time()
  76. while True:
  77. if not pump(1.0):
  78. print("\n=== output stream closed ===")
  79. break
  80. if not child_alive():
  81. break
  82. #Wait for the real command prompt, not early boot output that also
  83. #contains '> '
  84. if not sent_enroll and "Commands:" in buf.decode("utf-8", "replace"):
  85. drain_lines(False)
  86. sent_enroll = True
  87. time.sleep(0.3)
  88. os.write(fd, b"e\n")
  89. time.sleep(0.3)
  90. os.write(fd, f"{IDENTITY}\n".encode())
  91. time.sleep(0.3)
  92. os.write(fd, f"{FINGER}\n".encode())
  93. print(f"\n=== init done in {int(time.time()-init_start)}s; enroll sent for identity {IDENTITY}, finger {FINGER} ===", flush=True)
  94. print("=== TOUCH AND HOLD the sensor now; keep re-touching until it says enrolled ===\n", flush=True)
  95. break
  96. if time.time() > init_deadline:
  97. print("\n=== init timed out ===")
  98. try:
  99. os.write(fd, b"s\n")
  100. except OSError:
  101. pass
  102. break
  103. # Full output during capture, with a heartbeat so you know it is still waiting
  104. capture_deadline = time.time() + 1800
  105. last_beat = time.time()
  106. while sent_enroll and not enrolled:
  107. if not pump(1.0):
  108. print("\n=== output stream closed during capture ===")
  109. break
  110. if not child_alive():
  111. break
  112. text = buf.decode("utf-8", "replace")
  113. drain_lines(True)
  114. if "Successfully enrolled finger" in text:
  115. enrolled = True
  116. print("\n=== ENROLLED! saving and shutting down... ===", flush=True)
  117. time.sleep(1)
  118. os.write(fd, b"s\n")
  119. time.sleep(4)
  120. break
  121. if time.time() - last_beat > 30:
  122. last_beat = time.time()
  123. print(f" ... {int(time.time() - (capture_deadline - 1800))}s elapsed, still waiting for a finger touch", flush=True)
  124. if time.time() > capture_deadline:
  125. print("\n=== capture timed out after 30 min ===")
  126. try:
  127. os.write(fd, b"s\n")
  128. time.sleep(2)
  129. except OSError:
  130. pass
  131. break
  132. # Drain remaining output briefly
  133. end = time.time() + 15
  134. while time.time() < end:
  135. if not pump(1.0):
  136. break
  137. drain_lines(True)
  138. print("\n=== session ended ===")
  139. try:
  140. os.close(fd)
  141. except OSError:
  142. pass
  143. try:
  144. os.waitpid(pid, 0)
  145. except ChildProcessError:
  146. pass