+/**
+ * Adapter configuration
+ */
+static struct {
+ char *usb_location;
+} adapter_config;
+
+/*
+ * 1 char: bus
+ * 2 * 7 chars: max 7 ports
+ * 1 char: test for overflow
+ * ------
+ * 16 chars
+ */
+#define USB_MAX_LOCATION_LENGTH 16
+
+#ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
+static void adapter_usb_set_location(const char *location)
+{
+ if (strnlen(location, USB_MAX_LOCATION_LENGTH) == USB_MAX_LOCATION_LENGTH)
+ LOG_WARNING("usb location string is too long!!");
+
+ free(adapter_config.usb_location);
+
+ adapter_config.usb_location = strndup(location, USB_MAX_LOCATION_LENGTH);
+}
+#endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
+
+const char *adapter_usb_get_location(void)
+{
+ return adapter_config.usb_location;
+}
+
+bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len)
+{
+ size_t path_step, string_length;
+ char *ptr, *loc;
+ bool equal = false;
+
+ if (!adapter_usb_get_location())
+ return equal;
+
+ /* strtok need non const char */
+ loc = strndup(adapter_usb_get_location(), USB_MAX_LOCATION_LENGTH);
+ string_length = strnlen(loc, USB_MAX_LOCATION_LENGTH);
+
+ ptr = strtok(loc, "-");
+ if (!ptr) {
+ LOG_WARNING("no '-' in usb path\n");
+ goto done;
+ }
+
+ string_length -= strnlen(ptr, string_length);
+ /* check bus mismatch */
+ if (atoi(ptr) != dev_bus)
+ goto done;
+
+ path_step = 0;
+ while (path_step < path_len) {
+ ptr = strtok(NULL, ".");
+
+ /* no more tokens in path */
+ if (!ptr)
+ break;
+
+ /* path mismatch at some step */
+ if (path_step < path_len && atoi(ptr) != port_path[path_step])
+ break;
+
+ path_step++;
+ string_length -= strnlen(ptr, string_length) + 1;
+ };
+
+ /* walked the full path, all elements match */
+ if (path_step == path_len && !string_length)
+ equal = true;
+
+done:
+ free(loc);
+ return equal;
+}
+