Skip to content

Support unrooted android and rooted android - #760

Closed
chrisju wants to merge 4 commits into
libusb:masterfrom
chrisju:master
Closed

chrisju wants to merge 4 commits into
libusb:masterfrom
chrisju:master

Conversation

@chrisju

@chrisju chrisju commented Jul 29, 2020 •

Copy link
Copy Markdown

On unrooted Android, we can not operate usb directly and need use libusb_wrap_sys_device, we also will failed on linux_scan_devices. For 1.0.23, before commit 211d791, libusb can not work with rooted android. After commit 211d791, libusb can not work with unrooted android. Libusb can not know if it has authority to scan devices, so i add libusb_init_ex to specify it manually.

@Peter-St

Peter-St commented Aug 2, 2020

Copy link
Copy Markdown
Contributor

Hello chrisju,

I was able to sucessfuly test your commit on unrooted Android 8. Thanks for publishing.

@chrisju

chrisju commented Aug 5, 2020

Copy link
Copy Markdown
Author

@mcuee Can you add the bug label to this MR.? Current master not work in android apk, and libusb_wrap_sys_device is useless actually.

@tmm1

tmm1 commented Aug 6, 2020

Copy link
Copy Markdown

Instead of adding a new public initializer (libusb_init_ex), would it be possible to detect the failure and ignore it when building for android?

@chrisju

chrisju commented Aug 7, 2020

Copy link
Copy Markdown
Author

Instead of adding a new public initializer (libusb_init_ex), would it be possible to detect the failure and ignore it when building for android?

If so, when developing other than android apk, you will not get the error correctly.

@dickens

dickens commented Aug 10, 2020

Copy link
Copy Markdown
Member

Please, no. There doesn't need to be a new function for this. Something like libusb_set_option() would suffice.

@mcuee

mcuee commented Aug 13, 2020

Copy link
Copy Markdown
Member

Good point. We should use libusb_set_option() if possible, I remember that was introduced for usbdk.

@mcuee

mcuee commented Aug 13, 2020

Copy link
Copy Markdown
Member

@chrisju Would you please rework the patch based on the suggestion from Chris?

@chrisju

chrisju commented Aug 14, 2020 •

Copy link
Copy Markdown
Author

Please, no. There doesn't need to be a new function for this. Something like libusb_set_option() would suffice.

libusb_set_option is after libusb_init, but linux_scan_devices is in libusb_init. So libusb_set_option can not work. The authority option must set before libusb_init.

@hartcw

hartcw commented Aug 14, 2020

Copy link
Copy Markdown
Contributor

I also have a fork of libusb that supports unrooted android.
The fork is here: https://github.com/kuvacode/libusb

My approach was to add a new API called libusb_import_android_fd. This allows an android app to get a valid device FD from the java android.hardware.usb.UsbManager API, and pass this into libusb. The next call to libusb_get_device_list() will then return a valid libusb_device that wraps the FD.

@chrisju

chrisju commented Aug 14, 2020 •

Copy link
Copy Markdown
Author

I also have a fork of libusb that supports unrooted android.
The fork is here: https://github.com/kuvacode/libusb

My approach was to add a new API called libusb_import_android_fd. This allows an android app to get a valid device FD from the java android.hardware.usb.UsbManager API, and pass this into libusb. The next call to libusb_get_device_list() will then return a valid libusb_device that wraps the FD.

If you want to use as a general library, you need to consider rooted use on Android.

@dickens

dickens commented Aug 14, 2020

Copy link
Copy Markdown
Member

libusb_set_option is after libusb_init, but linux_scan_devices is in libusb_init. So libusb_set_option can not work. The authority option must set before libusb_init.

There is nothing requiring libusb_set_option to be called after libusb_init. You simply pass NULL as the context pointer. Since there is no default context at that point, the backend will receive NULL as the context pointer as well. This is actually a good way to set global options that would apply to all contexts.

@chrisju

chrisju commented Aug 19, 2020 •

Copy link
Copy Markdown
Author

@dickens @mcuee
I have updated the pull-request. Now i add an option LIBUSB_OPTION_WEAK_AUTHORITY to work with Android apk. Must call libusb_set_option(NULL, LIBUSB_OPTION_WEAK_AUTHORITY) before call libusb_init if use libusb in Android apk.

@dickens dickens left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the whitespace in your change. You need to use tabs.

Comment thread libusb/core.c Resolved
@dickens

dickens commented Aug 20, 2020

Copy link
Copy Markdown
Member

I also think this should be guarded in linux_usbfs.c with #ifdef __ANDROID__; it doesn’t make sense for standard Linux.

Comment thread libusb/os/linux_usbfs.c Resolved
Comment thread libusb/os/linux_usbfs.c Resolved
Comment thread libusb/os/linux_usbfs.c Resolved
Comment thread libusb/os/linux_usbfs.c Resolved
Comment thread libusb/os/linux_usbfs.c Outdated
UNUSED(ctx);
UNUSED(ap);

switch ((int)option) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a cast here? Is it to avoid a compiler warning, or is it copied from windows_common.c?

@chrisju chrisju Sep 2, 2020 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from windows_common.c to avoid a compiler warning, op_exit in linux_usbfs.c also has that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the warning?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jni/../../libusb/os/linux_usbfs.c:427:49: warning: unused parameter 'ctx' [-Wunused-parameter]
static int op_set_option(struct libusb_context *ctx, enum libusb_option option, va_list ap)
^
jni/../../libusb/os/linux_usbfs.c:427:89: warning: unused parameter 'ap' [-Wunused-parameter]
static int op_set_option(struct libusb_context *ctx, enum libusb_option option, va_list ap)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the question was about the cast in (int)option

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. Casting to an int for the switch is odd.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry i misunderstood. Thank LudovicRousseau. The cast (int)option is just copied from windows_common.c.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the cast in commit 86997c8.

Comment thread libusb/os/linux_usbfs.c Resolved
@Mathew44

Mathew44 commented Sep 6, 2020

Copy link
Copy Markdown

Hello. I am using Android 9.
I would like to run bulk transfer or isochronous transfer using asynchronous approach from NDK.
How we can check in Java code using callback from C plus plus file when data has been received?
Could you give me an example of code, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants