diff --git a/src/Device.Net.UnitTests/IntegrationTests.cs b/src/Device.Net.UnitTests/IntegrationTests.cs index 0b6a6932..26a991cb 100644 --- a/src/Device.Net.UnitTests/IntegrationTests.cs +++ b/src/Device.Net.UnitTests/IntegrationTests.cs @@ -67,43 +67,54 @@ public async Task TestConnectToSTMDFUMode2() var windowsUsbDevice = new UsbDevice(deviceID, new WindowsUsbInterfaceManager(deviceID)); await windowsUsbDevice.InitializeAsync(); - const int USB_DIR_IN = 128; //0x80 - const int DFU_RequestType = 0x21; // '2' => Class request ; '1' => to interface const byte DFU_GETSTATUS = 0x03; - //var buffer = new byte[6]; + const byte DFU_CLEARSTATUS = 0x04; - //Get status? Is this correct? + const byte STATE_DFU_IDLE = 0x02; + + // setup packet to send a DFU Clear Status command var setupPacket = new SetupPacket ( - requestType: DFU_RequestType | USB_DIR_IN, + requestType: new UsbDeviceRequestType( + RequestDirection.In, + RequestType.Class, + RequestRecipient.Interface), + request: DFU_CLEARSTATUS, + length: 0 + ); + + // send control transfer, no need to check the return + _ = await windowsUsbDevice.SendControlTransferAsync(setupPacket); + + // setup packet to send a DFU GetStatus command + setupPacket = new SetupPacket + ( + requestType: new UsbDeviceRequestType( + RequestDirection.In, + RequestType.Class, + RequestRecipient.Interface), request: DFU_GETSTATUS, length: 6 ); - //According to Florian get status - //var setupPacket = new WINUSB_SETUP_PACKET - //{ - // RequestType = 161, - // Request = 3, - // Length = (ushort)buffer.Length, - // Value = 0, - // Index = 0 - //}; - - //// DFU CLEAR SETUP PACKET - //var setupPacket = new WINUSB_SETUP_PACKET - //{ - // RequestType = 33, - // Request = 4, - // Length = 0, - // Value = 2, - // Index = 0 - //}; - - var result = await windowsUsbDevice.SendControlTransferAsync(setupPacket); + var dfuStatus = new ControlTransferResult(); - //Assert that the response part meets the specification - Assert.IsTrue(new byte[] { 0x02 }.Equals(result.Data)); + // because the device is not always able to reply, may take a couple of attempts to get one + for (var attempt = 0; attempt < 3; attempt++) + { + dfuStatus = await windowsUsbDevice.SendControlTransferAsync(setupPacket); + + if (dfuStatus.BytesTransferred == 0) + { + // wait for a little while before the next attempt + await Task.Delay(250); + } + } + + // Assert that the received buffer has the requested lenght ADN that DFU State (at position 4) is STATE_DFU_IDLE + Assert.IsTrue( + dfuStatus.BytesTransferred == setupPacket.Length && + dfuStatus.Data[4] == STATE_DFU_IDLE); } #endif diff --git a/src/Device.Net/ControlTransferResult.cs b/src/Device.Net/ControlTransferResult.cs new file mode 100644 index 00000000..06454abc --- /dev/null +++ b/src/Device.Net/ControlTransferResult.cs @@ -0,0 +1,30 @@ +namespace Device.Net +{ +#pragma warning disable CA1815 // Override equals and operator equals on value types + public readonly struct ControlTransferResult +#pragma warning restore CA1815 // Override equals and operator equals on value types + { + #region Public Properties +#pragma warning disable CA1819 // Properties should not return arrays + public byte[] Data { get; } +#pragma warning restore CA1819 // Properties should not return arrays + + public uint BytesTransferred { get; } + #endregion + + #region Conversion Operators + public byte[] ToByteArray() => Data; + + #endregion + + #region Constructor + public ControlTransferResult( + uint bytesTransferred, + byte[] data = null) + { + Data = data; + BytesTransferred = bytesTransferred; + } + #endregion + } +} \ No newline at end of file diff --git a/src/Usb.Net/Android/AndroidUsbInterface.cs b/src/Usb.Net/Android/AndroidUsbInterface.cs index e5aaa790..afa92bc5 100644 --- a/src/Usb.Net/Android/AndroidUsbInterface.cs +++ b/src/Usb.Net/Android/AndroidUsbInterface.cs @@ -139,7 +139,7 @@ public override Task ClaimInterface() } #pragma warning disable IDE0060 // Remove unused parameter - public Task SendControlTransferAsync(SetupPacket setupPacket, byte[] buffer = null, CancellationToken cancellationToken = default) => throw new NotImplementedException(); + public Task SendControlTransferAsync(SetupPacket setupPacket, byte[] buffer = null, CancellationToken cancellationToken = default) => throw new NotImplementedException(); #pragma warning restore IDE0060 // Remove unused parameter #endregion } diff --git a/src/Usb.Net/IUsbInterface.cs b/src/Usb.Net/IUsbInterface.cs index 7db46979..967acfe4 100644 --- a/src/Usb.Net/IUsbInterface.cs +++ b/src/Usb.Net/IUsbInterface.cs @@ -34,6 +34,6 @@ public interface IUsbInterface : IDisposable /// Can we just create the buffer in the method? /// Or, do we sometimes need to send data here? /// - Task SendControlTransferAsync(SetupPacket setupPacket, byte[] buffer = null, CancellationToken cancellationToken = default); + Task SendControlTransferAsync(SetupPacket setupPacket, byte[] buffer = null, CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/Usb.Net/UsbTransferDirection.cs b/src/Usb.Net/RequestDirection.cs similarity index 84% rename from src/Usb.Net/UsbTransferDirection.cs rename to src/Usb.Net/RequestDirection.cs index 00fe6f80..8a594047 100644 --- a/src/Usb.Net/UsbTransferDirection.cs +++ b/src/Usb.Net/RequestDirection.cs @@ -4,10 +4,11 @@ /// Defines constants that indicate the direction of USB transfer: IN or OUT transfers. /// /// + /// See 9.3 of the Universal Serial Bus (USB) specification () /// The direction of a USB transfer is determined from the host side and not the target USB device. /// In an IN transfer, data moves from the device to the host. In an OUT transfer, data moves from the host to the device. /// - public enum UsbTransferDirection + public enum RequestDirection { /// /// Indicates an IN transfer from the device to the host. diff --git a/src/Usb.Net/RequestRecipient.cs b/src/Usb.Net/RequestRecipient.cs new file mode 100644 index 00000000..5e5301ee --- /dev/null +++ b/src/Usb.Net/RequestRecipient.cs @@ -0,0 +1,41 @@ +namespace Usb.Net +{ + /// + /// Defines constants that indicate the recipient of a USB control transfer. + /// The recipient is defined in the setup packet of the control request. + /// + /// + /// See Table 9.2 of section 9.3 of the Universal Serial Bus (USB) specification () + /// + public enum RequestRecipient + { + ///// + ///// The recipient of the control transfer is the default (or the first) USB interface in the selected configuration of the device. + ///// If the recipient is the first interface of the active configuration(DefaultInterface ), + ///// SendControlInTransferAsync and SendControlOutTransferAsync methods overwrite the low byte of UsbSetupPacket.Index with the interface number of the default interface. + ///// + ///// By using this value, an app can omit the interface number in an interface-recipient request. + ///// + //DefaultInterface = 4, + + /// + /// The recipient of the control transfer is the device. + /// + Device = 0, + + /// + /// The recipient of the control transfer is the USB interface that is specified in the request. + /// + Interface = 1, + + /// + /// The recipient of the control transfer is an endpoint. + /// + Endpoint = 2, + + /// + /// The recipient of the control transfer is other. + /// + Other = 3, + } +} diff --git a/src/Usb.Net/UsbControlTransferType.cs b/src/Usb.Net/RequestType.cs similarity index 79% rename from src/Usb.Net/UsbControlTransferType.cs rename to src/Usb.Net/RequestType.cs index d88a5e30..52b99aa0 100644 --- a/src/Usb.Net/UsbControlTransferType.cs +++ b/src/Usb.Net/RequestType.cs @@ -1,9 +1,12 @@ namespace Usb.Net { /// - /// Defines constants that indicate the type of USB control transfer: standard, class, or vendor. + /// Defines the type of USB device request. /// - public enum UsbControlTransferType + /// + /// See 9.3 of the Universal Serial Bus (USB) specification () + /// + public enum RequestType { /// /// Indicates a class-specific USB control request described by a specific device class specification. diff --git a/src/Usb.Net/SetupPacket.cs b/src/Usb.Net/SetupPacket.cs index cad220f2..a9b31f15 100644 --- a/src/Usb.Net/SetupPacket.cs +++ b/src/Usb.Net/SetupPacket.cs @@ -2,8 +2,14 @@ { public class SetupPacket { + /// + /// Size of to be used in byte arrays. + /// + // this is the "byte" size of the properties below + public const int SetupPacketSize = 1 + 1 + 2 + 2 + 2; + #region Public Properties - public byte RequestType { get; } + public UsbDeviceRequestType RequestType { get; } public byte Request { get; } public ushort Value { get; } public ushort Index { get; } @@ -11,14 +17,12 @@ public class SetupPacket #endregion #region Constructors - public SetupPacket - ( - byte requestType, - byte request, - ushort value = 0, - ushort index = 0, - ushort length = 0 - ) + public SetupPacket( + UsbDeviceRequestType requestType, + byte request, + ushort value = 0, + ushort index = 0, + ushort length = 0) { RequestType = requestType; Request = request; diff --git a/src/Usb.Net/UWP/UWPUsbInterface.cs b/src/Usb.Net/UWP/UWPUsbInterface.cs index ae055513..ee15352d 100644 --- a/src/Usb.Net/UWP/UWPUsbInterface.cs +++ b/src/Usb.Net/UWP/UWPUsbInterface.cs @@ -134,6 +134,11 @@ public async Task WriteAsync(byte[] data, CancellationToken cancellationToken = throw; } } + +#pragma warning disable IDE0060 // Remove unused parameter + public Task SendControlTransferAsync(SetupPacket setupPacket, byte[] buffer = null, CancellationToken cancellationToken = default) => throw new NotImplementedException(); +#pragma warning restore IDE0060 // Remove unused parameter + #endregion #region IDisposable Support @@ -142,10 +147,6 @@ public void Dispose() if (disposedValue) return; disposedValue = true; } - -#pragma warning disable IDE0060 // Remove unused parameter - public Task SendControlTransferAsync(SetupPacket setupPacket, byte[] buffer = null, CancellationToken cancellationToken = default) => throw new NotImplementedException(); -#pragma warning restore IDE0060 // Remove unused parameter #endregion } } diff --git a/src/Usb.Net/UsbControlRecipient.cs b/src/Usb.Net/UsbControlRecipient.cs deleted file mode 100644 index 47b3a5f0..00000000 --- a/src/Usb.Net/UsbControlRecipient.cs +++ /dev/null @@ -1,40 +0,0 @@ -namespace Usb.Net -{ - /// - /// Defines constants that indicate the recipient of a USB control transfer. - /// The recipient is defined in the setup packet of the control request. See Table 9.2 of section 9.3 of the Universal Serial Bus (USB) specification (). - /// - public enum UsbControlRecipient - { - /// - /// The recipient of the control transfer is the default (or the first) USB interface in the selected configuration of the device. - /// If the recipient is the first interface of the active configuration(DefaultInterface ), - /// SendControlInTransferAsync and SendControlOutTransferAsync methods overwrite the low byte of UsbSetupPacket.Index with the interface number of the default interface. - /// - /// By using this value, an app can omit the interface number in an interface-recipient request. - /// - DefaultInterface = 4, - - /// - /// The recipient of the control transfer is the device. - /// - Device = 0, - - /// - /// The recipient of the control transfer is an endpoint. - /// - Endpoint = 2, - - /// - /// The recipient of the control transfer is other. - /// - Other = 3, - - /// - /// The recipient of the control transfer is the USB interface that is specified in the request. - /// - SpecifiedInterface = 1 - - - } -} diff --git a/src/Usb.Net/UsbControlRequestType.cs b/src/Usb.Net/UsbControlRequestType.cs deleted file mode 100644 index b5d90ae2..00000000 --- a/src/Usb.Net/UsbControlRequestType.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace Usb.Net -{ - public class UsbControlRequestType - { - /// - /// Gets or sets the bmRequestType value as a byte. - /// - public byte AsByte { get; set; } - - /// - /// Gets or sets the type of USB control transfer: standard, class, or vendor. - /// - public UsbControlTransferType ControlTransferType { get; set; } - - /// - /// Gets or sets the direction of the USB control transfer. - /// - public UsbTransferDirection Direction { get; set; } - - /// - /// Gets or sets the direction of the USB control transfer. - /// - public UsbControlRecipient Recipient { get; set; } - - } -} diff --git a/src/Usb.Net/UsbDeviceExtensions.cs b/src/Usb.Net/UsbDeviceExtensions.cs index 21399c69..fc61bfa2 100644 --- a/src/Usb.Net/UsbDeviceExtensions.cs +++ b/src/Usb.Net/UsbDeviceExtensions.cs @@ -8,7 +8,7 @@ namespace Usb.Net { public static class UsbDeviceExtensions { - public static Task SendControlTransferAsync(this IUsbDevice usbDevice, SetupPacket setupPacket, byte[] buffer = null, CancellationToken cancellationToken = default) + public static Task SendControlTransferAsync(this IUsbDevice usbDevice, SetupPacket setupPacket, byte[] buffer = null, CancellationToken cancellationToken = default) => usbDevice == null ? throw new ArgumentNullException(nameof(usbDevice)) : (usbDevice.UsbInterfaceManager.UsbInterfaces.FirstOrDefault() ?? throw new Exception("There are no interfaces to send a control transfer to")) diff --git a/src/Usb.Net/UsbDeviceRequestType.cs b/src/Usb.Net/UsbDeviceRequestType.cs new file mode 100644 index 00000000..b576148e --- /dev/null +++ b/src/Usb.Net/UsbDeviceRequestType.cs @@ -0,0 +1,40 @@ +using System; + +namespace Usb.Net +{ + public class UsbDeviceRequestType + { + /// + /// Gets the type of the USB device request. + /// + public RequestType Type { get; } + + /// + /// Gets the direction of the USB device request. + /// + public RequestDirection Direction { get; } + + /// + /// Gets the direction of the USB device request. + /// + public RequestRecipient Recipient { get; } + + public UsbDeviceRequestType( + RequestDirection direction, + RequestType type, + RequestRecipient recipient) + { + Direction = direction; + Type = type; + Recipient = recipient; + } + + public byte ToByte() + { + return (byte)( + ((byte)Direction << 7) + + ((byte)Type << 5) + + (byte)Recipient); + } + } +} diff --git a/src/Usb.Net/Windows/WindowsUsbExtensions.cs b/src/Usb.Net/Windows/WindowsUsbExtensions.cs index 2f20fbc8..09ec7bbf 100644 --- a/src/Usb.Net/Windows/WindowsUsbExtensions.cs +++ b/src/Usb.Net/Windows/WindowsUsbExtensions.cs @@ -10,7 +10,7 @@ public static WINUSB_SETUP_PACKET ToWindowsSetupPacket(this SetupPacket setupPac Index = setupPacket.Length, Length = setupPacket.Length, Request = setupPacket.Request, - RequestType = setupPacket.RequestType, + RequestType = setupPacket.RequestType.ToByte(), Value = setupPacket.Value }; } diff --git a/src/Usb.Net/Windows/WindowsUsbInterface.cs b/src/Usb.Net/Windows/WindowsUsbInterface.cs index d02e13f8..6b600601 100644 --- a/src/Usb.Net/Windows/WindowsUsbInterface.cs +++ b/src/Usb.Net/Windows/WindowsUsbInterface.cs @@ -71,29 +71,59 @@ public void Dispose() GC.SuppressFinalize(this); } - public Task SendControlTransferAsync(SetupPacket setupPacket, byte[] inputBuffer, CancellationToken cancellationToken = default) + public Task SendControlTransferAsync(SetupPacket setupPacket, byte[] buffer, CancellationToken cancellationToken = default) { return Task.Run(() => { - uint bytesWritten = 0; + var transferBuffer = Array.Empty(); - if (setupPacket.Length > 0 && inputBuffer == null) + uint bytesTransferred = 0; + + if (setupPacket.Length > 0) { - inputBuffer = new byte[setupPacket.Length]; + // the request has data exchange + // which direction? + if (setupPacket.RequestType.Direction == RequestDirection.In) + { + // data is to be received + // create buffer of the appropriate size + transferBuffer = new byte[setupPacket.Length]; + } + else + { + // data is to be sent + // TODO + transferBuffer = new byte[SetupPacket.SetupPacketSize + setupPacket.Length]; + // check buffer == null + } } - //Make a copy so we don't mess with the array passed in - var bufferCopy = new byte[inputBuffer.Length]; - Array.Copy(inputBuffer, bufferCopy, inputBuffer.Length); + ////Make a copy so we don't mess with the array passed in + //var bufferCopy = new byte[buffer.Length]; + //Array.Copy(buffer, bufferCopy, buffer.Length); - var isSuccess = WinUsbApiCalls.WinUsb_ControlTransfer(_SafeFileHandle.DangerousGetHandle(), setupPacket.ToWindowsSetupPacket(), bufferCopy, (uint)bufferCopy.Length, ref bytesWritten, IntPtr.Zero); + var isSuccess = WinUsbApiCalls.WinUsb_ControlTransfer(_SafeFileHandle.DangerousGetHandle(), setupPacket.ToWindowsSetupPacket(), transferBuffer, (uint)transferBuffer.Length, ref bytesTransferred, IntPtr.Zero); if (!isSuccess) { WindowsDeviceBase.HandleError(isSuccess, "Couldn't do a control transfer"); } - return new ReadResult(inputBuffer, bytesWritten); + + /* Unmerged change from project 'Usb.Net (net45)' + Before: + return new ReadResult(buffer, bytesWritten); + After: + return new Device.Net.ReadResult(buffer, bytesWritten); + */ + + /* Unmerged change from project 'Usb.Net (net45)' + Before: + return new ControlTransferResult(receiveBuffer, bytesTransferred); + After: + return new Device.Net.ControlTransferResult(receiveBuffer, bytesTransferred); + */ + return new ControlTransferResult(bytesTransferred, transferBuffer); }, cancellationToken); }