fix(embassy-usb): Make BOS descriptor optional

This commit is contained in:
bae 2023-05-29 17:59:17 -07:00
parent bc02c7ee8d
commit 56da5a2c57
2 changed files with 24 additions and 12 deletions

View File

@ -131,7 +131,7 @@ pub struct Builder<'d, D: Driver<'d>> {
device_descriptor: DescriptorWriter<'d>,
config_descriptor: DescriptorWriter<'d>,
bos_descriptor: BosWriter<'d>,
bos_descriptor: Option<BosWriter<'d>>,
#[cfg(feature = "msos-descriptor")]
msos_descriptor: MsOsDescriptorWriter<'d>,
@ -148,7 +148,7 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
config: Config<'d>,
device_descriptor_buf: &'d mut [u8],
config_descriptor_buf: &'d mut [u8],
bos_descriptor_buf: &'d mut [u8],
bos_descriptor_buf: Option<&'d mut [u8]>,
#[cfg(feature = "msos-descriptor")] msos_descriptor_buf: &'d mut [u8],
control_buf: &'d mut [u8],
) -> Self {
@ -170,11 +170,13 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
let mut device_descriptor = DescriptorWriter::new(device_descriptor_buf);
let mut config_descriptor = DescriptorWriter::new(config_descriptor_buf);
let mut bos_descriptor = BosWriter::new(DescriptorWriter::new(bos_descriptor_buf));
let mut bos_descriptor = bos_descriptor_buf.map(|buf| BosWriter::new(DescriptorWriter::new(buf)));
device_descriptor.device(&config);
config_descriptor.configuration(&config);
bos_descriptor.bos();
if let Some(descriptor) = bos_descriptor.as_mut() {
descriptor.bos();
}
Builder {
driver,
@ -199,12 +201,16 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
let msos_descriptor = self.msos_descriptor.build(&mut self.bos_descriptor);
self.config_descriptor.end_configuration();
self.bos_descriptor.end_bos();
if let Some(descriptor) = self.bos_descriptor.as_mut() {
descriptor.end_bos();
}
// Log the number of allocator bytes actually used in descriptor buffers
info!("USB: device_descriptor used: {}", self.device_descriptor.position());
info!("USB: config_descriptor used: {}", self.config_descriptor.position());
info!("USB: bos_descriptor used: {}", self.bos_descriptor.writer.position());
if let Some(descriptor) = self.bos_descriptor.as_ref() {
info!("USB: bos_descriptor used: {}", descriptor.writer.position());
}
#[cfg(feature = "msos-descriptor")]
info!("USB: msos_descriptor used: {}", msos_descriptor.len());
info!("USB: control_buf size: {}", self.control_buf.len());
@ -215,7 +221,8 @@ impl<'d, D: Driver<'d>> Builder<'d, D> {
self.handlers,
self.device_descriptor.into_buf(),
self.config_descriptor.into_buf(),
self.bos_descriptor.writer.into_buf(),
self.bos_descriptor
.map(|descriptor| descriptor.writer.into_buf() as &[u8]),
self.interfaces,
self.control_buf,
#[cfg(feature = "msos-descriptor")]

View File

@ -173,7 +173,9 @@ pub struct UsbBufferReport {
/// Number of config descriptor bytes used
pub config_descriptor_used: usize,
/// Number of bos descriptor bytes used
pub bos_descriptor_used: usize,
///
/// Will be `None` if "bos_descriptor_buf" was not supplied to builder
pub bos_descriptor_used: Option<usize>,
/// Number of msos descriptor bytes used
///
/// Will be `None` if the "msos-descriptor" feature is not active.
@ -196,7 +198,7 @@ struct Inner<'d, D: Driver<'d>> {
config: Config<'d>,
device_descriptor: &'d [u8],
config_descriptor: &'d [u8],
bos_descriptor: &'d [u8],
bos_descriptor: Option<&'d [u8]>,
device_state: UsbDeviceState,
suspended: bool,
@ -224,7 +226,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
handlers: Vec<&'d mut dyn Handler, MAX_HANDLER_COUNT>,
device_descriptor: &'d [u8],
config_descriptor: &'d [u8],
bos_descriptor: &'d [u8],
bos_descriptor: Option<&'d [u8]>,
interfaces: Vec<Interface, MAX_INTERFACE_COUNT>,
control_buf: &'d mut [u8],
#[cfg(feature = "msos-descriptor")] msos_descriptor: crate::msos::MsOsDescriptorSet<'d>,
@ -269,7 +271,7 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
UsbBufferReport {
device_descriptor_used: self.inner.device_descriptor.len(),
config_descriptor_used: self.inner.config_descriptor.len(),
bos_descriptor_used: self.inner.bos_descriptor.len(),
bos_descriptor_used: self.inner.bos_descriptor.map(|descriptor| descriptor.len()),
msos_descriptor_used: mdu,
control_buffer_size: self.control_buf.len(),
}
@ -726,7 +728,10 @@ impl<'d, D: Driver<'d>> Inner<'d, D> {
let (dtype, index) = req.descriptor_type_index();
match dtype {
descriptor_type::BOS => InResponse::Accepted(self.bos_descriptor),
descriptor_type::BOS => match self.bos_descriptor {
Some(descriptor) => InResponse::Accepted(descriptor),
None => InResponse::Rejected,
},
descriptor_type::DEVICE => InResponse::Accepted(self.device_descriptor),
descriptor_type::CONFIGURATION => InResponse::Accepted(self.config_descriptor),
descriptor_type::STRING => {