[bug+possible patch] open device fails in c++ bindings

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[bug+possible patch] open device fails in c++ bindings

Denis Sirotkin
Hi!

After updated libusb from 1.0.11 to 1.0.17 in my Debian Wheezy, I have got the same problem as described in next thread:

A sample program:

#include <stdio.h>
#include <stdlib.h>
#include <ftdi.hpp>

int main(void)
{
    Ftdi::Context *ftdic = new Ftdi::Context;

    int ret = ftdic->open(0x0403, 0x6001);
    if (ret < 0) {
        fprintf(stderr, "open error %d\n", ret);
    }
    delete ftdic;
    return EXIT_SUCCESS;
}

ftdic->open() returns -5

I suppose that this is because there is call get_strings_and_reopen() which calls ftdi_usb_get_strings() which calls libusb_open() for already opened device. A solution may be to call ftdi_usb_close_internal() from ftdi_usb_get_strings() function before reopen it or to check if it is already opened and don't open twice. I tried both but prefer second and simple do like this:

--- libftdi1-1.1/src/ftdi.c.orig 2014-02-05 22:24:11.000000000 +0600
+++ libftdi1-1.1/src/ftdi.c      2014-05-15 14:42:13.534444380 +0600
@@ -413,8 +413,9 @@
     if ((ftdi==NULL) || (dev==NULL))
         return -1;
 
-    if (libusb_open(dev, &ftdi->usb_dev) < 0)
-        ftdi_error_return(-4, "libusb_open() failed");
+    if (ftdi->usb_dev == NULL)
+        if (libusb_open(dev, &ftdi->usb_dev) < 0)
+            ftdi_error_return(-4, "libusb_open() failed");
 
     if (libusb_get_device_descriptor(dev, &desc) < 0)
         ftdi_error_return(-11, "libusb_get_device_descriptor() failed");


--
wbr
  Denis Sirotkin


libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [bug+possible patch] open device fails in c++ bindings

Thomas Jarosch
Hi Denis,

On Thursday, 15. May 2014 15:25:26 Denis Sirotkin wrote:

> I suppose that this is because there is call get_strings_and_reopen()
> which calls ftdi_usb_get_strings() which calls libusb_open() for already
> opened device. A solution may be to call ftdi_usb_close_internal() from
> ftdi_usb_get_strings() function before reopen it or to check if it is
> already opened and don't open twice. I tried both but prefer second and
> simple do like this:
>
> --- libftdi1-1.1/src/ftdi.c.orig 2014-02-05 22:24:11.000000000 +0600
> +++ libftdi1-1.1/src/ftdi.c      2014-05-15 14:42:13.534444380 +0600
> @@ -413,8 +413,9 @@
>      if ((ftdi==NULL) || (dev==NULL))
>          return -1;
>
> -    if (libusb_open(dev, &ftdi->usb_dev) < 0)
> -        ftdi_error_return(-4, "libusb_open() failed");
> +    if (ftdi->usb_dev == NULL)
> +        if (libusb_open(dev, &ftdi->usb_dev) < 0)
> +            ftdi_error_return(-4, "libusb_open() failed");
>
>      if (libusb_get_device_descriptor(dev, &desc) < 0)
>          ftdi_error_return(-11, "libusb_get_device_descriptor() failed");

I've commited an almost identical fix on your behalf. Thanks!

It's quite a "brain puzzle" to follow the call stack from all the layers,
but your solution looks correct to me :)

Cheers,
Thomas


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Loading...