aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-usb/src/control.rs58
-rw-r--r--embassy-usb/src/lib.rs9
-rw-r--r--examples/nrf/src/bin/usb/cdc_acm.rs17
3 files changed, 21 insertions, 63 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(),
diff --git a/examples/nrf/src/bin/usb/cdc_acm.rs b/examples/nrf/src/bin/usb/cdc_acm.rs
index 141c6ecd1..4be35fd3f 100644
--- a/examples/nrf/src/bin/usb/cdc_acm.rs
+++ b/examples/nrf/src/bin/usb/cdc_acm.rs
@@ -3,7 +3,7 @@ use core::mem::{self, MaybeUninit};
3use core::sync::atomic::{AtomicBool, Ordering}; 3use core::sync::atomic::{AtomicBool, Ordering};
4use defmt::info; 4use defmt::info;
5use embassy::blocking_mutex::CriticalSectionMutex; 5use embassy::blocking_mutex::CriticalSectionMutex;
6use embassy_usb::control::{self, ControlHandler, ControlIn, InResponse, OutResponse, Request}; 6use embassy_usb::control::{self, ControlHandler, InResponse, OutResponse, Request};
7use embassy_usb::driver::{Endpoint, EndpointIn, EndpointOut, ReadError, WriteError}; 7use embassy_usb::driver::{Endpoint, EndpointIn, EndpointOut, ReadError, WriteError};
8use embassy_usb::{driver::Driver, types::*, UsbDeviceBuilder}; 8use embassy_usb::{driver::Driver, types::*, UsbDeviceBuilder};
9 9
@@ -120,20 +120,19 @@ impl ControlHandler for Control {
120 } 120 }
121 } 121 }
122 122
123 fn control_in<'a>(&mut self, req: Request, control: ControlIn<'a>) -> InResponse<'a> { 123 fn control_in(&mut self, req: Request, resp: &mut [u8]) -> InResponse {
124 match req.request { 124 match req.request {
125 // REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below. 125 // REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below.
126 REQ_GET_LINE_CODING if req.length == 7 => { 126 REQ_GET_LINE_CODING if req.length == 7 => {
127 info!("Sending line coding"); 127 info!("Sending line coding");
128 let coding = self.shared().line_coding.lock(|x| x.get()); 128 let coding = self.shared().line_coding.lock(|x| x.get());
129 let mut data = [0; 7]; 129 resp[0..4].copy_from_slice(&coding.data_rate.to_le_bytes());
130 data[0..4].copy_from_slice(&coding.data_rate.to_le_bytes()); 130 resp[4] = coding.stop_bits as u8;
131 data[4] = coding.stop_bits as u8; 131 resp[5] = coding.parity_type as u8;
132 data[5] = coding.parity_type as u8; 132 resp[6] = coding.data_bits;
133 data[6] = coding.data_bits; 133 InResponse::Accepted(7)
134 control.accept(&data)
135 } 134 }
136 _ => control.reject(), 135 _ => InResponse::Rejected,
137 } 136 }
138 } 137 }
139} 138}