Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#29 UsbSetupPacket structure problem

v2.2.8
closed-fixed
5
2010-11-10
2010-11-10
DerEngel
No

Problem: The structure UsbSetupPacket only fully works on C#

The following code works as expected in C#:
int iTransferred = 0;
short lowaddr = (short) (0xffff & addr);
short highaddr = (short) (addr >> 0x10);
UsbSetupPacket setupPacket = new UsbSetupPacket(0x40, request, lowaddr, highaddr, (short) count);
usbdev.ControlTransfer(ref setupPacket, dataout, count, out iTransferred);

However, VB.net (VS2005) does not handle Uint16 to Int16 excactly as its C# version:

Dim iTransferred As Integer = 0
Dim lowaddr As Short = CShort((&HFFFF And addr))
Dim highaddr As Short = CShort((addr >> &H10))
Dim setupPacket As New UsbSetupPacket(&H40, request, lowaddr, highaddr, CShort(count))
usbdev.ControlTransfer((setupPacket), dataout, count, iTransferred)

The result on the VB.net side is a overflow crash when the upper or lower portion of the address is 16 bits wide. When it is, C# converts the 16th bit into the sign bit, but vb.net will crash. One possible workaround is to convert the first 15 bits of the value into a signed short integer and then add the sign if the 16th bit was high. However this then leaves the problem of converting the value 0x8000 (which converts to -0 and thus 0).

Solution: the UsbSetupPacket should have a overloaded version that allows you to create one with using unsigned 16 bit integers, or have a version specifically for vb.net.

Discussion

1 2 > >> (Page 1 of 2)
  • Yes. I was not aware of this limitation in VB.NET; it doesn't have an "unchecked" keyword.

    If you haven't already found it, there is a global project (compiling) option to remove integer overflow checks in VB.NET (as a work-a-round)

    Adding an overloaded constructed to UsbSetupPacket with everything declared as "int" should solve this problem:
    Public Sub New(bRequestType As Integer, bRequest As Integer, wValue As Integer, wIndex As Integer, wlength As Integer)

     
    • status: open --> open-accepted
     
    • status: open-accepted --> pending-fixed
     
  • FYI: Declare your 32bit address as an Int64 and treat it as if it were an unsigned Int32.

    Regards,
    Travis

     
  • DerEngel
    DerEngel
    2010-11-10

    UsbSetupPacket only accepts Int16 for those field inputs. If you declare a higher input variable (such as Int32 or Int64), VB.net IDE will not let you compile.

     
  • DerEngel
    DerEngel
    2010-11-10

    • status: pending-fixed --> open-fixed
     
  • DerEngel
    DerEngel
    2010-11-10

     
    Attachments
  • DerEngel
    DerEngel
    2010-11-10

    I modified UsbSetupPacket.cs and added the duplicate definition so that you can create the struct with either Int16 or Uint16, and confirmed that this works perfectly on both C# and VB.net, with all valid inputs.

    This limitation might only be in vb.net (dotnet 2.0), as it might have been fixed in 3.0 or 3.5. However, since Mono only fully supports 2.0 framework, I imagine a lot of developers will want to keep with the older framework.

     
    • milestone: --> v2.2.8
    • status: open-fixed --> closed-fixed
     
1 2 > >> (Page 1 of 2)