|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2025 Bertrik Sikken <bertrik@sikken.nl> |
| 4 | + |
| 5 | +import pyudev |
| 6 | + |
| 7 | + |
| 8 | +def main(): |
| 9 | + # Create udev context and monitor |
| 10 | + context = pyudev.Context() |
| 11 | + monitor = pyudev.Monitor.from_netlink(context) |
| 12 | + monitor.filter_by(subsystem='tty') # Only listen for tty devices (like /dev/ttyUSB0) |
| 13 | + |
| 14 | + # Monitor events indefinitely |
| 15 | + print("Monitoring for USB-Serial device insertions/removals...") |
| 16 | + for device in iter(monitor.poll, None): |
| 17 | + properties = device.properties |
| 18 | + |
| 19 | + # print(f"All properties: {properties}") |
| 20 | + # for p in properties: |
| 21 | + # val = properties.get(p) |
| 22 | + # print(f'{p}={val}') |
| 23 | + |
| 24 | + items = [] |
| 25 | + if 'ID_BUS' in properties and properties['ID_BUS'] == 'usb': |
| 26 | + vendor_string = properties["ID_VENDOR_FROM_DATABASE"] |
| 27 | + model_string = properties["ID_MODEL_FROM_DATABASE"] |
| 28 | + model_enc = properties["ID_USB_MODEL_ENC"] |
| 29 | + |
| 30 | + print(f"# {vendor_string} / {model_string} ({model_enc})") |
| 31 | + items.append('SUBSYSTEM=="tty"') |
| 32 | + vendor_id = properties['ID_USB_VENDOR_ID'] |
| 33 | + items.append(f'ATTRS{{idVendor}}=="{vendor_id}"') |
| 34 | + product_id = properties['ID_USB_MODEL_ID'] |
| 35 | + items.append(f'ATTRS{{idProduct}}=="{product_id}"') |
| 36 | + items.append('MODE="0660"') |
| 37 | + items.append('GROUP="dialout"') |
| 38 | + items.append(f'SYMLINK+="tty{product_id}"') |
| 39 | + udev = ", ".join(items) |
| 40 | + print(f'{udev}') |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + main() |
0 commit comments