Skip to content

Commit

Permalink
refactor(core/rust): introduce UIFeaturesXXX trait as an abstraction …
Browse files Browse the repository at this point in the history
…over different UI implementation

[no changelog]
  • Loading branch information
TychoVrahe committed Mar 28, 2024
1 parent b2649b0 commit 0524a35
Show file tree
Hide file tree
Showing 18 changed files with 558 additions and 262 deletions.
33 changes: 2 additions & 31 deletions core/embed/rust/rust_ui.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
#include <stdbool.h>

#include "common.h"

uint32_t screen_install_confirm(const char* vendor_str, uint8_t vendor_str_len,
const char* version_str,
const uint8_t* fingerprint,
bool should_keep_seed, bool is_newvendor,
int version_cmp);
uint32_t screen_wipe_confirm(void);
void screen_install_progress(int16_t progress, bool initialize,
bool initial_setup);
void screen_wipe_progress(int16_t progress, bool initialize);
uint32_t screen_intro(const char* bld_version_str, const char* vendor_str,
uint8_t vendor_str_len, const char* version_str,
bool fw_ok);
uint32_t screen_menu(secbool firmware_present);
void screen_connect(bool initial_setup);
void screen_fatal_error_rust(const char* title, const char* msg,
const char* footer);
void screen_wipe_success(void);
void screen_wipe_fail(void);
uint32_t screen_install_success(uint8_t restart_seconds, bool initial_setup,
bool complete_draw);
uint32_t screen_install_fail(void);
void screen_welcome(void);
void screen_boot_empty(bool fading);
void screen_boot_full(void);
uint32_t screen_unlock_bootloader_confirm(void);
void screen_unlock_bootloader_success(void);
void display_image(int16_t x, int16_t y, const uint8_t* data, uint32_t datalen);
void display_icon(int16_t x, int16_t y, const uint8_t* data, uint32_t datalen,
uint16_t fg_color, uint16_t bg_color);
void bld_continue_label(uint16_t bg_color);
#include "rust_ui_bootloader.h"
#include "rust_ui_common.h"
26 changes: 26 additions & 0 deletions core/embed/rust/rust_ui_bootloader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "common.h"

uint32_t screen_install_confirm(const char* vendor_str, uint8_t vendor_str_len,
const char* version_str,
const uint8_t* fingerprint,
bool should_keep_seed, bool is_newvendor,
int version_cmp);
uint32_t screen_wipe_confirm(void);
void screen_install_progress(int16_t progress, bool initialize,
bool initial_setup);
void screen_wipe_progress(int16_t progress, bool initialize);
uint32_t screen_intro(const char* bld_version_str, const char* vendor_str,
uint8_t vendor_str_len, const char* version_str,
bool fw_ok);
uint32_t screen_menu(secbool firmware_present);
void screen_connect(bool initial_setup);
void screen_wipe_success(void);
void screen_wipe_fail(void);
uint32_t screen_install_success(uint8_t restart_seconds, bool initial_setup,
bool complete_draw);
uint32_t screen_install_fail(void);
void screen_welcome(void);
void screen_boot_empty(bool fading);
uint32_t screen_unlock_bootloader_confirm(void);
void screen_unlock_bootloader_success(void);
void bld_continue_label(uint16_t bg_color);
10 changes: 10 additions & 0 deletions core/embed/rust/rust_ui_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "common.h"

void screen_fatal_error_rust(const char* title, const char* msg,
const char* footer);

void screen_boot_full(void);

void display_image(int16_t x, int16_t y, const uint8_t* data, uint32_t datalen);
void display_icon(int16_t x, int16_t y, const uint8_t* data, uint32_t datalen,
uint16_t fg_color, uint16_t bg_color);
6 changes: 3 additions & 3 deletions core/embed/rust/src/trezorhal/fatal_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ mod ffi {
}
}

use crate::ui::screens::screen_fatal_error;
use crate::ui::ui_features::{ModelUICommon, UIFeaturesCommon};

fn shutdown() -> ! {
unsafe { ffi::trezor_shutdown() }
}

#[cfg(feature = "bootloader")]
pub fn __fatal_error(_expr: &str, _msg: &str, _file: &str, _line: u32, _func: &str) -> ! {
screen_fatal_error("BL.rs", "BL.rs", "PLEASE VISIT\nTREZOR.IO/RSOD");
ModelUICommon::screen_fatal_error("BL.rs", "BL.rs", "PLEASE VISIT\nTREZOR.IO/RSOD");
shutdown()
}

#[cfg(not(feature = "bootloader"))]
pub fn __fatal_error(_expr: &str, msg: &str, _file: &str, _line: u32, _func: &str) -> ! {
screen_fatal_error("INTERNAL_ERROR", msg, "PLEASE VISIT\nTREZOR.IO/RSOD");
ModelUICommon::screen_fatal_error("INTERNAL_ERROR", msg, "PLEASE VISIT\nTREZOR.IO/RSOD");
shutdown()
}

