termux / termux-packages

A package build system for Termux.
https://termux.dev
Other
13.22k stars 3.04k forks source link

Add termux-api support to libusb #5831

Open normanr opened 4 years ago

normanr commented 4 years ago

It should be possible to use existing apps that use libusb without requiring them to individually support termux-usb.

Places where this has come up recently:

My proposal would be to enhance libusb so that (on non-rooted devices) it can use termux-api to enumerate and connect to usb devices. It would then be transparent to apps how they end up getting access to the usb devices that they use.

normanr commented 4 years ago

Aa part of https://github.com/pyusb/pyusb/pull/287 I had the idea that the usb library can open the device by executing termux-usb and exec'ing a small helper that passes the device fd back to the original app (via a unix pipe similar to how termux-api sends the fd back to termux). I wrote https://gist.github.com/normanr/9816a5db24dda0ce87a17da342a9684c as a proof of concept and it works very well.

A couple of small changes to termux-api would make this smoother:

Grimler91 commented 4 years ago

Hi, this would be a great improvement and we would be very happy to merge such a PR!

stale[bot] commented 2 years ago

This issue/PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

twaik commented 10 months ago

I think we may patch libusb to use termux-api for enumerating devices and getting device fds. I think it may be done by implementing some pretty dumb binary which will be invoked by termux-usb binary in order to get answers and file descriptors through some temporary unix socket (maybe passed as non-closeable on exec to target process).

Grimler91 commented 10 months ago

@twaik I did some proof-of-concept stuff in usb branch of termux-api{,-package} were programs could link to libtermux-api.so and get file descriptors through two functions termux_usb_get_fd_from_usbfs and termux_usb_get_fd_from_ids. As a more proper solution I then started looking into implementing a proper termux backend to libusb (where libusb would then communicate with termux-api to get info for filling in libusb_device_descriptor structs), but didn't get very far.

Having some simple functions as in the first attempt at an implementation would probably be useful, I should try to clean that up.. Maybe during the upcoming holidays

knyipab commented 9 months ago

See if this may help: there is a project called AnotherTerm similar to Termux and they implement a patched version libusb with helper service in the app: https://green-green-avk.github.io/AnotherTerm-docs/installing-libusb-for-nonrooted-android.html#main_content

I do not have much knowledge in this field but really glad to see progress of libusb in Termux. Look forward to being able to run Arduino IDE and PlatformIO one day in Termux (perhaps with proot-distro) and we can burn a program into microcontrollers!

twaik commented 9 months ago

See if this may help: there is a project called AnotherTerm similar to Termux and they implement a patched version libusb with helper service in the app: https://green-green-avk.github.io/AnotherTerm-docs/installing-libusb-for-nonrooted-android.html#main_content

I do not have much knowledge in this field but really glad to see progress of libusb in Termux. Look forward to run Arduino IDE and PlatformIO one day in Termux (perhaps with proot-distro) and we can burn a program into microcontrollers!

green-green-avk implemented the same thing as @Grimler91 did. Libusb simply interacts with some socket owned by Android Activity.

twaik commented 3 months ago

Probably it will be a good idea to move some part of code to termux-app. Effective polling for usb events is not possible without service running in background. termux-api's BroadcastReceiver can not be used for this since it is killed after some period of time.