Repository navigation
Rework new SendControlTransferAsync #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| namespace Device.Net | ||
| { | ||
| #pragma warning disable CA1815 // Override equals and operator equals on value types | ||
| public readonly struct ControlTransferResult | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @josesimoes let's rename ReadResult to TranserResult ,and rename BytesRead to BytesTransferred. Then we can delete this class. You can do that in your branch, or I can change that after the PR. Let me know.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest that we tackle that after this is merged, so the PR is more atomic. |
||
| #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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ReadResult has an implicit operator for converting to arrays
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but the code analyser complains about that....
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @josesimoes what's the issue with analyzer?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @josesimoes in my branch I fixed the code analyzer rule. csharp_style_expression_bodied_operators = true:error
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's good! |
||
|
|
||
| #endregion | ||
|
|
||
| #region Constructor | ||
| public ControlTransferResult( | ||
| uint bytesTransferred, | ||
| byte[] data = null) | ||
| { | ||
| Data = data; | ||
| BytesTransferred = bytesTransferred; | ||
| } | ||
| #endregion | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| namespace Usb.Net | ||
| { | ||
| /// <summary> | ||
| /// Defines constants that indicate the recipient of a USB control transfer. | ||
| /// The recipient is defined in the setup packet of the control request. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See Table 9.2 of section 9.3 of the Universal Serial Bus (USB) specification (<see cref="www.usb.org"/>) | ||
| /// </remarks> | ||
| public enum RequestRecipient | ||
| { | ||
| ///// <summary> | ||
| ///// 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. | ||
| ///// </summary> | ||
| //DefaultInterface = 4, | ||
|
|
||
| /// <summary> | ||
| /// The recipient of the control transfer is the device. | ||
| /// </summary> | ||
| Device = 0, | ||
|
|
||
| /// <summary> | ||
| /// The recipient of the control transfer is the USB interface that is specified in the request. | ||
| /// </summary> | ||
| Interface = 1, | ||
|
|
||
| /// <summary> | ||
| /// The recipient of the control transfer is an endpoint. | ||
| /// </summary> | ||
| Endpoint = 2, | ||
|
|
||
| /// <summary> | ||
| /// The recipient of the control transfer is other. | ||
| /// </summary> | ||
| Other = 3, | ||
| } | ||
| } |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using System; | ||
|
|
||
| namespace Usb.Net | ||
| { | ||
| public class UsbDeviceRequestType | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @josesimoes this looks good. |
||
| { | ||
| /// <summary> | ||
| /// Gets the type of the USB device request. | ||
| /// </summary> | ||
| public RequestType Type { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the direction of the USB device request. | ||
| /// </summary> | ||
| public RequestDirection Direction { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the direction of the USB device request. | ||
| /// </summary> | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josesimoes why is this necessary?
It's exactly the same as ReadResult
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost... the property BytesTransferred name is different to make it clear that's the bytes transferred.
That is properly set for both send and receive operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm...
It's still reading the bytes from the device... Is the distinction that important?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it is. Because when you are sending data it will report how many bytes where actually SENT.
And you're reading data it will tell you how many bytes where actually RECEIVED.
On both that's the gold check for the operation actually succeeding. Not only the return from the call.
Plus on the name, I feel that ReadResult is a bit... short... because it's being used for READ AND WRITE operations, therefore the name ControlTransferResult which works for both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could change the name to TransferResult, and the property to BytesTransferred but this would break all the existing implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're the boss... that's your call...
ReadResult is not used for write operations because there is none, yet. (I guess).
Where there is the name will be kind of awkward, no?!
Suggestion:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope... The LengthTransferred in WinUsb_ControlTransfer reports the actuall number of bytes that were SENT or RECEIVED according to the operation that was requested (RequestDirection.In or RequestDirection.Out).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No @josesimoes. you're pointing out something that I must be misunderstanding. I need to get this straight.
You're saying that the you want to use the structure for the result of sends, and receives, but the Windows API doesn't give us the result of sends - only receive. We only get the read result of what comes back from the device.,
This is the method signature:
I could be wrong, but my understanding is that
LengthTransferredis the length read from the device.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check here: https://docs.microsoft.com/en-us/windows/win32/api/winusb/nf-winusb-winusb_controltransfer
Depending if you're sending data to the device or reading from it, it will tell you how many bytes were actually transferred. I insist! 😉
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. That's where my misunderstanding was. Thanks
@josesimoes