Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm searching for the method in linux java to get the native file descriptor of an usb device. In Android Java I can use the Method:
int
getFileDescriptor()
Returns the native file descriptor for the device, or -1 if the device is not opened.



Thanks,

Peter

What I have tried:

On Linux I use Usb4Java to communicate with the device. I want to send an ioctl to the device and there for I need the native FileDescriptor.
Posted
Updated 22-May-18 21:47pm

1 solution

An IOCTL is not handled by a device but by a device specific driver. But libusb is a library for generic access to USB devices bypassing any device specific drivers.

libusb uses file descriptors internally but those are not accessible through any public function.

So you have either to open the USB device the "normal" way to use device specific IOCTLs or try to achieve the required operation using libusb functions.

For the first option use libusb as required (e.g. for identifying the device), close the libusb handle, and open the device for usage of IOCTLs. After closing that handle you may re-open using libusb.

For the second option check if the IOCTL is already executed by libusb (e.g. when a specific option is set) or if it is used when calling a function. This requires looking at the libusb sources.
 
Share this answer
 
Comments
Peter____ 23-May-18 13:10pm    
Hello Jochen,

are there better solutions than Usb4Java to get FileDescriptors from UsbDevices?
Perhaps the high level Api: Javax-Usb.

To Solution Nr. 1: Do you know any examples for opening an usb device the normal way, where I can have a look at, to find a way to get the ioctl working?

The aim is to start an isochronous transfer to a camera device over java. The used source class is UsbIso ( http://www.source-code.biz/snippets/java/UsbIso/ ), for which I need the native FileDescriptor.

Thanks,

Peter
Jochen Arndt 23-May-18 13:24pm    
I'm sorry, I have no examples.
I have also more knowledge about C/C++. There you can open a device using it's name and get the file descriptor. There should be something similar with Java when having a Java device handle.

Java is just not the language of choice for such low level stuff.
Peter____ 27-May-18 6:57am    
Thx for the reply.

In Java you can use JNA to get access to the native Libs.
Then you can call the Linux_C_lib_DirectMapping C Library and get the FileDescriptor with open(Path, Permission).

Serial Port Example:

http://www.source-code.biz/snippets/java/SerialPortChannel/SerialPortChannel.java
Jochen Arndt 28-May-18 2:45am    
Then use that. There is no method to get the FD used internally by libusb, even for C/C++ programs (besides implementing that in the sources and building your own version).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900