1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-31 10:58:43 +00:00

fix(core): fix usb disconnected warning in new homescreen

[no changelog]
This commit is contained in:
tychovrahe 2022-10-13 13:44:31 +02:00
parent b5d0c0eec9
commit 07a26080e8
7 changed files with 29 additions and 34 deletions

View File

@ -310,7 +310,9 @@ fn generate_trezorhal_bindings() {
.allowlist_function("buffers_get_line_buffer_4bpp") .allowlist_function("buffers_get_line_buffer_4bpp")
.allowlist_function("buffers_get_text_buffer") .allowlist_function("buffers_get_text_buffer")
.allowlist_var("text_buffer_height") .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. // Write the bindings to a file in the OUR_DIR.
bindings bindings
.generate() .generate()

View File

@ -13,6 +13,7 @@ pub mod random;
pub mod rgb_led; pub mod rgb_led;
pub mod slip39; pub mod slip39;
pub mod storage; pub mod storage;
pub mod usb;
pub mod uzlib; pub mod uzlib;
pub mod buffers; pub mod buffers;

View File

@ -0,0 +1,5 @@
use super::ffi;
pub fn usb_configured() -> bool {
unsafe { ffi::usb_configured() == ffi::sectrue }
}

View File

@ -1,5 +1,6 @@
use crate::{ use crate::{
time::{Duration, Instant}, time::{Duration, Instant},
trezorhal::usb::usb_configured,
ui::{ ui::{
component::{Component, Empty, Event, EventCtx, Pad, TimerToken}, component::{Component, Empty, Event, EventCtx, Pad, TimerToken},
display::{self, Color, Font}, display::{self, Color, Font},
@ -45,7 +46,6 @@ where
label, label,
notification, notification,
hold_to_lock, hold_to_lock,
usb_connected: true,
loader: Loader::new().with_durations(LOADER_DURATION, LOADER_DURATION / 3), loader: Loader::new().with_durations(LOADER_DURATION, LOADER_DURATION / 3),
pad: Pad::with_background(theme::BG), pad: Pad::with_background(theme::BG),
delay: None, delay: None,
@ -61,7 +61,7 @@ where
} }
fn paint_notification(&self) { fn paint_notification(&self) {
if !self.usb_connected { if !usb_configured() {
let (color, icon) = Self::level_to_style(0); let (color, icon) = Self::level_to_style(0);
NotificationFrame::<Empty, T>::paint_notification( NotificationFrame::<Empty, T>::paint_notification(
AREA, AREA,
@ -91,12 +91,13 @@ where
self.loader.paint() self.loader.paint()
} }
fn event_usb(&mut self, ctx: &mut EventCtx, event: Event) { pub fn set_paint_notification(&mut self) {
if let Event::USB(USBEvent::Connected(is_connected)) = event { self.paint_notification_only = true;
if self.usb_connected != is_connected {
self.usb_connected = is_connected;
ctx.request_paint();
} }
fn event_usb(&mut self, ctx: &mut EventCtx, event: Event) {
if let Event::USB(USBEvent::Connected(_)) = event {
ctx.request_paint();
} }
} }
@ -122,6 +123,7 @@ where
Event::Timer(token) if Some(token) == self.delay => { Event::Timer(token) if Some(token) == self.delay => {
self.delay = None; self.delay = None;
self.pad.clear(); self.pad.clear();
self.paint_notification_only = false;
self.loader.start_growing(ctx, Instant::now()); self.loader.start_growing(ctx, Instant::now());
} }
_ => {} _ => {}

View File

@ -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 skip_first_paint: bool = kwargs.get(Qstr::MP_QSTR_skip_first_paint)?.try_into()?;
let notification = notification.map(|w| (w, notification_level)); 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 { if skip_first_paint {
obj.skip_first_paint(); hs.set_paint_notification();
} }
let obj = LayoutObj::new(hs)?;
Ok(obj.into()) Ok(obj.into())
}; };
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }

View File

@ -8,6 +8,7 @@
#include "rgb_led.h" #include "rgb_led.h"
#include "secbool.h" #include "secbool.h"
#include "storage.h" #include "storage.h"
#include "usb.h"
#include "bip39.h" #include "bip39.h"
#include "rand.h" #include "rand.h"

View File

@ -2,7 +2,7 @@ from typing import Any, Tuple
import storage.cache import storage.cache
import storage.device import storage.device
from trezor import io, loop, ui, utils from trezor import io, loop, ui
import trezorui2 import trezorui2
from apps.base import set_homescreen from apps.base import set_homescreen
@ -15,31 +15,16 @@ class HomescreenBase(_RustLayout):
def __init__(self, layout: Any) -> None: def __init__(self, layout: Any) -> None:
super().__init__(layout=layout) super().__init__(layout=layout)
self.is_connected = True
async def __iter__(self) -> Any: def _paint(self) -> None:
# We need to catch the ui.Cancelled exception that kills us, because that means self.layout.paint()
# 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 _first_paint(self) -> None: def _first_paint(self) -> None:
if storage.cache.homescreen_shown is not self.RENDER_INDICATOR: if storage.cache.homescreen_shown is not self.RENDER_INDICATOR:
super()._first_paint() super()._first_paint()
storage.cache.homescreen_shown = self.RENDER_INDICATOR storage.cache.homescreen_shown = self.RENDER_INDICATOR
else:
# - 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)
self._paint() self._paint()
if msg is not None:
raise ui.Result(msg)
class Homescreen(HomescreenBase): class Homescreen(HomescreenBase):
@ -75,11 +60,8 @@ class Homescreen(HomescreenBase):
usbcheck = loop.wait(io.USB_CHECK) usbcheck = loop.wait(io.USB_CHECK)
while True: while True:
is_connected = await usbcheck is_connected = await usbcheck
if is_connected != self.is_connected:
self.is_connected = is_connected
self.layout.usb_event(is_connected) self.layout.usb_event(is_connected)
self.layout.paint() self.layout.paint()
storage.cache.homescreen_shown = None
def create_tasks(self) -> Tuple[loop.AwaitableTask, ...]: def create_tasks(self) -> Tuple[loop.AwaitableTask, ...]:
return super().create_tasks() + (self.usb_checker_task(),) return super().create_tasks() + (self.usb_checker_task(),)