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

The 1st example crashes when permission request is accepted: ActivityManager: freezing 4327 com.example.android_usb_cdc_test #5

Closed
zpg6 opened this issue Jan 2, 2025 · 11 comments

Comments

@zpg6
Copy link

zpg6 commented Jan 2, 2025

Really, really cool repo! But can't seem to get it to actually connect to device because of a SecurityException.

01-01 17:59:21.937 17125 17157 I android_usb_cdc_test: android_usb_cdc_test: 🟢 Polling events...
01-01 17:59:22.940 17125 17157 I android_usb_cdc_test: android_usb_cdc_test: 🟢 Polling events...
01-01 17:59:23.940 17125 17157 I android_usb_cdc_test: android_usb_cdc_test: 🟢 Polling events...
01-01 17:59:24.942 17125 17157 I android_usb_cdc_test: android_usb_cdc_test: 🟢 Polling events...
01-01 17:59:25.947 17125 17157 I android_usb_cdc_test: android_usb_cdc_test: 🟢 Polling events...
01-01 17:59:26.948 17125 17157 I android_usb_cdc_test: android_usb_cdc_test: 🟢 Polling events...
01-01 17:59:27.967 17125 17157 W android_usb_cdc_test: jni_min_helper: Exception in thread "ThreadId(2)" java.lang.SecurityException: User has not given 10262/com.example.android_usb_cdc_test permission to access device /dev/bus/usb/002/002
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test: jni_min_helper: 
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:    0: std::backtrace::Backtrace::create
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:    1: jni_min_helper::print_rust_stack
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:    2: core::ops::function::FnOnce::call_once
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:    3: android_usbser::usb_info::DeviceInfo::build
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:    4: android_usbser::usb_conn::get_extra_device
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:    5: <android_usbser::usb_conn::HotplugWatch as futures_core::stream::Stream>::poll_next
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:    6: futures_lite::future::block_on
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:    7: android_usbser::usb_conn::HotplugWatch::take_next
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:    8: android_main
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:    9: std::sys::backtrace::__rust_begin_short_backtrace
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:   10: core::ops::function::FnOnce::call_once{{vtable.shim}}
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:   11: std::sys::pal::unix::thread::Thread::new::thread_start
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:   12: _ZL15__pthread_startPv
01-01 17:59:28.008 17125 17157 W android_usb_cdc_test:   13: __start_thread
01-01 17:59:28.009 17125 17157 I android_usb_cdc_test: android_usb_cdc_test: 🟢 Device connected (/dev/bus/usb/002/002).
01-01 17:59:28.018 17125 17157 W android_usb_cdc_test: jni_min_helper: Exception in thread "ThreadId(2)" java.lang.SecurityException: User has not given 10262/com.example.android_usb_cdc_test permission to access device /dev/bus/usb/002/002
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test: jni_min_helper: 
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:    0: std::backtrace::Backtrace::create
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:    1: jni_min_helper::print_rust_stack
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:    2: core::ops::function::FnOnce::call_once
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:    3: android_usbser::usb_info::DeviceInfo::build
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:    4: android_usbser::usb_info::list_devices
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:    5: android_usbser::usb_conn::<impl android_usbser::usb_info::DeviceInfo>::request_permission
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:    6: android_main
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:    7: std::sys::backtrace::__rust_begin_short_backtrace
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:    8: core::ops::function::FnOnce::call_once{{vtable.shim}}
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:    9: std::sys::pal::unix::thread::Thread::new::thread_start
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:   10: _ZL15__pthread_startPv
01-01 17:59:28.022 17125 17157 W android_usb_cdc_test:   11: __start_thread
01-01 17:59:28.024 17125 17157 D CompatibilityChangeReporter: Compat change id reported: 160794467; UID 10262; state: ENABLED
01-01 17:59:28.024 17125 17157 D CompatibilityChangeReporter: Compat change id reported: 236704164; UID 10262; state: ENABLED

I am using this as my Cargo.toml to run it

