termux / termux-packages

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

Termux USB OTG support #1145

Closed efpage closed 5 years ago

efpage commented 7 years ago

We are trying to connect an Arduino via USB host to termux to read out serial data without success. Serial input is working to any Android Serial Monitor but cannot be used on termux / node red.

It seems that the Android Open Accessory Protocol 2.0 is not (or not fully) supportet ?!? Or maybe there is another reason?

While there are no other physical connections to android devices, USB support is very important, specially with node red. Any support is very welcome!

BR Eckehard

Grimler91 commented 7 years ago

I've managed to read from an arduino but it required root to change the ownership of /dev/ACM0. I'm not sure what would be needed to make it work without root, maybe someone else has more insight.

There has been some previous related discussion here: https://github.com/termux/termux-packages/issues/220

yerpj commented 6 years ago

Any solution for usb-serial on Android since then?

ghost commented 6 years ago

@yerpj Root is needed for usb serial.

532910 commented 5 years ago

I have root! LineageOS 15.1. But when I connect any usb-to-serial adapter (FTDI, Prolific) I got no device in /dev (ACM USB).

but de.kai_morich.serial_bluetooth_terminal (for exmaple) works

ghost commented 5 years ago

I got no device in /dev (ACM USB).

Why it should be ? Your kernel has a driver for such devices ?

but de.kai_morich.serial_bluetooth_terminal (for exmaple) works

