Add Android 5-7 support
This patch series adds support for recent Android devices (tested up to 7.1.1). On such devices, SELinux policies prevent the use of sysfs and reading /dev/bus/usb. The only way to use libusb as a regular user is to get a file descriptor through the Android Platform API and pass it on. All operations must use that file descriptor.
This series adds an API function to libusb to wrap an open file descriptor as a libusb_device_handle. To ease reviewing, it is decomposed as follows:
* Patches 1-3 contain light refactoring and small utility extensions needed later on.
* Patch 4 adds the public API function.
* Patch 5 implements the new API for the Linux platform.
* Patch 6-7 ensure the libusb library is able to initialize on Android.
* Patch 8 is a minor warning fix as a bonus.
Please note that patch 7 (disable hotplug events on Android) means that libusb cannot be used anymore standalone on a rooted Android device. Is that an issue? If so, I guess the way to go is to make it a compile-time option.
Please refer to the comments at https://github.com/libusb/libusb/issues/233
>#242 can solve the problem. The naming of the function is confusing though. The function libusb_wrap_fd is escentially opening the usb by file descriptor. Also, it will not add the usb device the to device list in libusb, so mind that some libraries depend on libusb may need to be modified to make it working.
@mcuee @kennethtang4 Thanks for the feedback. I am willing to improve this patchset to get it merged upstream. So let's get the discussion started.
Concerning the naming, I have chosen "wrap" over "open" to make it clear that we do not open a new file descriptor (and hence, that the provided file descriptor must not be closed), but I would not mind naming it "libusb_open_fd" or anything else. Suggestions are welcome.
I also have the concern that file descriptors are backend-specific (event though most OS have such concept). Having them exposed in the user API is not very nice, but I am afraid there is no way around it. Maybe we should replace the int argument by something more platform-agnostic, but what?
About the device list: yes, this feels a bit hacky. I would appreciate some input from libusb developers on the best way to handle this. The requirement for Android is that we should not rely on anything besides the provided file descriptor. This means that in the worst case, if the link in /proc/self/fd is unreadable or non-standard, we do not know the bus number of the device (afaik, there is no ioctl for that). Maybe we could do the following:
* If we got the complete device address: reuse existing libusb_device if it was enumerated. Otherwise create a new one, add it to the list, and remove it when the handle is closed.
* If the bus number is unknown: create a new libusb_device using the file descriptor as session id, add it to the list, and remove it when the handle is closed.
To libusb maintainers: is there any interest in merging this kind of patches? If so, should we keep compatibility with using libusb in the standard way on a rooted Android device (i.e., with hotplug enumeration and libusb_open)?