usb_stack/class/hid/custom.rs
1//! HID function with a caller-supplied report descriptor.
2
3use crate::bus::UsbBus;
4use crate::class::hid::{HidDevice, HidSpec, ReportType};
5
6/// Size of the buffer holding the last report written by the host.
7const REPORT_BUFFER_SIZE: usize = 64;
8
9/// A HID function defined entirely by its report descriptor.
10///
11/// Input reports are sent as-is with
12/// [`HidClass::send_input`](super::HidClass::send_input). Output and feature
13/// reports written by the host are buffered — in the same layout the device
14/// sends (`report_id` first when the spec uses report IDs) — and read with
15/// [`read_report`](Self::read_report). `Get Report` echoes the last buffered
16/// report back.
17///
18/// ```ignore
19/// static DESCRIPTOR: &[u8] = &[/* … */];
20///
21/// let spec = HidSpec::new(DESCRIPTOR, 8).out_report_size(8).report_ids(true);
22/// let mut hid = HidClass::new(&alloc, CustomHid::new(spec));
23///
24/// loop {
25/// usb_dev.poll(&mut [&mut hid]);
26/// let mut buf = [0u8; 8];
27/// if let Some(len) = hid.device_mut().read_report(&mut buf) {
28/// // host wrote a report
29/// }
30/// }
31/// ```
32pub struct CustomHid {
33 spec: HidSpec,
34 report: [u8; REPORT_BUFFER_SIZE],
35 report_len: usize,
36 report_pending: bool,
37}
38
39impl CustomHid {
40 /// Create a function from a report descriptor and its endpoint geometry.
41 ///
42 /// Set [`HidSpec::out_report_size`] for a device that receives output or
43 /// feature reports.
44 pub const fn new(spec: HidSpec) -> Self {
45 Self {
46 spec,
47 report: [0; REPORT_BUFFER_SIZE],
48 report_len: 0,
49 report_pending: false,
50 }
51 }
52
53 /// Read the last report written by the host and return its length.
54 ///
55 /// `None` when nothing has arrived since the previous call.
56 pub fn read_report(&mut self, buf: &mut [u8]) -> Option<usize> {
57 if !self.report_pending {
58 return None;
59 }
60 self.report_pending = false;
61 let len = self.report_len.min(buf.len());
62 buf[..len].copy_from_slice(&self.report[..len]);
63 Some(len)
64 }
65
66 /// Buffer a report, prefixing the report ID when the spec uses them.
67 fn store(&mut self, report_id: u8, data: &[u8]) {
68 let mut len = 0;
69 if self.spec.report_ids {
70 self.report[0] = report_id;
71 len = 1;
72 }
73 let data_len = data.len().min(REPORT_BUFFER_SIZE - len);
74 self.report[len..len + data_len].copy_from_slice(&data[..data_len]);
75 self.report_len = len + data_len;
76 self.report_pending = true;
77 }
78}
79
80impl<B: UsbBus> HidDevice<B> for CustomHid {
81 fn spec(&self) -> HidSpec {
82 self.spec
83 }
84
85 fn get_report(
86 &mut self,
87 _report_type: ReportType,
88 _report_id: u8,
89 buf: &mut [u8],
90 ) -> Option<usize> {
91 // Nothing meaningful to report before the host has written something.
92 if self.report_len == 0 {
93 return None;
94 }
95 let len = self.report_len.min(buf.len());
96 buf[..len].copy_from_slice(&self.report[..len]);
97 Some(len)
98 }
99
100 fn set_report(&mut self, report_type: ReportType, report_id: u8, data: &[u8]) -> bool {
101 // Input reports flow the other way.
102 if report_type == ReportType::Input {
103 return false;
104 }
105 self.store(report_id, data);
106 true
107 }
108
109 fn output_report(&mut self, report_id: u8, data: &[u8]) {
110 self.store(report_id, data);
111 }
112
113 fn reset(&mut self) {
114 self.report_len = 0;
115 self.report_pending = false;
116 }
117}