Skip to content

Commit

Permalink
Merge branch 'develop' into #9-AndroidRefactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MelbourneDeveloper committed Jan 4, 2019
2 parents 8597992 + ac13ae8 commit 26fc2e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
18 changes: 11 additions & 7 deletions src/Device.Net/Windows/WindowsDeviceFactoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<IEnumerable<DeviceDefinition>> GetConnectedDeviceDefinitions(u
var guidString = ClassGuid.ToString();
var copyOfClassGuid = new Guid(guidString);
var i = APICalls.SetupDiGetClassDevs(ref copyOfClassGuid, IntPtr.Zero, IntPtr.Zero, APICalls.DigcfDeviceinterface | APICalls.DigcfPresent);
var devicesHandle = APICalls.SetupDiGetClassDevs(ref copyOfClassGuid, IntPtr.Zero, IntPtr.Zero, APICalls.DigcfDeviceinterface | APICalls.DigcfPresent);
if (IntPtr.Size == 8)
{
Expand All @@ -43,16 +43,16 @@ public async Task<IEnumerable<DeviceDefinition>> GetConnectedDeviceDefinitions(u
spDeviceInterfaceDetailData.CbSize = 4 + Marshal.SystemDefaultCharSize;
}
var x = -1;
var i = -1;
var productIdHex = GetHex(productId);
var vendorHex = GetHex(vendorId);
while (true)
{
x++;
i++;
var isSuccess = APICalls.SetupDiEnumDeviceInterfaces(i, IntPtr.Zero, ref copyOfClassGuid, (uint)x, ref spDeviceInterfaceData);
var isSuccess = APICalls.SetupDiEnumDeviceInterfaces(devicesHandle, IntPtr.Zero, ref copyOfClassGuid, (uint)i, ref spDeviceInterfaceData);
if (!isSuccess)
{
var errorCode = Marshal.GetLastWin32Error();
Expand All @@ -64,17 +64,21 @@ public async Task<IEnumerable<DeviceDefinition>> GetConnectedDeviceDefinitions(u
throw new Exception($"Could not enumerate devices. Error code: {errorCode}");
}
isSuccess = APICalls.SetupDiGetDeviceInterfaceDetail(i, ref spDeviceInterfaceData, ref spDeviceInterfaceDetailData, 256, out _, ref spDeviceInfoData);
isSuccess = APICalls.SetupDiGetDeviceInterfaceDetail(devicesHandle, ref spDeviceInterfaceData, ref spDeviceInterfaceDetailData, 256, out _, ref spDeviceInfoData);
WindowsDeviceBase.HandleError(isSuccess, "Could not get device interface detail");
//Note this is a bit nasty but we can filter Vid and Pid this way I think...
if (vendorId.HasValue && !spDeviceInterfaceDetailData.DevicePath.ToLower().Contains(vendorHex)) continue;
if (productId.HasValue && !spDeviceInterfaceDetailData.DevicePath.ToLower().Contains(productIdHex)) continue;
deviceDefinitions.Add(GetDeviceDefinition(spDeviceInterfaceDetailData.DevicePath));
var deviceDefinition = GetDeviceDefinition(spDeviceInterfaceDetailData.DevicePath);
if (deviceDefinition == null) continue;
deviceDefinitions.Add(deviceDefinition);
}
APICalls.SetupDiDestroyDeviceInfoList(i);
APICalls.SetupDiDestroyDeviceInfoList(devicesHandle);
return deviceDefinitions;
});
Expand Down
47 changes: 26 additions & 21 deletions src/Hid.Net/Windows/WindowsHidDeviceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,34 @@ public IDevice GetDevice(DeviceDefinition deviceDefinition)
#region Private Static Methods
public static WindowsDeviceDefinition GetDeviceDefinition(string deviceId, SafeFileHandle safeFileHandle)
{
var hidAttributes = GetHidAttributes(safeFileHandle);
var hidCollectionCapabilities = GetHidCapabilities(safeFileHandle);
var manufacturer = GetManufacturer(safeFileHandle);
var serialNumber = GetSerialNumber(safeFileHandle);
var product = GetProduct(safeFileHandle);

var deviceInformation = new WindowsDeviceDefinition
try
{
DeviceId = deviceId,
WriteBufferSize = hidCollectionCapabilities.OutputReportByteLength,
ReadBufferSize = hidCollectionCapabilities.InputReportByteLength,
Manufacturer = manufacturer,
ProductName = product,
ProductId = (ushort)hidAttributes.ProductId,
SerialNumber = serialNumber,
Usage = hidCollectionCapabilities.Usage,
UsagePage = hidCollectionCapabilities.UsagePage,
VendorId = (ushort)hidAttributes.VendorId,
VersionNumber = (ushort)hidAttributes.VersionNumber,
DeviceType = DeviceType.Hid
};
var hidAttributes = GetHidAttributes(safeFileHandle);
var hidCollectionCapabilities = GetHidCapabilities(safeFileHandle);
var manufacturer = GetManufacturer(safeFileHandle);
var serialNumber = GetSerialNumber(safeFileHandle);
var product = GetProduct(safeFileHandle);

return deviceInformation;
return new WindowsDeviceDefinition
{
DeviceId = deviceId,
WriteBufferSize = hidCollectionCapabilities.OutputReportByteLength,
ReadBufferSize = hidCollectionCapabilities.InputReportByteLength,
Manufacturer = manufacturer,
ProductName = product,
ProductId = (ushort)hidAttributes.ProductId,
SerialNumber = serialNumber,
Usage = hidCollectionCapabilities.Usage,
UsagePage = hidCollectionCapabilities.UsagePage,
VendorId = (ushort)hidAttributes.VendorId,
VersionNumber = (ushort)hidAttributes.VersionNumber,
DeviceType = DeviceType.Hid
};
}
catch(Exception ex)
{
return null;
}
}
#endregion

Expand Down

0 comments on commit 26fc2e6

Please sign in to comment.