Tired several Android SDK versions between 29-35. I also tried to use your PR branch on cargo-apk library (rust-mobile/cargo-apk#67), and tried with and without value = "true".

[package]
name = "android-usb-cdc-test"
version = "0.1.0"
edition = "2021"
publish = false

[lib]
crate-type = ["lib", "dylib"]

[package.metadata.android.signing.release]
path = "release.keystore"
keystore_password = "<redacted>"

[dependencies]
log = "0.4"
android_logger = "0.14"
android-activity = { version = "0.6", features = ["native-activity"] }
android-usbser = "0.2.1"
serialport = "4.6"

[package.metadata.android]
package = "com.example.android_usb_cdc_test"
build_targets = [ "aarch64-linux-android" ]
resources = "./res"

[package.metadata.android.sdk]
min_sdk_version = 16
target_sdk_version = 30

[[package.metadata.android.uses_feature]]
name = "android.hardware.usb.host"
required = true

[[package.metadata.android.application.activity.intent_filter]]
actions = ["android.hardware.usb.action.USB_DEVICE_ATTACHED"]

# Please check <https://github.com/rust-mobile/cargo-apk/pull/67> if it fails.
# Otherwise comment out the lines below (request for permission purely at runtime).
[[package.metadata.android.application.activity.meta_data]]
name = "android.hardware.usb.action.USB_DEVICE_ATTACHED"
resource = "@xml/device_filter"
value = "true"

[[package.metadata.android.uses_permission]]
name = "android.permission.USB_PERMISSION"
max_sdk_version = 15

[[package.metadata.android.uses_feature]]
name = "android.hardware.usb.host"
required = true
@wuwbobo2021
Copy link
Owner

wuwbobo2021 commented Jan 2, 2025

Thank you for opening the issue.

The exception message itself can't tell anything but an existing problem which occurs even if the permission request is accepted and the device can be opened. Starting from Android API Level 29, UsbDevice.getSerialNumber will fail without permission for the device. With the the current version (v0.2.1) of this crate, logging of the exception (which should be expected) isn't disabled, and you cannot get the serial number from the same Rust DeviceInfo after gaining permission unless you look for the device again in list_devices.

Please post your test program here. Tell me if you have called request_permission() but the dialog isn't shown.

@zpg6
Copy link
Author

zpg6 commented Jan 3, 2025

Okay, now that I deleted another app I had on device that was taking USB_ATTACHED intents it seems to get the permission popup.

  1. Open the app with the device already plugged in. Nothing happens right away.
  2. Unplug, replug the USB device.
  3. Hit Ok on permission. (Allow android-usb-cdc-test to access CP2102 USB to UART : OK / CANCEL)
  4. Crashes ActivityManager: freezing 4327 com.example.android_usb_cdc_test
use android_activity::{AndroidApp, MainEvent, PollEvent};
use android_usbser::usb;
use log::{info, warn};
use std::time::Duration;

#[no_mangle]
fn android_main(app: AndroidApp) {
    android_logger::init_once(
        android_logger::Config::default()
            .with_max_level(log::LevelFilter::Info)
            .with_tag("android_usb_cdc_test"),
    );

    let mut startup_dev = usb::check_attached_intent().ok();
    let mut watcher = usb::watch_devices().unwrap();
    let mut perm_req: Option<usb::PermissionRequest> = None;
    let mut on_destroy = false;
    loop {
        info!("Polling events...");

        app.poll_events(
            Some(Duration::from_secs(1)), // timeout
            |event| match event {
                PollEvent::Main(MainEvent::Start) => {
                    info!("Main Start.");
                }
                PollEvent::Main(MainEvent::Resume { loader: _, .. }) => {
                    info!("Main Resume.");
                }
                PollEvent::Main(MainEvent::Pause) => {
                    info!("Main Pause.");
                }
                PollEvent::Main(MainEvent::Stop) => {
                    info!("Main Stop.");
                }
                PollEvent::Main(MainEvent::Destroy) => {
                    info!("Main Destroy.");
                    on_destroy = true;
                }
                _ => (),
            },
        );
        if on_destroy {
            return;
        }
        let dev_info = if let Some(dev) = startup_dev.take() {
            info!("Got device from startup intent.");
            if !dev.has_permission().unwrap() {
                warn!("Unexpected: no permission.");
                continue;
            }
            dev
        } else if let Some(req) = perm_req.as_ref() {
            if !req.responsed() {
                continue;
            }
            let req = perm_req.take().expect("Unexpected: permission request is None.");
            let dev = req.device_info().clone();
            // `responsed()` returned true, so unwrap here.
            if !req.take_response().expect("Unexpected: response is None.") {
                // boolean response
                info!("Permission denied.");
                continue;
            }
            dev
        } else {
            match watcher.take_next() {
                None => continue,
                Some(usb::HotplugEvent::Connected(dev)) => {
                    info!("🟢 Device connected ({}).", dev.path_name());
                    match dev.has_permission() {
                        Ok(true) => {
                            info!("Permission granted! Device is ready to use.");
                            info!("{:?}", dev);
                            dev
                        },
                        Ok(false) => {
                            perm_req = dev.request_permission().expect("Unexpected: failed to request permission.");
                            if perm_req.is_some() {
                                info!("Performing permission request...");
                            }
                            continue;
                        }
                        Err(e) => {
                            warn!("Unexpected: failed to check permission: {:?}", e);
                            continue;
                        }
                    }
                }
                Some(usb::HotplugEvent::Disconnected(dev)) => {
                    info!("🔴 Device disconnected ({}).", dev.path_name());
                    continue;
                }
            }
        };

        let Ok(conn) = dev_info.open_device() else {
            warn!("Unexpected: failed to open the device.");
            continue;
        };
        info!("Opened, printing USB configurations:");
        for conf in conn.configurations() {
            info!("{:#?}", conf);
        }
        std::thread::sleep(Duration::from_secs(1));
        info!("Closing the device.");
    }
}
01-03 00:38:06.361  1230  1598 I ActivityManager: Start proc 4327:com.example.android_usb_cdc_test/u10a157 for next-top-activity {com.example.android_usb_cdc_test/android.app.NativeActivity}
01-03 00:38:06.367  4327  4327 I id_usb_cdc_test: Using CollectorTypeCMC GC.
01-03 00:38:06.370  4327  4327 E id_usb_cdc_test: Not starting debugger since process cannot load the jdwp agent.
01-03 00:38:06.381  4327  4327 D nativeloader: Configuring clns-6 for other apk . target_sdk_version=30, uses_libraries=, library_path=/data/app/~~csPBoepeHCoZksmlNSP6-Q==/com.example.android_usb_cdc_test-3zvJafBdGGgUcvJFcOaFLQ==/lib/arm64:/data/app/~~csPBoepeHCoZksmlNSP6-Q==/com.example.android_usb_cdc_test-3zvJafBdGGgUcvJFcOaFLQ==/base.apk!/lib/arm64-v8a, permitted_path=/data:/mnt/expand:/data/user/10/com.example.android_usb_cdc_test
01-03 00:38:06.391  4327  4342 D vulkan  : searching for layers in '/data/app/~~csPBoepeHCoZksmlNSP6-Q==/com.example.android_usb_cdc_test-3zvJafBdGGgUcvJFcOaFLQ==/lib/arm64'
01-03 00:38:06.391  4327  4342 D vulkan  : searching for layers in '/data/app/~~csPBoepeHCoZksmlNSP6-Q==/com.example.android_usb_cdc_test-3zvJafBdGGgUcvJFcOaFLQ==/base.apk!/lib/arm64-v8a'
01-03 00:38:06.422  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:06.422  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Main Start.
01-03 00:38:06.422  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:06.423  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Main Resume.
01-03 00:38:06.423  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:06.426  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:06.434  1230  2365 D CoreBackPreview: Window{25dc08d u10 com.example.android_usb_cdc_test/android.app.NativeActivity}: Setting back callback OnBackInvokedCallbackInfo{mCallback=android.window.IOnBackInvokedCallback$Stub$Proxy@18dd790, mPriority=0, mIsAnimationCallback=false}
01-03 00:38:06.439  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:06.439  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:06.439  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:06.440  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:06.442  1230  1577 W ziparchive: Unable to open '/data/app/~~csPBoepeHCoZksmlNSP6-Q==/com.example.android_usb_cdc_test-3zvJafBdGGgUcvJFcOaFLQ==/base.dm': No such file or directory
01-03 00:38:06.443  1230  1577 I ActivityTaskManager: Displayed com.example.android_usb_cdc_test/android.app.NativeActivity for user 10: +116ms
01-03 00:38:06.469  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:06.471  1230  2404 I ImeTracker: com.example.android_usb_cdc_test:6df003e7: onRequestHide at ORIGIN_SERVER_HIDE_INPUT reason HIDE_UNSPECIFIED_WINDOW
01-03 00:38:06.471  1230  2404 I ImeTracker: com.example.android_usb_cdc_test:6df003e7: onCancelled at PHASE_SERVER_SHOULD_HIDE
01-03 00:38:06.822  2262  2393 D VRI[android_usb_cdc_test]: Not drawing due to not visible
01-03 00:38:06.823  1230  2365 D CoreBackPreview: Window{c8ee7ad u10 Splash Screen com.example.android_usb_cdc_test EXITING}: Setting back callback null
01-03 00:38:06.826  1230  2404 W InputManager-JNI: Input channel object 'c8ee7ad Splash Screen com.example.android_usb_cdc_test (client)' was disposed without first being removed with the input manager!
01-03 00:38:07.471  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:08.484  4327  4357 W android_usb_cdc_test: jni_min_helper: Exception in thread "ThreadId(2)" java.lang.SecurityException: User has not given 1010157/com.example.android_usb_cdc_test permission to access device /dev/bus/usb/002/002
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test: jni_min_helper: 
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:    0: std::backtrace::Backtrace::create
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:    1: jni_min_helper::print_rust_stack
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:    2: core::ops::function::FnOnce::call_once
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:    3: android_usbser::usb_info::DeviceInfo::build
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:    4: android_usbser::usb_conn::get_extra_device
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:    5: <android_usbser::usb_conn::HotplugWatch as futures_core::stream::Stream>::poll_next
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:    6: futures_lite::future::block_on
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:    7: android_usbser::usb_conn::HotplugWatch::take_next
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:    8: android_main
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:    9: std::sys::backtrace::__rust_begin_short_backtrace
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:   10: core::ops::function::FnOnce::call_once{{vtable.shim}}
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:   11: std::sys::pal::unix::thread::Thread::new::thread_start
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:   12: _ZL15__pthread_startPv
01-03 00:38:08.530  4327  4357 W android_usb_cdc_test:   13: __start_thread
01-03 00:38:08.531  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: 🔴 Device disconnected (/dev/bus/usb/002/002).
01-03 00:38:08.531  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:09.532  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:10.534  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:11.132  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Main Pause.
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test: jni_min_helper: Exception in thread "ThreadId(2)" java.lang.SecurityException: User has not given 1010157/com.example.android_usb_cdc_test permission to access device /dev/bus/usb/002/002
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test: jni_min_helper: 
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:    0: std::backtrace::Backtrace::create
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:    1: jni_min_helper::print_rust_stack
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:    2: core::ops::function::FnOnce::call_once
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:    3: android_usbser::usb_info::DeviceInfo::build
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:    4: android_usbser::usb_conn::get_extra_device
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:    5: <android_usbser::usb_conn::HotplugWatch as futures_core::stream::Stream>::poll_next
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:    6: futures_lite::future::block_on
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:    7: android_usbser::usb_conn::HotplugWatch::take_next
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:    8: android_main
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:    9: std::sys::backtrace::__rust_begin_short_backtrace
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:   10: core::ops::function::FnOnce::call_once{{vtable.shim}}
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:   11: std::sys::pal::unix::thread::Thread::new::thread_start
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:   12: _ZL15__pthread_startPv
01-03 00:38:11.133  4327  4357 W android_usb_cdc_test:   13: __start_thread
01-03 00:38:11.133  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: 🟢 Device connected (/dev/bus/usb/002/002).
01-03 00:38:11.137  4327  4357 W android_usb_cdc_test: jni_min_helper: Exception in thread "ThreadId(2)" java.lang.SecurityException: User has not given 1010157/com.example.android_usb_cdc_test permission to access device /dev/bus/usb/002/002
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test: jni_min_helper: 
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:    0: std::backtrace::Backtrace::create
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:    1: jni_min_helper::print_rust_stack
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:    2: core::ops::function::FnOnce::call_once
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:    3: android_usbser::usb_info::DeviceInfo::build
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:    4: android_usbser::usb_info::list_devices
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:    5: android_usbser::usb_conn::<impl android_usbser::usb_info::DeviceInfo>::request_permission
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:    6: android_main
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:    7: std::sys::backtrace::__rust_begin_short_backtrace
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:    8: core::ops::function::FnOnce::call_once{{vtable.shim}}
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:    9: std::sys::pal::unix::thread::Thread::new::thread_start
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:   10: _ZL15__pthread_startPv
01-03 00:38:11.138  4327  4357 W android_usb_cdc_test:   11: __start_thread
01-03 00:38:11.138  4327  4357 W PendingIntent: android.app.StackTrace: New mutable implicit PendingIntent: pkg=com.example.android_usb_cdc_test, action=rust.android_usbser.USB_PERMISSION, featureId=null. This will be blocked once the app targets U+ for security reasons.
01-03 00:38:11.148  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Performing permission request...
01-03 00:38:11.148  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:11.163  4327  4357 I android_usb_cdc_test: android_usb_cdc_test: Polling events...
01-03 00:38:12.185  4327  4327 E android_usb_cdc_test: android_activity::activity_impl::glue: Ignoring spurious JVM callback after last activity reference was dropped!
01-03 00:38:12.185  4327  4327 E android_usb_cdc_test: android_activity::activity_impl::glue: Ignoring spurious JVM callback after last activity reference was dropped!
01-03 00:38:12.187  4327  4327 E android_usb_cdc_test: android_activity::activity_impl::glue: Ignoring spurious JVM callback after last activity reference was dropped!
01-03 00:38:12.187  4327  4327 E android_usb_cdc_test: android_activity::activity_impl::glue: Ignoring spurious JVM callback after last activity reference was dropped!
01-03 00:38:12.188  1230  1769 D CoreBackPreview: Window{25dc08d u10 com.example.android_usb_cdc_test/android.app.NativeActivity}: Setting back callback null
01-03 00:38:12.191  1230  1769 W InputManager-JNI: Input channel object '25dc08d com.example.android_usb_cdc_test/android.app.NativeActivity (client)' was disposed without first being removed with the input manager!
01-03 00:38:22.185  1230  1817 D ActivityManager: freezing 4327 com.example.android_usb_cdc_test

@zpg6
Copy link
Author

zpg6 commented Jan 3, 2025

Ok maybe its the build I'm using... if you're curious its for using Pixel Tablet as an Android Automotive dev device. Here is a full logcat (only slightly redacted non-relevant items) for a few seconds leading up (when I plug in and hit Ok to permission):

The app I mentioned taking precedence is Jabara Terminal - which must be a clone of https://github.com/kai-morich/SimpleUsbTerminal as you can see from Activity not found: ComponentInfo{de.kai_morich.simple_usb_terminal/de.kai_morich.simple_usb_terminal.MainActivity}. Its not something you can find in the AOSP tree, so I presume its proprietary google prebuilt that gets added last step? Even with it deleted (adb root shell remount fs then rm /vendor/.../JabaraTerminal.apk) it seems that it's taking the permission acceptance over? Not sure how to read into this...

01-03 00:38:20.396  1230  2365 I UsbPortManager: AIDL UsbDataStatus:1
01-03 00:38:20.396  1230  2365 I UsbPortManager: ClientCallback AIDL V1: port0
01-03 00:38:20.399   898  1934 I android.hardware.usb.aidl-service: port0
01-03 00:38:20.399   898  1934 I android.hardware.usb.aidl-service: 0:port0 connected:0 canChangeMode:1 canChagedata:0 canChangePower:0 usbDataEnabled:1
01-03 00:38:20.400   898  1934 I android.hardware.usb.aidl-service: ContaminantDetectionStatus:2 ContaminantProtectionStatus:0
01-03 00:38:20.400   898  1934 I android.hardware.usb.aidl-service: powerTransferLimited:0
01-03 00:38:20.435   872   872 W healthd : battery l=77 v=4106 t=24.3 h=2 st=3 c=-279062 fc=7108000 cc=4 chg=
01-03 00:38:20.405   898   931 I android.hardware.usb.aidl-service.UsbDataSessionMonitor: epoll unregistered /sys/bus/usb/devices/usb2/2-0:1.0/usb2-port1/state
01-03 00:38:20.407  1230  2365 I UsbPortManager: AIDL UsbDataStatus:1
01-03 00:38:20.407  1230  2365 I UsbPortManager: ClientCallback AIDL V1: port0
01-03 00:38:20.451   872   872 W healthd : battery l=77 v=4106 t=24.3 h=2 st=3 c=-279062 fc=7108000 cc=4 chg=
01-03 00:38:20.466   872   872 W healthd : battery l=77 v=4106 t=24.3 h=2 st=3 c=-331562 fc=7108000 cc=4 chg=
01-03 00:38:20.481   872   872 W healthd : battery l=77 v=4106 t=24.3 h=2 st=3 c=-331562 fc=7108000 cc=4 chg=
01-03 00:38:20.498   872   872 W healthd : battery l=77 v=4106 t=24.3 h=2 st=3 c=-331562 fc=7108000 cc=4 chg=
01-03 00:38:20.579  3265  3284 W droid.car.media: ApkAssets: Deleting an ApkAssets object '<empty> and /system/priv-app/CarRadioApp/CarRadioApp.apk' with 1 weak references
01-03 00:38:21.064  2900  2900 D UsbDeviceHandlerResolver: dispatch: UsbDevice[mName=/dev/bus/usb/002/002,mVendorId=4292,mProductId=60000,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=Silicon Labs,mProductName=CP2102 USB to UART Bridge Controller,mVersion=1.00,mSerialNumberReader=android.hardware.usb.IUsbSerialReader$Stub$Proxy@d159848, mHasAudioPlayback=false, mHasAudioCapture=false, mHasMidi=false, mHasVideoCapture=false, mHasVideoPlayback=false, mConfigurations=[
01-03 00:38:21.064  2900  2900 D UsbDeviceHandlerResolver: UsbConfiguration[mId=1,mName=null,mAttributes=128,mMaxPower=50,mInterfaces=[
01-03 00:38:21.064  2900  2900 D UsbDeviceHandlerResolver: UsbInterface[mId=0,mAlternateSetting=0,mName=CP2102 USB to UART Bridge Controller,mClass=255,mSubclass=0,mProtocol=0,mEndpoints=[
01-03 00:38:21.064  2900  2900 D UsbDeviceHandlerResolver: UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=64,mInterval=0]
01-03 00:38:21.064  2900  2900 D UsbDeviceHandlerResolver: UsbEndpoint[mAddress=1,mAttributes=2,mMaxPacketSize=64,mInterval=0]]]] component: ComponentInfo{de.kai_morich.simple_usb_terminal/de.kai_morich.simple_usb_terminal.MainActivity} inAoap: false
01-03 00:38:21.067  2900  2900 E UsbDeviceHandlerResolver: Activity not found: ComponentInfo{de.kai_morich.simple_usb_terminal/de.kai_morich.simple_usb_terminal.MainActivity}
01-03 00:38:21.068  2900  2900 W MessageQueue: Handler (android.car.usb.handler.UsbDeviceHandlerResolver$UsbDeviceResolverHandler) {bafa119} sending message to a Handler on a dead thread
01-03 00:38:21.068  2900  2900 W MessageQueue: java.lang.IllegalStateException: Handler (android.car.usb.handler.UsbDeviceHandlerResolver$UsbDeviceResolverHandler) {bafa119} sending message to a Handler on a dead thread
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.os.MessageQueue.enqueueMessage(MessageQueue.java:569)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.os.Handler.enqueueMessage(Handler.java:787)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.os.Handler.sendMessageAtTime(Handler.java:736)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.os.Handler.sendMessageDelayed(Handler.java:706)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.os.Handler.sendMessage(Handler.java:644)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.car.usb.handler.UsbDeviceHandlerResolver$UsbDeviceResolverHandler.requestResolveHandlers(UsbDeviceHandlerResolver.java:364)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.car.usb.handler.UsbDeviceHandlerResolver.resolve(UsbDeviceHandlerResolver.java:101)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.car.usb.handler.UsbHostController.resolveDevice(UsbHostController.java:214)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.car.usb.handler.UsbHostController.-$$Nest$mresolveDevice(Unknown Source:0)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.car.usb.handler.UsbHostController$UsbHostControllerHandler.handleMessage(UsbHostController.java:381)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.os.Handler.dispatchMessage(Handler.java:107)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.os.Looper.loopOnce(Looper.java:232)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.os.Looper.loop(Looper.java:317)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at android.app.ActivityThread.main(ActivityThread.java:8501)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at java.lang.reflect.Method.invoke(Native Method)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
01-03 00:38:21.068  2900  2900 W MessageQueue: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
01-03 00:38:21.164  1230  1817 D ActivityManager: freezing 3338 android.process.acore
01-03 00:38:22.185  1230  1817 D ActivityManager: freezing 4327 com.example.android_usb_cdc_test
01-03 00:38:23.294   929   929 I s3c2410-wdt 10070000.watchdog_cl1: Watchdog cluster 1 keepalive!, old_wtcnt = 774f, wtcnt = cc8c

@wuwbobo2021
Copy link
Owner

wuwbobo2021 commented Jan 3, 2025

Sorry, the CP21xx serial adapter isn't supported yet. Actually, I'm looking for new maintainers who are willing to support non-CDC serial devices (probably according to https://github.com/mik3y/usb-serial-for-android). The ownership of this repository may be transferred as well.

With the problem of the USB host API helper for nusb, it will be helpful if you can reproduce the crashing on a regular phone with Android 13 (or above) installed. If it works well on the phone, then I can't help anymore (the issue will be left open).

For now, I don't know if your test application crashed before it's freezed (or during unfreezing?); my experience in Android development is really limited, especially with those newest Android versions in which application freezer has been introduced and changed.

Self-note: I've found https://github.com/flyinghead/flycast/issues/872 and https://github.com/rust-mobile/android-activity/issues/58, they may be unrelated issues.

@zpg6
Copy link
Author

zpg6 commented Jan 3, 2025

Ok I was able to get "full control" on my build.. but yes I see I overlooked the class of USB you support.

I'd like to help add serial devices if I can - just new to this area of Android as well. The best solution I've found is https://github.com/kai-morich/SimpleUsbTerminal (java - based on mk3y's you mentioned).

But the flow should be same as you have now up until last steps where I'm crashing right?

@wuwbobo2021
Copy link
Owner

Yes, it's a problem of the Android USB host API helper for nusb, not in the specific serial adapter driver implementation.

I may have a new phone (with Android 13 or above) in the future, but you can help locate this problem if you are able to test it now (see my last comment).

@wuwbobo2021 wuwbobo2021 changed the title SecurityException - no permission to access serial devices The 1st example crashes when permission request is accepted: ActivityManager: freezing 4327 com.example.android_usb_cdc_test Jan 5, 2025
@wuwbobo2021
Copy link
Owner

I've fixed a bug and released a new version of my crate; it now works under a fresh Android 13 environment. Please report if any other problem occurs.

@wuwbobo2021
Copy link
Owner

Sorry, I will close this issue if you can't reproduce it with the new version 0.2.2.

@zpg6
Copy link
Author

zpg6 commented Jan 18, 2025

Let me test I have a device I can use now

@zpg6
Copy link
Author

zpg6 commented Jan 19, 2025

@wuwbobo2021 which peripheral device do you usually test with?

@wuwbobo2021
Copy link
Owner

wuwbobo2021 commented Jan 19, 2025

I've tested it with an STM32F103 VCP device, based on Virtual_COM_Port or VirtualComport_Loopback in STM32_USB-FS-Device_Lib_V4.1.0. The device uploading speed may reach 960 KB/s when transfering non-serial data, but it's slower with android-usbser-rs host (its probably limited by the OS driver stack), see #1. I've tested my Rust firmware based on stm32f1xx-hal 0.10.0 and usbd-serial 0.1.1, its maximum speed is 128 KB/s (I don't know why).

Some other firmwares can be found for Arduino devices.

I'm planning to add support for FT232 and Prolific adapters, but not soon.

Please close this issue and open another issue "Add support for xxx serial adapter(s)" if the problem described here has been solved.

@zpg6 zpg6 closed this as completed Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants