1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
pub use libusb1_sys::constants;
pub use crate::{
config_descriptor::{ConfigDescriptor, Interfaces},
context::{Context, GlobalContext, Hotplug, LogLevel, Registration, UsbContext},
device::Device,
device_descriptor::DeviceDescriptor,
device_handle::DeviceHandle,
device_list::{DeviceList, Devices},
endpoint_descriptor::EndpointDescriptor,
error::{Error, Result},
fields::{
request_type, Direction, Recipient, RequestType, Speed, SyncType, TransferType, UsageType,
Version,
},
interface_descriptor::{
EndpointDescriptors, Interface, InterfaceDescriptor, InterfaceDescriptors,
},
language::{Language, PrimaryLanguage, SubLanguage},
options::UsbOption,
version::{version, LibraryVersion},
};
#[cfg(test)]
#[macro_use]
mod test_helpers;
#[macro_use]
mod error;
mod version;
mod context;
mod device;
mod device_handle;
mod device_list;
mod config_descriptor;
mod device_descriptor;
mod endpoint_descriptor;
mod fields;
mod interface_descriptor;
mod language;
mod options;
pub fn has_capability() -> bool {
GlobalContext::default().as_raw();
unsafe { libusb1_sys::libusb_has_capability(constants::LIBUSB_CAP_HAS_CAPABILITY) != 0 }
}
pub fn has_hotplug() -> bool {
GlobalContext::default().as_raw();
unsafe { libusb1_sys::libusb_has_capability(constants::LIBUSB_CAP_HAS_HOTPLUG) != 0 }
}
pub fn has_hid_access() -> bool {
GlobalContext::default().as_raw();
unsafe { libusb1_sys::libusb_has_capability(constants::LIBUSB_CAP_HAS_HID_ACCESS) != 0 }
}
pub fn supports_detach_kernel_driver() -> bool {
GlobalContext::default().as_raw();
unsafe {
libusb1_sys::libusb_has_capability(constants::LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER) != 0
}
}
pub fn devices() -> crate::Result<DeviceList<GlobalContext>> {
GlobalContext::default().devices()
}
pub fn set_log_level(level: LogLevel) {
unsafe {
libusb1_sys::libusb_set_debug(GlobalContext::default().as_raw(), level.as_c_int());
}
}
pub fn open_device_with_vid_pid(
vendor_id: u16,
product_id: u16,
) -> Option<DeviceHandle<GlobalContext>> {
let handle = unsafe {
libusb1_sys::libusb_open_device_with_vid_pid(
GlobalContext::default().as_raw(),
vendor_id,
product_id,
)
};
if handle.is_null() {
None
} else {
Some(unsafe {
DeviceHandle::from_libusb(
GlobalContext::default(),
std::ptr::NonNull::new_unchecked(handle),
)
})
}
}