Skip to content

Commit a5754a6

Browse files
committed
use Hal trait to combine Ui, Sd and future traits
We want to inject deps into, for BB02, testing and in the future for other BitBox products. For this we create a Hal trait that can be used anywhere with `&mut impl Hal`, without needing to repeat the trait bounds in every function. The alternative was to have a trait that contains all functions, which did not seem like a good thing for modularity. Another alternative was to colour every function with `<U: Ui, S: Sd, ...>` trait bounds, which is ugly and does not scale to more traits easily. The `Workflows` trait is renamed to `Ui` (in the `hal` module only for now, later we will untangle the workflows module and rename things there too).
1 parent c73ebba commit a5754a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+998
-935
lines changed

src/rust/bitbox02-rust-c/src/workflow.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ static mut CONFIRM_TITLE: Option<String> = None;
4040
static mut CONFIRM_BODY: Option<String> = None;
4141
static mut CONFIRM_PARAMS: Option<confirm::Params> = None;
4242
static mut CONFIRM_STATE: TaskState<'static, Result<(), confirm::UserAbort>> = TaskState::Nothing;
43-
static mut REAL_WORKFLOWS: bitbox02_rust::workflow::RealWorkflows =
44-
bitbox02_rust::workflow::RealWorkflows;
43+
static mut BITBOX02_HAL: bitbox02_rust::hal::BitBox02Hal = bitbox02_rust::hal::BitBox02Hal::new();
4544

4645
#[no_mangle]
4746
pub unsafe extern "C" fn rust_workflow_spawn_unlock() {
4847
UNLOCK_STATE = TaskState::Running(Box::pin(bitbox02_rust::workflow::unlock::unlock(
49-
&mut REAL_WORKFLOWS,
48+
&mut BITBOX02_HAL,
5049
)));
5150
}
5251

src/rust/bitbox02-rust/src/hal.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2025 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use crate::workflow::RealWorkflows;
16+
pub use crate::workflow::Workflows as Ui;
17+
18+
pub trait Sd {
19+
fn sdcard_inserted(&mut self) -> bool;
20+
}
21+
22+
/// Hardware abstraction layer for BitBox devices.
23+
pub trait Hal {
24+
fn ui(&mut self) -> &mut impl Ui;
25+
fn sd(&mut self) -> &mut impl Sd;
26+
}
27+
28+
pub struct BitBox02Sd;
29+
30+
impl Sd for BitBox02Sd {
31+
fn sdcard_inserted(&mut self) -> bool {
32+
bitbox02::sd::sdcard_inserted()
33+
}
34+
}
35+
36+
pub struct BitBox02Hal {
37+
ui: RealWorkflows,
38+
sd: BitBox02Sd,
39+
}
40+
41+
impl BitBox02Hal {
42+
pub const fn new() -> Self {
43+
Self {
44+
ui: crate::workflow::RealWorkflows,
45+
sd: BitBox02Sd,
46+
}
47+
}
48+
}
49+
50+
impl Hal for BitBox02Hal {
51+
fn ui(&mut self) -> &mut impl Ui {
52+
&mut self.ui
53+
}
54+
fn sd(&mut self) -> &mut impl Sd {
55+
&mut self.sd
56+
}
57+
}
58+
59+
#[cfg(feature = "testing")]
60+
pub mod testing {
61+
pub struct TestingSd {
62+
pub inserted: Option<bool>,
63+
}
64+
65+
impl TestingSd {
66+
pub fn new() -> Self {
67+
Self { inserted: None }
68+
}
69+
}
70+
pub struct TestingHal<'a> {
71+
pub ui: crate::workflow::testing::TestingWorkflows<'a>,
72+
pub sd: TestingSd,
73+
}
74+
75+
impl super::Sd for TestingSd {
76+
fn sdcard_inserted(&mut self) -> bool {
77+
self.inserted.unwrap()
78+
}
79+
}
80+
81+
impl TestingHal<'_> {
82+
pub fn new() -> Self {
83+
Self {
84+
ui: crate::workflow::testing::TestingWorkflows::new(),
85+
sd: TestingSd::new(),
86+
}
87+
}
88+
}
89+
90+
impl super::Hal for TestingHal<'_> {
91+
fn ui(&mut self) -> &mut impl super::Ui {
92+
&mut self.ui
93+
}
94+
fn sd(&mut self) -> &mut impl super::Sd {
95+
&mut self.sd
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)