Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add write_iter and endpoint_with_additional_data methods #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ impl DescriptorWriter<'_> {
Ok(())
}

/// Writes an arbitrary (usually class-specific) descriptor.
pub fn write_iter(&mut self, descriptor_type: u8, descriptor: impl IntoIterator<Item=u8>) -> Result<()> {
let start = self.position + 2;

let mut length = 0;
for byte in descriptor {
if (self.position + 2 + length) > self.buf.len() || (length + 2) > 255 {
return Err(UsbError::BufferOverflow);
}

self.buf[start + length] = byte;
length += 1;
}

self.buf[self.position] = (length + 2) as u8;
self.buf[self.position + 1] = descriptor_type;

self.position = start + length;

Ok(())
}

pub(crate) fn device(&mut self, config: &device::Config) -> Result<()> {
self.write(
descriptor_type::DEVICE,
Expand Down Expand Up @@ -286,6 +308,43 @@ impl DescriptorWriter<'_> {
Ok(())
}

/// Writes an endpoint descriptor with additional data appended.
/// Useful for Audio/Midi class endpoint descriptors.
///
/// # Arguments
///
/// * `endpoint` - Endpoint previously allocated with
/// [`UsbBusAllocator`](crate::bus::UsbBusAllocator).
/// * `additional_data` - Additional data bytes
pub fn endpoint_with_additional_data<'e, B: UsbBus, D: EndpointDirection>(
&mut self,
endpoint: &Endpoint<'e, B, D>,
additional_data: impl IntoIterator<Item = u8>,
) -> Result<()> {
match self.num_endpoints_mark {
Some(mark) => self.buf[mark] += 1,
None => return Err(UsbError::InvalidState),
};

let mps = endpoint.max_packet_size();

self.write_iter(
descriptor_type::ENDPOINT,
[
endpoint.address().into(), // bEndpointAddress
endpoint.ep_type() as u8, // bmAttributes
mps as u8,
(mps >> 8) as u8, // wMaxPacketSize
endpoint.interval(), // bInterval
]
.iter()
.cloned()
.chain(additional_data),
)?;

Ok(())
}

/// Writes a string descriptor.
pub(crate) fn string(&mut self, string: &str) -> Result<()> {
let mut pos = self.position;
Expand Down