Skip to content

Rework new SendControlTransferAsync - #167

Merged
MelbourneDeveloper merged 1 commit into
MelbourneDeveloper:develop-controltransfer-Refactorfrom
josesimoes:rework-control-transfer
Nov 11, 2020
Merged

MelbourneDeveloper merged 1 commit into
MelbourneDeveloper:develop-controltransfer-Refactorfrom
josesimoes:rework-control-transfer

Conversation

@josesimoes

Copy link
Copy Markdown
  • 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.

@josesimoes
josesimoes force-pushed the rework-control-transfer branch from aeca69b to 83037d9 Compare November 11, 2020 17:58
@@ -0,0 +1,30 @@
namespace Device.Net

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Author

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.

Copy link
Copy Markdown
Owner

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?

Copy link
Copy Markdown
Author

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.

Copy link
Copy Markdown
Owner

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.

Copy link
Copy Markdown
Author

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:

  1. keep the existing ReadResult and methods and add the [Obsolete] attribute to them.
  2. Implement the "new" TransferResult with those calls.
  3. At some future version remove the deprecated ones.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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).

Copy link
Copy Markdown
Owner

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:

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.

Copy link
Copy Markdown
Author

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! 😉

@MelbourneDeveloper MelbourneDeveloper Nov 11, 2020 •

Copy link
Copy Markdown
Owner

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).

OK. That's where my misunderstanding was. Thanks

@josesimoes

#endregion

#region Conversion Operators
public byte[] ToByteArray() => Data;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadResult has an implicit operator for converting to arrays

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the code analyser complains about that....
I'm quite all right if you want to default to that implicit operation here too.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josesimoes what's the issue with analyzer?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good!


namespace Usb.Net
{
public class UsbDeviceRequestType

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josesimoes this looks good.

{
// data is to be sent
// TODO
transferBuffer = new byte[SetupPacket.SetupPacketSize + setupPacket.Length];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josesimoes why not move the array.copy code here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still haven't tested this fully, but I think the buffer has to be done concatenating the setup packet and the data buffer.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josesimoes OK yes. We definitely need a test for this before we move forward.

@MelbourneDeveloper

Copy link
Copy Markdown
Owner

@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 josesimoes left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good!

@@ -0,0 +1,30 @@
namespace Device.Net

Copy link
Copy Markdown
Author

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.

namespace Device.Net
{
#pragma warning disable CA1815 // Override equals and operator equals on value types
public readonly struct ControlTransferResult

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.

@MelbourneDeveloper
MelbourneDeveloper merged commit 755bfff into MelbourneDeveloper:develop-controltransfer-Refactor Nov 11, 2020
@josesimoes
josesimoes deleted the rework-control-transfer branch November 12, 2020 00:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants