""" Find every LED driver on a Wi-Fi subnet and update them in parallel. A driver answers GET /ota with its partition and build info (see ota.py), so discovery is a parallel probe of every address on the subnet. Then each found driver gets the same image, JOBS at a time. python3 tools/ota_all.py # discover on this machine's subnet, then update all python3 tools/ota_all.py --subnet=192.168.4 # pick the subnet python3 tools/ota_all.py --list # only discover and print, no update python3 tools/ota_all.py --jobs=2 # fewer simultaneous uploads (default 4) """ import argparse import concurrent.futures as cf import os import socket import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from ota import IMAGE_NAME, _default_image, _info, ota_update # noqa: E402 def local_subnet() -> str: """First three octets of the address this machine uses to reach the internet.""" s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: s.connect(("192.0.2.1", 9)) # no packet is sent for a UDP connect ip = s.getsockname()[0] finally: s.close() return ".".join(ip.split(".")[:3]) def discover(subnet: str, port: int, workers: int, timeout: float) -> list[tuple[str, dict]]: hosts = [f"{subnet}.{i}" for i in range(1, 255)] found = [] with cf.ThreadPoolExecutor(max_workers=workers) as pool: futures = {pool.submit(_info, h, port): h for h in hosts} for fut in cf.as_completed(futures): info = fut.result() if info: found.append((futures[fut], info)) return sorted(found, key=lambda t: tuple(int(x) for x in t[0].split("."))) def main() -> int: ap = argparse.ArgumentParser(description="Discover LED drivers on a subnet and update them in parallel.") ap.add_argument("--subnet", help="first three octets, e.g. 192.168.4 (default: this machine's subnet)") ap.add_argument("--image", help=f"firmware image (default: build/{IMAGE_NAME})") ap.add_argument("--port", type=int, default=80) ap.add_argument("--jobs", type=int, default=4, help="simultaneous uploads (default 4)") ap.add_argument("--scan-workers", type=int, default=64, help="simultaneous probes during discovery") ap.add_argument("--list", action="store_true", help="only discover and list drivers, do not update") args = ap.parse_args() subnet = args.subnet or local_subnet() print(f"Scanning {subnet}.1-254 on port {args.port}...") drivers = discover(subnet, args.port, args.scan_workers, timeout=1.5) if not drivers: print("No drivers found.", file=sys.stderr) return 1 print(f"Found {len(drivers)} driver(s):") for host, info in drivers: print(f" {host:<16} {info.get('partition', '?'):<8} built {info.get('built', '?')}") if args.list: return 0 image = args.image or _default_image() if not image: print(f"error: no image given and {IMAGE_NAME} not found under ./build (run make build)", file=sys.stderr) return 1 print(f"\nUpdating {len(drivers)} driver(s) with {image}, {args.jobs} at a time...") failed = [] with cf.ThreadPoolExecutor(max_workers=args.jobs) as pool: futures = {pool.submit(ota_update, h, image, args.port): h for h, _ in drivers} for fut in cf.as_completed(futures): host = futures[fut] try: fut.result() print(f"OK {host}") except RuntimeError as e: failed.append(host) print(f"FAILED {host}: {e}", file=sys.stderr) ok = len(drivers) - len(failed) print(f"\n{ok} updated, {len(failed)} failed" + (f": {' '.join(failed)}" if failed else "")) return 1 if failed else 0 if __name__ == "__main__": sys.exit(main())