mirror of
https://github.com/trussed-dev/ctaphid-dispatch.git
synced 2026-06-20 04:16:18 -07:00
Cargo clippy+fmt for a first release
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
target/
|
||||
Cargo.lock
|
||||
@@ -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
|
||||
+6
-4
@@ -1,16 +1,18 @@
|
||||
[package]
|
||||
name = "ctaphid-dispatch"
|
||||
version = "0.0.1"
|
||||
version = "0.1.0"
|
||||
authors = ["Conor Patrick <conor@solokeys.com>", "Nicolas Stalder <n@stalder.io>"]
|
||||
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 = []
|
||||
|
||||
+1
-3
@@ -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];
|
||||
|
||||
|
||||
+17
-20
@@ -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<u8> 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<u8> for VendorCommand {
|
||||
type Error = ();
|
||||
|
||||
@@ -139,21 +136,21 @@ impl TryFrom<u8> for VendorCommand {
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<u8> 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<Command> 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-28
@@ -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<HidInterchange>,
|
||||
}
|
||||
|
||||
|
||||
impl Dispatch {
|
||||
pub fn new(
|
||||
responder: Responder<HidInterchange>,
|
||||
) -> Dispatch {
|
||||
Dispatch {
|
||||
responder,
|
||||
}
|
||||
pub fn new(responder: Responder<HidInterchange>) -> 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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -14,6 +14,6 @@ extern crate delog;
|
||||
generate_macros!();
|
||||
|
||||
pub mod app;
|
||||
pub mod types;
|
||||
pub mod command;
|
||||
pub mod dispatch;
|
||||
pub mod types;
|
||||
|
||||
+4
-3
@@ -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 = <U7168 as core::ops::Add<heapless::consts::U441>>::Output;
|
||||
// pub type U7609 = heapless::consts::U4096;
|
||||
|
||||
// TODO: find reasonable size
|
||||
// pub type Message = heapless::Vec<u8, 3072>;
|
||||
pub type Message = heapless::Vec<u8, 7609>;
|
||||
pub type AppResult = core::result::Result<(), Error>;
|
||||
pub type ShortMessage = heapless::Vec<u8, 1024>;
|
||||
pub type InterchangeResponse = core::result::Result<Message, Error>;
|
||||
|
||||
pub use crate::command::Command;
|
||||
@@ -21,4 +23,3 @@ pub use crate::command::Command;
|
||||
interchange::interchange! {
|
||||
HidInterchange: ((Command, crate::types::Message), crate::types::InterchangeResponse)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user