aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-03-28 03:30:08 +0200
committerDario Nieuwenhuis <[email protected]>2022-04-06 05:38:11 +0200
commitbfce731982af1f053b1b727e49e920fc496a9546 (patch)
treec634162e1b851d1c28a2148323a66d053cc8afcd
parent2b547f311efc7feaa3afbb9f1bf4100c5502839e (diff)
usb: nicer names for control structs.
-rw-r--r--embassy-usb/src/control.rs34
-rw-r--r--embassy-usb/src/lib.rs10
-rw-r--r--examples/nrf/src/bin/usb/cdc_acm.rs20
3 files changed, 27 insertions, 37 deletions
diff --git a/embassy-usb/src/control.rs b/embassy-usb/src/control.rs
index ae4ad04a9..05536dab2 100644
--- a/embassy-usb/src/control.rs
+++ b/embassy-usb/src/control.rs
@@ -126,7 +126,7 @@ impl Request {
126 126
127#[derive(Copy, Clone, Eq, PartialEq, Debug)] 127#[derive(Copy, Clone, Eq, PartialEq, Debug)]
128#[cfg_attr(feature = "defmt", derive(defmt::Format))] 128#[cfg_attr(feature = "defmt", derive(defmt::Format))]
129pub enum RequestStatus { 129pub enum OutResponse {
130 Accepted, 130 Accepted,
131 Rejected, 131 Rejected,
132} 132}
@@ -152,8 +152,8 @@ pub trait ControlHandler {
152 /// 152 ///
153 /// * `req` - The request from the SETUP packet. 153 /// * `req` - The request from the SETUP packet.
154 /// * `data` - The data from the request. 154 /// * `data` - The data from the request.
155 fn control_out(&mut self, req: Request, data: &[u8]) -> RequestStatus { 155 fn control_out(&mut self, req: Request, data: &[u8]) -> OutResponse {
156 RequestStatus::Rejected 156 OutResponse::Rejected
157 } 157 }
158 158
159 /// Called when a control request is received with direction DeviceToHost. 159 /// Called when a control request is received with direction DeviceToHost.
@@ -171,11 +171,7 @@ pub trait ControlHandler {
171 /// 171 ///
172 /// * `req` - The request from the SETUP packet. 172 /// * `req` - The request from the SETUP packet.
173 /// * `control` - The control pipe. 173 /// * `control` - The control pipe.
174 fn control_in<'a>( 174 fn control_in<'a>(&mut self, req: Request, control: ControlIn<'a>) -> InResponse<'a> {
175 &mut self,
176 req: Request,
177 control: ControlIn<'a>,
178 ) -> ControlInRequestStatus<'a> {
179 control.reject() 175 control.reject()
180 } 176 }
181} 177}
@@ -189,14 +185,14 @@ pub struct ControlIn<'a> {
189 185
190#[derive(Eq, PartialEq, Debug)] 186#[derive(Eq, PartialEq, Debug)]
191#[cfg_attr(feature = "defmt", derive(defmt::Format))] 187#[cfg_attr(feature = "defmt", derive(defmt::Format))]
192pub struct ControlInRequestStatus<'a> { 188pub struct InResponse<'a> {
193 pub(crate) status: RequestStatus, 189 pub(crate) response: OutResponse,
194 pub(crate) data: &'a [u8], 190 pub(crate) data: &'a [u8],
195} 191}
196 192
197impl<'a> ControlInRequestStatus<'a> { 193impl<'a> InResponse<'a> {
198 pub fn status(&self) -> RequestStatus { 194 pub fn status(&self) -> OutResponse {
199 self.status 195 self.response
200 } 196 }
201} 197}
202 198
@@ -206,22 +202,22 @@ impl<'a> ControlIn<'a> {
206 } 202 }
207 203
208 /// Accepts the transfer with the supplied buffer. 204 /// Accepts the transfer with the supplied buffer.
209 pub fn accept(self, data: &[u8]) -> ControlInRequestStatus<'a> { 205 pub fn accept(self, data: &[u8]) -> InResponse<'a> {
210 assert!(data.len() < self.buf.len()); 206 assert!(data.len() < self.buf.len());
211 207
212 let buf = &mut self.buf[0..data.len()]; 208 let buf = &mut self.buf[0..data.len()];
213 buf.copy_from_slice(data); 209 buf.copy_from_slice(data);
214 210
215 ControlInRequestStatus { 211 InResponse {
216 status: RequestStatus::Accepted, 212 response: OutResponse::Accepted,
217 data: buf, 213 data: buf,
218 } 214 }
219 } 215 }
220 216
221 /// Rejects the transfer by stalling the pipe. 217 /// Rejects the transfer by stalling the pipe.
222 pub fn reject(self) -> ControlInRequestStatus<'a> { 218 pub fn reject(self) -> InResponse<'a> {
223 ControlInRequestStatus { 219 InResponse {
224 status: RequestStatus::Rejected, 220 response: OutResponse::Rejected,
225 data: &[], 221 data: &[],
226 } 222 }
227 } 223 }
diff --git a/embassy-usb/src/lib.rs b/embassy-usb/src/lib.rs
index b6c95ac62..8d2024026 100644
--- a/embassy-usb/src/lib.rs
+++ b/embassy-usb/src/lib.rs
@@ -219,8 +219,8 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
219 .map(|(_, h)| h); 219 .map(|(_, h)| h);
220 match handler { 220 match handler {
221 Some(handler) => match handler.control_out(req, data) { 221 Some(handler) => match handler.control_out(req, data) {
222 RequestStatus::Accepted => return self.control.accept(), 222 OutResponse::Accepted => return self.control.accept(),
223 RequestStatus::Rejected => return self.control.reject(), 223 OutResponse::Rejected => return self.control.reject(),
224 }, 224 },
225 None => self.control.reject(), 225 None => self.control.reject(),
226 } 226 }
@@ -287,9 +287,9 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
287 match handler { 287 match handler {
288 Some(handler) => { 288 Some(handler) => {
289 let resp = handler.control_in(req, ControlIn::new(&mut buf)); 289 let resp = handler.control_in(req, ControlIn::new(&mut buf));
290 match resp.status { 290 match resp.response {
291 RequestStatus::Accepted => self.control.accept_in(resp.data).await, 291 OutResponse::Accepted => self.control.accept_in(resp.data).await,
292 RequestStatus::Rejected => self.control.reject(), 292 OutResponse::Rejected => self.control.reject(),
293 } 293 }
294 } 294 }
295 None => self.control.reject(), 295 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 25c3108a9..141c6ecd1 100644
--- a/examples/nrf/src/bin/usb/cdc_acm.rs
+++ b/examples/nrf/src/bin/usb/cdc_acm.rs
@@ -3,9 +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::{ 6use embassy_usb::control::{self, ControlHandler, ControlIn, InResponse, OutResponse, Request};
7 self, ControlHandler, ControlIn, ControlInRequestStatus, Request, RequestStatus,
8};
9use embassy_usb::driver::{Endpoint, EndpointIn, EndpointOut, ReadError, WriteError}; 7use embassy_usb::driver::{Endpoint, EndpointIn, EndpointOut, ReadError, WriteError};
10use embassy_usb::{driver::Driver, types::*, UsbDeviceBuilder}; 8use embassy_usb::{driver::Driver, types::*, UsbDeviceBuilder};
11 9
@@ -88,12 +86,12 @@ impl ControlHandler for Control {
88 shared.rts.store(false, Ordering::Relaxed); 86 shared.rts.store(false, Ordering::Relaxed);
89 } 87 }
90 88
91 fn control_out(&mut self, req: control::Request, data: &[u8]) -> RequestStatus { 89 fn control_out(&mut self, req: control::Request, data: &[u8]) -> OutResponse {
92 match req.request { 90 match req.request {
93 REQ_SEND_ENCAPSULATED_COMMAND => { 91 REQ_SEND_ENCAPSULATED_COMMAND => {
94 // We don't actually support encapsulated commands but pretend we do for standards 92 // We don't actually support encapsulated commands but pretend we do for standards
95 // compatibility. 93 // compatibility.
96 RequestStatus::Accepted 94 OutResponse::Accepted
97 } 95 }
98 REQ_SET_LINE_CODING if data.len() >= 7 => { 96 REQ_SET_LINE_CODING if data.len() >= 7 => {
99 let coding = LineCoding { 97 let coding = LineCoding {
@@ -105,7 +103,7 @@ impl ControlHandler for Control {
105 self.shared().line_coding.lock(|x| x.set(coding)); 103 self.shared().line_coding.lock(|x| x.set(coding));
106 info!("Set line coding to: {:?}", coding); 104 info!("Set line coding to: {:?}", coding);
107 105
108 RequestStatus::Accepted 106 OutResponse::Accepted
109 } 107 }
110 REQ_SET_CONTROL_LINE_STATE => { 108 REQ_SET_CONTROL_LINE_STATE => {
111 let dtr = (req.value & 0x0001) != 0; 109 let dtr = (req.value & 0x0001) != 0;
@@ -116,17 +114,13 @@ impl ControlHandler for Control {
116 shared.rts.store(rts, Ordering::Relaxed); 114 shared.rts.store(rts, Ordering::Relaxed);
117 info!("Set dtr {}, rts {}", dtr, rts); 115 info!("Set dtr {}, rts {}", dtr, rts);
118 116
119 RequestStatus::Accepted 117 OutResponse::Accepted
120 } 118 }
121 _ => RequestStatus::Rejected, 119 _ => OutResponse::Rejected,
122 } 120 }
123 } 121 }
124 122
125 fn control_in<'a>( 123 fn control_in<'a>(&mut self, req: Request, control: ControlIn<'a>) -> InResponse<'a> {
126 &mut self,
127 req: Request,
128 control: ControlIn<'a>,
129 ) -> ControlInRequestStatus<'a> {
130 match req.request { 124 match req.request {
131 // 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.
132 REQ_GET_LINE_CODING if req.length == 7 => { 126 REQ_GET_LINE_CODING if req.length == 7 => {