1use alloc::{boxed::Box, string::String, vec::Vec};
2use futures::future::LocalBoxFuture;
3
4use crate::{
5 descriptor::{ConfigurationDescriptor, DeviceDescriptor},
6 err::TransferError,
7 transfer::{Recipient, Request, RequestType, wait::Waiter},
8};
9
10pub trait Controller: Send + 'static {
11 fn init(&mut self) -> LocalBoxFuture<'_, Result<(), USBError>>;
12 fn device_list(&self) -> LocalBoxFuture<'_, Result<Vec<Box<dyn DeviceInfo>>, USBError>>;
13
14 fn handle_event(&mut self);
16}
17
18pub trait DeviceInfo: Send + 'static {
19 fn open(&mut self) -> LocalBoxFuture<'_, Result<Box<dyn Device>, USBError>>;
20 fn descriptor(&self) -> LocalBoxFuture<'_, Result<DeviceDescriptor, USBError>>;
21 fn configuration_descriptor(
22 &mut self,
23 index: u8,
24 ) -> LocalBoxFuture<'_, Result<ConfigurationDescriptor, USBError>>;
25}
26
27pub trait Device: Send + 'static {
28 fn set_configuration(&mut self, configuration: u8) -> LocalBoxFuture<'_, Result<(), USBError>>;
29 fn get_configuration(&mut self) -> LocalBoxFuture<'_, Result<u8, USBError>>;
30 fn claim_interface(
31 &mut self,
32 interface: u8,
33 alternate: u8,
34 ) -> LocalBoxFuture<'_, Result<Box<dyn Interface>, USBError>>;
35
36 fn string_descriptor(
37 &mut self,
38 index: u8,
39 language_id: u16,
40 ) -> LocalBoxFuture<'_, Result<String, USBError>>;
41}
42
43pub trait Interface: Send + 'static {
44 fn control_in<'a>(&mut self, setup: ControlSetup, data: &'a mut [u8]) -> ResultTransfer<'a>;
47 fn control_out<'a>(&mut self, setup: ControlSetup, data: &'a [u8]) -> ResultTransfer<'a>;
48 fn endpoint_bulk_in(&mut self, endpoint: u8) -> Result<Box<dyn EndpointBulkIn>, USBError>;
49 fn endpoint_bulk_out(&mut self, endpoint: u8) -> Result<Box<dyn EndpointBulkOut>, USBError>;
50 fn endpoint_interrupt_in(
51 &mut self,
52 endpoint: u8,
53 ) -> Result<Box<dyn EndpointInterruptIn>, USBError>;
54 fn endpoint_interrupt_out(
55 &mut self,
56 endpoint: u8,
57 ) -> Result<Box<dyn EndpointInterruptOut>, USBError>;
58 fn endpoint_iso_in(&mut self, endpoint: u8) -> Result<Box<dyn EndpintIsoIn>, USBError>;
59 fn endpoint_iso_out(&mut self, endpoint: u8) -> Result<Box<dyn EndpintIsoOut>, USBError>;
60}
61
62pub trait TEndpint: Send + 'static {}
63
64pub trait EndpointBulkIn: TEndpint {
65 fn submit<'a>(&mut self, data: &'a mut [u8]) -> ResultTransfer<'a>;
66}
67pub trait EndpointBulkOut: TEndpint {
68 fn submit<'a>(&mut self, data: &'a [u8]) -> ResultTransfer<'a>;
69}
70
71pub trait EndpointInterruptIn: TEndpint {
72 fn submit<'a>(&mut self, data: &'a mut [u8]) -> ResultTransfer<'a>;
73}
74
75pub trait EndpointInterruptOut: TEndpint {
76 fn submit<'a>(&mut self, data: &'a [u8]) -> ResultTransfer<'a>;
77}
78
79pub trait EndpintIsoIn: TEndpint {
80 fn submit<'a>(&mut self, data: &'a mut [u8], num_iso_packets: usize) -> ResultTransfer<'a>;
81}
82
83pub trait EndpintIsoOut: TEndpint {
84 fn submit<'a>(&mut self, data: &'a [u8], num_iso_packets: usize) -> ResultTransfer<'a>;
85}
86
87pub type TransferFuture<'a> = Waiter<'a, Result<usize, TransferError>>;
89pub type ResultTransfer<'a> = Result<TransferFuture<'a>, TransferError>;
90
91pub trait Transfer<'a>: Future<Output = Result<usize, TransferError>> + Send + 'a {}
92
93#[derive(thiserror::Error, Debug)]
94pub enum USBError {
95 #[error("Timeout")]
96 Timeout,
97 #[error("No memory available")]
98 NoMemory,
99 #[error("Transfer error: {0}")]
100 TransferError(#[from] TransferError),
101 #[error("Not initialized")]
102 NotInitialized,
103 #[error("Not found")]
104 NotFound,
105 #[error("Slot limit reached")]
106 SlotLimitReached,
107 #[error("Configuration not set")]
108 ConfigurationNotSet,
109 #[error("Other error: {0}")]
110 Other(#[from] Box<dyn core::error::Error>),
111}
112
113#[derive(Debug, Clone)]
114pub struct ControlSetup {
115 pub request_type: RequestType,
116 pub recipient: Recipient,
117 pub request: Request,
118 pub value: u16,
119 pub index: u16,
120}