From 70465eb084b6eaad35210c95d48e57202a1dc2ba Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Thu, 3 Oct 2024 12:25:22 +0200 Subject: [PATCH] TT ble handling --- core/embed/rust/src/ui/model_tt/layout.rs | 3 +++ core/mocks/generated/trezorui2.pyi | 2 ++ core/src/trezor/ui/layouts/tt/__init__.py | 14 ++++++++++++++ core/src/trezor/ui/layouts/tt/homescreen.py | 1 + 4 files changed, 20 insertions(+) diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index d9ba5cf77f..d9959312e0 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -1682,6 +1682,9 @@ pub static mp_module_trezorui2: Module = obj_module! { /// def usb_event(self, connected: bool) -> T | None: /// """Receive a USB connect/disconnect event.""" /// + /// def ble_event(self, event: int, data: bytes) -> T | None: + /// """Receive a BLE events.""" + /// /// def timer(self, token: int) -> T | None: /// """Callback for the timer set by `attach_timer_fn`. /// diff --git a/core/mocks/generated/trezorui2.pyi b/core/mocks/generated/trezorui2.pyi index 4721999aba..75920f8a6d 100644 --- a/core/mocks/generated/trezorui2.pyi +++ b/core/mocks/generated/trezorui2.pyi @@ -1148,6 +1148,8 @@ class LayoutObj(Generic[T]): """Receive a progress event.""" def usb_event(self, connected: bool) -> T | None: """Receive a USB connect/disconnect event.""" + def ble_event(self, event: int, data: bytes) -> T | None: + """Receive a BLE events.""" def timer(self, token: int) -> T | None: """Callback for the timer set by `attach_timer_fn`. This function should be called by the executor after the corresponding diff --git a/core/src/trezor/ui/layouts/tt/__init__.py b/core/src/trezor/ui/layouts/tt/__init__.py index 2e0094dd68..3005d3c9b5 100644 --- a/core/src/trezor/ui/layouts/tt/__init__.py +++ b/core/src/trezor/ui/layouts/tt/__init__.py @@ -77,6 +77,7 @@ class RustLayout(LayoutParentType[T]): self.handle_click_signal(), self.handle_result_signal(), self.handle_usb(context.get_context()), + self.handle_ble_events(), ) else: return ( @@ -85,6 +86,7 @@ class RustLayout(LayoutParentType[T]): self.handle_swipe(), self.handle_click_signal(), self.handle_result_signal(), + self.handle_ble_events(), ) async def handle_result_signal(self) -> None: @@ -189,11 +191,13 @@ class RustLayout(LayoutParentType[T]): self.handle_input_and_rendering(), self.handle_timers(), self.handle_usb(context.get_context()), + self.handle_ble_events(), ) else: return ( self.handle_input_and_rendering(), self.handle_timers(), + self.handle_ble_events(), ) def _first_paint(self) -> None: @@ -244,6 +248,16 @@ class RustLayout(LayoutParentType[T]): raise ui.Result(msg) self._paint() + def handle_ble_events(self) -> loop.Task: + input = loop.wait(io.BLE_CHECK) + while True: + # Using `yield` instead of `await` to avoid allocations. + (event, data) = yield input + msg = self.layout.ble_event(event, data) + if msg is not None: + raise ui.Result(msg) + self._paint() + def handle_timers(self) -> loop.Task: while True: # Using `yield` instead of `await` to avoid allocations. diff --git a/core/src/trezor/ui/layouts/tt/homescreen.py b/core/src/trezor/ui/layouts/tt/homescreen.py index 7089e8e5fb..85fe7e17ce 100644 --- a/core/src/trezor/ui/layouts/tt/homescreen.py +++ b/core/src/trezor/ui/layouts/tt/homescreen.py @@ -36,6 +36,7 @@ class HomescreenBase(RustLayout): self.handle_input_and_rendering(), self.handle_timers(), self.handle_click_signal(), # so we can receive debug events + self.handle_ble_events(), )