usb_stack/control/
request.rs1use crate::endpoint::{Direction, EndpointAddress};
4use crate::error::{UsbError, UsbResult};
5
6#[derive(Clone, Copy, PartialEq, Eq, Debug)]
10#[cfg_attr(feature = "defmt", derive(defmt::Format))]
11pub enum RequestType {
12 Standard = 0,
14 Class = 1,
16 Vendor = 2,
18 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#[derive(Clone, Copy, PartialEq, Eq, Debug)]
35#[cfg_attr(feature = "defmt", derive(defmt::Format))]
36pub enum Recipient {
37 Device = 0,
39 Interface = 1,
41 Endpoint = 2,
43 Other = 3,
45 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#[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#[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#[derive(Clone, Copy, Debug, PartialEq, Eq)]
93#[cfg_attr(feature = "defmt", derive(defmt::Format))]
94pub struct Request {
95 pub direction: Direction,
97 pub request_type: RequestType,
99 pub recipient: Recipient,
101 pub request: u8,
103 pub value: u16,
105 pub index: u16,
107 pub length: u16,
109}
110
111impl Request {
112 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 #[inline]
137 pub fn descriptor_type_index(&self) -> (u8, u8) {
138 ((self.value >> 8) as u8, self.value as u8)
139 }
140
141 #[inline]
143 pub fn endpoint_address(&self) -> EndpointAddress {
144 EndpointAddress::from((self.index & 0x8f) as u8)
145 }
146}