aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb/src/control.rs
diff options
context:
space:
mode:
authoralexmoon <[email protected]>2022-03-25 16:46:14 -0400
committerDario Nieuwenhuis <[email protected]>2022-04-06 05:38:11 +0200
commitbdc6e0481c42d20d5cca19dfc8ec56306e47296e (patch)
tree6beb805dd6ffea30877b654aa42e5c83f5a36c0b /embassy-usb/src/control.rs
parent5c0db627feae071182dd9978ffb56b0524558d93 (diff)
Add support for USB classes handling control requests.
Diffstat (limited to 'embassy-usb/src/control.rs')
-rw-r--r--embassy-usb/src/control.rs17
1 files changed, 4 insertions, 13 deletions
diff --git a/embassy-usb/src/control.rs b/embassy-usb/src/control.rs
index f1148ac76..77bc10aa4 100644
--- a/embassy-usb/src/control.rs
+++ b/embassy-usb/src/control.rs
@@ -2,12 +2,6 @@ use core::mem;
2 2
3use super::types::*; 3use super::types::*;
4 4
5#[derive(Debug, PartialEq, Eq, Clone, Copy)]
6#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7pub enum ParseError {
8 InvalidLength,
9}
10
11/// Control request type. 5/// Control request type.
12#[repr(u8)] 6#[repr(u8)]
13#[derive(Copy, Clone, Eq, PartialEq, Debug)] 7#[derive(Copy, Clone, Eq, PartialEq, Debug)]
@@ -104,15 +98,12 @@ impl Request {
104 /// Standard USB feature Device Remote Wakeup for Set/Clear Feature 98 /// Standard USB feature Device Remote Wakeup for Set/Clear Feature
105 pub const FEATURE_DEVICE_REMOTE_WAKEUP: u16 = 1; 99 pub const FEATURE_DEVICE_REMOTE_WAKEUP: u16 = 1;
106 100
107 pub(crate) fn parse(buf: &[u8]) -> Result<Request, ParseError> { 101 /// Parses a USB control request from a byte array.
108 if buf.len() != 8 { 102 pub fn parse(buf: &[u8; 8]) -> Request {
109 return Err(ParseError::InvalidLength);
110 }
111
112 let rt = buf[0]; 103 let rt = buf[0];
113 let recipient = rt & 0b11111; 104 let recipient = rt & 0b11111;
114 105
115 Ok(Request { 106 Request {
116 direction: rt.into(), 107 direction: rt.into(),
117 request_type: unsafe { mem::transmute((rt >> 5) & 0b11) }, 108 request_type: unsafe { mem::transmute((rt >> 5) & 0b11) },
118 recipient: if recipient <= 3 { 109 recipient: if recipient <= 3 {
@@ -124,7 +115,7 @@ impl Request {
124 value: (buf[2] as u16) | ((buf[3] as u16) << 8), 115 value: (buf[2] as u16) | ((buf[3] as u16) << 8),
125 index: (buf[4] as u16) | ((buf[5] as u16) << 8), 116 index: (buf[4] as u16) | ((buf[5] as u16) << 8),
126 length: (buf[6] as u16) | ((buf[7] as u16) << 8), 117 length: (buf[6] as u16) | ((buf[7] as u16) << 8),
127 }) 118 }
128 } 119 }
129 120
130 /// Gets the descriptor type and index from the value field of a GET_DESCRIPTOR request. 121 /// Gets the descriptor type and index from the value field of a GET_DESCRIPTOR request.