2021-03-26 12:22:33 +00:00
use xous ::{ MessageEnvelope } ;
use xous_ipc ::String ;
2021-03-09 20:18:57 +00:00
use core ::fmt ::Write ;
2021-03-11 14:54:24 +00:00
/////////////////////////// Common items to all commands
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-11 14:54:24 +00:00
fn process ( & mut self , args : String ::< 1024 > , env : & mut CommonEnv ) -> Result < Option < String ::< 1024 > > , xous ::Error > ;
2021-03-17 20:22:17 +00:00
// 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 ::< 1024 > > , xous ::Error > {
2021-04-09 17:01:51 +00:00
log ::info! ( " received unhandled message {:?} " , msg ) ;
2021-03-17 20:22:17 +00:00
Ok ( None )
}
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-17 20:22:17 +00:00
2021-03-11 14:54:24 +00:00
/////////////////////////// Command shell integration
2021-03-10 17:45:02 +00:00
#[ derive(Debug) ]
pub struct CommonEnv {
2021-04-06 11:32:35 +00:00
llio : llio ::Llio ,
2021-04-05 15:50:00 +00:00
com : com ::Com ,
ticktimer : ticktimer_server ::Ticktimer ,
gam : gam ::Gam ,
2021-03-10 17:45:02 +00:00
}
2021-03-11 14:54:24 +00:00
/*
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 ::* ;
2021-04-08 21:24:29 +00:00
//mod test; use test::*;
2021-04-06 11:32:35 +00:00
mod sleep ; use sleep ::* ;
mod sensors ; use sensors ::* ;
2021-04-09 17:01:51 +00:00
mod callback ; use callback ::* ;
2021-04-07 20:38:01 +00:00
mod rtc_cmd ; use rtc_cmd ::* ;
2021-04-16 11:27:48 +00:00
mod vibe ; use vibe ::* ;
2021-04-16 15:07:31 +00:00
mod ssid ; use ssid ::* ;
2021-04-14 20:10:47 +00:00
mod fcc ; use fcc ::* ;
mod pds ; // dependency of the FCC file
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 ,
2021-03-17 20:22:17 +00:00
lastverb : String ::< 256 > ,
2021-03-11 14:54:24 +00:00
///// 2. declare storage for your command here.
2021-04-08 21:24:29 +00:00
//test_cmd: Test,
2021-04-06 11:32:35 +00:00
sleep_cmd : Sleep ,
sensors_cmd : Sensors ,
2021-04-09 17:01:51 +00:00
callback_cmd : CallBack ,
2021-04-07 20:38:01 +00:00
rtc_cmd : RtcCmd ,
2021-04-16 11:27:48 +00:00
vibe_cmd : Vibe ,
2021-04-16 15:07:31 +00:00
ssid_cmd : Ssid ,
2021-04-14 20:10:47 +00:00
fcc_cmd : Fcc ,
2021-03-09 20:18:57 +00:00
}
impl CmdEnv {
2021-04-05 15:50:00 +00:00
pub fn new ( xns : & xous_names ::XousNames ) -> CmdEnv {
2021-03-28 10:14:41 +00:00
let ticktimer = ticktimer_server ::Ticktimer ::new ( ) . expect ( " Couldn't connect to Ticktimer " ) ;
2021-03-10 17:45:02 +00:00
CmdEnv {
common_env : CommonEnv {
2021-04-09 17:01:51 +00:00
llio : llio ::Llio ::new ( & xns ) . expect ( " couldn't connect to LLIO " ) ,
com : com ::Com ::new ( & xns ) . expect ( " could't connect to COM " ) ,
2021-03-28 10:14:41 +00:00
ticktimer : ticktimer ,
2021-04-09 17:01:51 +00:00
gam : gam ::Gam ::new ( & xns ) . expect ( " couldn't connect to GAM " ) ,
2021-03-10 17:45:02 +00:00
} ,
2021-03-17 20:22:17 +00:00
lastverb : String ::< 256 > ::new ( ) ,
2021-03-11 14:54:24 +00:00
///// 3. initialize your storage, by calling new()
2021-04-08 21:24:29 +00:00
//test_cmd: Test::new(),
2021-04-06 11:32:35 +00:00
sleep_cmd : Sleep ::new ( ) ,
sensors_cmd : Sensors ::new ( ) ,
2021-04-09 17:01:51 +00:00
callback_cmd : CallBack ::new ( ) ,
2021-04-07 20:38:01 +00:00
rtc_cmd : RtcCmd ::new ( & xns ) ,
2021-04-16 11:27:48 +00:00
vibe_cmd : Vibe ::new ( & xns ) ,
2021-04-16 15:07:31 +00:00
ssid_cmd : Ssid ::new ( ) ,
2021-04-14 20:10:47 +00:00
fcc_cmd : Fcc ::new ( ) ,
2021-03-10 17:45:02 +00:00
}
}
2021-03-17 20:22:17 +00:00
pub fn dispatch ( & mut self , maybe_cmdline : Option < & mut String ::< 1024 > > , maybe_callback : Option < & MessageEnvelope > ) -> Result < Option < String ::< 1024 > > , xous ::Error > {
2021-03-10 17:45:02 +00:00
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-11 14:54:24 +00:00
///// 4. add your command to this array, so that it can be looked up and dispatched
2021-03-10 18:57:58 +00:00
& mut echo_cmd ,
2021-04-08 21:24:29 +00:00
//&mut self.test_cmd,
2021-04-06 11:32:35 +00:00
& mut self . sleep_cmd ,
& mut self . sensors_cmd ,
2021-04-09 17:01:51 +00:00
& mut self . callback_cmd ,
2021-04-07 20:38:01 +00:00
& mut self . rtc_cmd ,
2021-04-16 11:27:48 +00:00
& mut self . vibe_cmd ,
2021-04-16 15:07:31 +00:00
& mut self . ssid_cmd ,
2021-04-14 20:10:47 +00:00
& mut self . fcc_cmd ,
2021-03-10 14:59:52 +00:00
] ;
2021-03-09 20:18:57 +00:00
2021-03-17 20:22:17 +00:00
if let Some ( cmdline ) = maybe_cmdline {
let maybe_verb = tokenize ( cmdline ) ;
2021-03-09 20:18:57 +00:00
2021-03-17 20:22:17 +00:00
let mut cmd_ret : Result < Option < String ::< 1024 > > , xous ::Error > = Ok ( None ) ;
if let Some ( verb_string ) = maybe_verb {
let verb = verb_string . to_str ( ) ;
2021-03-09 20:18:57 +00:00
2021-03-17 20:22:17 +00:00
// 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 " ) ;
} ;
}
2021-03-10 14:59:52 +00:00
2021-03-17 20:22:17 +00:00
// 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 ;
2021-03-10 14:59:52 +00:00
}
2021-03-17 20:22:17 +00:00
Ok ( Some ( ret ) )
} else {
cmd_ret
2021-03-10 14:59:52 +00:00
}
} else {
2021-03-17 20:22:17 +00:00
Ok ( None )
}
} else if let Some ( callback ) = maybe_callback {
let mut cmd_ret : Result < Option < String ::< 1024 > > , 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 ;
} ;
}
2021-03-10 14:59:52 +00:00
cmd_ret
2021-03-17 20:22:17 +00:00
} else {
Ok ( None )
2021-03-09 20:18:57 +00:00
}
} else {
Ok ( None )
}
}
2021-03-11 14:54:24 +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
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
}
}