diff options
Diffstat (limited to 'embassy-usb/src/class.rs')
| -rw-r--r-- | embassy-usb/src/class.rs | 104 |
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 @@ | |||
| 1 | use crate::control::Request; | ||
| 2 | |||
| 3 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
| 4 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 5 | pub 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) | ||
| 14 | pub 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()`]. | ||
| 62 | pub struct ControlIn<'a> { | ||
| 63 | buf: &'a mut [u8], | ||
| 64 | } | ||
| 65 | |||
| 66 | #[derive(Eq, PartialEq, Debug)] | ||
| 67 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 68 | pub struct ControlInRequestStatus<'a> { | ||
| 69 | pub(crate) status: RequestStatus, | ||
| 70 | pub(crate) data: &'a [u8], | ||
| 71 | } | ||
| 72 | |||
| 73 | impl<'a> ControlInRequestStatus<'a> { | ||
| 74 | pub fn status(&self) -> RequestStatus { | ||
| 75 | self.status | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | impl<'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 | } | ||
