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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::{
fmt::{self, Debug},
mem,
ptr::NonNull,
};
use libusb1_sys::*;
use crate::{
config_descriptor::{self, ConfigDescriptor},
device_descriptor::{self, DeviceDescriptor},
device_handle::DeviceHandle,
error,
fields::{self, Speed},
Error, UsbContext,
};
#[derive(Eq, PartialEq)]
pub struct Device<T: UsbContext> {
context: T,
device: NonNull<libusb_device>,
}
impl<T: UsbContext> Drop for Device<T> {
fn drop(&mut self) {
unsafe {
libusb_unref_device(self.device.as_ptr());
}
}
}
impl<T: UsbContext> Clone for Device<T> {
fn clone(&self) -> Self {
unsafe {
Self::from_libusb(self.context.clone(), self.device)
}
}
}
unsafe impl<T: UsbContext> Send for Device<T> {}
unsafe impl<T: UsbContext> Sync for Device<T> {}
impl<T: UsbContext> Debug for Device<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let descriptor = match self.device_descriptor() {
Ok(descriptor) => descriptor,
Err(e) => {
return write!(f, "Can't read device descriptor {:?}", e);
}
};
write!(
f,
"Bus {:03} Device {:03}: ID {:04x}:{:04x}",
self.bus_number(),
self.address(),
descriptor.vendor_id(),
descriptor.product_id(),
)
}
}
impl<T: UsbContext> Device<T> {
pub fn as_raw(&self) -> *mut libusb_device {
self.device.as_ptr()
}
pub fn context(&self) -> &T {
&self.context
}
pub unsafe fn from_libusb(context: T, device: NonNull<libusb_device>) -> Device<T> {
libusb_ref_device(device.as_ptr());
Device { context, device }
}
pub fn device_descriptor(&self) -> crate::Result<DeviceDescriptor> {
let mut descriptor = mem::MaybeUninit::<libusb_device_descriptor>::uninit();
try_unsafe!(libusb_get_device_descriptor(
self.device.as_ptr(),
descriptor.as_mut_ptr()
));
Ok(device_descriptor::from_libusb(unsafe {
descriptor.assume_init()
}))
}
pub fn config_descriptor(&self, config_index: u8) -> crate::Result<ConfigDescriptor> {
let mut config = mem::MaybeUninit::<*const libusb_config_descriptor>::uninit();
try_unsafe!(libusb_get_config_descriptor(
self.device.as_ptr(),
config_index,
config.as_mut_ptr()
));
Ok(unsafe { config_descriptor::from_libusb(config.assume_init()) })
}
pub fn active_config_descriptor(&self) -> crate::Result<ConfigDescriptor> {
let mut config = mem::MaybeUninit::<*const libusb_config_descriptor>::uninit();
try_unsafe!(libusb_get_active_config_descriptor(
self.device.as_ptr(),
config.as_mut_ptr()
));
Ok(unsafe { config_descriptor::from_libusb(config.assume_init()) })
}
pub fn bus_number(&self) -> u8 {
unsafe { libusb_get_bus_number(self.device.as_ptr()) }
}
pub fn address(&self) -> u8 {
unsafe { libusb_get_device_address(self.device.as_ptr()) }
}
pub fn speed(&self) -> Speed {
fields::speed_from_libusb(unsafe { libusb_get_device_speed(self.device.as_ptr()) })
}
pub fn open(&self) -> crate::Result<DeviceHandle<T>> {
let mut handle = mem::MaybeUninit::<*mut libusb_device_handle>::uninit();
try_unsafe!(libusb_open(self.device.as_ptr(), handle.as_mut_ptr()));
Ok(unsafe {
let ptr = NonNull::new(handle.assume_init()).ok_or(Error::NoDevice)?;
DeviceHandle::from_libusb(self.context.clone(), ptr)
})
}
pub fn port_number(&self) -> u8 {
unsafe { libusb_get_port_number(self.device.as_ptr()) }
}
pub fn get_parent(&self) -> Option<Self> {
let device = unsafe { libusb_get_parent(self.device.as_ptr()) };
NonNull::new(device)
.map(|device| unsafe { Device::from_libusb(self.context.clone(), device) })
}
pub fn port_numbers(&self) -> Result<Vec<u8>, Error> {
let mut ports = [0; 7];
let result = unsafe {
libusb_get_port_numbers(self.device.as_ptr(), ports.as_mut_ptr(), ports.len() as i32)
};
let ports_number = if result < 0 {
return Err(error::from_libusb(result));
} else {
result
};
Ok(ports[0..ports_number as usize].to_vec())
}
}