From e2909f61654596428eff0d79bc197dbc0befc42d Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Fri, 7 Dec 2012 20:37:13 +0200 Subject: [PATCH] fixed event detection --- lib/logitech/unifying_receiver/base.py | 4 ++-- lib/logitech/unifying_receiver/common.py | 5 +++++ lib/logitech/unifying_receiver/descriptors.py | 4 ++-- lib/logitech/unifying_receiver/status.py | 20 +++++++++---------- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/logitech/unifying_receiver/base.py b/lib/logitech/unifying_receiver/base.py index ff05de3d..e8387070 100644 --- a/lib/logitech/unifying_receiver/base.py +++ b/lib/logitech/unifying_receiver/base.py @@ -240,9 +240,9 @@ def make_event(devnumber, data): if devnumber == 0xFF: if sub_id == 0x4A: # receiver lock event return _Event(devnumber, sub_id, ord(data[1:2]), data[2:]) - else: + elif sub_id & 0x80 != 0x80: address = ord(data[1:2]) - if sub_id > 0x00 and (sub_id >= 0x40 or (address & 0x01 == 0)): + if sub_id >= 0x40 or address & 0x01 == 0: return _Event(devnumber, sub_id, address, data[2:]) diff --git a/lib/logitech/unifying_receiver/common.py b/lib/logitech/unifying_receiver/common.py index 0b81f1a2..94e82b83 100644 --- a/lib/logitech/unifying_receiver/common.py +++ b/lib/logitech/unifying_receiver/common.py @@ -85,6 +85,11 @@ class NamedInts(object): def flag_names(self, value): return ', '.join(str(self._indexed[k]) for k in self._indexed if k & value == k) + def index(self, value): + if value in self._values: + return self._values.index(value) + raise IndexError('%s not found' % value) + def __getitem__(self, index): if type(index) == int: if index in self._indexed: diff --git a/lib/logitech/unifying_receiver/descriptors.py b/lib/logitech/unifying_receiver/descriptors.py index de1cbb20..b68f1d7e 100644 --- a/lib/logitech/unifying_receiver/descriptors.py +++ b/lib/logitech/unifying_receiver/descriptors.py @@ -44,7 +44,7 @@ _D('Wireless Trackball M570') _D('Touch Mouse M600') _D('Marathon Mouse M705', registers=_NamedInts(battery=0x0D), - settings=[hidpp10.SmoothScroll_Setting(0x01)] + settings=[hidpp10.SmoothScroll_Setting(0x01)], ) _D('Wireless Keyboard K270') _D('Wireless Keyboard K350') @@ -59,7 +59,7 @@ _D('Anywhere Mouse MX', codename='Anywhere MX') _D('Performance Mouse MX', codename='Performance MX', settings=[ hidpp10.MouseDPI_Setting(0x63, _NamedInts(**dict((str(x * 100), 0x80 + x) for x in range(1, 16)))), - ] + ], ) del namedtuple diff --git a/lib/logitech/unifying_receiver/status.py b/lib/logitech/unifying_receiver/status.py index 1cea1cb8..ccdc43b4 100644 --- a/lib/logitech/unifying_receiver/status.py +++ b/lib/logitech/unifying_receiver/status.py @@ -31,7 +31,7 @@ ERROR='error' # if not updates have been receiver from the device for a while, assume # it has gone offline and clear all its know properties. -_STATUS_TIMEOUT = 180 # seconds +_STATUS_TIMEOUT = 120 # seconds # # @@ -157,6 +157,8 @@ class DeviceStatus(dict): self._changed(active=False, alert=ALERT.LOW, timestamp=timestamp) def process_event(self, event): + assert event.sub_id < 0x80 + if event.sub_id == 0x40: if event.address == 0x02: # device un-paired @@ -191,7 +193,7 @@ class DeviceStatus(dict): return True - if event.sub_id == 0x04B: + if event.sub_id == 0x4B: if event.address == 0x01: _log.debug("device came online? %d", event.devnumber) self._changed(alert=ALERT.LOW, reason='powered on') @@ -199,12 +201,6 @@ class DeviceStatus(dict): _log.warn("unknown event %s", event) return True - if event.sub_id >= 0x80: - # this can't possibly be an event, can it? - if _log.isEnabledFor(_DEBUG): - _log.debug("ignoring %s (not an event)", event) - return False - if event.sub_id >= 0x40: _log.warn("don't know how to handle event %s", event) return False @@ -214,7 +210,11 @@ class DeviceStatus(dict): _log.warn("device %d got event from unknown feature index %02X", self._device.number, event.sub_id) return False - feature = self._device.features[event.sub_id] + try: + feature = self._device.features[event.sub_id] + except IndexError: + _log.warn("don't know how to handle event %s for feature with invalid index %02X", event, event.sub_id) + return False if feature == _hidpp20.FEATURE.BATTERY: if event.address == 0x00: @@ -288,4 +288,4 @@ class DeviceStatus(dict): _log.debug("TOUCH MOUSE status: button_down=%s mouse_lifted=%s", button_down, mouse_lifted) return True - _log.warn("don't know how to handle event %s for feature %s", event, feature) + _log.warn("don't know how to handle event %s for feature %s (%02X)", event, feature, event.sub_id)