Skip to main content

DescriptorWriter

Struct DescriptorWriter 

Source
pub struct DescriptorWriter<'a> { /* private fields */ }
Expand description

Cursor-based USB descriptor builder.

Wraps a &mut [u8] buffer and advances a position pointer as descriptors are appended. The configuration-descriptor wTotalLength and bNumInterfaces fields are back-patched when end_configuration is called.

Implementations§

Source§

impl<'a> DescriptorWriter<'a>

Source

pub fn new(buf: &'a mut [u8]) -> Self

Create a new writer over buf.

Source

pub fn position(&self) -> usize

Bytes written so far.

Source

pub fn write_with( &mut self, descriptor_type: u8, f: impl FnOnce(&mut [u8]) -> UsbResult<usize>, ) -> UsbResult<()>

Append a raw byte sequence preceded by a 2-byte header (bLength, bDescriptorType).

f receives a mutable slice of up to 254 bytes (max descriptor body size) and should write the descriptor body, returning the body length.

Source

pub fn write(&mut self, descriptor_type: u8, body: &[u8]) -> UsbResult<()>

Append a descriptor with a fixed body slice.

Source

pub fn configuration( &mut self, attributes: u8, max_power_ma: u8, ) -> UsbResult<()>

Begin a configuration descriptor. Must be paired with end_configuration.

Source

pub fn end_configuration(&mut self)

Finalise the open configuration descriptor (patch wTotalLength and bNumInterfaces).

Source

pub fn iad( &mut self, first_interface: InterfaceNumber, interface_count: u8, function_class: u8, function_subclass: u8, function_protocol: u8, ) -> UsbResult<()>

Write an Interface Association Descriptor (IAD), if the configuration was built with composite_with_iads = true.

Always safe to call — if the outer device was not configured for IADs the call is a no-op.

Source

pub fn interface( &mut self, number: InterfaceNumber, num_endpoints: u8, class: u8, subclass: u8, protocol: u8, ) -> UsbResult<()>

Write an Interface descriptor and increment the interface count.

Source

pub fn interface_with_string( &mut self, number: InterfaceNumber, num_endpoints: u8, class: u8, subclass: u8, protocol: u8, string: StringIndex, ) -> UsbResult<()>

Write an Interface descriptor with a string index.

Source

pub fn interface_alt( &mut self, number: InterfaceNumber, alternate_setting: u8, num_endpoints: u8, class: u8, subclass: u8, protocol: u8, ) -> UsbResult<()>

Write an Interface descriptor with a non-zero alternate setting.

Does not increment bNumInterfaces — alternate settings share the same interface number and are not counted separately.

Source

pub fn endpoint( &mut self, addr: EndpointAddress, ep_type: EndpointType, max_packet_size: u16, interval: u8, ) -> UsbResult<()>

Write an Endpoint descriptor.

Source

pub fn write_string(buf: &mut [u8], s: &str) -> UsbResult<usize>

Encode a UTF-8 string as a USB string descriptor (UTF-16LE with header) and write it into buf. Returns the number of bytes written.

Call this from the device layer when responding to GET_DESCRIPTOR(String).

Source

pub fn write_lang_ids(buf: &mut [u8], lang_ids: &[u16]) -> UsbResult<usize>

Write the language ID list descriptor (index 0) into buf.

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for DescriptorWriter<'a>

§

impl<'a> Forget for DescriptorWriter<'a>

§

impl<'a> Freeze for DescriptorWriter<'a>

§

impl<'a> Move for DescriptorWriter<'a>

§

impl<'a> RefUnwindSafe for DescriptorWriter<'a>

§

impl<'a> Send for DescriptorWriter<'a>

§

impl<'a> Sync for DescriptorWriter<'a>

§

impl<'a> Unpin for DescriptorWriter<'a>

§

impl<'a> UnsafeUnpin for DescriptorWriter<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.