+static bool device_location_equal(libusb_device *device, const char *location)
+{
+ char *loc = strdup(location);
+ uint8_t port_path[7];
+ int path_step, path_len;
+ uint8_t dev_bus = libusb_get_bus_number(device);
+ char *ptr;
+ bool result = false;
+
+ path_len = libusb_get_port_numbers(device, port_path, 7);
+ if (path_len == LIBUSB_ERROR_OVERFLOW) {
+ LOG_ERROR("cannot determine path to usb device! (more than 7 ports in path)");
+ goto done;
+ }
+
+ LOG_DEBUG("device path has %i steps", path_len);
+
+ ptr = strtok(loc, ":");
+ if (ptr == NULL) {
+ LOG_DEBUG("no ':' in path");
+ goto done;
+ }
+ if (atoi(ptr) != dev_bus) {
+ LOG_DEBUG("bus mismatch");
+ goto done;
+ }
+
+ path_step = 0;
+ while (path_step < 7) {
+ ptr = strtok(NULL, ",");
+ if (ptr == NULL) {
+ LOG_DEBUG("no more tokens in path at step %i", path_step);
+ break;
+ }
+
+ if (path_step < path_len
+ && atoi(ptr) != port_path[path_step]) {
+ LOG_DEBUG("path mismatch at step %i", path_step);
+ break;
+ }
+
+ path_step++;
+ };
+
+ /* walked the full path, all elements match */
+ if (path_step == path_len)
+ result = true;
+
+ done:
+ free(loc);
+ return result;
+}
+