@@ -8,6 +8,7 @@ pub struct UsbVidPid(pub u16, pub u16);
8
8
/// Used to build new [`UsbDevice`]s.
9
9
pub struct UsbDeviceBuilder < ' a , B : UsbBus > {
10
10
alloc : & ' a UsbBusAllocator < B > ,
11
+ control_buffer : & ' a mut [ u8 ] ,
11
12
config : Config < ' a > ,
12
13
}
13
14
@@ -32,6 +33,8 @@ pub enum BuilderError {
32
33
InvalidPacketSize ,
33
34
/// Configuration specifies higher USB power draw than allowed
34
35
PowerTooHigh ,
36
+ /// The provided control buffer is too small for the provided maximum packet size.
37
+ ControlBufferTooSmall ,
35
38
}
36
39
37
40
/// Provides basic string descriptors about the device, including the manufacturer, product name,
@@ -82,9 +85,14 @@ impl<'a> StringDescriptors<'a> {
82
85
83
86
impl < ' a , B : UsbBus > UsbDeviceBuilder < ' a , B > {
84
87
/// Creates a builder for constructing a new [`UsbDevice`].
85
- pub fn new ( alloc : & ' a UsbBusAllocator < B > , vid_pid : UsbVidPid ) -> UsbDeviceBuilder < ' a , B > {
88
+ pub fn new (
89
+ alloc : & ' a UsbBusAllocator < B > ,
90
+ vid_pid : UsbVidPid ,
91
+ control_buffer : & ' a mut [ u8 ] ,
92
+ ) -> UsbDeviceBuilder < ' a , B > {
86
93
UsbDeviceBuilder {
87
94
alloc,
95
+ control_buffer,
88
96
config : Config {
89
97
device_class : 0x00 ,
90
98
device_sub_class : 0x00 ,
@@ -104,8 +112,16 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
104
112
}
105
113
106
114
/// Creates the [`UsbDevice`] instance with the configuration in this builder.
107
- pub fn build ( self ) -> UsbDevice < ' a , B > {
108
- UsbDevice :: build ( self . alloc , self . config )
115
+ pub fn build ( self ) -> Result < UsbDevice < ' a , B > , BuilderError > {
116
+ if self . control_buffer . len ( ) < self . config . max_packet_size_0 as usize {
117
+ return Err ( BuilderError :: ControlBufferTooSmall ) ;
118
+ }
119
+
120
+ Ok ( UsbDevice :: build (
121
+ self . alloc ,
122
+ self . config ,
123
+ self . control_buffer ,
124
+ ) )
109
125
}
110
126
111
127
builder_fields ! {
@@ -188,6 +204,10 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
188
204
_ => return Err ( BuilderError :: InvalidPacketSize ) ,
189
205
}
190
206
207
+ if self . control_buffer . len ( ) < max_packet_size_0 as usize {
208
+ return Err ( BuilderError :: ControlBufferTooSmall ) ;
209
+ }
210
+
191
211
self . config . max_packet_size_0 = max_packet_size_0;
192
212
Ok ( self )
193
213
}
0 commit comments