src/app/host-serial/libftdi/ftdi.c
int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
{
    if (ftdi == NULL)
        ftdi_error_return(-12, "ftdi context invalid");

    if (description[0] == 0 || description[1] != ':')
        ftdi_error_return(-11, "illegal description format");

    if (description[0] == 'd')
    {
        libusb_device *dev;
        libusb_device **devs;
        unsigned int bus_number, device_address;
        int i = 0;

        if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
            ftdi_error_return(-2, "libusb_get_device_list() failed");

        /* XXX: This doesn't handle symlinks/odd paths/etc... */
        if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
            ftdi_error_return_free_device_list(-11, "illegal description format", devs);

        while ((dev = devs[i++]) != NULL)
        {
            int ret;
            if (bus_number == libusb_get_bus_number (dev)
                    && device_address == libusb_get_device_address (dev))
            {
                ret = ftdi_usb_open_dev(ftdi, dev);
                libusb_free_device_list(devs,1);
                return ret;
            }
        }

        // device not found
        ftdi_error_return_free_device_list(-3, "device not found", devs);
    }
    else if (description[0] == 'i' || description[0] == 's')
    {
        unsigned int vendor;
        unsigned int product;
        unsigned int index=0;
        const char *serial=NULL;
        const char *startp, *endp;

        errno=0;
        startp=description+2;
        vendor=strtoul((char*)startp,(char**)&endp,0);
        if (*endp != ':' || endp == startp || errno != 0)
            ftdi_error_return(-11, "illegal description format");

        startp=endp+1;
        product=strtoul((char*)startp,(char**)&endp,0);
        if (endp == startp || errno != 0)
            ftdi_error_return(-11, "illegal description format");

        if (description[0] == 'i' && *endp != 0)
        {
            /* optional index field in i-mode */
            if (*endp != ':')
                ftdi_error_return(-11, "illegal description format");

            startp=endp+1;
            index=strtoul((char*)startp,(char**)&endp,0);
            if (*endp != 0 || endp == startp || errno != 0)
                ftdi_error_return(-11, "illegal description format");
        }
        if (description[0] == 's')
        {
            if (*endp != ':')
                ftdi_error_return(-11, "illegal description format");

            /* rest of the description is the serial */
            serial=endp+1;
        }

        return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
    }
    else
    {
        ftdi_error_return(-11, "illegal description format");
    }
}
/**
    Opens the ftdi-device described by a description-string.
    Intended to be used for parsing a device-description given as commandline argument.

    \param ftdi pointer to ftdi_context
    \param description NULL-terminated description-string, using this format:
        \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
        \li <tt>i:\<vendor>:\<product></tt> first device with given vendor and product id, ids can be decimal, octal (preceded by "0") or hex (preceded by "0x")
        \li <tt>i:\<vendor>:\<product>:\<index></tt> as above with index being the number of the device (starting with 0) if there are more than one
        \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string

    \note The description format may be extended in later versions.

    \retval  0: all fine
    \retval -2: libusb_get_device_list() failed
    \retval -3: usb device not found
    \retval -4: unable to open device
    \retval -5: unable to claim device
    \retval -6: reset failed
    \retval -7: set baudrate failed
    \retval -8: get product description failed
    \retval -9: get serial number failed
    \retval -10: unable to close device
    \retval -11: illegal description format
    \retval -12: ftdi context invalid
*/
Opens the ftdi-device described by a description-string.
Intended to be used for parsing a device-description given as commandline argument.



\note The description format may be extended in later versions.


