Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get device path / port number #10

Open
siochs opened this issue Dec 15, 2017 · 4 comments
Open

get device path / port number #10

siochs opened this issue Dec 15, 2017 · 4 comments

Comments

@siochs
Copy link

siochs commented Dec 15, 2017

Hi, using following code:

        UsbPort port = usbDevice.getParentUsbPort();
        System.out.println("Connected to port: " + port.getPortNumber());
        System.out.println("Parent: " + port.getUsbHub()); 

I can dump some basic info about the device connection. However, I need to know the physical location of the device. I can see it in dmesg:

[ 1415.390194] usb 1-1.2: Manufacturer: SanDisk

Attaching the device to another port prints

[ 1415.390194] usb 1-1.4: Manufacturer: SanDisk

So how do I retrieve the physical address 1-1.4 using usb4java-javax? The code above always prints the same port information, even when switching the ports.
See also this thread on StackOverflow.

@siochs
Copy link
Author

siochs commented Dec 15, 2017

Additional info:
I looked a bit into Usb4Java and was able to retrieve the physical path with the following code:

                        ByteBuffer path = BufferUtils.allocateByteBuffer(8);
                        result = LibUsb.getPortNumbers(device, path);
                        if (result > 0)
                        {
                            for (int i = 0; i < result; i++)
                            {                                
                                System.out.print(path.get(i));
                                if (i + 1 < result) System.out.print("-");
                            }
                            System.out.println("");
                        }                        

So when attaching a thumb drive between two different usb jacks it prints me for example 1-2 or 1-4.
It took a look into getPortNumbers(device, path); which dervies from libusb_get_port_numbers from LibUsb core.c. Basically, libusb_get_port_numbers does nothing elese then descending the device tree and places the according port number in a byte array:

...
		port_numbers[i] = dev->port_number;
		dev = dev->parent_dev;
...

Then, I tried to mimick that behaviour with Usb4Java-Javax:

        UsbPort port = usbDevice.getParentUsbPort();
        while (port != null)
        {
            System.out.print(port.getPortNumber());
            port = port.getUsbHub().getParentUsbPort();
            if (port != null) System.out.print("-");
        }

However, this does not work as expected since this allways gives me 1-2, independet of the port I attach the thumb drive to. Is this a bug or do I oversee something?

@Andrei-Pozolotin
Copy link

perhaps it is easier to hack around package/protected name space and cast
UsbDevice -> AbstractDevice to get access to bus/addr/port triple from DeviceId

see:

@siochs
Copy link
Author

siochs commented Oct 2, 2018

Yeah, this is what I ended up with. I added an Usb4JavaJavaxLibraryWrapper as part of the package org.usb4java.javax and wrote some casting functions like

public int getBusNumberFromUsbDevice(UsbDevice usbDevice)
    {
        DeviceId deviceId = ((AbstractDevice) usbDevice).getId();
        return deviceId.getBusNumber();        
    }

@kayahr
Copy link
Member

kayahr commented Oct 21, 2018

Changing the visibility of classes or methods is not an option here because this would break the compatibility to the javax.usb spec. But I guess this could be fixed properly by using the port numbers reported by libusb instead of creating virtual port numbers. I'm open for pull requests here because of lack of time and fading javax-usb knowledge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants