Fixed Context::open as get_strings() closes the device before. Thanks to Chris M...
authorMarek Vavrusa <marek@vavrusa.com>
Mon, 4 May 2009 09:54:52 +0000 (11:54 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 4 May 2009 15:52:20 +0000 (17:52 +0200)
Intra2net: Reverted return code behavior of Context::open() calls

examples/find_all_pp.cpp
ftdipp/ftdi.cpp

index 4e8e824..98e5133 100644 (file)
@@ -51,8 +51,17 @@ int main(int argc, char **argv)
         std::cout << "FTDI (" << &*it << "): "
         << it->vendor() << ", "
         << it->description() << ", "
-        << it->serial()
-        << std::endl;
+        << it->serial();
+
+        // Open test
+        if(it->open())
+           std::cout << " (Open OK)";
+        else
+           std::cout << " (Open FAILED)";
+
+        it->close();
+
+        std::cout << std::endl;
 
     }
 
index 584a04a..e5a8966 100644 (file)
@@ -81,37 +81,39 @@ int Context::open(int vendor, int product, const std::string& description, const
 {
     int ret = 0;
 
+    // Open device
     if (description.empty() && serial.empty())
         ret = ftdi_usb_open(d->ftdi, vendor, product);
     else
         ret = ftdi_usb_open_desc(d->ftdi, vendor, product, description.c_str(), serial.c_str());
 
-    d->dev = usb_device(d->ftdi->usb_dev);
+    if (ret < 0)
+       return ret;
 
-    if ((ret = ftdi_usb_open_dev(d->ftdi, d->dev)) >= 0)
-    {
-        d->open = true;
-        get_strings();
-    }
+    // Get device strings (closes device)
+    get_strings();
+
+    // Reattach device
+    ret = ftdi_usb_open_dev(d->ftdi, d->dev);
+    d->open = (ret >= 0);
 
     return ret;
 }
 
 int Context::open(struct usb_device *dev)
 {
-    int ret = 0;
-
     if (dev != 0)
         d->dev = dev;
 
     if (d->dev == 0)
         return -1;
 
-    if ((ret = ftdi_usb_open_dev(d->ftdi, d->dev)) >= 0)
-    {
-        d->open = true;
-        get_strings();
-    }
+    // Get device strings (closes device)
+    get_strings();
+
+    // Reattach device
+    int ret = ftdi_usb_open_dev(d->ftdi, d->dev);
+    d->open = (ret >= 0);
 
     return ret;
 }