diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c96eb1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +Cargo.lock diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..4607b57 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,11 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [0.1.0] - 2022-03-05 + +- make a first proper release diff --git a/Cargo.toml b/Cargo.toml index a26a1af..b7a3db8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,16 +1,18 @@ [package] name = "ctaphid-dispatch" -version = "0.0.1" +version = "0.1.0" authors = ["Conor Patrick ", "Nicolas Stalder "] -edition = "2018" +edition = "2021" +license = "Apache-2.0 OR MIT" +description = "Dispatch layer after usbd-ctaphid" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -delog = "0.1.0" +delog = "0.1" heapless = "0.7" heapless-bytes = "0.3" -interchange = "0.2.0" +interchange = "0.2" [features] default = [] diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..e69de29 diff --git a/src/app.rs b/src/app.rs index dfa5f20..620c081 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,12 +1,10 @@ - -pub use crate::types::{AppResult, Error, Message}; pub use crate::command::Command; +pub use crate::types::{AppResult, Error, Message}; /// trait interface for a CTAPHID application. /// The application chooses which commands to register to, and will be called upon /// when the commands are received in the CTAPHID layer. Only one application can be registered to a particular command. pub trait App { - /// Define which CTAPHID commands to register to. fn commands(&self) -> &'static [Command]; diff --git a/src/command.rs b/src/command.rs index 95ad87a..1d37d0e 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,6 +1,4 @@ -use core::convert::TryFrom; - -#[derive(Copy,Clone,Debug,Eq,PartialEq)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum Command { // mandatory for CTAP1 Ping, @@ -52,7 +50,7 @@ impl TryFrom for Command { /// Vendor CTAPHID commands, from 0x40 to 0x7f. #[repr(u8)] -#[derive(Copy,Clone,Debug,Eq,PartialEq)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum VendorCommand { H40 = 0x40, H41 = 0x41, @@ -125,7 +123,6 @@ impl VendorCommand { pub const LAST: u8 = 0x7f; } - impl TryFrom for VendorCommand { type Error = (); @@ -139,21 +136,21 @@ impl TryFrom for VendorCommand { } } -impl Into for Command { - fn into(self) -> u8 { - match self { - Command::Ping => 0x01, - Command::Msg => 0x03, - Command::Init => 0x06, - Command::Error => 0x3f, - Command::Wink => 0x08, - Command::Lock => 0x04, - Command::Cbor => 0x10, - Command::Cancel => 0x11, - Command::Deselect => 0x12, - Command::KeepAlive => 0x3b, - Command::Vendor(command) => command as u8, +impl From for u8 { + fn from(command: Command) -> u8 { + use Command::*; + match command { + Ping => 0x01, + Msg => 0x03, + Init => 0x06, + Error => 0x3f, + Wink => 0x08, + Lock => 0x04, + Cbor => 0x10, + Cancel => 0x11, + Deselect => 0x12, + KeepAlive => 0x3b, + Vendor(command) => command as u8, } } } - diff --git a/src/dispatch.rs b/src/dispatch.rs index 061343d..a28304f 100644 --- a/src/dispatch.rs +++ b/src/dispatch.rs @@ -1,30 +1,22 @@ - -use interchange::{Interchange, Responder}; -use crate::types::{Command, Message, HidInterchange, Error}; use crate::app::App; +use crate::types::{Command, Error, HidInterchange, Message}; +use interchange::{Interchange, Responder}; pub struct Dispatch { responder: Responder, } - impl Dispatch { - pub fn new( - responder: Responder, - ) -> Dispatch { - Dispatch { - responder, - } + pub fn new(responder: Responder) -> Dispatch { + Dispatch { responder } } fn find_app<'a, 'b>( command: Command, - apps: &'a mut [&'b mut dyn App] + apps: &'a mut [&'b mut dyn App], ) -> Option<&'a mut &'b mut dyn App> { - - apps.iter_mut().find(|app| - app.commands().contains(&command) - ) + apps.iter_mut() + .find(|app| app.commands().contains(&command)) } // // Using helper here to take potentially large stack burden off of call chain to application. @@ -37,10 +29,8 @@ impl Dispatch { // Using helper here to take potentially large stack burden off of call chain to application. #[inline(never)] - fn reply_with_error(&mut self, error: Error){ - self.responder.respond( - &Err(error) - ).expect("cant respond"); + fn reply_with_error(&mut self, error: Error) { + self.responder.respond(&Err(error)).expect("cant respond"); } #[inline(never)] @@ -57,7 +47,7 @@ impl Dispatch { let response_buffer = &mut tuple.1; response_buffer.clear(); - if let Err(error) = app.call(command, &request, response_buffer) { + if let Err(error) = app.call(command, request, response_buffer) { self.reply_with_error(error) } else { let response = Ok(response_buffer.clone()); @@ -66,18 +56,15 @@ impl Dispatch { } #[inline(never)] - pub fn poll<'a>( - &mut self, - apps: &mut [&'a mut dyn App], - ) -> bool { + pub fn poll<'a>(&mut self, apps: &mut [&'a mut dyn App]) -> bool { let maybe_request = self.responder.take_request(); if let Some((command, message)) = maybe_request { - info_now!("cmd: {}", u8::from(command)); + // info_now!("cmd: {}", u8::from(command)); + // info_now!("cmd: {:?}", command); if let Some(app) = Self::find_app(command, apps) { // match app.call(command, self.responder.response_mut().unwrap()) { - let request = message.clone(); - self.call_app(*app, command, &request); + self.call_app(*app, command, &message); } else { self.reply_with_error(Error::InvalidCommand); } @@ -85,5 +72,4 @@ impl Dispatch { self.responder.state() == interchange::State::Responded } - } diff --git a/src/lib.rs b/src/lib.rs index d212bc8..3ae6ef5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,6 @@ extern crate delog; generate_macros!(); pub mod app; -pub mod types; pub mod command; pub mod dispatch; +pub mod types; diff --git a/src/types.rs b/src/types.rs index 5e96e1c..6e54bfa 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,5 +1,4 @@ - -#[derive(Copy,Clone,Debug,Eq,PartialEq)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum Error { NoResponse, InvalidCommand, @@ -12,8 +11,11 @@ pub enum Error { // pub type U7609 = >::Output; // pub type U7609 = heapless::consts::U4096; +// TODO: find reasonable size +// pub type Message = heapless::Vec; pub type Message = heapless::Vec; pub type AppResult = core::result::Result<(), Error>; +pub type ShortMessage = heapless::Vec; pub type InterchangeResponse = core::result::Result; pub use crate::command::Command; @@ -21,4 +23,3 @@ pub use crate::command::Command; interchange::interchange! { HidInterchange: ((Command, crate::types::Message), crate::types::InterchangeResponse) } -