mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
refactor validator to not create a new single-use server
This commit is contained in:
parent
f720f3175c
commit
c97a00ed1f
3 changed files with 62 additions and 97 deletions
|
|
@ -27,10 +27,6 @@ pub struct ManagedListItem {
|
||||||
pub struct ManagedPromptWithTextResponse {
|
pub struct ManagedPromptWithTextResponse {
|
||||||
pub token: [u32; 4],
|
pub token: [u32; 4],
|
||||||
pub prompt: xous_ipc::String::<1024>,
|
pub prompt: xous_ipc::String::<1024>,
|
||||||
/// SID of a validator
|
|
||||||
pub validator: Option<[u32; 4]>,
|
|
||||||
/// the opcode to pass the validator
|
|
||||||
pub validator_op: u32,
|
|
||||||
}
|
}
|
||||||
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
|
||||||
pub struct ManagedNotification {
|
pub struct ManagedNotification {
|
||||||
|
|
@ -67,13 +63,16 @@ pub(crate) enum Opcode {
|
||||||
PromptWithFixedResponse,
|
PromptWithFixedResponse,
|
||||||
/// ask a question, get multiple responses from a list of defined items (check box)
|
/// ask a question, get multiple responses from a list of defined items (check box)
|
||||||
PromptWithMultiResponse,
|
PromptWithMultiResponse,
|
||||||
/// ask a question, get a free-form answer back
|
|
||||||
PromptWithTextResponse,
|
|
||||||
/// simple notification
|
/// simple notification
|
||||||
Notification,
|
Notification,
|
||||||
/// dynamic notification - a simple non-interactive notification that allows its text to be dynamically updated
|
/// dynamic notification - a simple non-interactive notification that allows its text to be dynamically updated
|
||||||
DynamicNotification,
|
DynamicNotification,
|
||||||
|
|
||||||
|
/// ask a question, get a free-form answer back
|
||||||
|
PromptWithTextResponse,
|
||||||
|
/// must be used by the PromptWithTextResponse caller to acknowledge correct input
|
||||||
|
TextResponseValid,
|
||||||
|
|
||||||
// these are non-blocking calls
|
// these are non-blocking calls
|
||||||
/// add an item to the radio box or check box. Note that all added items
|
/// add an item to the radio box or check box. Note that all added items
|
||||||
/// are cleared after the relevant "action" call happens (PromptWith[Fixed,Multi]Response)
|
/// are cleared after the relevant "action" call happens (PromptWith[Fixed,Multi]Response)
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,9 @@ pub mod api;
|
||||||
use api::*;
|
use api::*;
|
||||||
|
|
||||||
use gam::*;
|
use gam::*;
|
||||||
use xous::{CID, SID, send_message, Message};
|
use xous::{CID, send_message, Message};
|
||||||
use num_traits::{ToPrimitive, FromPrimitive};
|
use num_traits::*;
|
||||||
use xous_ipc::Buffer;
|
use xous_ipc::Buffer;
|
||||||
use std::thread;
|
|
||||||
use core::cell::Cell;
|
use core::cell::Cell;
|
||||||
|
|
||||||
pub struct Modals {
|
pub struct Modals {
|
||||||
|
|
@ -35,56 +34,48 @@ impl Modals {
|
||||||
maybe_validator_op: Option<u32>,
|
maybe_validator_op: Option<u32>,
|
||||||
) -> Result<TextEntryPayload, xous::Error> {
|
) -> Result<TextEntryPayload, xous::Error> {
|
||||||
self.lock();
|
self.lock();
|
||||||
let validator = if let Some(validator) = maybe_validator {
|
|
||||||
// create a one-time use server
|
let mut spec = ManagedPromptWithTextResponse {
|
||||||
let validator_server = xous::create_server().unwrap();
|
|
||||||
thread::spawn({
|
|
||||||
let vsid = validator_server.to_array();
|
|
||||||
move || {
|
|
||||||
loop {
|
|
||||||
let mut msg = xous::receive_message(SID::from_array(vsid)).unwrap();
|
|
||||||
log::debug!("validator message: {:?}", msg);
|
|
||||||
match FromPrimitive::from_usize(msg.body.id()) {
|
|
||||||
Some(ValidationOp::Validate) => {
|
|
||||||
let mut buffer = unsafe{Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())};
|
|
||||||
let validation = buffer.to_original::<Validation, _>().unwrap();
|
|
||||||
let result = validator(validation.text, validation.opcode);
|
|
||||||
buffer.replace(result).expect("couldn't place validation result");
|
|
||||||
}
|
|
||||||
Some(ValidationOp::Quit) => {
|
|
||||||
// this is a blocking scalar, have to return a dummy value to unblock the caller
|
|
||||||
xous::return_scalar(msg.sender, 0).unwrap();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
log::error!("received unknown message: {:?}", msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Some(validator_server.to_array())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
let spec = ManagedPromptWithTextResponse {
|
|
||||||
token: self.token,
|
token: self.token,
|
||||||
prompt: xous_ipc::String::from_str(prompt),
|
prompt: xous_ipc::String::from_str(prompt),
|
||||||
validator,
|
|
||||||
validator_op: maybe_validator_op.unwrap_or(0)
|
|
||||||
};
|
};
|
||||||
let mut buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?;
|
// question: do we want to add a retry limit?
|
||||||
buf.lend_mut(self.conn, Opcode::PromptWithTextResponse.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
loop {
|
||||||
if let Some(server) = validator {
|
let mut buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?;
|
||||||
let cid = xous::connect(SID::from_array(server)).unwrap();
|
buf.lend_mut(self.conn, Opcode::PromptWithTextResponse.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
||||||
send_message(cid, Message::new_blocking_scalar(ValidationOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
|
match buf.to_original::<TextEntryPayload, _>() {
|
||||||
unsafe{xous::disconnect(cid).unwrap()}; // must disconnect before destroy to avoid the CID from hanging out in our outbound table which is limited to 32 entries
|
Ok(response) => {
|
||||||
xous::destroy_server(SID::from_array(server)).expect("couldn't destroy temporary server");
|
if let Some(validator) = maybe_validator {
|
||||||
}
|
if let Some(err_msg) = validator(response, maybe_validator_op.unwrap_or(0)) {
|
||||||
self.unlock();
|
spec.prompt.clear();
|
||||||
match buf.to_original::<TextEntryPayload, _>() {
|
spec.prompt.append(err_msg.as_str().unwrap_or("UTF-8 error")).ok();
|
||||||
Ok(response) => Ok(response),
|
} else {
|
||||||
_ => Err(xous::Error::InternalError)
|
send_message(self.conn,
|
||||||
|
Message::new_blocking_scalar(Opcode::TextResponseValid.to_usize().unwrap(),
|
||||||
|
self.token[0] as _, self.token[1] as _, self.token[2] as _, self.token[3] as _,
|
||||||
|
)).expect("couldn't acknowledge text entry");
|
||||||
|
self.unlock();
|
||||||
|
return Ok(response)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
send_message(self.conn,
|
||||||
|
Message::new_blocking_scalar(Opcode::TextResponseValid.to_usize().unwrap(),
|
||||||
|
self.token[0] as _, self.token[1] as _, self.token[2] as _, self.token[3] as _,
|
||||||
|
)).expect("couldn't acknowledge text entry");
|
||||||
|
self.unlock();
|
||||||
|
return Ok(response)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
// we send the valid response token even in this case because we want the modals server to move on and not get stuck on this error.
|
||||||
|
send_message(self.conn,
|
||||||
|
Message::new_blocking_scalar(Opcode::TextResponseValid.to_usize().unwrap(),
|
||||||
|
self.token[0] as _, self.token[1] as _, self.token[2] as _, self.token[3] as _,
|
||||||
|
)).expect("couldn't acknowledge text entry");
|
||||||
|
self.unlock();
|
||||||
|
return Err(xous::Error::InternalError);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,6 @@ mod api;
|
||||||
use api::*;
|
use api::*;
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
use xous::SID;
|
|
||||||
|
|
||||||
use xous::{msg_scalar_unpack, msg_blocking_scalar_unpack, send_message, Message};
|
use xous::{msg_scalar_unpack, msg_blocking_scalar_unpack, send_message, Message};
|
||||||
use xous_ipc::Buffer;
|
use xous_ipc::Buffer;
|
||||||
|
|
||||||
|
|
@ -455,49 +453,17 @@ fn xmain() -> ! {
|
||||||
},
|
},
|
||||||
Some(Opcode::TextEntryReturn) => {
|
Some(Opcode::TextEntryReturn) => {
|
||||||
match op {
|
match op {
|
||||||
RendererState::RunText(config) => {
|
RendererState::RunText(_config) => {
|
||||||
log::trace!("validating text entry modal");
|
log::trace!("validating text entry modal");
|
||||||
let buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
let buf = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||||
let text = buf.to_original::<gam::modal::TextEntryPayload, _>().unwrap();
|
let text = buf.to_original::<gam::modal::TextEntryPayload, _>().unwrap();
|
||||||
if let Some(validator_sid) = config.validator {
|
if let Some(mut origin) = dr.take() {
|
||||||
let cid = xous::connect(SID::from_array(validator_sid)).unwrap();
|
let mut response = unsafe { Buffer::from_memory_message_mut(origin.body.memory_message_mut().unwrap()) };
|
||||||
let validation = Validation {
|
response.replace(text).unwrap();
|
||||||
text,
|
op = RendererState::None;
|
||||||
opcode: config.validator_op,
|
|
||||||
};
|
|
||||||
let mut buf = Buffer::into_buf(validation).expect("couldn't convert validator structure");
|
|
||||||
buf.lend_mut(cid, ValidationOp::Validate.to_u32().unwrap()).expect("validation call failed");
|
|
||||||
let response = buf.to_original::<Option<xous_ipc::String::<256>>, _>().expect("couldn't unpack validation response");
|
|
||||||
unsafe{xous::disconnect(cid).unwrap();}
|
|
||||||
if let Some(err) = response {
|
|
||||||
// try again
|
|
||||||
renderer_modal.modify(
|
|
||||||
Some(ActionType::TextEntry(text_action)),
|
|
||||||
Some(config.prompt.as_str().unwrap()), false,
|
|
||||||
Some(err.as_str().unwrap()), false, None
|
|
||||||
);
|
|
||||||
renderer_modal.activate();
|
|
||||||
} else {
|
|
||||||
if let Some(mut origin) = dr.take() {
|
|
||||||
let mut response = unsafe { Buffer::from_memory_message_mut(origin.body.memory_message_mut().unwrap()) };
|
|
||||||
response.replace(text).unwrap();
|
|
||||||
op = RendererState::None;
|
|
||||||
} else {
|
|
||||||
log::error!("Ux routine returned but no origin was recorded");
|
|
||||||
panic!("Ux routine returned but no origin was recorded");
|
|
||||||
}
|
|
||||||
token_lock = next_lock(&mut work_queue);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if let Some(mut origin) = dr.take() {
|
log::error!("Ux routine returned but no origin was recorded");
|
||||||
let mut response = unsafe { Buffer::from_memory_message_mut(origin.body.memory_message_mut().unwrap()) };
|
panic!("Ux routine returned but no origin was recorded");
|
||||||
response.replace(text).unwrap();
|
|
||||||
op = RendererState::None;
|
|
||||||
} else {
|
|
||||||
log::error!("Ux routine returned but no origin was recorded");
|
|
||||||
panic!("Ux routine returned but no origin was recorded");
|
|
||||||
}
|
|
||||||
token_lock = next_lock(&mut work_queue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RendererState::None => log::warn!("Text entry detected a fat finger event, ignoring."),
|
RendererState::None => log::warn!("Text entry detected a fat finger event, ignoring."),
|
||||||
|
|
@ -507,6 +473,15 @@ fn xmain() -> ! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Some(Opcode::TextResponseValid) => msg_blocking_scalar_unpack!(msg, t0, t1, t2, t3, {
|
||||||
|
let incoming_token = [t0 as u32, t1 as u32, t2 as u32, t3 as u32];
|
||||||
|
if incoming_token != token_lock.unwrap_or(default_nonce) {
|
||||||
|
log::warn!("Attempt to access modals without a mutex lock. Ignoring.");
|
||||||
|
} else {
|
||||||
|
token_lock = next_lock(&mut work_queue);
|
||||||
|
}
|
||||||
|
xous::return_scalar(msg.sender, 1).unwrap();
|
||||||
|
}),
|
||||||
Some(Opcode::NotificationReturn) => {
|
Some(Opcode::NotificationReturn) => {
|
||||||
match op {
|
match op {
|
||||||
RendererState::RunNotification(_) => {
|
RendererState::RunNotification(_) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue