Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing libusb_init example #1928

Closed
umarcor opened this issue Feb 11, 2021 · 12 comments
Closed

Failing libusb_init example #1928

umarcor opened this issue Feb 11, 2021 · 12 comments

Comments

@umarcor
Copy link

umarcor commented Feb 11, 2021

Problem description

I'm trying to follow https://wiki.termux.com/wiki/Termux-usb. Installing Termux:API and the packages worked. termux-usb -l works too. However, the usbtest.c example fails.

Steps to reproduce

I wrote a minimal version named usbinit.c:

#include <stdio.h>
#include <assert.h>
#include <libusb-1.0/libusb.h>

int main(int argc, char **argv) {
  libusb_context *context;
  assert(!libusb_init(&context));
  libusb_exit(context);
}
termux-usb -l
gcc usbinit.c -lusb-1.0 -o usbinit
termux-usb -r /dev/bus/usb/001/003
termux-usb -e ./usbinit /dev/bus/usb/001/003

Output:

~: termux-usb -r /dev/bus/usb/001/003
Access granted.
~: termux-usb -e ./usbinit /dev/bus/usb/001/003
usbinit.c:7: main: assertion "!libusb_init(&context)" failed
/data/data/com.termux/files/usr/libexec/termux-callback: line 3: 3112 Aborted   $TERMUX_CALLBACK "$0"

Expected behavior

usbinit should execute without failure.

Additional information

  • Termux application version: 0.101 (Termux:API 0.44)
  • Android OS version: 8.1.0
  • Device model: Redmi 5 Plus
@Grimler91
Copy link
Member

Grimler91 commented Feb 15, 2021

Downgrading libusb to 1.0.23 does not fix it. Not sure what else we might have changed that could be the problem.

@ghost
Copy link

ghost commented Mar 5, 2021

Same bug on Android 10, (oukitel wp5)
Termux 0.101
Termux:API 0.44

@ghost
Copy link

ghost commented Mar 7, 2021

Ok, i found the issue!

We need to add a call to:
(note: i initialised 'context' to NULL first, since it is passed to this call, before init: 'libusb_context *context = NULL;')

libusb_set_option(context, LIBUSB_OPTION_WEAK_AUTHORITY);

just BEFORE the libusb_init() one.
Please test it, and if all ok, someone could update the wiki example code for termux-usb! Thanks.

@ghost
Copy link

ghost commented Mar 7, 2021

Hello, though i solved the issue regarding the wiki libusb example, it does displays me the device information name, manufacturer...but i cannot get to read data from my device(which is a MIDI device: Akai midimix - works fine on Android),
i think on Android we don't have to claim the device right? cause it returns me 'busy' when i try,
i might need to call libusb_control_transfer() prior to trying to read anything... but i don't find much info, until now i can read nothing from the device either synchronous/asynchronous with bulk_transfer or interrupt_transfer, any help greatly appreciated, if someone has a working example to read some data from a USB device...not much examples online and just documentation is not enough for me to get anything atm, thanks!

[edit]
i'm trying to hack out libusb examples, especially the HID test one, i can get a fully detailled description of my device, so i see for instance it has 3 interfaces and multiples endpoints so maybe 1 ITF for reading, 1 for the LEDs?...i'm not sure. But i still can't read user input data from it...I'll try to get a clean version for the detailled device infos, would be usefull for any device.

@Grimler91
Copy link
Member

@ChewbakaJones what code are you trying to run? Have you modified it so that it uses libusb_wrap_sys_device + libusb_get_device instead of libusb_open?

I have an example modification for a sports watch here.

Will give your libusb_set_option fix a try later.

@ghost
Copy link

ghost commented Mar 7, 2021

Here is a modified version of the tool provided by libusb adapted for Termux, usefull to dump infos and debug USB devices,
you may want to try it on your PS3 or XBOX controller if you have some...i don't have that to try here.

termux-usb-test-infos.c :
https://gist.github.com/ChewbakaJones/e54fd750b6a0dec5603e565c4adc8813

Example usage (build it, then usage):


$ gcc -o termux-usb-test-infos termux-usb-test-infos.c -lusb-1.0


$ termux-usb -l
[
"/dev/bus/usb/001/006"
]

$ termux-usb -r /dev/bus/usb/001/006
Access granted.

$ termux-usb -e ./termux-usb-test-infos /dev/bus/usb/001/006

