From 77048d572df4be6484827de72143abba692ddf6d Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 22 Aug 2019 08:30:48 +1000 Subject: [PATCH] protocol: use a MisingReplyError instead of timeout exceptions It's caused by a timeout but the underlying issue is that we never got the reply that we wanted. Signed-off-by: Peter Hutterer --- tuhi/protocol.py | 14 ++++++++++++++ tuhi/wacom.py | 26 ++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/tuhi/protocol.py b/tuhi/protocol.py index 747ad1b..1ed85ca 100644 --- a/tuhi/protocol.py +++ b/tuhi/protocol.py @@ -344,6 +344,18 @@ class ProtocolError(Exception): self.message = message +class MissingReplyError(ProtocolError): + ''' + Thrown when we expected a reply but never got one. Usually caused by a + timeout. + ''' + def __init__(self, request, message=None): + self.request = request + + def __repr__(self): + return f'Missing reply for request {self.request}. {self.message}' + + class AuthorizationError(ProtocolError): ''' The device does not recognize our UUID. @@ -514,6 +526,8 @@ class Msg(object): timeout=self.timeout or None, userdata=self.userdata) if self.requires_reply: + if self.reply is None: + raise MissingReplyError(self.request) try: self._handle_reply(self.reply) # no exception? we can assume success diff --git a/tuhi/wacom.py b/tuhi/wacom.py index 80f72ab..02b8beb 100644 --- a/tuhi/wacom.py +++ b/tuhi/wacom.py @@ -24,7 +24,7 @@ from gi.repository import GObject from .drawing import Drawing from .uhid import UHIDDevice import tuhi.protocol -from tuhi.protocol import NordicData, Interactions, Mode, ProtocolVersion, StrokeFile, UnexpectedDataError, DeviceError +from tuhi.protocol import NordicData, Interactions, Mode, ProtocolVersion, StrokeFile, UnexpectedDataError, DeviceError, MissingReplyError from .util import list2hex, flatten from tuhi.config import TuhiConfig @@ -250,10 +250,6 @@ class WacomException(Exception): errno = errno.ENOSYS -class WacomTimeoutException(WacomException): - errno = errno.ETIME - - class WacomPacket(GObject.Object): ''' A single protocol packet of variable length. The protocol format is a @@ -367,8 +363,8 @@ class WacomProtocolLowLevelComm(GObject.Object): def wait_nordic_data(self, expected_opcode, timeout=None): if not self.nordic_event.acquire(timeout=timeout): - # timeout - raise WacomTimeoutException(f'{self.device.name}: Timeout while reading data') + logger.error(f'{self.device.name}: Timeout while reading data') + return None data = self.pop_next_message() @@ -622,14 +618,12 @@ class WacomProtocolBase(WacomProtocolLowLevelComm): def wait_nordic_unless_pen_data(self, opcode, timeout=None): data = None while data is None: - try: - data = self.wait_nordic_data(opcode, timeout) - except WacomTimeoutException: - # timeout is a time in seconds here - if time.time() - self._last_pen_data_time < timeout: - # we timed out, but we are still fetching offline pen data - continue - raise + data = self.wait_nordic_data(opcode, timeout) + # timeout is a time in seconds here + # if we exceeded the timeout, we return None, otherwise we keep + # looping until we have what we want + if time.time() - self._last_pen_data_time > timeout: + break return data @@ -639,7 +633,7 @@ class WacomProtocolBase(WacomProtocolLowLevelComm): try: msg = self.p.execute(Interactions.WAIT_FOR_END_READ) break - except WacomTimeoutException as e: + except MissingReplyError as e: # if we're still reading pen data, try again if time.time() - self._last_pen_data_time > timeout: raise e