Code cleanup

This commit is contained in:
Gustave Monce
2019-12-22 12:25:48 +01:00
parent 0e6f905809
commit a2a1c2302b
149 changed files with 3980 additions and 4088 deletions
+57 -58
View File
@@ -9,22 +9,21 @@
* See http://www.lvr.com/winusb.htm for more information
*/
using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel;
namespace MadWizard.WinUSBNet.API
{
/// <summary>
/// Wrapper for a WinUSB device dealing with the WinUSB and additional interface handles
/// </summary>
partial class WinUSBDevice : IDisposable
{
/// <summary>
/// Wrapper for a WinUSB device dealing with the WinUSB and additional interface handles
/// </summary>
partial class WinUSBDevice : IDisposable
{
private bool _disposed = false;
private SafeFileHandle _deviceHandle;
private SafeFileHandle _deviceHandle;
private IntPtr _winUsbHandle = IntPtr.Zero;
private IntPtr[] _addInterfaces = null;
public WinUSBDevice()
@@ -57,13 +56,13 @@ namespace MadWizard.WinUSBNet.API
if (disposing)
{
// Dispose managed resources
// Dispose managed resources
if (_deviceHandle != null && !_deviceHandle.IsInvalid)
_deviceHandle.Dispose();
_deviceHandle = null;
}
// Dispose unmanaged resources
// Dispose unmanaged resources
FreeWinUSB();
_disposed = true;
}
@@ -105,8 +104,8 @@ namespace MadWizard.WinUSBNet.API
bool success = WinUsb_GetDescriptor(_winUsbHandle, USB_STRING_DESCRIPTOR_TYPE,
index, 0, buffer, (uint)buffer.Length, out transfered);
if (!success)
throw APIException.Win32("Failed to get USB string descriptor (" + index + "): 0x" + Marshal.GetLastWin32Error().ToString("X8"));
throw APIException.Win32("Failed to get USB string descriptor (" + index + "): 0x" + Marshal.GetLastWin32Error().ToString("X8"));
int length = buffer[0] - 2;
if (length <= 0)
return null;
@@ -115,10 +114,10 @@ namespace MadWizard.WinUSBNet.API
}
public void ControlTransfer(byte requestType, byte request, ushort value, ushort index, ushort length, byte[] data)
{
uint bytesReturned = 0;
WINUSB_SETUP_PACKET setupPacket;
{
uint bytesReturned = 0;
WINUSB_SETUP_PACKET setupPacket;
setupPacket.RequestType = requestType;
setupPacket.Request = request;
setupPacket.Value = value;
@@ -128,26 +127,26 @@ namespace MadWizard.WinUSBNet.API
bool success = WinUsb_ControlTransfer(_winUsbHandle, setupPacket, data, length, ref bytesReturned, IntPtr.Zero);
if (!success) // todo check bytes returned?
throw APIException.Win32("Control transfer on WinUSB device failed.");
}
}
public void OpenDevice(string devicePathName)
{
public void OpenDevice(string devicePathName)
{
try
{
_deviceHandle = FileIO.CreateFile(devicePathName,
(FileIO.GENERIC_WRITE | FileIO.GENERIC_READ),
FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE,
IntPtr.Zero,
FileIO.OPEN_EXISTING,
FileIO.FILE_ATTRIBUTE_NORMAL | FileIO.FILE_FLAG_OVERLAPPED,
0);
_deviceHandle = FileIO.CreateFile(devicePathName,
(FileIO.GENERIC_WRITE | FileIO.GENERIC_READ),
FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE,
IntPtr.Zero,
FileIO.OPEN_EXISTING,
FileIO.FILE_ATTRIBUTE_NORMAL | FileIO.FILE_FLAG_OVERLAPPED,
0);
if (_deviceHandle.IsInvalid)
throw APIException.Win32("Failed to open WinUSB device handle.");
InitializeDevice();
}
catch(Exception)
catch (Exception)
{
if (_deviceHandle != null)
{
@@ -157,7 +156,7 @@ namespace MadWizard.WinUSBNet.API
FreeWinUSB();
throw;
}
}
}
private IntPtr InterfaceHandle(int index)
{
@@ -194,11 +193,11 @@ namespace MadWizard.WinUSBNet.API
pipes = pipeList.ToArray();
}
private void InitializeDevice()
{
bool success;
private void InitializeDevice()
{
bool success;
success = WinUsb_Initialize(_deviceHandle, ref _winUsbHandle);
success = WinUsb_Initialize(_deviceHandle, ref _winUsbHandle);
if (!success)
throw APIException.Win32("Failed to initialize WinUSB handle. Device might not be connected.");
@@ -237,10 +236,10 @@ namespace MadWizard.WinUSBNet.API
// Bind handle (needed for overlapped I/O thread pool)
ThreadPool.BindHandle(_deviceHandle);
// TODO: bind interface handles as well? doesn't seem to be necessary
}
}
public void ReadPipe(int ifaceIndex, byte pipeID, byte[] buffer, int offset, int bytesToRead, out uint bytesRead)
{
public void ReadPipe(int ifaceIndex, byte pipeID, byte[] buffer, int offset, int bytesToRead, out uint bytesRead)
{
bool success;
unsafe
{
@@ -254,7 +253,7 @@ namespace MadWizard.WinUSBNet.API
}
if (!success)
throw APIException.Win32("Failed to read pipe on WinUSB device.");
}
}
private unsafe void HandleOverlappedAPI(bool success, string errorMessage, NativeOverlapped* pOverlapped, USBAsyncResult result, int bytesTransfered)
{
@@ -276,13 +275,13 @@ namespace MadWizard.WinUSBNet.API
result.OnCompletion(true, null, bytesTransfered, false);
// is the callback still called in this case?? todo
}
}
public void ReadPipeOverlapped(int ifaceIndex, byte pipeID, byte[] buffer, int offset, int bytesToRead, USBAsyncResult result)
{
Overlapped overlapped = new Overlapped();
overlapped.AsyncResult = result;
unsafe
@@ -313,7 +312,7 @@ namespace MadWizard.WinUSBNet.API
uint bytesWritten;
pOverlapped = overlapped.Pack(PipeIOCallback, buffer);
bool success;
// Buffer is pinned already by overlapped.Pack
fixed (byte* pBuffer = buffer)
@@ -326,13 +325,13 @@ namespace MadWizard.WinUSBNet.API
}
}
public void ControlTransferOverlapped(byte requestType, byte request, ushort value, ushort index, ushort length, byte[] data, USBAsyncResult result)
{
uint bytesReturned = 0;
WINUSB_SETUP_PACKET setupPacket;
{
uint bytesReturned = 0;
WINUSB_SETUP_PACKET setupPacket;
setupPacket.RequestType = requestType;
setupPacket.Request = request;
setupPacket.Value = value;
@@ -349,7 +348,7 @@ namespace MadWizard.WinUSBNet.API
bool success = WinUsb_ControlTransfer(_winUsbHandle, setupPacket, data, length, ref bytesReturned, pOverlapped);
HandleOverlappedAPI(success, "Asynchronous control transfer on WinUSB device failed.", pOverlapped, result, (int)bytesReturned);
}
}
}
private unsafe void PipeIOCallback(uint errorCode, uint numBytes, NativeOverlapped* pOverlapped)
{
@@ -393,7 +392,7 @@ namespace MadWizard.WinUSBNet.API
}
public void WritePipe(int ifaceIndex, byte pipeID, byte[] buffer, int offset, int length)
{
{
uint bytesWritten;
bool success;
unsafe
@@ -408,8 +407,8 @@ namespace MadWizard.WinUSBNet.API
}
if (!success || (bytesWritten != length))
throw APIException.Win32("Failed to write pipe on WinUSB device.");
}
}
public void FlushPipe(int ifaceIndex, byte pipeID)
{
@@ -419,29 +418,29 @@ namespace MadWizard.WinUSBNet.API
}
public void SetPipePolicy(int ifaceIndex, byte pipeID, POLICY_TYPE policyType, bool value)
{
{
byte byteVal = (byte)(value ? 1 : 0);
bool success = WinUsb_SetPipePolicy(InterfaceHandle(ifaceIndex), pipeID, (uint)policyType, 1, ref byteVal);
if (!success)
throw APIException.Win32("Failed to set WinUSB pipe policy.");
}
}
public void SetPipePolicy(int ifaceIndex, byte pipeID, POLICY_TYPE policyType, uint value)
{
{
bool success = WinUsb_SetPipePolicy(InterfaceHandle(ifaceIndex), pipeID, (uint)policyType, 4, ref value);
if (!success)
throw APIException.Win32("Failed to set WinUSB pipe policy.");
}
}
public bool GetPipePolicyBool(int ifaceIndex, byte pipeID, POLICY_TYPE policyType)
{
byte result;
uint length = 1;
bool success = WinUsb_GetPipePolicy(InterfaceHandle(ifaceIndex), pipeID, (uint)policyType, ref length, out result);
if (!success || length != 1)
throw APIException.Win32("Failed to get WinUSB pipe policy.");
@@ -451,7 +450,7 @@ namespace MadWizard.WinUSBNet.API
public uint GetPipePolicyUInt(int ifaceIndex, byte pipeID, POLICY_TYPE policyType)
{
uint result;
uint length = 4;
bool success = WinUsb_GetPipePolicy(InterfaceHandle(ifaceIndex), pipeID, (uint)policyType, ref length, out result);
@@ -460,5 +459,5 @@ namespace MadWizard.WinUSBNet.API
throw APIException.Win32("Failed to get WinUSB pipe policy.");
return result;
}
}
}
}