aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb/src/class.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-03-28 03:27:21 +0200
committerDario Nieuwenhuis <[email protected]>2022-04-06 05:38:11 +0200
commit2b547f311efc7feaa3afbb9f1bf4100c5502839e (patch)
tree40b9ef749e486d6f902a9d9dd95a68ff672c151e /embassy-usb/src/class.rs
parent15cc97d794d8b4baa6c1a8f1ed6c64468701c9e7 (diff)
usb: move all control-related stuff to `mod control`.
Diffstat (limited to 'embassy-usb/src/class.rs')
-rw-r--r--embassy-usb/src/class.rs104
1 files changed, 0 insertions, 104 deletions
diff --git a/embassy-usb/src/class.rs b/embassy-usb/src/class.rs
deleted file mode 100644
index 754e3a209..000000000
--- a/embassy-usb/src/class.rs
+++ /dev/null
@@ -1,104 +0,0 @@
1use crate::control::Request;
2
3#[derive(Copy, Clone, Eq, PartialEq, Debug)]
4#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5pub enum RequestStatus {
6 Accepted,
7 Rejected,
8}
9
10/// A trait for implementing USB classes.
11///
12/// All methods are optional callbacks that will be called by
13/// [`UsbDevice::run()`](crate::UsbDevice::run)
14pub trait ControlHandler {
15 /// Called after a USB reset after the bus reset sequence is complete.
16 fn reset(&mut self) {}
17
18 /// Called when a control request is received with direction HostToDevice.
19 ///
20 /// All requests are passed to classes in turn, which can choose to accept, ignore or report an
21 /// error. Classes can even choose to override standard requests, but doing that is rarely
22 /// necessary.
23 ///
24 /// When implementing your own class, you should ignore any requests that are not meant for your
25 /// class so that any other classes in the composite device can process them.
26 ///
27 /// # Arguments
28 ///
29 /// * `req` - The request from the SETUP packet.
30 /// * `data` - The data from the request.
31 fn control_out(&mut self, req: Request, data: &[u8]) -> RequestStatus {
32 RequestStatus::Rejected
33 }
34
35 /// Called when a control request is received with direction DeviceToHost.
36 ///
37 /// All requests are passed to classes in turn, which can choose to accept, ignore or report an
38 /// error. Classes can even choose to override standard requests, but doing that is rarely
39 /// necessary.
40 ///
41 /// See [`ControlIn`] for how to respond to the transfer.
42 ///
43 /// When implementing your own class, you should ignore any requests that are not meant for your
44 /// class so that any other classes in the composite device can process them.
45 ///
46 /// # Arguments
47 ///
48 /// * `req` - The request from the SETUP packet.
49 /// * `control` - The control pipe.
50 fn control_in<'a>(
51 &mut self,
52 req: Request,
53 control: ControlIn<'a>,
54 ) -> ControlInRequestStatus<'a> {
55 control.reject()
56 }
57}
58
59/// Handle for a control IN transfer. When implementing a class, use the methods of this object to
60/// response to the transfer with either data or an error (STALL condition). To ignore the request
61/// and pass it on to the next class, call [`Self::ignore()`].
62pub struct ControlIn<'a> {
63 buf: &'a mut [u8],
64}
65
66#[derive(Eq, PartialEq, Debug)]
67#[cfg_attr(feature = "defmt", derive(defmt::Format))]
68pub struct ControlInRequestStatus<'a> {
69 pub(crate) status: RequestStatus,
70 pub(crate) data: &'a [u8],
71}
72
73impl<'a> ControlInRequestStatus<'a> {
74 pub fn status(&self) -> RequestStatus {
75 self.status
76 }
77}
78
79impl<'a> ControlIn<'a> {
80 pub(crate) fn new(buf: &'a mut [u8]) -> Self {
81 ControlIn { buf }
82 }
83
84 /// Accepts the transfer with the supplied buffer.
85 pub fn accept(self, data: &[u8]) -> ControlInRequestStatus<'a> {
86 assert!(data.len() < self.buf.len());
87
88 let buf = &mut self.buf[0..data.len()];
89 buf.copy_from_slice(data);
90
91 ControlInRequestStatus {
92 status: RequestStatus::Accepted,
93 data: buf,
94 }
95 }
96
97 /// Rejects the transfer by stalling the pipe.
98 pub fn reject(self) -> ControlInRequestStatus<'a> {
99 ControlInRequestStatus {
100 status: RequestStatus::Rejected,
101 data: &[],
102 }
103 }
104}