From 0e8767aea26ccfdd8a3e0e95d00643a75cd16812 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 16 Mar 2021 19:07:03 +0800 Subject: [PATCH] this commit crashes on boot --- emulation/renode.svd | 12 ++--- services/shellchat/src/cmds.rs | 86 +++++++++++++++++++++++----------- services/shellchat/src/main.rs | 28 +++++++++-- svd2utra/examples/soc.svd | 12 ++--- 4 files changed, 95 insertions(+), 43 deletions(-) diff --git a/emulation/renode.svd b/emulation/renode.svd index 4781c6264..3ae9e4c40 100644 --- a/emulation/renode.svd +++ b/emulation/renode.svd @@ -1328,10 +1328,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0xd77dca2f + 0x2a2abe5e 32 @@ -1348,7 +1348,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x0d + 0x0e 32 @@ -1362,7 +1362,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> GIT_DIRTY 0x001c - 0x01 + 0x00 32 @@ -3056,7 +3056,7 @@ again until the interrupt is serviced.]]> SEED1 0x0000 - 0x508efbe6 + 0x1a7a2a66 32 @@ -3071,7 +3071,7 @@ again until the interrupt is serviced.]]> SEED0 0x0004 - 0x1f1cd099 + 0x4b326b1e 32 diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index b21da8449..0f54633cc 100644 --- a/services/shellchat/src/cmds.rs +++ b/services/shellchat/src/cmds.rs @@ -1,10 +1,15 @@ -use xous::String; +use xous::{String, MessageEnvelope}; use core::fmt::Write; /////////////////////////// Common items to all commands pub trait ShellCmdApi<'a> { // user implemented: // called to process the command with the remainder of the string attached fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result>, xous::Error>; + // called to process incoming messages that may have been origniated by the most recently issued command + fn callback(&mut self, msg: &MessageEnvelope, _env: &mut CommonEnv) -> Result>, xous::Error> { + log::info!("SHCH: received unhandled message {:?}", msg); + Ok(None) + } // created with cmd_api! macro // checks if the command matches the current verb in question @@ -28,6 +33,7 @@ macro_rules! cmd_api { }; } + /////////////////////////// Command shell integration #[derive(Debug)] pub struct CommonEnv { @@ -55,14 +61,17 @@ mod echo; use echo::*; mod test; use test::*; mod sleep; use sleep::*; mod sensors; use sensors::*; +mod callback; use callback::*; #[derive(Debug)] pub struct CmdEnv { common_env: CommonEnv, + lastverb: String::<256>, ///// 2. declare storage for your command here. test_cmd: Test, sleep_cmd: Sleep, sensors_cmd: Sensors, + callback_cmd: CallBack, } impl CmdEnv { pub fn new(gam: xous::CID) -> CmdEnv { @@ -74,14 +83,16 @@ impl CmdEnv { ticktimer: xous::connect(ticktimer_server_id).unwrap(), gam, }, + lastverb: String::<256>::new(), ///// 3. initialize your storage, by calling new() test_cmd: Test::new(), sleep_cmd: Sleep::new(), sensors_cmd: Sensors::new(), + callback_cmd: CallBack::new(), } } - pub fn dispatch(&mut self, cmdline: &mut String::<1024>) -> Result>, xous::Error> { + pub fn dispatch(&mut self, maybe_cmdline: Option<&mut String::<1024>>, maybe_callback: Option<&MessageEnvelope>) -> Result>, xous::Error> { let mut ret = String::<1024>::new(); let mut echo_cmd = Echo {}; // this command has no persistent storage, so we can "create" it every time we call dispatch (but it's a zero-cost absraction so this doesn't actually create any instructions) @@ -91,38 +102,59 @@ impl CmdEnv { &mut self.test_cmd, &mut self.sleep_cmd, &mut self.sensors_cmd, + &mut self.callback_cmd, ]; - let maybe_verb = tokenize(cmdline); + if let Some(cmdline) = maybe_cmdline { + let maybe_verb = tokenize(cmdline); - let mut cmd_ret: Result>, xous::Error> = Ok(None); - if let Some(verb_string) = maybe_verb { - let verb = verb_string.to_str(); + let mut cmd_ret: Result>, xous::Error> = Ok(None); + if let Some(verb_string) = maybe_verb { + let verb = verb_string.to_str(); - // search through the list of commands linearly until one matches, - // then run it. - let mut match_found = false; - for cmd in commands.iter_mut() { - if cmd.matches(verb) { - match_found = true; - cmd_ret = cmd.process(*cmdline, &mut self.common_env); - }; - } - - // if none match, create a list of available commands - if !match_found { - let mut first = true; - write!(ret, "Commands: ").unwrap(); - for cmd in commands.iter() { - if !first { - ret.append(", ")?; - } - ret.append(cmd.verb())?; - first = false; + // search through the list of commands linearly until one matches, + // then run it. + let mut match_found = false; + for cmd in commands.iter_mut() { + if cmd.matches(verb) { + match_found = true; + cmd_ret = cmd.process(*cmdline, &mut self.common_env); + self.lastverb.clear(); + write!(self.lastverb, "{}", verb).expect("SHCH: couldn't record last verb"); + }; + } + + // if none match, create a list of available commands + if !match_found { + let mut first = true; + write!(ret, "Commands: ").unwrap(); + for cmd in commands.iter() { + if !first { + ret.append(", ")?; + } + ret.append(cmd.verb())?; + first = false; + } + Ok(Some(ret)) + } else { + cmd_ret } - Ok(Some(ret)) } else { + Ok(None) + } + } else if let Some(callback) = maybe_callback { + let mut cmd_ret: Result>, xous::Error> = Ok(None); + if self.lastverb.len() > 0 { + let verb = self.lastverb.to_str(); + for cmd in commands.iter_mut() { + if cmd.matches(verb) { + cmd_ret = cmd.callback(callback, &mut self.common_env); + break; + }; + } cmd_ret + } else { + Ok(None) } } else { Ok(None) diff --git a/services/shellchat/src/main.rs b/services/shellchat/src/main.rs index d49315962..f799698ec 100644 --- a/services/shellchat/src/main.rs +++ b/services/shellchat/src/main.rs @@ -1,7 +1,7 @@ #![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_main)] -use log::{error, info}; +use log::info; use core::fmt::Write; use core::convert::TryFrom; @@ -16,6 +16,8 @@ use graphics_server::{Gid, Point, Rectangle, TextBounds, TextView, DrawStyle, Gl use heapless::spsc::Queue; use heapless::consts::U16; +use xous::MessageEnvelope; + mod cmds; use cmds::*; @@ -32,7 +34,8 @@ struct Repl { // optional structures that indicate new input to the Repl loop per iteration // an input string input: Option>, - // examples of future inputs: returned values via other services + // messages from other servers + msg: Option, // record our input history history: Queue, @@ -57,6 +60,7 @@ impl Repl{ let screensize = gam::get_canvas_bounds(gam_conn, content).expect("SHCH: couldn't get dimensions of content canvas"); Repl { input: None, + msg: None, history: Queue::new(), content, gam: gam_conn, @@ -80,6 +84,10 @@ impl Repl{ Ok(()) } + fn msg(&mut self, message: MessageEnvelope) { + self.msg = Some(message); + } + fn circular_push(&mut self, item: History) { if self.history.len() == self.history.capacity() { self.history.dequeue().expect("SHCH: couldn't dequeue historye"); @@ -108,7 +116,17 @@ impl Repl{ // take the input and pass it on to the various command parsers, and attach result if let Some(mut local) = self.input { - if let Some(res) = self.env.dispatch(&mut local).expect("SHCH: command dispatch failed") { + if let Some(res) = self.env.dispatch(Some(&mut local), None).expect("SHCH: command dispatch failed") { + let mut response = xous::String::<1024>::new(); + write!(response, "{}", res).expect("SHCH: can't copy result to history"); + let output_history = History { + text: response, + is_input: false + }; + self.circular_push(output_history); + } + } else if let Some(msg) = &self.msg { + if let Some(res) = self.env.dispatch(None, Some(msg)).expect("SCHC: callback failed") { let mut response = xous::String::<1024>::new(); write!(response, "{}", res).expect("SHCH: can't copy result to history"); let output_history = History { @@ -121,6 +139,7 @@ impl Repl{ // clear all the inputs to the loop, so we don't process them twice self.input = None; + self.msg = None; // redraw UI now that we've responded self.redraw().expect("SHCH: can't redraw"); @@ -233,7 +252,8 @@ fn xmain() -> ! { } } } else { - error!("SHCH: couldn't convert message"); + repl.msg(envelope); + update_repl = true; } if update_repl { diff --git a/svd2utra/examples/soc.svd b/svd2utra/examples/soc.svd index 4781c6264..3ae9e4c40 100644 --- a/svd2utra/examples/soc.svd +++ b/svd2utra/examples/soc.svd @@ -1328,10 +1328,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0xd77dca2f + 0x2a2abe5e 32 @@ -1348,7 +1348,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x0d + 0x0e 32 @@ -1362,7 +1362,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> GIT_DIRTY 0x001c - 0x01 + 0x00 32 @@ -3056,7 +3056,7 @@ again until the interrupt is serviced.]]> SEED1 0x0000 - 0x508efbe6 + 0x1a7a2a66 32 @@ -3071,7 +3071,7 @@ again until the interrupt is serviced.]]> SEED0 0x0004 - 0x1f1cd099 + 0x4b326b1e 32