libftdi Archives

Subject: [PATCH] fixed open-function of cpp-wrapper

From: Peter Schneider <pitpompej@xxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Mon, 24 Jan 2011 11:40:37 +0100 (CET)
Subject: [PATCH] fixed open-function of cpp-wrapper, therefor change in 
ftdi_usb_open_desc needed to make it return usb_device struct

---
 ftdipp/ftdi.cpp |    9 ++-
 src/ftdi.c      |  187 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 src/ftdi.h      |    3 +
 3 files changed, 183 insertions(+), 16 deletions(-)

diff --git a/ftdipp/ftdi.cpp b/ftdipp/ftdi.cpp
index 8511a91..b4b268b 100644
--- a/ftdipp/ftdi.cpp
+++ b/ftdipp/ftdi.cpp
@@ -80,7 +80,8 @@ bool Context::is_open()
 int Context::open(int vendor, int product)
 {
     // Open device
-    int ret = ftdi_usb_open(d->ftdi, vendor, product);
+    int ret = ftdi_usb_get_dev_desc_index(d->ftdi, vendor, product,
+            NULL, NULL, 0, &(d->dev));
 
     if (ret < 0)
        return ret;
@@ -99,7 +100,7 @@ int Context::open(int vendor, int product, const 
std::string& description, const
     if (!serial.empty())
         c_serial=serial.c_str();
 
-    int ret = ftdi_usb_open_desc_index(d->ftdi, vendor, product, 
c_description, c_serial, index);
+    int ret = ftdi_usb_get_dev_desc_index(d->ftdi, vendor, product, 
c_description, c_serial, index, &(d->dev));
 
     if (ret < 0)
        return ret;
@@ -109,7 +110,7 @@ int Context::open(int vendor, int product, const 
std::string& description, const
 
 int Context::open(const std::string& description)
 {
-    int ret = ftdi_usb_open_string(d->ftdi, description.c_str());
+    int ret = ftdi_usb_get_dev_string(d->ftdi, description.c_str(), &(d->dev));
 
     if (ret < 0)
        return ret;
@@ -309,7 +310,7 @@ int Context::get_strings()
     int ret = ftdi_usb_get_strings(d->ftdi, d->dev, vendor, 512, desc, 512, 
serial, 512);
 
     if (ret < 0)
-        return -1;
+        return ret;
 
     d->vendor = vendor;
     d->description = desc;
diff --git a/src/ftdi.c b/src/ftdi.c
index 9cf425f..b846fc2 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -390,7 +390,7 @@ int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct 
usb_device * dev,
                          char * manufacturer, int mnf_len, char * description, 
int desc_len, char * serial, int serial_len)
 {
     if ((ftdi==NULL) || (dev==NULL))
-        return -1;
+        ftdi_error_return(-1, "wrong arguments");
 
     if (!(ftdi->usb_dev = usb_open(dev)))
         ftdi_error_return(-4, usb_strerror());
@@ -638,7 +638,7 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int 
vendor, int product,
 }
 
 /**
-    Opens the index-th device with a given, vendor id, product id,
+    Gets the index-th usb-device with a given, vendor id, product id,
     description and serial.
 
     \param ftdi pointer to ftdi_context
@@ -647,6 +647,7 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int 
vendor, int product,
     \param description Description to search for. Use NULL if not needed.
     \param serial Serial to search for. Use NULL if not needed.
     \param index Number of matching device to open if there are more than one, 
starts with 0.
+    \param usb_dev reference to an usb_device structure pointer
 
     \retval  0: all fine
     \retval -1: usb_find_busses() failed
@@ -661,11 +662,12 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int 
vendor, int product,
     \retval -10: unable to close device
     \retval -11: ftdi context invalid
 */
-int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int 
product,
-                       const char* description, const char* serial, unsigned 
int index)
+int ftdi_usb_get_dev_desc_index(struct ftdi_context *ftdi, int vendor, int 
product,
+                       const char* description, const char* serial, unsigned 
int index, struct usb_device **usb_dev)
 {
     struct usb_bus *bus;
-    struct usb_device *dev;
+    struct usb_device **dev;
+    dev = usb_dev;
     char string[256];
 
     usb_init();
@@ -680,17 +682,17 @@ int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, 
int vendor, int product,
 
     for (bus = usb_get_busses(); bus; bus = bus->next)
     {
-        for (dev = bus->devices; dev; dev = dev->next)
+        for ((*dev) = bus->devices; (*dev); (*dev) = (*dev)->next)
         {
-            if (dev->descriptor.idVendor == vendor
-                    && dev->descriptor.idProduct == product)
+            if ((*dev)->descriptor.idVendor == vendor
+                    && (*dev)->descriptor.idProduct == product)
             {
-                if (!(ftdi->usb_dev = usb_open(dev)))
+                if (!(ftdi->usb_dev = usb_open((*dev))))
                     ftdi_error_return(-4, "usb_open() failed");
 
                 if (description != NULL)
                 {
-                    if (usb_get_string_simple(ftdi->usb_dev, 
dev->descriptor.iProduct, string, sizeof(string)) <= 0)
+                    if (usb_get_string_simple(ftdi->usb_dev, 
(*dev)->descriptor.iProduct, string, sizeof(string)) <= 0)
                     {
                         ftdi_usb_close_internal (ftdi);
                         ftdi_error_return(-8, "unable to fetch product 
description");
@@ -704,7 +706,7 @@ int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int 
vendor, int product,
                 }
                 if (serial != NULL)
                 {
-                    if (usb_get_string_simple(ftdi->usb_dev, 
dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0)
+                    if (usb_get_string_simple(ftdi->usb_dev, 
(*dev)->descriptor.iSerialNumber, string, sizeof(string)) <= 0)
                     {
                         ftdi_usb_close_internal (ftdi);
                         ftdi_error_return(-9, "unable to fetch serial number");
@@ -726,7 +728,7 @@ int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int 
vendor, int product,
                     continue;
                 }
 
-                return ftdi_usb_open_dev(ftdi, dev);
+                return 0;
             }
         }
     }
@@ -735,6 +737,47 @@ int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, 
int vendor, int product,
     ftdi_error_return(-3, "device not found");
 }
 
+
+/**
+    Opens the index-th device with a given, vendor id, product id,
+    description and serial.
+
+    \param ftdi pointer to ftdi_context
+    \param vendor Vendor ID
+    \param product Product ID
+    \param description Description to search for. Use NULL if not needed.
+    \param serial Serial to search for. Use NULL if not needed.
+    \param index Number of matching device to open if there are more than one, 
starts with 0.
+
+    \retval  0: all fine
+    \retval -1: usb_find_busses() failed
+    \retval -2: usb_find_devices() 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: ftdi context invalid
+*/
+int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int 
product,
+                       const char* description, const char* serial, unsigned 
int index)
+{
+    struct usb_device *dev;
+
+    int ret_val;
+
+    ret_val = ftdi_usb_get_dev_desc_index(ftdi, vendor, product, description, 
serial, index, &dev);
+    if (0 > ret_val)
+    {
+       return ret_val;
+    }
+    return ftdi_usb_open_dev(ftdi, dev);
+}
+
+
 /**
     Opens the ftdi-device described by a description-string.
     Intended to be used for parsing a device-description given as commandline 
argument.
@@ -851,6 +894,126 @@ int ftdi_usb_open_string(struct ftdi_context *ftdi, const 
char* description)
     }
 }
 
+
+/**
+    gets the usb_device that represents 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
+    \param usb_dev reference to the usb_device pointer that will return the 
ftdi-usb-device
+
+    \note The description format may be extended in later versions.
+
+    \retval  0: all fine
+    \retval -1: usb_find_busses() failed
+    \retval -2: usb_find_devices() 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
+*/
+int ftdi_usb_get_dev_string(struct ftdi_context *ftdi, const char* 
description, struct usb_device ** usb_dev)
+{
+    if (ftdi == NULL)
+        ftdi_error_return(-12, "ftdi context invalid");
+
+    if (description[0] == 0 || description[1] != ':')
+        ftdi_error_return(-11, "illegal description format");
+
+    struct usb_device **dev;
+    dev = usb_dev;
+    if (description[0] == 'd')
+    {
+        struct usb_bus *bus;
+
+        usb_init();
+
+        if (usb_find_busses() < 0)
+            ftdi_error_return(-1, "usb_find_busses() failed");
+        if (usb_find_devices() < 0)
+            ftdi_error_return(-2, "usb_find_devices() failed");
+
+        for (bus = usb_get_busses(); bus; bus = bus->next)
+        {
+            for ((*dev) = bus->devices; (*dev); (*dev) = (*dev)->next)
+            {
+                /* XXX: This doesn't handle symlinks/odd paths/etc... */
+                const char *desc = description + 2;
+                size_t len = strlen(bus->dirname);
+                if (strncmp(desc, bus->dirname, len))
+                    continue;
+                desc += len;
+                if (desc[0] != '/')
+                    continue;
+                ++desc;
+                if (strcmp(desc, (*dev)->filename))
+                    continue;
+                return 0;
+            }
+        }
+
+        // device not found
+        ftdi_error_return(-3, "device not found");
+    }
+    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_get_dev_desc_index(ftdi, vendor, product, NULL, 
serial, index, dev);
+    }
+    else
+    {
+        ftdi_error_return(-11, "illegal description format");
+    }
+}
+
+
 /**
     Resets the ftdi device.
 
diff --git a/src/ftdi.h b/src/ftdi.h
index 0d94526..abc29ec 100644
--- a/src/ftdi.h
+++ b/src/ftdi.h
@@ -356,10 +356,13 @@ extern "C"
     int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product);
     int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
                            const char* description, const char* serial);
+    int ftdi_usb_get_dev_desc_index(struct ftdi_context *ftdi, int vendor, int 
product,
+                       const char* description, const char* serial, unsigned 
int index, struct usb_device **usb_dev);
     int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int 
product,
                            const char* description, const char* serial, 
unsigned int index);
     int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev);
     int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* 
description);
+    int ftdi_usb_get_dev_string(struct ftdi_context *ftdi, const char* 
description, struct usb_device **usb_dev);
 
     int ftdi_usb_close(struct ftdi_context *ftdi);
     int ftdi_usb_reset(struct ftdi_context *ftdi);
-- 
1.7.1

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread