Skip to content

Commit a9f3fe5

Browse files
committed
initial commit
0 parents  commit a9f3fe5

File tree

7 files changed

+974
-0
lines changed

7 files changed

+974
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
Cargo.lock

Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
3+
name = "libusb-sys"
4+
version = "0.0.1"
5+
authors = ["David Cuddeback <david.cuddeback@gmail.com>"]
6+
7+
build = "build.rs"
8+
links = "usb-1.0"
9+
10+
[build-dependencies]
11+
pkg-config = "0.3.0"

build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate "pkg-config" as pkg_config;
2+
3+
fn main() {
4+
pkg_config::find_library("libusb-1.0").unwrap();
5+
}

examples/libusb_info.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
 (0)