Introduce Customization struct (#458)

* Introduce Customization trait

* Introduce Customization trait including the customization accessors
  that control various behaviors.

* Expose Customization through a getter API in Env, and make the code
  that directly access the constants currently switch to accessing the
  customizations via Env.

* TockEnv's customization getter implementation directly returns the
  reference of the global DEFAULT_CUSTOMIZATION constant, so the
  constant values are still inlined and dead code won't be compiled.

* We'll add the customizations from global constants to the struct
  one-by-one, only MAX_MSG_SIZE in this commit.

* Small fixes

* Fix deploy script
* put is_valid under std gate
This commit is contained in:
hcyang
2022-04-14 14:57:18 +08:00
committed by GitHub
parent 81996f650e
commit 1ef9a4447d
12 changed files with 174 additions and 33 deletions

4
src/env/mod.rs vendored
View File

@@ -1,3 +1,4 @@
use crate::api::customization::Customization;
use crate::api::firmware_protection::FirmwareProtection;
use crate::api::upgrade_storage::UpgradeStorage;
use crate::ctap::status_code::Ctap2StatusCode;
@@ -24,6 +25,7 @@ pub trait Env {
type UpgradeStorage: UpgradeStorage;
type FirmwareProtection: FirmwareProtection;
type Write: core::fmt::Write;
type Customization: Customization;
fn rng(&mut self) -> &mut Self::Rng;
fn user_presence(&mut self) -> &mut Self::UserPresence;
@@ -44,4 +46,6 @@ pub trait Env {
/// using the defmt crate) and ignore this API. Non-embedded environments may either use this
/// API or use the log feature (to be implemented using the log crate).
fn write(&mut self) -> Self::Write;
fn customization(&self) -> &Self::Customization;
}

13
src/env/test/mod.rs vendored
View File

@@ -1,4 +1,5 @@
use self::upgrade_storage::BufferUpgradeStorage;
use crate::api::customization::{CustomizationImpl, DEFAULT_CUSTOMIZATION};
use crate::api::firmware_protection::FirmwareProtection;
use crate::ctap::status_code::Ctap2StatusCode;
use crate::ctap::Channel;
@@ -13,6 +14,7 @@ pub struct TestEnv {
user_presence: TestUserPresence,
store: Store<BufferStorage>,
upgrade_storage: Option<BufferUpgradeStorage>,
customization: CustomizationImpl,
}
pub struct TestUserPresence {
@@ -51,17 +53,23 @@ impl TestEnv {
let storage = new_storage();
let store = Store::new(storage).ok().unwrap();
let upgrade_storage = Some(BufferUpgradeStorage::new().unwrap());
let customization = DEFAULT_CUSTOMIZATION.clone();
TestEnv {
rng,
user_presence,
store,
upgrade_storage,
customization,
}
}
pub fn disable_upgrade_storage(&mut self) {
self.upgrade_storage = None;
}
pub fn customization_mut(&mut self) -> &mut CustomizationImpl {
&mut self.customization
}
}
impl TestUserPresence {
@@ -89,6 +97,7 @@ impl Env for TestEnv {
type UpgradeStorage = BufferUpgradeStorage;
type FirmwareProtection = Self;
type Write = TestWrite;
type Customization = CustomizationImpl;
fn rng(&mut self) -> &mut Self::Rng {
&mut self.rng
@@ -113,4 +122,8 @@ impl Env for TestEnv {
fn write(&mut self) -> Self::Write {
TestWrite
}
fn customization(&self) -> &Self::Customization {
&self.customization
}
}

6
src/env/tock/mod.rs vendored
View File

@@ -1,4 +1,5 @@
pub use self::storage::{TockStorage, TockUpgradeStorage};
use crate::api::customization::{CustomizationImpl, DEFAULT_CUSTOMIZATION};
use crate::api::firmware_protection::FirmwareProtection;
use crate::ctap::hid::{CtapHid, CtapHidCommand, KeepaliveStatus, ProcessedPacket};
use crate::ctap::status_code::Ctap2StatusCode;
@@ -80,6 +81,7 @@ impl Env for TockEnv {
type UpgradeStorage = TockUpgradeStorage;
type FirmwareProtection = Self;
type Write = Console;
type Customization = CustomizationImpl;
fn rng(&mut self) -> &mut Self::Rng {
&mut self.rng
@@ -104,6 +106,10 @@ impl Env for TockEnv {
fn write(&mut self) -> Self::Write {
Console::new()
}
fn customization(&self) -> &Self::Customization {
&DEFAULT_CUSTOMIZATION
}
}
// Returns whether the keepalive was sent, or false if cancelled.