Opens the ftdi - device described by a description - string . Intended to be used for parsing a device - description given as commandline argument . \ note The description format may be extended in later versions .
int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
{
    if (ftdi == NULL)
        ftdi_error_return(-12, "ftdi context invalid");
    if (description[0] == 0 || description[1] != ':')
        ftdi_error_return(-11, "illegal description format");
    if (description[0] == 'd')
    {
        libusb_device *dev;
        libusb_device **devs;
        unsigned int bus_number, device_address;
        int i = 0;
        if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
            ftdi_error_return(-2, "libusb_get_device_list() failed");
        if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
            ftdi_error_return_free_device_list(-11, "illegal description format", devs);
        while ((dev = devs[i++]) != NULL)
        {
            int ret;
            if (bus_number == libusb_get_bus_number (dev)
                    && device_address == libusb_get_device_address (dev))
            {
                ret = ftdi_usb_open_dev(ftdi, dev);
                libusb_free_device_list(devs,1);
                return ret;
            }
        }
        ftdi_error_return_free_device_list(-3, "device not found", devs);
    }
    else if (description[0] == 'i' || description[0] == 's')
    {
        unsigned int vendor;
        unsigned int product;
        unsigned int index=0;
        const char *serial=NULL;
        const char *startp, *endp;
        errno=0;
        startp=description+2;
        vendor=strtoul((char*)startp,(char**)&endp,0);
        if (*endp != ':' || endp == startp || errno != 0)
            ftdi_error_return(-11, "illegal description format");
        startp=endp+1;
        product=strtoul((char*)startp,(char**)&endp,0);
        if (endp == startp || errno != 0)
            ftdi_error_return(-11, "illegal description format");
        if (description[0] == 'i' && *endp != 0)
        {
            if (*endp != ':')
                ftdi_error_return(-11, "illegal description format");
            startp=endp+1;
            index=strtoul((char*)startp,(char**)&endp,0);
            if (*endp != 0 || endp == startp || errno != 0)
                ftdi_error_return(-11, "illegal description format");
        }
        if (description[0] == 's')
        {
            if (*endp != ':')
                ftdi_error_return(-11, "illegal description format");
            serial=endp+1;
        }
        return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
    }
    else
    {
        ftdi_error_return(-11, "illegal description format");
    }
}
int ftdi_usb_open_string ( struct ftdi_context * ftdi , const char * description ) { if ( ftdi == NULL ) ftdi_error_return ( -12 , " " ) ; if ( description [ 0 ] == 0 || description [ 1 ] != ' ' ) ftdi_error_return ( -11 , " " ) ; if ( description [ 0 ] == ' ' ) { libusb_device * dev ; libusb_device * * devs ; unsigned int bus_number , device_address ; int i = 0 ; if ( libusb_get_device_list ( ftdi -> usb_ctx , & devs ) < 0 ) ftdi_error_return ( -2 , " " ) ; if ( sscanf ( description + 2 , " " , & bus_number , & device_address ) != 2 ) ftdi_error_return_free_device_list ( -11 , " " , devs ) ; while ( ( dev = devs [ i ++ ] ) != NULL ) { int ret ; if ( bus_number == libusb_get_bus_number ( dev ) && device_address == libusb_get_device_address ( dev ) ) { ret = ftdi_usb_open_dev ( ftdi , dev ) ; libusb_free_device_list ( devs , 1 ) ; return ret ; } } ftdi_error_return_free_device_list ( -3 , " " , devs ) ; } else if ( description [ 0 ] == ' ' || description [ 0 ] == ' ' ) { unsigned int vendor ; unsigned int product ; unsigned int index = 0 ; const char * serial = NULL ; const char * startp , * endp ; errno = 0 ; startp = description + 2 ; vendor = strtoul ( ( char * ) startp , ( char * * ) & endp , 0 ) ; if ( * endp != ' ' || endp == startp || errno != 0 ) ftdi_error_return ( -11 , " " ) ; startp = endp + 1 ; product = strtoul ( ( char * ) startp , ( char * * ) & endp , 0 ) ; if ( endp == startp || errno != 0 ) ftdi_error_return ( -11 , " " ) ; if ( description [ 0 ] == ' ' && * endp != 0 ) { if ( * endp != ' ' ) ftdi_error_return ( -11 , " " ) ; startp = endp + 1 ; index = strtoul ( ( char * ) startp , ( char * * ) & endp , 0 ) ; if ( * endp != 0 || endp == startp || errno != 0 ) ftdi_error_return ( -11 , " " ) ; } if ( description [ 0 ] == ' ' ) { if ( * endp != ' ' ) ftdi_error_return ( -11 , " " ) ; serial = endp + 1 ; } return ftdi_usb_open_desc_index ( ftdi , vendor , product , NULL , serial , index ) ; } else { ftdi_error_return ( -11 , " " ) ; } }
Opens the ftdi-device described by a description-string.
Opens the ftdi - device described by a description - string .
/* XXX: This doesn't handle symlinks/odd paths/etc... */ // device not found /* optional index field in i-mode */ /* rest of the description is the serial */