Switch to ControlHandler owned bufs for control_in()
This commit is contained in:
parent
a22639ad92
commit
c53bb7394a
@ -133,8 +133,8 @@ pub enum OutResponse {
|
|||||||
|
|
||||||
#[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 InResponse {
|
pub enum InResponse<'a> {
|
||||||
Accepted(usize),
|
Accepted(&'a [u8]),
|
||||||
Rejected,
|
Rejected,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,8 +164,7 @@ pub trait ControlHandler {
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `req` - The request from the SETUP packet.
|
/// * `req` - The request from the SETUP packet.
|
||||||
/// * `resp` - The buffer for you to write the response.
|
fn control_in(&mut self, req: Request) -> InResponse<'_> {
|
||||||
fn control_in(&mut self, req: Request, resp: &mut [u8]) -> InResponse {
|
|
||||||
InResponse::Rejected
|
InResponse::Rejected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -284,13 +284,10 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
|||||||
.find(|(i, _)| req.index == *i as _)
|
.find(|(i, _)| req.index == *i as _)
|
||||||
.map(|(_, h)| h);
|
.map(|(_, h)| h);
|
||||||
match handler {
|
match handler {
|
||||||
Some(handler) => {
|
Some(handler) => match handler.control_in(req) {
|
||||||
let mut buf = [0; 128];
|
InResponse::Accepted(data) => self.control.accept_in(data).await,
|
||||||
match handler.control_in(req, &mut buf) {
|
InResponse::Rejected => self.control.reject(),
|
||||||
InResponse::Accepted(len) => self.control.accept_in(&buf[..len]).await,
|
},
|
||||||
InResponse::Rejected => self.control.reject(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => self.control.reject(),
|
None => self.control.reject(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,7 @@ pub struct CdcAcmClass<'d, D: Driver<'d>> {
|
|||||||
|
|
||||||
struct Control<'a> {
|
struct Control<'a> {
|
||||||
shared: &'a ControlShared,
|
shared: &'a ControlShared,
|
||||||
|
buf: [u8; 7],
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shared data between Control and CdcAcmClass
|
/// Shared data between Control and CdcAcmClass
|
||||||
@ -138,17 +139,17 @@ impl<'a> ControlHandler for Control<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn control_in(&mut self, req: Request, resp: &mut [u8]) -> InResponse {
|
fn control_in(&mut self, req: Request) -> InResponse<'_> {
|
||||||
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 => {
|
||||||
info!("Sending line coding");
|
info!("Sending line coding");
|
||||||
let coding = self.shared().line_coding.lock(|x| x.get());
|
let coding = self.shared().line_coding.lock(|x| x.get());
|
||||||
resp[0..4].copy_from_slice(&coding.data_rate.to_le_bytes());
|
self.buf[0..4].copy_from_slice(&coding.data_rate.to_le_bytes());
|
||||||
resp[4] = coding.stop_bits as u8;
|
self.buf[4] = coding.stop_bits as u8;
|
||||||
resp[5] = coding.parity_type as u8;
|
self.buf[5] = coding.parity_type as u8;
|
||||||
resp[6] = coding.data_bits;
|
self.buf[6] = coding.data_bits;
|
||||||
InResponse::Accepted(7)
|
InResponse::Accepted(&self.buf)
|
||||||
}
|
}
|
||||||
_ => InResponse::Rejected,
|
_ => InResponse::Rejected,
|
||||||
}
|
}
|
||||||
@ -165,6 +166,7 @@ impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
let control = state.control.write(Control {
|
let control = state.control.write(Control {
|
||||||
shared: &state.shared,
|
shared: &state.shared,
|
||||||
|
buf: [0; 7],
|
||||||
});
|
});
|
||||||
|
|
||||||
let control_shared = &state.shared;
|
let control_shared = &state.shared;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user