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
use std::fmt;
use libusb::*;
use fields::Version;
pub struct DeviceDescriptor {
descriptor: libusb_device_descriptor,
}
impl DeviceDescriptor {
pub fn usb_version(&self) -> Version {
Version::from_bcd(self.descriptor.bcdUSB)
}
pub fn device_version(&self) -> Version {
Version::from_bcd(self.descriptor.bcdDevice)
}
pub fn manufacturer_string_index(&self) -> Option<u8> {
match self.descriptor.iManufacturer {
0 => None,
n => Some(n),
}
}
pub fn product_string_index(&self) -> Option<u8> {
match self.descriptor.iProduct {
0 => None,
n => Some(n),
}
}
pub fn serial_number_string_index(&self) -> Option<u8> {
match self.descriptor.iSerialNumber {
0 => None,
n => Some(n),
}
}
pub fn class_code(&self) -> u8 {
self.descriptor.bDeviceClass
}
pub fn sub_class_code(&self) -> u8 {
self.descriptor.bDeviceSubClass
}
pub fn protocol_code(&self) -> u8 {
self.descriptor.bDeviceProtocol
}
pub fn vendor_id(&self) -> u16 {
self.descriptor.idVendor
}
pub fn product_id(&self) -> u16 {
self.descriptor.idProduct
}
pub fn max_packet_size(&self) -> u8 {
self.descriptor.bMaxPacketSize0
}
pub fn num_configurations(&self) -> u8 {
self.descriptor.bNumConfigurations
}
}
impl fmt::Debug for DeviceDescriptor {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let mut debug = fmt.debug_struct("DeviceDescriptor");
debug.field("bLength", &self.descriptor.bLength);
debug.field("bDescriptorType", &self.descriptor.bDescriptorType);
debug.field("bcdUSB", &self.descriptor.bcdUSB);
debug.field("bDeviceClass", &self.descriptor.bDeviceClass);
debug.field("bDeviceSubClass", &self.descriptor.bDeviceSubClass);
debug.field("bDeviceProtocol", &self.descriptor.bDeviceProtocol);
debug.field("bMaxPacketSize", &self.descriptor.bMaxPacketSize0);
debug.field("idVendor", &self.descriptor.idVendor);
debug.field("idProduct", &self.descriptor.idProduct);
debug.field("bcdDevice", &self.descriptor.bcdDevice);
debug.field("iManufacturer", &self.descriptor.iManufacturer);
debug.field("iProduct", &self.descriptor.iProduct);
debug.field("iSerialNumber", &self.descriptor.iSerialNumber);
debug.field("bNumConfigurations", &self.descriptor.bNumConfigurations);
debug.finish()
}
}
#[doc(hidden)]
pub fn from_libusb(device: libusb_device_descriptor) -> DeviceDescriptor {
DeviceDescriptor { descriptor: device }
}
#[cfg(test)]
mod test {
use fields::Version;
#[test]
fn it_has_usb_version() {
assert_eq!(Version::from_bcd(0x1234), super::from_libusb(device_descriptor!(bcdUSB: 0x1234)).usb_version());
}
#[test]
fn it_has_device_version() {
assert_eq!(Version::from_bcd(0x1234), super::from_libusb(device_descriptor!(bcdDevice: 0x1234)).device_version());
}
#[test]
fn it_has_manufacturer_string_index() {
assert_eq!(Some(42), super::from_libusb(device_descriptor!(iManufacturer: 42)).manufacturer_string_index());
}
#[test]
fn it_handles_missing_manufacturer_string_index() {
assert_eq!(None, super::from_libusb(device_descriptor!(iManufacturer: 0)).manufacturer_string_index());
}
#[test]
fn it_has_product_string_index() {
assert_eq!(Some(42), super::from_libusb(device_descriptor!(iProduct: 42)).product_string_index());
}
#[test]
fn it_handles_missing_product_string_index() {
assert_eq!(None, super::from_libusb(device_descriptor!(iProduct: 0)).product_string_index());
}
#[test]
fn it_has_serial_number_string_index() {
assert_eq!(Some(42), super::from_libusb(device_descriptor!(iSerialNumber: 42)).serial_number_string_index());
}
#[test]
fn it_handles_missing_serial_number_string_index() {
assert_eq!(None, super::from_libusb(device_descriptor!(iSerialNumber: 0)).serial_number_string_index());
}
#[test]
fn it_has_class_code() {
assert_eq!(42, super::from_libusb(device_descriptor!(bDeviceClass: 42)).class_code());
}
#[test]
fn it_has_sub_class_code() {
assert_eq!(42, super::from_libusb(device_descriptor!(bDeviceSubClass: 42)).sub_class_code());
}
#[test]
fn it_has_protocol_code() {
assert_eq!(42, super::from_libusb(device_descriptor!(bDeviceProtocol: 42)).protocol_code());
}
#[test]
fn it_has_vendor_id() {
assert_eq!(42, super::from_libusb(device_descriptor!(idVendor: 42)).vendor_id());
}
#[test]
fn it_has_product_id() {
assert_eq!(42, super::from_libusb(device_descriptor!(idProduct: 42)).product_id());
}
#[test]
fn it_has_max_packet_size() {
assert_eq!(42, super::from_libusb(device_descriptor!(bMaxPacketSize0: 42)).max_packet_size());
}
#[test]
fn it_has_num_configurations() {
assert_eq!(3, super::from_libusb(device_descriptor!(bNumConfigurations: 3)).num_configurations());
}
}