better coverage of incoming events

This commit is contained in:
Daniel Pavel 2013-07-04 13:20:00 +02:00
parent a03cc9ce64
commit 007cbef086
1 changed files with 12 additions and 10 deletions

View File

@ -22,6 +22,7 @@ import hidapi as _hid
# #
# #
_EVENT_MESSAGE_SIZE = 8
_SHORT_MESSAGE_SIZE = 7 _SHORT_MESSAGE_SIZE = 7
_LONG_MESSAGE_SIZE = 20 _LONG_MESSAGE_SIZE = 20
_MEDIUM_MESSAGE_SIZE = 15 _MEDIUM_MESSAGE_SIZE = 15
@ -193,15 +194,15 @@ def _read(handle, timeout):
if data: if data:
assert isinstance(data, bytes), (repr(data), type(data)) assert isinstance(data, bytes), (repr(data), type(data))
report_id = ord(data[:1]) 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 report_id & 0xF0 == 0x00:
if _log.isEnabledFor(_DEBUG): if _log.isEnabledFor(_DEBUG):
_log.debug("(%s) => r[%02X %s] ignoring unknown report", handle, report_id, _strhex(data[1:])) _log.debug("(%s) => r[%02X %s] ignoring unknown report", handle, report_id, _strhex(data[1:]))
return 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]) devnumber = ord(data[1:2])
if _log.isEnabledFor(_DEBUG): if _log.isEnabledFor(_DEBUG):
@ -230,13 +231,14 @@ def _skip_incoming(handle, ihandle, notifications_hook):
if data: if data:
assert isinstance(data, bytes), (repr(data), type(data)) assert isinstance(data, bytes), (repr(data), type(data))
report_id = ord(data[:1])
if _log.isEnabledFor(_DEBUG): if _log.isEnabledFor(_DEBUG):
report_id = ord(data[:1]) assert ((report_id & 0xF0 == 0 and len(data) == _EVENT_MESSAGE_SIZE) or
assert (report_id == 0x10 and len(data) == _SHORT_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 == 0x11 and len(data) == _LONG_MESSAGE_SIZE) or
report_id == 0x20 and len(data) == _MEDIUM_MESSAGE_SIZE), \ (report_id == 0x20 and len(data) == _MEDIUM_MESSAGE_SIZE)), \
"unexpected message size: report_id %02X message %s" % (report_id, _strhex(data)) "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:]) n = make_notification(ord(data[1:2]), data[2:])
if n: if n:
notifications_hook(n) notifications_hook(n)