device: read the serial number properly from the device hidraw nodes

This commit is contained in:
effective-light 2020-08-10 20:12:41 -04:00 committed by Peter F. Patel-Schneider
parent c4d2ec4008
commit eec5df5d56
2 changed files with 13 additions and 20 deletions

View File

@ -128,19 +128,14 @@ class Device(object):
# device is unpaired # device is unpaired
assert self.wpid is not None, 'failed to read wpid: device %d of %s' % (number, receiver) assert self.wpid is not None, 'failed to read wpid: device %d of %s' % (number, receiver)
serial = self.serial for dev in _hid.enumerate({'vendor_id': 0x046d, 'product_id': int(self.receiver.product_id, 16)}):
for dev in _hid.enumerate({ if dev.serial:
'vendor_id': 0x046d, split = dev.serial.split('-')
'product_id': int(self.receiver.product_id, 16), hidraw_serial = ''.join(split[1:]).upper()
'usb_interface': 2 if self.serial == hidraw_serial and split[0] == self.wpid:
}):
self.handle = _hid.open_path(dev.path)
if serial == self.serial and dev.serial.startswith(self.wpid):
self.path = dev.path self.path = dev.path
self.handle = _hid.open_path(dev.path)
break break
else:
_hid.close(self.handle)
self.handle = None
self.descriptor = _DESCRIPTORS.get(self.wpid) self.descriptor = _DESCRIPTORS.get(self.wpid)
if self.descriptor is None: if self.descriptor is None:
@ -225,7 +220,7 @@ class Device(object):
@property @property
def serial(self): def serial(self):
if self.receiver: if not self._serial and self.receiver:
serial = self.receiver.read_register(_R.receiver_info, 0x30 + self.number - 1) serial = self.receiver.read_register(_R.receiver_info, 0x30 + self.number - 1)
if serial: if serial:
ps = ord(serial[9:10]) & 0x0F ps = ord(serial[9:10]) & 0x0F

View File

@ -185,11 +185,9 @@ def run(cli_args=None, hidraw_path=None):
try: try:
c = list(_receivers(hidraw_path)) c = list(_receivers(hidraw_path))
if not c: if action == 'show':
if action != 'show':
raise Exception('Logitech receiver not found')
else:
c += list(_wired_devices(hidraw_path)) c += list(_wired_devices(hidraw_path))
if not c: if not c:
raise Exception('No devices found') raise Exception('No devices found')