Make usbd-ccid generic to avoid apdu-dispatch dependency

This commit is contained in:
Nicolas Stalder
2021-05-10 21:15:10 +02:00
committed by Nicolas Stalder
parent cf4d8fd4e2
commit aaa90fb87f
4 changed files with 40 additions and 32 deletions
-1
View File
@@ -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"
+13 -7
View File
@@ -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<T> = core::result::Result<T, UsbError>;
pub struct Ccid<Bus>
pub struct Ccid<Bus, I, N>
where
Bus: 'static + UsbBus,
I: 'static + Interchange<REQUEST = Bytes<N>, RESPONSE = Bytes<N>>,
N: heapless::ArrayLength<u8>,
{
interface_number: InterfaceNumber,
string_index: StringIndex,
read: EndpointOut<'static, Bus>,
// interrupt: EndpointIn<'static, Bus>,
pipe: Pipe<Bus>,
pipe: Pipe<Bus, I, N>,
}
impl<Bus> Ccid<Bus>
impl<Bus, I, N> Ccid<Bus, I, N>
where
Bus: 'static + UsbBus,
I: 'static + Interchange<REQUEST = Bytes<N>, RESPONSE = Bytes<N>>,
N: heapless::ArrayLength<u8>,
{
pub fn new(
allocator: &'static UsbBusAllocator<Bus>,
request_pipe: Requester<interchanges::Contact>,
request_pipe: Requester<I>,
) -> Self {
let read = allocator.bulk(PACKET_SIZE as _);
let write = allocator.bulk(PACKET_SIZE as _);
@@ -75,9 +79,11 @@ where
}
}
impl<Bus> UsbClass<Bus> for Ccid<Bus>
impl<Bus, I, N> UsbClass<Bus> for Ccid<Bus, I, N>
where
Bus: 'static + UsbBus,
I: 'static + Interchange<REQUEST = Bytes<N>, RESPONSE = Bytes<N>>,
N: heapless::ArrayLength<u8>,
{
fn get_configuration_descriptors(&self, writer: &mut DescriptorWriter)
-> Result<()>
+26 -23
View File
@@ -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<Bus>
pub struct Pipe<Bus, I, N>
where
Bus: UsbBus + 'static,
Bus: 'static + UsbBus,
I: 'static + Interchange<REQUEST = Bytes<N>, RESPONSE = Bytes<N>>,
N: heapless::ArrayLength<u8>,
{
pub(crate) write: EndpointIn<'static, Bus>,
// pub(crate) rpc: TransportEndpoint<'rpc>,
seq: u8,
state: State,
interchange: Requester<interchanges::Contact>,
interchange: Requester<I>,
sent: usize,
outbox: Option<RawPacket>,
@@ -65,13 +64,15 @@ where
pub(crate) started_processing: bool,
}
impl<Bus> Pipe<Bus>
impl<Bus, I, N> Pipe<Bus, I, N>
where
Bus: 'static + UsbBus,
I: 'static + Interchange<REQUEST = Bytes<N>, RESPONSE = Bytes<N>>,
N: heapless::ArrayLength<u8>,
{
pub(crate) fn new(
write: EndpointIn<'static, Bus>,
request_pipe: Requester<interchanges::Contact>,
request_pipe: Requester<I>,
) -> Self {
assert!(MAX_MSG_LENGTH >= PACKET_SIZE);
@@ -101,9 +102,11 @@ where
}
impl<Bus> Pipe<Bus>
impl<Bus, I, N> Pipe<Bus, I, N>
where
Bus: 'static + UsbBus
Bus: 'static + UsbBus,
I: 'static + Interchange<REQUEST = Bytes<N>, RESPONSE = Bytes<N>>,
N: heapless::ArrayLength<u8>,
{
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<N> = 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];
+1 -1
View File
@@ -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 {