aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-03-28 03:34:24 +0200
committerDario Nieuwenhuis <[email protected]>2022-04-06 05:38:11 +0200
commite99a3a1da42649bb9ad9a64508d14d6461be331d (patch)
treeeb28a76b0d0d08710ce1a44de58fd9a92209fee6 /embassy-usb
parentbfce731982af1f053b1b727e49e920fc496a9546 (diff)
usb: simplify buffer handling for Control IN transfers.
Diffstat (limited to 'embassy-usb')
-rw-r--r--embassy-usb/src/control.rs58
-rw-r--r--embassy-usb/src/lib.rs9
2 files changed, 13 insertions, 54 deletions
diff --git a/embassy-usb/src/control.rs b/embassy-usb/src/control.rs
index 05536dab2..567a595a1 100644
--- a/embassy-usb/src/control.rs
+++ b/embassy-usb/src/control.rs
@@ -131,6 +131,13 @@ pub enum OutResponse {
131 Rejected, 131 Rejected,
132} 132}
133 133
134#[derive(Copy, Clone, Eq, PartialEq, Debug)]
135#[cfg_attr(feature = "defmt", derive(defmt::Format))]
136pub enum InResponse {
137 Accepted(usize),
138 Rejected,
139}
140
134/// A trait for implementing USB classes. 141/// A trait for implementing USB classes.
135/// 142///
136/// All methods are optional callbacks that will be called by 143/// All methods are optional callbacks that will be called by
@@ -171,54 +178,7 @@ pub trait ControlHandler {
171 /// 178 ///
172 /// * `req` - The request from the SETUP packet. 179 /// * `req` - The request from the SETUP packet.
173 /// * `control` - The control pipe. 180 /// * `control` - The control pipe.
174 fn control_in<'a>(&mut self, req: Request, control: ControlIn<'a>) -> InResponse<'a> { 181 fn control_in(&mut self, req: Request, resp: &mut [u8]) -> InResponse {
175 control.reject() 182 InResponse::Rejected
176 }
177}
178
179/// Handle for a control IN transfer. When implementing a class, use the methods of this object to
180/// response to the transfer with either data or an error (STALL condition). To ignore the request
181/// and pass it on to the next class, call [`Self::ignore()`].
182pub struct ControlIn<'a> {
183 buf: &'a mut [u8],
184}
185
186#[derive(Eq, PartialEq, Debug)]
187#[cfg_attr(feature = "defmt", derive(defmt::Format))]
188pub struct InResponse<'a> {
189 pub(crate) response: OutResponse,
190 pub(crate) data: &'a [u8],
191}
192
193impl<'a> InResponse<'a> {
194 pub fn status(&self) -> OutResponse {
195 self.response
196 }
197}
198
199impl<'a> ControlIn<'a> {
200 pub(crate) fn new(buf: &'a mut [u8]) -> Self {
201 ControlIn { buf }
202 }
203
204 /// Accepts the transfer with the supplied buffer.
205 pub fn accept(self, data: &[u8]) -> InResponse<'a> {
206 assert!(data.len() < self.buf.len());
207
208 let buf = &mut self.buf[0..data.len()];
209 buf.copy_from_slice(data);
210
211 InResponse {
212 response: OutResponse::Accepted,
213 data: buf,
214 }
215 }
216
217 /// Rejects the transfer by stalling the pipe.
218 pub fn reject(self) -> InResponse<'a> {
219 InResponse {
220 response: OutResponse::Rejected,
221 data: &[],
222 }
223 } 183 }
224} 184}
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs
index 8d2024026..9ac55db78 100644
--- a/embassy-usb/src/lib.rs
+++ b/embassy-usb/src/lib.rs
@@ -278,7 +278,6 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
278 _ => self.control.reject(), 278 _ => self.control.reject(),
279 }, 279 },
280 (RequestType::Class, Recipient::Interface) => { 280 (RequestType::Class, Recipient::Interface) => {
281 let mut buf = [0; 128];
282 let handler = self 281 let handler = self
283 .interfaces 282 .interfaces
284 .iter_mut() 283 .iter_mut()
@@ -286,10 +285,10 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
286 .map(|(_, h)| h); 285 .map(|(_, h)| h);
287 match handler { 286 match handler {
288 Some(handler) => { 287 Some(handler) => {
289 let resp = handler.control_in(req, ControlIn::new(&mut buf)); 288 let mut buf = [0; 128];
290 match resp.response { 289 match handler.control_in(req, &mut buf) {
291 OutResponse::Accepted => self.control.accept_in(resp.data).await, 290 InResponse::Accepted(len) => self.control.accept_in(&buf[..len]).await,
292 OutResponse::Rejected => self.control.reject(), 291 InResponse::Rejected => self.control.reject(),
293 } 292 }
294 } 293 }
295 None => self.control.reject(), 294 None => self.control.reject(),