mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
add a minimal pure terminal based console for baosec target
The screen is tiny, so it's not useful for feedback -- all interaction happens on the serial console for this target. Main purpose for this console is debug/test/factory bringup.
This commit is contained in:
parent
2afc139957
commit
a9abba0bb0
9 changed files with 585 additions and 0 deletions
20
services/bao-console/Cargo.toml
Normal file
20
services/bao-console/Cargo.toml
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
[package]
|
||||||
|
name = "bao-console"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
utralib = { version = "0.1.25", optional = true, default-features = false }
|
||||||
|
xous-names = { package = "xous-api-names", version = "0.9.63" }
|
||||||
|
ticktimer = { package = "xous-api-ticktimer", version = "0.9.61" }
|
||||||
|
xous = "0.9.64"
|
||||||
|
log-server = { package = "xous-api-log", version = "0.1.61" }
|
||||||
|
log = "0.4.14"
|
||||||
|
num-derive = { version = "0.3.3", default-features = false }
|
||||||
|
num-traits = { version = "0.2.14", default-features = false }
|
||||||
|
cramium-hal = { path = "../../libs/cramium-hal", features = ["std"] }
|
||||||
|
cram-hal-service = { path = "../cram-hal-service" }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
cramium-soc = ["utralib/cramium-soc"]
|
||||||
|
default = []
|
||||||
218
services/bao-console/src/cmds.rs
Normal file
218
services/bao-console/src/cmds.rs
Normal file
|
|
@ -0,0 +1,218 @@
|
||||||
|
use core::fmt::Write;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use String;
|
||||||
|
use cram_hal_service::trng;
|
||||||
|
use xous::MessageEnvelope;
|
||||||
|
|
||||||
|
/////////////////////////// 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, env: &mut CommonEnv) -> Result<Option<String>, 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<Option<String>, xous::Error> {
|
||||||
|
log::info!("received unhandled message {:?}", msg);
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
// created with cmd_api! macro
|
||||||
|
// checks if the command matches the current verb in question
|
||||||
|
fn matches(&self, verb: &str) -> bool;
|
||||||
|
// returns my verb
|
||||||
|
fn verb(&self) -> &'static str;
|
||||||
|
}
|
||||||
|
// the argument to this macro is the command verb
|
||||||
|
macro_rules! cmd_api {
|
||||||
|
($verb:expr) => {
|
||||||
|
fn verb(&self) -> &'static str { stringify!($verb) }
|
||||||
|
fn matches(&self, verb: &str) -> bool { if verb == stringify!($verb) { true } else { false } }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////////////// Command shell integration
|
||||||
|
pub struct CommonEnv {
|
||||||
|
ticktimer: ticktimer::Ticktimer,
|
||||||
|
cb_registrations: HashMap<u32, String>,
|
||||||
|
trng: trng::Trng,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
xns: xous_names::XousNames,
|
||||||
|
}
|
||||||
|
impl CommonEnv {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn register_handler(&mut self, verb: String) -> u32 {
|
||||||
|
let mut key: u32;
|
||||||
|
loop {
|
||||||
|
key = self.trng.get_u32().unwrap();
|
||||||
|
// reserve the bottom 1000 IDs for the main loop enums.
|
||||||
|
if !self.cb_registrations.contains_key(&key) && (key > 1000) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.cb_registrations.insert(key, verb);
|
||||||
|
key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
To add a new command:
|
||||||
|
0. ensure that the command implements the ShellCmdApi (above)
|
||||||
|
1. mod/use the new command
|
||||||
|
2. create an entry for the command's storage in the CmdEnv structure
|
||||||
|
3. initialize the persistant storage here
|
||||||
|
4. add it to the "commands" array in the dispatch() routine below
|
||||||
|
|
||||||
|
Side note: if your command doesn't require persistent storage, you could,
|
||||||
|
technically, generate the command dynamically every time it's called. Echo
|
||||||
|
demonstrates this.
|
||||||
|
*/
|
||||||
|
|
||||||
|
///// 1. add your module here, and pull its namespace into the local crate
|
||||||
|
mod echo;
|
||||||
|
use echo::*;
|
||||||
|
mod ver;
|
||||||
|
use ver::*;
|
||||||
|
mod test;
|
||||||
|
use test::*;
|
||||||
|
mod trng_cmd;
|
||||||
|
use trng_cmd::*;
|
||||||
|
|
||||||
|
pub struct CmdEnv {
|
||||||
|
common_env: CommonEnv,
|
||||||
|
lastverb: String,
|
||||||
|
///// 2. declare storage for your command here.
|
||||||
|
trng_cmd: TrngCmd,
|
||||||
|
}
|
||||||
|
impl CmdEnv {
|
||||||
|
pub fn new(xns: &xous_names::XousNames) -> CmdEnv {
|
||||||
|
let ticktimer = ticktimer::Ticktimer::new().expect("Couldn't connect to Ticktimer");
|
||||||
|
// _ prefix allows us to leave the `mut` here without creating a warning.
|
||||||
|
// the `mut` option is needed for some features.
|
||||||
|
let mut _common = CommonEnv {
|
||||||
|
ticktimer,
|
||||||
|
cb_registrations: HashMap::new(),
|
||||||
|
trng: trng::Trng::new(&xns).unwrap(),
|
||||||
|
xns: xous_names::XousNames::new().unwrap(),
|
||||||
|
};
|
||||||
|
CmdEnv {
|
||||||
|
common_env: _common,
|
||||||
|
lastverb: String::new(),
|
||||||
|
///// 3. initialize your storage, by calling new()
|
||||||
|
trng_cmd: {
|
||||||
|
log::debug!("trng");
|
||||||
|
TrngCmd::new()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dispatch(
|
||||||
|
&mut self,
|
||||||
|
maybe_cmdline: Option<&mut String>,
|
||||||
|
maybe_callback: Option<&MessageEnvelope>,
|
||||||
|
) -> Result<Option<String>, xous::Error> {
|
||||||
|
let mut ret = String::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)
|
||||||
|
let mut ver_cmd = Ver {};
|
||||||
|
let mut console_cmd = Test {};
|
||||||
|
let commands: &mut [&mut dyn ShellCmdApi] = &mut [
|
||||||
|
///// 4. add your command to this array, so that it can be looked up and dispatched
|
||||||
|
&mut echo_cmd,
|
||||||
|
&mut ver_cmd,
|
||||||
|
&mut self.trng_cmd,
|
||||||
|
&mut console_cmd,
|
||||||
|
];
|
||||||
|
|
||||||
|
if let Some(cmdline) = maybe_cmdline {
|
||||||
|
let maybe_verb = tokenize(cmdline);
|
||||||
|
|
||||||
|
let mut cmd_ret: Result<Option<String>, xous::Error> = Ok(None);
|
||||||
|
if let Some(verb_string) = maybe_verb {
|
||||||
|
let verb = &verb_string;
|
||||||
|
|
||||||
|
// 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.to_string(), &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.push_str(", ");
|
||||||
|
}
|
||||||
|
ret.push_str(cmd.verb());
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
Ok(Some(ret))
|
||||||
|
} else {
|
||||||
|
cmd_ret
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
} else if let Some(callback) = maybe_callback {
|
||||||
|
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
|
||||||
|
let verb = match self.common_env.cb_registrations.get(&(callback.body.id() as u32)) {
|
||||||
|
Some(verb) => &verb,
|
||||||
|
None => &self.lastverb,
|
||||||
|
};
|
||||||
|
// now dispatch
|
||||||
|
let mut verbfound = false;
|
||||||
|
for cmd in commands.iter_mut() {
|
||||||
|
if cmd.matches(verb) {
|
||||||
|
cmd_ret = cmd.callback(callback, &mut self.common_env);
|
||||||
|
verbfound = true;
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if verbfound { cmd_ret } else { Ok(None) }
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// extract the first token, as delimited by spaces
|
||||||
|
/// modifies the incoming line by removing the token and returning the remainder
|
||||||
|
/// returns the found token
|
||||||
|
/// note: we don't have split() because of nostd
|
||||||
|
pub fn tokenize(line: &mut String) -> Option<String> {
|
||||||
|
let mut token = String::new();
|
||||||
|
let mut retline = String::new();
|
||||||
|
|
||||||
|
let lineiter = line.as_str().chars();
|
||||||
|
let mut foundspace = false;
|
||||||
|
let mut foundrest = false;
|
||||||
|
for ch in lineiter {
|
||||||
|
if ch != ' ' && !foundspace {
|
||||||
|
token.push(ch);
|
||||||
|
} else if foundspace && foundrest {
|
||||||
|
retline.push(ch);
|
||||||
|
} else if foundspace && ch != ' ' {
|
||||||
|
// handle case of multiple spaces in a row
|
||||||
|
foundrest = true;
|
||||||
|
retline.push(ch);
|
||||||
|
} else {
|
||||||
|
foundspace = true;
|
||||||
|
// consume the space
|
||||||
|
}
|
||||||
|
}
|
||||||
|
line.clear();
|
||||||
|
write!(line, "{}", retline.as_str()).unwrap();
|
||||||
|
if token.len() > 0 { Some(token) } else { None }
|
||||||
|
}
|
||||||
16
services/bao-console/src/cmds/echo.rs
Normal file
16
services/bao-console/src/cmds/echo.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
use String;
|
||||||
|
|
||||||
|
use crate::{CommonEnv, ShellCmdApi};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Echo {}
|
||||||
|
|
||||||
|
impl<'a> ShellCmdApi<'a> for Echo {
|
||||||
|
cmd_api!(echo);
|
||||||
|
|
||||||
|
// inserts boilerplate for command API
|
||||||
|
|
||||||
|
fn process(&mut self, args: String, _env: &mut CommonEnv) -> Result<Option<String>, xous::Error> {
|
||||||
|
Ok(Some(args))
|
||||||
|
}
|
||||||
|
}
|
||||||
31
services/bao-console/src/cmds/test.rs
Normal file
31
services/bao-console/src/cmds/test.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
use String;
|
||||||
|
|
||||||
|
use crate::{CommonEnv, ShellCmdApi};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Test {}
|
||||||
|
|
||||||
|
impl<'a> ShellCmdApi<'a> for Test {
|
||||||
|
cmd_api!(test);
|
||||||
|
|
||||||
|
// inserts boilerplate for command API
|
||||||
|
|
||||||
|
fn process(&mut self, args: String, _env: &mut CommonEnv) -> Result<Option<String>, xous::Error> {
|
||||||
|
use core::fmt::Write;
|
||||||
|
let mut ret = String::new();
|
||||||
|
let helpstring = "Test commands. See code for options.";
|
||||||
|
|
||||||
|
let mut tokens = args.split(' ');
|
||||||
|
|
||||||
|
if let Some(sub_cmd) = tokens.next() {
|
||||||
|
match sub_cmd {
|
||||||
|
_ => {
|
||||||
|
write!(ret, "{}", helpstring).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
write!(ret, "{}", helpstring).unwrap();
|
||||||
|
}
|
||||||
|
Ok(Some(ret))
|
||||||
|
}
|
||||||
|
}
|
||||||
42
services/bao-console/src/cmds/trng_cmd.rs
Normal file
42
services/bao-console/src/cmds/trng_cmd.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
use String;
|
||||||
|
|
||||||
|
use crate::{CommonEnv, ShellCmdApi};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TrngCmd {}
|
||||||
|
impl TrngCmd {
|
||||||
|
pub fn new() -> Self { TrngCmd {} }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ShellCmdApi<'a> for TrngCmd {
|
||||||
|
cmd_api!(trng);
|
||||||
|
|
||||||
|
fn process(&mut self, args: String, env: &mut CommonEnv) -> Result<Option<String>, xous::Error> {
|
||||||
|
use core::fmt::Write;
|
||||||
|
let mut ret = String::new();
|
||||||
|
let helpstring = "trng [pump]";
|
||||||
|
|
||||||
|
let mut tokens = args.split(' ');
|
||||||
|
|
||||||
|
if let Some(sub_cmd) = tokens.next() {
|
||||||
|
match sub_cmd {
|
||||||
|
"pump" => {
|
||||||
|
const ROUNDS: usize = 16;
|
||||||
|
for i in 0..ROUNDS {
|
||||||
|
log::info!("pump round {}", i);
|
||||||
|
let mut buf: [u32; 1020] = [0; 1020];
|
||||||
|
env.trng.fill_buf(&mut buf).unwrap();
|
||||||
|
log::info!("pump samples: {:x}, {:x}, {:x}", buf[0], buf[512], buf[1019]);
|
||||||
|
}
|
||||||
|
write!(ret, "Pumped {}x1k values out of the engine", ROUNDS).unwrap();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
write!(ret, "{}", helpstring).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
write!(ret, "{}", helpstring).unwrap();
|
||||||
|
}
|
||||||
|
Ok(Some(ret))
|
||||||
|
}
|
||||||
|
}
|
||||||
40
services/bao-console/src/cmds/ver.rs
Normal file
40
services/bao-console/src/cmds/ver.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
use String;
|
||||||
|
|
||||||
|
use crate::{CommonEnv, ShellCmdApi};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Ver {}
|
||||||
|
|
||||||
|
impl<'a> ShellCmdApi<'a> for Ver {
|
||||||
|
cmd_api!(ver);
|
||||||
|
|
||||||
|
// inserts boilerplate for command API
|
||||||
|
|
||||||
|
fn process(&mut self, args: String, env: &mut CommonEnv) -> Result<Option<String>, xous::Error> {
|
||||||
|
use core::fmt::Write;
|
||||||
|
let mut ret = String::new();
|
||||||
|
let helpstring = "ver [xous]";
|
||||||
|
|
||||||
|
let mut tokens = args.split(' ');
|
||||||
|
|
||||||
|
if let Some(sub_cmd) = tokens.next() {
|
||||||
|
match sub_cmd {
|
||||||
|
"xous" => {
|
||||||
|
write!(ret, "Xous version: {}", env.ticktimer.get_version()).unwrap();
|
||||||
|
log::info!(
|
||||||
|
"{}VER.XOUS,{},{}",
|
||||||
|
xous::BOOKEND_START,
|
||||||
|
env.ticktimer.get_version(),
|
||||||
|
xous::BOOKEND_END
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
write!(ret, "{}", helpstring).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
write!(ret, "{}", helpstring).unwrap();
|
||||||
|
}
|
||||||
|
Ok(Some(ret))
|
||||||
|
}
|
||||||
|
}
|
||||||
18
services/bao-console/src/main.rs
Normal file
18
services/bao-console/src/main.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
mod cmds;
|
||||||
|
mod repl;
|
||||||
|
mod shell;
|
||||||
|
|
||||||
|
use cmds::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
log_server::init_wait().unwrap();
|
||||||
|
log::set_max_level(log::LevelFilter::Info);
|
||||||
|
|
||||||
|
let tt = ticktimer::Ticktimer::new().unwrap();
|
||||||
|
shell::start_shell();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
// just sleep as this is the parent thread
|
||||||
|
tt.sleep_ms(120_000).ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
94
services/bao-console/src/repl.rs
Normal file
94
services/bao-console/src/repl.rs
Normal file
|
|
@ -0,0 +1,94 @@
|
||||||
|
use xous::MessageEnvelope;
|
||||||
|
|
||||||
|
use crate::CmdEnv;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
#[allow(dead_code)] // until we implement history processing...
|
||||||
|
pub(crate) struct History {
|
||||||
|
// the history record
|
||||||
|
pub text: String,
|
||||||
|
// if true, this was input from the user; if false, it's a response from the shell
|
||||||
|
pub is_input: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const HISTORY_DEPTH: usize = 10;
|
||||||
|
pub(crate) struct Repl {
|
||||||
|
// optional structures that indicate new input to the Repl loop per iteration
|
||||||
|
// an input string
|
||||||
|
input: Option<String>,
|
||||||
|
// messages from other servers
|
||||||
|
msg: Option<MessageEnvelope>,
|
||||||
|
|
||||||
|
// record our input history
|
||||||
|
history: Vec<History>,
|
||||||
|
history_len: usize,
|
||||||
|
|
||||||
|
// command environment
|
||||||
|
env: CmdEnv,
|
||||||
|
}
|
||||||
|
impl Repl {
|
||||||
|
pub(crate) fn new(xns: &xous_names::XousNames) -> Self {
|
||||||
|
Repl {
|
||||||
|
input: None,
|
||||||
|
msg: None,
|
||||||
|
history: Vec::new(),
|
||||||
|
history_len: HISTORY_DEPTH,
|
||||||
|
env: CmdEnv::new(xns),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// accept a new input string
|
||||||
|
pub(crate) fn input(&mut self, line: &str) -> Result<(), xous::Error> {
|
||||||
|
self.input = Some(String::from(line));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn msg(&mut self, message: MessageEnvelope) { self.msg = Some(message); }
|
||||||
|
|
||||||
|
pub(crate) fn circular_push(&mut self, item: History) {
|
||||||
|
if self.history.len() >= self.history_len {
|
||||||
|
self.history.remove(0);
|
||||||
|
}
|
||||||
|
self.history.push(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// An index of -1 gives the most recent command.
|
||||||
|
// -2 would give the second most recent command
|
||||||
|
pub(crate) fn get_history(&mut self, index: isize) -> &str {
|
||||||
|
&self.history
|
||||||
|
[(self.history.len() as isize + index).max(0).min(self.history.len() as isize - 1) as usize]
|
||||||
|
.text
|
||||||
|
}
|
||||||
|
|
||||||
|
/// update the loop, in response to various inputs
|
||||||
|
pub(crate) fn update(&mut self, _was_callback: bool, _init_done: bool) -> Result<(), xous::Error> {
|
||||||
|
// if we had an input string, do something
|
||||||
|
if let Some(local) = &self.input {
|
||||||
|
let input_history = History { text: local.to_string(), is_input: true };
|
||||||
|
self.circular_push(input_history);
|
||||||
|
}
|
||||||
|
|
||||||
|
// AT THIS POINT: if we have other inputs, update accordingly
|
||||||
|
// other inputs might be, for example, events that came in from other servers that would
|
||||||
|
// side effect our commands
|
||||||
|
// take the input and pass it on to the various command parsers, and attach result
|
||||||
|
if let Some(local) = &mut self.input {
|
||||||
|
log::trace!("processing line: {}", local);
|
||||||
|
if let Some(res) = self.env.dispatch(Some(local), None).expect("command dispatch failed") {
|
||||||
|
println!("{}", res);
|
||||||
|
}
|
||||||
|
} else if let Some(msg) = &self.msg {
|
||||||
|
log::trace!("processing callback msg: {:?}", msg);
|
||||||
|
if let Some(res) = self.env.dispatch(None, Some(msg)).expect("callback failed") {
|
||||||
|
println!("{}", res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// clear all the inputs to the loop, so we don't process them twice
|
||||||
|
self.input = None;
|
||||||
|
self.msg = None;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
106
services/bao-console/src/shell.rs
Normal file
106
services/bao-console/src/shell.rs
Normal file
|
|
@ -0,0 +1,106 @@
|
||||||
|
use cram_hal_service::keyboard;
|
||||||
|
use xous::msg_scalar_unpack;
|
||||||
|
|
||||||
|
pub fn start_shell() {
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
shell();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////// local message passing from Ux Callback
|
||||||
|
use num_traits::*;
|
||||||
|
|
||||||
|
use crate::repl::HISTORY_DEPTH;
|
||||||
|
|
||||||
|
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||||
|
enum ConsoleOp {
|
||||||
|
/// A character is incoming
|
||||||
|
Keypress,
|
||||||
|
/// exit the application
|
||||||
|
Quit,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) const SERVER_NAME_SHELLCHAT: &str = "_Bao console application_"; // used internally by xous-names
|
||||||
|
|
||||||
|
fn shell() {
|
||||||
|
let xns = xous_names::XousNames::new().unwrap();
|
||||||
|
// unlimited connections allowed, this is a user app and it's up to the app to decide its policy
|
||||||
|
let shch_sid = xns.register_name(SERVER_NAME_SHELLCHAT, None).expect("can't register server");
|
||||||
|
|
||||||
|
let kbd = keyboard::Keyboard::new(&xns).unwrap();
|
||||||
|
kbd.register_listener(SERVER_NAME_SHELLCHAT, ConsoleOp::Keypress.to_u32().unwrap() as usize);
|
||||||
|
|
||||||
|
let mut repl = crate::repl::Repl::new(&xns);
|
||||||
|
let mut update_repl = false;
|
||||||
|
let mut was_callback = false;
|
||||||
|
let mut history_index: isize = 0;
|
||||||
|
|
||||||
|
let mut input = String::new();
|
||||||
|
loop {
|
||||||
|
let msg = xous::receive_message(shch_sid).unwrap();
|
||||||
|
let console_op: Option<ConsoleOp> = FromPrimitive::from_usize(msg.body.id());
|
||||||
|
log::debug!("{:?}", console_op);
|
||||||
|
match console_op {
|
||||||
|
Some(ConsoleOp::Keypress) => msg_scalar_unpack!(msg, k1, _k2, _k3, _k4, {
|
||||||
|
let k = char::from_u32(k1 as u32).unwrap_or('\u{0000}');
|
||||||
|
if k1 == 0x08 {
|
||||||
|
// backspace character
|
||||||
|
input.pop(); // returns None if empty
|
||||||
|
} else if k == '↑' || k == '↓' {
|
||||||
|
// a partial attempt at history processing.
|
||||||
|
if k == '↑' {
|
||||||
|
history_index -= 1;
|
||||||
|
} else if k == '↓' {
|
||||||
|
history_index += 1;
|
||||||
|
}
|
||||||
|
if history_index > -1 {
|
||||||
|
history_index = -1;
|
||||||
|
}
|
||||||
|
if history_index < -(HISTORY_DEPTH as isize) {
|
||||||
|
history_index = -(HISTORY_DEPTH as isize);
|
||||||
|
}
|
||||||
|
let input_len = input.len();
|
||||||
|
// line editing doesn't work because the "print" implementation waits until a newline
|
||||||
|
// to send the buffer to reduce message traffic. I think it's reasonable to leave it
|
||||||
|
// without line editing for now to preserve this trade-off.
|
||||||
|
if false {
|
||||||
|
// clear the old input
|
||||||
|
let spaces: String = std::iter::repeat(' ').take(input_len).collect();
|
||||||
|
print!("\r{}", spaces);
|
||||||
|
}
|
||||||
|
input.clear();
|
||||||
|
input.push_str(repl.get_history(history_index));
|
||||||
|
// print the new input
|
||||||
|
println!("{}", &input);
|
||||||
|
} else if k != '\u{0000}' && k != '\n' && k != '\r' {
|
||||||
|
input.push(k);
|
||||||
|
} else if k == '\n' || k == '\r' {
|
||||||
|
repl.input(input.as_str()).expect("REPL crashed");
|
||||||
|
update_repl = true;
|
||||||
|
was_callback = false;
|
||||||
|
history_index = 0;
|
||||||
|
input.clear();
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
Some(ConsoleOp::Quit) => {
|
||||||
|
log::error!("got Quit");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
log::trace!("got unknown message, treating as callback");
|
||||||
|
repl.msg(msg);
|
||||||
|
update_repl = true;
|
||||||
|
was_callback = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if update_repl {
|
||||||
|
repl.update(was_callback, true).expect("REPL had problems updating");
|
||||||
|
update_repl = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// clean up our program
|
||||||
|
log::error!("shell loop exit, destroying servers");
|
||||||
|
xns.unregister_server(shch_sid).unwrap();
|
||||||
|
xous::destroy_server(shch_sid).unwrap();
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue