From db141fbc146577037269664d72cbe66d31f70799 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Sat, 24 Feb 2018 21:02:14 +0100 Subject: [PATCH] src/trezor: rework PIN progress update --- embed/extmod/modtrezorconfig/storage.c | 18 +++++++++++++++--- src/trezor/pin.py | 12 ++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/embed/extmod/modtrezorconfig/storage.c b/embed/extmod/modtrezorconfig/storage.c index 452974d5..4ee73f56 100644 --- a/embed/extmod/modtrezorconfig/storage.c +++ b/embed/extmod/modtrezorconfig/storage.c @@ -136,11 +136,23 @@ static secbool pin_check(uint32_t pin, mp_obj_t callback) pin_fails_check_max(ctr); // Sleep for ~ctr seconds before checking the PIN. + uint32_t progress; for (uint32_t wait = ~ctr; wait > 0; wait--) { - if (mp_obj_is_callable(callback)) { - mp_call_function_2(callback, mp_obj_new_int(wait), mp_obj_new_int(~ctr)); + for (int i = 0; i < 10; i++) { + if (mp_obj_is_callable(callback)) { + if ((~ctr) > 1000000) { // precise enough + progress = (~ctr - wait) / ((~ctr) / 1000); + } else { + progress = ((~ctr - wait) * 10 + i) * 100 / (~ctr); + } + mp_call_function_2(callback, mp_obj_new_int(wait), mp_obj_new_int(progress)); + } + hal_delay(100); } - hal_delay(1000); + } + // Show last frame if we were waiting + if ((~ctr > 0) && mp_obj_is_callable(callback)) { + mp_call_function_2(callback, mp_obj_new_int(0), mp_obj_new_int(1000)); } // First, we increase PIN fail counter in storage, even before checking the diff --git a/src/trezor/pin.py b/src/trezor/pin.py index 29be6f96..728642ca 100644 --- a/src/trezor/pin.py +++ b/src/trezor/pin.py @@ -5,8 +5,12 @@ def pin_to_int(pin: str) -> int: return int('1' + pin) -def show_pin_timeout(wait: int, total: int): - ui.display.bar(0, 0, ui.WIDTH, ui.HEIGHT, ui.BG) - ui.display.loader(1000 - (1000 * wait // total), -10, ui.FG, ui.BG) - ui.display.text_center(ui.WIDTH // 2, ui.HEIGHT - 20, 'Waiting for %d s' % wait, ui.BOLD, ui.FG, ui.BG) +def show_pin_timeout(seconds: int, progress: int): + if progress == 0: + ui.display.bar(0, 0, ui.WIDTH, ui.HEIGHT, ui.BG) + ui.display.loader(progress, -10, ui.FG, ui.BG) + if seconds == 0: + ui.display.text_center(ui.WIDTH // 2, ui.HEIGHT - 20, 'Done', ui.BOLD, ui.FG, ui.BG, ui.WIDTH) + else: + ui.display.text_center(ui.WIDTH // 2, ui.HEIGHT - 20, 'Waiting for %d s' % seconds, ui.BOLD, ui.FG, ui.BG, ui.WIDTH) ui.display.refresh()