libftdi-git Archives

Subject: A library to talk to FTDI chips branch, master, updated. v1.2-28-gae95806

From: libftdi-git@xxxxxxxxxxxxxxxxxxxxxxx
To: libftdi-git@xxxxxxxxxxxxxxxxxxxxxxx
Date: Mon, 18 Apr 2016 22:59:41 +0200 (CEST)
The branch, master has been updated
       via  ae95806888d65aacf931a02a3aca1721af61b06b (commit)
       via  a001e73a039feaf7905a0a782ca16a6d09737985 (commit)
       via  15079e78d3cbca7e33bd7f421c00ada20599e495 (commit)
      from  f838a4e3b0466abc34823750e9eef24d785c8232 (commit)


- Log -----------------------------------------------------------------
commit ae95806888d65aacf931a02a3aca1721af61b06b
Author: Thomas Jarosch <thomas.jarosch@xxxxxxxxxxxxx>
Date:   Mon Apr 18 22:58:44 2016 +0200

    Add Fahrzin Hemmati to AUTHORS, update ChangeLog

commit a001e73a039feaf7905a0a782ca16a6d09737985
Author: Thomas Jarosch <thomas.jarosch@xxxxxxxxxxxxx>
Date:   Mon Apr 18 22:57:00 2016 +0200

    Add ftdi_usb_get_strings2() to the python wrapper

commit 15079e78d3cbca7e33bd7f421c00ada20599e495
Author: Fahrzin Hemmati <fahhem@xxxxxxxxx>
Date:   Mon Apr 18 22:48:42 2016 +0200

    Add new function ftdi_usb_get_strings2()
    
    This new version of ftdi_usb_get_strings() only closes
    the device if it was opened by the function, too.
    
    [minor code and changelog tweaks by Thomas Jarosch]

-----------------------------------------------------------------------

Summary of changes:
 AUTHORS        |    1 +
 ChangeLog      |    1 +
 python/ftdi1.i |    5 ++++
 src/ftdi.c     |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 src/ftdi.h     |   10 +++++--
 5 files changed, 80 insertions(+), 8 deletions(-)

diff --git a/AUTHORS b/AUTHORS
index 0200e22..fce747b 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -25,6 +25,7 @@ see Changelog for full details:
   Eugene Hutorny <eugene@xxxxxxxxxxxxx>
   Evan Nemerson <evan@xxxxxxxxxxxxxxx>
   Evgeny Sinelnikov <sin@xxxxxxxx>
+  Fahrzin Hemmati <fahhem@xxxxxxxxx>
   Flynn Marquardt <ftdi@xxxxxxxxxx>
   Forest Crossman <cyrozap@xxxxxxxxx>
   Ian Abbott <abbotti@xxxxxxxxx>
diff --git a/ChangeLog b/ChangeLog
index 82c5a9c..aed9adf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,6 @@
 New in 1.3 - 2016-04-xx
 -----------------------
+* Added ftdi_usb_get_strings2() to prevent automatic device close (Fahrzin 
Hemmati)
 * Added ftdi_transfer_data_cancel for cancelation of a submitted transfer,
   avoided resubmittion of a cancelled transfer in the callbacks, replaced calls
   to libusb_handle_events with libusb_handle_events_timeout_completed
diff --git a/python/ftdi1.i b/python/ftdi1.i
index cc71a3c..755b6b9 100644
--- a/python/ftdi1.i
+++ b/python/ftdi1.i
@@ -60,6 +60,7 @@ inline char * str2charp_size(PyObject* pyObj, int * size)
 "usb_get_strings(context, device) -> (return_code, manufacturer, description, 
serial)"
 %enddef
 %feature("autodoc", ftdi_usb_get_strings_docstring) ftdi_usb_get_strings;
+%feature("autodoc", ftdi_usb_get_strings_docstring) ftdi_usb_get_strings2;
 %apply char *OUTPUT { char * manufacturer, char * description, char * serial };
 %cstring_bounded_output( char * manufacturer, 256 );
 %cstring_bounded_output( char * description, 256 );
