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-05-07 08:22:06 +00:00
use heapless ::FnvIndexMap ;
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-05-07 08:22:06 +00:00
cb_registrations : heapless ::FnvIndexMap ::< u32 , String ::< 256 > , 8 > ,
trng : trng ::Trng ,
2021-05-20 19:15:13 +00:00
xns : xous_names ::XousNames ,
2021-05-07 08:22:06 +00:00
}
impl CommonEnv {
pub fn register_handler ( & mut self , verb : String ::< 256 > ) -> 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 ) . unwrap ( ) ;
key
}
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-25 17:11:45 +00:00
mod ver ; use ver ::* ;
2021-05-03 18:22:35 +00:00
mod audio ; use audio ::* ;
2021-05-15 20:29:12 +00:00
mod backlight ; use backlight ::* ;
2021-05-16 08:52:27 +00:00
mod accel ; use accel ::* ;
2021-05-20 19:15:13 +00:00
mod sha ; use sha ::* ;
2021-05-23 20:34:50 +00:00
mod ecup ; use ecup ::* ;
2021-06-03 16:49:34 +00:00
mod aes ; use aes ::* ;
2021-06-08 13:29:23 +00:00
mod trng_cmd ; use trng_cmd ::* ;
2021-06-23 19:17:32 +00:00
mod engine ; use engine ::* ;
2021-04-16 15:07:31 +00:00
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-05-03 18:22:35 +00:00
audio_cmd : Audio ,
2021-05-20 19:15:13 +00:00
sha_cmd : Sha ,
2021-05-23 20:34:50 +00:00
ecup_cmd : EcUpdate ,
2021-06-03 16:49:34 +00:00
aes_cmd : Aes ,
2021-06-08 13:29:23 +00:00
trng_cmd : TrngCmd ,
2021-06-23 19:17:32 +00:00
engine_cmd : Engine ,
2021-04-16 15:07:31 +00:00
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-05-08 15:55:08 +00:00
let mut common = CommonEnv {
llio : llio ::Llio ::new ( & xns ) . expect ( " couldn't connect to LLIO " ) ,
com : com ::Com ::new ( & xns ) . expect ( " could't connect to COM " ) ,
ticktimer : ticktimer ,
gam : gam ::Gam ::new ( & xns ) . expect ( " couldn't connect to GAM " ) ,
cb_registrations : FnvIndexMap ::new ( ) ,
trng : trng ::Trng ::new ( & xns ) . unwrap ( ) ,
2021-05-20 19:15:13 +00:00
xns : xous_names ::XousNames ::new ( ) . unwrap ( ) ,
2021-05-08 15:55:08 +00:00
} ;
let fcc = Fcc ::new ( & mut common ) ;
2021-05-20 19:15:13 +00:00
let sha = Sha ::new ( & xns , & mut common ) ;
2021-06-03 16:49:34 +00:00
let aes = Aes ::new ( & xns , & mut common ) ;
2021-05-23 20:34:50 +00:00
let ecup = EcUpdate ::new ( & mut common ) ;
2021-06-23 19:17:32 +00:00
let engine = Engine ::new ( & xns , & mut common ) ;
2021-06-21 14:18:39 +00:00
// print our version info
let ( maj , min , rev , extra , gitrev ) = common . llio . soc_gitrev ( ) . unwrap ( ) ;
log ::info! ( " SoC git rev {}.{}.{}+{} commit {:x} " , maj , min , rev , extra , gitrev ) ;
log ::info! ( " SoC DNA: 0x{:x} " , common . llio . soc_dna ( ) . unwrap ( ) ) ;
let ( rev , dirty ) = common . com . get_ec_git_rev ( ) . unwrap ( ) ;
let dirtystr = if dirty { " dirty " } else { " clean " } ;
log ::info! ( " EC git commit: {:x}, {} " , rev , dirtystr ) ;
let ( maj , min , rev ) = common . com . get_wf200_fw_rev ( ) . unwrap ( ) ;
log ::info! ( " WF200 fw rev {}.{}.{} " , maj , min , rev ) ;
2021-03-10 17:45:02 +00:00
CmdEnv {
2021-05-08 15:55:08 +00:00
common_env : common ,
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-22 10:08:19 +00:00
sleep_cmd : Sleep ::new ( & xns ) ,
2021-04-06 11:32:35 +00:00
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-06-16 17:12:25 +00:00
vibe_cmd : Vibe ::new ( ) ,
2021-04-16 15:07:31 +00:00
ssid_cmd : Ssid ::new ( ) ,
2021-05-03 18:22:35 +00:00
audio_cmd : Audio ::new ( & xns ) ,
2021-05-20 19:15:13 +00:00
sha_cmd : sha ,
2021-05-23 20:34:50 +00:00
ecup_cmd : ecup ,
2021-06-03 16:49:34 +00:00
aes_cmd : aes ,
2021-06-08 13:29:23 +00:00
trng_cmd : TrngCmd ::new ( ) ,
2021-06-23 19:17:32 +00:00
engine_cmd : engine ,
2021-04-16 15:07:31 +00:00
2021-05-08 15:55:08 +00:00
fcc_cmd : fcc ,
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-04-25 17:11:45 +00:00
let mut ver_cmd = Ver { } ;
2021-05-15 20:29:12 +00:00
let mut backlight_cmd = Backlight { } ;
2021-05-16 08:52:27 +00:00
let mut accel_cmd = Accel { } ;
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-25 17:11:45 +00:00
& mut ver_cmd ,
2021-05-03 18:22:35 +00:00
& mut self . audio_cmd ,
2021-05-15 20:29:12 +00:00
& mut backlight_cmd ,
2021-05-16 08:52:27 +00:00
& mut accel_cmd ,
2021-05-20 19:15:13 +00:00
& mut self . sha_cmd ,
2021-05-23 20:34:50 +00:00
& mut self . ecup_cmd ,
2021-06-03 16:49:34 +00:00
& mut self . aes_cmd ,
2021-06-08 13:29:23 +00:00
& mut self . trng_cmd ,
2021-06-23 19:17:32 +00:00
& mut self . engine_cmd ,
2021-04-16 15:07:31 +00:00
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 ) ;
2021-05-07 08:22:06 +00:00
// 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 . to_str ( )
} ,
None = > {
self . lastverb . to_str ( )
2021-03-17 20:22:17 +00:00
}
2021-05-07 08:22:06 +00:00
} ;
// 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 {
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
}
}