Include files for libUSB Linux and Windows.

For issues with communication ports, protocols, etc.
Imortis
Posts: 1568
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Imortis » Feb 05, 2018 15:49

srvaldez wrote:... I got confused on libusb_device **devs, not sure if ptr ptr is the right FB interpretation.


A good translation of that into FB would be:

Code: Select all

dim devs as libusb_device ptr ptr
Dinosaur
Posts: 1082
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Dinosaur » Feb 05, 2018 17:43

Hi All

Using usblib-0.1
I am lost with this section of code that always replies with a number in the 4294967271 range.
Now I thought that Failure should respond with -1 and success with the number of bytes.

Code: Select all

Type USBPart
    HandlePtr   As Any Ptr
    Buffer      As Zstring Ptr
    BufferLen   As Integer
    ReqType     As Integer
    Req         As Integer
    size        As Integer
End Type

Code: Select all

'--------Next setup mesg and send-------------------
    usb_ctrl_setup.bRequestType = 8
    usb_ctrl_setup.bRequest = 6
    usb_ctrl_setup.wValue = 1
    usb_ctrl_setup.wIndex = 0
    usb_ctrl_setup.wLength = 18
    usbpart.Buffer = Allocate(65)
    Dim As Integer RetBytes
    RetBytes = usb_control_msg(usbpart.HandlePtr, usb_ctrl_setup.bRequestType, usb_ctrl_setup.bRequest, usb_ctrl_setup.wValue,usb_ctrl_setup.wIndex, usbpart.Buffer, usbpart.BufferLen,500)
    Print "Mesg Bytes =;";RetBytes

Looking at WireShark it appears that it all works by responding with all the fields, but I don't seem to be able to work out HOW to get that.

