Skip to content

Commit 2fe39d2

Browse files
authored
Merge pull request #76 from LedgerHQ/y333/use_sync_nbgl_homeandsettings
Use async NbglHomeAndSettings and stop redrawing screen for every received…
2 parents bddcf06 + 52811eb commit 2fe39d2

File tree

9 files changed

+58
-36
lines changed

9 files changed

+58
-36
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
22
name = "app-boilerplate-rust"
3-
version = "1.5.1"
3+
version = "1.6.0"
44
authors = ["yhql", "agrojean-ledger"]
55
edition = "2021"
66

77
[dependencies]
8-
ledger_device_sdk = { version="1.15.2" }
8+
ledger_device_sdk = { version="1.16.0" }
99
include_gif = "1.2.0"
1010
serde = {version="1.0.192", default_features = false, features = ["derive"]}
1111
serde-json-core = { git = "https://github.com/rust-embedded-community/serde-json-core"}

src/app_ui/menu.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*****************************************************************************/
1717

1818
use include_gif::include_gif;
19-
use ledger_device_sdk::io::{Comm, Event};
19+
use ledger_device_sdk::io::Comm;
2020

2121
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
2222
use ledger_device_sdk::ui::{
@@ -29,7 +29,10 @@ use crate::settings::Settings;
2929
#[cfg(any(target_os = "stax", target_os = "flex"))]
3030
use ledger_device_sdk::nbgl::{NbglGlyph, NbglHomeAndSettings};
3131

32+
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
3233
use crate::Instruction;
34+
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
35+
use ledger_device_sdk::io::Event;
3336

3437
// use ledger_device_sdk::nvm::*;
3538

@@ -70,12 +73,13 @@ pub fn ui_menu_main(comm: &mut Comm) -> Event<Instruction> {
7073
}
7174

7275
#[cfg(any(target_os = "stax", target_os = "flex"))]
73-
pub fn ui_menu_main(_: &mut Comm) -> Event<Instruction> {
76+
pub fn ui_menu_main(_: &mut Comm) -> NbglHomeAndSettings {
7477
// Load glyph from 64x64 4bpp gif file with include_gif macro. Creates an NBGL compatible glyph.
7578
const FERRIS: NbglGlyph = NbglGlyph::from_include(include_gif!("crab_64x64.gif", NBGL));
7679

7780
let settings_strings = [["Display Memo", "Allow display of transaction memo."]];
7881
let mut settings: Settings = Default::default();
82+
7983
// Display the home screen.
8084
NbglHomeAndSettings::new()
8185
.glyph(&FERRIS)
@@ -85,5 +89,4 @@ pub fn ui_menu_main(_: &mut Comm) -> Event<Instruction> {
8589
env!("CARGO_PKG_VERSION"),
8690
env!("CARGO_PKG_AUTHORS"),
8791
)
88-
.show()
8992
}

src/handlers/sign_tx.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ use ledger_device_sdk::ecc::{Secp256k1, SeedDerive};
2222
use ledger_device_sdk::hash::{sha3::Keccak256, HashInit};
2323
use ledger_device_sdk::io::Comm;
2424

25+
#[cfg(any(target_os = "stax", target_os = "flex"))]
26+
use ledger_device_sdk::nbgl::NbglHomeAndSettings;
27+
2528
use serde::Deserialize;
2629
use serde_json_core::from_slice;
2730

@@ -42,6 +45,8 @@ pub struct TxContext {
4245
raw_tx: Vec<u8>,
4346
path: Bip32Path,
4447
review_finished: bool,
48+
#[cfg(any(target_os = "stax", target_os = "flex"))]
49+
pub home: NbglHomeAndSettings,
4550
}
4651

4752
// Implement constructor for TxInfo with default values
@@ -52,6 +57,8 @@ impl TxContext {
5257
raw_tx: Vec::new(),
5358
path: Default::default(),
5459
review_finished: false,
60+
#[cfg(any(target_os = "stax", target_os = "flex"))]
61+
home: Default::default(),
5562
}
5663
}
5764
// Get review status

src/main.rs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ use handlers::{
3838
get_version::handler_get_version,
3939
sign_tx::{handler_sign_tx, TxContext},
4040
};
41-
use ledger_device_sdk::io::{ApduHeader, Comm, Event, Reply, StatusWords};
41+
use ledger_device_sdk::io::{ApduHeader, Comm, Reply, StatusWords};
4242
#[cfg(feature = "pending_review_screen")]
4343
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
4444
use ledger_device_sdk::ui::gadgets::display_pending_review;
4545

46+
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
47+
use ledger_device_sdk::io::Event;
48+
4649
ledger_device_sdk::set_panic!(ledger_device_sdk::exiting_panic);
4750

4851
// Required for using String, Vec, format!...
@@ -129,7 +132,7 @@ impl TryFrom<ApduHeader> for Instruction {
129132
}
130133

131134
#[cfg(any(target_os = "stax", target_os = "flex"))]
132-
fn show_status_if_needed(ins: &Instruction, tx_ctx: &TxContext, status: &AppSW) {
135+
fn show_status_and_home_if_needed(ins: &Instruction, tx_ctx: &mut TxContext, status: &AppSW) {
133136
let (show_status, status_type) = match (ins, status) {
134137
(Instruction::GetPubkey { display: true }, AppSW::Deny | AppSW::Ok) => {
135138
(true, StatusType::Address)
@@ -145,6 +148,9 @@ fn show_status_if_needed(ins: &Instruction, tx_ctx: &TxContext, status: &AppSW)
145148
NbglReviewStatus::new()
146149
.status_type(status_type)
147150
.show(success);
151+
152+
// call home.show_and_return() to show home and setting screen
153+
tx_ctx.home.show_and_return();
148154
}
149155
}
150156

@@ -155,38 +161,44 @@ extern "C" fn sample_main() {
155161
// BadCla status word.
156162
let mut comm = Comm::new().set_expected_cla(0xe0);
157163

158-
// Initialize reference to Comm instance for NBGL
159-
// API calls.
164+
let mut tx_ctx = TxContext::new();
165+
160166
#[cfg(any(target_os = "stax", target_os = "flex"))]
161-
init_comm(&mut comm);
167+
{
168+
// Initialize reference to Comm instance for NBGL
169+
// API calls.
170+
init_comm(&mut comm);
171+
tx_ctx.home = ui_menu_main(&mut comm);
172+
tx_ctx.home.show_and_return();
173+
}
162174

163-
// Developer mode / pending review popup
164-
// must be cleared with user interaction
165-
#[cfg(feature = "pending_review_screen")]
166175
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
176+
#[cfg(feature = "pending_review_screen")]
167177
display_pending_review(&mut comm);
168178

169-
let mut tx_ctx = TxContext::new();
170-
171179
loop {
172-
// Wait for either a specific button push to exit the app
173-
// or an APDU command
174-
if let Event::Command(ins) = ui_menu_main(&mut comm) {
175-
let result = handle_apdu(&mut comm, &ins, &mut tx_ctx);
176-
let _status: AppSW = match result {
177-
Ok(()) => {
178-
comm.reply_ok();
179-
AppSW::Ok
180-
}
181-
Err(sw) => {
182-
comm.reply(sw);
183-
sw
184-
}
185-
};
186-
187-
#[cfg(any(target_os = "stax", target_os = "flex"))]
188-
show_status_if_needed(&ins, &tx_ctx, &_status);
189-
}
180+
#[cfg(any(target_os = "stax", target_os = "flex"))]
181+
let ins: Instruction = comm.next_command();
182+
183+
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
184+
let ins = if let Event::Command(ins) = ui_menu_main(&mut comm) {
185+
ins
186+
} else {
187+
continue;
188+
};
189+
190+
let _status = match handle_apdu(&mut comm, &ins, &mut tx_ctx) {
191+
Ok(()) => {
192+
comm.reply_ok();
193+
AppSW::Ok
194+
}
195+
Err(sw) => {
196+
comm.reply(sw);
197+
sw
198+
}
199+
};
200+
#[cfg(any(target_os = "stax", target_os = "flex"))]
201+
show_status_and_home_if_needed(&ins, &mut tx_ctx, &_status);
190202
}
191203
}
192204

218 Bytes
Loading
-5 Bytes
Loading
-5 Bytes
Loading
221 Bytes
Loading

0 commit comments

Comments
 (0)