usb: nicer names for control structs.

This commit is contained in:
Dario Nieuwenhuis 2022-03-28 03:30:08 +02:00
parent 2b547f311e
commit bfce731982
3 changed files with 27 additions and 37 deletions

View File

@ -126,7 +126,7 @@ impl Request {
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum RequestStatus {
pub enum OutResponse {
Accepted,
Rejected,
}
@ -152,8 +152,8 @@ pub trait ControlHandler {
///
/// * `req` - The request from the SETUP packet.
/// * `data` - The data from the request.
fn control_out(&mut self, req: Request, data: &[u8]) -> RequestStatus {
RequestStatus::Rejected
fn control_out(&mut self, req: Request, data: &[u8]) -> OutResponse {
OutResponse::Rejected
}
/// Called when a control request is received with direction DeviceToHost.
@ -171,11 +171,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>,
) -> ControlInRequestStatus<'a> {
fn control_in<'a>(&mut self, req: Request, control: ControlIn<'a>) -> InResponse<'a> {
control.reject()
}
}
@ -189,14 +185,14 @@ pub struct ControlIn<'a> {
#[derive(Eq, PartialEq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ControlInRequestStatus<'a> {
pub(crate) status: RequestStatus,
pub struct InResponse<'a> {
pub(crate) response: OutResponse,
pub(crate) data: &'a [u8],
}
impl<'a> ControlInRequestStatus<'a> {
pub fn status(&self) -> RequestStatus {
self.status
impl<'a> InResponse<'a> {
pub fn status(&self) -> OutResponse {
self.response
}
}
@ -206,22 +202,22 @@ impl<'a> ControlIn<'a> {
}
/// Accepts the transfer with the supplied buffer.
pub fn accept(self, data: &[u8]) -> ControlInRequestStatus<'a> {
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);
ControlInRequestStatus {
status: RequestStatus::Accepted,
InResponse {
response: OutResponse::Accepted,
data: buf,
}
}
/// Rejects the transfer by stalling the pipe.
pub fn reject(self) -> ControlInRequestStatus<'a> {
ControlInRequestStatus {
status: RequestStatus::Rejected,
pub fn reject(self) -> InResponse<'a> {
InResponse {
response: OutResponse::Rejected,
data: &[],
}
}

View File

@ -219,8 +219,8 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
.map(|(_, h)| h);
match handler {
Some(handler) => match handler.control_out(req, data) {
RequestStatus::Accepted => return self.control.accept(),
RequestStatus::Rejected => return self.control.reject(),
OutResponse::Accepted => return self.control.accept(),
OutResponse::Rejected => return self.control.reject(),
},
None => self.control.reject(),
}
@ -287,9 +287,9 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
match handler {
Some(handler) => {
let resp = handler.control_in(req, ControlIn::new(&mut buf));
match resp.status {
RequestStatus::Accepted => self.control.accept_in(resp.data).await,
RequestStatus::Rejected => self.control.reject(),
match resp.response {
OutResponse::Accepted => self.control.accept_in(resp.data).await,
OutResponse::Rejected => self.control.reject(),
}
}
None => self.control.reject(),

View File

@ -3,9 +3,7 @@ use core::mem::{self, MaybeUninit};
use core::sync::atomic::{AtomicBool, Ordering};
use defmt::info;
use embassy::blocking_mutex::CriticalSectionMutex;
use embassy_usb::control::{
self, ControlHandler, ControlIn, ControlInRequestStatus, Request, RequestStatus,
};
use embassy_usb::control::{self, ControlHandler, ControlIn, InResponse, OutResponse, Request};
use embassy_usb::driver::{Endpoint, EndpointIn, EndpointOut, ReadError, WriteError};
use embassy_usb::{driver::Driver, types::*, UsbDeviceBuilder};
@ -88,12 +86,12 @@ impl ControlHandler for Control {
shared.rts.store(false, Ordering::Relaxed);
}
fn control_out(&mut self, req: control::Request, data: &[u8]) -> RequestStatus {
fn control_out(&mut self, req: control::Request, data: &[u8]) -> OutResponse {
match req.request {
REQ_SEND_ENCAPSULATED_COMMAND => {
// We don't actually support encapsulated commands but pretend we do for standards
// compatibility.
RequestStatus::Accepted
OutResponse::Accepted
}
REQ_SET_LINE_CODING if data.len() >= 7 => {
let coding = LineCoding {
@ -105,7 +103,7 @@ impl ControlHandler for Control {
self.shared().line_coding.lock(|x| x.set(coding));
info!("Set line coding to: {:?}", coding);
RequestStatus::Accepted
OutResponse::Accepted
}
REQ_SET_CONTROL_LINE_STATE => {
let dtr = (req.value & 0x0001) != 0;
@ -116,17 +114,13 @@ impl ControlHandler for Control {
shared.rts.store(rts, Ordering::Relaxed);
info!("Set dtr {}, rts {}", dtr, rts);
RequestStatus::Accepted
OutResponse::Accepted
}
_ => RequestStatus::Rejected,
_ => OutResponse::Rejected,
}
}
fn control_in<'a>(
&mut self,
req: Request,
control: ControlIn<'a>,
) -> ControlInRequestStatus<'a> {
fn control_in<'a>(&mut self, req: Request, control: ControlIn<'a>) -> InResponse<'a> {
match req.request {
// REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below.
REQ_GET_LINE_CODING if req.length == 7 => {