From 007cbef0869d68a0716a12873b894602962b4d93 Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Thu, 4 Jul 2013 13:20:00 +0200 Subject: [PATCH] better coverage of incoming events --- lib/logitech/unifying_receiver/base.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/logitech/unifying_receiver/base.py b/lib/logitech/unifying_receiver/base.py index 5e74dce0..817ffe6c 100644 --- a/lib/logitech/unifying_receiver/base.py +++ b/lib/logitech/unifying_receiver/base.py @@ -22,6 +22,7 @@ import hidapi as _hid # # +_EVENT_MESSAGE_SIZE = 8 _SHORT_MESSAGE_SIZE = 7 _LONG_MESSAGE_SIZE = 20 _MEDIUM_MESSAGE_SIZE = 15 @@ -193,15 +194,15 @@ def _read(handle, timeout): if data: assert isinstance(data, bytes), (repr(data), type(data)) report_id = ord(data[:1]) + assert ((report_id & 0xF0 == 0 and len(data) == _EVENT_MESSAGE_SIZE) or + (report_id == 0x10 and len(data) == _SHORT_MESSAGE_SIZE) or + (report_id == 0x11 and len(data) == _LONG_MESSAGE_SIZE) or + (report_id == 0x20 and len(data) == _MEDIUM_MESSAGE_SIZE)), \ + "unexpected message size: report_id %02X message %s" % (report_id, _strhex(data)) if report_id & 0xF0 == 0x00: if _log.isEnabledFor(_DEBUG): _log.debug("(%s) => r[%02X %s] ignoring unknown report", handle, report_id, _strhex(data[1:])) return - - assert (report_id == 0x10 and len(data) == _SHORT_MESSAGE_SIZE or - report_id == 0x11 and len(data) == _LONG_MESSAGE_SIZE or - report_id == 0x20 and len(data) == _MEDIUM_MESSAGE_SIZE), \ - "unexpected message size: report_id %02X message %s" % (report_id, _strhex(data)) devnumber = ord(data[1:2]) if _log.isEnabledFor(_DEBUG): @@ -230,13 +231,14 @@ def _skip_incoming(handle, ihandle, notifications_hook): if data: assert isinstance(data, bytes), (repr(data), type(data)) + report_id = ord(data[:1]) if _log.isEnabledFor(_DEBUG): - report_id = ord(data[:1]) - assert (report_id == 0x10 and len(data) == _SHORT_MESSAGE_SIZE or - report_id == 0x11 and len(data) == _LONG_MESSAGE_SIZE or - report_id == 0x20 and len(data) == _MEDIUM_MESSAGE_SIZE), \ + assert ((report_id & 0xF0 == 0 and len(data) == _EVENT_MESSAGE_SIZE) or + (report_id == 0x10 and len(data) == _SHORT_MESSAGE_SIZE) or + (report_id == 0x11 and len(data) == _LONG_MESSAGE_SIZE) or + (report_id == 0x20 and len(data) == _MEDIUM_MESSAGE_SIZE)), \ "unexpected message size: report_id %02X message %s" % (report_id, _strhex(data)) - if notifications_hook: + if notifications_hook and report_id & 0xF0: n = make_notification(ord(data[1:2]), data[2:]) if n: notifications_hook(n)