libftdi Archives

Subject: ftdi_usb_get_strings fails because usb_dev is not set

From: Larry Martin <Larry@xxxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Thu, 14 Nov 2024 16:07:08 -0500
I'm trying to get FT232 serial numbers into my C++ program, to check the COM port mapping at load time.  Starting with libftdi1-1.5.tar.bz2, I have adapted the example program find_all.c to a function in my program:
#include <libusb-1.0/libusb.h>
#include <libftdi1/ftdi.h> // Nov2024, get serial numbers from COM ports
int FtdiSerialNumbersRead()
{
    char mfg[64], dsc[32], s_n[16];
    int ret = 0;
    bzero(mfg);
    bzero(dsc);
    bzero(s_n);
    //see https://github.com/particle-iot/libftdi/blob/master/examples/find_all.c
    struct ftdi_context* ftdi_p = ftdi_new();
    struct ftdi_device_list * devlist_p, *curdev;
    int numDevs = ftdi_usb_find_all(ftdi_p, &devlist_p, 0, 0); // send zeroes for VID and PID so nothing is excluded
    curdev = devlist_p;
    printf("ftdi_usb_find_all returned %d. Context is %lx. Devlist is %lx %lx\n", numDevs, ftdi_p, devlist_p->next, devlist_p->dev);
    for (int dix = 0; (dix < numDevs) && (NULL != curdev); dix++)
    {
        //ftdi_p->usb_dev is NULL here, with -4 return values from ftdi_usb_get_strings[2]()
        //type mismatch: ftdi_p->usb_dev = curdev->dev;
        struct libusb_device_descriptor descr;
        int descRc = libusb_get_device_descriptor(curdev->dev, &descr);
        //segfault if ftdi->usb_dev is not set and opened
        //int snRc = libusb_get_string_descriptor_ascii(ftdi_p->usb_dev, descr.iSerialNumber, (unsigned char *)s_n, sizeof(s_n));         //printf("           libusb_get_string_descriptor_ascii returned %d [%s]\n", snRc, s_n);
        int stringsRc = ftdi_usb_get_strings(ftdi_p, curdev->dev,
                                             mfg, sizeof(mfg),
                                             dsc, sizeof(dsc),
                                             s_n, sizeof(s_n));
        printf("  strings: %d [%s] [%s] [%s]\n", stringsRc, mfg, dsc, s_n);
        curdev = curdev->next;
    }
    return ret;
}

ftdi_usb_get_strings always returns -4, indicating that this happened:
    if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0)
        ftdi_error_return(-4, "libusb_open() failed");

The listing above shows that I tried bypassing ftdi_usb_get_strings and going straight to ftdi_usb_get_strings2, then started copying lines from that function until I got a SEGV on libusb_get_string_descriptor_ascii.

I think the key is that libusb_device_handle ftdi->usb_dev is NULL at call time.  I have the libusb_device curdev->dev, but the mapping between the two isn't obvious.

Should I be plugging curdev->dev into the ftdi context before calling ftdi_usb_get_strings?  The example doesn't show that.  What is the transformation?  I hit bottom in libusb.h.  The structures are referenced there but defined elsewhere.  How does it tie together?

Thank you.


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx
Current Thread
  • ftdi_usb_get_strings fails because usb_dev is not set, Larry Martin <=