Regards
Edit: [SOLVED]
By using ValInt(("&b11111111111111111111111111100111") returns -25
Looking up in errno.h = #define ENOTTY 25 /* Inappropriate I/O control operation */
caseih
Posts: 1260
Joined: Feb 26, 2007 5:32

Re: Include files for libUSB Linux and Windows.

Postby caseih » Feb 06, 2018 4:38

Dinosaur, you've got the wrong type for RetBytes. It should be Long, not Integer. Had you made RetBytes Long, you'd have got the -25 code instead of some big number.

Any time the C declaration is int, that is Long in FB. And actually your Type USBPart should be using Longs as well, not Integers. Failure to do this is going to lead to some pretty weird crashes sometimes. In your case there was no crash because the compiler could coerce the types during the call to usb_control_msg. But if you check the C declaration all the parameters are int which is Long in FB.
Dinosaur
Posts: 1082
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Dinosaur » Feb 06, 2018 15:09

Hi All

Thanks for that caseih, I recall reading that somewhere, but forgot to implement it.

Regards
caseih
Posts: 1260
Joined: Feb 26, 2007 5:32

Re: Include files for libUSB Linux and Windows.

Postby caseih » Feb 06, 2018 15:36

In thinking about it, I'm actually confused why you were seeing that RetBytes was 4294967271 and not -25. When the int (32-bit) return value from usb_control_msg() was copied into your 64-bit Integer, the compiler normally extends the sign bit across the additional upper bits, making the negative values stay the same even when you change the size of the type. And I can verify that this is what happens on FB also. So I have no idea why you were not seeing -25, even when using the Integer type. Very odd. Unfortunately I cannot say what's going on since your code snippet is not compile-able. In the future I suggest you post a small but run-able example that illustrates the problem.
Dinosaur
Posts: 1082
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Dinosaur » Feb 10, 2018 21:30

Hi All

Compiling with 64 bit FB 1.05

I am still having difficulties with variable sizes in Libusb.
D.J.Peters originally posted the translated .bi file, which includes the following declares.

Code: Select all

type usb_ctrl_setup
  as ubyte  bRequestType
  as ubyte  bRequest
  as ushort wValue
  as ushort wIndex
  as ushort wLength
end type


However it is being used as Integers instead of uByte.

Code: Select all

declare function usb_control_msg      (dev as usb_dev_handle ptr, requesttype as integer, request as integer, value as integer, index as integer, bytes as byte ptr, size as integer, timeout as integer) as integer


When I run usb_control_msg and observe it in WireShark, I can't even find that transaction and the error code returned is -32 /* Broken pipe */
My code for the startup is:

Code: Select all

Sub usbInitialize
    ''----Start by init, list busses & devices-------
    ''1605:0018
    Starttime = Timer * 1000
    usb_init()
    Print "Busses  found   =;";usb_find_busses()        ''prints 3 for Nbr of busses found
    Print "Devices found   =;";usb_find_devices()       ''print  9 for Nbr of devices found
    ''--------Then see if Vendor:Product exists----------
    aBus = usb_get_busses()                             ''sets aBus as pointer to usb_get_busses
    while (aBus)                                        ''gets usb_get_busses Next                       
      aDev = aBus->devices
      while aDev
        If Hex(aDev->descriptor.idVendor,4) = "1605" And Hex(aDev->descriptor.idProduct,4) = "0018" Then
            Found = 1                                   ''stop looking we found it
            Exit While                                  ''exit loop
        End If
        aDev=aDev->next                                 ''otherwise go for Next device
      wend
      If Found = 1 Then Exit While                      ''exit loop
      aBus = aBus->next                                 ''otherwise go for Next Bus
    wend
    ''--------If Vendor:Product found then get handle and claim port----
    If Found = 1 Then
        Dim As usb_device Ptr AccessIO = aDev             ''save the Ptr for device.
        handlePtr = usb_open(AccessIO)                    ''use it to open, which returns a handle
        Print "AccessIO Handle =;";handlePtr
        Detach = usb_detach_kernel_driver_np(handlePtr, 0) ''This fails
        Print "Detach success  =;";Detach                 ''0 if success
        Print "Claim Success   =;";usb_claim_interface(handlePtr, 0) ''This succeeds
    Endif
    ''--------Set Digital I/O bits using Control Transfer--
    usb_ctrl_setup.bRequestType = &H80
    usb_ctrl_setup.bRequest     = &H10                '
    usb_ctrl_setup.wValue       = &H00
    usb_ctrl_setup.wIndex       = &H00
    usb_ctrl_setup.wLength      = &H08
    Buffer = Allocate(8)
    Buffer[0] = 255                                     ''O/P 0-7 output bits
    Buffer[1] = 127                                     ''O/P 8-15
    Buffer[2] = &HFF                                    ''I/P 0-7 ignored when setting O/P's
    Buffer[3] = &HFF                                    ''I/P 8-15
    RetBytes = usb_control_msg(HandlePtr, usb_ctrl_setup.bRequestType,usb_ctrl_setup.bRequest,usb_ctrl_setup.wValue,usb_ctrl_setup.wIndex, Buffer,usb_ctrl_setup.wLength,5000)
    Print "RetBytes        =;";RetBytes
    Print "Values in Buffer=;";(Buffer[3]),(Buffer[2]),(Buffer[1]),(Buffer[0])
End Sub

Are the different variable sizes to blame for problems ?, or can someone see mistakes in my code.

Regards
MrSwiss
Posts: 2679
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Include files for libUSB Linux and Windows.

Postby MrSwiss » Feb 10, 2018 21:52

Try Long (instead of: Integer) in declaration (and, also in implementation):

Code: Select all

declare function usb_control_msg( _
    ByVal dev as usb_dev_handle ptr, _
    ByVal requesttype as Long, _
    ByVal request as Long, _
    ByVal value as Long, _
    ByVal index as Long, _
    ByVal bytes as byte ptr, _
    ByVal size as Long, _
    ByVal timeout as Long _
    ) as Long

U/Long = 32-bit integer type, fixed size (in both compilers 32/64)!
Last edited by MrSwiss on Feb 10, 2018 22:03, edited 1 time in total.
Dinosaur
Posts: 1082
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Dinosaur » Feb 10, 2018 22:02

Hi All

Been there & tried that.
Have tried various combinations, but by putting an unusual value in the Host transmission,
like usb_ctrl_setup.bRequest = &H11, I should be able to find the transaction, but it looks like it
is being ignored and immediately an error code of -32 is sent back.

Regards
caseih
Posts: 1260
Joined: Feb 26, 2007 5:32

Re: Include files for libUSB Linux and Windows.

Postby caseih » Feb 11, 2018 0:46

You're of course welcome to try permutations of types. But if you're not using Long in place of C's int, then it's not going to work, or if it does, it will be completely by accident. Methinks the problem is in your debug strategy.

As I mentioned before, it would help greatly if you posted a short, self-contained, compile-able example. Then you'll get some better feedback because we'll be able to try your code and help discover where the problem lies. Randomly changing your code with different types, and other random changes, is unlikely to lead to a speedy solution!
Dinosaur
Posts: 1082
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Dinosaur » Feb 11, 2018 1:35

Hi All

caseih I appreciate your reponse.
The basic requirement of this code is to communicate with a usb device that allows me to set I/O bits and read them
via control transfers. According to the manufacturer "AccessIO" , after the port is opened I should be able to set the following:
VENDOR REQUEST 10 H : DIO W RITE
This request writes DO data to all bytes. (DO data for input bytes will be ignored.)
Value: Reserved, use 0000h.
Index: Reserved, use 0000h.
Data Written: 4 bytes, written to the corresponding DO bytes.

VENDOR REQUEST 11 H : DIO READ
This request reads all DIO bytes. For output bytes, this will read back the output values.
Value: Reserved, use 0000h.
Index: Reserved, use 0000h.
Data Read: 4 bytes, read from the corresponding DIO bytes

My problem is two fold:
1: The Detach = usb_detach_kernel_driver_np(handlePtr, 0) returns -61
Normally after the first time running my program (on a different test board) this error would happen.
But by unplugging and re-plugging the board, the kernel would claim the port and the Detach would report "Success" after that.
In this case it reports -61 every time. Maybe it means that the Kernel simply didn't claim the port for whatever reason.
2: Whenever I apply the value usb_ctrl_setup.bRequest = &H10 or &H11 the usb_control_msg returns -32.

What the manufacturer has NOT told me, is what the usb_ctrl_setup.bRequestType should be for either bRequest.

So the .bi file below has been edited from the OP to remove the confusion of items not used or required, Note that the usb_ctrl_setup has been changed to Long

Code: Select all

#ifndef __USB_BI__
#define __USB_BI__
#inclib "usb"
#define USB_MAXENDPOINTS  32
#ifndef PATH_MAX
    #define PATH_MAX 4096
#endif
type usb_endpoint_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ubyte  bEndpointAddress
  as ubyte  bmAttributes
  as ushort wMaxPacketSize
  as ubyte  bInterval
  as ubyte  bRefresh
  as ubyte  bSynchAddress
  as ubyte ptr extra  /' Extra descriptors '/
  as Long   extralen
end type
type usb_interface_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ubyte  bInterfaceNumber
  as ubyte  bAlternateSetting
  as ubyte  bNumEndpoints
  as ubyte  bInterfaceClass
  as ubyte  bInterfaceSubClass
  as ubyte  bInterfaceProtocol
  as ubyte  iInterface
  as usb_endpoint_descriptor ptr endpoint
  as ubyte ptr extra  /' Extra descriptors '/
  as Long   extralen
end type
type usb_interface
  as usb_interface_descriptor ptr altsetting
  as Long num_altsetting
end type
type usb_config_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ushort wTotalLength
  as ubyte  bNumInterfaces
  as ubyte  bConfigurationValue
  as ubyte  iConfiguration
  as ubyte  bmAttributes
  as ubyte  MaxPower
  as usb_interface ptr interface
  as ubyte ptr extra  /' Extra descriptors '/
  as Long   extralen
end type
type usb_device_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ushort bcdUSB
  as ubyte  bDeviceClass
  as ubyte  bDeviceSubClass
  as ubyte  bDeviceProtocol
  as ubyte  bMaxPacketSize0
  as ushort idVendor
  as ushort idProduct
  as ushort bcdDevice
  as ubyte  iManufacturer
  as ubyte  iProduct
  as ubyte  iSerialNumber
  as ubyte  bNumConfigurations
end type
type usb_ctrl_setup
  as Long  bRequestType
  as Long  bRequest
  as Long  wValue
  as Long  wIndex
  as Long  wLength
end type
type usb_bus_ as usb_bus
type usb_device
  as  usb_device ptr next, prev
  as zstring * PATH_MAX + 1 filename
  as usb_bus_ ptr bus
  as usb_device_descriptor descriptor
  as usb_config_descriptor ptr config
  as any ptr dev    /' Darwin support '/
  as ubyte devnum
  as ubyte num_children
  as usb_device ptr ptr children
end type
type usb_bus
  as usb_bus ptr next, prev
  as zstring * PATH_MAX + 1  dirname
  as usb_device ptr devices
  as Ulong location
  as usb_device ptr root_dev
end type
type usb_dev_handle as any
type usb_bus_ as usb_bus
/' Variables '/
extern usb_busses as usb_bus ptr
extern "C"
    declare function usb_open             (dev as usb_device ptr) as usb_dev_handle ptr
    declare function usb_close            (dev as usb_dev_handle ptr) as Long
    declare function usb_control_msg      (dev as usb_dev_handle ptr, requesttype as Long, request as Long, value as Long, index as Long, bytes as byte ptr, size as Long, timeout as Integer) as Long
    declare function usb_set_configuration(dev as usb_dev_handle ptr, configuration as Long) as Long
    declare function usb_claim_interface  (dev as usb_dev_handle ptr, interface as long) as long
    declare function usb_release_interface(dev as usb_dev_handle ptr, interface as Long) as Long
    declare function usb_detach_kernel_driver_np(dev as usb_dev_handle ptr, interface as Long) as Long
    declare sub usb_init()
    declare function usb_find_busses() as Long
    declare function usb_find_devices() as Long
    declare function usb_device(dev as usb_dev_handle ptr) as usb_device ptr
    declare function usb_get_busses() as usb_bus Ptr
end extern

#endif /' __USB_BI__ '/
Dim Shared as Long Found, RetBytes, Detach
Dim Shared as usb_device ptr aDev
Dim Shared as usb_bus ptr aBus                             
Dim Shared handlePtr As Any Ptr
Dim Shared usb_ctrl_setup As usb_ctrl_setup
Dim Shared Buffer As UByte Ptr
And below the program.

Code: Select all

#include once "libusb.bi"

Sub usbInitialize
    ''----Start by init, list busses & devices-------
    ''1605:0018 Change this to suit device you are looking for.
    usb_init()
    Print "Busses  found   =;";usb_find_busses()        ''prints 3 for Nbr of busses found
    Print "Devices found   =;";usb_find_devices()       ''print  9 for Nbr of devices found
    ''--------Then see if Vendor:Product exists----------
    aBus = usb_get_busses()                             ''sets aBus as pointer to usb_get_busses
    while (aBus)                                        ''gets usb_get_busses Next                       
      aDev = aBus->devices
      while aDev
        If Hex(aDev->descriptor.idVendor,4) = "1605" And Hex(aDev->descriptor.idProduct,4) = "0018" Then
            Found = 1                                   ''stop looking we found it
            Exit While                                  ''exit loop
        End If
        aDev=aDev->next                                 ''otherwise go for Next device
      wend
      If Found = 1 Then Exit While                      ''exit loop
      aBus = aBus->next                                 ''otherwise go for Next Bus
    wend
    ''--------If Vendor:Product found then get handle and claim port----
    If Found = 1 Then
        Dim As usb_device Ptr AccessIO = aDev             ''save the Ptr for device.
        handlePtr = usb_open(AccessIO)                    ''use it to open, which returns a handle
        Print "AccessIO Handle =;";handlePtr
        Detach = usb_detach_kernel_driver_np(handlePtr, 0) ''This fails
        Print "Detach success  =;";Detach                 ''0 if success
        Print "Claim Success   =;";usb_claim_interface(handlePtr, 0) ''This succeeds
    Endif
    ''--------Set Digital I/O bits using Control Transfer--
    usb_ctrl_setup.bRequestType = &H80
    usb_ctrl_setup.bRequest     = &H00
    usb_ctrl_setup.wValue       = &H00
    usb_ctrl_setup.wIndex       = &H00
    usb_ctrl_setup.wLength      = 4
    Buffer = Allocate(4)
    Buffer[0] = &H99                                     ''O/P 0-7 output bits
    Buffer[1] = 100                                     ''O/P 8-15
    Buffer[2] = &H0                                    ''I/P 0-7 ignored when setting O/P's
    Buffer[3] = &H0                                    ''I/P 8-15
    RetBytes = usb_control_msg(HandlePtr, usb_ctrl_setup.bRequestType,usb_ctrl_setup.bRequest,usb_ctrl_setup.wValue,usb_ctrl_setup.wIndex, Buffer,usb_ctrl_setup.wLength,5000)
    Print "RetBytes        =;";RetBytes
    Print "Values in Buffer=;";(Buffer[3]),(Buffer[2]),(Buffer[1]),(Buffer[0])
    Deallocate Buffer
End Sub
Sub usbQuit
    ''--------Release/Close port and quit-------------------------
    If handlePtr Then
            Print "Release result  =;";usb_release_interface(handlePtr, 0)
            Print "usb_Close result=;";usb_close(HandlePtr)
        Else
            Print "Interface Not found"
    Endif
End Sub

usbinitialize
usbQuit
End
   
Now I would have thought that with WireShark I should be able to see the values in usb_ctrl_setup.bRequest = &H10 from the host.
But either the transaction gets rejected "before the wire" or I am missing something fundamental.
Obviously you can't test the "usb_ctrl_setup.bRequest = &H10" response but perhaps you can see my other failures.

If I leave the "usb_ctrl_setup.bRequest = &H00" then the response is the value 2. When that happens the buffer bytes are also reset to 0.

Regards
caseih
Posts: 1260
Joined: Feb 26, 2007 5:32

Re: Include files for libUSB Linux and Windows.

Postby caseih » Feb 11, 2018 4:48

I'll see what your program does on my machine tomorrow if I can.

EDIT: On my machine, it segfaults at the usb_control_msg() call. This is likely because the if Found=1 block didn't run. I'm wondering if the lines under the "--------Set Digital I/O bits using Control Transfer--" comment should all be inside that if block. Otherwise, HandlePtr is not always set properly before the call to usb_control_msg(). When I make that change, the code itself now runs, but since I don't have the particular USB device you are working with, the program just falls through and says "Interface not found."
Dinosaur
Posts: 1082
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Dinosaur » Feb 12, 2018 0:47

Hi All

caseih, you are right, the usb_control_msg call should be protected by "If Found = 1".

Did you find any problems with variable sizes ?
Also I was wondering if the installed libusb-1.0 is interfering with my attempts to Detach the port from the kernel,
considering that I am using libusb-0.1.12.

Wouldn't it be nice if some one has already done the full conversion of .h to .bi for libusb-1.0 :(

I will communicate with the manufacturer on Monday, and try to clarify the operation.
Many thanks for your time.

Regards

Edit: I tried uninstalling libusb-1.0 but Mint 18.3 Cinnamon needs it, and resulted in Cinnamon not starting.
That meant I got to test TimeShift, which worked really well, as the pc is back up again.
caseih
Posts: 1260
Joined: Feb 26, 2007 5:32

Re: Include files for libUSB Linux and Windows.

Postby caseih » Feb 12, 2018 3:59

I have libusb-0.1.4 installed (/usr/include/usb.h), and also libusb-1.0. I think your program is linking against 0.1.4. Anyway if I compare usb.h to your libusb.bi, everything lines up except the usb_ctrl_setup type. In my usb.h, they are two u_int8 and three u_int16 members. In your .bi file you have them as all Long. Maybe that's a problem? Should be ubytes and ushorts.
grindstone
Posts: 493
Joined: May 05, 2015 5:35
Location: Germany

Re: Include files for libUSB Linux and Windows.

Postby grindstone » Feb 12, 2018 5:41

Dinosaur wrote:Wouldn't it be nice if some one has already done the full conversion of .h to .bi for libusb-1.0 :(
Here you are:
http://users.freebasic-portal.de/grindstone/Codes/libUSB/libusb.bi
Dinosaur
Posts: 1082
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Dinosaur » Feb 12, 2018 23:25

Hi All

Thank you very much for that grindstone.
The only thing I had to do was comment out:
Function libusb_get_iso_packet_buffer

getting errors from that:
error 41: Variable not declared, offset in 'offset += transfer->iso_packet_desc(i).length
Other than that it worked perfectly.

It didn't solve my problem though with the I/O board, so still waiting for manufacturers response.
JFYI, this is a quick & dirty test program using libusb-1.0

Code: Select all

#include once "libusb-1.0.bi"
    Found = libusb_init(0)
    Print "libusb_Init =;";Found                            ''This print 0   
    Handleptr = libusb_open_device_with_vid_pid(0, &H1605, &H0018)
    Print "Handle Ptr=;";handleptr                          ''This print the pointer (34551824)
    If Handleptr > 0 Then
        Detach = libusb_detach_kernel_driver(Handleptr, 0) 
        Print "Detached =;";Detach                          ''This print -5
        Claim = libusb_claim_interface(Handleptr, 0)
        Print "Claim =;";Claim                              ''This print 0
        libusb_control_setup.bmRequestType = &B10000000
        libusb_control_setup.bRequest      = &H00
        libusb_control_setup.wValue        = &H00
        libusb_control_setup.wIndex        = &H00
        libusb_control_setup.wLength       = 4
        Buffer = Allocate(4)
        Buffer[0] = &H99                                   ''O/P 0-7 output bits
        Buffer[1] = 100                                    ''O/P 8-15
        Buffer[2] = &H0                                    ''I/P 0-7 ignored when setting O/P's
        Buffer[3] = &H0
        With libusb_control_setup
            RetBytes = libusb_control_transfer(HandlePtr,.bmRequestType,.bRequest,.wValue,.wIndex,Buffer,.wLength,5000)
        End With
        Print "RetBytes        =;";RetBytes                 ''This prints the value 2
        Print "Values in Buffer=;";(Buffer[3]),(Buffer[2]),(Buffer[1]),(Buffer[0]) ''This prints 0,0,0,0
        Deallocate Buffer
    Endif
    Libusb_exit(0)
    End

Regards

Return to “Hardware Interfaces / Communication”

Who is online

Users browsing this forum: No registered users and 1 guest