Skip to main content

usb_stack/class/
mod.rs

1//! [`UsbClass`] trait and the class implementations.
2//!
3//! Provided classes:
4//!
5//! | Class | Interface |
6//! |---|---|
7//! | [`cdc_acm::CdcAcm`] | virtual serial port |
8//! | [`hid::HidClass`] | HID (boot keyboard/mouse, custom reports, power device) |
9//! | [`gs_usb::GsUsbClass`] | candleLight/GS_USB CAN adapter |
10//! | [`i2c_tiny_usb::I2cTinyUsb`] | i2c-tiny-usb I2C bridge |
11//! | [`uac::AudioClass`] | USB Audio Class 1.0 |
12//!
13//! Classes are generic over the [`UsbBus`] trait.
14
15pub mod cdc_acm;
16pub mod gs_usb;
17pub mod hid;
18pub mod i2c_tiny_usb;
19pub mod uac;
20
21use crate::bus::{InterfaceNumber, StringIndex, UsbBus};
22use crate::control::{ControlIn, ControlOut};
23use crate::descriptor::DescriptorWriter;
24use crate::endpoint::EndpointAddress;
25use crate::error::UsbResult;
26
27/// USB device class trait.
28///
29/// All methods have default no-op implementations.  Override only the ones your
30/// class needs.
31pub trait UsbClass<B: UsbBus> {
32    // ── Descriptors ──────────────────────────────────────────────────────────
33
34    /// Append this class's interface and endpoint descriptors.
35    fn configuration_descriptors(&self, writer: &mut DescriptorWriter) -> UsbResult<()> {
36        let _ = writer;
37        Ok(())
38    }
39
40    /// Return the string for a previously allocated [`StringIndex`], or `None`.
41    fn string(&self, index: StringIndex, lang_id: u16) -> Option<&str> {
42        let _ = (index, lang_id);
43        None
44    }
45
46    // ── Control transfers ─────────────────────────────────────────────────────
47
48    /// Handle a control IN transfer.  Accept, reject, or pass (drop without acting).
49    fn control_in(&mut self, xfer: ControlIn<'_, B>) {
50        let _ = xfer;
51    }
52
53    /// Handle a control OUT transfer.  Accept, reject, or pass (drop without acting).
54    fn control_out(&mut self, xfer: ControlOut<'_, B>) {
55        let _ = xfer;
56    }
57
58    // ── Data events ───────────────────────────────────────────────────────────
59
60    /// Called when endpoint `addr` received an OUT packet.
61    fn endpoint_out(&mut self, addr: EndpointAddress) {
62        let _ = addr;
63    }
64
65    /// Called when endpoint `addr` completed an IN transfer.
66    fn endpoint_in_complete(&mut self, addr: EndpointAddress) {
67        let _ = addr;
68    }
69
70    /// Called when endpoint `addr` received a SETUP packet (non-EP0 control).
71    fn endpoint_setup(&mut self, addr: EndpointAddress) {
72        let _ = addr;
73    }
74
75    // ── Lifecycle ─────────────────────────────────────────────────────────────
76
77    /// Called after a USB bus reset.
78    fn reset(&mut self) {}
79
80    /// Called when the host sends SET_CONFIGURATION(1).
81    fn configure(&mut self) {}
82
83    /// Called when the host sends SET_CONFIGURATION(0) or on reset.
84    fn deconfigure(&mut self) {}
85
86    /// Called unconditionally on every [`UsbDevice::poll`](crate::device::UsbDevice::poll).
87    fn poll(&mut self) {}
88
89    // ── Alternate settings ────────────────────────────────────────────────────
90
91    /// Return the current alternate setting for `interface`, or `None`.
92    fn alt_setting(&mut self, interface: InterfaceNumber) -> Option<u8> {
93        let _ = interface;
94        None
95    }
96
97    /// Set the alternate setting for `interface`.  Return `true` if handled.
98    fn set_alt_setting(&mut self, interface: InterfaceNumber, alt: u8) -> bool {
99        let _ = (interface, alt);
100        false
101    }
102}