diff --git a/README.md b/README.md index 4ded4bdf..1ee793d6 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/src/Device.Net.LibUsb/LibUsbDevice.cs b/src/Device.Net.LibUsb/LibUsbDevice.cs index 79a62cd7..a168fea1 100644 --- a/src/Device.Net.LibUsb/LibUsbDevice.cs +++ b/src/Device.Net.LibUsb/LibUsbDevice.cs @@ -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 ReadAsync() { await _WriteAndReadLock.WaitAsync(); diff --git a/src/Usb.Net/ISetupPacket.cs b/src/Usb.Net/ISetupPacket.cs new file mode 100644 index 00000000..1d1c1d7c --- /dev/null +++ b/src/Usb.Net/ISetupPacket.cs @@ -0,0 +1,28 @@ + +namespace Usb.Net +{ + public interface ISetupPacket + { + byte RequestType { get; set; } + + /// + /// 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. + /// + byte Request { get; set; } + + /// + /// The contents of this field vary according to the request. It is used to pass a parameter to the device, specific to the request. + /// + short Value { get; set; } + + /// + /// The contents of this field vary according to the request. It is used to pass a parameter to the device, specific to the request. + /// + short Index { get; set; } + + /// + /// 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 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. + /// + short Length { get; set; } + } +} diff --git a/src/Usb.Net/IUsbInterface.cs b/src/Usb.Net/IUsbInterface.cs index 195ca5df..ae942992 100644 --- a/src/Usb.Net/IUsbInterface.cs +++ b/src/Usb.Net/IUsbInterface.cs @@ -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 ReadAsync(uint bufferLength); byte InterfaceNumber { get; } diff --git a/src/Usb.Net/UsbDevice.cs b/src/Usb.Net/UsbDevice.cs index 80580b14..c4cc591c 100644 --- a/src/Usb.Net/UsbDevice.cs +++ b/src/Usb.Net/UsbDevice.cs @@ -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 ReadAsync() { if (UsbInterfaceManager.ReadUsbInterface == null) throw new DeviceException(Messages.ErrorMessageNoReadInterfaceSpecified); diff --git a/src/Usb.Net/Windows/WindowsUsbInterface.cs b/src/Usb.Net/Windows/WindowsUsbInterface.cs index e9af4ae2..caa79486 100644 --- a/src/Usb.Net/Windows/WindowsUsbInterface.cs +++ b/src/Usb.Net/Windows/WindowsUsbInterface.cs @@ -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 ReadAsync(uint bufferLength) { return await Task.Run(() =>