From 3f2f304202d6114ee8a4175a88452c3a013092e6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 27 Jan 2022 13:46:43 +0800 Subject: [PATCH] refactor connection manager to be more responsive to suspend/resume --- services/net/src/connection_manager.rs | 95 ++++++++++++++++++-------- services/susres/src/main.rs | 2 +- 2 files changed, 67 insertions(+), 30 deletions(-) diff --git a/services/net/src/connection_manager.rs b/services/net/src/connection_manager.rs index 90280e11f..7516473c5 100644 --- a/services/net/src/connection_manager.rs +++ b/services/net/src/connection_manager.rs @@ -1,10 +1,10 @@ use crate::api::*; use std::sync::Arc; -use core::sync::atomic::{AtomicU32, Ordering}; +use core::sync::atomic::{AtomicU32, AtomicBool, Ordering}; use com::{WlanStatus, WlanStatusIpc}; use com_rs_ref::{ConnectResult, LinkState}; use net::MIN_EC_REV; -use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, Message}; +use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, try_send_message, Message}; use xous_ipc::Buffer; use num_traits::*; use std::io::Read; @@ -30,6 +30,11 @@ pub(crate) enum ConnectionManagerOpcode { SuspendResume, Quit, } +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +enum PumpOp { + Pump, + Quit, +} #[derive(Eq, PartialEq, Copy, Clone, Debug)] enum WifiState { @@ -72,9 +77,10 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc::new(); let mut wifi_state = WifiState::Unknown; @@ -83,6 +89,40 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc::new(); let mut wait_count = 0; + let run_sid = xous::create_server().unwrap(); + let run_cid = xous::connect(run_sid).unwrap(); + let _ = std::thread::spawn({ + let run = run.clone(); + let sid = run_sid.clone(); + let main_cid = self_cid.clone(); + let self_cid = run_cid.clone(); + let interval = current_interval.clone(); + let pumping = pumping.clone(); + move || { + let tt = ticktimer_server::Ticktimer::new().unwrap(); + loop { + let msg = xous::receive_message(sid).unwrap(); + match FromPrimitive::from_usize(msg.body.id()) { + Some(PumpOp::Pump) => msg_scalar_unpack!(msg, _, _, _, _, { + if run.load(Ordering::SeqCst) { + pumping.store(true, Ordering::SeqCst); + try_send_message(main_cid, Message::new_scalar(ConnectionManagerOpcode::Poll.to_usize().unwrap(), 0, 0, 0, 0)).ok(); + tt.sleep_ms(interval.load(Ordering::SeqCst) as usize).unwrap(); + send_message(self_cid, Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); + pumping.store(false, Ordering::SeqCst); + } + }), + Some(PumpOp::Quit) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { + xous::return_scalar(msg.sender, 1).ok(); + break; + }), + _ => log::error!("Unrecognized message: {:?}", msg), + } + } + xous::destroy_server(sid).unwrap(); + } + }); + let mut susres = susres::Susres::new( Some(susres::SuspendOrder::Early), &xns, ConnectionManagerOpcode::SuspendResume as u32, self_cid).expect("couldn't create suspend/resume object"); @@ -90,7 +130,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc msg_scalar_unpack!(msg, _, _, _, _, { - if !run { - run = true; - send_message(self_cid, Message::new_scalar(ConnectionManagerOpcode::Poll.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't kick off next poll"); - } - }), Some(ConnectionManagerOpcode::ComInt) => msg_scalar_unpack!(msg, ints, raw_arg, 0, 0, { log::debug!("debug: {:x}, {:x}", ints, raw_arg); let mut mask_bit: u16 = 1; @@ -231,7 +265,8 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc msg_scalar_unpack!(msg, _, _, _, _, { - if activity_interval.fetch_add(current_interval as u32, Ordering::SeqCst) > current_interval as u32 { + // heh. this probably should be rewritten to be a bit more thread-safe if we had a multi-core CPU we're running on. but we're single-core so... + if activity_interval.fetch_add(current_interval.load(Ordering::SeqCst) as u32, Ordering::SeqCst) > current_interval.load(Ordering::SeqCst) as u32 { log::info!("wlan activity interval timeout"); // if the pddb isn't mounted, don't even bother checking -- we can't connect until we have a place to get keys if pddb.is_mounted() && rev_ok { @@ -288,18 +323,18 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc { log::debug!("got error on connect, resetting wifi chip"); com.wifi_reset().expect("couldn't reset the wf200 chip"); - netmgr.reset(); + netmgr.reset(); // this can result in a suspend failure, but the suspend timeout is currently set long enough to accommodate this possibility wifi_state = WifiState::Disconnected; if scan_state == SsidScanState::Idle { com.set_ssid_scanning(true).unwrap(); scan_state = SsidScanState::Scanning; } - send_message(self_cid, Message::new_scalar(ConnectionManagerOpcode::Poll.to_usize().unwrap(), current_interval, 0, 0, 0)).expect("couldn't kick off next poll"); + send_message(self_cid, Message::new_scalar(ConnectionManagerOpcode::Poll.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't kick off next poll"); } WifiState::Connected => { // this is the "rare" path -- it's if we connected and not much is going on, so we timeout and hit this ping @@ -333,14 +368,10 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc { @@ -366,13 +397,19 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc msg_blocking_scalar_unpack!(msg, _, _, _, _, { - run = false; - xous::return_scalar(msg.sender, 0).expect("couldn't ack stop"); + Some(ConnectionManagerOpcode::Run) => msg_scalar_unpack!(msg, _, _, _, _, { + if !run.swap(true, Ordering::SeqCst) { + if !pumping.load(Ordering::SeqCst) { // avoid having multiple pump messages being sent if a user tries to rapidly toggle the run/stop switch + send_message(run_cid, Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't kick off next poll"); + } + } + }), + Some(ConnectionManagerOpcode::Stop) => msg_scalar_unpack!(msg, _, _, _, _, { + run.store(false, Ordering::SeqCst); }), Some(ConnectionManagerOpcode::Quit) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { + send_message(run_cid, Message::new_blocking_scalar(PumpOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't tell Pump to quit"); + unsafe{xous::disconnect(run_cid).ok()}; xous::return_scalar(msg.sender, 0).unwrap(); log::warn!("exiting connection manager"); break; diff --git a/services/susres/src/main.rs b/services/susres/src/main.rs index 37aeb0db9..bc814017f 100644 --- a/services/susres/src/main.rs +++ b/services/susres/src/main.rs @@ -469,7 +469,7 @@ enum TimeoutOpcode { Drop, } -static TIMEOUT_TIME: AtomicU32 = AtomicU32::new(2000); +static TIMEOUT_TIME: AtomicU32 = AtomicU32::new(5000); // this is gated by the possibility that an EC reset was called just as a suspend was initiated. EC reset takes about 3500ms static TIMEOUT_CONN: AtomicU32 = AtomicU32::new(0); pub fn timeout_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);