diff --git a/imports/com_rs-ref/Cargo.toml b/imports/com_rs-ref/Cargo.toml index e8bf80019..607343de8 100644 --- a/imports/com_rs-ref/Cargo.toml +++ b/imports/com_rs-ref/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" description = "com_rs import package" [dependencies] -com_rs = { git = "https://github.com/betrusted-io/com_rs.git", rev = "4b45a42ad2d932550566d6c32ec1e1dd60e93c67" } +com_rs = { git = "https://github.com/betrusted-io/com_rs.git", rev = "8959b373adc359f03a0da7c000bd8208b47d90a7" } #com_rs = { path = "../../../betrusted-ec/sw/com_rs" } diff --git a/services/com/src/api.rs b/services/com/src/api.rs index b5ea4318f..210cffe31 100644 --- a/services/com/src/api.rs +++ b/services/com/src/api.rs @@ -136,6 +136,27 @@ pub(crate) enum Opcode { /// suspend/resume callback SuspendResume, + + /// wlan: make sure radio is on (reset from standby if needed) + WlanOn, + + /// wlan: switch radio to lowest power standby mode + WlanOff, + + /// wlan: set SSID to use for joining AP + WlanSetSSID, + + /// wlan: set password to use for joining AP + WlanSetPass, + + /// wlan: join AP using previously set SSID & password + WlanJoin, + + /// wlan: disconnect from AP + WlanLeave, + + /// wlan: get wlan radio status (power state? connected? AP info?) + WlanStatus, } /// These enums indicate what kind of callback type we're sending. diff --git a/services/com/src/lib.rs b/services/com/src/lib.rs index e8cd9f105..05ae89f4d 100644 --- a/services/com/src/lib.rs +++ b/services/com/src/lib.rs @@ -301,6 +301,72 @@ impl Com { } } } + + pub fn wlan_set_on(&mut self) -> Result { + send_message( + self.conn, + Message::new_scalar(Opcode::WlanOn.to_usize().unwrap(), 0, 0, 0, 0), + ) + } + + pub fn wlan_set_off(&mut self) -> Result { + send_message( + self.conn, + Message::new_scalar(Opcode::WlanOff.to_usize().unwrap(), 0, 0, 0, 0), + ) + } + + pub fn wlan_set_ssid(&mut self, s: &String<1024>) -> Result { + use core::fmt::Write; + // Enforce WF200 driver API length limit + const WF200_SSID_MAX_LEN: usize = 32; + if s.len() > WF200_SSID_MAX_LEN { + return Err(xous::Error::InvalidString); + } + let mut copy: String:: = String::new(); + let _ = write!(copy, "{}", s); + let buf = Buffer::into_buf(copy).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::WlanSetSSID.to_u32().unwrap()) + } + + pub fn wlan_set_pass(&mut self, s: &String<1024>) -> Result { + use core::fmt::Write; + // Enforce WF200 driver API length limit + const WF200_PASS_MAX_LEN: usize = 64; + if s.len() > WF200_PASS_MAX_LEN { + return Err(xous::Error::InvalidString); + } + let mut copy: String:: = String::new(); + let _ = write!(copy, "{}", s); + let buf = Buffer::into_buf(copy).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::WlanSetPass.to_u32().unwrap()) + } + + pub fn wlan_join(&mut self) -> Result { + // TODO: how to make this return success/fail status from WF200? + send_message( + self.conn, + Message::new_scalar(Opcode::WlanJoin.to_usize().unwrap(), 0, 0, 0, 0), + ) + } + + pub fn wlan_leave(&mut self) -> Result { + send_message( + self.conn, + Message::new_scalar(Opcode::WlanLeave.to_usize().unwrap(), 0, 0, 0, 0), + ) + } + + pub fn wlan_status(&mut self) -> Result, xous::Error> { + // TODO: how to make this return IP, netmask, gateway, DNS server, and STA MAC? + const STATUS_MAX_LEN: usize = 160; + let status = xous_ipc::String::::new(); + let mut buf = Buffer::into_buf(status).or(Err(xous::Error::InternalError))?; + buf.lend_mut(self.conn, Opcode::WlanStatus.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + let response = buf.to_original::, _>().unwrap(); + Ok(response) + } + } use core::sync::atomic::{AtomicU32, Ordering}; diff --git a/services/com/src/main.rs b/services/com/src/main.rs index a0b8837f1..6d6901822 100644 --- a/services/com/src/main.rs +++ b/services/com/src/main.rs @@ -10,6 +10,7 @@ use log::{error, info, trace}; use com_rs_ref as com_rs; use com_rs::*; +use com_rs::serdes::{STR_32_WORDS, STR_64_WORDS, STR_64_U8_SIZE, StringSer, StringDes}; use xous::{CID, msg_scalar_unpack, msg_blocking_scalar_unpack}; use xous_ipc::{Buffer, String}; @@ -576,6 +577,73 @@ fn xmain() -> ! { } buffer.replace(ssid_str).unwrap(); } + Some(Opcode::WlanOn) => { + info!("TODO: implement WlanOn"); + com.txrx(ComState::WLAN_ON.verb); + } + Some(Opcode::WlanOff) => { + info!("TODO: implement WlanOff"); + com.txrx(ComState::WLAN_OFF.verb); + } + Some(Opcode::WlanSetSSID) => { + const WF200_SSID_LEN: usize = 32; + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let ssid = buffer.to_original::, _>().unwrap(); + info!("WlanSetSSID: {}", ssid); + let mut str_ser = StringSer::::new(); + match str_ser.encode(&ssid.to_str()) { + Ok(tx_words) => { + com.txrx(ComState::WLAN_SET_SSID.verb); + for w in tx_words.iter() { + com.txrx(*w); + } + } + _ => info!("WlanSetSSID FAIL"), + } + } + Some(Opcode::WlanSetPass) => { + const WF200_PASS_LEN: usize = 64; + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let pass = buffer.to_original::, _>().unwrap(); + info!("WlanSetPass: {}", pass); + let mut str_ser = StringSer::::new(); + match str_ser.encode(&pass.to_str()) { + Ok(tx_words) => { + com.txrx(ComState::WLAN_SET_PASS.verb); + for w in tx_words.iter() { + com.txrx(*w); + } + } + _ => info!("WlanSetPASS FAIL"), + } + } + Some(Opcode::WlanJoin) => { + info!("TODO: implement WlanJoin"); + com.txrx(ComState::WLAN_JOIN.verb); + } + Some(Opcode::WlanLeave) => { + info!("TODO: implement WlanLeave"); + com.txrx(ComState::WLAN_LEAVE.verb); + } + Some(Opcode::WlanStatus) => { + let mut buffer = unsafe { + Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) + }; + com.txrx(ComState::WLAN_STATUS.verb); + let mut rx_buf: [u16; STR_64_WORDS] = [0; STR_64_WORDS]; + for dest in rx_buf.iter_mut() { + *dest = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)); + } + let mut des = StringDes::::new(); + match des.decode_u16(&rx_buf) { + Ok(status) => { + info!("status: {}", status); + let status_str = String::::from_str(&status); + let _ = buffer.replace(status_str); + } + _ => info!("status decode failed"), + }; + } None => {error!("unknown opcode"); break}, } diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index 7642a8042..2c572124d 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 jtag_cmd; use jtag_cmd::*; //mod fcc; use fcc::*; @@ -184,6 +185,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{}; @@ -210,6 +212,7 @@ impl CmdEnv { &mut console_cmd, &mut self.memtest_cmd, &mut self.keys_cmd, + &mut wlan_cmd, &mut self.jtag_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..3ea702ce9 --- /dev/null +++ b/services/shellchat/src/cmds/wlan.rs @@ -0,0 +1,113 @@ +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 +- status: get wlan radio status (power state? connected? AP info?) +*/ +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] [status]"; + let mut show_help = false; + + let mut tokens = args.as_str().unwrap().split(' '); + if let Some(sub_cmd) = tokens.next() { + match sub_cmd { + "on" => { + let _ = match env.com.wlan_set_on() { + Ok(_) => write!(ret, "wlan on"), + Err(e) => write!(ret, "Error: {:?}", e), + }; + } + "off" => { + let _ = match env.com.wlan_set_off() { + Ok(_) => write!(ret, "wlan off"), + Err(e) => write!(ret, "Error: {:?}", e), + }; + } + "setssid" => { + let mut val = String::<1024>::new(); + join_tokens(&mut val, &mut tokens); + if val.len() == 0 { + let _ = write!(ret, "Error: SSID too short"); + } else { + let _ = match env.com.wlan_set_ssid(&val) { + Ok(_) => write!(ret, "wlan setssid {}", val), + Err(_) => write!(ret, "Error: SSID too long for WF200"), + }; + } + } + "setpass" => { + let mut val = String::<1024>::new(); + join_tokens(&mut val, &mut tokens); + let _ = match env.com.wlan_set_pass(&val) { + Ok(_) => write!(ret, "wlan setpass {}", val), + Err(_) => write!(ret, "Error: passphrase too long for WF200"), + }; + } + "join" => { + let _ = match env.com.wlan_join() { + Ok(_) => write!(ret, "wlan join"), + Err(e) => write!(ret, "Error: {:?}", e), + }; + } + "leave" => { + let _ = match env.com.wlan_leave() { + Ok(_) => write!(ret, "wlan leave"), + Err(e) => write!(ret, "Error: {:?}", e), + }; + } + "status" => { + let _ = match env.com.wlan_status() { + Ok(msg) => write!(ret, "{}", msg), + Err(e) => write!(ret, "Error: {:?}", e), + }; + } + _ => { + show_help = true; + } + } + } else { + show_help = true; + } + if show_help { + let _ = write!(ret, "{}", helpstring); + } + 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(); + } + } +}