Skip to content

Commit 1d0a095

Browse files
committed
Merge remote-tracking branch 'ata/cancel-insert-sd'
2 parents 03a7434 + 62d8093 commit 1d0a095

File tree

10 files changed

+57
-35
lines changed

10 files changed

+57
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ customers cannot upgrade their bootloader, its changes are recorded separately.
1212
- Bitcoin: allow multisig accounts at arbitrary keypaths
1313
- Bitcoin: allow spendung UTXOs at very high BIP-44 address indices
1414
- Ethereum: allow signing EIP-712 messages containing multi-line strings
15+
- Allow exiting the screen asking to insert the microSD card
1516

1617
### 9.18.0
1718
- Add support for deriving BIP-39 mnemonics according to BIP-85

py/send_message.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@ def _check_sd_presence(self) -> None:
312312
print(f"SD Card inserted: {self._device.check_sdcard()}")
313313

314314
def _insert_sdcard(self) -> None:
315-
self._device.insert_sdcard()
315+
try:
316+
self._device.insert_sdcard()
317+
except UserAbortException:
318+
print("Aborted by user")
316319

317320
def _remove_sdcard(self) -> None:
318321
self._device.remove_sdcard()

src/rust/bitbox02-rust/src/hww/api/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ impl core::convert::From<crate::workflow::transaction::UserAbort> for Error {
7575
}
7676
}
7777

78+
impl core::convert::From<crate::workflow::sdcard::UserAbort> for Error {
79+
fn from(_error: crate::workflow::sdcard::UserAbort) -> Self {
80+
Error::UserAbort
81+
}
82+
}
83+
7884
impl core::convert::From<crate::workflow::verify_message::Error> for Error {
7985
fn from(error: crate::workflow::verify_message::Error) -> Self {
8086
match error {

src/rust/bitbox02-rust/src/hww/api/sdcard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub async fn process(
3434
{
3535
return Ok(Response::Success(pb::Success {}));
3636
}
37-
sdcard::sdcard(action == SdCardAction::InsertCard).await;
37+
sdcard::sdcard(action == SdCardAction::InsertCard).await?;
3838
Ok(Response::Success(pb::Success {}))
3939
}
4040

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515
use crate::bb02_async::option_no_screensaver;
1616
use core::cell::RefCell;
1717

18-
pub async fn sdcard(insert: bool) {
19-
let result = RefCell::new(None);
20-
let mut component = bitbox02::ui::sdcard_create(insert, || {
21-
*result.borrow_mut() = Some(());
18+
pub struct UserAbort;
19+
20+
pub async fn sdcard(insert: bool) -> Result<(), UserAbort> {
21+
let result = RefCell::new(None as Option<Result<(), UserAbort>>);
22+
let mut component = bitbox02::ui::sdcard_create(insert, |sd_done| {
23+
*result.borrow_mut() = if sd_done {
24+
Some(Ok(()))
25+
} else {
26+
Some(Err(UserAbort))
27+
};
2228
});
2329
component.screen_stack_push();
2430
option_no_screensaver(&result).await

src/rust/bitbox02/src/ui/ui.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,27 +197,27 @@ where
197197
}
198198
}
199199

200-
pub fn sdcard_create<'a, F>(insert: bool, continue_callback: F) -> Component<'a>
200+
pub fn sdcard_create<'a, F>(insert: bool, callback: F) -> Component<'a>
201201
where
202202
// Callback must outlive component.
203-
F: FnMut() + 'a,
203+
F: FnMut(bool) + 'a,
204204
{
205-
unsafe extern "C" fn c_continue_callback<F2>(param: *mut c_void)
205+
unsafe extern "C" fn c_callback<F2>(sd_done: bool, param: *mut c_void)
206206
where
207-
F2: FnMut(),
207+
F2: FnMut(bool),
208208
{
209209
// The callback is dropped afterwards. This is safe because
210210
// this C callback is guaranteed to be called only once.
211211
let mut callback = Box::from_raw(param as *mut F2);
212-
callback();
212+
callback(sd_done);
213213
}
214214

215215
let component = unsafe {
216216
bitbox02_sys::sdcard_create(
217217
insert,
218-
Some(c_continue_callback::<F>),
218+
Some(c_callback::<F>),
219219
// passed to the C callback as `param`
220-
Box::into_raw(Box::new(continue_callback)) as *mut _,
220+
Box::into_raw(Box::new(callback)) as *mut _,
221221
)
222222
};
223223
Component {

src/rust/bitbox02/src/ui/ui_stub.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ where
9393
}
9494
}
9595

96-
pub fn sdcard_create<'a, F>(insert: bool, mut continue_callback: F) -> Component<'a>
96+
pub fn sdcard_create<'a, F>(insert: bool, mut callback: F) -> Component<'a>
9797
where
98-
F: FnMut() + 'a,
98+
F: FnMut(bool) + 'a,
9999
{
100100
let data = crate::testing::DATA.0.borrow();
101101
assert_eq!(data.ui_sdcard_create_arg.unwrap(), insert);
102-
continue_callback();
102+
callback(true);
103103
Component {
104104
is_pushed: false,
105105
_p: PhantomData,

src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ where
100100
}
101101
}
102102

103-
pub fn sdcard_create<'a, F>(_insert: bool, mut continue_callback: F) -> Component<'a>
103+
pub fn sdcard_create<'a, F>(_insert: bool, mut callback: F) -> Component<'a>
104104
where
105-
F: FnMut() + 'a,
105+
F: FnMut(bool) + 'a,
106106
{
107-
continue_callback();
107+
callback(true);
108108
Component {
109109
is_pushed: false,
110110
_p: PhantomData,

src/ui/components/sdcard.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ typedef struct {
2727
// if true, the callback won't be called until the sd card is inserted.
2828
// the insert/remove label changes depending on this flag.
2929
bool insert;
30-
void (*continue_callback)(void*);
31-
void* continue_callback_param;
30+
void (*callback)(bool, void*);
31+
void* callback_param;
3232
} data_t;
3333

3434
static void _render(component_t* component)
@@ -51,17 +51,23 @@ static void _continue_callback(component_t* component)
5151
{
5252
data_t* data = (data_t*)component->parent->data;
5353
if (!data->insert || sd_card_inserted()) {
54-
if (data->continue_callback) {
55-
data->continue_callback(data->continue_callback_param);
56-
data->continue_callback = NULL;
54+
if (data->callback) {
55+
data->callback(true, data->callback_param);
56+
data->callback = NULL;
5757
}
5858
}
5959
}
6060

61-
component_t* sdcard_create(
62-
bool insert,
63-
void (*continue_callback)(void*),
64-
void* continue_callback_param)
61+
static void _cancel_callback(component_t* component)
62+
{
63+
data_t* data = (data_t*)component->parent->data;
64+
if (data->callback) {
65+
data->callback(false, data->callback_param);
66+
data->callback = NULL;
67+
}
68+
}
69+
70+
component_t* sdcard_create(bool insert, void (*callback)(bool, void*), void* callback_param)
6571
{
6672
component_t* component = malloc(sizeof(component_t));
6773
if (!component) {
@@ -75,8 +81,8 @@ component_t* sdcard_create(
7581
memset(component, 0, sizeof(component_t));
7682

7783
data->insert = insert;
78-
data->continue_callback = continue_callback;
79-
data->continue_callback_param = continue_callback_param;
84+
data->callback = callback;
85+
data->callback_param = callback_param;
8086
component->data = data;
8187
component->f = &_component_functions;
8288
component->dimension.width = SCREEN_WIDTH;
@@ -91,6 +97,9 @@ component_t* sdcard_create(
9197
component));
9298
ui_util_add_sub_component(
9399
component, icon_button_create(top_slider, ICON_BUTTON_CHECK, _continue_callback));
94-
100+
if (insert) {
101+
ui_util_add_sub_component(
102+
component, icon_button_create(top_slider, ICON_BUTTON_CROSS, _cancel_callback));
103+
}
95104
return component;
96105
}

src/ui/components/sdcard.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
* @param[in] insert if true, the user is asked to insert the sdcard. Otherwise the user is asked to
2323
* remove it.
2424
*/
25-
component_t* sdcard_create(
26-
bool insert,
27-
void (*continue_callback)(void*),
28-
void* continue_callback_param);
25+
component_t* sdcard_create(bool insert, void (*callback)(bool, void*), void* callback_param);
2926

3027
#endif

0 commit comments

Comments
 (0)