Skip to content

Commit

Permalink
Updating PR after review
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Mar 11, 2024
1 parent 390ac1d commit edafa99
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/device_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum BuilderError {
InvalidPacketSize,
/// Configuration specifies higher USB power draw than allowed
PowerTooHigh,
/// The provided control buffer is too small for the provided maximum packet size.
ControlBufferTooSmall,
}

/// Provides basic string descriptors about the device, including the manufacturer, product name,
Expand Down Expand Up @@ -110,8 +112,16 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
}

/// Creates the [`UsbDevice`] instance with the configuration in this builder.
pub fn build(self) -> UsbDevice<'a, B> {
UsbDevice::build(self.alloc, self.config, self.control_buffer)
pub fn build(self) -> Result<UsbDevice<'a, B>, BuilderError> {
if self.control_buffer.len() < self.config.max_packet_size_0 as usize {
return Err(BuilderError::ControlBufferTooSmall);
}

Ok(UsbDevice::build(
self.alloc,
self.config,
self.control_buffer,
))
}

builder_fields! {
Expand Down Expand Up @@ -194,6 +204,10 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
_ => return Err(BuilderError::InvalidPacketSize),
}

if self.control_buffer.len() < max_packet_size_0 as usize {
return Err(BuilderError::ControlBufferTooSmall);
}

self.config.max_packet_size_0 = max_packet_size_0;
Ok(self)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub mod endpoint;
/// .product("Serial port")])
/// .expect("Failed to set strings")
/// .device_class(usb_serial::DEVICE_CLASS)
/// .build();
/// .build().unwrap();
///
/// // At this point the USB peripheral is enabled and a connected host will attempt to enumerate
/// // it.
Expand Down
2 changes: 1 addition & 1 deletion src/test_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<B: UsbBus> TestClass<'_, B> {

/// Convenience method to create a UsbDevice that is configured correctly for TestClass.
pub fn make_device<'a>(&self, usb_bus: &'a UsbBusAllocator<B>) -> UsbDevice<'a, B> {
self.make_device_builder(usb_bus).build()
self.make_device_builder(usb_bus).build().unwrap()
}

/// Convenience method to create a UsbDeviceBuilder that is configured correctly for TestClass.
Expand Down

0 comments on commit edafa99

Please sign in to comment.