Likely userspace driver implementation. Android usb API allows that. But device /dev/* won't appear anyway.

532910 commented 5 years ago

Your kernel has a driver for such devices? I don't know, looks like it hasn't.

Really I mean that "root is insufficient for usb serial", but I agree that it's required.

532910 commented 5 years ago

Anyway, cat termux implement usb-serial drivers in userspace?

ghost commented 5 years ago

cat termux implement usb-serial drivers in userspace?

That's possible maybe, I even seen implementation somewhere on Github. But such stuff should go to Termux:API and won't be same as interacting with /dev/tty{USB,ACM} devices.

marcodutto commented 5 years ago

Hi! As far as I understand after some research on google, on android you can get access to USB OTG devices by adding to Manifest file (located in App > src > main > AndroidManifest.xml) the following line:

Anyone knows if this may grant permissions in accessing OTG devices including serial ports to Termux environment?

ghost commented 5 years ago

Anyone knows if this may grant permissions in accessing OTG devices including serial ports to Termux environment?

No. It won't grant filesystem-level permission to /dev/*. Most Android's kernels don't have enabled drivers for serial line.

Driver can be implemented at API level. Example: kai-morich/usb-serial-for-android.

freemandealer commented 5 years ago

Quite interested in this topic. I checked the tutorial about connecting arduino and Apps through OTG using java library. However, It could be even cooler if termux can talk directly to arduino.

bubnikv commented 5 years ago

Let's consider USB OTG access on an unrooted Android. There seems to be an agreement that there are no serial over USB kernel drivers available on a stock Android. There are the following ways to access serial over USB:

1) Using the USB API from Java, implementing the serial over USB drivers in Java in user space. We can then pipe the serial streams from / to a native application using Unix sockets, for example. https://github.com/kai-morich/usb-serial-for-android

2) libusb based native user space implementation of serial over USB drivers. Such a solution requires one to open the USB device from Java first and to pass a file descriptor to the native libusb. I have tested the solution below and it works. It seems like a similar approach has recently been adopted to the mainline libusb. https://github.com/martinmarinov/rtl_tcp_andro-/blob/master/libusb/src/main/jni/README

Here is a native libusb based serial over USB library which we may adopt. https://github.com/hutorny/usbuart

For termux, I would like the 2nd solution. Ideally we would have a generic libusb support in Termux or Termux:API, which would help a native application to enumerate and open a USB device, negotiate access and pass an open file descriptor back to the native application. This would be the most performant solution, as the native application would talk directly to the kernel USB driver.

It seems like there is a way to pass an open file descriptor from Termux::API to the native application using Binder. Also in Linux it is possible to pass open file descriptors using Unix sockets, but I am not sure whether that works on a stock Android linux kernel. https://mattias.niklewski.com/2014/03/binder.html and the following example documents the usage of Binder API from the native code: https://github.com/mcr/Android-HelloWorldService

Termux has a potential to become an "Arduino" for Android development, if we can get access to at least USB serial ports, better to bluetooth serial and all USB devices through libusb. It would be great to be able to sketch an application on Linux and then to transfer the application to Termux with minimum changes (compile time switch to open the serial port differently). This way one would be able to prototype a solution and maybe deploy it to a handful of customers before diving into Android development.

Also a Sixel support would help https://github.com/termux/termux-app/issues/142 :-)

podarok commented 5 years ago

What if https://github.com/mik3y/usb-serial-for-android + https://github.com/hunterli/remserial The idea is to create sock file that will behave like a serial port. Localhost loop

bubnikv commented 5 years ago

I would warn against the first project - the Java implementation of the USB OTG drivers. I believe this is what https://play.google.com/store/apps/details?id=ro.yo3ggx.btvna&hl=en uses and it is about 5x slower than an equivalent bluetooth link (with native bluetooth drivers of Android).

On Fri, Aug 9, 2019 at 12:46 PM Andrii Podanenko notifications@github.com wrote:

What if https://github.com/mik3y/usb-serial-for-android + https://github.com/hunterli/remserial The idea is to create sock file that will behave like a serial port. Localhost loop

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/termux/termux-packages/issues/1145?email_source=notifications&email_token=ABMPSI7GDENM46MKJXWBVBTQDVDHLA5CNFSM4DTHMKHKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD36KHTI#issuecomment-519873485, or mute the thread https://github.com/notifications/unsubscribe-auth/ABMPSI3GNC4ZFORIN6R6JHDQDVDHLANCNFSM4DTHMKHA .

green-green-avk commented 5 years ago

My 5 cents... Why just not to add one more console utility which will use this one: https://github.com/felHR85/UsbSerial ? It's pretty complete and I saw no visible performance issues. See implementation of this approach here: https://play.google.com/store/apps/details?id=green_green_avk.anothertermlite https://github.com/green-green-avk/AnotherTermLite

podarok commented 5 years ago

once access to USB ports on non rooted devices is resolved we'll open whole bunch of home automation stuff like zigbee2mqtt nodejs app, openhub, etc

ghost commented 5 years ago

I am trying PlatformIO on termux right now which runs fine and offers a big palette of sdks and frameworks, arduino, stm32...getting a black magic probe on usb to work would be a dream come true

ghost commented 5 years ago

Basic usb support is implemented via API & specially patched libusb - https://github.com/termux/termux-api/pull/294. Related page on wiki - https://wiki.termux.com/wiki/Termux-usb.

shubhampaul commented 5 years ago

@StephanBeer Are you able to upload the code to arduino using the Termux-usb api and platformio?

bubnikv commented 5 years ago

@shubhampaul Android usually does not come with USB over serial kernel drivers, you have to implement the USB serial protocol in user space. You need to extend the work by @xeffyr with something like https://github.com/hutorny/usbuart

uDude commented 4 years ago

Have a look at "Serial USB Terminal" in the play store (and the referenced code.) I use a serial term on a d1minir2 running my lua code. This d1mini uses a ch340 uart chip. It works fabuously. I need to give the dev a few $$. It is not an ide and does not support adding custom behaviours AFAIK.

nival999 commented 4 years ago

I'm not sure this issue should remain closed. The real issue (outside the title) is to use an Arduino with Termux. This would be huge!!!! Esp for something like octoprint running inside termux. Can someone get it going?

ghost commented 4 years ago

@nival999 USB functionality is already implemented. Not in way suitable for most people, but we can't offer something else at this time. https://wiki.termux.com/wiki/Termux-usb

If you have any suggestions how termux-usb can be enhanced, do that in a new issue.

andreibobirica commented 4 years ago

I would like to use termux with ubuntu to use octoprint with serial connection But I couldn't find anywhere how to do it.

bubnikv commented 4 years ago

Basic usb support is implemented via API & specially patched libusb - termux/termux-api#294. Related page on wiki - https://wiki.termux.com/wiki/Termux-usb.

This is great, thanks a lot. I sketched up a server for streaming audio and commands to/from a HAM radio SDR transceiver. It works nicely.

However, the termux-usb tool is a bit inconvenient. It would be great, if the termux-usb tool optionally accepted some form of USB device identification, for example VID/PID pair and/or the readable device description.

Also I am having issues with TermuxAPI in general. For some unknown reason to me, it takes couple of tries and possibly couple of minutes before the termux API command line tools work. For example, the first call to termux-info hangs for a veeery long time and I often lose my patience with termux-usb -l, which hangs on me seemingly indefinitely. Once it starts working, the termux-api tools work reasonably quickly. I think I have https://github.com/termux/termux-packages/issues/3809