macOS: handle `HIDError` in `hidapi.hidapi_impl._match()` (#2804)

* Fix: handle `HIDError` in `hidapi.hidapi_impl._match()`
The `open_path()` function may raise `HIDError` but `_match()`, its caller, does not handle it, unlike other cases after opening the path. This affects to the device enumeration process in `hidapi.enumerate()`, causing some devices to be randomly undiscovered.

* hidapi: revert to independent checking of long and short HID++ features with an extensible refactor

* Refactor: too long line
This commit is contained in:
SeongWoo Chung 2025-02-10 02:31:20 +09:00 committed by GitHub
parent cefc502db9
commit df2df301e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 12 deletions

View File

@ -255,20 +255,30 @@ def _match(
device["hidpp_short"] = False device["hidpp_short"] = False
device["hidpp_long"] = False device["hidpp_long"] = False
device_handle = None device_handle = None
def check_hidpp_short():
report = _get_input_report(device_handle, 0x10, 32)
if len(report) == 1 + 6 and report[0] == 0x10:
device["hidpp_short"] = True
def check_hidpp_long():
report = _get_input_report(device_handle, 0x11, 32)
if len(report) == 1 + 19 and report[0] == 0x11:
device["hidpp_long"] = True
try: try:
device_handle = open_path(device["path"]) device_handle = open_path(device["path"])
try:
report = _get_input_report(device_handle, 0x10, 32) for check_func in (check_hidpp_short, check_hidpp_long):
if len(report) == 1 + 6 and report[0] == 0x10: try:
device["hidpp_short"] = True check_func()
except HIDError as e: except HIDError as e:
logger.info(f"Error opening device {device['path']} ({bus_id}/{vid:04X}/{pid:04X}) for hidpp check: {e}") logger.info(
try: f"Error while {check_func.__name__}"
report = _get_input_report(device_handle, 0x11, 32) f"on device {device['path']} ({bus_id}/{vid:04X}/{pid:04X}) for hidpp check: {e}"
if len(report) == 1 + 19 and report[0] == 0x11: )
device["hidpp_long"] = True except HIDError as e:
except HIDError as e: logger.info(f"Error opening device {device['path']} ({bus_id}/{vid:04X}/{pid:04X}) for hidpp check: {e}")
logger.info(f"Error opening device {device['path']} ({bus_id}/{vid:04X}/{pid:04X}) for hidpp check: {e}")
finally: finally:
if device_handle: if device_handle:
close(device_handle) close(device_handle)