From e8bdf91cc3135c526d1749f06acd3ed9c26f64f3 Mon Sep 17 00:00:00 2001 From: Sam Blenny <68084116+samblenny@users.noreply.github.com> Date: Tue, 6 Jul 2021 02:14:25 +0000 Subject: [PATCH] add wlan shell command This is for turning the WF200 wifi radio on and off, setting the SSID and password, joining an AP, leaving an AP, and checking the AP connection status. Signed-off-by: samblenny --- services/shellchat/src/cmds.rs | 3 + services/shellchat/src/cmds/wlan.rs | 95 +++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 services/shellchat/src/cmds/wlan.rs diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index 5bca20bc6..59a85d3c1 100644 --- a/services/shellchat/src/cmds.rs +++ b/services/shellchat/src/cmds.rs @@ -97,6 +97,7 @@ mod engine; use engine::*; mod console; use console::*; mod memtest; use memtest::*; mod keys; use keys::*; +mod wlan; use wlan::*; //mod fcc; use fcc::*; //mod pds; // dependency of the FCC file @@ -181,6 +182,7 @@ impl CmdEnv { let mut ret = String::<1024>::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 wlan_cmd = Wlan {}; let mut ver_cmd = Ver{}; let mut backlight_cmd = Backlight{}; let mut accel_cmd = Accel{}; @@ -207,6 +209,7 @@ impl CmdEnv { &mut console_cmd, &mut self.memtest_cmd, &mut self.keys_cmd, + &mut wlan_cmd, //&mut self.fcc_cmd, ]; diff --git a/services/shellchat/src/cmds/wlan.rs b/services/shellchat/src/cmds/wlan.rs new file mode 100644 index 000000000..68a3a7876 --- /dev/null +++ b/services/shellchat/src/cmds/wlan.rs @@ -0,0 +1,95 @@ +use crate::{CommonEnv, ShellCmdApi}; +use core::fmt::Write; +use xous_ipc::String; + +#[derive(Debug)] +pub struct Wlan {} + +/** +wlan shell command: +- on: if in off mode, reset WF200 and load firmware, otherwise NOP +- off: disconnect from AP (if joined) and put WF200 in low power standby +- setssid ...: set AP SSID to ... (... can include spaces) +- setpass ...: set AP password to ... (... can include spaces) +- join: if disconnected, connect by WPA2 personal with previously set SSID + and password, otherwise NOP +- leave: if joined, disconnect from AP +- show: show AP connection info (SSID, signal strength) +*/ +impl<'a> ShellCmdApi<'a> for Wlan { + cmd_api!(wlan); // 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 = "wlan [on] [off] [setssid ...] [setpass ...] [join] [leave] [show]"; + let mut show_help = false; + + let mut tokens = args.as_str().unwrap().split(' '); + if let Some(sub_cmd) = tokens.next() { + match sub_cmd { + "on" => { + //env.com.set_wlan_on().unwrap(); + write!(ret, "wlan on").unwrap(); + } + "off" => { + //env.com.set_wlan_off().unwrap(); + write!(ret, "wlan off").unwrap(); + } + "setssid" => { + let mut val = String::<1024>::new(); + join_tokens(&mut val, &mut tokens); + //env.com.set_wlan_ssid(&val).unwrap(); + write!(ret, "wlan setssid {}", val).unwrap(); + } + "setpass" => { + let mut val = String::<1024>::new(); + join_tokens(&mut val, &mut tokens); + //env.com.set_wlan_pass(&val).unwrap(); + write!(ret, "wlan setpass {}", val).unwrap(); + } + "join" => { + //env.com.wlan_join().unwrap(); + write!(ret, "wlan join").unwrap(); + } + "leave" => { + //env.com.wlan_leave().unwrap(); + write!(ret, "wlan leave").unwrap(); + } + "show" => { + //env.com.wlan_show().unwrap(); + write!(ret, "wlan show").unwrap(); + } + _ => { + 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(); + } + } +}