Skip to main content

usb_stack/control/
request.rs

1//! USB SETUP packet parser.
2
3use crate::endpoint::{Direction, EndpointAddress};
4use crate::error::{UsbError, UsbResult};
5
6// ─── Request type & recipient ─────────────────────────────────────────────────
7
8/// `bmRequestType` bits 6:5 — who defined the request.
9#[derive(Clone, Copy, PartialEq, Eq, Debug)]
10#[cfg_attr(feature = "defmt", derive(defmt::Format))]
11pub enum RequestType {
12    /// USB 2.0 standard request.
13    Standard = 0,
14    /// Class-specific request.
15    Class = 1,
16    /// Vendor-specific request.
17    Vendor = 2,
18    /// Reserved encoding.
19    Reserved = 3,
20}
21
22impl From<u8> for RequestType {
23    fn from(v: u8) -> Self {
24        match (v >> 5) & 0x03 {
25            0 => RequestType::Standard,
26            1 => RequestType::Class,
27            2 => RequestType::Vendor,
28            _ => RequestType::Reserved,
29        }
30    }
31}
32
33/// `bmRequestType` bits 4:0 — who the request is addressed to.
34#[derive(Clone, Copy, PartialEq, Eq, Debug)]
35#[cfg_attr(feature = "defmt", derive(defmt::Format))]
36pub enum Recipient {
37    /// The device as a whole.
38    Device = 0,
39    /// A specific interface.
40    Interface = 1,
41    /// A specific endpoint.
42    Endpoint = 2,
43    /// Another device on the bus (hub class).
44    Other = 3,
45    /// Reserved encoding.
46    Reserved = 4,
47}
48
49impl From<u8> for Recipient {
50    fn from(v: u8) -> Self {
51        match v & 0x1f {
52            0 => Recipient::Device,
53            1 => Recipient::Interface,
54            2 => Recipient::Endpoint,
55            3 => Recipient::Other,
56            _ => Recipient::Reserved,
57        }
58    }
59}
60
61// ─── Standard request codes ───────────────────────────────────────────────────
62
63/// Standard USB request codes (`bRequest`).
64#[allow(non_snake_case)]
65#[allow(missing_docs)]
66pub mod StandardRequest {
67    pub const GET_STATUS: u8 = 0;
68    pub const CLEAR_FEATURE: u8 = 1;
69    pub const SET_FEATURE: u8 = 3;
70    pub const SET_ADDRESS: u8 = 5;
71    pub const GET_DESCRIPTOR: u8 = 6;
72    pub const SET_DESCRIPTOR: u8 = 7;
73    pub const GET_CONFIGURATION: u8 = 8;
74    pub const SET_CONFIGURATION: u8 = 9;
75    pub const GET_INTERFACE: u8 = 10;
76    pub const SET_INTERFACE: u8 = 11;
77    pub const SYNCH_FRAME: u8 = 12;
78}
79
80/// Standard feature selectors (`wValue`).
81#[allow(non_snake_case)]
82#[allow(missing_docs)]
83pub mod Feature {
84    pub const ENDPOINT_HALT: u16 = 0;
85    pub const DEVICE_REMOTE_WAKEUP: u16 = 1;
86    pub const TEST_MODE: u16 = 2;
87}
88
89// ─── Request ─────────────────────────────────────────────────────────────────
90
91/// A parsed USB SETUP packet.
92#[derive(Clone, Copy, Debug, PartialEq, Eq)]
93#[cfg_attr(feature = "defmt", derive(defmt::Format))]
94pub struct Request {
95    /// Transfer direction (from host's perspective).
96    pub direction: Direction,
97    /// Who defined this request.
98    pub request_type: RequestType,
99    /// Who the request is directed at.
100    pub recipient: Recipient,
101    /// Request code (`bRequest`).
102    pub request: u8,
103    /// Request-specific value (`wValue`).
104    pub value: u16,
105    /// Request-specific index (`wIndex`).
106    pub index: u16,
107    /// Number of bytes in the data stage (`wLength`).
108    pub length: u16,
109}
110
111impl Request {
112    /// Parse a SETUP packet from its 8-byte wire encoding.
113    pub fn parse(buf: &[u8]) -> UsbResult<Self> {
114        if buf.len() < 8 {
115            return Err(UsbError::ParseError);
116        }
117        let bm = buf[0];
118        let recipient_raw = bm & 0x1f;
119        Ok(Self {
120            direction: Direction::from(bm),
121            request_type: RequestType::from(bm),
122            recipient: if recipient_raw <= 3 {
123                Recipient::from(bm)
124            } else {
125                Recipient::Reserved
126            },
127            request: buf[1],
128            value: u16::from_le_bytes([buf[2], buf[3]]),
129            index: u16::from_le_bytes([buf[4], buf[5]]),
130            length: u16::from_le_bytes([buf[6], buf[7]]),
131        })
132    }
133
134    /// Extract (descriptor_type, descriptor_index) from `wValue` as used in
135    /// GET_DESCRIPTOR requests.
136    #[inline]
137    pub fn descriptor_type_index(&self) -> (u8, u8) {
138        ((self.value >> 8) as u8, self.value as u8)
139    }
140
141    /// Extract the endpoint address from `wIndex` for endpoint-directed requests.
142    #[inline]
143    pub fn endpoint_address(&self) -> EndpointAddress {
144        EndpointAddress::from((self.index & 0x8f) as u8)
145    }
146}