|
| 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