From e99a3a1da42649bb9ad9a64508d14d6461be331d Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 28 Mar 2022 03:34:24 +0200 Subject: usb: simplify buffer handling for Control IN transfers. --- embassy-usb/src/control.rs | 58 +++++++--------------------------------------- embassy-usb/src/lib.rs | 9 ++++--- 2 files changed, 13 insertions(+), 54 deletions(-) (limited to 'embassy-usb/src') 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 { Rejected, } +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +pub enum InResponse { + Accepted(usize), + Rejected, +} + /// A trait for implementing USB classes. /// /// All methods are optional callbacks that will be called by @@ -171,54 +178,7 @@ pub trait ControlHandler { /// /// * `req` - The request from the SETUP packet. /// * `control` - The control pipe. - fn control_in<'a>(&mut self, req: Request, control: ControlIn<'a>) -> InResponse<'a> { - control.reject() - } -} - -/// Handle for a control IN transfer. When implementing a class, use the methods of this object to -/// response to the transfer with either data or an error (STALL condition). To ignore the request -/// and pass it on to the next class, call [`Self::ignore()`]. -pub struct ControlIn<'a> { - buf: &'a mut [u8], -} - -#[derive(Eq, PartialEq, Debug)] -#[cfg_attr(feature = "defmt", derive(defmt::Format))] -pub struct InResponse<'a> { - pub(crate) response: OutResponse, - pub(crate) data: &'a [u8], -} - -impl<'a> InResponse<'a> { - pub fn status(&self) -> OutResponse { - self.response - } -} - -impl<'a> ControlIn<'a> { - pub(crate) fn new(buf: &'a mut [u8]) -> Self { - ControlIn { buf } - } - - /// Accepts the transfer with the supplied buffer. - pub fn accept(self, data: &[u8]) -> InResponse<'a> { - assert!(data.len() < self.buf.len()); - - let buf = &mut self.buf[0..data.len()]; - buf.copy_from_slice(data); - - InResponse { - response: OutResponse::Accepted, - data: buf, - } - } - - /// Rejects the transfer by stalling the pipe. - pub fn reject(self) -> InResponse<'a> { - InResponse { - response: OutResponse::Rejected, - data: &[], - } + fn control_in(&mut self, req: Request, resp: &mut [u8]) -> InResponse { + InResponse::Rejected } } 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> { _ => self.control.reject(), }, (RequestType::Class, Recipient::Interface) => { - let mut buf = [0; 128]; let handler = self .interfaces .iter_mut() @@ -286,10 +285,10 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> { .map(|(_, h)| h); match handler { Some(handler) => { - let resp = handler.control_in(req, ControlIn::new(&mut buf)); - match resp.response { - OutResponse::Accepted => self.control.accept_in(resp.data).await, - OutResponse::Rejected => self.control.reject(), + let mut buf = [0; 128]; + match handler.control_in(req, &mut buf) { + InResponse::Accepted(len) => self.control.accept_in(&buf[..len]).await, + InResponse::Rejected => self.control.reject(), } } None => self.control.reject(), -- cgit