@@ -8,6 +8,7 @@ pub struct UsbVidPid(pub u16, pub u16);
88/// Used to build new [`UsbDevice`]s.
99pub struct UsbDeviceBuilder < ' a , B : UsbBus > {
1010 alloc : & ' a UsbBusAllocator < B > ,
11+ control_buffer : & ' a mut [ u8 ] ,
1112 config : Config < ' a > ,
1213}
1314
@@ -32,6 +33,8 @@ pub enum BuilderError {
3233 InvalidPacketSize ,
3334 /// Configuration specifies higher USB power draw than allowed
3435 PowerTooHigh ,
36+ /// The provided control buffer is too small for the provided maximum packet size.
37+ ControlBufferTooSmall ,
3538}
3639
3740/// Provides basic string descriptors about the device, including the manufacturer, product name,
@@ -82,9 +85,14 @@ impl<'a> StringDescriptors<'a> {
8285
8386impl < ' a , B : UsbBus > UsbDeviceBuilder < ' a , B > {
8487 /// 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 > {
8693 UsbDeviceBuilder {
8794 alloc,
95+ control_buffer,
8896 config : Config {
8997 device_class : 0x00 ,
9098 device_sub_class : 0x00 ,
@@ -104,8 +112,16 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
104112 }
105113
106114 /// 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+ ) )
109125 }
110126
111127 builder_fields ! {
@@ -188,6 +204,10 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
188204 _ => return Err ( BuilderError :: InvalidPacketSize ) ,
189205 }
190206
207+ if self . control_buffer . len ( ) < max_packet_size_0 as usize {
208+ return Err ( BuilderError :: ControlBufferTooSmall ) ;
209+ }
210+
191211 self . config . max_packet_size_0 = max_packet_size_0;
192212 Ok ( self )
193213 }
0 commit comments