From 04fa59ce1d61681c425a70f8f196438cda2fdb68 Mon Sep 17 00:00:00 2001 From: Viktor Dragomiretskyy Date: Sat, 11 Jul 2020 17:03:27 +1200 Subject: [PATCH] Add option to start service on a specific device identified by bus number and device address. --- dbus_service/dbus-service | 14 +++++++++++--- validitysensor/init.py | 10 +++++++--- validitysensor/usb.py | 19 +++++++++++++++---- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/dbus_service/dbus-service b/dbus_service/dbus-service index c2ab89e..55332ec 100755 --- a/dbus_service/dbus-service +++ b/dbus_service/dbus-service @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import argparse +import re import dbus import dbus.mainloop.glib import dbus.service @@ -150,15 +151,22 @@ class Device(dbus.service.Object): if __name__ == '__main__': parser = argparse.ArgumentParser('Open fprintd DBus service') parser.add_argument('--debug', help='Enable tracing', action='store_true') + parser.add_argument('--devpath', help='USB device path: usb--
') args = parser.parse_args() if args.debug: logging.basicConfig(level=logging.DEBUG) + usb.trace_enabled = True + tls.trace_enabled = True - init.open() + if args.devpath is None: + init.open() + else: + z = re.match('^usb-(\d+)-(\d+)$', args.devpath) + if not z: + parser.error('Option --devpath should look like this: usb--
') - usb.trace_enabled = True - tls.trace_enabled = True + init.open_devpath(*map(int, z.groups())) bus = dbus.SystemBus() diff --git a/validitysensor/init.py b/validitysensor/init.py index 7457f6e..65cb1dc 100644 --- a/validitysensor/init.py +++ b/validitysensor/init.py @@ -17,8 +17,7 @@ def close(): finally: usb.close() -def open(): - usb.open() +def open_common(): usb.send_init() # We must register atexit only after we opened usb device, # so that our handler is called before pyusb's one and we can still talk to the device @@ -29,5 +28,10 @@ def open(): sensor.open() +def open(): + usb.open() + open_common() - +def open_devpath(busnum, address): + usb.open_devpath(busnum, address) + open_common() diff --git a/validitysensor/usb.py b/validitysensor/usb.py index 91c573d..db80f5d 100644 --- a/validitysensor/usb.py +++ b/validitysensor/usb.py @@ -15,9 +15,6 @@ supported_devices=[ ] -def custom_match(d): - return (d.idVendor, d.idProduct) in supported_devices - class Usb(): def __init__(self): self.interrupt_cb = None @@ -29,11 +26,25 @@ class Usb(): if vendor is not None and product is not None: dev = ucore.find(idVendor=vendor, idProduct=product) else: - dev = ucore.find(custom_match=custom_match) + def match(d): + return (d.idVendor, d.idProduct) in supported_devices + + dev = ucore.find(custom_match=match) + + self.open_dev(dev) + + def open_devpath(self, busnum, address): + def match(d): + return d.bus == busnum and d.address == address + + dev = ucore.find(custom_match=match) self.open_dev(dev) def open_dev(self, dev): + if dev is None: + raise Exception('No matching devices found') + self.dev = dev self.dev.default_timeout = 15000 self.thread = Thread(target=lambda: self.int_thread())