Merge pull request #2812 from borisfaure/mut-hid-request-handler

usb::hid::RequestHandler::set_report(&mut self, ...)
This commit is contained in:
Dario Nieuwenhuis 2024-04-23 17:42:56 +00:00 committed by GitHub
commit 4d4cbc0dd3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 58 additions and 58 deletions

View File

@ -37,7 +37,7 @@ pub struct Config<'d> {
pub report_descriptor: &'d [u8],
/// 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.
///
@ -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
/// 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);
assert!(offset == 0);
let mut buf = [0; N];
@ -378,13 +378,13 @@ pub trait RequestHandler {
/// Reads the value of report `id` into `buf` returning the size.
///
/// Returns `None` if `id` is invalid or no data is available.
fn get_report(&self, id: ReportId, buf: &mut [u8]) -> Option<usize> {
fn get_report(&mut self, id: ReportId, buf: &mut [u8]) -> Option<usize> {
let _ = (id, buf);
None
}
/// 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);
OutResponse::Rejected
}
@ -394,7 +394,7 @@ pub trait RequestHandler {
/// If `id` is `None`, get the idle rate for all reports. Returning `None`
/// will reject the control request. Any duration at or above 1.024 seconds
/// or below 4ms will be returned as an indefinite idle rate.
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
fn get_idle_ms(&mut self, id: Option<ReportId>) -> Option<u32> {
let _ = id;
None
}
@ -403,7 +403,7 @@ pub trait RequestHandler {
///
/// If `id` is `None`, set the idle rate of all input reports to `dur`. If
/// an indefinite duration is requested, `dur` will be set to `u32::MAX`.
fn set_idle_ms(&self, id: Option<ReportId>, duration_ms: u32) {
fn set_idle_ms(&mut self, id: Option<ReportId>, duration_ms: u32) {
let _ = (id, duration_ms);
}
}
@ -411,7 +411,7 @@ pub trait RequestHandler {
struct Control<'d> {
if_num: InterfaceNumber,
report_descriptor: &'d [u8],
request_handler: Option<&'d dyn RequestHandler>,
request_handler: Option<&'d mut dyn RequestHandler>,
out_report_offset: &'d AtomicUsize,
hid_descriptor: [u8; 9],
}
@ -420,7 +420,7 @@ impl<'d> Control<'d> {
fn new(
if_num: InterfaceNumber,
report_descriptor: &'d [u8],
request_handler: Option<&'d dyn RequestHandler>,
request_handler: Option<&'d mut dyn RequestHandler>,
out_report_offset: &'d AtomicUsize,
) -> Self {
Control {
@ -468,7 +468,7 @@ impl<'d> Handler for Control<'d> {
trace!("HID control_out {:?} {=[u8]:x}", req, data);
match req.request {
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 = (id != 0).then_some(ReportId::In(id));
let dur = u32::from(req.value >> 8);
@ -477,7 +477,7 @@ impl<'d> Handler for Control<'d> {
}
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)),
_ => Some(OutResponse::Rejected),
},
@ -513,7 +513,7 @@ impl<'d> Handler for Control<'d> {
match req.request {
HID_REQ_GET_REPORT => {
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,
};
@ -524,7 +524,7 @@ impl<'d> Handler for Control<'d> {
}
}
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 = (id != 0).then_some(ReportId::In(id));
if let Some(dur) = handler.get_idle_ms(id) {

View File

@ -54,7 +54,7 @@ async fn main(_spawner: Spawner) {
let mut bos_descriptor = [0; 256];
let mut msos_descriptor = [0; 256];
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let mut request_handler = MyRequestHandler {};
let mut device_handler = MyDeviceHandler::new();
let mut state = State::new();
@ -73,7 +73,7 @@ async fn main(_spawner: Spawner) {
// Create classes on the builder.
let config = embassy_usb::class::hid::Config {
report_descriptor: KeyboardReport::desc(),
request_handler: Some(&request_handler),
request_handler: None,
poll_ms: 60,
max_packet_size: 64,
};
@ -137,7 +137,7 @@ async fn main(_spawner: Spawner) {
};
let out_fut = async {
reader.run(false, &request_handler).await;
reader.run(false, &mut request_handler).await;
};
// Run everything concurrently.
@ -148,21 +148,21 @@ async fn main(_spawner: Spawner) {
struct MyRequestHandler {}
impl RequestHandler for MyRequestHandler {
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
fn get_report(&mut self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
info!("Get report for {:?}", id);
None
}
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
fn set_report(&mut self, id: ReportId, data: &[u8]) -> OutResponse {
info!("Set report for {:?}: {=[u8]}", id, data);
OutResponse::Accepted
}
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) {
info!("Set idle rate for {:?} to {:?}", id, dur);
}
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
fn get_idle_ms(&mut self, id: Option<ReportId>) -> Option<u32> {
info!("Get idle rate for {:?}", id);
None
}

View File

@ -47,7 +47,7 @@ async fn main(_spawner: Spawner) {
let mut bos_descriptor = [0; 256];
let mut msos_descriptor = [0; 256];
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let mut request_handler = MyRequestHandler {};
let mut state = State::new();
@ -63,7 +63,7 @@ async fn main(_spawner: Spawner) {
// Create classes on the builder.
let config = embassy_usb::class::hid::Config {
report_descriptor: MouseReport::desc(),
request_handler: Some(&request_handler),
request_handler: Some(&mut request_handler),
poll_ms: 60,
max_packet_size: 8,
};
@ -105,21 +105,21 @@ async fn main(_spawner: Spawner) {
struct MyRequestHandler {}
impl RequestHandler for MyRequestHandler {
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
fn get_report(&mut self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
info!("Get report for {:?}", id);
None
}
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
fn set_report(&mut self, id: ReportId, data: &[u8]) -> OutResponse {
info!("Set report for {:?}: {=[u8]}", id, data);
OutResponse::Accepted
}
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) {
info!("Set idle rate for {:?} to {:?}", id, dur);
}
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
fn get_idle_ms(&mut self, id: Option<ReportId>) -> Option<u32> {
info!("Get idle rate for {:?}", id);
None
}

View File

@ -41,7 +41,7 @@ async fn main(_spawner: Spawner) {
// You can also add a Microsoft OS descriptor.
let mut msos_descriptor = [0; 256];
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let mut request_handler = MyRequestHandler {};
let mut device_handler = MyDeviceHandler::new();
let mut state = State::new();
@ -60,7 +60,7 @@ async fn main(_spawner: Spawner) {
// Create classes on the builder.
let config = embassy_usb::class::hid::Config {
report_descriptor: KeyboardReport::desc(),
request_handler: Some(&request_handler),
request_handler: None,
poll_ms: 60,
max_packet_size: 64,
};
@ -114,7 +114,7 @@ async fn main(_spawner: Spawner) {
};
let out_fut = async {
reader.run(false, &request_handler).await;
reader.run(false, &mut request_handler).await;
};
// Run everything concurrently.
@ -125,21 +125,21 @@ async fn main(_spawner: Spawner) {
struct MyRequestHandler {}
impl RequestHandler for MyRequestHandler {
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
fn get_report(&mut self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
info!("Get report for {:?}", id);
None
}
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
fn set_report(&mut self, id: ReportId, data: &[u8]) -> OutResponse {
info!("Set report for {:?}: {=[u8]}", id, data);
OutResponse::Accepted
}
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) {
info!("Set idle rate for {:?} to {:?}", id, dur);
}
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
fn get_idle_ms(&mut self, id: Option<ReportId>) -> Option<u32> {
info!("Get idle rate for {:?}", id);
None
}

View File

@ -44,7 +44,7 @@ async fn main(_spawner: Spawner) {
// You can also add a Microsoft OS descriptor.
let mut msos_descriptor = [0; 256];
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let mut request_handler = MyRequestHandler {};
let mut device_handler = MyDeviceHandler::new();
let mut state = State::new();
@ -63,7 +63,7 @@ async fn main(_spawner: Spawner) {
// Create classes on the builder.
let config = embassy_usb::class::hid::Config {
report_descriptor: MouseReport::desc(),
request_handler: Some(&request_handler),
request_handler: None,
poll_ms: 60,
max_packet_size: 64,
};
@ -106,7 +106,7 @@ async fn main(_spawner: Spawner) {
};
let out_fut = async {
reader.run(false, &request_handler).await;
reader.run(false, &mut request_handler).await;
};
// Run everything concurrently.
@ -117,21 +117,21 @@ async fn main(_spawner: Spawner) {
struct MyRequestHandler {}
impl RequestHandler for MyRequestHandler {
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
fn get_report(&mut self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
info!("Get report for {:?}", id);
None
}
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
fn set_report(&mut self, id: ReportId, data: &[u8]) -> OutResponse {
info!("Set report for {:?}: {=[u8]}", id, data);
OutResponse::Accepted
}
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) {
info!("Set idle rate for {:?} to {:?}", id, dur);
}
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
fn get_idle_ms(&mut self, id: Option<ReportId>) -> Option<u32> {
info!("Get idle rate for {:?}", id);
None
}

View File

@ -88,7 +88,7 @@ async fn main(_spawner: Spawner) {
let mut msos_descriptor = [0; 256];
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let mut request_handler = MyRequestHandler {};
let mut device_handler = MyDeviceHandler::new();
let mut state = State::new();
@ -107,7 +107,7 @@ async fn main(_spawner: Spawner) {
// Create classes on the builder.
let config = embassy_usb::class::hid::Config {
report_descriptor: KeyboardReport::desc(),
request_handler: Some(&request_handler),
request_handler: None,
poll_ms: 60,
max_packet_size: 8,
};
@ -160,7 +160,7 @@ async fn main(_spawner: Spawner) {
};
let out_fut = async {
reader.run(false, &request_handler).await;
reader.run(false, &mut request_handler).await;
};
// Run everything concurrently.
@ -171,21 +171,21 @@ async fn main(_spawner: Spawner) {
struct MyRequestHandler {}
impl RequestHandler for MyRequestHandler {
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
fn get_report(&mut self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
info!("Get report for {:?}", id);
None
}
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
fn set_report(&mut self, id: ReportId, data: &[u8]) -> OutResponse {
info!("Set report for {:?}: {=[u8]}", id, data);
OutResponse::Accepted
}
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) {
info!("Set idle rate for {:?} to {:?}", id, dur);
}
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
fn get_idle_ms(&mut self, id: Option<ReportId>) -> Option<u32> {
info!("Get idle rate for {:?}", id);
None
}

View File

@ -81,7 +81,7 @@ async fn main(_spawner: Spawner) {
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let mut request_handler = MyRequestHandler {};
let mut state = State::new();
@ -97,7 +97,7 @@ async fn main(_spawner: Spawner) {
// Create classes on the builder.
let config = embassy_usb::class::hid::Config {
report_descriptor: MouseReport::desc(),
request_handler: Some(&request_handler),
request_handler: Some(&mut request_handler),
poll_ms: 60,
max_packet_size: 8,
};
@ -139,21 +139,21 @@ async fn main(_spawner: Spawner) {
struct MyRequestHandler {}
impl RequestHandler for MyRequestHandler {
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
fn get_report(&mut self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
info!("Get report for {:?}", id);
None
}
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
fn set_report(&mut self, id: ReportId, data: &[u8]) -> OutResponse {
info!("Set report for {:?}: {=[u8]}", id, data);
OutResponse::Accepted
}
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) {
info!("Set idle rate for {:?} to {:?}", id, dur);
}
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
fn get_idle_ms(&mut self, id: Option<ReportId>) -> Option<u32> {
info!("Get idle rate for {:?}", id);
None
}

View File

@ -54,7 +54,7 @@ async fn main(_spawner: Spawner) {
let mut config_descriptor = [0; 256];
let mut bos_descriptor = [0; 256];
let mut control_buf = [0; 64];
let request_handler = MyRequestHandler {};
let mut request_handler = MyRequestHandler {};
let mut state = State::new();
@ -70,7 +70,7 @@ async fn main(_spawner: Spawner) {
// Create classes on the builder.
let config = embassy_usb::class::hid::Config {
report_descriptor: MouseReport::desc(),
request_handler: Some(&request_handler),
request_handler: Some(&mut request_handler),
poll_ms: 60,
max_packet_size: 8,
};
@ -112,21 +112,21 @@ async fn main(_spawner: Spawner) {
struct MyRequestHandler {}
impl RequestHandler for MyRequestHandler {
fn get_report(&self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
fn get_report(&mut self, id: ReportId, _buf: &mut [u8]) -> Option<usize> {
info!("Get report for {:?}", id);
None
}
fn set_report(&self, id: ReportId, data: &[u8]) -> OutResponse {
fn set_report(&mut self, id: ReportId, data: &[u8]) -> OutResponse {
info!("Set report for {:?}: {=[u8]}", id, data);
OutResponse::Accepted
}
fn set_idle_ms(&self, id: Option<ReportId>, dur: u32) {
fn set_idle_ms(&mut self, id: Option<ReportId>, dur: u32) {
info!("Set idle rate for {:?} to {:?}", id, dur);
}
fn get_idle_ms(&self, id: Option<ReportId>) -> Option<u32> {
fn get_idle_ms(&mut self, id: Option<ReportId>) -> Option<u32> {
info!("Get idle rate for {:?}", id);
None
}