Include files for libUSB Linux and Windows.

For issues with communication ports, protocols, etc.
grindstone
Posts: 452
Joined: May 05, 2015 5:35
Location: Germany

Re: Include files for libUSB Linux and Windows.

Postby grindstone » Feb 14, 2018 22:47

I have massive problems here with libusb overwriting the memory FB stores its variables in. Couldn't figure out the reason for that behaviour yet. The original C programs seem to run correct.

Very strange. If anyone has a suggestion how to fix it, please let me know.
caseih
Posts: 1188
Joined: Feb 26, 2007 5:32

Re: Include files for libUSB Linux and Windows.

Postby caseih » Feb 15, 2018 0:01

Care to post an example of the C version that works, and the FB version that sees corruption? Memory corruption almost certainly points to some structure that's not translated quite correctly, so pointers get messed up. I think I'd prefer a complete crash to memory corruption. Would be a lot easier to track down.
Dinosaur
Posts: 1054
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Dinosaur » Feb 16, 2018 1:05

Hi All

Just as an update.
Turns out that the I/O board is a "Dumb Device" that gets loaded during Linux bootup with the help of a .rules file and a routine called fxload.
Until this "bootstrap" has successfully completed, the device couldn't be mounted, and I could not talk to it.

Once I got all that resolved I wrote some code to check the speed of communicating with it.
Doing 1000 Writes & Reads translated to 190 micro seconds per call (Writing or reading 32 bits of I/O)

Doing the same thing using an FTDI driver to another board takes 12 milli seconds per call.

So, I am officially impressed.
If anyone is interested in the code I will post it.

Regards
Edit: Point of Interest.
The device Vendor:Product is 1605:8018
But until it has been mounted it showed up with lsusb as 1606:0018
Apparently all devices not mounted drop the msb.
grindstone
Posts: 452
Joined: May 05, 2015 5:35
Location: Germany

Re: Include files for libUSB Linux and Windows.

Postby grindstone » Feb 16, 2018 6:53

@Dinosaur: You are using Linux, right? And you got no memory corruption? Are you running any For...Next loops in your program? And if so, do they terminate correctly?

This would be a precious hint if I knew that the trouble I ran in here affects only Windows.
grindstone
Posts: 452
Joined: May 05, 2015 5:35
Location: Germany

Re: Include files for libUSB Linux and Windows.

Postby grindstone » Feb 16, 2018 11:27

Allright, I found the bug and fixed it. The updated libusb.bi can be downloaded here.

Unlike other dlls libusb differentiates the calling convention depending on the current OS, so for Windows it has to be set it to StdCall instead of Cdecl.

And (@srvaldez) here the FB translation of listdevs.c:

Code: Select all

/'
 * libusb example program to list devices on the bus
 * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 '/
 
 'Ported to freeBasic by grindstone 2018

#Include "libusb.bi"

Sub print_devs(devs As libusb_device Ptr Ptr)
   Dim As libusb_device Ptr dev
   Dim As Long i = 0, j = 0
   Dim As UByte path(8)

   dev = devs[0]
   While dev <> NULL
      Dim As libusb_device_descriptor desc
      Dim As Long r = libusb_get_device_descriptor(dev, @desc)
      
      If r < 0 Then
         Print "failed to get device descriptor"
         Return
      EndIf
           
      Print Hex(desc.idVendor,4);":";Hex(desc.idProduct,4);" (bus";libusb_get_bus_number(dev); _
            ", device ";libusb_get_device_address(dev);")";
      
      r = libusb_get_port_numbers(dev, @path(0), UBound(path))
      
      If r > 0 Then
         Print " path: ";path(0);
         For j = 1 To r - 1
            Print ".";path(j);
         Next
      EndIf
      Print
      i += 1
      dev = devs[i]
   Wend
End Sub

Dim As libusb_device Ptr Ptr devs
Dim As Long r
Dim As ssize_t cnt

r = libusb_init(NULL)
If r < 0 Then
   End r
EndIf

cnt = libusb_get_device_list(NULL, @devs)
If cnt < 0 Then
   End cnt
