|
| 1 | +// The purpose of this file is to address the issue of the ledger rust sdk not supporting |
| 2 | +// navigation to the settings page. |
| 3 | +// To maintain consistency with the ledger rust sdk code, we have ignored clipply warnings here. |
| 4 | +#![allow(clippy::all)] |
| 5 | + |
| 6 | +use crate::settings::{SETTINGS_DATA, SETTINGS_SIZE}; |
| 7 | +use const_zero::const_zero; |
| 8 | + |
| 9 | +extern crate alloc; |
| 10 | +use alloc::ffi::CString; |
| 11 | +use alloc::vec::Vec; |
| 12 | +use core::ffi::*; |
| 13 | +use core::mem::transmute; |
| 14 | +use include_gif::include_gif; |
| 15 | +use ledger_device_sdk::io::{ApduHeader, Comm, Event, Reply}; |
| 16 | +use ledger_device_sdk::nbgl::{NbglGlyph, TuneIndex}; |
| 17 | +use ledger_device_sdk::nvm::{AtomicStorage, SingleStorage}; |
| 18 | +use ledger_secure_sdk_sys::*; |
| 19 | + |
| 20 | +static mut NVM_REF: Option<&mut AtomicStorage<[u8; SETTINGS_SIZE]>> = None; |
| 21 | +static mut SWITCH_ARRAY: [nbgl_contentSwitch_t; SETTINGS_SIZE] = |
| 22 | + [unsafe { const_zero!(nbgl_contentSwitch_t) }; SETTINGS_SIZE]; |
| 23 | + |
| 24 | +/// Information fields name to display in the dedicated |
| 25 | +/// page of the home screen. |
| 26 | +const INFO_FIELDS: [*const c_char; 2] = [ |
| 27 | + "Version\0".as_ptr() as *const c_char, |
| 28 | + "Developer\0".as_ptr() as *const c_char, |
| 29 | +]; |
| 30 | + |
| 31 | +pub fn nbgl_display<'a, T: TryFrom<ApduHeader>>( |
| 32 | + comm: &mut Comm, |
| 33 | + settings_strings: &[[&'a str; 2]], |
| 34 | + page: u8, |
| 35 | +) -> Event<T> |
| 36 | +where |
| 37 | + Reply: From<<T as TryFrom<ApduHeader>>::Error>, |
| 38 | +{ |
| 39 | + let mut info_contents: Vec<CString> = Vec::new(); |
| 40 | + info_contents.push(CString::new("Alephium").unwrap()); |
| 41 | + info_contents.push(CString::new(env!("CARGO_PKG_VERSION")).unwrap()); |
| 42 | + info_contents.push(CString::new(env!("CARGO_PKG_AUTHORS")).unwrap()); |
| 43 | + |
| 44 | + unsafe { |
| 45 | + NVM_REF = Some(transmute(SETTINGS_DATA.get_mut())); |
| 46 | + } |
| 47 | + |
| 48 | + let nb_settings = settings_strings.len() as u8; |
| 49 | + let setting_contents: Vec<[CString; 2]> = settings_strings |
| 50 | + .iter() |
| 51 | + .map(|s| [CString::new(s[0]).unwrap(), CString::new(s[1]).unwrap()]) |
| 52 | + .collect(); |
| 53 | + |
| 54 | + const APP_ICON: NbglGlyph = NbglGlyph::from_include(include_gif!("alph_64x64.gif", NBGL)); |
| 55 | + unsafe { |
| 56 | + loop { |
| 57 | + let info_contents: Vec<*const c_char> = |
| 58 | + info_contents.iter().map(|s| s.as_ptr()).collect::<Vec<_>>(); |
| 59 | + |
| 60 | + let info_list: nbgl_contentInfoList_t = nbgl_contentInfoList_t { |
| 61 | + infoTypes: INFO_FIELDS.as_ptr() as *const *const c_char, |
| 62 | + infoContents: info_contents[1..].as_ptr() as *const *const c_char, |
| 63 | + nbInfos: INFO_FIELDS.len() as u8, |
| 64 | + }; |
| 65 | + |
| 66 | + let icon: nbgl_icon_details_t = (&APP_ICON).into(); |
| 67 | + |
| 68 | + for (i, setting) in setting_contents.iter().enumerate() { |
| 69 | + SWITCH_ARRAY[i].text = setting[0].as_ptr(); |
| 70 | + SWITCH_ARRAY[i].subText = setting[1].as_ptr(); |
| 71 | + SWITCH_ARRAY[i].initState = NVM_REF.as_mut().unwrap().get_ref()[i] as nbgl_state_t; |
| 72 | + SWITCH_ARRAY[i].token = (FIRST_USER_TOKEN + i as u32) as u8; |
| 73 | + SWITCH_ARRAY[i].tuneId = TuneIndex::TapCasual as u8; |
| 74 | + } |
| 75 | + |
| 76 | + let content: nbgl_content_t = nbgl_content_t { |
| 77 | + content: nbgl_content_u { |
| 78 | + switchesList: nbgl_pageSwitchesList_s { |
| 79 | + switches: &SWITCH_ARRAY as *const nbgl_contentSwitch_t, |
| 80 | + nbSwitches: nb_settings, |
| 81 | + }, |
| 82 | + }, |
| 83 | + contentActionCallback: Some(settings_callback), |
| 84 | + type_: SWITCHES_LIST, |
| 85 | + }; |
| 86 | + |
| 87 | + let generic_contents: nbgl_genericContents_t = nbgl_genericContents_t { |
| 88 | + callbackCallNeeded: false, |
| 89 | + __bindgen_anon_1: nbgl_genericContents_t__bindgen_ty_1 { |
| 90 | + contentsList: &content as *const nbgl_content_t, |
| 91 | + }, |
| 92 | + nbContents: if nb_settings > 0 { 1 } else { 0 }, |
| 93 | + }; |
| 94 | + |
| 95 | + match ux_sync_homeAndSettings( |
| 96 | + info_contents[0], |
| 97 | + &icon as *const nbgl_icon_details_t, |
| 98 | + core::ptr::null(), |
| 99 | + page, |
| 100 | + &generic_contents as *const nbgl_genericContents_t, |
| 101 | + &info_list as *const nbgl_contentInfoList_t, |
| 102 | + core::ptr::null(), |
| 103 | + ) { |
| 104 | + UX_SYNC_RET_APDU_RECEIVED => { |
| 105 | + if let Some(event) = comm.check_event() { |
| 106 | + return event; |
| 107 | + } |
| 108 | + } |
| 109 | + _ => { |
| 110 | + panic!("Unexpected return value from ux_sync_homeAndSettings"); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/// Callback triggered by the NBGL API when a setting switch is toggled. |
| 118 | +unsafe extern "C" fn settings_callback(token: c_int, _index: u8, _page: c_int) { |
| 119 | + let idx = token - FIRST_USER_TOKEN as i32; |
| 120 | + if idx < 0 || idx >= SETTINGS_SIZE as i32 { |
| 121 | + panic!("Invalid token."); |
| 122 | + } |
| 123 | + |
| 124 | + if let Some(data) = NVM_REF.as_mut() { |
| 125 | + let setting_idx: usize = idx as usize; |
| 126 | + let mut switch_values: [u8; SETTINGS_SIZE] = data.get_ref().clone(); |
| 127 | + switch_values[setting_idx] = !switch_values[setting_idx]; |
| 128 | + data.update(&switch_values); |
| 129 | + SWITCH_ARRAY[setting_idx].initState = switch_values[setting_idx] as nbgl_state_t; |
| 130 | + } |
| 131 | +} |
0 commit comments