From cd8c762c55b64b5da222bf1881bf0e4dac1bdc64 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 Apr 2022 15:19:56 +0800 Subject: [PATCH] patch up QR code PR, change method signature as discussed now any modal notification can also render as a QR code --- Cargo.lock | 16 +++++++ services/gam/src/modal.rs | 3 -- services/gam/src/modal/notification.rs | 6 +-- services/modals/src/lib.rs | 18 +------- services/modals/src/tests.rs | 6 +-- services/net/src/connection_manager.rs | 2 +- services/pddb/src/backend/hw.rs | 6 +-- services/pddb/src/main.rs | 6 +-- services/root-keys/src/main.rs | 58 +++++++++++++------------- services/status/src/main.rs | 4 +- services/status/src/time.rs | 6 +-- 11 files changed, 65 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc7c2ea2d..a25dc0534 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -395,6 +395,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "checked_int_cast" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17cc5e6b5ab06331c33589842070416baa137e8b0eb912b008cfd4a78ada7919" + [[package]] name = "chrono" version = "0.4.19" @@ -1059,6 +1065,7 @@ dependencies = [ "log-server", "num-derive", "num-traits", + "qrcode", "rkyv", "susres", "ticktimer-server", @@ -1944,6 +1951,15 @@ dependencies = [ "syn", ] +[[package]] +name = "qrcode" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16d2f1455f3630c6e5107b4f2b94e74d76dea80736de0981fd27644216cff57f" +dependencies = [ + "checked_int_cast", +] + [[package]] name = "quick-error" version = "1.2.3" diff --git a/services/gam/src/modal.rs b/services/gam/src/modal.rs index bed5da424..604b78cdd 100644 --- a/services/gam/src/modal.rs +++ b/services/gam/src/modal.rs @@ -14,8 +14,6 @@ mod progressbar; pub use progressbar::*; mod consoleinput; pub use consoleinput::*; -mod qrcode; -pub use self::qrcode::*; use enum_dispatch::enum_dispatch; @@ -39,7 +37,6 @@ pub enum ActionType { CheckBoxes, Slider, Notification, - Qrcode, ConsoleInput } diff --git a/services/gam/src/modal/notification.rs b/services/gam/src/modal/notification.rs index 384ad1e37..00f6ab563 100644 --- a/services/gam/src/modal/notification.rs +++ b/services/gam/src/modal/notification.rs @@ -55,7 +55,7 @@ impl Notification { None => { self.qrcode = Vec::new(); self.qrwidth = 0; - }, + }, } } fn draw_text(&self, at_height: i16, modal: &Modal) { @@ -105,7 +105,7 @@ impl Notification { let mod_size_px: i16 = canvas_width / modules; let qrcode_width_px = qrcode_modules * mod_size_px; let quiet_px: i16 = (canvas_width - qrcode_width_px) / 2; - + // Iterate thru qrcode and draw each module as a rectangle (square) let black = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); let top = at_height + 4 * modal.margin + quiet_px; @@ -114,7 +114,7 @@ impl Notification { let mut bot_right; let mut x: i16; let mut y: i16; - let mut j: i16; + let mut j: i16; for (i, module) in self.qrcode.iter().enumerate() { j = i.try_into().unwrap(); x = left + (j % qrcode_modules) * mod_size_px; diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 87ff14d42..f10b434cf 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -80,32 +80,18 @@ impl Modals { } /// this blocks until the notification has been acknowledged. - pub fn show_notification(&self, notification: &str) -> Result<(), xous::Error> { + pub fn show_notification(&self, notification: &str, as_qrcode: bool) -> Result<(), xous::Error> { self.lock(); let spec = ManagedNotification { token: self.token, message: xous_ipc::String::from_str(notification), - as_qrcode: false, + as_qrcode, }; let buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; buf.lend(self.conn, Opcode::Notification.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; Ok(()) } - /// this blocks until the qrcode has been acknowledged. - pub fn show_qrcode(&self, text: &str) -> Result<(), xous::Error> { - self.lock(); - let spec = ManagedNotification { - token: self.token, - message: xous_ipc::String::from_str(text), - as_qrcode: true, - }; - let buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; - buf.lend(self.conn, Opcode::Notification.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; - self.unlock(); - Ok(()) - } - pub fn start_progress(&self, title: &str, start: u32, end: u32, current: u32) -> Result<(), xous::Error> { self.lock(); let spec = ManagedProgress { diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index 37d57c4f4..301df9905 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -60,7 +60,7 @@ pub(crate) fn spawn_test() { // 3. test notificatons log::info!("testing notification"); - modals.show_notification("This is a test!").expect("notification failed"); + modals.show_notification("This is a test!", false).expect("notification failed"); log::info!("notification test done"); } }); @@ -93,12 +93,12 @@ pub(crate) fn spawn_test() { // 3. test notificatons log::info!("testing notification"); - modals.show_notification("这是一个测验!").expect("notification failed"); + modals.show_notification("这是一个测验!", false).expect("notification failed"); log::info!("notification test done"); // 4. test qrcode log::info!("testing qrcode"); - modals.show_qrcode("https://github.com/betrusted-io/xous-core").expect("qrcode failed"); + modals.show_notification("https://github.com/betrusted-io/xous-core", true).expect("qrcode failed"); log::info!("qrcode test done"); } }); diff --git a/services/net/src/connection_manager.rs b/services/net/src/connection_manager.rs index a72932f78..bfd5601c6 100644 --- a/services/net/src/connection_manager.rs +++ b/services/net/src/connection_manager.rs @@ -79,7 +79,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc ! { for basis in bases.iter() { note.push_str(basis); } - modals.show_notification(¬e).expect("couldn't show basis list"); + modals.show_notification(¬e, false).expect("couldn't show basis list"); }, #[cfg(not(any(target_os = "none", target_os = "xous")))] Some(Opcode::DangerousDebug) => { @@ -1281,7 +1281,7 @@ fn try_mount_or_format(modals: &modals::Modals, pddb_os: &mut PddbOs, basis_cach log::error!("Despite formatting, no PDDB was found!"); let mut err = String::from(t!("pddb.internalerror", xous::LANG)); err.push_str(" #1"); // punt and leave an error code, because this "should" be rare - modals.show_notification(err.as_str()).expect("notification failed"); + modals.show_notification(err.as_str(), false).expect("notification failed"); false } } else { @@ -1306,7 +1306,7 @@ fn try_mount_or_format(modals: &modals::Modals, pddb_os: &mut PddbOs, basis_cach log::error!("Despite formatting, no PDDB was found!"); let mut err = String::from(t!("pddb.internalerror", xous::LANG)); err.push_str(" #1"); // punt and leave an error code, because this "should" be rare - modals.show_notification(err.as_str()).expect("notification failed"); + modals.show_notification(err.as_str(), false).expect("notification failed"); false } } diff --git a/services/root-keys/src/main.rs b/services/root-keys/src/main.rs index a4a40bfdd..d78f8d5c6 100644 --- a/services/root-keys/src/main.rs +++ b/services/root-keys/src/main.rs @@ -463,7 +463,7 @@ fn xmain() -> ! { // - pepper if keys.is_initialized() { - modals.show_notification(t!("rootkeys.already_init", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.already_init", xous::LANG), false).expect("modals error"); #[cfg(feature="tts")] tts.tts_blocking(t!("rootkeys.already_init", xous::LANG)).unwrap(); keys.set_ux_password_type(None); @@ -552,16 +552,16 @@ fn xmain() -> ! { ).expect("couldn't initiate dialog box"); } Err(RootkeyResult::AlignmentError) => { - modals.show_notification(t!("rootkeys.init.fail_alignment", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_alignment", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::KeyError) => { - modals.show_notification(t!("rootkeys.init.fail_key", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_key", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::IntegrityError) => { - modals.show_notification(t!("rootkeys.init.fail_verify", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_verify", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::FlashError) => { - modals.show_notification(t!("rootkeys.init.fail_burn", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_burn", xous::LANG), false).expect("modals error"); } } }, @@ -576,7 +576,7 @@ fn xmain() -> ! { log::info!("Vbus is: {:.3}V", vbus); if vbus > 1.5 { // if power is plugged in, request that it be removed - modals.show_notification(t!("rootkeys.init.unplug_power", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.unplug_power", xous::LANG), false).expect("modals error"); log::info!("vbus is high, holding off on reboot"); send_message(main_cid, xous::Message::new_scalar(Opcode::UxTryReboot.to_usize().unwrap(), 0, 0, 0, 0) @@ -629,7 +629,7 @@ fn xmain() -> ! { SignatureResult::DevKeyOk => t!("rootkeys.gwup.viewinfo_dk", xous::LANG), _ => { modals.dynamic_notification_close().expect("modals error"); - modals.show_notification(t!("rootkeys.gwup.no_update_found", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.gwup.no_update_found", xous::LANG), false).expect("modals error"); continue; } }; @@ -659,18 +659,18 @@ fn xmain() -> ! { match modals.get_radiobutton(prompt) { Ok(response) => { if response == t!("rootkeys.gwup.short", xous::LANG) { - modals.show_notification(info.as_str()).expect("modals error"); + modals.show_notification(info.as_str(), false).expect("modals error"); } else if response == t!("rootkeys.gwup.details", xous::LANG) { - modals.show_notification(info.as_str()).expect("modals error"); + modals.show_notification(info.as_str(), false).expect("modals error"); let gw_info = keys.fetch_gw_metadata(GatewareRegion::Staging); // truncate the message to better fit in the rendering box let info_len = if gw_info.log_len > 256 { 256 } else {gw_info.log_len}; let info = format!("{}", str::from_utf8(&gw_info.log_str[..info_len as usize]).unwrap()); - modals.show_notification(info.as_str()).expect("modals error"); + modals.show_notification(info.as_str(), false).expect("modals error"); // truncate the message to better fit in the rendering box let status_len = if gw_info.status_len > 256 { 256 } else {gw_info.status_len}; let info = format!("{}", str::from_utf8(&gw_info.status_str[..status_len as usize]).unwrap()); - modals.show_notification(info.as_str()).expect("modals error"); + modals.show_notification(info.as_str(), false).expect("modals error"); } else { skip_confirmation = true; } @@ -760,21 +760,21 @@ fn xmain() -> ! { match result { Ok(_) => { - modals.show_notification(t!("rootkeys.gwup.finished", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.gwup.finished", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::AlignmentError) => { - modals.show_notification(t!("rootkeys.init.fail_alignment", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_alignment", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::KeyError) => { // probably a bad password, purge it, so the user can try again keys.purge_password(PasswordType::Update); - modals.show_notification(t!("rootkeys.init.fail_key", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_key", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::IntegrityError) => { - modals.show_notification(t!("rootkeys.init.fail_verify", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_verify", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::FlashError) => { - modals.show_notification(t!("rootkeys.init.fail_burn", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_burn", xous::LANG), false).expect("modals error"); } } } @@ -834,21 +834,21 @@ fn xmain() -> ! { match result { Ok(_) => { - modals.show_notification(t!("rootkeys.signxous.finished", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.signxous.finished", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::AlignmentError) => { - modals.show_notification(t!("rootkeys.init.fail_alignment", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_alignment", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::KeyError) => { // probably a bad password, purge it, so the user can try again keys.purge_password(PasswordType::Update); - modals.show_notification(t!("rootkeys.init.fail_key", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_key", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::IntegrityError) => { - modals.show_notification(t!("rootkeys.init.fail_verify", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_verify", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::FlashError) => { - modals.show_notification(t!("rootkeys.init.fail_burn", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_burn", xous::LANG), false).expect("modals error"); } } } @@ -885,7 +885,7 @@ fn xmain() -> ! { // more keys with more passwords, this policy may need to become markedly more complicated! // otherwise, an invalid password request - modals.show_notification(t!("rootkeys.bad_password_request", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.bad_password_request", xous::LANG), false).expect("modals error"); xous::return_scalar(msg.sender, 0).unwrap(); } @@ -978,7 +978,7 @@ fn xmain() -> ! { } Some(Opcode::BbramProvision) => { - modals.show_notification(t!("rootkeys.bbram.confirm", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.bbram.confirm", xous::LANG), false).expect("modals error"); let console_input = gam::modal::ConsoleInput::new( main_cid, Opcode::UxBbramCheckReturn.to_u32().unwrap() @@ -1016,7 +1016,7 @@ fn xmain() -> ! { rootkeys_modal.activate(); } } else { - modals.show_notification(t!("rootkeys.bbram.no_helper", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.bbram.no_helper", xous::LANG), false).expect("modals error"); continue; } } @@ -1039,21 +1039,21 @@ fn xmain() -> ! { match result { Ok(_) => { - modals.show_notification(t!("rootkeys.bbram.finished", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.bbram.finished", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::AlignmentError) => { - modals.show_notification(t!("rootkeys.init.fail_alignment", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_alignment", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::KeyError) => { // probably a bad password, purge it, so the user can try again keys.purge_password(PasswordType::Update); - modals.show_notification(t!("rootkeys.init.fail_key", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_key", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::IntegrityError) => { - modals.show_notification(t!("rootkeys.init.fail_verify", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_verify", xous::LANG), false).expect("modals error"); } Err(RootkeyResult::FlashError) => { - modals.show_notification(t!("rootkeys.init.fail_burn", xous::LANG)).expect("modals error"); + modals.show_notification(t!("rootkeys.init.fail_burn", xous::LANG), false).expect("modals error"); } } } diff --git a/services/status/src/main.rs b/services/status/src/main.rs index d6da4f6ce..a1771471c 100644 --- a/services/status/src/main.rs +++ b/services/status/src/main.rs @@ -639,14 +639,14 @@ fn xmain() -> ! { }), Some(StatusOpcode::TrySuspend) => { if ((llio.adc_vbus().unwrap() as f64) * 0.005033) > 1.5 { - modals.show_notification(t!("mainmenu.cant_sleep", xous::LANG)).expect("couldn't notify that power is plugged in"); + modals.show_notification(t!("mainmenu.cant_sleep", xous::LANG), false).expect("couldn't notify that power is plugged in"); } else { susres.initiate_suspend().expect("couldn't initiate suspend op"); } }, Some(StatusOpcode::BatteryDisconnect) => { if ((llio.adc_vbus().unwrap() as f64) * 0.005033) > 1.5 { - modals.show_notification(t!("mainmenu.cant_sleep", xous::LANG)).expect("couldn't notify that power is plugged in"); + modals.show_notification(t!("mainmenu.cant_sleep", xous::LANG), false).expect("couldn't notify that power is plugged in"); } else { gam.shipmode_blank_request().ok(); ticktimer.sleep_ms(500).unwrap(); diff --git a/services/status/src/time.rs b/services/status/src/time.rs index 1a2d03aaf..e535d830f 100644 --- a/services/status/src/time.rs +++ b/services/status/src/time.rs @@ -493,7 +493,7 @@ pub fn start_time_ux(sid: xous::SID) { match FromPrimitive::from_usize(msg.body.id()) { Some(TimeUxOp::SetTime) => xous::msg_scalar_unpack!(msg, _, _, _, _, { if !pddb_poller.is_mounted_nonblocking() { - modals.show_notification(t!("stats.please_mount", xous::LANG)).expect("couldn't show notification"); + modals.show_notification(t!("stats.please_mount", xous::LANG), false).expect("couldn't show notification"); continue; } let mut tz_set_handle = pddb::Pddb::new(); @@ -573,7 +573,7 @@ pub fn start_time_ux(sid: xous::SID) { } Err(err) => { log::info!("Err: {:?}", err); - modals.show_notification(t!("rtc.ntp_fail", xous::LANG)).expect("couldn't show NTP error"); + modals.show_notification(t!("rtc.ntp_fail", xous::LANG), false).expect("couldn't show NTP error"); }, } } @@ -641,7 +641,7 @@ pub fn start_time_ux(sid: xous::SID) { }), Some(TimeUxOp::SetTimeZone) => xous::msg_scalar_unpack!(msg, _, _, _, _, { if !pddb_poller.is_mounted_nonblocking() { - modals.show_notification(t!("stats.please_mount", xous::LANG)).expect("couldn't show notification"); + modals.show_notification(t!("stats.please_mount", xous::LANG), false).expect("couldn't show notification"); continue; } let tz = modals.get_text(