1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-08 07:38:11 +00:00

refactor(core): separate BLE and THP pairing code screens

[no changelog]
This commit is contained in:
Ioan Bizău 2025-06-19 15:31:53 +02:00 committed by Ioan Bizău
parent 785f52f082
commit 320ee7b044
13 changed files with 123 additions and 56 deletions

View File

@ -706,6 +706,7 @@ static void _librust_qstrs(void) {
MP_QSTR_share_words__words_in_order;
MP_QSTR_share_words__wrote_down_all;
MP_QSTR_show_address_details;
MP_QSTR_show_ble_pairing_code;
MP_QSTR_show_checklist;
MP_QSTR_show_danger;
MP_QSTR_show_device_menu;
@ -717,7 +718,6 @@ static void _librust_qstrs(void) {
MP_QSTR_show_instructions;
MP_QSTR_show_lockscreen;
MP_QSTR_show_mismatch;
MP_QSTR_show_pairing_code;
MP_QSTR_show_pairing_device_name;
MP_QSTR_show_progress;
MP_QSTR_show_progress_coinjoin;
@ -726,6 +726,7 @@ static void _librust_qstrs(void) {
MP_QSTR_show_share_words_extended;
MP_QSTR_show_simple;
MP_QSTR_show_success;
MP_QSTR_show_thp_pairing_code;
MP_QSTR_show_wait_text;
MP_QSTR_show_warning;
MP_QSTR_sign;

View File

@ -874,12 +874,30 @@ extern "C" fn new_show_pairing_device_name(
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
}
extern "C" fn new_show_pairing_code(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
extern "C" fn new_show_ble_pairing_code(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
#[cfg(feature = "ble")]
{
let block = move |_args: &[Obj], kwargs: &Map| {
let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?;
let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?;
let code: TString = kwargs.get(Qstr::MP_QSTR_code)?.try_into()?;
let layout = ModelUI::show_ble_pairing_code(title, description, code)?;
let layout_obj = LayoutObj::new_root(layout)?;
Ok(layout_obj.into())
};
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
}
#[cfg(not(feature = "ble"))]
unimplemented!()
}
extern "C" fn new_show_thp_pairing_code(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
let block = move |_args: &[Obj], kwargs: &Map| {
let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?;
let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?;
let code: TString = kwargs.get(Qstr::MP_QSTR_code)?.try_into()?;
let layout = ModelUI::show_pairing_code(title, description, code)?;
let layout = ModelUI::show_thp_pairing_code(title, description, code)?;
let layout_obj = LayoutObj::new_root(layout)?;
Ok(layout_obj.into())
};
@ -1689,15 +1707,24 @@ pub static mp_module_trezorui_api: Module = obj_module! {
/// Returns if BLEEvent::PairingRequest is received."""
Qstr::MP_QSTR_show_pairing_device_name => obj_fn_kw!(0, new_show_pairing_device_name).as_obj(),
/// def show_pairing_code(
/// def show_ble_pairing_code(
/// *,
/// title: str,
/// description: str,
/// code: str,
/// ) -> LayoutObj[UiResult]:
/// """Pairing device: second screen (pairing code).
/// """BLE pairing: second screen (pairing code).
/// Returns on BLEEvent::{PairingCanceled, Disconnected}."""
Qstr::MP_QSTR_show_pairing_code => obj_fn_kw!(0, new_show_pairing_code).as_obj(),
Qstr::MP_QSTR_show_ble_pairing_code => obj_fn_kw!(0, new_show_ble_pairing_code).as_obj(),
/// def show_thp_pairing_code(
/// *,
/// title: str,
/// description: str,
/// code: str,
/// ) -> LayoutObj[UiResult]:
/// """THP pairing: second screen (pairing code)."""
Qstr::MP_QSTR_show_thp_pairing_code => obj_fn_kw!(0, new_show_thp_pairing_code).as_obj(),
/// def show_info(
/// *,

View File

@ -625,7 +625,6 @@ pub enum FlowMsg {
Next,
Choice(usize),
Text(ShortString),
Number(u32),
}
#[cfg(feature = "micropython")]
@ -641,7 +640,6 @@ impl TryFrom<FlowMsg> for crate::micropython::obj::Obj {
FlowMsg::Info => Ok(result::INFO.as_obj()),
FlowMsg::Choice(i) => i.try_into(),
FlowMsg::Text(s) => s.as_str().try_into(),
FlowMsg::Number(i) => i.try_into(),
}
}
}

View File

@ -3,12 +3,8 @@ use crate::ui::{
event::BLEEvent,
geometry::Rect,
shape::Renderer,
util::Pager,
};
#[cfg(all(feature = "micropython", feature = "touch"))]
use crate::ui::{component::swipe_detect::SwipeConfig, flow::Swipable};
pub struct BLEHandler<T> {
inner: T,
waiting_for_pairing: bool,
@ -29,19 +25,6 @@ impl<T> BLEHandler<T> {
}
}
#[cfg(all(feature = "micropython", feature = "touch"))]
impl<T> Swipable for BLEHandler<T>
where
T: Component,
{
fn get_pager(&self) -> Pager {
Pager::single_page()
}
fn get_swipe_config(&self) -> SwipeConfig {
SwipeConfig::default()
}
}
impl<T> Component for BLEHandler<T>
where
T: Component,

View File

@ -892,7 +892,18 @@ impl FirmwareUI for UIBolt {
))
}
fn show_pairing_code(
#[cfg(feature = "ble")]
fn show_ble_pairing_code(
_title: TString<'static>,
_description: TString<'static>,
_code: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error> {
Err::<RootComponent<Empty, ModelUI>, Error>(Error::ValueError(
c"show_ble_pairing_code not supported",
))
}
fn show_thp_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,

View File

@ -1089,13 +1089,24 @@ impl FirmwareUI for UICaesar {
))
}
fn show_pairing_code(
#[cfg(feature = "ble")]
fn show_ble_pairing_code(
_title: TString<'static>,
_description: TString<'static>,
_code: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error> {
Err::<RootComponent<Empty, ModelUI>, Error>(Error::ValueError(
c"show_pairing_code not supported",
c"show_ble_pairing_code not supported",
))
}
fn show_thp_pairing_code(
_title: TString<'static>,
_description: TString<'static>,
_code: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error> {
Err::<RootComponent<Empty, ModelUI>, Error>(Error::ValueError(
c"show_thp_pairing_code not supported",
))
}

View File

@ -916,13 +916,24 @@ impl FirmwareUI for UIDelizia {
))
}
fn show_pairing_code(
#[cfg(feature = "ble")]
fn show_ble_pairing_code(
_title: TString<'static>,
_description: TString<'static>,
_code: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error> {
Err::<RootComponent<Empty, ModelUI>, Error>(Error::ValueError(
c"show_pairing_code not supported",
c"show_ble_pairing_code not supported",
))
}
fn show_thp_pairing_code(
_title: TString<'static>,
_description: TString<'static>,
_code: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error> {
Err::<RootComponent<Empty, ModelUI>, Error>(Error::ValueError(
c"show_thp_pairing_code not supported",
))
}

View File

@ -13,8 +13,8 @@ pub mod prompt_backup;
pub mod request_number;
pub mod request_passphrase;
pub mod show_danger;
pub mod show_pairing_code;
pub mod show_share_words;
pub mod show_thp_pairing_code;
#[cfg(feature = "universal_fw")]
pub use confirm_fido::new_confirm_fido;
@ -31,5 +31,5 @@ pub use prompt_backup::PromptBackup;
pub use request_number::new_request_number;
pub use request_passphrase::RequestPassphrase;
pub use show_danger::ShowDanger;
pub use show_pairing_code::new_show_pairing_code;
pub use show_share_words::new_show_share_words_flow;
pub use show_thp_pairing_code::new_show_thp_pairing_code;

View File

@ -12,9 +12,6 @@ use crate::{
},
};
#[cfg(feature = "ble")]
use crate::ui::component::BLEHandlerMsg;
use super::super::{
component::Button,
firmware::{
@ -45,9 +42,6 @@ impl FlowController for ShowPairingCode {
(Self::Main, FlowMsg::Cancelled) => self.return_msg(FlowMsg::Cancelled),
(Self::Main, FlowMsg::Confirmed) => self.return_msg(FlowMsg::Confirmed),
(Self::Main, FlowMsg::Info) => Self::Menu.goto(),
(Self::Main, FlowMsg::Number(pairing_code)) => {
self.return_msg(FlowMsg::Number(pairing_code))
}
(Self::Menu, FlowMsg::Choice(..)) => self.return_msg(FlowMsg::Cancelled),
(Self::Menu, FlowMsg::Cancelled) => Self::Main.goto(),
_ => self.do_nothing(),
@ -55,7 +49,7 @@ impl FlowController for ShowPairingCode {
}
}
pub fn new_show_pairing_code(
pub fn new_show_thp_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,
@ -69,16 +63,6 @@ pub fn new_show_pairing_code(
.add_text(code, fonts::FONT_SATOSHI_EXTRALIGHT_72);
let screen =
TextScreen::new(FormattedText::new(ops)).with_header(Header::new(title).with_menu_button());
#[cfg(feature = "ble")]
let main_content = crate::ui::component::BLEHandler::new(screen, false).map(|msg| match msg {
BLEHandlerMsg::Cancelled => Some(FlowMsg::Cancelled),
BLEHandlerMsg::Content(TextScreenMsg::Cancelled) => Some(FlowMsg::Cancelled),
BLEHandlerMsg::Content(TextScreenMsg::Confirmed) => Some(FlowMsg::Confirmed),
BLEHandlerMsg::Content(TextScreenMsg::Menu) => Some(FlowMsg::Info),
BLEHandlerMsg::PairingCode(code) => Some(FlowMsg::Number(code)),
});
#[cfg(not(feature = "ble"))]
let main_content = screen.map(|msg| match msg {
TextScreenMsg::Cancelled => Some(FlowMsg::Cancelled),
TextScreenMsg::Confirmed => Some(FlowMsg::Confirmed),

View File

@ -18,7 +18,7 @@ use crate::{
},
ComponentExt as _, Empty, FormattedText, Timeout,
},
geometry::{LinearPlacement, Offset},
geometry::{Alignment, LinearPlacement, Offset},
layout::{
obj::{LayoutMaybeTrace, LayoutObj, RootComponent},
util::{ConfirmValueParams, PropsList, RecoveryType, StrOrBytes},
@ -1079,12 +1079,36 @@ impl FirmwareUI for UIEckhart {
Ok(layout)
}
fn show_pairing_code(
#[cfg(feature = "ble")]
fn show_ble_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error> {
let flow = flow::show_pairing_code::new_show_pairing_code(title, description, code)?;
let mut ops = OpTextLayout::new(theme::firmware::TEXT_REGULAR);
ops.add_text(description, fonts::FONT_SATOSHI_REGULAR_38)
.add_newline()
.add_newline()
.add_newline()
.add_alignment(Alignment::Center)
.add_text(code, fonts::FONT_SATOSHI_EXTRALIGHT_72);
let screen = crate::ui::component::BLEHandler::new(
TextScreen::new(FormattedText::new(ops))
.with_header(Header::new(title))
.with_action_bar(ActionBar::new_cancel_confirm()),
false,
);
let layout = RootComponent::new(screen);
Ok(layout)
}
fn show_thp_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error> {
let flow =
flow::show_thp_pairing_code::new_show_thp_pairing_code(title, description, code)?;
Ok(flow)
}

View File

@ -325,7 +325,14 @@ pub trait FirmwareUI {
device_name: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error>;
fn show_pairing_code(
#[cfg(feature = "ble")]
fn show_ble_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error>;
fn show_thp_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,

View File

@ -571,16 +571,26 @@ def show_pairing_device_name(
# rust/src/ui/api/firmware_micropython.rs
def show_pairing_code(
def show_ble_pairing_code(
*,
title: str,
description: str,
code: str,
) -> LayoutObj[UiResult]:
"""Pairing device: second screen (pairing code).
"""BLE pairing: second screen (pairing code).
Returns on BLEEvent::{PairingCanceled, Disconnected}."""
# rust/src/ui/api/firmware_micropython.rs
def show_thp_pairing_code(
*,
title: str,
description: str,
code: str,
) -> LayoutObj[UiResult]:
"""THP pairing: second screen (pairing code)."""
# rust/src/ui/api/firmware_micropython.rs
def show_info(
*,

View File

@ -28,7 +28,7 @@ async def pair_new_device() -> None:
try:
result = await raise_if_not_confirmed(
trezorui_api.show_pairing_code(
trezorui_api.show_ble_pairing_code(
title="Bluetooth pairing",
description="Pairing code match?",
code=f"{code:0>6}",