use crate::descriptor::descriptor_type;
use crate::endpoint::{EndpointAddress, EndpointType, IsoSynchronization, IsoUsage};
use crate::host::error::{HostError, HostResult};
#[inline]
const fn le16(bytes: &[u8], offset: usize) -> u16 {
(bytes[offset] as u16) | ((bytes[offset + 1] as u16) << 8)
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DeviceDescriptor {
pub usb_version: u16,
pub class: u8,
pub sub_class: u8,
pub protocol: u8,
pub max_packet0: u16,
pub vendor_id: u16,
pub product_id: u16,
pub device_version: u16,
pub manufacturer: u8,
pub product: u8,
pub serial_number: u8,
pub configurations: u8,
}
impl DeviceDescriptor {
pub const LEN: usize = 18;
pub fn parse(bytes: &[u8]) -> HostResult<Self> {
if bytes.len() < Self::LEN || bytes[0] < Self::LEN as u8 {
return Err(HostError::Malformed);
}
if bytes[1] != descriptor_type::DEVICE {
return Err(HostError::Malformed);
}
Ok(Self {
usb_version: le16(bytes, 2),
class: bytes[4],
sub_class: bytes[5],
protocol: bytes[6],
max_packet0: bytes[7] as u16,
vendor_id: le16(bytes, 8),
product_id: le16(bytes, 10),
device_version: le16(bytes, 12),
manufacturer: bytes[14],
product: bytes[15],
serial_number: bytes[16],
configurations: bytes[17],
})
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Descriptor<'a> {
pub kind: u8,
pub bytes: &'a [u8],
}
#[derive(Clone, Copy, Debug)]
pub struct Descriptors<'a> {
rest: &'a [u8],
}
impl<'a> Iterator for Descriptors<'a> {
type Item = Descriptor<'a>;
fn next(&mut self) -> Option<Self::Item> {
let length = *self.rest.first()? as usize;
if length < 2 || length > self.rest.len() {
self.rest = &[];
return None;
}
let (head, tail) = self.rest.split_at(length);
self.rest = tail;
Some(Descriptor {
kind: head[1],
bytes: head,
})
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Configuration<'a> {
raw: &'a [u8],
}
impl<'a> Configuration<'a> {
pub const HEADER_LEN: usize = 9;
pub fn parse(raw: &'a [u8]) -> HostResult<Self> {
if raw.len() < Self::HEADER_LEN || raw[1] != descriptor_type::CONFIGURATION {
return Err(HostError::Malformed);
}
let total = le16(raw, 2) as usize;
if total < Self::HEADER_LEN || total > raw.len() {
return Err(HostError::Malformed);
}
Ok(Self { raw: &raw[..total] })
}
pub(crate) fn validated(raw: &'a [u8]) -> Self {
Self { raw }
}
#[inline]
pub const fn bytes(&self) -> &'a [u8] {
self.raw
}
#[inline]
pub fn total_length(&self) -> u16 {
le16(self.raw, 2)
}
#[inline]
pub fn value(&self) -> u8 {
self.raw[5]
}
#[inline]
pub fn attributes(&self) -> u8 {
self.raw[6]
}
#[inline]
pub fn max_power_ma(&self) -> u16 {
self.raw[8] as u16 * 2
}
#[inline]
pub fn descriptors(&self) -> Descriptors<'a> {
Descriptors { rest: self.raw }
}
#[inline]
pub fn interfaces(&self) -> Interfaces<'a> {
Interfaces {
raw: self.raw,
offset: Self::HEADER_LEN,
}
}
pub fn find_interface(&self, class: u8, sub_class: u8, protocol: u8) -> Option<Interface<'a>> {
self.interfaces()
.find(|i| i.class() == class && i.sub_class() == sub_class && i.protocol() == protocol)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Interface<'a> {
raw: &'a [u8],
length: usize,
}
impl<'a> Interface<'a> {
fn parse(bytes: &'a [u8]) -> Option<Self> {
if bytes.len() < 9 || bytes[1] != descriptor_type::INTERFACE {
return None;
}
Some(Self {
raw: bytes,
length: bytes[0] as usize,
})
}
#[inline]
pub fn number(&self) -> u8 {
self.raw[2]
}
#[inline]
pub fn alternate_setting(&self) -> u8 {
self.raw[3]
}
#[inline]
pub fn endpoint_count(&self) -> u8 {
self.raw[4]
}
#[inline]
pub fn class(&self) -> u8 {
self.raw[5]
}
#[inline]
pub fn sub_class(&self) -> u8 {
self.raw[6]
}
#[inline]
pub fn protocol(&self) -> u8 {
self.raw[7]
}
#[inline]
pub fn endpoints(&self) -> Endpoints<'a> {
Endpoints {
rest: &self.raw[self.length.min(self.raw.len())..],
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Interfaces<'a> {
raw: &'a [u8],
offset: usize,
}
impl<'a> Iterator for Interfaces<'a> {
type Item = Interface<'a>;
fn next(&mut self) -> Option<Self::Item> {
while self.offset + 2 <= self.raw.len() {
let start = self.offset;
let length = self.raw[start] as usize;
if length < 2 || start + length > self.raw.len() {
self.offset = self.raw.len();
return None;
}
self.offset += length;
if self.raw[start + 1] == descriptor_type::INTERFACE {
return Interface::parse(&self.raw[start..]);
}
}
None
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct EndpointDescriptor {
pub address: EndpointAddress,
pub attributes: u8,
pub max_packet: u16,
pub interval: u8,
}
impl EndpointDescriptor {
pub const LEN: usize = 7;
pub fn parse(bytes: &[u8]) -> HostResult<Self> {
if bytes.len() < Self::LEN || bytes[0] < Self::LEN as u8 {
return Err(HostError::Malformed);
}
if bytes[1] != descriptor_type::ENDPOINT {
return Err(HostError::Malformed);
}
Ok(Self {
address: EndpointAddress::from(bytes[2]),
attributes: bytes[3],
max_packet: le16(bytes, 4) & 0x07ff,
interval: bytes[6],
})
}
pub fn transfer_type(&self) -> EndpointType {
match self.attributes & 0x03 {
0 => EndpointType::Control,
1 => EndpointType::Isochronous {
synchronization: IsoSynchronization::None,
usage: IsoUsage::Data,
},
2 => EndpointType::Bulk,
_ => EndpointType::Interrupt,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Endpoints<'a> {
rest: &'a [u8],
}
impl Iterator for Endpoints<'_> {
type Item = EndpointDescriptor;
fn next(&mut self) -> Option<Self::Item> {
let length = *self.rest.first()? as usize;
if length < 2 || length > self.rest.len() || self.rest[1] != descriptor_type::ENDPOINT {
self.rest = &[];
return None;
}
let (head, tail) = self.rest.split_at(length);
self.rest = tail;
EndpointDescriptor::parse(head).ok()
}
}