From 5cf8a40f73cd070927fc57160ae5b89217d144ea Mon Sep 17 00:00:00 2001 From: Christian Findlay Date: Sun, 16 Feb 2020 11:57:44 +1100 Subject: [PATCH 01/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4ded4bdf..8574d25b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **Cross platform C# framework for talking to connected devices such as Usb, Serial Port and Hid devices.** -**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** +**I've been to busy to work Device.Net lately because of other projects, but I'm planning to get to the issues soon. Meantime, Pull Requests are super welcome! This framework really needs the community's help** 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/) From d5bac6562ab15eafda70b023a0ada17266828988 Mon Sep 17 00:00:00 2001 From: Christian Findlay Date: Sun, 16 Feb 2020 11:58:01 +1100 Subject: [PATCH 02/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8574d25b..165d81fa 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **Cross platform C# framework for talking to connected devices such as Usb, Serial Port and Hid devices.** -**I've been to busy to work Device.Net lately because of other projects, but I'm planning to get to the issues soon. Meantime, Pull Requests are super welcome! This framework really needs the community's help** +**I've been too busy to work Device.Net lately because of other projects, but I'm planning to get to the issues soon. Meantime, Pull Requests are super welcome! This framework really needs the community's help** 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/) From 46fe29712ec5120213f1ee230a0d651268b4d804 Mon Sep 17 00:00:00 2001 From: Christian Findlay Date: Wed, 17 Jun 2020 08:41:09 +1000 Subject: [PATCH 03/10] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 165d81fa..e9ca0377 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # 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.** From 8c1da02bf3aed9c87388325a22a3f1bee173b6ce Mon Sep 17 00:00:00 2001 From: Christian Findlay Date: Wed, 17 Jun 2020 08:42:04 +1000 Subject: [PATCH 04/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e9ca0377..5bd0d696 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Hid.Net, Usb.Net, SerialPort.Net (Device.Net) -[Follow Me on Twitter](https://twitter.com/intent/follow?screen_name=cfdevelop&tw_p=followbutton) +## [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) From eb0fac2080bd22c70e8019f2dfdb9e3d8f3bf279 Mon Sep 17 00:00:00 2001 From: Christian Findlay Date: Wed, 17 Jun 2020 08:45:56 +1000 Subject: [PATCH 05/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5bd0d696..4d9f0f60 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ **Cross platform C# framework for talking to connected devices such as Usb, Serial Port and Hid devices.** -**I've been too busy to work Device.Net lately because of other projects, but I'm planning to get to the issues soon. Meantime, Pull Requests are super welcome! This framework really needs the community's help** +**Pull Requests are super welcome! This framework really needs the community's help. I should be able to get to a round of fixes very soon** 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/) From b5709aa8b15331c0f5ef8354b69c79635451013b Mon Sep 17 00:00:00 2001 From: Christian Findlay Date: Tue, 18 Aug 2020 09:46:44 +1000 Subject: [PATCH 06/10] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4d9f0f60..8445c455 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ ![diagram](https://github.com/MelbourneDeveloper/Device.Net/blob/master/Diagram.png) +**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!** + **Cross platform C# framework for talking to connected devices such as Usb, Serial Port and Hid devices.** **Pull Requests are super welcome! This framework really needs the community's help. I should be able to get to a round of fixes very soon** From 8642e094850b3c1a1d65f0195e963353de68d2d9 Mon Sep 17 00:00:00 2001 From: Christian Findlay Date: Tue, 18 Aug 2020 09:47:00 +1000 Subject: [PATCH 07/10] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 8445c455..1ee793d6 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,6 @@ **Cross platform C# framework for talking to connected devices such as Usb, Serial Port and Hid devices.** -**Pull Requests are super welcome! This framework really needs the community's help. I should be able to get to a round of fixes very soon** - 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/) ### Currently supports: From e1574ccbd59554ce392a612a9c3c4805a343a2dd Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 28 Aug 2020 20:27:59 +0100 Subject: [PATCH 08/10] Adds attempt at adding control transfers. --- src/Device.Net.LibUsb/LibUsbDevice.cs | 11 +++++++++++ src/Usb.Net/Windows/WindowsUsbInterface.cs | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Device.Net.LibUsb/LibUsbDevice.cs b/src/Device.Net.LibUsb/LibUsbDevice.cs index 79a62cd7..04e63863 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 int ControlTransfer(UsbSetupPacket setupPacket, byte[] buffer) + { + UsbDevice.ControlTransfer(ref setupPacket, buffer, buffer.Length, out int length); + return length; + } + + + public override async Task ReadAsync() { await _WriteAndReadLock.WaitAsync(); 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(() => From 7fd19c0616fb065ee38db0d61b6c1e738d3b4a16 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 28 Aug 2020 21:04:37 +0100 Subject: [PATCH 09/10] Add ControlTransfer to Interface --- src/Device.Net.LibUsb/LibUsbDevice.cs | 4 ++-- src/Usb.Net/IUsbInterface.cs | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Device.Net.LibUsb/LibUsbDevice.cs b/src/Device.Net.LibUsb/LibUsbDevice.cs index 04e63863..a168fea1 100644 --- a/src/Device.Net.LibUsb/LibUsbDevice.cs +++ b/src/Device.Net.LibUsb/LibUsbDevice.cs @@ -98,10 +98,10 @@ await Task.Run(() => //TODO: make async? //TODO: setupPacket not exposed - public int ControlTransfer(UsbSetupPacket setupPacket, byte[] buffer) + public uint ControlTransfer(UsbSetupPacket setupPacket, byte[] buffer) { UsbDevice.ControlTransfer(ref setupPacket, buffer, buffer.Length, out int length); - return length; + return (uint)length; } 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; } From 78833a90471c16c946facc3b47de2b06d70774ed Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 28 Aug 2020 21:54:15 +0100 Subject: [PATCH 10/10] Add ISetupPacket to UsbDevice --- src/Usb.Net/ISetupPacket.cs | 28 ++++++++++++++++++++++++++++ src/Usb.Net/UsbDevice.cs | 5 +++++ 2 files changed, 33 insertions(+) create mode 100644 src/Usb.Net/ISetupPacket.cs 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/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);