From 1ad7c43f81bdc60ccee8f53e9a2ad89ae8249374 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 7 Apr 2022 14:46:44 +0800 Subject: [PATCH] refactor pddb ux thread to use deferred response messaging --- services/pddb/src/ux.rs | 207 +++++++++++++++------------------------- 1 file changed, 75 insertions(+), 132 deletions(-) diff --git a/services/pddb/src/ux.rs b/services/pddb/src/ux.rs index 2dfaed2c0..7ab29987a 100644 --- a/services/pddb/src/ux.rs +++ b/services/pddb/src/ux.rs @@ -1,19 +1,13 @@ use xous::{SID, CID}; -use xous::{msg_scalar_unpack, send_message}; +use xous::msg_scalar_unpack; use xous_ipc::Buffer; -use std::sync::{Arc, Mutex}; -use std::thread; -use core::sync::atomic::{AtomicBool, Ordering}; - use num_traits::*; use gam::modal::*; use locales::t; -use std::fmt::Write; - use crate::BasisRequestPassword; /* Conclusions: @@ -54,12 +48,6 @@ GAM Modal Object -> redraw -> gam requests pub(crate) enum PwManagerOpcode { // blocking call to get a password RequestPassword, - Quit, -} - -#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] -pub(crate) enum PwRendererOpcode { - RaisePwModal, PwReturn, ModalRedraw, ModalKeypress, @@ -73,144 +61,99 @@ pub(crate) fn password_ux_manager( // the SID we're to use for our loop handler, for getting requests ux_sid: SID) { - let tt = ticktimer_server::Ticktimer::new().unwrap(); - - let renderer_sid = xous::create_server().expect("couldn't create a server for the password UX renderer"); - let renderer_cid = xous::connect(renderer_sid).expect("couldn't connect to the password UX renderer"); - let plaintext_pw = Arc::new(Mutex::new(xous_ipc::String::<{crate::api::PASSWORD_LEN}>::new())); - - let renderer_active = Arc::new(AtomicBool::new(false)); + let ux_cid = xous::connect(ux_sid).unwrap(); // create a thread that just handles the redrawing requests - let redraw_handle = thread::spawn({ - let renderer_active = Arc::clone(&renderer_active); - let plaintext_pw = Arc::clone(&plaintext_pw); - move || { - // build the core data structure here - let password_action = TextEntry { - is_password: true, - visibility: TextEntryVisibility::LastChars, - action_conn: renderer_cid, - action_opcode: PwRendererOpcode::PwReturn.to_u32().unwrap(), - action_payload: TextEntryPayload::new(), - validator: None, - }; + // build the core data structure here + let password_action = TextEntry { + is_password: true, + visibility: TextEntryVisibility::LastChars, + action_conn: ux_cid, + action_opcode: PwManagerOpcode::PwReturn.to_u32().unwrap(), + action_payload: TextEntryPayload::new(), + validator: None, + }; - let mut pddb_modal = - Modal::new( - gam::PDDB_MODAL_NAME, - ActionType::TextEntry(password_action), - Some(t!("pddb.password", xous::LANG)), - None, - GlyphStyle::Regular, - 8 - ); - pddb_modal.spawn_helper(renderer_sid, pddb_modal.sid, - PwRendererOpcode::ModalRedraw.to_u32().unwrap(), - PwRendererOpcode::ModalKeypress.to_u32().unwrap(), - PwRendererOpcode::ModalDrop.to_u32().unwrap(), - ); + let mut pddb_modal = + Modal::new( + gam::PDDB_MODAL_NAME, + ActionType::TextEntry(password_action), + Some(t!("pddb.password", xous::LANG)), + None, + GlyphStyle::Regular, + 8 + ); + pddb_modal.spawn_helper(ux_sid, pddb_modal.sid, + PwManagerOpcode::ModalRedraw.to_u32().unwrap(), + PwManagerOpcode::ModalKeypress.to_u32().unwrap(), + PwManagerOpcode::ModalDrop.to_u32().unwrap(), + ); - loop { - let msg = xous::receive_message(renderer_sid).unwrap(); - log::debug!("message: {:?}", msg); - match FromPrimitive::from_usize(msg.body.id()) { - Some(PwRendererOpcode::RaisePwModal) => { - let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; - let db_name = buffer.to_original::, _>().unwrap(); - pddb_modal.modify( - Some(ActionType::TextEntry(password_action)), - Some(t!("pddb.password", xous::LANG)), false, - Some(db_name.as_str().unwrap()), false, None - ); - pddb_modal.activate(); - } - Some(PwRendererOpcode::PwReturn) => { - if renderer_active.load(Ordering::SeqCst) { - let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; - let mut pw = buf.to_original::().unwrap(); - - plaintext_pw.lock().unwrap().clear(); - write!(plaintext_pw.lock().unwrap(), "{}", pw.as_str()).expect("couldn't transfer password to local buffer"); - - pw.volatile_clear(); // ensure the data is destroyed after sending to the keys enclave - buf.volatile_clear(); - } else { - log::warn!("Fat finger event received from Ux, ignoring"); - } - - // this resumes the waiting Ux Manager thread - renderer_active.store(false, Ordering::SeqCst); - }, - Some(PwRendererOpcode::ModalRedraw) => { - pddb_modal.redraw(); - }, - Some(PwRendererOpcode::ModalKeypress) => msg_scalar_unpack!(msg, k1, k2, k3, k4, { - let keys = [ - core::char::from_u32(k1 as u32).unwrap_or('\u{0000}'), - core::char::from_u32(k2 as u32).unwrap_or('\u{0000}'), - core::char::from_u32(k3 as u32).unwrap_or('\u{0000}'), - core::char::from_u32(k4 as u32).unwrap_or('\u{0000}'), - ]; - pddb_modal.key_event(keys); - }), - Some(PwRendererOpcode::ModalDrop) => { // this guy should never quit, it's a core OS service - panic!("Password modal for PDDB quit unexpectedly"); - }, - Some(PwRendererOpcode::Quit) => { - log::warn!("received quit on PDDB password UX renderer loop"); - xous::return_scalar(msg.sender, 0).unwrap(); - break; - }, - None => { - log::error!("Couldn't convert opcode: {:?}", msg); - } - } - } - xous::destroy_server(renderer_sid).unwrap(); - } - }); + let mut dr: Option = None; // the main loop merely wraps a blocking wrapper around the renderer thread loop { - let mut msg = xous::receive_message(ux_sid).unwrap(); + let msg = xous::receive_message(ux_sid).unwrap(); log::debug!("message: {:?}", msg); match FromPrimitive::from_usize(msg.body.id()) { Some(PwManagerOpcode::RequestPassword) => { - let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut request = buffer.to_original::().unwrap(); - let fwd_buf = Buffer::into_buf(request.db_name).unwrap(); - renderer_active.store(true, Ordering::SeqCst); - fwd_buf.lend(renderer_cid, PwRendererOpcode::RaisePwModal.to_u32().unwrap()).expect("couldn't request renderer to raise the password modal"); - // this returns almost immediately, because the Ux Manager thread can't block (it has to manage rendering), but the password hasn't come back yet. - // so...."busy wait" by polling once every 100ms until the Ux is finished with its task. - // I can't seem to figure out a way to do this using a solely asynchronous method. I really want a spsc queue, but these don't exist in xous stdlib yet. - // apparently the thing I'm looking for is a `condvar`, which is coming Real Soon Now. - while renderer_active.load(Ordering::SeqCst) { - tt.sleep_ms(100).unwrap(); - } - // when this unblocks, the password request cycle is done. - let retpw = xous_ipc::String::<{crate::api::PASSWORD_LEN}>::from_str(plaintext_pw.lock().unwrap().as_str().unwrap()); - request.plaintext_pw = Some(retpw); + let db_name = { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let request = buffer.to_original::().unwrap(); + request.db_name + }; + pddb_modal.modify( + Some(ActionType::TextEntry(password_action)), + Some(t!("pddb.password", xous::LANG)), false, + Some(db_name.as_str().unwrap()), false, None + ); + pddb_modal.activate(); + dr = Some(msg); - // return the password to the caller - buffer.replace(request).unwrap(); + }, + Some(PwManagerOpcode::PwReturn) => { + let mut buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let mut pw = buf.to_original::().unwrap(); + + if let Some(mut response) = dr.take() { + let mut buffer = unsafe { Buffer::from_memory_message_mut(response.body.memory_message_mut().unwrap()) }; + let mut request = buffer.to_original::().unwrap(); + request.plaintext_pw = Some(xous_ipc::String::from_str(pw.as_str())); + // return the password to the caller + buffer.replace(request).unwrap(); + // response goes out of scope here and calls Drop which returns the message + } else { + log::error!("Password return received, but no deferred response on record"); + } + + pw.volatile_clear(); // ensure the data is destroyed after sending to the keys enclave + buf.volatile_clear(); + }, + Some(PwManagerOpcode::ModalRedraw) => { + pddb_modal.redraw(); + }, + Some(PwManagerOpcode::ModalKeypress) => msg_scalar_unpack!(msg, k1, k2, k3, k4, { + let keys = [ + core::char::from_u32(k1 as u32).unwrap_or('\u{0000}'), + core::char::from_u32(k2 as u32).unwrap_or('\u{0000}'), + core::char::from_u32(k3 as u32).unwrap_or('\u{0000}'), + core::char::from_u32(k4 as u32).unwrap_or('\u{0000}'), + ]; + pddb_modal.key_event(keys); + }), + Some(PwManagerOpcode::ModalDrop) => { // this guy should never quit, it's a core OS service + panic!("Password modal for PDDB quit unexpectedly"); }, Some(PwManagerOpcode::Quit) => { - log::warn!("PDDB password handler exiting."); - send_message( - renderer_cid, - xous::Message::new_blocking_scalar(PwRendererOpcode::Quit.to_usize().unwrap(), - 0, 0, 0, 0)).unwrap(); + log::warn!("received quit on PDDB password UX renderer loop"); xous::return_scalar(msg.sender, 0).unwrap(); - break + break; }, + None => { log::error!("couldn't convert opcode"); } } } - redraw_handle.join().expect("redraw thread didn't quit as expected"); - unsafe{xous::disconnect(renderer_cid).unwrap()}; // can't remember if this is...necessary? it'll throw an error if it's not. xous::destroy_server(ux_sid).unwrap(); }