device: be defensive when getting device id

This commit is contained in:
Peter F. Patel-Schneider 2020-11-01 15:11:46 -05:00
parent 73ddb12d8e
commit 1162ccb897
3 changed files with 26 additions and 17 deletions

View File

@ -218,7 +218,9 @@ class Device(object):
def unitId(self): def unitId(self):
if not self._unitId: if not self._unitId:
if self.online and self.protocol >= 2.0: if self.online and self.protocol >= 2.0:
self._unitId, self._modelId, self._tid_map = _hidpp20.get_ids(self) ids = _hidpp20.get_ids(self)
if ids:
self._unitId, self._modelId, self._tid_map = ids
if _log.isEnabledFor(_INFO) and self._serial and self._serial != self._unitId: if _log.isEnabledFor(_INFO) and self._serial and self._serial != self._unitId:
_log.info('%s: unitId %s does not match serial %s', self, self._unitId, self._serial) _log.info('%s: unitId %s does not match serial %s', self, self._unitId, self._serial)
return self._unitId return self._unitId
@ -227,6 +229,8 @@ class Device(object):
def modelId(self): def modelId(self):
if not self._modelId: if not self._modelId:
if self.online and self.protocol >= 2.0: if self.online and self.protocol >= 2.0:
ids = _hidpp20.get_ids(self)
if ids:
self._unitId, self._modelId, self._tid_map = _hidpp20.get_ids(self) self._unitId, self._modelId, self._tid_map = _hidpp20.get_ids(self)
return self._modelId return self._modelId
@ -234,6 +238,8 @@ class Device(object):
def tid_map(self): def tid_map(self):
if not self._tid_map: if not self._tid_map:
if self.online and self.protocol >= 2.0: if self.online and self.protocol >= 2.0:
ids = _hidpp20.get_ids(self)
if ids:
self._unitId, self._modelId, self._tid_map = _hidpp20.get_ids(self) self._unitId, self._modelId, self._tid_map = _hidpp20.get_ids(self)
return self._tid_map return self._tid_map

View File

@ -1054,6 +1054,7 @@ def get_firmware(device):
def get_ids(device): def get_ids(device):
"""Reads a device's ids (unit and model numbers)""" """Reads a device's ids (unit and model numbers)"""
ids = feature_request(device, FEATURE.DEVICE_FW_VERSION) ids = feature_request(device, FEATURE.DEVICE_FW_VERSION)
if ids:
unitId = ids[1:5] unitId = ids[1:5]
modelId = ids[7:13] modelId = ids[7:13]
transport_bits = ord(ids[6:7]) transport_bits = ord(ids[6:7])

View File

@ -208,7 +208,9 @@ def _print_device(dev, num=None):
for fw in _hidpp20.get_firmware(dev): for fw in _hidpp20.get_firmware(dev):
extras = _strhex(fw.extras) if fw.extras else '' extras = _strhex(fw.extras) if fw.extras else ''
print(' Firmware: %s %s %s %s' % (fw.kind, fw.name, fw.version, extras)) print(' Firmware: %s %s %s %s' % (fw.kind, fw.name, fw.version, extras))
unitId, modelId, tid_map = _hidpp20.get_ids(dev) ids = _hidpp20.get_ids(dev)
if ids:
unitId, modelId, tid_map = ids
print(' Unit ID: %s Model ID: %s Transport IDs: %s' % (unitId, modelId, tid_map)) print(' Unit ID: %s Model ID: %s Transport IDs: %s' % (unitId, modelId, tid_map))
elif feature == _hidpp20.FEATURE.REPORT_RATE: elif feature == _hidpp20.FEATURE.REPORT_RATE:
print(' Polling Rate (ms): %d' % _hidpp20.get_polling_rate(dev)) print(' Polling Rate (ms): %d' % _hidpp20.get_polling_rate(dev))