diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index 59a85d3c1..43aded60f 100644 --- a/services/shellchat/src/cmds.rs +++ b/services/shellchat/src/cmds.rs @@ -98,6 +98,7 @@ mod console; use console::*; mod memtest; use memtest::*; mod keys; use keys::*; mod wlan; use wlan::*; +mod ip; use ip::*; //mod fcc; use fcc::*; //mod pds; // dependency of the FCC file @@ -183,6 +184,7 @@ impl CmdEnv { 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 wlan_cmd = Wlan {}; + let mut ip_cmd = Ip {}; let mut ver_cmd = Ver{}; let mut backlight_cmd = Backlight{}; let mut accel_cmd = Accel{}; @@ -210,6 +212,7 @@ impl CmdEnv { &mut self.memtest_cmd, &mut self.keys_cmd, &mut wlan_cmd, + &mut ip_cmd, //&mut self.fcc_cmd, ]; diff --git a/services/shellchat/src/cmds/ip.rs b/services/shellchat/src/cmds/ip.rs new file mode 100644 index 000000000..67976f09d --- /dev/null +++ b/services/shellchat/src/cmds/ip.rs @@ -0,0 +1,90 @@ +use crate::{CommonEnv, ShellCmdApi}; +use core::fmt::Write; +use xous_ipc::String; + +#[derive(Debug)] +pub struct Ip {} + +/** +ip shell command: +- list: show ip address, netmask, gateway, dns server, etc. +- ping host: send 3 ICMP pings to host (ip/hostname) +- udptx host port ...: send a UDP packet with string ... to host:port +- udprx: listen on udp localhost:port and echo payload of first incoming + packet (5s timeout) +*/ +impl<'a> ShellCmdApi<'a> for Ip { + cmd_api!(ip); // inserts boilerplate for command API + + fn process( + &mut self, + args: String<1024>, + _env: &mut CommonEnv, + ) -> Result>, xous::Error> { + let mut ret = String::<1024>::new(); + let helpstring = "ip [list] [ping host] [udptx host port ...] [udprx port]"; + let mut show_help = false; + + let mut tokens = args.as_str().unwrap().split(' '); + if let Some(sub_cmd) = tokens.next() { + match sub_cmd { + "list" => { + //env.com.ip_list().unwrap(); + write!(ret, "ip list").unwrap(); + } + "ping" => { + if let Some(host) = tokens.next() { + //env.com.ip_ping(&host).unwrap(); + write!(ret, "ip ping {}", host).unwrap(); + } else { + show_help = true; + } + } + "udptx" => { + if let (Some(host), Some(port)) = (tokens.next(), tokens.next()) { + let mut val = String::<1024>::new(); + join_tokens(&mut val, &mut tokens); + //env.com.ip_udptx(&host, &port, &val).unwrap(); + write!(ret, "ip udptx {} {} {}", host, port, val).unwrap(); + } else { + show_help = true; + } + } + "udprx" => { + if let Some(port) = tokens.next() { + //env.com.ip_updprx(&port).unwrap(); + write!(ret, "ip udprx {}", port).unwrap(); + } else { + show_help = true; + } + } + _ => { + show_help = true; + } + } + } else { + show_help = true; + } + if show_help { + write!(ret, "{}", helpstring).unwrap(); + } + Ok(Some(ret)) + } +} + +/** +Join an iterator of string tokens with spaces. + +This is intended to reverse the effect of .split(' ') in the context of a very simple +command parser. This is a lazy way to avoid building a parser for quoted strings, since +SSIDs or passwords might include spaces. +*/ +fn join_tokens<'a>(buf: &mut String<1024>, tokens: impl Iterator) { + for (i, tok) in tokens.enumerate() { + if i == 0 { + write!(buf, "{}", tok).unwrap(); + } else { + write!(buf, " {}", tok).unwrap(); + } + } +}