Skip to content

Commit

Permalink
bLength, bDescriptorType and wTotalLength to descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Nov 13, 2023
1 parent d1fa27c commit 03d9873
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/list_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ fn print_device<T: UsbContext>(device_desc: &DeviceDescriptor, handle: &mut Opti
};

println!("Device Descriptor:");
println!(" bLength {:3}", device_desc.length());
println!(" bDescriptorType {:3}", device_desc.descriptor_type());
println!(
" bcdUSB {:2}.{}{}",
device_desc.usb_version().major(),
Expand Down Expand Up @@ -147,6 +149,12 @@ fn print_device<T: UsbContext>(device_desc: &DeviceDescriptor, handle: &mut Opti

fn print_config<T: UsbContext>(config_desc: &ConfigDescriptor, handle: &mut Option<UsbDevice<T>>) {
println!(" Config Descriptor:");
println!(" bLength {:3}", config_desc.length());
println!(
" bDescriptorType {:3}",
config_desc.descriptor_type()
);
println!(" wTotalLength {:#06x}", config_desc.total_length());
println!(
" bNumInterfaces {:3}",
config_desc.num_interfaces()
Expand Down Expand Up @@ -177,6 +185,11 @@ fn print_interface<T: UsbContext>(
handle: &mut Option<UsbDevice<T>>,
) {
println!(" Interface Descriptor:");
println!(" bLength {:3}", interface_desc.length());
println!(
" bDescriptorType {:3}",
interface_desc.descriptor_type()
);
println!(
" bInterfaceNumber {:3}",
interface_desc.interface_number()
Expand Down Expand Up @@ -219,6 +232,11 @@ fn print_interface<T: UsbContext>(

fn print_endpoint(endpoint_desc: &EndpointDescriptor) {
println!(" Endpoint Descriptor:");
println!(" bLength {:3}", endpoint_desc.length());
println!(
" bDescriptorType {:3}",
endpoint_desc.descriptor_type()
);
println!(
" bEndpointAddress {:#04x} EP {} {:?}",
endpoint_desc.address(),
Expand Down
15 changes: 15 additions & 0 deletions src/config_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ unsafe impl Sync for ConfigDescriptor {}
unsafe impl Send for ConfigDescriptor {}

impl ConfigDescriptor {
/// Returns the size of the descriptor in bytes
pub fn length(&self) -> u8 {
unsafe { (*self.descriptor).bLength }
}

/// Returns the total length in bytes of data returned for this configuration: all interfaces and endpoints
pub fn total_length(&self) -> u16 {
unsafe { (*self.descriptor).wTotalLength }
}

/// Returns the descriptor type
pub fn descriptor_type(&self) -> u8 {
unsafe { (*self.descriptor).bDescriptorType }
}

/// Returns the configuration number.
pub fn number(&self) -> u8 {
unsafe { (*self.descriptor).bConfigurationValue }
Expand Down
10 changes: 10 additions & 0 deletions src/device_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ pub struct DeviceDescriptor {
}

impl DeviceDescriptor {
/// Returns the size of the descriptor in bytes
pub fn length(&self) -> u8 {
self.descriptor.bLength
}

/// Returns the descriptor type
pub fn descriptor_type(&self) -> u8 {
self.descriptor.bDescriptorType
}

/// Returns the device's maximum supported USB version.
pub fn usb_version(&self) -> Version {
Version::from_bcd(self.descriptor.bcdUSB)
Expand Down
10 changes: 10 additions & 0 deletions src/endpoint_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ pub struct EndpointDescriptor<'a> {
}

impl<'a> EndpointDescriptor<'a> {
/// Returns the size of the descriptor in bytes
pub fn length(&self) -> u8 {
self.descriptor.bLength
}

/// Returns the descriptor type
pub fn descriptor_type(&self) -> u8 {
self.descriptor.bDescriptorType
}

/// Returns the endpoint's address.
pub fn address(&self) -> u8 {
self.descriptor.bEndpointAddress
Expand Down
10 changes: 10 additions & 0 deletions src/interface_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ pub struct InterfaceDescriptor<'a> {
}

impl<'a> InterfaceDescriptor<'a> {
/// Returns the size of the descriptor in bytes
pub fn length(&self) -> u8 {
self.descriptor.bLength
}

/// Returns the descriptor type
pub fn descriptor_type(&self) -> u8 {
self.descriptor.bDescriptorType
}

/// Returns the interface's number.
pub fn interface_number(&self) -> u8 {
self.descriptor.bInterfaceNumber
Expand Down

0 comments on commit 03d9873

Please sign in to comment.