EndIf

print_devs(devs)
libusb_free_device_list(devs, 1)

libusb_exit(NULL)

? "OK"
Sleep


Dinosaur
Posts: 1054
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Dinosaur » Feb 16, 2018 13:25

Hi All

grindstone , yes I run Linux MInt 18.3 with libusb-1.0.21.
There were a couple of items that I had to modify on the .bi file

I remmed out

Code: Select all

   '#If Not Defined(__CYGWIN__)
   '   #Include "win\winsock.bi"
   '#EndIf

and also changed

Code: Select all

tv As timeval Ptr

to

Code: Select all

tv As Long Ptr
in a number of places as it threw errors of not being declared.

Will try the new .bi file without changes and see if any errors.

Regards
Glad you found the problem.
grindstone
Posts: 452
Joined: May 05, 2015 5:35
Location: Germany

Re: Include files for libUSB Linux and Windows.

Postby grindstone » Feb 16, 2018 14:33

Hello Dinosaur!

For I'm only on Windows, I will need some help to make the header file available for Linux. Every hint is welcome.

Umfortunately, the libusb site is down at the moment, so that I can't take a closer look to the meaning of timeval, but AFAIK it's a structure consisting of 2 ULong members (seconds and microseconds).

Try to replace

Code: Select all

#If Defined(__FB_WIN32__) Or Defined(__FB_CYGWIN__)   
      #Include "windows.bi"
   #EndIf
with

Code: Select all

#If Defined(__FB_WIN32__)
      #Include "windows.bi"
   #ElseIf Defined(__FB_LINUX__)
      #Include "crt/sys/socket.bi"
   #EndIf
and delete (or comment)

Code: Select all

#If Not Defined(__FB_CYGWIN__)
      #Include "win\winsock.bi"
   #EndIf

This should solve the timeval issue.
Dinosaur
Posts: 1054
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Re: Include files for libUSB Linux and Windows.

Postby Dinosaur » Feb 25, 2018 20:15

Hi All

grindstone, thanks for the update, however the timeval Ptr must be defined in other Windows only files.
So for the moment the only solution is to convert it to "tv as Long Ptr"

I have completed all my testing for the AccesIO board and preparing the project for posting on their site
under the FreeBasic sample sessions.

There is only one area where I am confused and that is to do with Dim Sharing the Types in the libusb.bi file.
So before I post the code I need someone to tell me WHY I need to add the lines as per below.

Code: Select all

Type libusb_device_handle As libusb_device_handle
has a line added

Code: Select all

Dim Shared libusb_device_handle as libusb_device_handle Ptr
So I am modifying your .bi file by adding these statements.
Is the conversion from .h to .bi missing something here ?

So far I have only added two, but I am sure that if I were to call a number of others the same would apply.

Regards
grindstone
Posts: 452
Joined: May 05, 2015 5:35
Location: Germany

Re: Include files for libUSB Linux and Windows.

Postby grindstone » Feb 26, 2018 9:40

Code: Select all

Type libusb_device_handle As libusb_device_handle
is the conversion of

Code: Select all

struct libusb_device_handle;
Seems not to be necessary for FB, but it doesn't hurt, so I left it in.

Code: Select all

Dim Shared libusb_device_handle as libusb_device_handle Ptr
Have a look at the latest update, there's no (longer) such line in the code.

Converting timeval Ptr to Long Ptr may cause trouble. Better replace

Code: Select all

#If Defined(__FB_LINUX__ ) Or Defined(__FB_CYGWIN__)
      #Include Once "crt\time.bi"
   #EndIf
with

Code: Select all

#If Defined(__FB_LINUX__ ) Or Defined(__FB_CYGWIN__)
      Type timeval
         tv_sec As Long
         tv_usec As Long
      End Type
   #EndIf
As far as I know this Type is the only reason to include the time.bi. Please tell me if it works on Linux. If so, I will update my code.

Return to “Hardware Interfaces / Communication”

Who is online

Users browsing this forum: No registered users and 1 guest