Skip to content

Commit

Permalink
Merge branch '#9-AndroidRefactor' into #9-AndroidRefactorPolling
Browse files Browse the repository at this point in the history
  • Loading branch information
MelbourneDeveloper committed Jan 5, 2019
2 parents 35a6ccf + 1857ba3 commit 1349b82
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/Usb.Net/Windows/WindowsUsbDeviceFactory.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using Device.Net;
using Device.Net.Windows;
using System;
using System.Globalization;

namespace Usb.Net.Windows
{
public class WindowsUsbDeviceFactory : WindowsDeviceFactoryBase, IDeviceFactory
{
#region Public Override Properties
public override DeviceType DeviceType => DeviceType.Usb;

/// <summary>
/// The parent Guid to enumerate through devices at. This probably shouldn't be changed. It defaults to the WinUSB Guid.
/// </summary>
Expand All @@ -25,7 +26,32 @@ public IDevice GetDevice(DeviceDefinition deviceDefinition)
#region Private Static Methods
protected override DeviceDefinition GetDeviceDefinition(string deviceId)
{
return new DeviceDefinition { DeviceId = deviceId, DeviceType = DeviceType.Usb };
uint? vid = null;
uint? pid = null;
try
{
vid = GetNumberFromDeviceId(deviceId, "vid_");
pid = GetNumberFromDeviceId(deviceId, "pid_");
}
catch (Exception)
{
//TODO: Logging
//We really need the Vid/Pid here for polling etc. so not sure if swallowing errors it the way to go
}

return new DeviceDefinition { DeviceId = deviceId, DeviceType = DeviceType.Usb, VendorId = vid, ProductId = pid };
}

private static uint GetNumberFromDeviceId(string deviceId, string searchString)
{
var indexOfSearchString = deviceId.ToLower().IndexOf(searchString);
string hexString = null;
if (indexOfSearchString > -1)
{
hexString = deviceId.Substring(indexOfSearchString + searchString.Length, 4);
}
var numberAsInteger = uint.Parse(hexString, NumberStyles.HexNumber);
return numberAsInteger;
}
#endregion

Expand Down

0 comments on commit 1349b82

Please sign in to comment.