title: linux / libusb get usb device path
body: <p>I use libusb to enumerate over a few usb-devices. Now i like to get the "device-path". I think it's not called usb device-path, because i was not successful with google.</p>
  
  <p>If i connect a usb-device with linux, i get a message in <code>dmesg</code>, here are a few examples for such a "device-path" with an usb temperature sensor (something like <a href="http://www.ohgizmo.com/wp-content/uploads/2007/09/usb_temper.jpg" rel="noreferrer">this</a>):</p>
  
  <p>Directly to a usb port:
  <code>[68448.099682] generic-usb 0003:0C45:7401.0056: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.0-1/input0</code> => <strong>12.0-1</strong></p>
  
  <p>Directly to another port:
  <code>[68560.853108] generic-usb 0003:0C45:7401.0058: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:13.0-1/input0</code> => <strong>13.0-1</strong></p>
  
  <p>To a usb hub on the first used port:
  <code>[68600.245809] generic-usb 0003:0C45:7401.005A: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.4/input0</code> => <strong>12.2-1.4</strong></p>
  
  <p>To another port on the same usb hub:
  <code>[68647.925092] generic-usb 0003:0C45:7401.005C: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.3/input0</code> => <strong>12.2-1.3</strong></p>
  
  <p>An now to a usb hub on the usb hub used before:
  <code>[68740.715518] generic-usb 0003:0C45:7401.005E: input,hidraw1: USB HID v1.10 Keyboard [RDing TEMPer1V1.2] on usb-0000:00:12.2-1.4.4/input0</code> => <strong>12.2-1.4.4</strong></p>
  
  <p>Long story short:
  The kernel message always contains a unique path for the physical usb device location (see the bold text before). Is it possible to get this "path" in the user space via libusb? I tried many things with <code>struct usb_bus</code> and <code>struct usb_device</code>, but i always was unsuccessfully.</p>
  
  <p>I need this to identify multiple of these usb thermometers, because they don't have a unique serial number and sometimes they just "reconnect" at runtime, so they get different usb id's. So i think the only way to identify them is via the physical location.</p>
  
  <p>Thanks for the help,</p>
  
  <p>Best Regards
  Kevin M.</p>
  
  <p>-edit-</p>
  
  <p>Currently i use the following code to search my usb device:</p>
  
  <pre><code>usb_dev_handle *find_lvr_winusb() {
  
       struct usb_bus *bus;
          struct usb_device *dev;
  
          for (bus = usb_busses; bus; bus = bus-&gt;next) {
          for (dev = bus-&gt;devices; dev; dev = dev-&gt;next) {
                          if (dev-&gt;descriptor.idVendor == VENDOR_ID &amp;&amp; 
                                  dev-&gt;descriptor.idProduct == PRODUCT_ID ) {
                                  usb_dev_handle *handle;
                                  if(debug) {
                                    printf("lvr_winusb with Vendor Id: %x and Product Id: %x found.\n", VENDOR_ID, PRODUCT_ID);
                                      printf("INFO: %d\n", dev-&gt;bus-&gt;location);
                                      printf("INFO: %d %s\n", bus-&gt;location, bus-&gt;dirname);
                                  }
  
                                  if (!(handle = usb_open(dev))) {
                                          printf("Could not open USB device\n");
                                          return NULL;
                                  }
                                  return handle;
                          }
                  }
          }
          return NULL;
  }
  </code></pre>
  
  <p>But with this code i cannot get a unique physical position id. The <code>bus-&gt;location</code> returns an integer (bus->dirname contains the same, but as string), which is not unique. I know usb has a hierarchy and in the <code>dmesg</code> i can see this hierarchys path.</p>
  
  <p>With libusb i only can get the bus-id (?) and some device id's. But they don't help me, because i need to identify two or more of these temperature sensors. The device-id always changes when the temperature sensor reset's the connection (every 5 to 60 seconds) and the bus id is not unique. Unfortunately the temperature sensor has no unique serial id.</p>
  
  <p>So i think the physical path is the only way to identify the device.</p>
  
  <p>Best regards
  Kevin M.</p>