question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Termux (Android) support

See original GitHub issue

Hi,

In Termux you only receive a USB file descriptor (number bound to single process). Any operations like usb.core.find(..) won’t work. I wrote a workaround/fix that extends the libusb1 interface and provides the Device object given the file descriptor. See here: https://github.com/Querela/termux-usb-python/blob/e6166ded4555d988365e6609b91dec4f5ebb8fcd/usblib.py#L94

def device_from_fd(fd):
    # setup library
    backend = libusb1.get_backend()
    lib = backend.lib
    ctx = backend.ctx

    # extend c wrapper with android functionality
    lib.libusb_wrap_sys_device.argtypes = [
        libusb1.c_void_p,
        libusb1.c_int,
        libusb1.POINTER(libusb1._libusb_device_handle),
    ]

    lib.libusb_get_device.argtypes = [libusb1.c_void_p]
    lib.libusb_get_device.restype = libusb1._libusb_device_handle

    # get handle from file descriptor
    handle = libusb1._libusb_device_handle()
    libusb1._check(lib.libusb_wrap_sys_device(ctx, fd, libusb1.byref(handle)))

    # get device (id?) from handle
    devid = lib.libusb_get_device(handle)

    # device: devid + handle wrapper
    class DummyDevice:
        def __init__(self, devid, handle):
            self.devid = devid
            self.handle = handle
    dev = DummyDevice(devid, handle)

    # create pyusb device
    device = usb.core.Device(dev, backend)
    device._ctx.handle = dev

    # device.set_configuration()
    return device

I only tested it on the most current Android (10) and Termux version … Usage examples are in my Repo.

Can we implement this in your library?

Enumerating devices would maybe work with subprocesses but working with a specific device is only possible in a process(?) callback and does not seem feasable. I guess …?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:26 (20 by maintainers)

github_iconTop GitHub Comments

2reactions
normanrcommented, Jul 3, 2021

termux_device_info.py accepts a file descriptor, not a path. When it’s run via termux-usb the -e option will execute the specified command with a file descriptor referring to the device as its argument. You can not see the files because selinux does not allow it, only the java api’s have access to the usb devices (and can be used open and return a file descriptor for them).

So if you want to test without termux-usb (like on a non-android device), you need to do something like: ./termux_device_info.py 9 9</dev/bus/usb/005/007 (i.e. use a shell redirect to open the usb device and pass the fd number to the process). You can see the effect by running termux-usb -e echo /dev/bus/usb/005/007 and seeing that it prints out a file descriptor number.

The latest libusb does not have the right-stuff in place yet (as far as I know) to support opening usb devices on non-rooted devices without having to change the source code of the binary that you’re using. #287 has the same problem (the source code of the binary needs to be changed to explicitly support reading the fd from the command line and opening it and giving it to libusb).

libusb/libusb#874 doesn’t get rid of the helper it just proposes to add some of the java glue required to get libusb working with non-rooted devices into libusb itself. It still requires the program that’s opening the usb device to link to java and call the right init functions to make everything work. That only works for programs you have the source code to, and you’re willing to change to make it work. The nice thing about AnotherTerm’s helper is that it “just-works” for existing binaries.

It could be nice to expose the wrap_sys_device API in pyusb, but it does require code changes to make use of it. I have some pending changes in a working tree, mostly the suggestions I made on #287, if you want I can look at committing and pushing them somewhere (not as a full PR, but so that they’re not 100% lost).

1reaction
normanrcommented, Aug 30, 2020

Also note that if https://github.com/termux/termux-api/issues/349 is fixed then no special handling for termux will be required in pyusb as the termux build of libusb will handle everything for you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FAQ - Termux Wiki
Will Termux work on Android 11 ... In short: yes, it works on Android 11 and 12 beta. Android security improvements in new...
Read more >
Termux on android 5 or 6 - GitHub
Support for android 5 and 6 was re-added to termux-app in beginning of 2022, but no support or package updates are planned.
Read more >
Termux | The main termux site and help pages.
Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required. A minimal base system...
Read more >
Termux - Free and Open Source Android App Repository
Termux combines powerful terminal emulation with an extensive Linux package collection. * Enjoy the bash and zsh shells. * Edit files with nano...
Read more >
Here's why the Termux app is no longer getting updates on ...
Termux and F-Droid. As many of you may well be aware, the Android operating system is powered by the Linux kernel underneath.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found