udev: add a timeout to find_paired_node because the device might not be instantly ready (e.g. after pairing)

This commit is contained in:
effective-light 2020-08-12 16:31:34 -04:00 committed by Peter F. Patel-Schneider
parent 2d29db4609
commit 246c6cc1b2
2 changed files with 12 additions and 7 deletions

View File

@ -34,6 +34,7 @@ import os as _os
from collections import namedtuple
from select import select as _select
from time import sleep
from time import time as _timestamp
from pyudev import Context as _Context
from pyudev import Device as _Device
@ -154,7 +155,7 @@ def _match(action, device, filter):
return d_info
def find_paired_node(receiver_path, index):
def find_paired_node(receiver_path, index, timeout):
"""Find the node of a device paired with a receiver"""
context = _Context()
receiver_phys = _Devices.from_device_file(context, receiver_path).find_parent('hid').get('HID_PHYS')
@ -163,10 +164,14 @@ def find_paired_node(receiver_path, index):
return None
phys = f'{receiver_phys}:{index}'
for dev in context.list_devices(subsystem='hidraw'):
dev_phys = dev.find_parent('hid').get('HID_PHYS')
if dev_phys and dev_phys == phys:
return dev.device_node
timeout += _timestamp()
delta = _timestamp()
while delta < timeout:
for dev in context.list_devices(subsystem='hidraw'):
dev_phys = dev.find_parent('hid').get('HID_PHYS')
if dev_phys and dev_phys == phys:
return dev.device_node
delta = _timestamp()
return None

View File

@ -128,7 +128,7 @@ class Device(object):
# device is unpaired
assert self.wpid is not None, 'failed to read wpid: device %d of %s' % (number, receiver)
self.path = _hid.find_paired_node(receiver.path, number)
self.path = _hid.find_paired_node(receiver.path, number, _base.DEFAULT_TIMEOUT)
self.handle = _hid.open_path(self.path) if self.path else None
self.descriptor = _DESCRIPTORS.get(self.wpid)
@ -280,7 +280,7 @@ class Device(object):
for s in self.descriptor.settings:
try:
setting = s(self)
except Exception as e: # Do nothing if the device is offline
except Exception as e: # Do nothing if the device is offline
setting = None
if self.online:
raise e