Create a tock environment

This commit is contained in:
Julien Cretin
2022-03-03 10:10:56 +01:00
committed by Julien Cretin
parent 6b8523ba93
commit 7d39d4e2e8
3 changed files with 46 additions and 31 deletions

1
src/env/mod.rs vendored
View File

@@ -4,6 +4,7 @@ use crypto::rng256::Rng256;
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub mod test; pub mod test;
pub mod tock;
pub trait UserPresence { pub trait UserPresence {
/// Blocks for user presence. /// Blocks for user presence.

43
src/env/tock.rs vendored Normal file
View File

@@ -0,0 +1,43 @@
use crypto::rng256::TockRng256;
use crate::ctap::hid::ChannelID;
use crate::ctap::status_code::Ctap2StatusCode;
use crate::env::{Env, UserPresence};
pub struct TockEnv<CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>> {
rng: TockRng256,
check_user_presence: CheckUserPresence,
}
impl<CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>> TockEnv<CheckUserPresence> {
pub fn new(check_user_presence: CheckUserPresence) -> Self {
let rng = TockRng256 {};
TockEnv {
rng,
check_user_presence,
}
}
}
impl<CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>> UserPresence
for TockEnv<CheckUserPresence>
{
fn check(&self, cid: ChannelID) -> Result<(), Ctap2StatusCode> {
(self.check_user_presence)(cid)
}
}
impl<CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>> Env
for TockEnv<CheckUserPresence>
{
type Rng = TockRng256;
type UserPresence = Self;
fn rng(&mut self) -> &mut Self::Rng {
&mut self.rng
}
fn user_presence(&mut self) -> &mut Self::UserPresence {
self
}
}

View File

@@ -24,10 +24,9 @@ extern crate lang_items;
use core::cell::Cell; use core::cell::Cell;
#[cfg(feature = "debug_ctap")] #[cfg(feature = "debug_ctap")]
use core::fmt::Write; use core::fmt::Write;
use crypto::rng256::TockRng256;
use ctap2::ctap::hid::{ChannelID, CtapHid, KeepaliveStatus, ProcessedPacket}; use ctap2::ctap::hid::{ChannelID, CtapHid, KeepaliveStatus, ProcessedPacket};
use ctap2::ctap::status_code::Ctap2StatusCode; use ctap2::ctap::status_code::Ctap2StatusCode;
use ctap2::env; use ctap2::env::tock::TockEnv;
use libtock_core::result::{CommandError, EALREADY}; use libtock_core::result::{CommandError, EALREADY};
use libtock_drivers::buttons; use libtock_drivers::buttons;
use libtock_drivers::buttons::ButtonState; use libtock_drivers::buttons::ButtonState;
@@ -49,31 +48,6 @@ const KEEPALIVE_DELAY_MS: isize = 100;
const KEEPALIVE_DELAY: Duration<isize> = Duration::from_ms(KEEPALIVE_DELAY_MS); const KEEPALIVE_DELAY: Duration<isize> = Duration::from_ms(KEEPALIVE_DELAY_MS);
const SEND_TIMEOUT: Duration<isize> = Duration::from_ms(1000); const SEND_TIMEOUT: Duration<isize> = Duration::from_ms(1000);
struct TockEnv {
rng: TockRng256,
user_presence: UserPresence,
}
struct UserPresence;
impl env::UserPresence for UserPresence {
fn check(&self, cid: ChannelID) -> Result<(), Ctap2StatusCode> {
check_user_presence(cid)
}
}
impl env::Env for TockEnv {
type Rng = TockRng256;
type UserPresence = UserPresence;
fn rng(&mut self) -> &mut Self::Rng {
&mut self.rng
}
fn user_presence(&mut self) -> &mut Self::UserPresence {
&mut self.user_presence
}
}
fn main() { fn main() {
// Setup the timer with a dummy callback (we only care about reading the current time, but the // Setup the timer with a dummy callback (we only care about reading the current time, but the
// API forces us to set an alarm callback too). // API forces us to set an alarm callback too).
@@ -86,10 +60,7 @@ fn main() {
} }
let boot_time = timer.get_current_clock().flex_unwrap(); let boot_time = timer.get_current_clock().flex_unwrap();
let env = TockEnv { let env = TockEnv::new(check_user_presence);
rng: TockRng256 {},
user_presence: UserPresence,
};
let mut ctap = ctap2::Ctap::new(env, boot_time); let mut ctap = ctap2::Ctap::new(env, boot_time);
let mut led_counter = 0; let mut led_counter = 0;