From 726362107bddb4f237b21cbae42a7fbd7a101848 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 14 Feb 2018 11:20:36 +1000 Subject: [PATCH] wacom: append multiple nordic answers to each other On the Spark, the 0xc9 CRC comes almost immediately after the 0xc8 end-of-sequence. This causes a race condition where sometimes we process the 0xc8 before the 0xc9 arrives, other times the 0xc9 overwrites the current nordic_answer value before we get to it - triggering an unexpected opcode exception. Fix this by appending the nordic answer and only ever pulling off enough to satisfy the current request. Fixes #90 --- tuhi/wacom.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tuhi/wacom.py b/tuhi/wacom.py index c3929c8..d54e0d3 100644 --- a/tuhi/wacom.py +++ b/tuhi/wacom.py @@ -160,7 +160,7 @@ class WacomProtocolLowLevelComm(GObject.Object): def __init__(self, device): GObject.Object.__init__(self) self.device = device - self.nordic_answer = None + self.nordic_answer = [] self.fw_logger = logging.getLogger('tuhi.fw') device.connect_gatt_value(NORDIC_UART_CHRC_RX_UUID, @@ -168,7 +168,7 @@ class WacomProtocolLowLevelComm(GObject.Object): def _on_nordic_data_received(self, name, value): self.fw_logger.debug(f'RX Nordic <-- {list2hex(value)}') - self.nordic_answer = value + self.nordic_answer += value def send_nordic_command(self, command, arguments): chrc = self.device.characteristics[NORDIC_UART_CHRC_TX_UUID] @@ -177,20 +177,20 @@ class WacomProtocolLowLevelComm(GObject.Object): chrc.write_value(data) def check_nordic_incoming(self): - if self.nordic_answer is None: + if not self.nordic_answer: raise WacomTimeoutException(f'{self.device.name}: Timeout while reading data') answer = self.nordic_answer - self.nordic_answer = None length = answer[1] args = answer[2:] - if length != len(args): + if length > len(args): raise WacomException(f'error while processing answer, should get an answer of size {length} instead of {len(args)}') + self.nordic_answer = self.nordic_answer[length + 2:] # opcode + len return NordicData(answer) def wait_nordic_data(self, expected_opcode, timeout): t = time.time() - while self.nordic_answer is None and time.time() - t < timeout: + while not self.nordic_answer and time.time() - t < timeout: time.sleep(0.1) data = self.check_nordic_incoming()