|
| 1 | +extern crate "libusb-sys" as ffi; |
| 2 | + |
| 3 | +use std::mem; |
| 4 | +use std::str; |
| 5 | +use std::ffi::CStr; |
| 6 | + |
| 7 | +fn main() { |
| 8 | + print_version(); |
| 9 | + print_capabilities(); |
| 10 | +} |
| 11 | + |
| 12 | +fn print_version() { |
| 13 | + let version = unsafe { ::ffi::libusb_get_version() }; |
| 14 | + |
| 15 | + let rc = str::from_utf8(unsafe { CStr::from_ptr(version.rc) }.to_bytes()).unwrap_or(""); |
| 16 | + let describe = str::from_utf8(unsafe { CStr::from_ptr(version.describe) }.to_bytes()).unwrap_or(""); |
| 17 | + |
| 18 | + println!("libusb v{}.{}.{}.{}{} {}", version.major, version.minor, version.micro, version.nano, rc, describe); |
| 19 | +} |
| 20 | + |
| 21 | +fn print_capabilities() { |
| 22 | + let mut context: *mut ::ffi::libusb_context = unsafe { mem::uninitialized() }; |
| 23 | + |
| 24 | + // library must be initialized before calling libusb_has_capabililty() |
| 25 | + match unsafe { ::ffi::libusb_init(&mut context) } { |
| 26 | + 0 => (), |
| 27 | + e => panic!("libusb_init: {}", e) |
| 28 | + }; |
| 29 | + |
| 30 | + unsafe { |
| 31 | + ::ffi::libusb_set_debug(context, ::ffi::LIBUSB_LOG_LEVEL_DEBUG); |
| 32 | + ::ffi::libusb_set_debug(context, ::ffi::LIBUSB_LOG_LEVEL_INFO); |
| 33 | + ::ffi::libusb_set_debug(context, ::ffi::LIBUSB_LOG_LEVEL_WARNING); |
| 34 | + ::ffi::libusb_set_debug(context, ::ffi::LIBUSB_LOG_LEVEL_ERROR); |
| 35 | + ::ffi::libusb_set_debug(context, ::ffi::LIBUSB_LOG_LEVEL_NONE); |
| 36 | + } |
| 37 | + |
| 38 | + println!("has capability? {}", unsafe { ::ffi::libusb_has_capability(::ffi::LIBUSB_CAP_HAS_CAPABILITY) }); |
| 39 | + println!("has hotplug? {}", unsafe { ::ffi::libusb_has_capability(::ffi::LIBUSB_CAP_HAS_HOTPLUG) }); |
| 40 | + println!("has HID access? {}", unsafe { ::ffi::libusb_has_capability(::ffi::LIBUSB_CAP_HAS_HID_ACCESS) }); |
| 41 | + println!("supports detach kernel driver? {}", unsafe { ::ffi::libusb_has_capability(::ffi::LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER) }); |
| 42 | + |
| 43 | + unsafe { ::ffi::libusb_exit(context) }; |
| 44 | +} |
0 commit comments