Repository navigation
Rework new SendControlTransferAsync - #167
MelbourneDeveloper merged 1 commit into
Conversation
josesimoes
commented
Nov 11, 2020
- Rework USB Device Request and related.
- Update SetupPacket accordingly.
- Add new return class ControlTransferResult.
- Update SendControlTransferAsync accordingly.
- Rework SendControlTransferAsync to better handle send and receive control transfer requests.
- Rework TestConnectToSTMDFUMode2 test.
aeca69b to
83037d9
Compare
| @@ -0,0 +1,30 @@ | |||
| namespace Device.Net | |||
There was a problem hiding this comment.
@josesimoes why is this necessary?
It's exactly the same as ReadResult
There was a problem hiding this comment.
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.
Hmm...
It's still reading the bytes from the device... Is the distinction that important?
There was a problem hiding this comment.
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.
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.
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:
- keep the existing ReadResult and methods and add the [Obsolete] attribute to them.
- Implement the "new" TransferResult with those calls.
- At some future version remove the deprecated ones.
There was a problem hiding this comment.
@josesimoes after reading this a few times, I'm more confused now.
When we do a control transfer, we send a buffer of data to the device. Then, the device sends a buffer back with different data. So, technically, we may be interested in the transfer length for both operations (send and receive). However, the Windows API does not give us that. We only get the length of the data that was read back from the device. Isn't that understanding correct?
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.
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:
public static extern bool WinUsb_ControlTransfer(IntPtr InterfaceHandle, WINUSB_SETUP_PACKET SetupPacket, byte[] Buffer, uint BufferLength, ref uint LengthTransferred, IntPtr Overlapped);I could be wrong, but my understanding is that LengthTransferred is the length read from the device.
There was a problem hiding this comment.
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! 😉
There was a problem hiding this comment.
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).
OK. That's where my misunderstanding was. Thanks
| #endregion | ||
|
|
||
| #region Conversion Operators | ||
| public byte[] ToByteArray() => Data; |
There was a problem hiding this comment.
ReadResult has an implicit operator for converting to arrays
There was a problem hiding this comment.
Yes, but the code analyser complains about that....
I'm quite all right if you want to default to that implicit operation here too.
There was a problem hiding this comment.
@josesimoes what's the issue with analyzer?
There was a problem hiding this comment.
@josesimoes in my branch I fixed the code analyzer rule.
csharp_style_expression_bodied_operators = true:error
|
|
||
| namespace Usb.Net | ||
| { | ||
| public class UsbDeviceRequestType |
There was a problem hiding this comment.
@josesimoes this looks good.
| { | ||
| // data is to be sent | ||
| // TODO | ||
| transferBuffer = new byte[SetupPacket.SetupPacketSize + setupPacket.Length]; |
There was a problem hiding this comment.
@josesimoes why not move the array.copy code here?
There was a problem hiding this comment.
Still haven't tested this fully, but I think the buffer has to be done concatenating the setup packet and the data buffer.
There was a problem hiding this comment.
@josesimoes OK yes. We definitely need a test for this before we move forward.
|
@josesimoes the only issue I have is ControlTransferResult, but other than that I'm happy to accept this PR. Thanks. We do need to test sending some data as part of the control transfer so I will put the array copy part back, but I you may be right that that is not the correct way. We will need a test for that. |
josesimoes
left a comment
There was a problem hiding this comment.
@josesimoes the only issue I have is ControlTransferResult, but other than that I'm happy to accept this PR. Thanks.
We do need to test sending some data as part of the control transfer so I will put the array copy part back, but I you may be right that that is not the correct way. We will need a test for that.
As soon as we are OK with this I'll start with the "sending data" part.
Just wanted to have a feedback if this was heading on the right direction before moving any further. 😉
| #endregion | ||
|
|
||
| #region Conversion Operators | ||
| public byte[] ToByteArray() => Data; |
| @@ -0,0 +1,30 @@ | |||
| namespace Device.Net | |||
There was a problem hiding this comment.
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.
| namespace Device.Net | ||
| { | ||
| #pragma warning disable CA1815 // Override equals and operator equals on value types | ||
| public readonly struct ControlTransferResult |
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
Suggest that we tackle that after this is merged, so the PR is more atomic.
755bfff
into
MelbourneDeveloper:develop-controltransfer-Refactor