usb: nicer names for control structs.
This commit is contained in:
parent
2b547f311e
commit
bfce731982
@ -126,7 +126,7 @@ impl Request {
|
|||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||||
pub enum RequestStatus {
|
pub enum OutResponse {
|
||||||
Accepted,
|
Accepted,
|
||||||
Rejected,
|
Rejected,
|
||||||
}
|
}
|
||||||
@ -152,8 +152,8 @@ pub trait ControlHandler {
|
|||||||
///
|
///
|
||||||
/// * `req` - The request from the SETUP packet.
|
/// * `req` - The request from the SETUP packet.
|
||||||
/// * `data` - The data from the request.
|
/// * `data` - The data from the request.
|
||||||
fn control_out(&mut self, req: Request, data: &[u8]) -> RequestStatus {
|
fn control_out(&mut self, req: Request, data: &[u8]) -> OutResponse {
|
||||||
RequestStatus::Rejected
|
OutResponse::Rejected
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called when a control request is received with direction DeviceToHost.
|
/// Called when a control request is received with direction DeviceToHost.
|
||||||
@ -171,11 +171,7 @@ pub trait ControlHandler {
|
|||||||
///
|
///
|
||||||
/// * `req` - The request from the SETUP packet.
|
/// * `req` - The request from the SETUP packet.
|
||||||
/// * `control` - The control pipe.
|
/// * `control` - The control pipe.
|
||||||
fn control_in<'a>(
|
fn control_in<'a>(&mut self, req: Request, control: ControlIn<'a>) -> InResponse<'a> {
|
||||||
&mut self,
|
|
||||||
req: Request,
|
|
||||||
control: ControlIn<'a>,
|
|
||||||
) -> ControlInRequestStatus<'a> {
|
|
||||||
control.reject()
|
control.reject()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -189,14 +185,14 @@ pub struct ControlIn<'a> {
|
|||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug)]
|
#[derive(Eq, PartialEq, Debug)]
|
||||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||||
pub struct ControlInRequestStatus<'a> {
|
pub struct InResponse<'a> {
|
||||||
pub(crate) status: RequestStatus,
|
pub(crate) response: OutResponse,
|
||||||
pub(crate) data: &'a [u8],
|
pub(crate) data: &'a [u8],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ControlInRequestStatus<'a> {
|
impl<'a> InResponse<'a> {
|
||||||
pub fn status(&self) -> RequestStatus {
|
pub fn status(&self) -> OutResponse {
|
||||||
self.status
|
self.response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,22 +202,22 @@ impl<'a> ControlIn<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Accepts the transfer with the supplied buffer.
|
/// 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());
|
assert!(data.len() < self.buf.len());
|
||||||
|
|
||||||
let buf = &mut self.buf[0..data.len()];
|
let buf = &mut self.buf[0..data.len()];
|
||||||
buf.copy_from_slice(data);
|
buf.copy_from_slice(data);
|
||||||
|
|
||||||
ControlInRequestStatus {
|
InResponse {
|
||||||
status: RequestStatus::Accepted,
|
response: OutResponse::Accepted,
|
||||||
data: buf,
|
data: buf,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rejects the transfer by stalling the pipe.
|
/// Rejects the transfer by stalling the pipe.
|
||||||
pub fn reject(self) -> ControlInRequestStatus<'a> {
|
pub fn reject(self) -> InResponse<'a> {
|
||||||
ControlInRequestStatus {
|
InResponse {
|
||||||
status: RequestStatus::Rejected,
|
response: OutResponse::Rejected,
|
||||||
data: &[],
|
data: &[],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -219,8 +219,8 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
|||||||
.map(|(_, h)| h);
|
.map(|(_, h)| h);
|
||||||
match handler {
|
match handler {
|
||||||
Some(handler) => match handler.control_out(req, data) {
|
Some(handler) => match handler.control_out(req, data) {
|
||||||
RequestStatus::Accepted => return self.control.accept(),
|
OutResponse::Accepted => return self.control.accept(),
|
||||||
RequestStatus::Rejected => return self.control.reject(),
|
OutResponse::Rejected => return self.control.reject(),
|
||||||
},
|
},
|
||||||
None => self.control.reject(),
|
None => self.control.reject(),
|
||||||
}
|
}
|
||||||
@ -287,9 +287,9 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
|||||||
match handler {
|
match handler {
|
||||||
Some(handler) => {
|
Some(handler) => {
|
||||||
let resp = handler.control_in(req, ControlIn::new(&mut buf));
|
let resp = handler.control_in(req, ControlIn::new(&mut buf));
|
||||||
match resp.status {
|
match resp.response {
|
||||||
RequestStatus::Accepted => self.control.accept_in(resp.data).await,
|
OutResponse::Accepted => self.control.accept_in(resp.data).await,
|
||||||
RequestStatus::Rejected => self.control.reject(),
|
OutResponse::Rejected => self.control.reject(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => self.control.reject(),
|
None => self.control.reject(),
|
||||||
|
@ -3,9 +3,7 @@ use core::mem::{self, MaybeUninit};
|
|||||||
use core::sync::atomic::{AtomicBool, Ordering};
|
use core::sync::atomic::{AtomicBool, Ordering};
|
||||||
use defmt::info;
|
use defmt::info;
|
||||||
use embassy::blocking_mutex::CriticalSectionMutex;
|
use embassy::blocking_mutex::CriticalSectionMutex;
|
||||||
use embassy_usb::control::{
|
use embassy_usb::control::{self, ControlHandler, ControlIn, InResponse, OutResponse, Request};
|
||||||
self, ControlHandler, ControlIn, ControlInRequestStatus, Request, RequestStatus,
|
|
||||||
};
|
|
||||||
use embassy_usb::driver::{Endpoint, EndpointIn, EndpointOut, ReadError, WriteError};
|
use embassy_usb::driver::{Endpoint, EndpointIn, EndpointOut, ReadError, WriteError};
|
||||||
use embassy_usb::{driver::Driver, types::*, UsbDeviceBuilder};
|
use embassy_usb::{driver::Driver, types::*, UsbDeviceBuilder};
|
||||||
|
|
||||||
@ -88,12 +86,12 @@ impl ControlHandler for Control {
|
|||||||
shared.rts.store(false, Ordering::Relaxed);
|
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 {
|
match req.request {
|
||||||
REQ_SEND_ENCAPSULATED_COMMAND => {
|
REQ_SEND_ENCAPSULATED_COMMAND => {
|
||||||
// We don't actually support encapsulated commands but pretend we do for standards
|
// We don't actually support encapsulated commands but pretend we do for standards
|
||||||
// compatibility.
|
// compatibility.
|
||||||
RequestStatus::Accepted
|
OutResponse::Accepted
|
||||||
}
|
}
|
||||||
REQ_SET_LINE_CODING if data.len() >= 7 => {
|
REQ_SET_LINE_CODING if data.len() >= 7 => {
|
||||||
let coding = LineCoding {
|
let coding = LineCoding {
|
||||||
@ -105,7 +103,7 @@ impl ControlHandler for Control {
|
|||||||
self.shared().line_coding.lock(|x| x.set(coding));
|
self.shared().line_coding.lock(|x| x.set(coding));
|
||||||
info!("Set line coding to: {:?}", coding);
|
info!("Set line coding to: {:?}", coding);
|
||||||
|
|
||||||
RequestStatus::Accepted
|
OutResponse::Accepted
|
||||||
}
|
}
|
||||||
REQ_SET_CONTROL_LINE_STATE => {
|
REQ_SET_CONTROL_LINE_STATE => {
|
||||||
let dtr = (req.value & 0x0001) != 0;
|
let dtr = (req.value & 0x0001) != 0;
|
||||||
@ -116,17 +114,13 @@ impl ControlHandler for Control {
|
|||||||
shared.rts.store(rts, Ordering::Relaxed);
|
shared.rts.store(rts, Ordering::Relaxed);
|
||||||
info!("Set dtr {}, rts {}", dtr, rts);
|
info!("Set dtr {}, rts {}", dtr, rts);
|
||||||
|
|
||||||
RequestStatus::Accepted
|
OutResponse::Accepted
|
||||||
}
|
}
|
||||||
_ => RequestStatus::Rejected,
|
_ => OutResponse::Rejected,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn control_in<'a>(
|
fn control_in<'a>(&mut self, req: Request, control: ControlIn<'a>) -> InResponse<'a> {
|
||||||
&mut self,
|
|
||||||
req: Request,
|
|
||||||
control: ControlIn<'a>,
|
|
||||||
) -> ControlInRequestStatus<'a> {
|
|
||||||
match req.request {
|
match req.request {
|
||||||
// REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below.
|
// REQ_GET_ENCAPSULATED_COMMAND is not really supported - it will be rejected below.
|
||||||
REQ_GET_LINE_CODING if req.length == 7 => {
|
REQ_GET_LINE_CODING if req.length == 7 => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user