Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Y333/new nbgl #188

Merged
merged 8 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions ledger_device_sdk/src/nbgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ pub use nbgl_status::*;
pub use nbgl_streaming_review::*;

static mut COMM_REF: Option<&mut Comm> = None;
pub const SETTINGS_SIZE: usize = 10;
static mut NVM_REF: Option<&mut AtomicStorage<[u8; SETTINGS_SIZE]>> = None;
static mut SWITCH_ARRAY: [nbgl_contentSwitch_t; SETTINGS_SIZE] =
[unsafe { const_zero!(nbgl_contentSwitch_t) }; SETTINGS_SIZE];

#[derive(Copy, Clone)]
enum SyncNbgl {
Expand Down
74 changes: 74 additions & 0 deletions ledger_device_sdk/src/nbgl/nbgl_home_and_settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use super::*;

pub const SETTINGS_SIZE: usize = 10;
static mut NVM_REF: Option<&mut AtomicStorage<[u8; SETTINGS_SIZE]>> = None;
static mut SWITCH_ARRAY: [nbgl_contentSwitch_t; SETTINGS_SIZE] =
[unsafe { const_zero!(nbgl_contentSwitch_t) }; SETTINGS_SIZE];

/// Callback triggered by the NBGL API when a setting switch is toggled.
unsafe extern "C" fn settings_callback(token: c_int, _index: u8, _page: c_int) {
let idx = token - FIRST_USER_TOKEN as i32;
Expand Down Expand Up @@ -49,6 +54,10 @@ pub struct NbglHomeAndSettings {

impl SyncNBGL for NbglHomeAndSettings {}

unsafe extern "C" fn quit_cb() {
exit_app(0);
}

impl<'a> NbglHomeAndSettings {
pub fn new() -> NbglHomeAndSettings {
NbglHomeAndSettings {
Expand Down Expand Up @@ -111,6 +120,10 @@ impl<'a> NbglHomeAndSettings {
}
}

/// Show the home screen and settings page.
/// This function will block until an APDU is received or the user quits the app.
/// DEPRECATED as it constraints to refresh screen for every received APDU.
/// Use `display` instead.
pub fn show<T: TryFrom<ApduHeader>>(&mut self) -> Event<T>
where
Reply: From<<T as TryFrom<ApduHeader>>::Error>,
Expand Down Expand Up @@ -190,4 +203,65 @@ impl<'a> NbglHomeAndSettings {
}
}
}

/// Show the home screen and settings page.
/// This function returns immediately after the screen is displayed.
pub fn display(&mut self) {
yogh333 marked this conversation as resolved.
Show resolved Hide resolved
unsafe {
self.info_contents_ptr = self
.info_contents
.iter()
.map(|s| s.as_ptr())
.collect::<Vec<_>>();

self.info_list = nbgl_contentInfoList_t {
infoTypes: INFO_FIELDS.as_ptr() as *const *const c_char,
infoContents: self.info_contents_ptr[..].as_ptr() as *const *const c_char,
nbInfos: INFO_FIELDS.len() as u8,
};

for (i, setting) in self.setting_contents.iter().enumerate() {
SWITCH_ARRAY[i].text = setting[0].as_ptr();
SWITCH_ARRAY[i].subText = setting[1].as_ptr();
let state = if let Some(data) = NVM_REF.as_mut() {
data.get_ref()[i]
} else {
OFF_STATE
};
SWITCH_ARRAY[i].initState = state;
SWITCH_ARRAY[i].token = (FIRST_USER_TOKEN + i as u32) as u8;
SWITCH_ARRAY[i].tuneId = TuneIndex::TapCasual as u8;
}

self.content = nbgl_content_t {
content: nbgl_content_u {
switchesList: nbgl_pageSwitchesList_s {
switches: &SWITCH_ARRAY as *const nbgl_contentSwitch_t,
nbSwitches: self.nb_settings,
},
},
contentActionCallback: Some(settings_callback),
type_: SWITCHES_LIST,
};

self.generic_contents = nbgl_genericContents_t {
callbackCallNeeded: false,
__bindgen_anon_1: nbgl_genericContents_t__bindgen_ty_1 {
contentsList: &self.content as *const nbgl_content_t,
},
nbContents: if self.nb_settings > 0 { 1 } else { 0 },
};

nbgl_useCaseHomeAndSettings(
self.app_name.as_ptr() as *const c_char,
&self.icon as *const nbgl_icon_details_t,
core::ptr::null(),
INIT_HOME_PAGE as u8,
&self.generic_contents as *const nbgl_genericContents_t,
&self.info_list as *const nbgl_contentInfoList_t,
core::ptr::null(),
Some(quit_cb),
);
}
}
}
Loading