Skip to content

Commit

Permalink
Added support for multiple usb devices (NZXT Kraken, EVGA CLC, NZXT R…
Browse files Browse the repository at this point in the history
…GB&FAN controller)
  • Loading branch information
lich426 committed Jul 8, 2020
1 parent 6eca6fb commit 1281ec7
Show file tree
Hide file tree
Showing 48 changed files with 579 additions and 525 deletions.
2 changes: 1 addition & 1 deletion FanCtrl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
<Compile Include="FanCtrl\Hardware\HardwareManager.cs" />
<Compile Include="FanCtrl\Hardware\Kraken.cs" />
<Compile Include="FanCtrl\Hardware\LHM.cs" />
<Compile Include="FanCtrl\Hardware\Liquid.cs" />
<Compile Include="FanCtrl\Hardware\USBDevice.cs" />
<Compile Include="FanCtrl\Hardware\OHM.cs" />
<Compile Include="FanCtrl\Hardware\RGBnFC.cs" />
<Compile Include="FanCtrl\Hardware\Sensor\CLCFanSpeed.cs" />
Expand Down
52 changes: 33 additions & 19 deletions FanCtrl/Controller/HidUSBController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,50 @@ public HidUSBController(USBVendorID vendorID, USBProductID productID) : base(ven

}

public override bool start()
public static uint getDeviceCount(USBVendorID vendorID, USBProductID productID)
{
uint count = 0;
try
{
int venderID2 = (int)vendorID;
int productID2 = (int)productID;
foreach (HidDevice dev in DeviceList.Local.GetHidDevices(venderID2))
{
if (dev.ProductID == productID2)
{
count++;
}
}
}
catch { }
return count;
}

public override bool start(uint index)
{
try
{
uint i = 0;
int venderID = (int)this.VendorID;
int productID = (int)this.ProductID;
HidDevice hidDevice = null;
foreach (HidDevice dev in DeviceList.Local.GetHidDevices(venderID))
{
if(dev.ProductID == productID)
if (dev.ProductID == productID)
{
hidDevice = dev;
break;
if (i == index)
{
if (dev.TryOpen(out mHidStream) == false)
{
Console.WriteLine("HidUSBController.start() : could not open the device");
this.stop();
return false;
}
break;
}
i++;
}
}

if (hidDevice == null)
{
Console.WriteLine("HidUSBController.start() : could not find the device");
this.stop();
return false;
}

if (hidDevice.TryOpen(out mHidStream) == false)
{
Console.WriteLine("HidUSBController.start() : could not open the device");
this.stop();
return false;
}

this.readAsync();
}
catch (Exception e)
Expand Down
100 changes: 60 additions & 40 deletions FanCtrl/Controller/SiUSBController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,58 @@ public SiUSBController(USBVendorID vendorID, USBProductID productID) : base(vend

}

public override bool start()
public static uint getDeviceCount(USBVendorID vendorID, USBProductID productID)
{
Monitor.Enter(mLock);
uint count = 0;
try
{
uint count = SiUSBController.count();
if (count == 0)
uint devCount = SiUSBController.count();
for (uint i = 0; i < devCount; i++)
{
Console.WriteLine("SiUSBController.start() : USB device is zero");
Monitor.Exit(mLock);
this.stop();
return false;
}
var vidSB = new StringBuilder();
var pidSB = new StringBuilder();
if (SiUSBController.getVID(i, vidSB) > 0 && SiUSBController.getPID(i, pidSB) > 0)
{
string vidString = vidSB.ToString();
string pidString = pidSB.ToString();

Console.WriteLine("SiUSBController.getDeviceCount() : SiUSB index({0}), VendorID(0x{1:X4}), ProductID(0x{2:X4})", i, vidString, pidString);

Console.WriteLine("SiUSBController.start() : USB device count({0})", count);
var vidHex = Util.getHexBytes(vidString);
var pidHex = Util.getHexBytes(pidString);
Array.Reverse(vidHex);
Array.Reverse(pidHex);

ushort vID = BitConverter.ToUInt16(vidHex, 0);
ushort pID = BitConverter.ToUInt16(pidHex, 0);

if (vID == (ushort)vendorID && pID == (ushort)productID)
{
count++;
}
}
}
}
catch { }
return count;
}

bool isDeviceOpen = false;
for (uint i = 0; i < count; i++)
public override bool start(uint index)
{
Monitor.Enter(mLock);
try
{
uint i = 0;
uint devCount = SiUSBController.count();
for (uint j = 0; j < devCount; j++)
{
var vidSB = new StringBuilder();
var pidSB = new StringBuilder();
if (SiUSBController.getVID(i, vidSB) > 0 && SiUSBController.getPID(i, pidSB) > 0)
if (SiUSBController.getVID(j, vidSB) > 0 && SiUSBController.getPID(j, pidSB) > 0)
{
string vidString = vidSB.ToString();
string pidString = pidSB.ToString();

Console.WriteLine("SiUSBController.start() : Device index({0}), VendorID(0x{1:X4}), ProductID(0x{2:X4})", i, vidString, pidString);

var vidHex = Util.getHexBytes(vidString);
var pidHex = Util.getHexBytes(pidString);
Array.Reverse(vidHex);
Expand All @@ -62,41 +86,37 @@ public override bool start()

if (vID == (ushort)VendorID && pID == (ushort)ProductID)
{
if (SiUSBController.open(i, ref mDeviceHandle) == true)
if (i == index)
{
Console.WriteLine("SiUSBController.start() : Success open({0})", i);
isDeviceOpen = true;
break;
}
else
{
Console.WriteLine("SiUSBController.start() : Failed open({0})", i);
if (SiUSBController.open(j, ref mDeviceHandle) == false)
{
Console.WriteLine("SiUSBController.start() : Failed open (SiUSB index : {0})", j);
Monitor.Exit(mLock);
this.stop();
return false;
}

Console.WriteLine("SiUSBController.start() : Success open (SiUSB index : {0})", j);

mThreadState = true;
mThread = new Thread(threadFunc);
mThread.Start();
Monitor.Exit(mLock);
return true;
}
i++;
}
}
}

if(isDeviceOpen == false)
{
Console.WriteLine("SiUSBController.start() : Failed device open");
Monitor.Exit(mLock);
this.stop();
return false;
}

mThreadState = true;
mThread = new Thread(threadFunc);
mThread.Start();
Monitor.Exit(mLock);
return true;
}
catch (Exception e)
{
Console.WriteLine("SiUSBController.start() : Failed catch({0})", e.Message);
Monitor.Exit(mLock);
this.stop();
return false;

}
Monitor.Exit(mLock);
this.stop();
return false;
}

public override void stop()
Expand Down
14 changes: 7 additions & 7 deletions FanCtrl/Controller/USBController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,14 @@ public USBController(USBVendorID vendorID, USBProductID productID)
ProductID = productID;
}

public virtual bool start()
public virtual bool start(uint index)
{
return false;
}

public virtual void stop()
{

}

protected void onRecv(byte[] recvArray, int recvDataSize)
{
onRecvHandler(recvArray, recvDataSize);

}

public virtual void send(byte[] buffer)
Expand All @@ -78,5 +73,10 @@ public virtual void send(List<byte[]> bufferList)
{

}

protected void onRecv(byte[] recvArray, int recvDataSize)
{
onRecvHandler(recvArray, recvDataSize);
}
}
}
34 changes: 19 additions & 15 deletions FanCtrl/Controller/WinUSBController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,22 @@ public WinUSBController(USBVendorID vendorID, USBProductID productID, byte endPo
mEndPointOut = endPointOut;
}

public override bool start()
public static uint getDeviceCount(USBVendorID vendorID, USBProductID productID)
{
Monitor.Enter(mLock);
try
{
if (WinUSBController.initUSB() == false)
{
Console.WriteLine("WinUSBController.start() : Failed init");
Monitor.Exit(mLock);
this.stop();
return false;
}
return WinUSBController.count((ushort)vendorID, (ushort)productID);
}
catch { }
return 0;
}

mDeviceHandle = WinUSBController.open((ushort)VendorID, (ushort)ProductID);
public override bool start(uint index)
{
Monitor.Enter(mLock);
try
{
mDeviceHandle = WinUSBController.open(index, (ushort)VendorID, (ushort)ProductID);
mIsDeviceOpen = WinUSBController.isOpen(mDeviceHandle);
if (mIsDeviceOpen == false)
{
Expand Down Expand Up @@ -117,8 +119,7 @@ public override void stop()
{
WinUSBController.close(mDeviceHandle);
mIsDeviceOpen = false;
}
WinUSBController.exitUSB();
}
}
catch { }
Monitor.Exit(mLock);
Expand Down Expand Up @@ -255,13 +256,16 @@ public override void send(List<byte[]> bufferList)
}

[DllImport("libusb.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern bool initUSB();
public static extern bool initUSB();

[DllImport("libusb.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void exitUSB();

[DllImport("libusb.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void exitUSB();
private static extern uint count(ushort vendorID, ushort productID);

[DllImport("libusb.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr open(ushort vendorID, ushort productID);
private static extern IntPtr open(uint index, ushort vendorID, ushort productID);

[DllImport("libusb.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern bool isOpen(IntPtr deviceHandle);
Expand Down
Loading

0 comments on commit 1281ec7

Please sign in to comment.