final cleanup on shellchat xous-ipc abstractions

the implementation relied on a quirk of xous-ipc where the message
ID is 0 for string send/receives. This cleans it up so that the API
is transparent and consistent with other API calls in the system.
This commit is contained in:
bunnie 2024-09-23 00:01:32 +08:00
parent 8424d19ea9
commit 3c0140bf2d
5 changed files with 29 additions and 35 deletions

View file

@ -381,7 +381,7 @@ impl CmdEnv {
let mut cmd_ret: Result<Option<String>, xous::Error> = Ok(None); let mut cmd_ret: Result<Option<String>, xous::Error> = Ok(None);
if let Some(verb_string) = maybe_verb { if let Some(verb_string) = maybe_verb {
let verb = verb_string.to_str(); let verb = &verb_string;
// search through the list of commands linearly until one matches, // search through the list of commands linearly until one matches,
// then run it. // then run it.
@ -389,7 +389,7 @@ impl CmdEnv {
for cmd in commands.iter_mut() { for cmd in commands.iter_mut() {
if cmd.matches(verb) { if cmd.matches(verb) {
match_found = true; match_found = true;
cmd_ret = cmd.process(*cmdline, &mut self.common_env); cmd_ret = cmd.process(cmdline.to_string(), &mut self.common_env);
self.lastverb.clear(); self.lastverb.clear();
write!(self.lastverb, "{}", verb).expect("SHCH: couldn't record last verb"); write!(self.lastverb, "{}", verb).expect("SHCH: couldn't record last verb");
}; };
@ -403,7 +403,7 @@ impl CmdEnv {
if !first { if !first {
ret.push_str(", "); ret.push_str(", ");
} }
ret.append(cmd.verb())?; ret.push_str(cmd.verb());
first = false; first = false;
} }
Ok(Some(ret)) Ok(Some(ret))
@ -416,9 +416,9 @@ impl CmdEnv {
} else if let Some(callback) = maybe_callback { } else if let Some(callback) = maybe_callback {
let mut cmd_ret: Result<Option<String>, xous::Error> = Ok(None); let mut cmd_ret: Result<Option<String>, xous::Error> = Ok(None);
// first check and see if we have a callback registration; if not, just map to the last verb // first check and see if we have a callback registration; if not, just map to the last verb
let verb = match self.common_env.cb_registrations.get(&(callback.body.id() as u32)) { let verb: &str = match self.common_env.cb_registrations.get(&(callback.body.id() as u32)) {
Some(verb) => verb.to_str(), Some(verb) => verb.as_str(),
None => self.lastverb.to_str(), None => self.lastverb.as_str(),
}; };
// now dispatch // now dispatch
let mut verbfound = false; let mut verbfound = false;
@ -449,13 +449,13 @@ pub fn tokenize(line: &mut String) -> Option<String> {
let mut foundrest = false; let mut foundrest = false;
for ch in lineiter { for ch in lineiter {
if ch != ' ' && !foundspace { if ch != ' ' && !foundspace {
token.push(ch).unwrap(); token.push(ch);
} else if foundspace && foundrest { } else if foundspace && foundrest {
retline.push(ch).unwrap(); retline.push(ch);
} else if foundspace && ch != ' ' { } else if foundspace && ch != ' ' {
// handle case of multiple spaces in a row // handle case of multiple spaces in a row
foundrest = true; foundrest = true;
retline.push(ch).unwrap(); retline.push(ch);
} else { } else {
foundspace = true; foundspace = true;
// consume the space // consume the space

View file

@ -13,6 +13,7 @@ use num_traits::*;
#[cfg(feature = "shellperf")] #[cfg(feature = "shellperf")]
use perflib::*; use perflib::*;
use xous::MessageEnvelope; use xous::MessageEnvelope;
use xous_ipc::Buffer;
use String; use String;
#[cfg(feature = "ditherpunk")] #[cfg(feature = "ditherpunk")]
use {gam::DecodePng, std::str::FromStr}; use {gam::DecodePng, std::str::FromStr};
@ -22,7 +23,7 @@ use {
tungstenite::{stream::MaybeTlsStream, WebSocket}, tungstenite::{stream::MaybeTlsStream, WebSocket},
}; };
use crate::{CommonEnv, ShellCmdApi}; use crate::{CommonEnv, ShellCmdApi, ShellOpcode};
pub struct NetCmd { pub struct NetCmd {
callback_id: Option<u32>, callback_id: Option<u32>,
@ -303,7 +304,8 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
let mut iters = 0; let mut iters = 0;
let mut s = String::new(); let mut s = String::new();
write!(s, "UDP server {} started", index).unwrap(); write!(s, "UDP server {} started", index).unwrap();
s.send(self_cid).unwrap(); let buf = Buffer::into_buf(s.to_string()).unwrap();
buf.send(self_cid, ShellOpcode::Line.to_u32().unwrap()).unwrap();
loop { loop {
s.clear(); s.clear();
let mut buf = [0u8; NET_MTU]; let mut buf = [0u8; NET_MTU];
@ -318,7 +320,8 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
std::str::from_utf8(&buf[..bytes]).unwrap() std::str::from_utf8(&buf[..bytes]).unwrap()
) )
.unwrap(); .unwrap();
s.send(self_cid).unwrap(); let buf = Buffer::into_buf(s.to_string()).unwrap();
buf.send(self_cid, ShellOpcode::Line.to_u32().unwrap()).unwrap();
if do_response { if do_response {
match udp.send_to( match udp.send_to(
format!("Server {} received {} bytes\r\n", index, bytes) format!("Server {} received {} bytes\r\n", index, bytes)
@ -347,7 +350,8 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
} }
s.clear(); s.clear();
write!(s, "UDP server {} rx closed after {} iters", index, iters).unwrap(); write!(s, "UDP server {} rx closed after {} iters", index, iters).unwrap();
s.send(self_cid).unwrap(); let buf = Buffer::into_buf(s.to_string()).unwrap();
buf.send(self_cid, ShellOpcode::Line.to_u32().unwrap()).unwrap();
} }
}); });
} }
@ -841,7 +845,8 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
} }
} }
xous::Message::Move(m) => { xous::Message::Move(m) => {
let s = String::from_message(m).unwrap(); let buffer = unsafe { Buffer::from_memory_message(m) };
let s = buffer.as_flat::<String, _>().unwrap();
write!(ret, "{}", s.as_str()).unwrap(); write!(ret, "{}", s.as_str()).unwrap();
} }
_ => { _ => {

View file

@ -37,8 +37,7 @@ impl<'a> ShellCmdApi<'a> for Ssid {
write!(ret, "RSSI reported in dBm:\n").unwrap(); write!(ret, "RSSI reported in dBm:\n").unwrap();
for ssid in ssid_list { for ssid in ssid_list {
if ssid.name.len() > 0 { if ssid.name.len() > 0 {
write!(ret, "-{} {}\n", ssid.rssi, &ssid.name.as_str().unwrap_or("UTF-8 error")) write!(ret, "-{} {}\n", ssid.rssi, &ssid.name.as_str()).unwrap();
.unwrap();
} }
} }
write!(ret, "Scan state: {:?}\n", state).unwrap(); write!(ret, "Scan state: {:?}\n", state).unwrap();

View file

@ -695,13 +695,7 @@ impl<'a> ShellCmdApi<'a> for Test {
write!(ret, "RSSI reported in dBm:\n").unwrap(); write!(ret, "RSSI reported in dBm:\n").unwrap();
for ssid in ssid_list { for ssid in ssid_list {
if ssid.name.len() > 0 { if ssid.name.len() > 0 {
write!( write!(ret, "-{} {}\n", ssid.rssi, &ssid.name.as_str()).unwrap();
ret,
"-{} {}\n",
ssid.rssi,
&ssid.name.as_str().unwrap_or("UTF-8 error")
)
.unwrap();
} }
} }
write!( write!(
@ -736,8 +730,8 @@ impl<'a> ShellCmdApi<'a> for Test {
net_up = status.link_state == com_rs::LinkState::Connected; net_up = status.link_state == com_rs::LinkState::Connected;
dhcp_ok = status.ipv4.dhcp == com_rs::DhcpState::Bound; dhcp_ok = status.ipv4.dhcp == com_rs::DhcpState::Bound;
ssid_ok = if let Some(ssid) = status.ssid { ssid_ok = if let Some(ssid) = status.ssid {
log::info!("got ssid: {}", ssid.name.as_str().unwrap_or("invalid")); log::info!("got ssid: {}", ssid.name.as_str());
ssid.name.as_str().unwrap_or("invalid") == "precursortest" ssid.name.as_str() == "precursortest"
} else { } else {
false false
}; };

View file

@ -231,19 +231,16 @@ impl Repl {
let mut dirty = true; let mut dirty = true;
// take the input and pass it on to the various command parsers, and attach result // take the input and pass it on to the various command parsers, and attach result
if let Some(local) = &self.input { if let Some(local) = &mut self.input {
log::trace!("processing line: {}", local); log::trace!("processing line: {}", local);
if let Some(res) = if let Some(res) = self.env.dispatch(Some(local), None).expect("command dispatch failed") {
self.env.dispatch(Some(&mut String::from(&local)), None).expect("command dispatch failed")
{
#[cfg(feature = "tts")] #[cfg(feature = "tts")]
{ {
let mut output = t!("shellchat.output-tts", locales::LANG).to_string(); let mut output = t!("shellchat.output-tts", locales::LANG).to_string();
output.push_str(res.as_str().unwrap_or("UTF-8 error")); output.push_str(res.as_str());
self.tts.tts_simple(&output).unwrap(); self.tts.tts_simple(&output).unwrap();
} }
let output_history = let output_history = History { text: String::from(res.as_str()), is_input: false };
History { text: String::from(res.as_str().unwrap_or("UTF-8 Error")), is_input: false };
self.circular_push(output_history); self.circular_push(output_history);
} else { } else {
dirty = false; dirty = false;
@ -254,11 +251,10 @@ impl Repl {
#[cfg(feature = "tts")] #[cfg(feature = "tts")]
{ {
let mut output = t!("shellchat.output-tts", locales::LANG).to_string(); let mut output = t!("shellchat.output-tts", locales::LANG).to_string();
output.push_str(res.as_str().unwrap_or("UTF-8 error")); output.push_str(res.as_str());
self.tts.tts_simple(&output).unwrap(); self.tts.tts_simple(&output).unwrap();
} }
let output_history = let output_history = History { text: String::from(res.as_str()), is_input: false };
History { text: String::from(res.as_str().unwrap_or("UTF-8 Error")), is_input: false };
self.circular_push(output_history); self.circular_push(output_history);
} else { } else {
dirty = false; dirty = false;