From aaa90fb87fc4585ca7e2e3d22baa9ea3eca0af20 Mon Sep 17 00:00:00 2001 From: Nicolas Stalder Date: Mon, 10 May 2021 20:55:43 +0200 Subject: [PATCH] Make usbd-ccid generic to avoid apdu-dispatch dependency --- Cargo.toml | 1 - src/class.rs | 20 +++++++++++++------- src/pipe.rs | 49 ++++++++++++++++++++++++++----------------------- src/types.rs | 2 +- 4 files changed, 40 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8a0b3f5..e265d9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -apdu-dispatch = { git = "https://github.com/solokeys/apdu-dispatch", branch = "main" } delog = "0.1.0" embedded-time = "0.10.1" heapless = "0.6" diff --git a/src/class.rs b/src/class.rs index 04bba64..0bf7592 100644 --- a/src/class.rs +++ b/src/class.rs @@ -1,8 +1,8 @@ use core::convert::TryFrom; use embedded_time::duration::Extensions; -use interchange::Requester; -use apdu_dispatch::interchanges; +use heapless_bytes::Bytes; +use interchange::{Interchange, Requester}; use crate::{ constants::*, @@ -17,24 +17,28 @@ use crate::{ use usb_device::class_prelude::*; type Result = core::result::Result; -pub struct Ccid +pub struct Ccid where Bus: 'static + UsbBus, + I: 'static + Interchange, RESPONSE = Bytes>, + N: heapless::ArrayLength, { interface_number: InterfaceNumber, string_index: StringIndex, read: EndpointOut<'static, Bus>, // interrupt: EndpointIn<'static, Bus>, - pipe: Pipe, + pipe: Pipe, } -impl Ccid +impl Ccid where Bus: 'static + UsbBus, + I: 'static + Interchange, RESPONSE = Bytes>, + N: heapless::ArrayLength, { pub fn new( allocator: &'static UsbBusAllocator, - request_pipe: Requester, + request_pipe: Requester, ) -> Self { let read = allocator.bulk(PACKET_SIZE as _); let write = allocator.bulk(PACKET_SIZE as _); @@ -75,9 +79,11 @@ where } } -impl UsbClass for Ccid +impl UsbClass for Ccid where Bus: 'static + UsbBus, + I: 'static + Interchange, RESPONSE = Bytes>, + N: heapless::ArrayLength, { fn get_configuration_descriptors(&self, writer: &mut DescriptorWriter) -> Result<()> diff --git a/src/pipe.rs b/src/pipe.rs index c5b3e70..c8e829e 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -1,24 +1,21 @@ use core::convert::TryFrom; -use apdu_dispatch::interchanges; +use heapless_bytes::Bytes; use interchange::{Interchange, Requester}; use crate::{ constants::*, - types::{ - MessageBuffer, - packet::{ - Chain, - Command as PacketCommand, - DataBlock, - Error as PacketError, - ExtPacket, - RawPacket, - XfrBlock, + types::packet::{ + Chain, + Command as PacketCommand, + DataBlock, + Error as PacketError, + ExtPacket, + RawPacket, + XfrBlock, - ChainedPacket as _, - PacketWithData as _, - }, + ChainedPacket as _, + PacketWithData as _, }, }; @@ -44,15 +41,17 @@ enum Error { CommandNotSupported = 0x00, } -pub struct Pipe +pub struct Pipe where - Bus: UsbBus + 'static, + Bus: 'static + UsbBus, + I: 'static + Interchange, RESPONSE = Bytes>, + N: heapless::ArrayLength, { pub(crate) write: EndpointIn<'static, Bus>, // pub(crate) rpc: TransportEndpoint<'rpc>, seq: u8, state: State, - interchange: Requester, + interchange: Requester, sent: usize, outbox: Option, @@ -65,13 +64,15 @@ where pub(crate) started_processing: bool, } -impl Pipe +impl Pipe where Bus: 'static + UsbBus, + I: 'static + Interchange, RESPONSE = Bytes>, + N: heapless::ArrayLength, { pub(crate) fn new( write: EndpointIn<'static, Bus>, - request_pipe: Requester, + request_pipe: Requester, ) -> Self { assert!(MAX_MSG_LENGTH >= PACKET_SIZE); @@ -101,9 +102,11 @@ where } -impl Pipe +impl Pipe where - Bus: 'static + UsbBus + Bus: 'static + UsbBus, + I: 'static + Interchange, RESPONSE = Bytes>, + N: heapless::ArrayLength, { pub fn handle_packet(&mut self, packet: RawPacket) { use crate::types::packet::RawPacketExt; @@ -187,7 +190,7 @@ where #[inline(never)] fn reset_interchange(&mut self) { - let message = MessageBuffer::new(); + let message = Bytes::new(); self.interchange.take_response(); // this may no longer be needed // before the interchange change (adding the request_mut method), @@ -337,7 +340,7 @@ where if self.outbox.is_some() { panic!(); } // if let Some(message) = self.interchange.response() { - let message: &mut MessageBuffer = unsafe { self.interchange.interchange.rp_mut() }; + let message: &mut Bytes = unsafe { self.interchange.interchange.rp_mut() }; let chunk_size = core::cmp::min(PACKET_SIZE - 10, message.len() - self.sent); let chunk = &message[self.sent..][..chunk_size]; diff --git a/src/types.rs b/src/types.rs index 3e2e324..d360bfe 100644 --- a/src/types.rs +++ b/src/types.rs @@ -4,7 +4,7 @@ use embedded_time::duration::Milliseconds; pub mod packet; pub mod tlv; -pub type MessageBuffer = apdu_dispatch::interchanges::Data; +// pub type MessageBuffer = apdu_dispatch::interchanges::Data; #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum ClassRequest {