From 0c9607e38d6329f030f83b30606a726be6fd9562 Mon Sep 17 00:00:00 2001 From: Dhruv Gramopadhye Date: Sat, 20 Jul 2024 18:59:13 -0700 Subject: [PATCH 1/4] feat: libusb_init_context and associated structs --- libusb1-sys/src/lib.rs | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/libusb1-sys/src/lib.rs b/libusb1-sys/src/lib.rs index b0ac87d..044e46e 100644 --- a/libusb1-sys/src/lib.rs +++ b/libusb1-sys/src/lib.rs @@ -204,6 +204,88 @@ pub struct libusb_pollfd { pub events: c_short, } +#[repr(C)] +pub union libusb_init_option__value { + pub ival: c_int, + pub log_cbval: libusb_log_cb, +} + +#[repr(C)] +pub enum libusb_option { + /** Set the log message verbosity. + * + * This option must be provided an argument of type \ref libusb_log_level. + * The default level is LIBUSB_LOG_LEVEL_NONE, which means no messages are ever + * printed. If you choose to increase the message verbosity level, ensure + * that your application does not close the stderr file descriptor. + * + * You are advised to use level LIBUSB_LOG_LEVEL_WARNING. libusb is conservative + * with its message logging and most of the time, will only log messages that + * explain error conditions and other oddities. This will help you debug + * your software. + * + * If the LIBUSB_DEBUG environment variable was set when libusb was + * initialized, this option does nothing: the message verbosity is fixed + * to the value in the environment variable. + * + * If libusb was compiled without any message logging, this option does + * nothing: you'll never get any messages. + * + * If libusb was compiled with verbose debug message logging, this option + * does nothing: you'll always get messages from all levels. + */ + LIBUSB_OPTION_LOG_LEVEL = 0, + + /** Use the UsbDk backend for a specific context, if available. + * + * This option should be set at initialization with libusb_init_context() + * otherwise unspecified behavior may occur. + * + * Only valid on Windows. Ignored on all other platforms. + */ + LIBUSB_OPTION_USE_USBDK = 1, + + /** Do not scan for devices + * + * With this option set, libusb will skip scanning devices in + * libusb_init_context(). + * + * Hotplug functionality will also be deactivated. + * + * The option is useful in combination with libusb_wrap_sys_device(), + * which can access a device directly without prior device scanning. + * + * This is typically needed on Android, where access to USB devices + * is limited. + * + * This option should only be used with libusb_init_context() + * otherwise unspecified behavior may occur. + * + * Only valid on Linux. Ignored on all other platforms. + */ + LIBUSB_OPTION_NO_DEVICE_DISCOVERY = 2, + + //LIBUSB_OPTION_WEAK_AUTHORITY = LIBUSB_OPTION_NO_DEVICE_DISCOVERY as isize, + /** Set the context log callback function. + * + * Set the log callback function either on a context or globally. This + * option must be provided an argument of type \ref libusb_log_cb. + * Using this option with a NULL context is equivalent to calling + * libusb_set_log_cb() with mode \ref LIBUSB_LOG_CB_GLOBAL. + * Using it with a non-NULL context is equivalent to calling + * libusb_set_log_cb() with mode \ref LIBUSB_LOG_CB_CONTEXT. + */ + LIBUSB_OPTION_LOG_CB = 3, + + LIBUSB_OPTION_MAX = 4, +} + +#[repr(C)] +pub struct libusb_init_option { + pub option: libusb_option, + pub value: libusb_init_option__value, +} + pub type libusb_hotplug_callback_handle = c_int; pub type libusb_hotplug_flag = c_int; pub type libusb_hotplug_event = c_int; @@ -230,6 +312,11 @@ extern "system" { pub fn libusb_strerror(errcode: c_int) -> *const c_char; pub fn libusb_init(context: *mut *mut libusb_context) -> c_int; + pub fn libusb_init_context( + context: *mut *mut libusb_context, + options: *mut libusb_init_option, + num_options: c_int, + ) -> c_int; pub fn libusb_exit(context: *mut libusb_context); pub fn libusb_set_debug(context: *mut libusb_context, level: c_int); pub fn libusb_set_log_cb(context: *mut libusb_context, cb: Option, mode: c_int); From b422257b8ea39437976eaa4d18e9aaa5da60f43e Mon Sep 17 00:00:00 2001 From: Dhruv Gramopadhye Date: Sun, 21 Jul 2024 13:55:16 -0700 Subject: [PATCH 2/4] no udev on android --- libusb1-sys/Cargo.toml | 1 + libusb1-sys/build.rs | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/libusb1-sys/Cargo.toml b/libusb1-sys/Cargo.toml index c4d4fb7..56a0cbc 100644 --- a/libusb1-sys/Cargo.toml +++ b/libusb1-sys/Cargo.toml @@ -43,5 +43,6 @@ libc = "0.2" vcpkg = "0.2" [build-dependencies] +build-target = "0.4.0" cc = "1.0" pkg-config = "0.3" diff --git a/libusb1-sys/build.rs b/libusb1-sys/build.rs index bd1f43b..fd3f6ee 100644 --- a/libusb1-sys/build.rs +++ b/libusb1-sys/build.rs @@ -158,14 +158,16 @@ fn make_source() { Some("__attribute__((visibility(\"default\")))"), ); - if let Ok(lib) = pkg_config::probe_library("libudev") { - base_config.define("USE_UDEV", Some("1")); - base_config.define("HAVE_LIBUDEV", Some("1")); - base_config.file(libusb_source.join("libusb/os/linux_udev.c")); - for path in lib.include_paths { - base_config.include(path.to_str().unwrap()); - } - }; + if build_target::target_os().unwrap().to_string() != "android" { + if let Ok(lib) = pkg_config::probe_library("libudev") { + base_config.define("USE_UDEV", Some("1")); + base_config.define("HAVE_LIBUDEV", Some("1")); + base_config.file(libusb_source.join("libusb/os/linux_udev.c")); + for path in lib.include_paths { + base_config.include(path.to_str().unwrap()); + } + }; + } println!("Including posix!"); base_config.file(libusb_source.join("libusb/os/events_posix.c")); From 66a1ce99c84e74ceb82fff68d0b6d78df808c54d Mon Sep 17 00:00:00 2001 From: Dhruv Gramopadhye Date: Tue, 23 Jul 2024 20:54:41 -0700 Subject: [PATCH 3/4] refactor: use env variables for consistency instead of build-target --- libusb1-sys/Cargo.toml | 1 - libusb1-sys/build.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/libusb1-sys/Cargo.toml b/libusb1-sys/Cargo.toml index 56a0cbc..c4d4fb7 100644 --- a/libusb1-sys/Cargo.toml +++ b/libusb1-sys/Cargo.toml @@ -43,6 +43,5 @@ libc = "0.2" vcpkg = "0.2" [build-dependencies] -build-target = "0.4.0" cc = "1.0" pkg-config = "0.3" diff --git a/libusb1-sys/build.rs b/libusb1-sys/build.rs index fd3f6ee..3d05d43 100644 --- a/libusb1-sys/build.rs +++ b/libusb1-sys/build.rs @@ -158,7 +158,7 @@ fn make_source() { Some("__attribute__((visibility(\"default\")))"), ); - if build_target::target_os().unwrap().to_string() != "android" { + if std::env::var("CARGO_CFG_TARGET_OS") != Ok("android".into()) { if let Ok(lib) = pkg_config::probe_library("libudev") { base_config.define("USE_UDEV", Some("1")); base_config.define("HAVE_LIBUDEV", Some("1")); From b81e27465ec19f0df6aabd465985893f7bcaf7ef Mon Sep 17 00:00:00 2001 From: Dhruv Gramopadhye Date: Thu, 19 Jun 2025 23:52:53 -0700 Subject: [PATCH 4/4] dont unwrap, just option none if we can't run sw_vers --- libusb1-sys/build.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libusb1-sys/build.rs b/libusb1-sys/build.rs index 3d05d43..a815479 100644 --- a/libusb1-sys/build.rs +++ b/libusb1-sys/build.rs @@ -27,10 +27,11 @@ fn get_macos_major_version() -> Option { let output = std::process::Command::new("sw_vers") .args(["-productVersion"]) .output() - .unwrap(); - let version = std::str::from_utf8(&output.stdout).unwrap().trim_end(); + .ok()?; + + let version = std::str::from_utf8(&output.stdout).ok()?.trim_end(); let components: Vec<&str> = version.split('.').collect(); - let major: usize = components[0].parse().unwrap(); + let major: usize = components[0].parse().ok()?; Some(major) }