patch up QR code PR, change method signature as discussed

now any modal notification can also render as a QR code
This commit is contained in:
bunnie 2022-04-08 15:19:56 +08:00
parent 679fde33a7
commit cd8c762c55
11 changed files with 65 additions and 66 deletions

16
Cargo.lock generated
View file

@ -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"

View file

@ -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
}

View file

@ -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;

View file

@ -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 {

View file

@ -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");
}
});

View file

@ -79,7 +79,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc<AtomicU3
log::warn!("EC firmware is too old to interoperate with the connec tion manager.");
let mut note = String::from(t!("net.ec_rev_old", xous::LANG));
note.push_str(&format!("\n\n{}{}.{}.{}+{}", t!("net.ec_current_rev", xous::LANG), maj, min, rev, commits));
modals.show_notification(&note).unwrap();
modals.show_notification(&note, false).unwrap();
}
let run = Arc::new(AtomicBool::new(rev_ok));

View file

@ -523,7 +523,7 @@ impl PddbOs {
self.clear_password(); // clear the bad password entry
let xns = xous_names::XousNames::new().unwrap();
let modals = modals::Modals::new(&xns).expect("can't connect to Modals server");
modals.show_notification(t!("pddb.badpass_infallible", xous::LANG)).expect("notification failed");
modals.show_notification(t!("pddb.badpass_infallible", xous::LANG), false).expect("notification failed");
}
}
@ -1367,7 +1367,7 @@ impl PddbOs {
self.rootkeys.decrypt_block(GenericArray::from_mut_slice(&mut checkblock_a));
#[cfg(any(target_os = "none", target_os = "xous"))] // skip this dialog in hosted mode
modals.show_notification(t!("pddb.checkpass", xous::LANG)).expect("notification failed");
modals.show_notification(t!("pddb.checkpass", xous::LANG), false).expect("notification failed");
self.clear_password();
let mut checkblock_b = [0u8; BLOCK_SIZE];
@ -1376,7 +1376,7 @@ impl PddbOs {
if checkblock_a == checkblock_b {
success = true;
} else {
modals.show_notification(t!("pddb.checkpass_fail", xous::LANG)).expect("notification failed");
modals.show_notification(t!("pddb.checkpass_fail", xous::LANG), false).expect("notification failed");
self.clear_password();
}
}

View file

@ -1132,7 +1132,7 @@ fn xmain() -> ! {
for basis in bases.iter() {
note.push_str(basis);
}
modals.show_notification(&note).expect("couldn't show basis list");
modals.show_notification(&note, 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
}
}

View file

@ -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");
}
}
}

View file

@ -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();

View file

@ -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(