Expand Down
127 changes: 127 additions & 0 deletions core/embed/rust/src/ui/api/bootloader_c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use crate::{
strutil::hexlify,
trezorhal::secbool::secbool,
ui::{
ui_features::{ModelUIBootloader, UIFeaturesBootloader},
util::{from_c_array, from_c_str},
},
};

#[no_mangle]
extern "C" fn screen_welcome() {
ModelUIBootloader::screen_welcome();
}

#[no_mangle]
extern "C" fn bld_continue_label(bg_color: cty::uint16_t) {
ModelUIBootloader::bld_continue_label(bg_color.into());
}

#[no_mangle]
extern "C" fn screen_install_success(
restart_seconds: u8,
initial_setup: bool,
complete_draw: bool,
) {
ModelUIBootloader::screen_install_success(restart_seconds, initial_setup, complete_draw);
}

#[no_mangle]
extern "C" fn screen_install_fail() {
ModelUIBootloader::screen_install_fail();
}

#[no_mangle]
extern "C" fn screen_install_confirm(
vendor_str: *const cty::c_char,
vendor_str_len: u8,
version: *const cty::c_char,
fingerprint: *const cty::uint8_t,
should_keep_seed: bool,
is_newvendor: bool,
version_cmp: cty::c_int,
) -> u32 {
let text = unwrap!(unsafe { from_c_array(vendor_str, vendor_str_len as usize) });
let version = unwrap!(unsafe { from_c_str(version) });

let mut fingerprint_buffer: [u8; 64] = [0; 64];
let fingerprint_str = unsafe {
let fingerprint_slice = core::slice::from_raw_parts(fingerprint, 32);
hexlify(fingerprint_slice, &mut fingerprint_buffer);
core::str::from_utf8_unchecked(fingerprint_buffer.as_ref())
};

ModelUIBootloader::screen_install_confirm(
text,
version,
fingerprint_str,
should_keep_seed,
is_newvendor,
version_cmp,
)
}

#[no_mangle]
extern "C" fn screen_wipe_confirm() -> u32 {
ModelUIBootloader::screen_wipe_confirm()
}

#[no_mangle]
extern "C" fn screen_unlock_bootloader_confirm() -> u32 {
ModelUIBootloader::screen_unlock_bootloader_confirm()
}

#[no_mangle]
extern "C" fn screen_unlock_bootloader_success() {
ModelUIBootloader::screen_unlock_bootloader_success();
}

#[no_mangle]
extern "C" fn screen_menu(firmware_present: secbool) -> u32 {
ModelUIBootloader::screen_menu(firmware_present)
}

#[no_mangle]
extern "C" fn screen_intro(
bld_version: *const cty::c_char,
vendor_str: *const cty::c_char,
vendor_str_len: u8,
version: *const cty::c_char,
fw_ok: bool,
) -> u32 {
let vendor = unwrap!(unsafe { from_c_array(vendor_str, vendor_str_len as usize) });
let version = unwrap!(unsafe { from_c_str(version) });
let bld_version = unwrap!(unsafe { from_c_str(bld_version) });

ModelUIBootloader::screen_intro(bld_version, vendor, version, fw_ok)
}

#[no_mangle]
extern "C" fn screen_boot_empty(fading: bool) {
ModelUIBootloader::screen_boot_empty(fading)
}

#[no_mangle]
extern "C" fn screen_wipe_progress(progress: u16, initialize: bool) {
ModelUIBootloader::screen_wipe_progress(progress, initialize)
}

#[no_mangle]
extern "C" fn screen_install_progress(progress: u16, initialize: bool, initial_setup: bool) {
ModelUIBootloader::screen_install_progress(progress, initialize, initial_setup)
}

#[no_mangle]
extern "C" fn screen_connect(initial_setup: bool) {
ModelUIBootloader::screen_connect(initial_setup)
}

#[no_mangle]
extern "C" fn screen_wipe_success() {
ModelUIBootloader::screen_wipe_success()
}

#[no_mangle]
extern "C" fn screen_wipe_fail() {
ModelUIBootloader::screen_wipe_fail()
}
60 changes: 60 additions & 0 deletions core/embed/rust/src/ui/api/common_c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Reexporting the `screens` module according to the
//! current feature (Trezor model)

use crate::ui::{
component::image::Image,
display::{Color, Icon},
geometry::{Alignment2D, Point},
ui_features::{ModelUICommon, UIFeaturesCommon},
};

use crate::ui::util::from_c_str;

#[no_mangle]
extern "C" fn screen_fatal_error_rust(
title: *const cty::c_char,
msg: *const cty::c_char,
footer: *const cty::c_char,
) {
let title = unsafe { from_c_str(title) }.unwrap_or("");
let msg = unsafe { from_c_str(msg) }.unwrap_or("");
let footer = unsafe { from_c_str(footer) }.unwrap_or("");

ModelUICommon::screen_fatal_error(title, msg, footer);
}

