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
use std::fmt;
use libusb::*;
use fields::{Direction, TransferType, SyncType, UsageType};
pub struct EndpointDescriptor<'a> {
descriptor: &'a libusb_endpoint_descriptor,
}
impl<'a> EndpointDescriptor<'a> {
pub fn address(&self) -> u8 {
self.descriptor.bEndpointAddress
}
pub fn number(&self) -> u8 {
self.descriptor.bEndpointAddress & 0x07
}
pub fn direction(&self) -> Direction {
match self.descriptor.bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK {
LIBUSB_ENDPOINT_OUT => Direction::Out,
LIBUSB_ENDPOINT_IN | _ => Direction::In,
}
}
pub fn transfer_type(&self) -> TransferType {
match self.descriptor.bmAttributes & LIBUSB_TRANSFER_TYPE_MASK {
LIBUSB_TRANSFER_TYPE_CONTROL => TransferType::Control,
LIBUSB_TRANSFER_TYPE_ISOCHRONOUS => TransferType::Isochronous,
LIBUSB_TRANSFER_TYPE_BULK => TransferType::Bulk,
LIBUSB_TRANSFER_TYPE_INTERRUPT | _ => TransferType::Interrupt,
}
}
pub fn sync_type(&self) -> SyncType {
match (self.descriptor.bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) >> 2 {
LIBUSB_ISO_SYNC_TYPE_NONE => SyncType::NoSync,
LIBUSB_ISO_SYNC_TYPE_ASYNC => SyncType::Asynchronous,
LIBUSB_ISO_SYNC_TYPE_ADAPTIVE => SyncType::Adaptive,
LIBUSB_ISO_SYNC_TYPE_SYNC | _ => SyncType::Synchronous,
}
}
pub fn usage_type(&self) -> UsageType {
match (self.descriptor.bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) >> 4 {
LIBUSB_ISO_USAGE_TYPE_DATA => UsageType::Data,
LIBUSB_ISO_USAGE_TYPE_FEEDBACK => UsageType::Feedback,
LIBUSB_ISO_USAGE_TYPE_IMPLICIT => UsageType::FeedbackData,
_ => UsageType::Reserved,
}
}
pub fn max_packet_size(&self) -> u16 {
self.descriptor.wMaxPacketSize
}
pub fn interval(&self) -> u8 {
self.descriptor.bInterval
}
}
impl<'a> fmt::Debug for EndpointDescriptor<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let mut debug = fmt.debug_struct("EndpointDescriptor");
debug.field("bLength", &self.descriptor.bLength);
debug.field("bDescriptorType", &self.descriptor.bDescriptorType);
debug.field("bEndpointAddress", &self.descriptor.bEndpointAddress);
debug.field("bmAttributes", &self.descriptor.bmAttributes);
debug.field("wMaxPacketSize", &self.descriptor.wMaxPacketSize);
debug.field("bInterval", &self.descriptor.bInterval);
debug.finish()
}
}
#[doc(hidden)]
pub fn from_libusb(endpoint: &libusb_endpoint_descriptor) -> EndpointDescriptor {
EndpointDescriptor { descriptor: endpoint }
}
#[cfg(test)]
mod test {
use ::fields::{Direction,TransferType,SyncType,UsageType};
#[test]
fn it_interprets_number_for_output_endpoints() {
assert_eq!(0, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b0000_0000)).number());
assert_eq!(1, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b0000_0001)).number());
}
#[test]
fn it_interprets_number_for_input_endpoints() {
assert_eq!(2, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b1000_0010)).number());
assert_eq!(3, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b1000_0011)).number());
}
#[test]
fn it_ignores_reserved_bits_in_address() {
assert_eq!(0, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b0000_1000)).number());
assert_eq!(0, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b0001_0000)).number());
assert_eq!(0, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b0010_0000)).number());
assert_eq!(0, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b0100_0000)).number());
assert_eq!(7, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b1111_1111)).number());
}
#[test]
fn it_interprets_direction_bit_in_address() {
assert_eq!(Direction::Out, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b0000_0000)).direction());
assert_eq!(Direction::In, super::from_libusb(&endpoint_descriptor!(bEndpointAddress: 0b1000_0000)).direction());
}
#[test]
fn it_interprets_transfer_type_in_attributes() {
assert_eq!(TransferType::Control, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0000_0000)).transfer_type());
assert_eq!(TransferType::Isochronous, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0000_0001)).transfer_type());
assert_eq!(TransferType::Bulk, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0000_0010)).transfer_type());
assert_eq!(TransferType::Interrupt, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0000_0011)).transfer_type());
}
#[test]
fn it_interprets_synchronization_type_in_attributes() {
assert_eq!(SyncType::NoSync, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0000_0001)).sync_type());
assert_eq!(SyncType::Asynchronous, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0000_0101)).sync_type());
assert_eq!(SyncType::Adaptive, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0000_1001)).sync_type());
assert_eq!(SyncType::Synchronous, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0000_1101)).sync_type());
}
#[test]
fn it_interprets_usage_type_in_attributes() {
assert_eq!(UsageType::Data, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0000_0001)).usage_type());
assert_eq!(UsageType::Feedback, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0001_0001)).usage_type());
assert_eq!(UsageType::FeedbackData, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0010_0001)).usage_type());
assert_eq!(UsageType::Reserved, super::from_libusb(&endpoint_descriptor!(bmAttributes: 0b0011_0001)).usage_type());
}
#[test]
fn it_has_max_packet_size() {
assert_eq!(64, super::from_libusb(&endpoint_descriptor!(wMaxPacketSize: 64)).max_packet_size());
assert_eq!(4096, super::from_libusb(&endpoint_descriptor!(wMaxPacketSize: 4096)).max_packet_size());
assert_eq!(65535, super::from_libusb(&endpoint_descriptor!(wMaxPacketSize: 65535)).max_packet_size());
}
#[test]
fn it_has_interval() {
assert_eq!(1, super::from_libusb(&endpoint_descriptor!(bInterval: 1)).interval());
assert_eq!(20, super::from_libusb(&endpoint_descriptor!(bInterval: 20)).interval());
assert_eq!(255, super::from_libusb(&endpoint_descriptor!(bInterval: 255)).interval());
}
}