Skip to content

Commit

Permalink
Update NBGL to integrate the new lib_ux_sync from the C SDK.
Browse files Browse the repository at this point in the history
  • Loading branch information
agrojean-ledger committed Apr 16, 2024
1 parent 6c4c59e commit fa7ca9c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 44 deletions.
90 changes: 52 additions & 38 deletions ledger_device_sdk/src/nbgl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::io::{ApduHeader, Comm, Event, Reply};
use const_zero::const_zero;
use core::str::from_utf8;
use core::mem::transmute;
use ledger_secure_sdk_sys::{nbgl_icon_details_t, nbgl_bpp_t};
use ledger_secure_sdk_sys::*;

#[derive(PartialEq)]
Expand All @@ -19,11 +17,9 @@ pub struct NbglHome<'a> {
}

pub struct NbglReview<'a>{
intro_str: &'a str,
long_press_str: &'a str,
long_press_button_str: &'a str,
confirmed_str: &'a str,
rejected_str: &'a str,
title: &'a str,
subtitle: &'a str,
finish_title: &'a str,
glyph: Option<&'a NbglGlyph<'a>>,
}

Expand All @@ -44,6 +40,9 @@ impl<'a> Into<nbgl_layoutTagValue_t> for Field<'a> {
item: self.name.as_ptr() as *const i8,
value: self.value.as_ptr() as *const i8,
valueIcon: core::ptr::null() as *const nbgl_icon_details_t,
_bitfield_align_1: [0; 0],
_bitfield_1: __BindgenBitfieldUnit::new([0; 1usize]),
__bindgen_padding_0: [0; 3usize],
}
}
}
Expand Down Expand Up @@ -95,17 +94,12 @@ impl<'a> Into<nbgl_icon_details_t> for &NbglGlyph<'a> {
}
}

// const INFOTYPES: [*const ::core::ffi::c_char; 2] = [
// "Version\0".as_ptr() as *const ::core::ffi::c_char,
// "Developer\0".as_ptr() as *const ::core::ffi::c_char,
// ];

