Add Android 5-7 support#242
Conversation
|
Please refer to the comments at #233
|
|
@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:
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)? |
|
@vianney It will be helpful that you can subscribe to libusb mailing list and post there to seek comments. Thanks. |
|
@vianney Thanks for the patch! If I'm calling libusb_wrap_fd instead of libusb_open, does it mean I shouldn't call libusb_close? |
|
Could someone explain how this Android solution should be used? What to call and in what order? |
Mostly untested. Write more in the commit message. libusb/libusb#242 Signed-off-by: Stephan Linz <linz@li-pro.net>
Mostly untested. Write more in the commit message. libusb/libusb#242 Signed-off-by: Stephan Linz <linz@li-pro.net>
Mostly untested. Write more in the commit message. libusb/libusb#242 Signed-off-by: Stephan Linz <linz@li-pro.net>
|
This looks like it might be what I'm looking for in order to be able to connect to USB dive computers from Android... but just like @chipweinberger I am wondering how to use these patches in order to be able to open an FTDI device from Android... Help would be appreciated. |
|
I'm also currently trying to use these patches, and I'm trying to use existing code (libmaxtouch) that relies on the contents of the I tried the
If it was - at least on some devices - really impossible to get the |
|
@reflog If I'm reading the comment in this commit you should call libusb_close to close the libusb object, but you would still need to manually close the OS file descriptor manually afterward. @vianney Does the above sound right? @lenaschimmel Not immediately useful for libusb but you might want to look into getRawDescriptors in the Android API. |
|
@NickAtAccuPS On a first glimpse, that seems very useful. The android documentation says nothing about its endianness, but that should be an easy trial-end-error process. Also, it's unclear to me which descriptor(s) is/are returned. If it's just the 18 byte device descriptor, that's not entirely useful, because there seems to be no API to get the other descriptors (Configuration Descriptors, Interface Descriptors, Endpoint Descriptors, String Descriptors), but I guess this will return all the descriptors concatenated, just as if we read them from the usbfs? (Sorry, didn't have time to just try it on Android). If so, I think we could just add a few more parameters to Am I missing anything? |
|
Guys, did you read the documentation? Let me quote the doc for
and what I wrote in the description of this pull request:
It follows that:
To maintainers: when I get some time, I will rebase this patch onto master and post to the mailing list. Unfortunately, other projects keep me quite busy right now. |
|
@vianney Thank you very much! I was (mostly) aware of your points 1, 2 and 4 but I completely missed 3 (probably because the existing code I worked with assumed to have access to the descriptors before opening the device). A quick test on my device confirms that I understood it correctly this time. |
|
Hi All, Now in my native-lib.cpp I am trying to use libusb's functions to init or get list of device. But I am failing. I have also posted Question on stackoverflow related to this: https://stackoverflow.com/q/44281689/1468354 If you need any more info. I will provide |
|
@akshaytaru2007 As stated in this readme, the usual Also, you need a patched version of Regarding the list of devices, I think it is not possible to use @ALL Maybe I can setup a sample Android project somewhere next week and post a link. This should make it easier for new users to get started. @vianney I respect that you don't have much time right now. I just want to say that it seems that maybe rebasing might be a matter of 5 minutes work - I just had to fix two trivial merge conflicts. Anyway, I'm looking forward to the day when this goes upstream! |
|
@lenaschimmel I tried using @lenaschimmel As you guys know much about USB communication, I am asking your assistance that is there any way I can talk to this device |
|
@akshaytaru2007 I'm very new to USB communication and I think if you get an empty list from Once you get the |
Mostly untested. Write more in the commit message. libusb/libusb#242 Signed-off-by: Stephan Linz <linz@li-pro.net>
|
I modification bu your code ,now can use the libusb in android 7.0,THX ; |
c816259 to
8681342
Compare
|
Could even make the old call be a helper: static inline int libusb_wrap_fd (libusb_context *ctx, int fd, libusb_device_handle **dev_handle) {
return libusb_wrap_sys_device (ctx, (intptr_t) fd, dev_handle);
} |
Handle devices that are initialized with NULL as sysfs_dir. For such devices, the usbfs functions should be used instead of their sysfs counterparts. Signed-off-by: Vianney le Clément de Saint-Marcq <code@quartic.eu>
Slightly refactor op_open() to extract the device handle initialization code into a new initialize_handle() function, similar to the initialize_device() function. Signed-off-by: Vianney le Clément de Saint-Marcq <code@quartic.eu>
Extend linux_get_device_address() to try to read the device address from a file descriptor as a last resort, if provided. Additionally, linux_get_device_address() will now return an error if the path could not be parsed. Signed-off-by: Vianney le Clément de Saint-Marcq <code@quartic.eu>
Introduce a new API function for wrapping an existing platform-specific device handle as a libusb_device_handle. Signed-off-by: Vianney le Clément de Saint-Marcq <code@quartic.eu>
Add support for the libusb_wrap_sys_device() API on Linux. Because sysfs_dir is set to NULL, only the provided file descriptor will be used. This is needed on some platforms (e.g., Android) where sysfs might not be available. Signed-off-by: Vianney le Clément de Saint-Marcq <code@quartic.eu>
On newer Android devices (5+), SELinux policies block normal processes from reading /dev. The consequence is that the usbfs path cannot be found. Rather than failing to initialize libusb in such case, fall back to /dev/bus/usb. This path will actually not be used, as USB devices should be opened through the Android API, passing the file descriptor to libusb_wrap_sys_device. Signed-off-by: Vianney le Clément de Saint-Marcq <code@quartic.eu>
On Android, the platform API should be used to scan for and open devices and pass file descriptors to libusb. Newer devices (Android 5+) even prohibit listening for hotplug events, resulting in libusb failing to initialize without this patch. Note that this patch effectively renders libusb useless on older devices that do not have USB support in the platform API (anything before Android 5). Signed-off-by: Vianney le Clément de Saint-Marcq <code@quartic.eu>
|
@hjelmn I have just rebased my branch to the latest master and renamed the API function to The patch set is sent to the libusb-devel mailing list. I hope it will go through, as I am not registered to the list. |
|
@vianney It will once I approve it. I will keep an eye out for the email. |
|
@vianney Approved it on the mailing list. Will see what (if anything) other people have to say. |
|
I've been trying to use this, and wonder if anyone could help me understand if:
|
On android we need to get a file descriptor through the android API and then get the device handle from libusb_wrap_sys_device, instead of through libusb_open as on a "normal" GNU/Linux system. With these changes it is possible to use ttwatch from termux with a command like: ``` termux-usb -r -e "ttwatch --update-gps --usb-fd" /dev/bus/usb/001/002 ``` The file descriptor is then passed as the last argument to ttwatch from termux-usb. Further reading: libusb/libusb#242 I should refactor this, make it compatible with both android and GNU/Linux and open a PR against the ttwatch repo.
libusb_wrap_sys_device was the requested name in the pull request to the libusb project for Android support: libusb/libusb#242
On Android, the platform API should be used to scan for and open devices and pass file descriptors to libusb. Newer devices (Android 5+) even prohibit listening for hotplug events, resulting in libusb failing to initialize without this patch. Note that this patch effectively renders libusb useless on older devices that do not have USB support in the platform API (anything before Android 5). Closes libusb#242 Signed-off-by: Vianney le Clément de Saint-Marcq <code@quartic.eu> Signed-off-by: Nathan Hjelm <hjelmn@me.com>
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:
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.