qrposter.py (4972B)
1 # This file is part of GNU Taler 2 # (C) 2026 Taler Systems S.A. 3 # 4 # GNU Taler is free software; you can redistribute it and/or modify it under the 5 # terms of the GNU General Public License as published by the Free Software 6 # Foundation; either version 3, or (at your option) any later version. 7 # 8 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 9 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 10 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 # 12 # You should have received a copy of the GNU General Public License along with 13 # GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 14 """Show the terminal a QR code through its camera, for real. 15 16 The emulator's virtual scene can hang a PNG on its wall, and the back camera 17 sees it. That is the only way to exercise the scan path end to end -- the 18 analyzer, the stride handling, the rotation, the decode and the parse -- rather 19 than synthesising the URI the scanner would have produced. 20 21 The poster can only be set when the emulator launches, so there is one boot per 22 code. 23 24 python3 qrposter.py --uri 'taler-pos://host/instances/x#token' --worker 2 25 python3 qrposter.py --demo --worker 2 # a setup URI for the demo instance 26 27 It prints whether the terminal provisioned itself from what it saw. 28 """ 29 30 from __future__ import annotations 31 32 import argparse 33 import os 34 import subprocess 35 import sys 36 import time 37 from pathlib import Path 38 39 sys.path.insert(0, str(Path(__file__).resolve().parent)) 40 41 import merchant 42 from device import Device, serial_for 43 from pos import Pos, PosError 44 45 SIM_ROOT = Path(os.environ.get("POS_SIM_ROOT", "/tmp/taler-pos-sim")) 46 EMU = Path(__file__).resolve().parent.parent / "emu" / "emu.sh" 47 48 49 def render(uri: str, path: Path) -> Path: 50 """A QR code the virtual scene will actually hang on its wall. 51 52 Two things matter and neither is obvious from a failure, because the 53 emulator says nothing and shows its default poster instead. 54 55 The image has to be a true-colour PNG. `qrencode` writes a 1-bit 56 palette PNG, which the scene's texture loader ignores -- the wall keeps 57 its default checkerboard and the terminal spends its whole run looking at 58 a calibration pattern. So it is converted to 24-bit RGB. 59 60 And it has to have room around it. The wall is two metres square and the 61 camera sees it at an angle, so a code that fills the poster edge to edge 62 loses its quiet zone to the frame; `-s 12 -m 6` leaves enough. 63 """ 64 path.parent.mkdir(parents=True, exist_ok=True) 65 raw = path.with_suffix(".raw.png") 66 subprocess.run(["qrencode", "-o", str(raw), "-s", "12", "-m", "6", 67 "-l", "M", uri], check=True) 68 subprocess.run(["convert", str(raw), "-type", "TrueColor", 69 "PNG24:" + str(path)], check=True) 70 raw.unlink(missing_ok=True) 71 return path 72 73 74 def main(argv: list[str]) -> int: 75 ap = argparse.ArgumentParser(description=__doc__, 76 formatter_class=argparse.RawDescriptionHelpFormatter) 77 ap.add_argument("--uri", default=None) 78 ap.add_argument("--demo", action="store_true", 79 help="render a setup URI for the demo sandbox instance") 80 ap.add_argument("--worker", type=int, 81 default=int(os.environ.get("POS_WORKER", "0"))) 82 ap.add_argument("--apk", default=None) 83 ap.add_argument("--keep-up", action="store_true", 84 help="do not reboot; the poster is already on the wall") 85 args = ap.parse_args(argv) 86 87 uri = args.uri 88 if args.demo or not uri: 89 inst = merchant.demo() 90 uri = inst.setup_uri() 91 print(f"setup URI for {inst.label}: {uri[:80]}...") 92 poster = render(uri, SIM_ROOT / "run" / f"poster-w{args.worker}.png") 93 print(f"poster: {poster} ({poster.stat().st_size} bytes)") 94 95 env = dict(os.environ) 96 env["POS_SIM_ROOT"] = str(SIM_ROOT) 97 if not args.keep_up: 98 subprocess.run([str(EMU), "kill", str(args.worker)], env=env, check=False) 99 subprocess.run([str(EMU), "boot", str(args.worker), str(poster)], 100 env=env, check=True) 101 prepare = [str(EMU), "prepare", str(args.worker)] 102 if args.apk: 103 prepare += [args.apk] 104 subprocess.run(prepare, env=env, check=True) 105 106 dev = Device(serial_for(args.worker)) 107 app = Pos(dev) 108 app.fresh_start() 109 print("selecting the QR tab and waiting for the camera to read the wall") 110 try: 111 app.provision_by_qr(timeout=180) 112 except (PosError, TimeoutError) as e: 113 screen = dev.dump() 114 print(f"NOT PROVISIONED: {e}") 115 print(f"activity: {screen.activity}") 116 print(f"screen: {screen.texts()[:12]}") 117 return 1 118 bound, url = app.bound_instance() 119 print(f"PROVISIONED from the camera: instance {bound!r}, url {url!r}") 120 return 0 121 122 123 if __name__ == "__main__": 124 sys.exit(main(sys.argv[1:]))