Small documentation update
[libftdi] / src / ftdi.c
index 8399030..096a03d 100644 (file)
@@ -158,6 +158,7 @@ int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devli
         ftdi_error_return(-2, "usb_find_devices() failed");
 
     curdev = devlist;
+    *curdev = NULL;
     for (bus = usb_busses; bus; bus = bus->next) {
         for (dev = bus->devices; dev; dev = dev->next) {
             if (dev->descriptor.idVendor == vendor
@@ -186,13 +187,77 @@ int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devli
 */
 void ftdi_list_free(struct ftdi_device_list **devlist)
 {
-    struct ftdi_device_list **curdev;
-    for (; *devlist == NULL; devlist = curdev) {
-        curdev = &(*devlist)->next;
-        free(*devlist);
+    struct ftdi_device_list *curdev, *next;
+
+    for (curdev = *devlist; curdev != NULL;) {
+        next = curdev->next;
+        free(curdev);
+        curdev = next;
     }
 
-    devlist = NULL;
+    *devlist = NULL;
+}
+
+/**
+    Return device ID strings from the usb device.
+
+    The parameters manufacturer, description and serial may be NULL
+    or pointer to buffers to store the fetched strings.
+
+    \note Use this function only in combination with ftdi_usb_find_all()
+          as it closes the internal "usb_dev" after use.
+
+    \param ftdi pointer to ftdi_context
+    \param dev libusb usb_dev to use
+    \param manufacturer Store manufacturer string here if not NULL
+    \param mnf_len Buffer size of manufacturer string
+    \param description Store product description string here if not NULL
+    \param desc_len Buffer size of product description string
+    \param serial Store serial string here if not NULL
+    \param serial_len Buffer size of serial string
+
+    \retval   0: all fine
+    \retval  -1: wrong arguments
+    \retval  -4: unable to open device
+    \retval  -7: get product manufacturer failed
+    \retval  -8: get product description failed
+    \retval  -9: get serial number failed
+    \retval -10: unable to close device
+*/
+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;
+
+    if (!(ftdi->usb_dev = usb_open(dev)))
+        ftdi_error_return(-4, usb_strerror());
+
+    if (manufacturer != NULL) {
+        if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0) {
+            usb_close (ftdi->usb_dev);
+            ftdi_error_return(-7, usb_strerror());
+        }
+    }
+
+    if (description != NULL) {
+        if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0) {
+            usb_close (ftdi->usb_dev);
+            ftdi_error_return(-8, usb_strerror());
+        }
+    }
+
+    if (serial != NULL) {
+        if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0) {
+            usb_close (ftdi->usb_dev);
+            ftdi_error_return(-9, usb_strerror());
+        }
+    }
+
+    if (usb_close (ftdi->usb_dev) != 0)
+        ftdi_error_return(-10, usb_strerror());
+
+    return 0;
 }
 
 /**
@@ -676,6 +741,7 @@ int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunk
     \param size Size of the buffer
 
     \retval <0: error code from usb_bulk_read()
+    \retval  0: no data was available
     \retval >0: number of bytes read
 
     \remark This function is not useful in bitbang mode.