From ca5455635b165099719db1805ce8f3d397e89694 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 11 Jul 2022 04:58:48 +0800 Subject: [PATCH] add some heuristics to improve the connection manager the CM can't always detect if the EC changes state; this is especially true after an update or a forced reset where the state caches are not reliably updated. If no packets are seen for a while, poll the actual state and force a sync-up. Also try to make sure that the interrupts are enabled correctly. --- services/net/Cargo.toml | 2 ++ services/net/src/api.rs | 6 ++++- services/net/src/connection_manager.rs | 37 ++++++++++++++++++-------- services/net/src/main.rs | 17 ++++++++++++ 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/services/net/Cargo.toml b/services/net/Cargo.toml index 88a6274ea..074a6f0d1 100644 --- a/services/net/Cargo.toml +++ b/services/net/Cargo.toml @@ -29,6 +29,8 @@ locales = {path = "../../locales"} # for automatic SSID management and AP list storage pddb = {path = "../pddb"} +xous-semver = "0.1.2" + [dependencies.smoltcp] # some historical notes for development on branches in the future # path = "../../../smoltcp" diff --git a/services/net/src/api.rs b/services/net/src/api.rs index 777ba74b4..bd7c4dbe6 100644 --- a/services/net/src/api.rs +++ b/services/net/src/api.rs @@ -14,6 +14,7 @@ use std::fmt; use std::fmt::Debug; use std::io::Write; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; +use xous_semver::SemVer; // republish this so we can decode the icmpv4 error codes pub use smoltcp::wire::Icmpv4DstUnreachable; @@ -25,7 +26,10 @@ pub const AP_DICT_NAME: &'static str = "wlan.networks"; #[allow(dead_code)] /// minimum revision required for compatibility with Net crate -pub const MIN_EC_REV: u32 = 0x00_09_06_00; +pub const MIN_EC_REV: SemVer = SemVer { + maj: 0, min: 9, rev: 6, extra: 0, + commit: None, +}; /// Dispatch opcodes to the Net crate main loop. #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] diff --git a/services/net/src/connection_manager.rs b/services/net/src/connection_manager.rs index 9037e9043..1a0731cc1 100644 --- a/services/net/src/connection_manager.rs +++ b/services/net/src/connection_manager.rs @@ -10,8 +10,6 @@ use num_traits::*; use std::io::Read; use std::collections::{HashMap, HashSet}; use crate::ComIntSources; -#[cfg(any(target_os = "none", target_os = "xous"))] -use locales::t; #[allow(dead_code)] const BOOT_POLL_INTERVAL_MS: usize = 4_758; // a slightly faster poll during boot so we acquire wifi faster once PDDB is mounted @@ -66,26 +64,20 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc= MIN_EC_REV; - #[cfg(any(target_os = "none", target_os = "xous"))] // don't show this pop-up in hosted mode, it's just annoying and not helpful if !rev_ok { - log::warn!("EC firmware is too old to interoperate with the connec tion manager."); - let mut note = String::from(t!("net.ec_rev_old", xous::LANG)); - note.push_str(&format!("\n\n{}{}.{}.{}+{}", t!("net.ec_current_rev", xous::LANG), maj, min, rev, commits)); - modals.show_notification(¬e, None).unwrap(); + log::warn!("EC rev {} is incompatible with connection manager", ec_rev.to_string()); } let run = Arc::new(AtomicBool::new(rev_ok)); let pumping = Arc::new(AtomicBool::new(false)); let mut mounted = false; let current_interval = Arc::new(AtomicU32::new(BOOT_POLL_INTERVAL_MS as u32)); + let mut intervals_without_activity = 0; let mut wifi_stats_cache: WlanStatus = WlanStatus::from_ipc(WlanStatusIpc::default()); let mut status_subscribers = HashMap::::new(); let mut wifi_state = WifiState::Unknown; @@ -318,9 +310,28 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc interval { log::info!("wlan activity interval timeout"); + intervals_without_activity += 1; if rev_ok { mounted = true; + // heuristic to catch sync problems in the state machine: the cache won't get updated if the EC reset itself otherwise + if intervals_without_activity > 3 { // we'd expect at least an ARP or something... + wifi_stats_cache = com.wlan_status().unwrap(); + if wifi_stats_cache.link_state != com_rs_ref::LinkState::Connected { + log::info!("Link state mismatch: moving state to disconnected"); + wifi_state = WifiState::Disconnected; + ssid_list.clear(); + com.set_ssid_scanning(true).unwrap(); + scan_state = SsidScanState::Scanning; + } else if wifi_stats_cache.ipv4.dhcp != com_rs_ref::DhcpState::Bound { + log::info!("DHCP state mismatch: moving state to disconnected"); + wifi_state = WifiState::Disconnected; + ssid_list.clear(); + com.set_ssid_scanning(true).unwrap(); + scan_state = SsidScanState::Scanning; + } + } + if last_wifi_state == WifiState::Connected && wifi_state != WifiState::Connected { log::debug!("sending disconnect update to subscribers"); // reset the stats cache, and update subscribers that we're disconnected @@ -354,6 +365,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc) { com_int_list.push(ComIntSources::WlanSsidScanUpdate); com_int_list.push(ComIntSources::WlanSsidScanFinished); com_int_list.push(ComIntSources::WfxErr); + com_int_list.push(ComIntSources::Invalid); } fn setup_icmp(iface: &mut Interface::) -> SocketHandle { @@ -1132,6 +1133,13 @@ fn main() -> ! { } } } + ComIntSources::Invalid => { + com.ints_ack(&com_int_list); // ack everything that's pending + // re-enable the interrupts as we intended + let mut ena_list: Vec = vec![]; + set_com_ints(&mut ena_list); + com.ints_enable(&ena_list); + } _ => { log::debug!("Unhandled: {:?}", pending); } @@ -1841,6 +1849,15 @@ fn main() -> ! { } }), Some(Opcode::Reset) => { + // ack any pending ints + com_int_list.clear(); + com.ints_get_active(&mut com_int_list).ok(); + com.ints_ack(&com_int_list); + // re-enable the interrupts as we intended + set_com_ints(&mut com_int_list); + com.ints_enable(&com_int_list); + com_int_list.clear(); + // note: ARP cache isn't reset iface.routes_mut().remove_default_ipv4_route(); dns_allclear_hook.notify();