Skip to content

Commit

Permalink
[Windows] Improve USB device hardware ID triplet collection (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored May 15, 2024
1 parent 5ae6804 commit 1e63e81
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions windows/QMK Toolbox/Usb/UsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public UsbDevice(ManagementBaseObject d)
ManufacturerString = (string)WmiDevice.GetPropertyValue("Manufacturer");
ProductString = (string)WmiDevice.GetPropertyValue("Name");

var hardwareIdTriplet = HardwareIdTripletRegex.Match(GetHardwareId(WmiDevice));
VendorId = Convert.ToUInt16(hardwareIdTriplet.Groups[1].ToString(), 16);
ProductId = Convert.ToUInt16(hardwareIdTriplet.Groups[2].ToString(), 16);
RevisionBcd = Convert.ToUInt16(hardwareIdTriplet.Groups[3].ToString(), 16);
var hardwareIdTriplet = GetHardwareId(WmiDevice).Value;
VendorId = hardwareIdTriplet.Item1;
ProductId = hardwareIdTriplet.Item2;
RevisionBcd = hardwareIdTriplet.Item3;

Driver = GetDriverName(WmiDevice);
}
Expand All @@ -42,12 +42,23 @@ public override string ToString()
return $"{ManufacturerString} {ProductString} ({VendorId:X4}:{ProductId:X4}:{RevisionBcd:X4})";
}

private static string GetHardwareId(ManagementBaseObject d)
private static (ushort, ushort, ushort)? GetHardwareId(ManagementBaseObject d)
{
var hardwareIds = (string[])d.GetPropertyValue("HardwareID");
if (hardwareIds != null && hardwareIds.Length > 0)
if (hardwareIds != null)
{
return hardwareIds[0];
foreach (string hardwareId in hardwareIds)
{
Match match = HardwareIdTripletRegex.Match(hardwareId);
if (match.Success)
{
return (
Convert.ToUInt16(match.Groups[1].ToString(), 16),
Convert.ToUInt16(match.Groups[2].ToString(), 16),
Convert.ToUInt16(match.Groups[3].ToString(), 16)
);
}
}
}

return null;
Expand Down

0 comments on commit 1e63e81

Please sign in to comment.