use fugit::ExtU32;
use usb_device::bus::UsbBus;
use usb_device::class_prelude::UsbBusAllocator;
use crate::hid_class::prelude::*;
use crate::interface::raw::{RawInterface, RawInterfaceConfig};
use crate::interface::{InterfaceClass, UsbAllocatable};
use crate::UsbHidError;
#[rustfmt::skip]
pub const FIDO_REPORT_DESCRIPTOR: &[u8] = &[
0x06, 0xD0, 0xF1, 0x09, 0x01, 0xA1, 0x01, 0x09, 0x20, 0x15, 0x00, 0x26, 0xFF, 0x00, 0x75, 0x08, 0x95, 0x40, 0x81, 0x02, 0x09, 0x21, 0x15, 0x00, 0x26, 0xFF, 0x00, 0x75, 0x08, 0x95, 0x40, 0x91, 0x02, 0xC0, ];
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C, align(8))]
pub struct RawFidoMsg {
pub packet: [u8; 64],
}
impl Default for RawFidoMsg {
fn default() -> Self {
Self { packet: [0u8; 64] }
}
}
pub struct RawFidoInterface<'a, B: UsbBus> {
inner: RawInterface<'a, B>,
}
impl<'a, B: UsbBus> RawFidoInterface<'a, B> {
pub fn write_report(&mut self, report: &RawFidoMsg) -> Result<(), UsbHidError> {
self.inner
.write_report(&report.packet)
.map(|_| ())
.map_err(UsbHidError::from)
}
pub fn read_report(&mut self) -> usb_device::Result<RawFidoMsg> {
let mut report = RawFidoMsg::default();
match self.inner.read_report(&mut report.packet) {
Err(e) => Err(e),
Ok(_) => Ok(report),
}
}
}
impl<'a, B: UsbBus> InterfaceClass<'a, B> for RawFidoInterface<'a, B> {
fn interface(&mut self) -> &mut RawInterface<'a, B> {
&mut self.inner
}
fn reset(&mut self) {}
}
pub struct RawFidoConfig<'a> {
interface: RawInterfaceConfig<'a>,
}
impl<'a> Default for RawFidoConfig<'a> {
#[must_use]
fn default() -> Self {
Self::new(
unwrap!(
unwrap!(unwrap!(RawInterfaceBuilder::new(FIDO_REPORT_DESCRIPTOR))
.description("U2F Token")
.in_endpoint(UsbPacketSize::Bytes64, 5.millis()))
.with_out_endpoint(UsbPacketSize::Bytes64, 5.millis())
)
.build(),
)
}
}
impl<'a> RawFidoConfig<'a> {
#[must_use]
pub fn new(interface: RawInterfaceConfig<'a>) -> Self {
Self { interface }
}
}
impl<'a, B: UsbBus + 'a> UsbAllocatable<'a, B> for RawFidoConfig<'a> {
type Allocated = RawFidoInterface<'a, B>;
fn allocate(self, usb_alloc: &'a UsbBusAllocator<B>) -> Self::Allocated {
Self::Allocated {
inner: RawInterface::new(usb_alloc, self.interface),
}
}
}