usb/hid: make RequestHandler::set_report(&mut self,…)

That way, it is possible to change some fields in the RequestHandler based
on outside report.
This commit is contained in:
Boris Faure 2024-04-14 15:46:19 +02:00
parent 3f4e326ec5
commit 41415fae0d

View File

@ -37,7 +37,7 @@ pub struct Config<'d> {
pub report_descriptor: &'d [u8], pub report_descriptor: &'d [u8],
/// Handler for control requests. /// Handler for control requests.
pub request_handler: Option<&'d dyn RequestHandler>, pub request_handler: Option<&'d mut dyn RequestHandler>,
/// Configures how frequently the host should poll for reading/writing HID reports. /// Configures how frequently the host should poll for reading/writing HID reports.
/// ///
@ -299,7 +299,7 @@ impl<'d, D: Driver<'d>, const N: usize> HidReader<'d, D, N> {
/// ///
/// If `use_report_ids` is true, the first byte of the report will be used as /// If `use_report_ids` is true, the first byte of the report will be used as
/// the `ReportId` value. Otherwise the `ReportId` value will be 0. /// the `ReportId` value. Otherwise the `ReportId` value will be 0.
pub async fn run<T: RequestHandler>(mut self, use_report_ids: bool, handler: &T) -> ! { pub async fn run<T: RequestHandler>(mut self, use_report_ids: bool, handler: &mut T) -> ! {
let offset = self.offset.load(Ordering::Acquire); let offset = self.offset.load(Ordering::Acquire);
assert!(offset == 0); assert!(offset == 0);
let mut buf = [0; N]; let mut buf = [0; N];
@ -384,7 +384,7 @@ pub trait RequestHandler {
} }
/// Sets the value of report `id` to `data`. /// Sets the value of report `id` to `data`.
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse { fn set_report(&mut self, id: ReportId, data: &[u8]) -> OutResponse {
let _ = (id, data); let _ = (id, data);
OutResponse::Rejected OutResponse::Rejected
} }
@ -411,7 +411,7 @@ pub trait RequestHandler {
struct Control<'d> { struct Control<'d> {
if_num: InterfaceNumber, if_num: InterfaceNumber,
report_descriptor: &'d [u8], report_descriptor: &'d [u8],
request_handler: Option<&'d dyn RequestHandler>, request_handler: Option<&'d mut dyn RequestHandler>,
out_report_offset: &'d AtomicUsize, out_report_offset: &'d AtomicUsize,
hid_descriptor: [u8; 9], hid_descriptor: [u8; 9],
} }
@ -420,7 +420,7 @@ impl<'d> Control<'d> {
fn new( fn new(
if_num: InterfaceNumber, if_num: InterfaceNumber,
report_descriptor: &'d [u8], report_descriptor: &'d [u8],
request_handler: Option<&'d dyn RequestHandler>, request_handler: Option<&'d mut dyn RequestHandler>,
out_report_offset: &'d AtomicUsize, out_report_offset: &'d AtomicUsize,
) -> Self { ) -> Self {
Control { Control {
@ -468,7 +468,7 @@ impl<'d> Handler for Control<'d> {
trace!("HID control_out {:?} {=[u8]:x}", req, data); trace!("HID control_out {:?} {=[u8]:x}", req, data);
match req.request { match req.request {
HID_REQ_SET_IDLE => { HID_REQ_SET_IDLE => {
if let Some(handler) = self.request_handler { if let Some(handler) = self.request_handler.as_mut() {
let id = req.value as u8; let id = req.value as u8;
let id = (id != 0).then_some(ReportId::In(id)); let id = (id != 0).then_some(ReportId::In(id));
let dur = u32::from(req.value >> 8); let dur = u32::from(req.value >> 8);
@ -477,7 +477,7 @@ impl<'d> Handler for Control<'d> {
} }
Some(OutResponse::Accepted) Some(OutResponse::Accepted)
} }
HID_REQ_SET_REPORT => match (ReportId::try_from(req.value), self.request_handler) { HID_REQ_SET_REPORT => match (ReportId::try_from(req.value), self.request_handler.as_mut()) {
(Ok(id), Some(handler)) => Some(handler.set_report(id, data)), (Ok(id), Some(handler)) => Some(handler.set_report(id, data)),
_ => Some(OutResponse::Rejected), _ => Some(OutResponse::Rejected),
}, },
@ -513,7 +513,7 @@ impl<'d> Handler for Control<'d> {
match req.request { match req.request {
HID_REQ_GET_REPORT => { HID_REQ_GET_REPORT => {
let size = match ReportId::try_from(req.value) { let size = match ReportId::try_from(req.value) {
Ok(id) => self.request_handler.and_then(|x| x.get_report(id, buf)), Ok(id) => self.request_handler.as_mut().and_then(|x| x.get_report(id, buf)),
Err(_) => None, Err(_) => None,
}; };
@ -524,7 +524,7 @@ impl<'d> Handler for Control<'d> {
} }
} }
HID_REQ_GET_IDLE => { HID_REQ_GET_IDLE => {
if let Some(handler) = self.request_handler { if let Some(handler) = self.request_handler.as_mut() {
let id = req.value as u8; let id = req.value as u8;
let id = (id != 0).then_some(ReportId::In(id)); let id = (id != 0).then_some(ReportId::In(id));
if let Some(dur) = handler.get_idle_ms(id) { if let Some(dur) = handler.get_idle_ms(id) {