From 092c86083c1bd22c124ab2f50a96d5d4f0346ddc Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Tue, 8 Apr 2025 17:08:26 +0200 Subject: [PATCH] refactor(core): drain the BLE event queue whenever layout is running [no changelog] --- core/src/trezor/ui/__init__.py | 15 +++++++++++++++ core/src/trezor/ui/layouts/homescreen.py | 14 +------------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/core/src/trezor/ui/__init__.py b/core/src/trezor/ui/__init__.py index 9691402d61..3ae22b05d9 100644 --- a/core/src/trezor/ui/__init__.py +++ b/core/src/trezor/ui/__init__.py @@ -381,6 +381,9 @@ class Layout(Generic[T]): yield self._handle_input_iface(io.BUTTON, self.layout.button_event) if utils.USE_TOUCH: yield self._handle_input_iface(io.TOUCH, self.layout.touch_event) + if utils.USE_BLE: + # most layouts don't care but we don't want to keep stale events in the queue + yield self._handle_ble_events() def _handle_input_iface( self, iface: int, event_call: Callable[..., LayoutState | None] @@ -430,6 +433,18 @@ class Layout(Generic[T]): except Exception: raise + if utils.USE_BLE: + + async def _handle_ble_events(self) -> None: + blecheck = loop.wait(io.BLE_EVENT) + try: + while True: + event = await blecheck + log.debug(__name__, "BLE event: %s", event) + self._event(self.layout.ble_event, *event) + except Shutdown: + return + def _task_finalizer(self, task: loop.Task, value: Any) -> None: if value is None: # all is good diff --git a/core/src/trezor/ui/layouts/homescreen.py b/core/src/trezor/ui/layouts/homescreen.py index d0c1e21e80..ebad72eba0 100644 --- a/core/src/trezor/ui/layouts/homescreen.py +++ b/core/src/trezor/ui/layouts/homescreen.py @@ -3,7 +3,7 @@ from typing import TYPE_CHECKING import storage.cache as storage_cache import trezorui_api from storage.cache_common import APP_COMMON_BUSY_DEADLINE_MS -from trezor import TR, ui, utils +from trezor import TR, ui if TYPE_CHECKING: from typing import Any, Callable, Iterator, ParamSpec, TypeVar @@ -101,21 +101,9 @@ class Homescreen(HomescreenBase): event = await usbcheck self._event(self.layout.usb_event, event) - if utils.USE_BLE: - - async def ble_checker_task(self) -> None: - from trezor import io, loop - - blecheck = loop.wait(io.BLE_EVENT) - while True: - event = await blecheck - self._event(self.layout.ble_event, *event) - def create_tasks(self) -> Iterator[loop.Task]: yield from super().create_tasks() yield self.usb_checker_task() - if utils.USE_BLE: - yield self.ble_checker_task() class Lockscreen(HomescreenBase):