fix(hid): Enhance CmDevice initialization and error handling during device enumeration #318
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request introduces significant improvements to device enumeration and error handling in the Windows HID device listener and the
CmDevice
class. The main changes involve making device creation more robust against transient errors, introducing factory methods for safer instantiation, and improving logging for diagnostics. Additionally, some code has been refactored for clarity and maintainability.These changes make device enumeration more robust and the codebase easier to maintain, especially in scenarios where devices are rapidly connected or disconnected.
Fixes: #144
Device creation and error handling improvements:
FromDevicePath
andFromDeviceInstance
toCmDevice
, which safely handle exceptions and log errors when devices disappear or when the Configuration Manager API fails. The constructors are now marked as obsolete and direct usage is discouraged. (Yubico.Core/src/Yubico/PlatformInterop/Windows/Cfgmgr32/CmDevice.cs
)GetList
,Parent
, andChildren
inCmDevice
to use the new factory methods, ensuring that only valid devices are returned and errors are handled gracefully. (Yubico.Core/src/Yubico/PlatformInterop/Windows/Cfgmgr32/CmDevice.cs
) [1] [2] [3]Windows HID device listener changes:
WindowsHidDeviceListener
to use the newCmDevice.FromDevicePath
method when handling device arrival events. If device creation fails, a warning is logged and the event is ignored, preventing crashes during rapid device changes. (Yubico.Core/src/Yubico/Core/Devices/Hid/WindowsHidDeviceListener.cs
)Code refactoring and clarity:
CmDevice
to a dedicatedInitializeProperties
method, improving constructor clarity and maintainability. (Yubico.Core/src/Yubico/PlatformInterop/Windows/Cfgmgr32/CmDevice.cs
)LocateDevNode
to be static and accept an explicitinstanceId
. (Yubico.Core/src/Yubico/PlatformInterop/Windows/Cfgmgr32/CmDevice.cs
)Other minor improvements:
Yubico.Core/src/Yubico/PlatformInterop/Windows/Cfgmgr32/CmDevice.cs
,Yubico.Core/src/Yubico/Core/Devices/Hid/WindowsHidDeviceListener.cs
) [1] [2]Yubico.Core/src/Yubico/PlatformInterop/Windows/Cfgmgr32/CmPropertyAccessHelper.cs
,Yubico.Core/src/Yubico/Core/Devices/Hid/WindowsHidDeviceListener.cs
) [1] [2] [3]Type of change
How has this been tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Test configuration:
Checklist:
dotnet format
to format my codeFootnotes
See Yubikey models (Multi-protocol, Security Key, FIPS, Bio, YubiHSM, YubiHSM FIPS) ↩