add enhanced debugging to the COM for wf200

This commit is contained in:
bunnie 2022-01-14 15:27:24 +08:00
parent a4bb889a5d
commit 4f9374e860
10 changed files with 156 additions and 5 deletions

2
Cargo.lock generated
View file

@ -459,7 +459,7 @@ dependencies = [
[[package]]
name = "com_rs"
version = "0.1.0"
source = "git+https://github.com/betrusted-io/com_rs.git?rev=a8f282291cbc54ea9c6d6195caab029a1c7abd57#a8f282291cbc54ea9c6d6195caab029a1c7abd57"
source = "git+https://github.com/betrusted-io/com_rs.git?rev=1effb7eaebbec59f7fda4a678e2d36efafc2e673#1effb7eaebbec59f7fda4a678e2d36efafc2e673"
[[package]]
name = "com_rs-ref"

View file

@ -7,7 +7,7 @@ description = "com_rs import package"
# Dependency policy: fully specify dependencies to the minor version number
[dependencies]
com_rs = { git = "https://github.com/betrusted-io/com_rs.git", rev = "a8f282291cbc54ea9c6d6195caab029a1c7abd57" }
com_rs = { git = "https://github.com/betrusted-io/com_rs.git", rev = "1effb7eaebbec59f7fda4a678e2d36efafc2e673" }
#com_rs = { path = "../../../betrusted-ec/sw/com_rs" }

View file

@ -118,6 +118,19 @@ impl WlanStatus {
}
}
#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Default)]
pub struct WlanDebug {
pub tx_errs: u32,
pub drops: u32,
// config(2) - control - alloc_fail(2) - alloc_oversize(2) - alloc_count
pub config: u32,
pub control: u16,
pub alloc_fail: u32,
pub alloc_oversize: u32,
pub alloc_free_count: u16,
}
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub(crate) enum Opcode {
/// Reset the COM link - useful after an EC reset
@ -244,6 +257,9 @@ pub(crate) enum Opcode {
/// wlan: send net packet
WlanSendPacket,
/// wlan: debug infos
WlanDebug,
/// sets the EC-side com interrupt mask
IntSetMask,

View file

@ -467,6 +467,14 @@ impl Com {
Ok(config)
}
pub fn wlan_debug(&self) -> Result<WlanDebug, xous::Error> {
let prealloc = WlanDebug::default();
let mut buf = Buffer::into_buf(prealloc).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, Opcode::WlanDebug.to_u32().expect("WlanDebug failed")).or(Err(xous::Error::InternalError))?;
let response = buf.to_original().expect("Couldn't convert WlanDebug buffer");
Ok(response)
}
pub fn wlan_fetch_packet(&self, pkt: &mut [u8]) -> Result<(), xous::Error> {
if pkt.len() > NET_MTU {
return Err(xous::Error::OutOfMemory)

View file

@ -929,6 +929,38 @@ fn xmain() -> ! {
.expect("couldn't return IntFetchVector");
}
}),
Some(Opcode::WlanDebug) => {
com.txrx(ComState::WLAN_GET_ERRCOUNTS.verb);
let mut tx_errs_16 = [0u16; 2];
let mut drops_16 = [0u16; 2];
tx_errs_16[0] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
tx_errs_16[1] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
drops_16[0] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
drops_16[1] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
com.txrx(ComState::WF200_DEBUG.verb);
let mut config_16 = [0u16; 2];
config_16[0] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
config_16[1] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
let control = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
let mut alloc_fail_16 = [0u16; 2];
alloc_fail_16[0] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
alloc_fail_16[1] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
let mut alloc_oversize_16 = [0u16; 2];
alloc_oversize_16[0] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
alloc_oversize_16[1] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
let alloc_free_count = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let debug = WlanDebug {
tx_errs: from_le_words(tx_errs_16),
drops: from_le_words(drops_16),
config: from_le_words(config_16),
control,
alloc_fail: from_le_words(alloc_fail_16),
alloc_oversize: from_le_words(alloc_oversize_16),
alloc_free_count,
};
buffer.replace(debug).unwrap();
},
None => {error!("unknown opcode"); break},
}
@ -963,7 +995,8 @@ fn parse_version(com: &mut crate::implementation::XousCom) -> u32 {
dst[1] = src.to_le_bytes()[1];
}
// translate u8 array into &str
let revstr = core::str::from_utf8(&rev_bytes[..rev_ret[0] as usize]).expect("gitrev is not valid utf-8");
let len_checked = if rev_ret[0] as usize <= rev_bytes.len() { rev_ret[0] as usize} else {rev_bytes.len()};
let revstr = core::str::from_utf8(&rev_bytes[..len_checked]).unwrap_or("v0.0.0-0-xxxxxxx"); // fake version number for hosted mode
// parse &str -- we do it here not in the EC, because the EC is memory-constrained
// v0.9.5-2-gb3e2868 exemplar template
let hyphenstr: Vec<&str> = revstr[1..].split('-').collect(); // drop the leading 'v' by doing [1..]
@ -982,4 +1015,8 @@ fn parse_version(com: &mut crate::implementation::XousCom) -> u32 {
0
};
(commit & 0xff) as u32 | ((rev & 0xff) as u32) << 8 | (((minor & 0xff) as u32) << 16) | (((major & 0xff) as u32) << 24)
}
fn from_le_words(words: [u16; 2]) -> u32 {
words[0] as u32 | (words[1] as u32) << 16
}

View file

@ -49,6 +49,10 @@ pub(crate) enum Opcode {
PingSetTimeout,
PingGetTimeout,
/// Link Management,
GetIpv4Config,
Reset,
/// [Internal] com llio interrupt callback
ComInterrupt,
/// [Internal] run the network stack code

View file

@ -20,6 +20,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc<AtomicU3
let tt = ticktimer_server::Ticktimer::new().unwrap();
let xns = xous_names::XousNames::new().unwrap();
let mut com = com::Com::new(&xns).unwrap();
let netmgr = net::NetManager::new();
let mut pddb = pddb::Pddb::new();
let self_cid = xous::connect(sid).unwrap();
// give the system some time to boot before trying to run this
@ -53,10 +54,27 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc<AtomicU3
if pddb.is_mounted() && rev_ok {
// the status check code is going to get refactored, so this is a "bare minimum" check
let status = com.wlan_status().unwrap();
if status.link_state != com_rs_ref::LinkState::Connected {
let config = netmgr.get_ipv4_config();
let needs_reconnect =
if status.link_state == com_rs_ref::LinkState::Connected {
if let Some(config) = config { // check that the EC's view of the world is synchronized with our view
// is it enough to just check that the address is the same?
if config.addr != status.ipv4.addr {
true
} else {
false
}
} else {
true
}
} else {
true
};
if needs_reconnect {
log::info!("wlan is not connected, attempting auto-reconnect to known AP list");
if let Ok(ap_list) = pddb.list_keys(AP_DICT_NAME, None) {
com.wlan_leave().expect("couldn't issue leave command"); // leave the previous config to reset state
netmgr.reset();
// TODO: add an SSID scan phase, so we only try to connect to SSIDs that are currently visible.
// for now, just try every single one as a brute force approach.
for ap in ap_list {

View file

@ -1,7 +1,10 @@
#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use xous::CID;
use com::Ipv4Conf;
use xous::{CID, send_message, Message};
use xous_ipc::Buffer;
use num_traits::*;
pub mod protocols;
pub use protocols::*;
@ -42,3 +45,32 @@ impl Drop for NetConn {
// de-allocate those items here. They don't need a reference count because they are object-specific
}
}
pub struct NetManager {
netconn: NetConn,
}
impl NetManager {
pub fn new() -> NetManager {
NetManager {
netconn: NetConn::new(&xous_names::XousNames::new().unwrap()).expect("can't connect to Net Server"),
}
}
pub fn get_ipv4_config(&self) -> Option<Ipv4Conf> {
let storage = Some(Ipv4Conf::default().encode_u16());
let mut buf = Buffer::into_buf(storage).expect("Couldn't convert to memory structure");
buf.lend_mut(self.netconn.conn(), Opcode::GetIpv4Config.to_u32().unwrap()).expect("Couldn't execute GetIpv4Config opcode");
let maybe_config = buf.to_original().expect("couldn't restore config structure");
if let Some(config) = maybe_config {
let ipv4 = Ipv4Conf::decode_u16(&config);
Some(ipv4)
} else {
None
}
}
pub fn reset(&self) {
send_message(
self.netconn.conn(),
Message::new_blocking_scalar(Opcode::Reset.to_usize().unwrap(), 0, 0, 0, 0),
).expect("couldn't send reset");
}
}

View file

@ -950,6 +950,38 @@ fn xmain() -> ! {
}
}
}
Some(Opcode::GetIpv4Config) => {
let mut buffer = unsafe {
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
};
let ser = if let Some(config) = net_config {
Some(config.encode_u16())
} else {
None
};
buffer.replace(ser).expect("couldn't return config");
},
Some(Opcode::Reset) => {
net_config = None;
let neighbor_cache = NeighborCache::new(BTreeMap::new());
let ip_addrs = [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 0)];
let routes = Routes::new(BTreeMap::new());
let device = device::NetPhy::new(&xns);
let medium = device.capabilities().medium;
let mut builder = InterfaceBuilder::new(device)
.ip_addrs(ip_addrs)
.routes(routes);
if medium == Medium::Ethernet {
builder = builder
.ethernet_addr(EthernetAddress::from_bytes(&[0; 6]))
.neighbor_cache(neighbor_cache);
}
iface = builder.finalize();
iface.routes_mut().remove_default_ipv4_route();
dns_allclear_hook.notify();
// question: do we need to clear the UDP and ICMP states?
xous::return_scalar(msg.sender, 1).unwrap();
}
Some(Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
// handle an suspend/resume state stuff here. right now, it's a NOP
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");

View file

@ -132,6 +132,10 @@ impl<'a> ShellCmdApi<'a> for Wlan {
Err(e) => write!(ret, "Error: {:?}", e),
};
}
"debug" => {
let debug = env.com.wlan_debug().expect("couldn't issue debug command");
write!(ret, "{:x?}", debug).unwrap();
}
_ => {
show_help = true;
}