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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::fmt;
use std::mem;
use std::slice;
use libusb::*;
use interface_descriptor::{self, Interface};
pub struct ConfigDescriptor {
descriptor: *const libusb_config_descriptor,
}
impl Drop for ConfigDescriptor {
fn drop(&mut self) {
unsafe {
libusb_free_config_descriptor(self.descriptor);
}
}
}
unsafe impl Sync for ConfigDescriptor {}
unsafe impl Send for ConfigDescriptor {}
impl ConfigDescriptor {
pub fn number(&self) -> u8 {
unsafe {
(*self.descriptor).bConfigurationValue
}
}
pub fn max_power(&self) -> u16 {
unsafe {
(*self.descriptor).bMaxPower as u16 * 2
}
}
pub fn self_powered(&self) -> bool {
unsafe {
(*self.descriptor).bmAttributes & 0x40 != 0
}
}
pub fn remote_wakeup(&self) -> bool {
unsafe {
(*self.descriptor).bmAttributes & 0x20 != 0
}
}
pub fn description_string_index(&self) -> Option<u8> {
unsafe {
match (*self.descriptor).iConfiguration {
0 => None,
n => Some(n),
}
}
}
pub fn num_interfaces(&self) -> u8 {
unsafe {
(*self.descriptor).bNumInterfaces
}
}
pub fn interfaces(&self) -> Interfaces {
let interfaces = unsafe {
slice::from_raw_parts(
(*self.descriptor).interface,
(*self.descriptor).bNumInterfaces as usize
)
};
Interfaces { iter: interfaces.iter() }
}
}
impl fmt::Debug for ConfigDescriptor {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let mut debug = fmt.debug_struct("ConfigDescriptor");
let descriptor: &libusb_config_descriptor = unsafe {
mem::transmute(self.descriptor)
};
debug.field("bLength", &descriptor.bLength);
debug.field("bDescriptorType", &descriptor.bDescriptorType);
debug.field("wTotalLength", &descriptor.wTotalLength);
debug.field("bNumInterfaces", &descriptor.bNumInterfaces);
debug.field("bConfigurationValue", &descriptor.bConfigurationValue);
debug.field("iConfiguration", &descriptor.iConfiguration);
debug.field("bmAttributes", &descriptor.bmAttributes);
debug.field("bMaxPower ", &descriptor.bMaxPower);
debug.finish()
}
}
pub struct Interfaces<'a> {
iter: slice::Iter<'a, libusb_interface>,
}
impl<'a> Iterator for Interfaces<'a> {
type Item = Interface<'a>;
fn next(&mut self) -> Option<Interface<'a>> {
self.iter.next().map(|interface| {
unsafe { interface_descriptor::from_libusb(interface) }
})
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
#[doc(hidden)]
pub unsafe fn from_libusb(config: *const libusb_config_descriptor) -> ConfigDescriptor {
ConfigDescriptor { descriptor: config }
}
#[cfg(test)]
mod test {
use std::mem;
macro_rules! with_config {
($name:ident : $config:expr => $body:block) => {
{
let $name = unsafe { super::from_libusb(&$config) };
$body;
mem::forget($name);
}
}
}
#[test]
fn it_has_number() {
with_config!(config: config_descriptor!(bConfigurationValue: 42) => {
assert_eq!(42, config.number());
});
}
#[test]
fn it_has_max_power() {
with_config!(config: config_descriptor!(bMaxPower: 21) => {
assert_eq!(42, config.max_power());
});
}
#[test]
fn it_interprets_self_powered_bit_in_attributes() {
with_config!(config: config_descriptor!(bmAttributes: 0b0000_0000) => {
assert_eq!(false, config.self_powered());
});
with_config!(config: config_descriptor!(bmAttributes: 0b0100_0000) => {
assert_eq!(true, config.self_powered());
});
}
#[test]
fn it_interprets_remote_wakeup_bit_in_attributes() {
with_config!(config: config_descriptor!(bmAttributes: 0b0000_0000) => {
assert_eq!(false, config.remote_wakeup());
});
with_config!(config: config_descriptor!(bmAttributes: 0b0010_0000) => {
assert_eq!(true, config.remote_wakeup());
});
}
#[test]
fn it_has_description_string_index() {
with_config!(config: config_descriptor!(iConfiguration: 42) => {
assert_eq!(Some(42), config.description_string_index());
});
}
#[test]
fn it_handles_missing_description_string_index() {
with_config!(config: config_descriptor!(iConfiguration: 0) => {
assert_eq!(None, config.description_string_index());
});
}
#[test]
fn it_has_num_interfaces() {
let interface1 = interface!(interface_descriptor!(bInterfaceNumber: 1));
let interface2 = interface!(interface_descriptor!(bInterfaceNumber: 2));
with_config!(config: config_descriptor!(interface1, interface2) => {
assert_eq!(2, config.num_interfaces());
});
}
#[test]
fn it_has_interfaces() {
let interface = interface!(interface_descriptor!(bInterfaceNumber: 1));
with_config!(config: config_descriptor!(interface) => {
let interface_numbers = config.interfaces().map(|interface| {
interface.number()
}).collect::<Vec<_>>();
assert_eq!(vec![1], interface_numbers);
});
}
}