2022-03-09 01:34:35 +01:00
|
|
|
#![no_std]
|
2022-03-25 21:46:14 +01:00
|
|
|
#![feature(type_alias_impl_trait)]
|
2023-01-31 22:27:19 +01:00
|
|
|
#![doc = include_str!("../README.md")]
|
|
|
|
#![warn(missing_docs)]
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
// This mod MUST go first, so that the others see its macros.
|
|
|
|
pub(crate) mod fmt;
|
|
|
|
|
2022-09-26 12:29:27 +02:00
|
|
|
pub use embassy_usb_driver as driver;
|
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
mod builder;
|
2022-09-26 13:00:21 +02:00
|
|
|
pub mod class;
|
2022-03-25 21:46:14 +01:00
|
|
|
pub mod control;
|
2022-03-09 01:34:35 +01:00
|
|
|
pub mod descriptor;
|
2022-04-16 04:47:27 +02:00
|
|
|
mod descriptor_reader;
|
2023-01-12 21:59:25 +01:00
|
|
|
pub mod msos;
|
2022-03-09 01:34:35 +01:00
|
|
|
pub mod types;
|
|
|
|
|
2023-02-07 20:49:10 +01:00
|
|
|
mod config {
|
|
|
|
#![allow(unused)]
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/config.rs"));
|
|
|
|
}
|
|
|
|
|
2022-08-28 22:57:35 +02:00
|
|
|
use embassy_futures::select::{select, Either};
|
2022-03-28 02:20:01 +02:00
|
|
|
use heapless::Vec;
|
2022-03-27 23:12:57 +02:00
|
|
|
|
2022-09-26 12:29:27 +02:00
|
|
|
pub use crate::builder::{Builder, Config};
|
2023-02-07 20:49:10 +01:00
|
|
|
use crate::config::*;
|
2022-09-26 12:29:27 +02:00
|
|
|
use crate::control::*;
|
|
|
|
use crate::descriptor::*;
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::descriptor_reader::foreach_endpoint;
|
2022-09-26 12:29:27 +02:00
|
|
|
use crate::driver::{Bus, ControlPipe, Direction, Driver, EndpointAddress, Event};
|
|
|
|
use crate::types::*;
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
/// The global state of the USB device.
|
|
|
|
///
|
|
|
|
/// In general class traffic is only possible in the `Configured` state.
|
|
|
|
#[repr(u8)]
|
|
|
|
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
2022-04-10 21:41:51 +02:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2022-03-09 01:34:35 +01:00
|
|
|
pub enum UsbDeviceState {
|
2022-07-08 07:30:15 +02:00
|
|
|
/// The USB device has no power.
|
|
|
|
Unpowered,
|
|
|
|
|
2022-04-10 21:41:51 +02:00
|
|
|
/// The USB device is disabled.
|
|
|
|
Disabled,
|
|
|
|
|
|
|
|
/// The USB device has just been enabled or reset.
|
2022-03-09 01:34:35 +01:00
|
|
|
Default,
|
|
|
|
|
|
|
|
/// The USB device has received an address from the host.
|
|
|
|
Addressed,
|
|
|
|
|
|
|
|
/// The USB device has been configured and is fully functional.
|
|
|
|
Configured,
|
|
|
|
}
|
|
|
|
|
2023-01-31 22:27:19 +01:00
|
|
|
/// Error returned by [`UsbDevice::remote_wakeup`].
|
2022-04-13 19:09:08 +02:00
|
|
|
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum RemoteWakeupError {
|
2023-01-31 22:27:19 +01:00
|
|
|
/// The USB device is not suspended, or remote wakeup was not enabled.
|
2022-04-13 19:09:08 +02:00
|
|
|
InvalidState,
|
2023-01-31 22:27:19 +01:00
|
|
|
/// The underlying driver doesn't support remote wakeup.
|
2022-04-13 19:09:08 +02:00
|
|
|
Unsupported,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<driver::Unsupported> for RemoteWakeupError {
|
|
|
|
fn from(_: driver::Unsupported) -> Self {
|
|
|
|
RemoteWakeupError::Unsupported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 01:34:35 +01:00
|
|
|
/// The bConfiguration value for the not configured state.
|
|
|
|
pub const CONFIGURATION_NONE: u8 = 0;
|
|
|
|
|
|
|
|
/// The bConfiguration value for the single configuration supported by this device.
|
|
|
|
pub const CONFIGURATION_VALUE: u8 = 1;
|
|
|
|
|
2022-04-23 04:40:57 +02:00
|
|
|
const STRING_INDEX_MANUFACTURER: u8 = 1;
|
|
|
|
const STRING_INDEX_PRODUCT: u8 = 2;
|
|
|
|
const STRING_INDEX_SERIAL_NUMBER: u8 = 3;
|
|
|
|
const STRING_INDEX_CUSTOM_START: u8 = 4;
|
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
/// Handler for device events and control requests.
|
|
|
|
///
|
|
|
|
/// All methods are optional callbacks that will be called by
|
|
|
|
/// [`UsbDevice::run()`](crate::UsbDevice::run)
|
|
|
|
pub trait Handler {
|
2022-04-13 19:09:08 +02:00
|
|
|
/// Called when the USB device has been enabled or disabled.
|
2023-02-07 22:49:14 +01:00
|
|
|
fn enabled(&mut self, _enabled: bool) {}
|
2022-04-13 19:09:08 +02:00
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
/// Called after a USB reset after the bus reset sequence is complete.
|
|
|
|
fn reset(&mut self) {}
|
2022-04-10 21:41:51 +02:00
|
|
|
|
|
|
|
/// Called when the host has set the address of the device to `addr`.
|
2023-02-07 22:49:14 +01:00
|
|
|
fn addressed(&mut self, _addr: u8) {}
|
2022-04-10 21:41:51 +02:00
|
|
|
|
|
|
|
/// Called when the host has enabled or disabled the configuration of the device.
|
2023-02-07 22:49:14 +01:00
|
|
|
fn configured(&mut self, _configured: bool) {}
|
2022-04-10 21:41:51 +02:00
|
|
|
|
|
|
|
/// Called when the bus has entered or exited the suspend state.
|
2023-02-07 22:49:14 +01:00
|
|
|
fn suspended(&mut self, _suspended: bool) {}
|
2022-04-10 21:41:51 +02:00
|
|
|
|
|
|
|
/// Called when remote wakeup feature is enabled or disabled.
|
2023-02-07 22:49:14 +01:00
|
|
|
fn remote_wakeup_enabled(&mut self, _enabled: bool) {}
|
|
|
|
|
|
|
|
/// Called when a "set alternate setting" control request is done on the interface.
|
|
|
|
fn set_alternate_setting(&mut self, iface: InterfaceNumber, alternate_setting: u8) {
|
|
|
|
let _ = iface;
|
|
|
|
let _ = alternate_setting;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when a control request is received with direction HostToDevice.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `req` - The request from the SETUP packet.
|
|
|
|
/// * `data` - The data from the request.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// If you didn't handle this request (for example if it's for the wrong interface), return
|
|
|
|
/// `None`. In this case, the the USB stack will continue calling the other handlers, to see
|
|
|
|
/// if another handles it.
|
|
|
|
///
|
|
|
|
/// If you did, return `Some` with either `Accepted` or `Rejected`. This will make the USB stack
|
|
|
|
/// respond to the control request, and stop calling other handlers.
|
|
|
|
fn control_out(&mut self, req: Request, data: &[u8]) -> Option<OutResponse> {
|
|
|
|
let _ = (req, data);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when a control request is received with direction DeviceToHost.
|
|
|
|
///
|
|
|
|
/// You should write the response somewhere (usually to `buf`, but you may use another buffer
|
|
|
|
/// owned by yourself, or a static buffer), then return `InResponse::Accepted(data)`.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `req` - The request from the SETUP packet.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// If you didn't handle this request (for example if it's for the wrong interface), return
|
|
|
|
/// `None`. In this case, the the USB stack will continue calling the other handlers, to see
|
|
|
|
/// if another handles it.
|
|
|
|
///
|
|
|
|
/// If you did, return `Some` with either `Accepted` or `Rejected`. This will make the USB stack
|
|
|
|
/// respond to the control request, and stop calling other handlers.
|
|
|
|
fn control_in<'a>(&'a mut self, req: Request, buf: &'a mut [u8]) -> Option<InResponse<'a>> {
|
|
|
|
let _ = (req, buf);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when a GET_DESCRIPTOR STRING control request is received.
|
|
|
|
fn get_string(&mut self, index: StringIndex, lang_id: u16) -> Option<&str> {
|
|
|
|
let _ = (index, lang_id);
|
|
|
|
None
|
|
|
|
}
|
2022-04-10 21:41:51 +02:00
|
|
|
}
|
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
struct Interface {
|
2022-04-16 04:47:27 +02:00
|
|
|
current_alt_setting: u8,
|
|
|
|
num_alt_settings: u8,
|
|
|
|
}
|
|
|
|
|
2023-01-31 22:27:19 +01:00
|
|
|
/// Main struct for the USB device stack.
|
2022-04-13 19:09:08 +02:00
|
|
|
pub struct UsbDevice<'d, D: Driver<'d>> {
|
2022-05-09 02:11:02 +02:00
|
|
|
control_buf: &'d mut [u8],
|
2022-05-09 03:25:21 +02:00
|
|
|
control: D::ControlPipe,
|
2022-05-09 02:11:02 +02:00
|
|
|
inner: Inner<'d, D>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Inner<'d, D: Driver<'d>> {
|
2022-03-09 23:06:27 +01:00
|
|
|
bus: D::Bus,
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
config: Config<'d>,
|
|
|
|
device_descriptor: &'d [u8],
|
|
|
|
config_descriptor: &'d [u8],
|
|
|
|
bos_descriptor: &'d [u8],
|
|
|
|
|
|
|
|
device_state: UsbDeviceState,
|
2022-04-10 21:41:51 +02:00
|
|
|
suspended: bool,
|
2022-03-09 01:34:35 +01:00
|
|
|
remote_wakeup_enabled: bool,
|
|
|
|
self_powered: bool,
|
2022-05-30 00:03:36 +02:00
|
|
|
|
|
|
|
/// Our device address, or 0 if none.
|
|
|
|
address: u8,
|
2023-01-11 17:47:12 +01:00
|
|
|
/// SET_ADDRESS requests have special handling depending on the driver.
|
|
|
|
/// This flag indicates that requests must be handled by `ControlPipe::accept_set_address()`
|
|
|
|
/// instead of regular `accept()`.
|
2022-05-30 00:03:36 +02:00
|
|
|
set_address_pending: bool,
|
2022-03-25 21:46:14 +01:00
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
interfaces: Vec<Interface, MAX_INTERFACE_COUNT>,
|
|
|
|
handlers: Vec<&'d mut dyn Handler, MAX_HANDLER_COUNT>,
|
|
|
|
|
2023-02-02 22:13:16 +01:00
|
|
|
#[cfg(feature = "msos-descriptor")]
|
|
|
|
msos_descriptor: crate::msos::MsOsDescriptorSet<'d>,
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 19:09:08 +02:00
|
|
|
impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
|
2022-04-10 21:41:51 +02:00
|
|
|
pub(crate) fn build(
|
2022-05-10 16:53:42 +02:00
|
|
|
driver: D,
|
2022-03-09 01:34:35 +01:00
|
|
|
config: Config<'d>,
|
2023-02-07 22:49:14 +01:00
|
|
|
handlers: Vec<&'d mut dyn Handler, MAX_HANDLER_COUNT>,
|
2022-03-09 01:34:35 +01:00
|
|
|
device_descriptor: &'d [u8],
|
|
|
|
config_descriptor: &'d [u8],
|
|
|
|
bos_descriptor: &'d [u8],
|
2023-02-07 22:49:14 +01:00
|
|
|
interfaces: Vec<Interface, MAX_INTERFACE_COUNT>,
|
2022-03-29 21:09:24 +02:00
|
|
|
control_buf: &'d mut [u8],
|
2023-02-02 22:13:16 +01:00
|
|
|
#[cfg(feature = "msos-descriptor")] msos_descriptor: crate::msos::MsOsDescriptorSet<'d>,
|
2022-04-13 19:09:08 +02:00
|
|
|
) -> UsbDevice<'d, D> {
|
2022-05-10 16:53:42 +02:00
|
|
|
// Start the USB bus.
|
2022-03-09 01:34:35 +01:00
|
|
|
// This prevent further allocation by consuming the driver.
|
2022-05-10 16:53:42 +02:00
|
|
|
let (bus, control) = driver.start(config.max_packet_size_0 as u16);
|
2022-03-09 01:34:35 +01:00
|
|
|
|
|
|
|
Self {
|
2022-03-29 21:09:24 +02:00
|
|
|
control_buf,
|
2022-05-09 03:25:21 +02:00
|
|
|
control,
|
2022-05-09 02:11:02 +02:00
|
|
|
inner: Inner {
|
|
|
|
bus,
|
|
|
|
config,
|
|
|
|
device_descriptor,
|
|
|
|
config_descriptor,
|
|
|
|
bos_descriptor,
|
|
|
|
|
2022-07-08 07:30:15 +02:00
|
|
|
device_state: UsbDeviceState::Unpowered,
|
2022-05-09 02:11:02 +02:00
|
|
|
suspended: false,
|
|
|
|
remote_wakeup_enabled: false,
|
|
|
|
self_powered: false,
|
2022-05-30 00:03:36 +02:00
|
|
|
address: 0,
|
|
|
|
set_address_pending: false,
|
2022-05-09 02:11:02 +02:00
|
|
|
interfaces,
|
2023-02-07 22:49:14 +01:00
|
|
|
handlers,
|
2023-02-02 22:13:16 +01:00
|
|
|
#[cfg(feature = "msos-descriptor")]
|
2023-01-12 21:59:25 +01:00
|
|
|
msos_descriptor,
|
2022-05-09 02:11:02 +02:00
|
|
|
},
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 22:04:31 +02:00
|
|
|
/// Runs the `UsbDevice` forever.
|
|
|
|
///
|
|
|
|
/// This future may leave the bus in an invalid state if it is dropped.
|
|
|
|
/// After dropping the future, [`UsbDevice::disable()`] should be called
|
|
|
|
/// before calling any other `UsbDevice` methods to fully reset the
|
|
|
|
/// peripheral.
|
2022-04-10 21:41:51 +02:00
|
|
|
pub async fn run(&mut self) -> ! {
|
2022-04-13 22:04:31 +02:00
|
|
|
loop {
|
|
|
|
self.run_until_suspend().await;
|
|
|
|
self.wait_resume().await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the `UsbDevice` until the bus is suspended.
|
|
|
|
///
|
|
|
|
/// This future may leave the bus in an invalid state if it is dropped.
|
|
|
|
/// After dropping the future, [`UsbDevice::disable()`] should be called
|
|
|
|
/// before calling any other `UsbDevice` methods to fully reset the
|
|
|
|
/// peripheral.
|
|
|
|
pub async fn run_until_suspend(&mut self) -> () {
|
2022-07-08 07:30:15 +02:00
|
|
|
while !self.inner.suspended {
|
2022-03-25 21:46:14 +01:00
|
|
|
let control_fut = self.control.setup();
|
2022-05-09 02:11:02 +02:00
|
|
|
let bus_fut = self.inner.bus.poll();
|
2022-04-13 19:09:08 +02:00
|
|
|
match select(bus_fut, control_fut).await {
|
2022-07-08 07:30:15 +02:00
|
|
|
Either::First(evt) => self.inner.handle_bus_event(evt).await,
|
2022-05-09 03:25:21 +02:00
|
|
|
Either::Second(req) => self.handle_control(req).await,
|
2022-04-10 21:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 22:04:31 +02:00
|
|
|
/// Disables the USB peripheral.
|
2022-04-13 19:09:08 +02:00
|
|
|
pub async fn disable(&mut self) {
|
2022-05-09 02:11:02 +02:00
|
|
|
if self.inner.device_state != UsbDeviceState::Disabled {
|
|
|
|
self.inner.bus.disable().await;
|
|
|
|
self.inner.device_state = UsbDeviceState::Disabled;
|
|
|
|
self.inner.suspended = false;
|
|
|
|
self.inner.remote_wakeup_enabled = false;
|
2022-04-13 19:09:08 +02:00
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.inner.handlers {
|
2022-04-13 19:09:08 +02:00
|
|
|
h.enabled(false);
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-13 19:09:08 +02:00
|
|
|
}
|
2022-04-10 21:41:51 +02:00
|
|
|
|
2022-04-13 22:04:31 +02:00
|
|
|
/// Waits for a resume condition on the USB bus.
|
|
|
|
///
|
|
|
|
/// This future is cancel-safe.
|
|
|
|
pub async fn wait_resume(&mut self) {
|
2022-07-08 07:30:15 +02:00
|
|
|
while self.inner.suspended {
|
2022-05-09 02:11:02 +02:00
|
|
|
let evt = self.inner.bus.poll().await;
|
2022-07-08 07:30:15 +02:00
|
|
|
self.inner.handle_bus_event(evt).await;
|
2022-04-13 22:04:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initiates a device remote wakeup on the USB bus.
|
|
|
|
///
|
|
|
|
/// If the bus is not suspended or remote wakeup is not enabled, an error
|
|
|
|
/// will be returned.
|
|
|
|
///
|
|
|
|
/// This future may leave the bus in an inconsistent state if dropped.
|
|
|
|
/// After dropping the future, [`UsbDevice::disable()`] should be called
|
|
|
|
/// before calling any other `UsbDevice` methods to fully reset the peripheral.
|
2022-04-13 19:09:08 +02:00
|
|
|
pub async fn remote_wakeup(&mut self) -> Result<(), RemoteWakeupError> {
|
2022-05-09 02:11:02 +02:00
|
|
|
if self.inner.suspended && self.inner.remote_wakeup_enabled {
|
|
|
|
self.inner.bus.remote_wakeup().await?;
|
|
|
|
self.inner.suspended = false;
|
2022-04-13 19:09:08 +02:00
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.inner.handlers {
|
2022-04-13 19:09:08 +02:00
|
|
|
h.suspended(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(RemoteWakeupError::InvalidState)
|
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-05-09 03:25:21 +02:00
|
|
|
|
2022-05-12 18:14:48 +02:00
|
|
|
async fn handle_control(&mut self, req: [u8; 8]) {
|
|
|
|
let req = Request::parse(&req);
|
|
|
|
|
2022-09-24 18:42:06 +02:00
|
|
|
trace!("control request: {:?}", req);
|
2022-05-09 03:25:21 +02:00
|
|
|
|
|
|
|
match req.direction {
|
2022-09-26 12:29:27 +02:00
|
|
|
Direction::In => self.handle_control_in(req).await,
|
|
|
|
Direction::Out => self.handle_control_out(req).await,
|
2022-05-09 03:25:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_control_in(&mut self, req: Request) {
|
|
|
|
let mut resp_length = req.length as usize;
|
|
|
|
let max_packet_size = self.control.max_packet_size();
|
|
|
|
|
|
|
|
// If we don't have an address yet, respond with max 1 packet.
|
|
|
|
// The host doesn't know our EP0 max packet size yet, and might assume
|
|
|
|
// a full-length packet is a short packet, thinking we're done sending data.
|
|
|
|
// See https://github.com/hathach/tinyusb/issues/184
|
|
|
|
const DEVICE_DESCRIPTOR_LEN: usize = 18;
|
2022-05-30 00:03:36 +02:00
|
|
|
if self.inner.address == 0
|
2022-05-09 03:25:21 +02:00
|
|
|
&& max_packet_size < DEVICE_DESCRIPTOR_LEN
|
|
|
|
&& (max_packet_size as usize) < resp_length
|
|
|
|
{
|
|
|
|
trace!("received control req while not addressed: capping response to 1 packet.");
|
|
|
|
resp_length = max_packet_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.inner.handle_control_in(req, &mut self.control_buf) {
|
|
|
|
InResponse::Accepted(data) => {
|
|
|
|
let len = data.len().min(resp_length);
|
|
|
|
let need_zlp = len != resp_length && (len % usize::from(max_packet_size)) == 0;
|
|
|
|
|
2022-05-30 00:08:28 +02:00
|
|
|
let chunks = data[0..len]
|
2022-05-09 03:25:21 +02:00
|
|
|
.chunks(max_packet_size)
|
|
|
|
.chain(need_zlp.then(|| -> &[u8] { &[] }));
|
|
|
|
|
2022-05-30 00:08:28 +02:00
|
|
|
for (first, last, chunk) in first_last(chunks) {
|
|
|
|
match self.control.data_in(chunk, first, last).await {
|
2022-05-09 03:25:21 +02:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => {
|
|
|
|
warn!("control accept_in failed: {:?}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 00:35:27 +02:00
|
|
|
InResponse::Rejected => self.control.reject().await,
|
2022-05-09 03:25:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_control_out(&mut self, req: Request) {
|
|
|
|
let req_length = req.length as usize;
|
|
|
|
let max_packet_size = self.control.max_packet_size();
|
|
|
|
let mut total = 0;
|
|
|
|
|
2022-05-30 00:08:28 +02:00
|
|
|
let chunks = self.control_buf[..req_length].chunks_mut(max_packet_size);
|
|
|
|
for (first, last, chunk) in first_last(chunks) {
|
|
|
|
let size = match self.control.data_out(chunk, first, last).await {
|
2022-05-09 03:25:21 +02:00
|
|
|
Ok(x) => x,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("usb: failed to read CONTROL OUT data stage: {:?}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
total += size;
|
|
|
|
if size < max_packet_size || total == req_length {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = &self.control_buf[0..total];
|
|
|
|
#[cfg(feature = "defmt")]
|
|
|
|
trace!(" control out data: {:02x}", data);
|
|
|
|
#[cfg(not(feature = "defmt"))]
|
|
|
|
trace!(" control out data: {:02x?}", data);
|
|
|
|
|
|
|
|
match self.inner.handle_control_out(req, data) {
|
2023-01-11 17:47:12 +01:00
|
|
|
OutResponse::Accepted => {
|
|
|
|
if self.inner.set_address_pending {
|
|
|
|
self.control.accept_set_address(self.inner.address).await;
|
|
|
|
self.inner.set_address_pending = false;
|
|
|
|
} else {
|
|
|
|
self.control.accept().await
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 00:35:27 +02:00
|
|
|
OutResponse::Rejected => self.control.reject().await,
|
2022-05-09 03:25:21 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
}
|
2022-03-09 01:34:35 +01:00
|
|
|
|
2022-05-09 02:11:02 +02:00
|
|
|
impl<'d, D: Driver<'d>> Inner<'d, D> {
|
2022-07-08 07:30:15 +02:00
|
|
|
async fn handle_bus_event(&mut self, evt: Event) {
|
2022-04-13 22:04:31 +02:00
|
|
|
match evt {
|
|
|
|
Event::Reset => {
|
|
|
|
trace!("usb: reset");
|
|
|
|
self.device_state = UsbDeviceState::Default;
|
|
|
|
self.suspended = false;
|
|
|
|
self.remote_wakeup_enabled = false;
|
2022-05-30 00:03:36 +02:00
|
|
|
self.address = 0;
|
2022-04-13 22:04:31 +02:00
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.handlers {
|
|
|
|
h.reset();
|
2022-04-13 22:04:31 +02:00
|
|
|
}
|
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
for (i, iface) in self.interfaces.iter_mut().enumerate() {
|
|
|
|
iface.current_alt_setting = 0;
|
|
|
|
|
|
|
|
for h in &mut self.handlers {
|
|
|
|
h.set_alternate_setting(InterfaceNumber::new(i as _), 0);
|
|
|
|
}
|
2022-04-13 22:04:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Resume => {
|
|
|
|
trace!("usb: resume");
|
|
|
|
self.suspended = false;
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.handlers {
|
2022-04-13 22:04:31 +02:00
|
|
|
h.suspended(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Suspend => {
|
|
|
|
trace!("usb: suspend");
|
|
|
|
self.suspended = true;
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.handlers {
|
2022-04-13 22:04:31 +02:00
|
|
|
h.suspended(true);
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 08:08:58 +02:00
|
|
|
Event::PowerDetected => {
|
|
|
|
trace!("usb: power detected");
|
2022-07-08 07:30:15 +02:00
|
|
|
self.bus.enable().await;
|
|
|
|
self.device_state = UsbDeviceState::Default;
|
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.handlers {
|
2022-07-08 07:30:15 +02:00
|
|
|
h.enabled(true);
|
|
|
|
}
|
2022-06-16 08:08:58 +02:00
|
|
|
}
|
|
|
|
Event::PowerRemoved => {
|
|
|
|
trace!("usb: power removed");
|
2022-07-08 07:30:15 +02:00
|
|
|
self.bus.disable().await;
|
|
|
|
self.device_state = UsbDeviceState::Unpowered;
|
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.handlers {
|
2022-07-08 07:30:15 +02:00
|
|
|
h.enabled(false);
|
|
|
|
}
|
2022-06-16 08:08:58 +02:00
|
|
|
}
|
2022-04-13 22:04:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 02:11:02 +02:00
|
|
|
fn handle_control_out(&mut self, req: Request, data: &[u8]) -> OutResponse {
|
2022-03-09 01:34:35 +01:00
|
|
|
const CONFIGURATION_NONE_U16: u16 = CONFIGURATION_NONE as u16;
|
|
|
|
const CONFIGURATION_VALUE_U16: u16 = CONFIGURATION_VALUE as u16;
|
2022-03-29 23:13:16 +02:00
|
|
|
|
2022-03-28 03:16:45 +02:00
|
|
|
match (req.request_type, req.recipient) {
|
|
|
|
(RequestType::Standard, Recipient::Device) => match (req.request, req.value) {
|
|
|
|
(Request::CLEAR_FEATURE, Request::FEATURE_DEVICE_REMOTE_WAKEUP) => {
|
2022-03-09 01:34:35 +01:00
|
|
|
self.remote_wakeup_enabled = false;
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.handlers {
|
2022-04-10 21:41:51 +02:00
|
|
|
h.remote_wakeup_enabled(false);
|
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
OutResponse::Accepted
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
(Request::SET_FEATURE, Request::FEATURE_DEVICE_REMOTE_WAKEUP) => {
|
2022-03-09 01:34:35 +01:00
|
|
|
self.remote_wakeup_enabled = true;
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.handlers {
|
2022-04-10 21:41:51 +02:00
|
|
|
h.remote_wakeup_enabled(true);
|
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
OutResponse::Accepted
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-04-02 22:35:03 +02:00
|
|
|
(Request::SET_ADDRESS, addr @ 1..=127) => {
|
2022-05-30 00:03:36 +02:00
|
|
|
self.address = addr as u8;
|
|
|
|
self.set_address_pending = true;
|
2022-04-10 21:41:51 +02:00
|
|
|
self.device_state = UsbDeviceState::Addressed;
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.handlers {
|
2022-05-30 00:03:36 +02:00
|
|
|
h.addressed(self.address);
|
2022-04-10 21:41:51 +02:00
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
OutResponse::Accepted
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
(Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => {
|
2022-04-16 04:47:27 +02:00
|
|
|
debug!("SET_CONFIGURATION: configured");
|
2022-03-09 01:34:35 +01:00
|
|
|
self.device_state = UsbDeviceState::Configured;
|
2022-04-16 04:47:27 +02:00
|
|
|
|
|
|
|
// Enable all endpoints of selected alt settings.
|
|
|
|
foreach_endpoint(self.config_descriptor, |ep| {
|
2023-02-07 22:49:14 +01:00
|
|
|
let iface = &self.interfaces[ep.interface.0 as usize];
|
2022-06-12 22:15:44 +02:00
|
|
|
self.bus
|
|
|
|
.endpoint_set_enabled(ep.ep_address, iface.current_alt_setting == ep.interface_alt);
|
2022-04-16 04:47:27 +02:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
// Notify handlers.
|
|
|
|
for h in &mut self.handlers {
|
2022-04-10 21:41:51 +02:00
|
|
|
h.configured(true);
|
|
|
|
}
|
2022-04-16 04:47:27 +02:00
|
|
|
|
2022-05-09 02:11:02 +02:00
|
|
|
OutResponse::Accepted
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
(Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => match self.device_state {
|
2022-05-09 02:11:02 +02:00
|
|
|
UsbDeviceState::Default => OutResponse::Accepted,
|
2022-03-28 03:16:45 +02:00
|
|
|
_ => {
|
2022-04-16 04:47:27 +02:00
|
|
|
debug!("SET_CONFIGURATION: unconfigured");
|
2022-03-28 03:16:45 +02:00
|
|
|
self.device_state = UsbDeviceState::Addressed;
|
2022-04-16 04:47:27 +02:00
|
|
|
|
|
|
|
// Disable all endpoints.
|
|
|
|
foreach_endpoint(self.config_descriptor, |ep| {
|
|
|
|
self.bus.endpoint_set_enabled(ep.ep_address, false);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
// Notify handlers.
|
|
|
|
for h in &mut self.handlers {
|
2022-04-10 21:41:51 +02:00
|
|
|
h.configured(false);
|
|
|
|
}
|
2022-04-16 04:47:27 +02:00
|
|
|
|
2022-05-09 02:11:02 +02:00
|
|
|
OutResponse::Accepted
|
2022-03-28 03:16:45 +02:00
|
|
|
}
|
|
|
|
},
|
2022-05-09 02:11:02 +02:00
|
|
|
_ => OutResponse::Rejected,
|
2022-03-28 03:16:45 +02:00
|
|
|
},
|
2022-04-16 04:47:27 +02:00
|
|
|
(RequestType::Standard, Recipient::Interface) => {
|
2023-02-07 22:49:14 +01:00
|
|
|
let iface_num = InterfaceNumber::new(req.index as _);
|
|
|
|
let iface = match self.interfaces.get_mut(iface_num.0 as usize) {
|
2022-04-16 04:47:27 +02:00
|
|
|
Some(iface) => iface,
|
2022-05-09 02:11:02 +02:00
|
|
|
None => return OutResponse::Rejected,
|
2022-04-16 04:47:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
match req.request {
|
|
|
|
Request::SET_INTERFACE => {
|
|
|
|
let new_altsetting = req.value as u8;
|
|
|
|
|
|
|
|
if new_altsetting >= iface.num_alt_settings {
|
|
|
|
warn!("SET_INTERFACE: trying to select alt setting out of range.");
|
2022-05-09 02:11:02 +02:00
|
|
|
return OutResponse::Rejected;
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
iface.current_alt_setting = new_altsetting;
|
|
|
|
|
|
|
|
// Enable/disable EPs of this interface as needed.
|
|
|
|
foreach_endpoint(self.config_descriptor, |ep| {
|
2023-02-07 22:49:14 +01:00
|
|
|
if ep.interface == iface_num {
|
2022-06-12 22:15:44 +02:00
|
|
|
self.bus
|
|
|
|
.endpoint_set_enabled(ep.ep_address, iface.current_alt_setting == ep.interface_alt);
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// TODO check it is valid (not out of range)
|
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
for h in &mut self.handlers {
|
|
|
|
h.set_alternate_setting(iface_num, new_altsetting);
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
OutResponse::Accepted
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
_ => OutResponse::Rejected,
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
(RequestType::Standard, Recipient::Endpoint) => match (req.request, req.value) {
|
|
|
|
(Request::SET_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
|
|
|
|
let ep_addr = ((req.index as u8) & 0x8f).into();
|
2022-04-16 04:47:27 +02:00
|
|
|
self.bus.endpoint_set_stalled(ep_addr, true);
|
2022-05-09 02:11:02 +02:00
|
|
|
OutResponse::Accepted
|
2022-03-28 03:16:45 +02:00
|
|
|
}
|
|
|
|
(Request::CLEAR_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
|
|
|
|
let ep_addr = ((req.index as u8) & 0x8f).into();
|
2022-04-16 04:47:27 +02:00
|
|
|
self.bus.endpoint_set_stalled(ep_addr, false);
|
2022-05-09 02:11:02 +02:00
|
|
|
OutResponse::Accepted
|
2022-03-28 03:16:45 +02:00
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
_ => OutResponse::Rejected,
|
2022-03-09 01:34:35 +01:00
|
|
|
},
|
2023-02-07 22:49:14 +01:00
|
|
|
_ => self.handle_control_out_delegated(req, data),
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 02:11:02 +02:00
|
|
|
fn handle_control_in<'a>(&'a mut self, req: Request, buf: &'a mut [u8]) -> InResponse<'a> {
|
2022-03-28 03:16:45 +02:00
|
|
|
match (req.request_type, req.recipient) {
|
|
|
|
(RequestType::Standard, Recipient::Device) => match req.request {
|
|
|
|
Request::GET_STATUS => {
|
2022-03-09 01:34:35 +01:00
|
|
|
let mut status: u16 = 0x0000;
|
|
|
|
if self.self_powered {
|
|
|
|
status |= 0x0001;
|
|
|
|
}
|
|
|
|
if self.remote_wakeup_enabled {
|
|
|
|
status |= 0x0002;
|
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
buf[..2].copy_from_slice(&status.to_le_bytes());
|
|
|
|
InResponse::Accepted(&buf[..2])
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
Request::GET_DESCRIPTOR => self.handle_get_descriptor(req, buf),
|
2022-03-28 03:16:45 +02:00
|
|
|
Request::GET_CONFIGURATION => {
|
2022-03-09 01:34:35 +01:00
|
|
|
let status = match self.device_state {
|
|
|
|
UsbDeviceState::Configured => CONFIGURATION_VALUE,
|
|
|
|
_ => CONFIGURATION_NONE,
|
|
|
|
};
|
2022-05-09 02:11:02 +02:00
|
|
|
buf[0] = status;
|
|
|
|
InResponse::Accepted(&buf[..1])
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
_ => InResponse::Rejected,
|
2022-03-28 03:16:45 +02:00
|
|
|
},
|
2022-04-16 04:47:27 +02:00
|
|
|
(RequestType::Standard, Recipient::Interface) => {
|
|
|
|
let iface = match self.interfaces.get_mut(req.index as usize) {
|
|
|
|
Some(iface) => iface,
|
2022-05-09 02:11:02 +02:00
|
|
|
None => return InResponse::Rejected,
|
2022-04-16 04:47:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
match req.request {
|
|
|
|
Request::GET_STATUS => {
|
|
|
|
let status: u16 = 0;
|
2022-05-09 02:11:02 +02:00
|
|
|
buf[..2].copy_from_slice(&status.to_le_bytes());
|
|
|
|
InResponse::Accepted(&buf[..2])
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
|
|
|
Request::GET_INTERFACE => {
|
2022-05-09 02:11:02 +02:00
|
|
|
buf[0] = iface.current_alt_setting;
|
|
|
|
InResponse::Accepted(&buf[..1])
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
2023-02-07 22:49:14 +01:00
|
|
|
_ => self.handle_control_in_delegated(req, buf),
|
2022-04-16 04:47:27 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 03:16:45 +02:00
|
|
|
(RequestType::Standard, Recipient::Endpoint) => match req.request {
|
|
|
|
Request::GET_STATUS => {
|
|
|
|
let ep_addr: EndpointAddress = ((req.index as u8) & 0x8f).into();
|
|
|
|
let mut status: u16 = 0x0000;
|
2022-04-16 04:47:27 +02:00
|
|
|
if self.bus.endpoint_is_stalled(ep_addr) {
|
2022-03-28 03:16:45 +02:00
|
|
|
status |= 0x0001;
|
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
buf[..2].copy_from_slice(&status.to_le_bytes());
|
|
|
|
InResponse::Accepted(&buf[..2])
|
2022-03-28 03:16:45 +02:00
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
_ => InResponse::Rejected,
|
2022-03-28 03:16:45 +02:00
|
|
|
},
|
2023-02-02 22:13:16 +01:00
|
|
|
#[cfg(feature = "msos-descriptor")]
|
2023-01-12 21:59:25 +01:00
|
|
|
(RequestType::Vendor, Recipient::Device) => {
|
2023-02-07 22:49:14 +01:00
|
|
|
if !self.msos_descriptor.is_empty()
|
|
|
|
&& req.request == self.msos_descriptor.vendor_code()
|
|
|
|
&& req.index == 7
|
|
|
|
{
|
|
|
|
// Index 7 retrieves the MS OS Descriptor Set
|
|
|
|
InResponse::Accepted(self.msos_descriptor.descriptor())
|
2023-01-12 21:59:25 +01:00
|
|
|
} else {
|
2023-02-07 22:49:14 +01:00
|
|
|
self.handle_control_in_delegated(req, buf)
|
2023-01-12 21:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 22:49:14 +01:00
|
|
|
_ => self.handle_control_in_delegated(req, buf),
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:49:14 +01:00
|
|
|
fn handle_control_out_delegated(&mut self, req: Request, data: &[u8]) -> OutResponse {
|
|
|
|
for h in &mut self.handlers {
|
|
|
|
if let Some(res) = h.control_out(req, data) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OutResponse::Rejected
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_control_in_delegated<'a>(&'a mut self, req: Request, buf: &'a mut [u8]) -> InResponse<'a> {
|
|
|
|
unsafe fn extend_lifetime<'x, 'y>(r: InResponse<'x>) -> InResponse<'y> {
|
|
|
|
core::mem::transmute(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
for h in &mut self.handlers {
|
|
|
|
if let Some(res) = h.control_in(req, buf) {
|
|
|
|
// safety: the borrow checker isn't smart enough to know this pattern (returning a
|
|
|
|
// borrowed value from inside the loop) is sound. Workaround by unsafely extending lifetime.
|
|
|
|
// Also, Polonius (the WIP new borrow checker) does accept it.
|
|
|
|
|
|
|
|
return unsafe { extend_lifetime(res) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InResponse::Rejected
|
|
|
|
}
|
|
|
|
|
2022-05-09 02:11:02 +02:00
|
|
|
fn handle_get_descriptor<'a>(&'a mut self, req: Request, buf: &'a mut [u8]) -> InResponse<'a> {
|
2022-03-09 01:34:35 +01:00
|
|
|
let (dtype, index) = req.descriptor_type_index();
|
|
|
|
|
|
|
|
match dtype {
|
2022-05-09 02:11:02 +02:00
|
|
|
descriptor_type::BOS => InResponse::Accepted(self.bos_descriptor),
|
|
|
|
descriptor_type::DEVICE => InResponse::Accepted(self.device_descriptor),
|
|
|
|
descriptor_type::CONFIGURATION => InResponse::Accepted(self.config_descriptor),
|
2022-03-09 01:34:35 +01:00
|
|
|
descriptor_type::STRING => {
|
|
|
|
if index == 0 {
|
2022-05-09 02:11:02 +02:00
|
|
|
buf[0] = 4; // len
|
|
|
|
buf[1] = descriptor_type::STRING;
|
|
|
|
buf[2] = lang_id::ENGLISH_US as u8;
|
|
|
|
buf[3] = (lang_id::ENGLISH_US >> 8) as u8;
|
|
|
|
InResponse::Accepted(&buf[..4])
|
2022-03-09 01:34:35 +01:00
|
|
|
} else {
|
|
|
|
let s = match index {
|
2022-04-23 04:40:57 +02:00
|
|
|
STRING_INDEX_MANUFACTURER => self.config.manufacturer,
|
|
|
|
STRING_INDEX_PRODUCT => self.config.product,
|
|
|
|
STRING_INDEX_SERIAL_NUMBER => self.config.serial_number,
|
2022-03-09 01:34:35 +01:00
|
|
|
_ => {
|
2023-02-07 22:49:14 +01:00
|
|
|
let mut s = None;
|
|
|
|
for handler in &mut self.handlers {
|
|
|
|
let index = StringIndex::new(index);
|
|
|
|
let lang_id = req.index;
|
|
|
|
if let Some(res) = handler.get_string(index, lang_id) {
|
|
|
|
s = Some(res);
|
2022-04-23 04:40:57 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 22:49:14 +01:00
|
|
|
s
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(s) = s {
|
2022-05-09 02:11:02 +02:00
|
|
|
if buf.len() < 2 {
|
|
|
|
panic!("control buffer too small");
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[1] = descriptor_type::STRING;
|
|
|
|
let mut pos = 2;
|
|
|
|
for c in s.encode_utf16() {
|
2023-01-11 17:47:12 +01:00
|
|
|
if pos + 2 >= buf.len() {
|
2022-05-09 02:11:02 +02:00
|
|
|
panic!("control buffer too small");
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[pos..pos + 2].copy_from_slice(&c.to_le_bytes());
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[0] = pos as u8;
|
|
|
|
InResponse::Accepted(&buf[..pos])
|
2022-03-09 01:34:35 +01:00
|
|
|
} else {
|
2022-05-09 02:11:02 +02:00
|
|
|
InResponse::Rejected
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-09 02:11:02 +02:00
|
|
|
_ => InResponse::Rejected,
|
2022-03-09 01:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 00:08:28 +02:00
|
|
|
|
|
|
|
fn first_last<T: Iterator>(iter: T) -> impl Iterator<Item = (bool, bool, T::Item)> {
|
|
|
|
let mut iter = iter.peekable();
|
|
|
|
let mut first = true;
|
|
|
|
core::iter::from_fn(move || {
|
|
|
|
let val = iter.next()?;
|
|
|
|
let is_first = first;
|
|
|
|
first = false;
|
|
|
|
let is_last = iter.peek().is_none();
|
|
|
|
Some((is_first, is_last, val))
|
|
|
|
})
|
|
|
|
}
|