"""VID/PID/product discovery; a USB descriptor is not the protocol serial.""" import hashlib from dataclasses import dataclass from pathlib import Path def device_id(value): return "usbcan_" + hashlib.sha256(value.encode()).hexdigest()[:32] @dataclass(frozen=True) class Attachment: binding: str usb: str path: str speed: str @property def id(self): return device_id("provisional:" + self.binding) def discover(root=Path("/sys/bus/usb/devices")): result = [] for path in sorted(root.glob("*")): try: def read(name, path=path): return (path / name).read_text().strip() if (read("idVendor"), read("idProduct"), read("product")) != ( "04d8", "0053", "USB CANFD DEBUG", ): continue bus, dev = int(read("busnum")), int(read("devnum")) result.append( Attachment( path.name + ":" + str(dev), path.name, f"/dev/bus/usb/{bus:03}/{dev:03}", read("speed") + " Мбит/с", ) ) except (OSError, ValueError): continue return result[:8]