Skip to content

Commit

Permalink
Merge remote-tracking branch 'ata/cancel-insert-sd'
Browse files Browse the repository at this point in the history
  • Loading branch information
asi345 committed Jun 12, 2024
2 parents 03a7434 + 62d8093 commit 1d0a095
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ customers cannot upgrade their bootloader, its changes are recorded separately.
- Bitcoin: allow multisig accounts at arbitrary keypaths
- Bitcoin: allow spendung UTXOs at very high BIP-44 address indices
- Ethereum: allow signing EIP-712 messages containing multi-line strings
- Allow exiting the screen asking to insert the microSD card

### 9.18.0
- Add support for deriving BIP-39 mnemonics according to BIP-85
Expand Down
5 changes: 4 additions & 1 deletion py/send_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ def _check_sd_presence(self) -> None:
print(f"SD Card inserted: {self._device.check_sdcard()}")

def _insert_sdcard(self) -> None:
self._device.insert_sdcard()
try:
self._device.insert_sdcard()
except UserAbortException:
print("Aborted by user")

def _remove_sdcard(self) -> None:
self._device.remove_sdcard()
Expand Down
6 changes: 6 additions & 0 deletions src/rust/bitbox02-rust/src/hww/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ impl core::convert::From<crate::workflow::transaction::UserAbort> for Error {
}
}

impl core::convert::From<crate::workflow::sdcard::UserAbort> for Error {
fn from(_error: crate::workflow::sdcard::UserAbort) -> Self {
Error::UserAbort
}
}

impl core::convert::From<crate::workflow::verify_message::Error> for Error {
fn from(error: crate::workflow::verify_message::Error) -> Self {
match error {
Expand Down
2 changes: 1 addition & 1 deletion src/rust/bitbox02-rust/src/hww/api/sdcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub async fn process(
{
return Ok(Response::Success(pb::Success {}));
}
sdcard::sdcard(action == SdCardAction::InsertCard).await;
sdcard::sdcard(action == SdCardAction::InsertCard).await?;
Ok(Response::Success(pb::Success {}))
}

Expand Down
14 changes: 10 additions & 4 deletions src/rust/bitbox02-rust/src/workflow/sdcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
use crate::bb02_async::option_no_screensaver;
use core::cell::RefCell;

pub async fn sdcard(insert: bool) {
let result = RefCell::new(None);
let mut component = bitbox02::ui::sdcard_create(insert, || {
*result.borrow_mut() = Some(());
pub struct UserAbort;

pub async fn sdcard(insert: bool) -> Result<(), UserAbort> {
let result = RefCell::new(None as Option<Result<(), UserAbort>>);
let mut component = bitbox02::ui::sdcard_create(insert, |sd_done| {
*result.borrow_mut() = if sd_done {
Some(Ok(()))
} else {
Some(Err(UserAbort))
};
});
component.screen_stack_push();
option_no_screensaver(&result).await
Expand Down
14 changes: 7 additions & 7 deletions src/rust/bitbox02/src/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,27 +197,27 @@ where
}
}

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

let component = unsafe {
bitbox02_sys::sdcard_create(
insert,
Some(c_continue_callback::<F>),
Some(c_callback::<F>),
// passed to the C callback as `param`
Box::into_raw(Box::new(continue_callback)) as *mut _,
Box::into_raw(Box::new(callback)) as *mut _,
)
};
Component {
Expand Down
6 changes: 3 additions & 3 deletions src/rust/bitbox02/src/ui/ui_stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ where
}
}

pub fn sdcard_create<'a, F>(insert: bool, mut continue_callback: F) -> Component<'a>
pub fn sdcard_create<'a, F>(insert: bool, mut callback: F) -> Component<'a>
where
F: FnMut() + 'a,
F: FnMut(bool) + 'a,
{
let data = crate::testing::DATA.0.borrow();
assert_eq!(data.ui_sdcard_create_arg.unwrap(), insert);
continue_callback();
callback(true);
Component {
is_pushed: false,
_p: PhantomData,
Expand Down
6 changes: 3 additions & 3 deletions src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ where
}
}

pub fn sdcard_create<'a, F>(_insert: bool, mut continue_callback: F) -> Component<'a>
pub fn sdcard_create<'a, F>(_insert: bool, mut callback: F) -> Component<'a>
where
F: FnMut() + 'a,
F: FnMut(bool) + 'a,
{
continue_callback();
callback(true);
Component {
is_pushed: false,
_p: PhantomData,
Expand Down
33 changes: 21 additions & 12 deletions src/ui/components/sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ typedef struct {
// if true, the callback won't be called until the sd card is inserted.
// the insert/remove label changes depending on this flag.
bool insert;
void (*continue_callback)(void*);
void* continue_callback_param;
void (*callback)(bool, void*);
void* callback_param;
} data_t;

static void _render(component_t* component)
Expand All @@ -51,17 +51,23 @@ static void _continue_callback(component_t* component)
{
data_t* data = (data_t*)component->parent->data;
if (!data->insert || sd_card_inserted()) {
if (data->continue_callback) {
data->continue_callback(data->continue_callback_param);
data->continue_callback = NULL;
if (data->callback) {
data->callback(true, data->callback_param);
data->callback = NULL;
}
}
}

component_t* sdcard_create(
bool insert,
void (*continue_callback)(void*),
void* continue_callback_param)
static void _cancel_callback(component_t* component)
{
data_t* data = (data_t*)component->parent->data;
if (data->callback) {
data->callback(false, data->callback_param);
data->callback = NULL;
}
}

component_t* sdcard_create(bool insert, void (*callback)(bool, void*), void* callback_param)
{
component_t* component = malloc(sizeof(component_t));
if (!component) {
Expand All @@ -75,8 +81,8 @@ component_t* sdcard_create(
memset(component, 0, sizeof(component_t));

data->insert = insert;
data->continue_callback = continue_callback;
data->continue_callback_param = continue_callback_param;
data->callback = callback;
data->callback_param = callback_param;
component->data = data;
component->f = &_component_functions;
component->dimension.width = SCREEN_WIDTH;
Expand All @@ -91,6 +97,9 @@ component_t* sdcard_create(
component));
ui_util_add_sub_component(
component, icon_button_create(top_slider, ICON_BUTTON_CHECK, _continue_callback));

if (insert) {
ui_util_add_sub_component(
component, icon_button_create(top_slider, ICON_BUTTON_CROSS, _cancel_callback));
}
return component;
}
5 changes: 1 addition & 4 deletions src/ui/components/sdcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
* @param[in] insert if true, the user is asked to insert the sdcard. Otherwise the user is asked to
* remove it.
*/
component_t* sdcard_create(
bool insert,
void (*continue_callback)(void*),
void* continue_callback_param);
component_t* sdcard_create(bool insert, void (*callback)(bool, void*), void* callback_param);

#endif

0 comments on commit 1d0a095

Please sign in to comment.