From 07a26080e8fb027a4cb8e7c681f18bfc03434874 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Thu, 13 Oct 2022 13:44:31 +0200 Subject: [PATCH] fix(core): fix usb disconnected warning in new homescreen [no changelog] --- core/embed/rust/build.rs | 4 ++- core/embed/rust/src/trezorhal/mod.rs | 1 + core/embed/rust/src/trezorhal/usb.rs | 5 ++++ .../src/ui/model_tt/component/homescreen.rs | 16 +++++----- core/embed/rust/src/ui/model_tt/layout.rs | 6 ++-- core/embed/rust/trezorhal.h | 1 + .../src/trezor/ui/layouts/tt_v2/homescreen.py | 30 ++++--------------- 7 files changed, 29 insertions(+), 34 deletions(-) create mode 100644 core/embed/rust/src/trezorhal/usb.rs diff --git a/core/embed/rust/build.rs b/core/embed/rust/build.rs index d5a0104a9b..eb3ac05624 100644 --- a/core/embed/rust/build.rs +++ b/core/embed/rust/build.rs @@ -310,7 +310,9 @@ fn generate_trezorhal_bindings() { .allowlist_function("buffers_get_line_buffer_4bpp") .allowlist_function("buffers_get_text_buffer") .allowlist_var("text_buffer_height") - .allowlist_var("buffer_width"); + .allowlist_var("buffer_width") + //usb + .allowlist_function("usb_configured"); // Write the bindings to a file in the OUR_DIR. bindings .generate() diff --git a/core/embed/rust/src/trezorhal/mod.rs b/core/embed/rust/src/trezorhal/mod.rs index 496a0f2917..9696c53dcd 100644 --- a/core/embed/rust/src/trezorhal/mod.rs +++ b/core/embed/rust/src/trezorhal/mod.rs @@ -13,6 +13,7 @@ pub mod random; pub mod rgb_led; pub mod slip39; pub mod storage; +pub mod usb; pub mod uzlib; pub mod buffers; diff --git a/core/embed/rust/src/trezorhal/usb.rs b/core/embed/rust/src/trezorhal/usb.rs new file mode 100644 index 0000000000..241e0fc3e0 --- /dev/null +++ b/core/embed/rust/src/trezorhal/usb.rs @@ -0,0 +1,5 @@ +use super::ffi; + +pub fn usb_configured() -> bool { + unsafe { ffi::usb_configured() == ffi::sectrue } +} diff --git a/core/embed/rust/src/ui/model_tt/component/homescreen.rs b/core/embed/rust/src/ui/model_tt/component/homescreen.rs index fa10dd7013..21c03368ff 100644 --- a/core/embed/rust/src/ui/model_tt/component/homescreen.rs +++ b/core/embed/rust/src/ui/model_tt/component/homescreen.rs @@ -1,5 +1,6 @@ use crate::{ time::{Duration, Instant}, + trezorhal::usb::usb_configured, ui::{ component::{Component, Empty, Event, EventCtx, Pad, TimerToken}, display::{self, Color, Font}, @@ -45,7 +46,6 @@ where label, notification, hold_to_lock, - usb_connected: true, loader: Loader::new().with_durations(LOADER_DURATION, LOADER_DURATION / 3), pad: Pad::with_background(theme::BG), delay: None, @@ -61,7 +61,7 @@ where } fn paint_notification(&self) { - if !self.usb_connected { + if !usb_configured() { let (color, icon) = Self::level_to_style(0); NotificationFrame::::paint_notification( AREA, @@ -91,12 +91,13 @@ where self.loader.paint() } + pub fn set_paint_notification(&mut self) { + self.paint_notification_only = true; + } + fn event_usb(&mut self, ctx: &mut EventCtx, event: Event) { - if let Event::USB(USBEvent::Connected(is_connected)) = event { - if self.usb_connected != is_connected { - self.usb_connected = is_connected; - ctx.request_paint(); - } + if let Event::USB(USBEvent::Connected(_)) = event { + ctx.request_paint(); } } @@ -122,6 +123,7 @@ where Event::Timer(token) if Some(token) == self.delay => { self.delay = None; self.pad.clear(); + self.paint_notification_only = false; self.loader.start_growing(ctx, Instant::now()); } _ => {} diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index 1154def802..6ce6309f1d 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -1014,10 +1014,12 @@ extern "C" fn new_show_homescreen(n_args: usize, args: *const Obj, kwargs: *mut let skip_first_paint: bool = kwargs.get(Qstr::MP_QSTR_skip_first_paint)?.try_into()?; let notification = notification.map(|w| (w, notification_level)); - let obj = LayoutObj::new(Homescreen::new(label, notification, hold))?; + let mut hs = Homescreen::new(label, notification, hold); if skip_first_paint { - obj.skip_first_paint(); + hs.set_paint_notification(); } + let obj = LayoutObj::new(hs)?; + Ok(obj.into()) }; unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } diff --git a/core/embed/rust/trezorhal.h b/core/embed/rust/trezorhal.h index 0f474ae0a6..e077fd0400 100644 --- a/core/embed/rust/trezorhal.h +++ b/core/embed/rust/trezorhal.h @@ -8,6 +8,7 @@ #include "rgb_led.h" #include "secbool.h" #include "storage.h" +#include "usb.h" #include "bip39.h" #include "rand.h" diff --git a/core/src/trezor/ui/layouts/tt_v2/homescreen.py b/core/src/trezor/ui/layouts/tt_v2/homescreen.py index 66ad6bf9a0..767255bc57 100644 --- a/core/src/trezor/ui/layouts/tt_v2/homescreen.py +++ b/core/src/trezor/ui/layouts/tt_v2/homescreen.py @@ -2,7 +2,7 @@ from typing import Any, Tuple import storage.cache import storage.device -from trezor import io, loop, ui, utils +from trezor import io, loop, ui import trezorui2 from apps.base import set_homescreen @@ -15,31 +15,16 @@ class HomescreenBase(_RustLayout): def __init__(self, layout: Any) -> None: super().__init__(layout=layout) - self.is_connected = True - async def __iter__(self) -> Any: - # We need to catch the ui.Cancelled exception that kills us, because that means - # that we will need to draw on screen again after restart. - try: - return await super().__iter__() - except ui.Cancelled: - storage.cache.homescreen_shown = None - raise + def _paint(self) -> None: + self.layout.paint() def _first_paint(self) -> None: if storage.cache.homescreen_shown is not self.RENDER_INDICATOR: super()._first_paint() storage.cache.homescreen_shown = self.RENDER_INDICATOR - - # - RENDER_INDICATOR is set -> USB warning is not displayed - # - RENDER_INDICATOR is not set -> initially homescreen does not display warning - # - usb_checker_task only handles state changes - # Here we need to handle the case when homescreen is started with USB disconnected. - if not utils.usb_data_connected(): - msg = self.layout.usb_event(False) + else: self._paint() - if msg is not None: - raise ui.Result(msg) class Homescreen(HomescreenBase): @@ -75,11 +60,8 @@ class Homescreen(HomescreenBase): usbcheck = loop.wait(io.USB_CHECK) while True: is_connected = await usbcheck - if is_connected != self.is_connected: - self.is_connected = is_connected - self.layout.usb_event(is_connected) - self.layout.paint() - storage.cache.homescreen_shown = None + self.layout.usb_event(is_connected) + self.layout.paint() def create_tasks(self) -> Tuple[loop.AwaitableTask, ...]: return super().create_tasks() + (self.usb_checker_task(),)