-
Notifications
You must be signed in to change notification settings - Fork 33
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
Android support for enumerating devices via JNI #86
Comments
The You don't really need to know async to use nusb, just wrap anything that returns a Future in |
When I talked about "porting", I was thinking about porting My program currently enumerates device descriptors and interface descriptors successfully, but fails to wrap the file descriptor obtained from the connection with I found some comment on the Web, claiming that Android Java API should be better than wrapping the low level fd in sense of compatability. Maybe I should forget about "better performance", avoid I've found that a few people are making efforts for Code snippet from my program: fn open_permitted_usb_device(&self, java_dev: &JObject) -> Result<DeviceHandle, Error> {
use rusb::UsbContext;
with_jni_env_activity(|env, _| {
let conn = env
.call_method(
&self.internal,
"openDevice",
"(Landroid/hardware/usb/UsbDevice;)Landroid/hardware/usb/UsbDeviceConnection;",
&[java_dev.into()],
)
.and_then(|o| o.l())
.map_err(|_| Error::Access)?;
let mut native_fd =
env.call_method(conn, "getFileDescriptor", "()I", &[])
.and_then(|n| n.i())
.map_err(|_| Error::BadDescriptor)? as std::ffi::c_int;
let mut p_libusb_hdl =
std::mem::MaybeUninit::<*mut libusb1_sys::libusb_device_handle>::uninit();
unsafe {
eprintln!("before check_libusb_error. fd: {:#010x}", native_fd);
check_libusb_error(libusb1_sys::libusb_wrap_sys_device(
self.rusb_context.as_raw(),
&mut native_fd,
p_libusb_hdl.as_mut_ptr(),
))?;
eprintln!("after check_libusb_error.");
let libusb_hdl =
std::ptr::NonNull::new(p_libusb_hdl.assume_init()).ok_or(Error::NoDevice)?;
Ok(rusb::DeviceHandle::from_libusb(
self.rusb_context.clone(),
libusb_hdl,
))
}
})
} ADB logcat:
|
Errno 9 is If JNI can call into Java from Rust rather than the other way around, yeah, it might be nice to implement |
I have published the initial version: https://crates.io/crates/android-usbser. Please help me with the optional feature |
That's using rusb not nusb so is off topic here, and you'd need to be more specific than "doesn’t work on my device" in any case. |
I have got I assume that these functions and structs are required for porting
Thoughts for Android:
Please describe here if anything listed above is wrong. Here is the main problem of the possible porting for Android: Function In the main thread, In a background thread, it must not poll for events directly, and sleeping is possible while it waits for the request result. In the first case (main thread), the main Resume event may be polled, because the In the second case (background thread), a Java helper handling a custom implementation of These complicated issues might be avoided by giving up the idea of putting all required prodecures into This Flutter library can be checked: https://pub.dev/packages/libusb_android_helper. |
You can get started without any changes to nusb. Yes, it could be good to have an Android implementation of |
Here is the prototype: https://docs.rs/android-usbser/0.2.1/android_usbser/usb/index.html. |
Hello. I have spent a lot of time to make permission request handling and hotplug stream available in Limited by the Android SDK API, a few items are missing in the device information structure.
Information of the same device, printed by
|
rusb
Thanks, this looks great. This would be good to integrate eventually, but I'm not going to be able to prioritize it in the near future; need to get v0.2 done at the very least. But if people are looking for how to do this at Android, for now I'll point them at your code. |
a1ien/rusb#213
Inspired by #76, I realized that libusb is a wrapper of OS APIs, and rusb is yet another wrapper around libusb.
If only the authors of rusb and nusb had worked together and made newer versions of rusb on the nusb code base, improvements to the new library would have been much faster, making it much more popular than the current
nusb
.Currently, I'm more familiar with
rusb
instead of asynchronous programming. I'll provide a link to my Android minimal CDC-ACM driver crate (which is unimplemented in Android OS), with the rusb wrapper as its private module. Hopefully someone will do it for nusb, or just guide me to make the port (of course, some parts of the existing linux_usbfs code can be used).The text was updated successfully, but these errors were encountered: