Skip to content

Commit 711225d

Browse files
authored
Merge pull request #188 from LedgerHQ/y333/new_nbgl
Y333/new nbgl
2 parents f6faf62 + 85df212 commit 711225d

File tree

4 files changed

+91
-14
lines changed

4 files changed

+91
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ledger_device_sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ledger_device_sdk"
3-
version = "1.15.6"
3+
version = "1.16.0"
44
authors = ["yhql", "yogh333", "agrojean-ledger", "kingofpayne"]
55
edition = "2021"
66
license.workspace = true

ledger_device_sdk/src/nbgl.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::nvm::*;
33
use const_zero::const_zero;
44
extern crate alloc;
55
use alloc::ffi::CString;
6-
use alloc::vec::Vec;
6+
use alloc::{vec, vec::Vec};
77
use core::ffi::{c_char, c_int};
88
use core::mem::transmute;
99
use ledger_secure_sdk_sys::*;
@@ -32,10 +32,6 @@ pub use nbgl_status::*;
3232
pub use nbgl_streaming_review::*;
3333

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

4036
#[derive(Copy, Clone)]
4137
enum SyncNbgl {

ledger_device_sdk/src/nbgl/nbgl_home_and_settings.rs

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use super::*;
22

3+
pub const SETTINGS_SIZE: usize = 10;
4+
static mut NVM_REF: Option<&mut AtomicStorage<[u8; SETTINGS_SIZE]>> = None;
5+
static mut SWITCH_ARRAY: [nbgl_contentSwitch_t; SETTINGS_SIZE] =
6+
[unsafe { const_zero!(nbgl_contentSwitch_t) }; SETTINGS_SIZE];
7+
38
/// Callback triggered by the NBGL API when a setting switch is toggled.
49
unsafe extern "C" fn settings_callback(token: c_int, _index: u8, _page: c_int) {
510
let idx = token - FIRST_USER_TOKEN as i32;
@@ -16,7 +21,7 @@ unsafe extern "C" fn settings_callback(token: c_int, _index: u8, _page: c_int) {
1621
}
1722

1823
if let Some(data) = NVM_REF.as_mut() {
19-
let mut switch_values: [u8; SETTINGS_SIZE] = data.get_ref().clone();
24+
let mut switch_values: [u8; SETTINGS_SIZE] = *data.get_ref();
2025
if switch_values[setting_idx] == OFF_STATE {
2126
switch_values[setting_idx] = ON_STATE;
2227
} else {
@@ -49,6 +54,16 @@ pub struct NbglHomeAndSettings {
4954

5055
impl SyncNBGL for NbglHomeAndSettings {}
5156

57+
unsafe extern "C" fn quit_cb() {
58+
exit_app(0);
59+
}
60+
61+
impl<'a> Default for NbglHomeAndSettings {
62+
fn default() -> Self {
63+
Self::new()
64+
}
65+
}
66+
5267
impl<'a> NbglHomeAndSettings {
5368
pub fn new() -> NbglHomeAndSettings {
5469
NbglHomeAndSettings {
@@ -66,7 +81,7 @@ impl<'a> NbglHomeAndSettings {
6681

6782
pub fn glyph(self, glyph: &'a NbglGlyph) -> NbglHomeAndSettings {
6883
let icon = glyph.into();
69-
NbglHomeAndSettings { icon: icon, ..self }
84+
NbglHomeAndSettings { icon, ..self }
7085
}
7186

7287
pub fn infos(
@@ -75,9 +90,10 @@ impl<'a> NbglHomeAndSettings {
7590
version: &'a str,
7691
author: &'a str,
7792
) -> NbglHomeAndSettings {
78-
let mut v: Vec<CString> = Vec::new();
79-
v.push(CString::new(version).unwrap());
80-
v.push(CString::new(author).unwrap());
93+
let v: Vec<CString> = vec![
94+
CString::new(version).unwrap(),
95+
CString::new(author).unwrap(),
96+
];
8197

8298
NbglHomeAndSettings {
8399
app_name: CString::new(app_name).unwrap(),
@@ -111,6 +127,10 @@ impl<'a> NbglHomeAndSettings {
111127
}
112128
}
113129

130+
/// Show the home screen and settings page.
131+
/// This function will block until an APDU is received or the user quits the app.
132+
/// DEPRECATED as it constraints to refresh screen for every received APDU.
133+
/// Use `show_and_return` instead.
114134
pub fn show<T: TryFrom<ApduHeader>>(&mut self) -> Event<T>
115135
where
116136
Reply: From<<T as TryFrom<ApduHeader>>::Error>,
@@ -124,8 +144,8 @@ impl<'a> NbglHomeAndSettings {
124144
.collect::<Vec<_>>();
125145

126146
self.info_list = nbgl_contentInfoList_t {
127-
infoTypes: INFO_FIELDS.as_ptr() as *const *const c_char,
128-
infoContents: self.info_contents_ptr[..].as_ptr() as *const *const c_char,
147+
infoTypes: INFO_FIELDS.as_ptr(),
148+
infoContents: self.info_contents_ptr[..].as_ptr(),
129149
nbInfos: INFO_FIELDS.len() as u8,
130150
};
131151

@@ -190,4 +210,65 @@ impl<'a> NbglHomeAndSettings {
190210
}
191211
}
192212
}
213+
214+
/// Show the home screen and settings page.
215+
/// This function returns immediately after the screen is displayed.
216+
pub fn show_and_return(&mut self) {
217+
unsafe {
218+
self.info_contents_ptr = self
219+
.info_contents
220+
.iter()
221+
.map(|s| s.as_ptr())
222+
.collect::<Vec<_>>();
223+
224+
self.info_list = nbgl_contentInfoList_t {
225+
infoTypes: INFO_FIELDS.as_ptr(),
226+
infoContents: self.info_contents_ptr[..].as_ptr(),
227+
nbInfos: INFO_FIELDS.len() as u8,
228+
};
229+
230+
for (i, setting) in self.setting_contents.iter().enumerate() {
231+
SWITCH_ARRAY[i].text = setting[0].as_ptr();
232+
SWITCH_ARRAY[i].subText = setting[1].as_ptr();
233+
let state = if let Some(data) = NVM_REF.as_mut() {
234+
data.get_ref()[i]
235+
} else {
236+
OFF_STATE
237+
};
238+
SWITCH_ARRAY[i].initState = state;
239+
SWITCH_ARRAY[i].token = (FIRST_USER_TOKEN + i as u32) as u8;
240+
SWITCH_ARRAY[i].tuneId = TuneIndex::TapCasual as u8;
241+
}
242+
243+
self.content = nbgl_content_t {
244+
content: nbgl_content_u {
245+
switchesList: nbgl_pageSwitchesList_s {
246+
switches: &SWITCH_ARRAY as *const nbgl_contentSwitch_t,
247+
nbSwitches: self.nb_settings,
248+
},
249+
},
250+
contentActionCallback: Some(settings_callback),
251+
type_: SWITCHES_LIST,
252+
};
253+
254+
self.generic_contents = nbgl_genericContents_t {
255+
callbackCallNeeded: false,
256+
__bindgen_anon_1: nbgl_genericContents_t__bindgen_ty_1 {
257+
contentsList: &self.content as *const nbgl_content_t,
258+
},
259+
nbContents: if self.nb_settings > 0 { 1 } else { 0 },
260+
};
261+
262+
nbgl_useCaseHomeAndSettings(
263+
self.app_name.as_ptr() as *const c_char,
264+
&self.icon as *const nbgl_icon_details_t,
265+
core::ptr::null(),
266+
INIT_HOME_PAGE as u8,
267+
&self.generic_contents as *const nbgl_genericContents_t,
268+
&self.info_list as *const nbgl_contentInfoList_t,
269+
core::ptr::null(),
270+
Some(quit_cb),
271+
);
272+
}
273+
}
193274
}

0 commit comments

Comments
 (0)