#[no_mangle]
pub extern "C" fn recv_and_process_event(return_on_apdu: bool) -> bool {
pub extern "C" fn io_recv_and_process_event() -> bool {
unsafe {
if let Some(comm) = COMM_REF.as_mut() {
let apdu_received = comm.next_event_ahead::<ApduHeader>();
if apdu_received && return_on_apdu {
if apdu_received {
return true;
}
}
Expand Down Expand Up @@ -161,9 +155,30 @@ impl<'a> NbglHome<'a> {
core::ptr::null() as *const nbgl_icon_details_t
};
unsafe {
let info_list : nbgl_contentInfoList_t = nbgl_contentInfoList_t {
infoTypes: ["Version\0".as_ptr() as *const ::core::ffi::c_char,
"Developer\0".as_ptr() as *const ::core::ffi::c_char].as_ptr(),
infoContents: [self.info_contents[0].as_ptr() as *const ::core::ffi::c_char,
self.info_contents[1].as_ptr() as *const ::core::ffi::c_char].as_ptr(),
nbInfos: 2,
};

let setting_contents : nbgl_genericContents_t = nbgl_genericContents_t {
callbackCallNeeded: false,
__bindgen_anon_1: nbgl_genericContents_t__bindgen_ty_1 {
contentsList: core::ptr::null(),
},
nbContents: 0,
};
loop {
match ledger_secure_sdk_sys::sync_nbgl_useCaseHomeAndSettings(self.app_name.as_ptr() as *const ::core::ffi::c_char ,icon) {
ledger_secure_sdk_sys::NBGL_SYNC_RET_RX_APDU => {
match ledger_secure_sdk_sys::ux_sync_homeAndSettings(self.app_name.as_ptr() as *const ::core::ffi::c_char,
icon,
core::ptr::null(),
INIT_HOME_PAGE as u8,
&setting_contents as *const nbgl_genericContents_t,
&info_list as *const nbgl_contentInfoList_t,
core::ptr::null()) {
ledger_secure_sdk_sys::UX_SYNC_RET_APDU_RECEIVED => {
if let Some(comm) = COMM_REF.as_mut() {
if let Some(value) = comm.check_event() {
return value;
Expand All @@ -182,19 +197,18 @@ impl<'a> NbglHome<'a> {
impl<'a> NbglReview<'a> {
pub fn new() -> NbglReview<'a> {
NbglReview {
confirmed_str: "TRANSACTION\nSIGNED\0",
rejected_str: "Transaction\nRejected\0",
intro_str: "Please Review\nTransaction\0",
long_press_button_str: "Hold to sign\0",
long_press_str: "Sign transaction\nto send CRAB\0",
title: "Please review\ntransaction\0",
subtitle: "\0",
finish_title: "Sign transaction\0",
glyph: None,
}
}

pub fn status_strings(self, confirmed: &'a str, rejected: &'a str) -> NbglReview<'a> {
pub fn titles(self, title: &'a str, subtitle: &'a str, finish_title: &'a str) -> NbglReview<'a> {
NbglReview {
confirmed_str: confirmed,
rejected_str: rejected,
title,
subtitle,
finish_title,
..self
}
}
Expand Down Expand Up @@ -225,26 +239,25 @@ impl<'a> NbglReview<'a> {
core::ptr::null() as *const nbgl_icon_details_t
};

let sync_ret = ledger_secure_sdk_sys::sync_nbgl_useCaseTransactionReview(
let sync_ret = ledger_secure_sdk_sys::ux_sync_review(
TYPE_TRANSACTION,
&tag_value_list as *const nbgl_layoutTagValueList_t,
icon,
self.intro_str.as_ptr() as *const ::core::ffi::c_char,
core::ptr::null(),
self.long_press_str.as_ptr() as *const ::core::ffi::c_char,
self.title.as_ptr() as *const ::core::ffi::c_char,
self.subtitle.as_ptr() as *const ::core::ffi::c_char,
self.finish_title.as_ptr() as *const ::core::ffi::c_char,
);

match sync_ret {
ledger_secure_sdk_sys::NBGL_SYNC_RET_SUCCESS => {
ledger_secure_sdk_sys::sync_nbgl_useCaseStatus(
self.confirmed_str.as_ptr() as *const ::core::ffi::c_char,
true,
ledger_secure_sdk_sys::UX_SYNC_RET_APPROVED => {
ledger_secure_sdk_sys::ux_sync_reviewStatus(
STATUS_TYPE_TRANSACTION_SIGNED
);
return true;
}
_ => {
ledger_secure_sdk_sys::sync_nbgl_useCaseStatus(
self.rejected_str.as_ptr() as *const ::core::ffi::c_char,
false,
ledger_secure_sdk_sys::ux_sync_reviewStatus(
STATUS_TYPE_TRANSACTION_REJECTED
);
return false;
}
Expand Down Expand Up @@ -285,18 +298,19 @@ impl<'a> NbglAddressConfirm<'a> {
core::ptr::null() as *const nbgl_icon_details_t
};

let sync_ret = sync_nbgl_useCaseAddressReview(
let sync_ret = ux_sync_addressReview(
address.as_ptr() as *const ::core::ffi::c_char,
core::ptr::null(),
icon,
self.verify_str.as_ptr() as *const ::core::ffi::c_char,
core::ptr::null(),
);

match sync_ret {
ledger_secure_sdk_sys::NBGL_SYNC_RET_SUCCESS => {
ledger_secure_sdk_sys::UX_SYNC_RET_APPROVED => {
return true;
}
ledger_secure_sdk_sys::NBGL_SYNC_RET_REJECTED => {
ledger_secure_sdk_sys::UX_SYNC_RET_REJECTED => {
return false;
}
_ => {
Expand Down
16 changes: 10 additions & 6 deletions ledger_secure_sdk_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ fn clone_sdk(device: &Device) -> PathBuf {
),
Device::Stax => (
Path::new("https://github.com/LedgerHQ/ledger-secure-sdk"),
"API_LEVEL_14",
// TODO : Replace with API_LEVEL_15 when feature is cherry-picked.
"xch/test-nbgl-sync-api-level-15",
),
};

Expand Down Expand Up @@ -519,7 +520,8 @@ impl SDKBuilder {
Device::Stax => {
bindings = bindings.clang_args([
format!("-I{bsdk}/lib_nbgl/include/").as_str(),
format!("-I{bsdk}/lib_ux_stax/").as_str(),
format!("-I{bsdk}/lib_ux_sync/include/").as_str(),
format!("-I{bsdk}/lib_ux_nbgl/").as_str(),
"-I./src/c/",
]);
bindings = bindings
Expand All @@ -531,11 +533,11 @@ impl SDKBuilder {
)
.header(
self.bolos_sdk
.join("lib_nbgl/include/nbgl_sync.h")
.join("lib_ux_sync/include/ux_sync.h")
.to_str()
.unwrap(),
)
.header(self.bolos_sdk.join("lib_ux_stax/ux.h").to_str().unwrap())
.header(self.bolos_sdk.join("lib_ux_nbgl/ux_nbgl.h").to_str().unwrap())
}
_ => (),
}
Expand Down Expand Up @@ -689,12 +691,14 @@ fn configure_lib_nbgl(command: &mut cc::Build, bolos_sdk: &Path) {
.define("HAVE_PIEZO_SOUND", None)
.define("HAVE_SPRINTF", None)
.include(bolos_sdk.join("lib_nbgl/include/"))
.include(bolos_sdk.join("lib_ux_stax/"))
.include(bolos_sdk.join("lib_ux_sync/include/"))
.include(bolos_sdk.join("lib_ux_nbgl/"))
.include(bolos_sdk.join("qrcode/include/"))
.include("./src/c/")
.include(bolos_sdk.join("lib_bagl/include/"))
.file("./src/c/glyphs.c")
.file(bolos_sdk.join("lib_ux_stax/ux.c"))
.file(bolos_sdk.join("lib_ux_nbgl/ux.c"))
.file(bolos_sdk.join("lib_ux_sync/src/ux_sync.c"))
.file(bolos_sdk.join("lib_bagl/src/bagl_fonts.c"))
.file(bolos_sdk.join("src/os_printf.c"))
.files(
Expand Down
1 change: 1 addition & 0 deletions ledger_secure_sdk_sys/sdk_stax.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define SCREEN_SIZE_WALLET
#define HAVE_NBGL
#define NBGL_USE_CASE
#define HAVE_SE_TOUCH
Expand Down

0 comments on commit fa7ca9c

Please sign in to comment.