@@ -69,6 +70,10 @@ inline char * str2charp_size(PyObject* pyObj, int * size)
                              char * manufacturer, int mnf_len,
                              char * description, int desc_len,
                              char * serial, int serial_len);
+    int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device 
*dev,
+                              char * manufacturer, int mnf_len,
+                              char * description, int desc_len,
+                              char * serial, int serial_len);
 %clear char * manufacturer, char * description, char * serial;
 %clear int mnf_len, int desc_len, int serial_len;
 
diff --git a/src/ftdi.c b/src/ftdi.c
index 573080a..529b99e 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -406,16 +406,76 @@ void ftdi_list_free2(struct ftdi_device_list *devlist)
     \retval  -9: get serial number failed
     \retval -11: libusb_get_device_descriptor() failed
 */
-int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * 
dev,
-                         char * manufacturer, int mnf_len, char * description, 
int desc_len, char * serial, int serial_len)
+int ftdi_usb_get_strings(struct ftdi_context *ftdi,
+                         struct libusb_device *dev,
+                         char *manufacturer, int mnf_len,
+                         char *description, int desc_len,
+                         char *serial, int serial_len)
 {
-    struct libusb_device_descriptor desc;
+    int ret;
 
     if ((ftdi==NULL) || (dev==NULL))
         return -1;
 
     if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0)
-            ftdi_error_return(-4, "libusb_open() failed");
+        ftdi_error_return(-4, "libusb_open() failed");
+
+    // ftdi->usb_dev will not be NULL when entering ftdi_usb_get_strings2(), so
+    // it won't be closed either. This allows us to close it whether we 
actually
+    // called libusb_open() up above or not. This matches the expected behavior
+    // (and note) for ftdi_usb_get_strings().
+    ret = ftdi_usb_get_strings2(ftdi, dev,
+                                manufacturer, mnf_len,
+                                description, desc_len,
+                                serial, serial_len);
+
+    // only close it if it was successful, as all other return codes close
+    // before returning already.
+    if (ret == 0)
+        ftdi_usb_close_internal(ftdi);
+
+    return ret;
+}
+
+/**
+    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 The old function ftdi_usb_get_strings() always closes the device.
+          This version only closes the device if it was opened by it.
+
+    \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 -11: libusb_get_device_descriptor() failed
+*/
+int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev,
+                          char *manufacturer, int mnf_len,
+                          char *description, int desc_len,
+                          char *serial, int serial_len)
+{
+    struct libusb_device_descriptor desc;
+
+    if ((ftdi==NULL) || (dev==NULL))
+        return -1;
+
+    char need_open = (ftdi->usb_dev == NULL);
+    if (need_open && 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");
@@ -447,7 +507,8 @@ int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct 
libusb_device * dev,
         }
     }
 
-    ftdi_usb_close_internal (ftdi);
+    if (need_open)
+        ftdi_usb_close_internal (ftdi);
 
     return 0;
 }
diff --git a/src/ftdi.h b/src/ftdi.h
index fa53b29..ca38bbe 100644
--- a/src/ftdi.h
+++ b/src/ftdi.h
@@ -480,9 +480,13 @@ extern "C"
     void ftdi_list_free(struct ftdi_device_list **devlist);
     void ftdi_list_free2(struct ftdi_device_list *devlist);
     int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device 
*dev,
-                             char * manufacturer, int mnf_len,
-                             char * description, int desc_len,
-                             char * serial, int serial_len);
+                             char *manufacturer, int mnf_len,
+                             char *description, int desc_len,
+                             char *serial, int serial_len);
+    int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device 
*dev,
+                              char *manufacturer, int mnf_len,
+                              char *description, int desc_len,
+                              char *serial, int serial_len);
     int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
                                 char * product, char * serial);
 


hooks/post-receive
-- 
A library to talk to FTDI chips

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

Current Thread
  • A library to talk to FTDI chips branch, master, updated. v1.2-28-gae95806, libftdi-git <=