Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Hid.Net, Usb.Net, SerialPort.Net (Device.Net)

## [Follow Me on Twitter](https://twitter.com/intent/follow?screen_name=cfdevelop&tw_p=followbutton) ##

![diagram](https://github.com/MelbourneDeveloper/Device.Net/blob/master/Diagram.png)

**Cross platform C# framework for talking to connected devices such as Usb, Serial Port and Hid devices.**
**Check out the [plan for 4.0](https://github.com/MelbourneDeveloper/Device.Net/projects/11). Many enhancements are already done and I'm in the middle of testing with more real devices with real apps. Pull Requests are super welcome!**

**SerialPort.Net is an implementation for Serial Port devices. Currently, Windows is supported, but UWP and others will be on their way. The current version of SerialPort.Net is only in its early stages and may have issues**
**Cross platform C# framework for talking to connected devices such as Usb, Serial Port and Hid devices.**

This framework provides a common Task based Async interface across platforms and device types. This allows for dependency injection so that different types of devices can be used on any platform with the same code. The supported device types are Hid, Serial Port, and USB. Other device types such as Bluetooth and so on may be added in future. Hid.Net is specifically for Hid devices that may be Usb devices. Usb.Net is specifically for Usb devices that don't have a Hid interface. Please visit the [documentation page](https://github.com/MelbourneDeveloper/Device.Net/wiki). Would you you like to [contribute?](https://christianfindlay.com/2019/04/28/calling-all-c-crypto-developers/)

Expand Down
11 changes: 11 additions & 0 deletions src/Device.Net.LibUsb/LibUsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ await Task.Run(() =>
});
}


//TODO: make async?
//TODO: setupPacket not exposed
public uint ControlTransfer(UsbSetupPacket setupPacket, byte[] buffer)
{
UsbDevice.ControlTransfer(ref setupPacket, buffer, buffer.Length, out int length);
return (uint)length;
}



public override async Task<ReadResult> ReadAsync()
{
await _WriteAndReadLock.WaitAsync();
Expand Down
28 changes: 28 additions & 0 deletions src/Usb.Net/ISetupPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

namespace Usb.Net
{
public interface ISetupPacket
{
byte RequestType { get; set; }

/// <summary>
/// This field specifies the particular request. The Type bits in the bmRequestType field modify the meaning of this field. This specification defines values for the bRequest field only when the bits are reset to zero, indicating a standard request.
/// </summary>
byte Request { get; set; }

/// <summary>
/// The contents of this field vary according to the request. It is used to pass a parameter to the device, specific to the request.
/// </summary>
short Value { get; set; }

/// <summary>
/// The contents of this field vary according to the request. It is used to pass a parameter to the device, specific to the request.
/// </summary>
short Index { get; set; }

/// <summary>
/// This field specifies the length of the data transferred during the second phase of the control transfer. The direction of data transfer (host-to-device or device-to-host) is indicated by the Direction bit of the <see cref="RequestType"/> field. If this field is zero, there is no data transfer phase. On an input request, a device must never return more data than is indicated by the wLength value; it may return less. On an output request, wLength will always indicate the exact amount of data to be sent by the host. Device behavior is undefined if the host should send more data than is specified in wLength.
/// </summary>
short Length { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Usb.Net/IUsbInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IUsbInterface : IDisposable
//TODO: Remove these. They should come from the endpoint... or be specified there
ushort ReadBufferSize { get; }
ushort WriteBufferSize { get; }
uint ControlTransfer(ISetupPacket setupPacket, byte[] buffer); //TODO: setup packet is defined differently between libUsb and WinUsb (also possibly convert to async)
Task WriteAsync(byte[] data);
Task<ReadResult> ReadAsync(uint bufferLength);
byte InterfaceNumber { get; }
Expand Down
5 changes: 5 additions & 0 deletions src/Usb.Net/UsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public async Task InitializeAsync()
ConnectedDeviceDefinition = await UsbInterfaceManager.GetConnectedDeviceDefinitionAsync();
}

public uint ControlTransfer(ISetupPacket setupPacket, byte[] buffer)
{
return UsbInterfaceManager.ReadUsbInterface.ControlTransfer(setupPacket, buffer);
}

public override async Task<ReadResult> ReadAsync()
{
if (UsbInterfaceManager.ReadUsbInterface == null) throw new DeviceException(Messages.ErrorMessageNoReadInterfaceSpecified);
Expand Down
11 changes: 11 additions & 0 deletions src/Usb.Net/Windows/WindowsUsbInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public WindowsUsbInterface(SafeFileHandle handle, ILogger logger, ITracer tracer
#endregion

#region Public Methods

//TODO: make async?
//TODO: WINUSB_SETUP_PACKET not exposed
public uint ControlTransfer(WINUSB_SETUP_PACKET setupPacket, byte[] buffer)
{
uint bytesWritten = 0;
//TODO: cannot convert from SafeFileHandle to IntPtr
WinUsbApiCalls.WinUsb_ControlTransfer(_SafeFileHandle, setupPacket, buffer, (uint) buffer.Length, ref bytesWritten, IntPtr.Zero); //last pointer is overlapped structure for async operations
return bytesWritten;
}

public async Task<ReadResult> ReadAsync(uint bufferLength)
{
return await Task.Run(() =>
Expand Down