2021-03-09 20:18:57 +00:00
use xous ::String ;
use core ::fmt ::Write ;
2021-03-10 14:59:52 +00:00
pub trait ShellCmdApi < ' a > {
2021-03-11 14:31:38 +00:00
// user implemented:
2021-03-09 20:18:57 +00:00
// called to process the command with the remainder of the string attached
2021-03-10 17:45:02 +00:00
fn process ( & mut self , rest : String ::< 1024 > , env : & mut CommonEnv ) -> Result < Option < String ::< 1024 > > , xous ::Error > ;
2021-03-11 14:31:38 +00:00
// created with cmd_api! macro
// checks if the command matches the current verb in question
fn matches ( & self , verb : & str ) -> bool ;
2021-03-09 20:18:57 +00:00
// returns my verb
2021-03-10 14:59:52 +00:00
fn verb ( & self ) -> & 'static str ;
2021-03-09 20:18:57 +00:00
}
2021-03-11 14:31:38 +00:00
// 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
}
}
} ;
}
2021-03-09 20:18:57 +00:00
2021-03-10 14:59:52 +00:00
/// 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
2021-03-09 20:18:57 +00:00
pub fn tokenize ( line : & mut String ::< 1024 > ) -> Option < String ::< 1024 > > {
let mut token = String ::< 1024 > ::new ( ) ;
let mut retline = String ::< 1024 > ::new ( ) ;
let lineiter = line . as_str ( ) . unwrap ( ) . chars ( ) ;
let mut foundspace = false ;
let mut foundrest = false ;
for ch in lineiter {
if ch ! = ' ' & & ! foundspace {
token . push ( ch ) . unwrap ( ) ;
} else if foundspace & & foundrest {
retline . push ( ch ) . unwrap ( ) ;
} else if foundspace & & ch ! = ' ' {
// handle case of multiple spaces in a row
foundrest = true ;
retline . push ( ch ) . unwrap ( ) ;
} else {
foundspace = true ;
// consume the space
}
}
line . clear ( ) ;
write! ( line , " {} " , retline . as_str ( ) . unwrap ( ) ) . unwrap ( ) ;
if token . len ( ) > 0 {
Some ( token )
} else {
None
}
}
2021-03-10 17:45:02 +00:00
#[ derive(Debug) ]
pub struct CommonEnv {
llio : xous ::CID ,
com : xous ::CID ,
ticktimer : xous ::CID ,
gam : xous ::CID ,
}
2021-03-10 14:59:52 +00:00
mod echo ;
use echo ::* ;
mod test ;
use test ::* ;
2021-03-10 17:45:02 +00:00
mod sleep ;
use sleep ::* ;
2021-03-10 18:57:58 +00:00
mod sensors ;
use sensors ::* ;
2021-03-10 14:59:52 +00:00
2021-03-09 20:18:57 +00:00
#[ derive(Debug) ]
pub struct CmdEnv {
2021-03-10 17:45:02 +00:00
common_env : CommonEnv ,
test_cmd : Test ,
sleep_cmd : Sleep ,
2021-03-10 18:57:58 +00:00
sensors_cmd : Sensors ,
2021-03-09 20:18:57 +00:00
}
impl CmdEnv {
2021-03-10 17:45:02 +00:00
pub fn new ( gam : xous ::CID ) -> CmdEnv {
let ticktimer_server_id = xous ::SID ::from_bytes ( b " ticktimer-server " ) . unwrap ( ) ;
2021-03-10 14:59:52 +00:00
/*
To add a new command :
- ensure that the command implements the ShellCmdApi
- mod / use the new command ( above )
2021-03-10 17:45:02 +00:00
- create an entry for the command ' s storage in the CmdEnv structure
2021-03-10 18:57:58 +00:00
- initialize the persistant storage here
2021-03-10 17:45:02 +00:00
- add it to the " commands " array in the dispatch ( ) routine below
2021-03-10 18:57:58 +00:00
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 .
2021-03-10 14:59:52 +00:00
* /
2021-03-10 17:45:02 +00:00
CmdEnv {
common_env : CommonEnv {
llio : xous_names ::request_connection_blocking ( xous ::names ::SERVER_NAME_LLIO ) . expect ( " CMD: can't connect to LLIO " ) ,
com : xous_names ::request_connection_blocking ( xous ::names ::SERVER_NAME_COM ) . expect ( " CMD: can't connect to COM " ) ,
ticktimer : xous ::connect ( ticktimer_server_id ) . unwrap ( ) ,
gam ,
} ,
test_cmd : Test ::new ( ) ,
sleep_cmd : Sleep ::new ( ) ,
2021-03-10 18:57:58 +00:00
sensors_cmd : Sensors ::new ( ) ,
2021-03-10 17:45:02 +00:00
}
}
pub fn dispatch ( & mut self , cmdline : & mut String ::< 1024 > ) -> Result < Option < String ::< 1024 > > , xous ::Error > {
let mut ret = String ::< 1024 > ::new ( ) ;
2021-03-10 18:57:58 +00:00
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)
2021-03-10 17:45:02 +00:00
let commands : & mut [ & mut dyn ShellCmdApi ] = & mut [
2021-03-10 18:57:58 +00:00
& mut echo_cmd ,
2021-03-10 17:45:02 +00:00
& mut self . test_cmd ,
& mut self . sleep_cmd ,
2021-03-10 18:57:58 +00:00
& mut self . sensors_cmd ,
2021-03-10 14:59:52 +00:00
] ;
2021-03-09 20:18:57 +00:00
let maybe_verb = tokenize ( cmdline ) ;
2021-03-10 14:59:52 +00:00
let mut cmd_ret : Result < Option < String ::< 1024 > > , xous ::Error > = Ok ( None ) ;
2021-03-09 20:18:57 +00:00
if let Some ( verb_string ) = maybe_verb {
let verb = verb_string . to_str ( ) ;
2021-03-10 14:59:52 +00:00
// search through the list of commands linearly until one matches,
// then run it.
let mut match_found = false ;
2021-03-10 17:45:02 +00:00
for cmd in commands . iter_mut ( ) {
2021-03-10 14:59:52 +00:00
if cmd . matches ( verb ) {
match_found = true ;
2021-03-10 17:45:02 +00:00
cmd_ret = cmd . process ( * cmdline , & mut self . common_env ) ;
2021-03-10 14:59:52 +00:00
} ;
}
// if none match, create a list of available commands
if ! match_found {
let mut first = true ;
write! ( ret , " Commands: " ) . unwrap ( ) ;
2021-03-10 17:45:02 +00:00
for cmd in commands . iter ( ) {
2021-03-10 14:59:52 +00:00
if ! first {
ret . append ( " , " ) ? ;
}
ret . append ( cmd . verb ( ) ) ? ;
2021-03-10 17:45:02 +00:00
first = false ;
2021-03-10 14:59:52 +00:00
}
2021-03-09 20:18:57 +00:00
Ok ( Some ( ret ) )
2021-03-10 14:59:52 +00:00
} else {
cmd_ret
2021-03-09 20:18:57 +00:00
}
} else {
Ok ( None )
}
}
}