diff --git a/validitysensor/sensor.py b/validitysensor/sensor.py index dd019bb..362455d 100644 --- a/validitysensor/sensor.py +++ b/validitysensor/sensor.py @@ -870,7 +870,8 @@ def match_finger(self) -> typing.Tuple[int, int, bytes]: rsp = tls.app(cmd) assert_status(rsp) - b = usb.wait_int() + # The sensor drops off the USB bus if the match is interrupted, so let it finish + b = usb.wait_int(cancellable=False) if b[0] != 3: raise Exception('Finger not recognized: %s' % hexlify(b).decode()) diff --git a/validitysensor/usb.py b/validitysensor/usb.py index 464b092..3bc19d4 100644 --- a/validitysensor/usb.py +++ b/validitysensor/usb.py @@ -4,6 +4,7 @@ from binascii import hexlify, unhexlify from enum import Enum from struct import unpack +from time import monotonic import usb.core as ucore from usb.core import USBError @@ -122,8 +123,10 @@ def read_82(self): def cancel(self): self.cancel = True - def wait_int(self): + # With cancellable=False a cancel request is ignored and the wait gives up after max_wait seconds instead. + def wait_int(self, cancellable: bool = True, max_wait: float = 5): self.cancel = False + deadline = monotonic() + max_wait while True: try: @@ -133,7 +136,10 @@ def wait_int(self): return resp except USBError as e: if e.errno == errno.ETIMEDOUT: - if self.cancel: + if cancellable: + if self.cancel: + raise CancelledException() + elif monotonic() > deadline: raise CancelledException() else: raise e