libftdi Archives

Subject: PATCH: Add a ftdi_usb_open_bus_port function.

From: Maxwell Dreytser <admin@xxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Sun, 9 Apr 2017 20:16:38 -0400
Hello all,

This patch adds a function that will open using the specified Bus and Port. I don't know how useful it would be to others, but I decided to submit it cause why not. I pretty much just copied "ftdi_usb_open_desc_index" and modified the parameters and loop.

Maxwell.

--- PATCH Below This line ---
From f49494b97dfeab9ebb5daeeea97ef622f9cc7c9c Mon Sep 17 00:00:00 2001
From: Maxwell Dreytser <admin@xxxxxxxxx>
Date: Sun, 9 Apr 2017 20:02:56 -0400
Subject: [PATCH] Add a ftdi_usb_open_bus_port function.

---
 src/ftdi.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/ftdi.h |  1 +
 2 files changed, 48 insertions(+)

diff --git a/src/ftdi.c b/src/ftdi.c
index 2fff1ff..d44cbb3 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -824,6 +824,53 @@ int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
 }

 /**
+    Opens the device at a given USB Bus and Port.
+
+    \param ftdi pointer to ftdi_context
+    \param bus Bus Number
+    \param port Port Number
+
+    \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_bus_port(struct ftdi_context *ftdi, uint8_t bus, uint8_t port)
+{
+    libusb_device *dev;
+    libusb_device **devs;
+    int i = 0;
+
+    if (ftdi == NULL)
+        ftdi_error_return(-11, "ftdi context invalid");
+
+    if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
+        ftdi_error_return(-12, "libusb_get_device_list() failed");
+
+    while ((dev = devs[i++]) != NULL)
+    {
+ if (libusb_get_bus_number(dev) == bus && libusb_get_port_number(dev) == port)
+        {
+            int res;
+            res = ftdi_usb_open_dev(ftdi, dev);
+            libusb_free_device_list(devs,1);
+            return res;
+        }
+    }
+
+    // device not found
+    ftdi_error_return_free_device_list(-3, "device not found", devs);
+}
+
+/**
     Opens the ftdi-device described by a description-string.
Intended to be used for parsing a device-description given as commandline argument.

diff --git a/src/ftdi.h b/src/ftdi.h
index ef174bf..5927c48 100644
--- a/src/ftdi.h
+++ b/src/ftdi.h
@@ -502,6 +502,7 @@ extern "C"
                            const char* description, const char* serial);
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_bus_port(struct ftdi_context *ftdi, uint8_t bus, uint8_t port); int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct libusb_device *dev); int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description);

--
2.9.3


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