#[no_mangle]
extern "C" fn screen_boot_full() {
ModelUICommon::screen_boot_full();
}

#[no_mangle]
extern "C" fn display_icon(
x: cty::int16_t,
y: cty::int16_t,
data: *const cty::uint8_t,
data_len: cty::uint32_t,
fg_color: cty::uint16_t,
bg_color: cty::uint16_t,
) {
let data_slice = unsafe { core::slice::from_raw_parts(data, data_len as usize) };
let icon = Icon::new(data_slice);
icon.draw(
Point::new(x, y),
Alignment2D::TOP_LEFT,
Color::from_u16(fg_color),
Color::from_u16(bg_color),
);
}

#[no_mangle]
extern "C" fn display_image(
x: cty::int16_t,
y: cty::int16_t,
data: *const cty::uint8_t,
data_len: cty::uint32_t,
) {
let data_slice = unsafe { core::slice::from_raw_parts(data, data_len as usize) };
let image = Image::new(data_slice);
image.draw(Point::new(x, y), Alignment2D::TOP_LEFT);
}
4 changes: 4 additions & 0 deletions core/embed/rust/src/ui/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod common_c;

#[cfg(feature = "bootloader")]
pub mod bootloader_c;
31 changes: 0 additions & 31 deletions core/embed/rust/src/ui/display/toif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,6 @@ pub fn render_toif(toif: &Toif, center: Point, fg_color: Color, bg_color: Color)
pixeldata_dirty();
}

#[no_mangle]
extern "C" fn display_image(
x: cty::int16_t,
y: cty::int16_t,
data: *const cty::uint8_t,
data_len: cty::uint32_t,
) {
let data_slice = unsafe { core::slice::from_raw_parts(data, data_len as usize) };
let image = Image::new(data_slice);
image.draw(Point::new(x, y), Alignment2D::TOP_LEFT);
}

#[cfg(feature = "dma2d")]
pub fn image(image: &Image, center: Point) {
let r = Rect::from_center_and_size(center, image.toif.size());
Expand Down Expand Up @@ -303,25 +291,6 @@ impl<'i> Toif<'i> {
}
}

#[no_mangle]
extern "C" fn display_icon(
x: cty::int16_t,
y: cty::int16_t,
data: *const cty::uint8_t,
data_len: cty::uint32_t,
fg_color: cty::uint16_t,
bg_color: cty::uint16_t,
) {
let data_slice = unsafe { core::slice::from_raw_parts(data, data_len as usize) };
let icon = Icon::new(data_slice);
icon.draw(
Point::new(x, y),
Alignment2D::TOP_LEFT,
Color::from_u16(fg_color),
Color::from_u16(bg_color),
);
}

#[derive(PartialEq, Eq, Clone, Copy)]
pub struct Icon {
pub toif: Toif<'static>,
Expand Down
27 changes: 8 additions & 19 deletions core/embed/rust/src/ui/layout/simplified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,11 @@ use crate::ui::event::TouchEvent;
use crate::ui::{
component::{Component, Event, EventCtx, Never},
display,
geometry::Rect,
ui_features::ModelUICommon,
UIFeaturesCommon,
};
use num_traits::ToPrimitive;

pub trait SimplifiedFeatures {
fn fadein() {}
fn fadeout() {}

const SCREEN: Rect;
}

#[cfg(all(feature = "model_tr", not(feature = "model_tt")))]
pub type ModelFeatures = crate::ui::model_tr::ModelTRFeatures;
#[cfg(feature = "model_tt")]
pub type ModelFeatures = crate::ui::model_tt::ModelTTFeatures;

pub trait ReturnToC {
fn return_to_c(self) -> u32;
}
Expand Down Expand Up @@ -79,12 +68,12 @@ where
F: Component,
F::Msg: ReturnToC,
{
frame.place(ModelFeatures::SCREEN);
ModelFeatures::fadeout();
frame.place(ModelUICommon::SCREEN);
ModelUICommon::fadeout();
display::sync();
frame.paint();
display::refresh();
ModelFeatures::fadein();
ModelUICommon::fadein();

#[cfg(feature = "button")]
while button_eval().is_some() {}
Expand Down Expand Up @@ -115,14 +104,14 @@ pub fn show<F>(frame: &mut F, fading: bool)
where
F: Component,
{
frame.place(ModelFeatures::SCREEN);
frame.place(ModelUICommon::SCREEN);
if fading {
ModelFeatures::fadeout()
ModelUICommon::fadeout()
};
display::sync();
frame.paint();
display::refresh();
if fading {
ModelFeatures::fadein()
ModelUICommon::fadein()
};
}
Loading

0 comments on commit 0524a35

Please sign in to comment.