Use trait objects instead of generics for UsbDevice::classes

This commit is contained in:
alexmoon 2022-03-27 17:12:57 -04:00 committed by Dario Nieuwenhuis
parent bdc6e0481c
commit 52c622b1cd
5 changed files with 120 additions and 201 deletions

View file

@ -175,10 +175,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> {
}
/// Creates the [`UsbDevice`] instance with the configuration in this builder.
///
/// If a device has mutliple [`UsbClass`]es, they can be provided as a tuple list:
/// `(class1, (class2, (class3, ()))`.
pub fn build<C: UsbClass<'d, D>>(mut self, classes: C) -> UsbDevice<'d, D, C> {
pub fn build(mut self, classes: &'d mut [&'d mut dyn UsbClass]) -> UsbDevice<'d, D> {
self.config_descriptor.end_configuration();
self.bos_descriptor.end_bos();

View file

@ -1,7 +1,4 @@
use core::future::Future;
use crate::control::Request;
use crate::driver::{ControlPipe, Driver};
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@ -21,19 +18,7 @@ impl Default for RequestStatus {
///
/// All methods are optional callbacks that will be called by
/// [`UsbDevice::run()`](crate::UsbDevice::run)
pub trait UsbClass<'d, D: Driver<'d>> {
type ControlOutFuture<'a>: Future<Output = RequestStatus> + 'a
where
Self: 'a,
'd: 'a,
D: 'a;
type ControlInFuture<'a>: Future<Output = ControlInRequestStatus> + 'a
where
Self: 'a,
'd: 'a,
D: 'a;
pub trait UsbClass {
/// Called after a USB reset after the bus reset sequence is complete.
fn reset(&mut self) {}
@ -50,10 +35,7 @@ pub trait UsbClass<'d, D: Driver<'d>> {
///
/// * `req` - The request from the SETUP packet.
/// * `data` - The data from the request.
fn control_out<'a>(&'a mut self, req: Request, data: &'a [u8]) -> Self::ControlOutFuture<'a>
where
'd: 'a,
D: 'a;
fn control_out(&mut self, req: Request, data: &[u8]) -> RequestStatus;
/// Called when a control request is received with direction DeviceToHost.
///
@ -71,120 +53,63 @@ pub trait UsbClass<'d, D: Driver<'d>> {
/// * `req` - The request from the SETUP packet.
/// * `control` - The control pipe.
fn control_in<'a>(
&'a mut self,
&mut self,
req: Request,
control: ControlIn<'a, 'd, D>,
) -> Self::ControlInFuture<'a>
where
'd: 'a;
}
impl<'d, D: Driver<'d>> UsbClass<'d, D> for () {
type ControlOutFuture<'a> = impl Future<Output = RequestStatus> + 'a where Self: 'a, 'd: 'a, D: 'a;
type ControlInFuture<'a> = impl Future<Output = ControlInRequestStatus> + 'a where Self: 'a, 'd: 'a, D: 'a;
fn control_out<'a>(&'a mut self, _req: Request, _data: &'a [u8]) -> Self::ControlOutFuture<'a>
where
'd: 'a,
D: 'a,
{
async move { RequestStatus::default() }
}
fn control_in<'a>(
&'a mut self,
_req: Request,
control: ControlIn<'a, 'd, D>,
) -> Self::ControlInFuture<'a>
where
'd: 'a,
D: 'a,
{
async move { control.ignore() }
}
}
impl<'d, D: Driver<'d>, Head, Tail> UsbClass<'d, D> for (Head, Tail)
where
Head: UsbClass<'d, D>,
Tail: UsbClass<'d, D>,
{
type ControlOutFuture<'a> = impl Future<Output = RequestStatus> + 'a where Self: 'a, 'd: 'a, D: 'a;
type ControlInFuture<'a> = impl Future<Output = ControlInRequestStatus> + 'a where Self: 'a, 'd: 'a, D: 'a;
fn control_out<'a>(&'a mut self, req: Request, data: &'a [u8]) -> Self::ControlOutFuture<'a>
where
'd: 'a,
D: 'a,
{
async move {
match self.0.control_out(req, data).await {
RequestStatus::Unhandled => self.1.control_out(req, data).await,
status => status,
}
}
}
fn control_in<'a>(
&'a mut self,
req: Request,
control: ControlIn<'a, 'd, D>,
) -> Self::ControlInFuture<'a>
where
'd: 'a,
{
async move {
match self
.0
.control_in(req, ControlIn::new(control.control))
.await
{
ControlInRequestStatus(RequestStatus::Unhandled) => {
self.1.control_in(req, control).await
}
status => status,
}
}
}
control: ControlIn<'a>,
) -> ControlInRequestStatus<'a>;
}
/// Handle for a control IN transfer. When implementing a class, use the methods of this object to
/// response to the transfer with either data or an error (STALL condition). To ignore the request
/// and pass it on to the next class, call [`Self::ignore()`].
pub struct ControlIn<'a, 'd: 'a, D: Driver<'d>> {
control: &'a mut D::ControlPipe,
pub struct ControlIn<'a> {
buf: &'a mut [u8],
}
#[derive(Eq, PartialEq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ControlInRequestStatus(pub(crate) RequestStatus);
pub struct ControlInRequestStatus<'a> {
pub(crate) status: RequestStatus,
pub(crate) data: &'a [u8],
}
impl ControlInRequestStatus {
pub fn status(self) -> RequestStatus {
self.0
impl<'a> ControlInRequestStatus<'a> {
pub fn status(&self) -> RequestStatus {
self.status
}
}
impl<'a, 'd: 'a, D: Driver<'d>> ControlIn<'a, 'd, D> {
pub(crate) fn new(control: &'a mut D::ControlPipe) -> Self {
ControlIn { control }
impl<'a> ControlIn<'a> {
pub(crate) fn new(buf: &'a mut [u8]) -> Self {
ControlIn { buf }
}
/// Ignores the request and leaves it unhandled.
pub fn ignore(self) -> ControlInRequestStatus {
ControlInRequestStatus(RequestStatus::Unhandled)
pub fn ignore(self) -> ControlInRequestStatus<'a> {
ControlInRequestStatus {
status: RequestStatus::Unhandled,
data: &[],
}
}
/// Accepts the transfer with the supplied buffer.
pub async fn accept(self, data: &[u8]) -> ControlInRequestStatus {
self.control.accept_in(data).await;
pub fn accept(self, data: &[u8]) -> ControlInRequestStatus<'a> {
assert!(data.len() < self.buf.len());
ControlInRequestStatus(RequestStatus::Accepted)
let buf = &mut self.buf[0..data.len()];
buf.copy_from_slice(data);
ControlInRequestStatus {
status: RequestStatus::Accepted,
data: buf,
}
}
/// Rejects the transfer by stalling the pipe.
pub fn reject(self) -> ControlInRequestStatus {
self.control.reject();
ControlInRequestStatus(RequestStatus::Rejected)
pub fn reject(self) -> ControlInRequestStatus<'a> {
ControlInRequestStatus {
status: RequestStatus::Unhandled,
data: &[],
}
}
}

View file

@ -13,6 +13,8 @@ pub mod driver;
pub mod types;
mod util;
use class::ControlInRequestStatus;
use self::class::{RequestStatus, UsbClass};
use self::control::*;
use self::descriptor::*;
@ -51,7 +53,7 @@ pub const CONFIGURATION_VALUE: u8 = 1;
/// The default value for bAlternateSetting for all interfaces.
pub const DEFAULT_ALTERNATE_SETTING: u8 = 0;
pub struct UsbDevice<'d, D: Driver<'d>, C: UsbClass<'d, D>> {
pub struct UsbDevice<'d, D: Driver<'d>> {
bus: D::Bus,
control: D::ControlPipe,
@ -65,17 +67,17 @@ pub struct UsbDevice<'d, D: Driver<'d>, C: UsbClass<'d, D>> {
self_powered: bool,
pending_address: u8,
classes: C,
classes: &'d mut [&'d mut dyn UsbClass],
}
impl<'d, D: Driver<'d>, C: UsbClass<'d, D>> UsbDevice<'d, D, C> {
impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
pub(crate) fn build(
mut driver: D,
config: Config<'d>,
device_descriptor: &'d [u8],
config_descriptor: &'d [u8],
bos_descriptor: &'d [u8],
classes: C,
classes: &'d mut [&'d mut dyn UsbClass],
) -> Self {
let control = driver
.alloc_control_pipe(config.max_packet_size_0 as u16)
@ -113,7 +115,9 @@ impl<'d, D: Driver<'d>, C: UsbClass<'d, D>> UsbDevice<'d, D, C> {
self.remote_wakeup_enabled = false;
self.pending_address = 0;
self.classes.reset();
for c in self.classes.iter_mut() {
c.reset();
}
}
Event::Resume => {}
Event::Suspend => {
@ -155,12 +159,14 @@ impl<'d, D: Driver<'d>, C: UsbClass<'d, D>> UsbDevice<'d, D, C> {
&[]
};
match self.classes.control_out(req, data).await {
for c in self.classes.iter_mut() {
match c.control_out(req, data) {
RequestStatus::Accepted => return self.control.accept(),
RequestStatus::Rejected => return self.control.reject(),
RequestStatus::Unhandled => (),
}
}
}
const CONFIGURATION_NONE_U16: u16 = CONFIGURATION_NONE as u16;
const CONFIGURATION_VALUE_U16: u16 = CONFIGURATION_VALUE as u16;
@ -233,14 +239,22 @@ impl<'d, D: Driver<'d>, C: UsbClass<'d, D>> UsbDevice<'d, D, C> {
}
async fn handle_control_in(&mut self, req: Request) {
match self
.classes
.control_in(req, class::ControlIn::new(&mut self.control))
.await
.status()
{
RequestStatus::Accepted | RequestStatus::Rejected => return,
RequestStatus::Unhandled => (),
let mut buf = [0; 128];
for c in self.classes.iter_mut() {
match c.control_in(req, class::ControlIn::new(&mut buf)) {
ControlInRequestStatus {
status: RequestStatus::Accepted,
data,
} => return self.control.accept_in(data).await,
ControlInRequestStatus {
status: RequestStatus::Rejected,
..
} => return self.control.reject(),
ControlInRequestStatus {
status: RequestStatus::Unhandled,
..
} => (),
}
}
match req.request_type {

View file

@ -1,4 +1,3 @@
use core::future::Future;
use core::mem;
use defmt::info;
use embassy_usb::class::{ControlInRequestStatus, RequestStatus, UsbClass};
@ -56,26 +55,14 @@ pub struct CdcAcmControl {
pub rts: bool,
}
impl<'d, D: Driver<'d>> UsbClass<'d, D> for CdcAcmControl {
type ControlOutFuture<'a> = impl Future<Output = RequestStatus> + 'a where Self: 'a, 'd: 'a, D: 'a;
type ControlInFuture<'a> = impl Future<Output = ControlInRequestStatus> + 'a where Self: 'a, 'd: 'a, D: 'a;
impl UsbClass for CdcAcmControl {
fn reset(&mut self) {
self.line_coding = LineCoding::default();
self.dtr = false;
self.rts = false;
}
fn control_out<'a>(
&'a mut self,
req: control::Request,
data: &'a [u8],
) -> Self::ControlOutFuture<'a>
where
'd: 'a,
D: 'a,
{
async move {
fn control_out(&mut self, req: control::Request, data: &[u8]) -> RequestStatus {
if !(req.request_type == control::RequestType::Class
&& req.recipient == control::Recipient::Interface
&& req.index == u8::from(self.comm_if) as u16)
@ -108,17 +95,12 @@ impl<'d, D: Driver<'d>> UsbClass<'d, D> for CdcAcmControl {
_ => RequestStatus::Rejected,
}
}
}
fn control_in<'a>(
&'a mut self,
&mut self,
req: Request,
control: embassy_usb::class::ControlIn<'a, 'd, D>,
) -> Self::ControlInFuture<'a>
where
'd: 'a,
{
async move {
control: embassy_usb::class::ControlIn<'a>,
) -> ControlInRequestStatus<'a> {
if !(req.request_type == control::RequestType::Class
&& req.recipient == control::Recipient::Interface
&& req.index == u8::from(self.comm_if) as u16)
@ -135,13 +117,12 @@ impl<'d, D: Driver<'d>> UsbClass<'d, D> for CdcAcmControl {
data[4] = self.line_coding.stop_bits as u8;
data[5] = self.line_coding.parity_type as u8;
data[6] = self.line_coding.data_bits;
control.accept(&data).await
control.accept(&data)
}
_ => control.reject(),
}
}
}
}
impl<'d, D: Driver<'d>> CdcAcmClass<'d, D> {
/// Creates a new CdcAcmClass with the provided UsbBus and max_packet_size in bytes. For

View file

@ -16,6 +16,7 @@ use embassy_nrf::interrupt;
use embassy_nrf::pac;
use embassy_nrf::usb::Driver;
use embassy_nrf::Peripherals;
use embassy_usb::class::UsbClass;
use embassy_usb::driver::{EndpointIn, EndpointOut};
use embassy_usb::{Config, UsbDeviceBuilder};
use futures::future::join3;
@ -59,7 +60,8 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let mut class = CdcAcmClass::new(&mut builder, 64);
// Build the builder.
let mut usb = builder.build(class.control);
let mut classes: [&mut dyn UsbClass; 1] = [&mut class.control];
let mut usb = builder.build(&mut classes);
// Run the USB device.
let fut1 = usb.run();