OR for debug infos:

$ termux-usb -e "./termux-usb-test-infos -d " /dev/bus/usb/001/006


@Grimler91 i started with the termux-usb wiki code snippet but now i was looking at the libusb samples...i still can't read my MIDI device's input, but i got much more infos about it already, you may wanna try this code on some other USB devices, HID or pads or storage to see..the smart watch code won't help me much, but you got able to interact with that device on Termux already?

@Grimler91
Copy link
Member

i still can't read my MIDI device's input

What input? What is it suppose to give you? termux-usb just allows existing libusb application to work (after some modifications) on android without root, you cannot really use termux-usb to reverse-engineer (or create from scratch) the communication with any usb device

the smart watch code won't help me much

No, that code just shows an example of the modification needed to make it possible for existing libusb-based programs to work via the android API without root.

but you got able to interact with that device on Termux already?

The application I forked, ttwatch, works on android with termux-usb after those modifications, yes.

@ghost
Copy link

ghost commented Mar 7, 2021

I just want to read the raw midi events with libusb, like when i'm turning a knob on the device it must send a packet with 1 byte that is the value of the knob, just like for a gamepad. Nice to hear you got the smartwatch working, i guess if this works my device is much more simple...

@umarcor
Copy link
Author

umarcor commented Apr 1, 2021

I tried mofidying the minimal snippet above according to @ChewbakaJones' explanation:

#include <stdio.h>
#include <assert.h>
#include <libusb-1.0/libusb.h>

int main(int argc, char **argv) {
  libusb_context *context = NULL;
  libusb_set_option(context, LIBUSB_OPTION_WEAK_AUTHORITY);
  assert(!libusb_init(&context));
  libusb_exit(context);
}

I'm still getting an error:

~: termux-usb -e ./usbinit /dev/bus/usb/001/003
/home/builder/termux-packages/.termux-builder/libusb/src/libusb/os/linux_usbfs.c:419: void op_exit(struct libusb_context *): assertion "init_count != 0" failed
/data/data/com.termux/files/usr/libexec/termux-callback: line 3: 16012 Aborted   $TERMUX_CALLBACK "$0"
  • Termux application version: 0.108 (Termux:API 0.47)
  • Android OS version: 8.1.0
  • Device model: Redmi 5 Plus

@meltdown03
Copy link

meltdown03 commented May 29, 2021

I tried mofidying the minimal snippet above according to @ChewbakaJones' explanation:

#include <stdio.h>
#include <assert.h>
#include <libusb-1.0/libusb.h>

int main(int argc, char **argv) {
  libusb_context *context = NULL;
  libusb_set_option(context, LIBUSB_OPTION_WEAK_AUTHORITY);
  assert(!libusb_init(&context));
  libusb_exit(context);
}

I'm still getting an error:

~: termux-usb -e ./usbinit /dev/bus/usb/001/003
/home/builder/termux-packages/.termux-builder/libusb/src/libusb/os/linux_usbfs.c:419: void op_exit(struct libusb_context *): assertion "init_count != 0" failed
/data/data/com.termux/files/usr/libexec/termux-callback: line 3: 16012 Aborted   $TERMUX_CALLBACK "$0"
  • Termux application version: 0.108 (Termux:API 0.47)
  • Android OS version: 8.1.0
  • Device model: Redmi 5 Plus

This is an error with libusb, see: libusb/libusb@e78cafb

It should be fixed in the next version of libusb. It should only affect the exiting of libusb. After adding the line:
libusb_set_option(NULL, LIBUSB_OPTION_WEAK_AUTHORITY) to the usbtest.c example, it works, then shows the error at exit.

Your source code isn't doing anything so you just see the error.

@meltdown03
Copy link

I verified it works without error by building libusb from the GitHub source in termux. If you want a step-by-step on how to build it, let me know. It's just a few lines in the terminal.

@agnostic-apollo
Copy link
Member

Closing since its fixed and not a termux-app issue. Someone should update the wiki though.

Grimler91 added a commit to termux/termux-packages that referenced this issue May 30, 2021
Martinvlba pushed a commit to NeoTerrm/neoterm-packages that referenced this issue Jun 13, 2021
amuramatsu pushed a commit to amuramatsu/termux-packages that referenced this issue Jun 26, 2021
@ghost ghost locked and limited conversation to collaborators Oct 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants