From: Soeren Apel Date: Mon, 22 Sep 2014 21:08:17 +0000 (+0200) Subject: Add usb_get_port_path() helper function X-Git-Tag: libsigrok-0.4.0~988 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=db156e5409b44bb90a81c1fcd160c2313fc156a0 Add usb_get_port_path() helper function There is currently no way to uniquely identify USB devices in libsigrok. It uses the "bus.address" scheme which is only constant as long as the device remains attached to the bus. In order to allow USB device persistence in PulseView, devices need to provide a unique identifier even in absence of a serial number. This function is the first step as it provides the ability to query the physical location of a USB device. --- diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index b0df768c..28e26c1a 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -679,6 +679,7 @@ SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb); SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx, int timeout, sr_receive_data_callback cb, void *cb_data); SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx); +SR_PRIV int usb_get_port_path(libusb_device *dev, const char *path, int path_len); #endif /*--- hardware/common/scpi.c ------------------------------------------------*/ diff --git a/src/usb.c b/src/usb.c index c3279d0a..1c9e4118 100644 --- a/src/usb.c +++ b/src/usb.c @@ -20,6 +20,7 @@ */ #include +#include #include #include #include "libsigrok.h" @@ -269,3 +270,28 @@ SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx return SR_OK; } + +SR_PRIV int usb_get_port_path(libusb_device *dev, const char *path, int path_len) +{ + gchar s[64]; + gchar tmp_s[8]; + uint8_t port_numbers[32]; + int i; + + memset(port_numbers, 0, sizeof(port_numbers)); + libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers)); + + g_snprintf((gchar*)&s, sizeof(s), "usb/%d-%d", + libusb_get_bus_number(dev), port_numbers[0]); + + i = 1; + while (port_numbers[i]) { + g_snprintf(tmp_s, sizeof(tmp_s), ".%d", port_numbers[i]); + g_strlcat((gchar*)&s, tmp_s, sizeof(s)); + i++; + } + + g_strlcpy((gchar*)path, s, path_len); + + return SR_OK; +}