Add option to start service on a specific device identified by bus number and device address.

This commit is contained in:
Viktor Dragomiretskyy 2020-07-11 17:03:27 +12:00
parent 22d2027469
commit 04fa59ce1d
3 changed files with 33 additions and 10 deletions

View file

@ -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-<busnum>-<address>')
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-<busnum>-<address>')
usb.trace_enabled = True
tls.trace_enabled = True
init.open_devpath(*map(int, z.groups()))
bus = dbus.SystemBus()

View file

@ -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()

View file

@ -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())