2022-01-09 06:03:20 +00:00
use crate ::api ::* ;
use std ::sync ::Arc ;
use core ::sync ::atomic ::{ AtomicU32 , Ordering } ;
2022-01-22 10:25:39 +00:00
use com ::{ WlanStatus , WlanStatusIpc } ;
2022-01-13 18:23:57 +00:00
use net ::MIN_EC_REV ;
2022-01-09 06:03:20 +00:00
use xous ::{ msg_blocking_scalar_unpack , msg_scalar_unpack , send_message , Message } ;
2022-01-22 10:25:39 +00:00
use xous_ipc ::Buffer ;
2022-01-09 06:03:20 +00:00
use num_traits ::* ;
use std ::io ::Read ;
2022-01-22 16:24:45 +00:00
use std ::collections ::HashMap ;
#[ allow(dead_code) ]
const BOOT_POLL_INTERVAL_MS : usize = 5_758 ; // a slightly faster poll during boot so we acquire wifi faster once PDDB is mounted
/// this is shared externally so other functions (e.g. in status bar) that want to query the net manager know how long to back off, otherwise the status query will block
#[ allow(dead_code) ]
const POLL_INTERVAL_MS : usize = 20_151 ; // stagger slightly off of an integer-seconds interval to even out loads
2022-01-09 06:03:20 +00:00
#[ derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug) ]
pub ( crate ) enum ConnectionManagerOpcode {
Run ,
Poll ,
Stop ,
2022-01-22 16:24:45 +00:00
SubscribeWifiStats ,
UnsubWifiStats ,
2022-01-09 06:03:20 +00:00
Quit ,
}
pub ( crate ) fn connection_manager ( sid : xous ::SID , activity_interval : Arc < AtomicU32 > ) {
let tt = ticktimer_server ::Ticktimer ::new ( ) . unwrap ( ) ;
let xns = xous_names ::XousNames ::new ( ) . unwrap ( ) ;
let mut com = com ::Com ::new ( & xns ) . unwrap ( ) ;
2022-01-14 07:27:24 +00:00
let netmgr = net ::NetManager ::new ( ) ;
2022-01-09 06:03:20 +00:00
let mut pddb = pddb ::Pddb ::new ( ) ;
let self_cid = xous ::connect ( sid ) . unwrap ( ) ;
2022-01-22 10:25:39 +00:00
// give the system some time to boot before trying to run a check on the EC minimum version, as it is in reset on boot
2022-01-09 06:03:20 +00:00
tt . sleep_ms ( POLL_INTERVAL_MS ) . unwrap ( ) ;
2022-01-13 18:23:57 +00:00
// check that the EC rev meets the minimum version for this service to function
// otherwise, we could crash the EC before it can update itself.
let ( maj , min , rev , commits ) = com . get_ec_sw_tag ( ) . unwrap ( ) ;
let ec_rev = ( maj as u32 ) < < 24 | ( min as u32 ) < < 16 | ( rev as u32 ) < < 8 | commits as u32 ;
let rev_ok = ec_rev > = MIN_EC_REV ;
if ! rev_ok {
log ::warn! ( " EC firmware is too old to interoperate with the connection manager. " ) ;
}
let mut run = rev_ok ;
2022-01-15 07:13:51 +00:00
let mut mounted = false ;
let mut current_interval = BOOT_POLL_INTERVAL_MS ;
2022-01-22 10:25:39 +00:00
let mut wifi_stats_cache : WlanStatus = WlanStatus ::from_ipc ( WlanStatusIpc ::default ( ) ) ;
2022-01-22 16:24:45 +00:00
let mut status_subscribers = HashMap ::< xous ::CID , WifiStateSubscription > ::new ( ) ;
2022-01-15 07:13:51 +00:00
send_message ( self_cid , Message ::new_scalar ( ConnectionManagerOpcode ::Poll . to_usize ( ) . unwrap ( ) , current_interval , 0 , 0 , 0 ) ) . expect ( " couldn't kick off next poll " ) ;
2022-01-09 06:03:20 +00:00
loop {
2022-01-22 16:24:45 +00:00
let msg = xous ::receive_message ( sid ) . unwrap ( ) ;
2022-01-09 06:03:20 +00:00
log ::debug! ( " got msg: {:?} " , msg ) ;
match FromPrimitive ::from_usize ( msg . body . id ( ) ) {
2022-01-15 07:13:51 +00:00
Some ( ConnectionManagerOpcode ::Run ) = > msg_scalar_unpack! ( msg , _ , _ , _ , _ , {
2022-01-09 06:03:20 +00:00
if ! run {
run = true ;
2022-01-15 07:13:51 +00:00
send_message ( self_cid , Message ::new_scalar ( ConnectionManagerOpcode ::Poll . to_usize ( ) . unwrap ( ) , 0 , 0 , 0 , 0 ) ) . expect ( " couldn't kick off next poll " ) ;
2022-01-09 06:03:20 +00:00
}
} ) ,
2022-01-15 07:13:51 +00:00
Some ( ConnectionManagerOpcode ::Poll ) = > msg_scalar_unpack! ( msg , _ , _ , _ , _ , {
if activity_interval . fetch_add ( current_interval as u32 , Ordering ::SeqCst ) > current_interval as u32 {
2022-01-09 06:03:20 +00:00
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
2022-01-13 18:23:57 +00:00
if pddb . is_mounted ( ) & & rev_ok {
2022-01-15 07:13:51 +00:00
mounted = true ;
2022-01-09 06:03:20 +00:00
// the status check code is going to get refactored, so this is a "bare minimum" check
2022-01-22 16:24:45 +00:00
let new_state = com . wlan_status ( ) . unwrap ( ) ;
if wifi_stats_cache . link_state ! = new_state . link_state | |
wifi_stats_cache . ssid . unwrap_or ( com ::SsidRecord ::default ( ) ) . name ! = new_state . ssid . unwrap_or ( com ::SsidRecord ::default ( ) ) . name | |
wifi_stats_cache . ssid . unwrap_or ( com ::SsidRecord ::default ( ) ) . rssi ! = new_state . ssid . unwrap_or ( com ::SsidRecord ::default ( ) ) . rssi
{
for & sub in status_subscribers . keys ( ) {
let buf = Buffer ::into_buf ( com ::WlanStatusIpc ::from_status ( wifi_stats_cache ) ) . or ( Err ( xous ::Error ::InternalError ) ) . unwrap ( ) ;
buf . send ( sub , WifiStateCallback ::Update . to_u32 ( ) . unwrap ( ) ) . or ( Err ( xous ::Error ::InternalError ) ) . unwrap ( ) ;
}
}
wifi_stats_cache = new_state ;
2022-01-14 07:27:24 +00:00
let config = netmgr . get_ipv4_config ( ) ;
2022-01-22 10:25:39 +00:00
if wifi_stats_cache . link_state = = com_rs_ref ::LinkState ::WFXError {
2022-01-15 07:13:51 +00:00
com . wlan_leave ( ) . expect ( " couldn't issue leave command " ) ; // this may not be received by the wf200, *but* it will also re-init the DHCP stack on the EC
netmgr . reset ( ) ; // this will clear our internal net state
// the wfx chip is wedged. kick it. This call has a built-in 2 second delay.
com . wifi_reset ( ) . expect ( " couldn't reset the wf200 chip " ) ;
}
2022-01-14 07:27:24 +00:00
let needs_reconnect =
2022-01-22 10:25:39 +00:00
if wifi_stats_cache . link_state = = com_rs_ref ::LinkState ::Connected {
2022-01-14 07:27:24 +00:00
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?
2022-01-22 10:25:39 +00:00
if config . addr ! = wifi_stats_cache . ipv4 . addr {
2022-01-14 07:27:24 +00:00
true
} else {
false
}
} else {
true
}
} else {
true
} ;
if needs_reconnect {
2022-01-09 06:03:20 +00:00
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
2022-01-14 07:27:24 +00:00
netmgr . reset ( ) ;
2022-01-09 06:03:20 +00:00
// 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 {
let mut wpa_pw_file = pddb . get ( AP_DICT_NAME , & ap , None , false , false , None , Some ( | | { } ) ) . expect ( " couldn't retrieve AP password " ) ;
let mut wp_pw_raw = [ 0 u8 ; com ::api ::WF200_PASS_MAX_LEN ] ;
if let Ok ( readlen ) = wpa_pw_file . read ( & mut wp_pw_raw ) {
let pw = std ::str ::from_utf8 ( & wp_pw_raw [ .. readlen ] ) . expect ( " password was not valid utf-8 " ) ;
com . wlan_set_ssid ( & ap ) . expect ( " couldn't set SSID " ) ;
com . wlan_set_pass ( pw ) . expect ( " couldn't set password " ) ;
com . wlan_join ( ) . expect ( " couldn't issue join command " ) ;
}
// this needs to not be a dead-wait loop, but for now the WLAN API doesn't support anything better
tt . sleep_ms ( 5_000 ) . unwrap ( ) ;
2022-01-22 10:25:39 +00:00
wifi_stats_cache = com . wlan_status ( ) . unwrap ( ) ;
if wifi_stats_cache . link_state = = com_rs_ref ::LinkState ::Connected {
2022-01-09 06:03:20 +00:00
break ;
}
}
2022-01-22 16:24:45 +00:00
for & sub in status_subscribers . keys ( ) {
let buf = Buffer ::into_buf ( com ::WlanStatusIpc ::from_status ( wifi_stats_cache ) ) . or ( Err ( xous ::Error ::InternalError ) ) . unwrap ( ) ;
buf . send ( sub , WifiStateCallback ::Update . to_u32 ( ) . unwrap ( ) ) . or ( Err ( xous ::Error ::InternalError ) ) . unwrap ( ) ;
}
2022-01-09 06:03:20 +00:00
} else {
log ::warn! ( " Connection manager couldn't access {}, but continuing to poll. " , AP_DICT_NAME ) ;
}
} else {
// we're up, reset the interval
activity_interval . store ( 0 , Ordering ::SeqCst ) ;
}
}
}
if run {
2022-01-15 07:13:51 +00:00
tt . sleep_ms ( current_interval ) . unwrap ( ) ;
if ! mounted {
current_interval = BOOT_POLL_INTERVAL_MS ;
} else {
current_interval = POLL_INTERVAL_MS ;
}
send_message ( self_cid , Message ::new_scalar ( ConnectionManagerOpcode ::Poll . to_usize ( ) . unwrap ( ) , 0 , 0 , 0 , 0 ) ) . expect ( " couldn't kick off next poll " ) ;
2022-01-09 06:03:20 +00:00
}
} ) ,
2022-01-22 16:24:45 +00:00
Some ( ConnectionManagerOpcode ::SubscribeWifiStats ) = > {
let buffer = unsafe {
Buffer ::from_memory_message ( msg . body . memory_message ( ) . unwrap ( ) )
2022-01-22 10:25:39 +00:00
} ;
2022-01-22 16:24:45 +00:00
let sub = buffer . to_original ::< WifiStateSubscription , _ > ( ) . unwrap ( ) ;
let sub_cid = xous ::connect ( xous ::SID ::from_array ( sub . sid ) ) . expect ( " couldn't connect to wifi subscriber callback " ) ;
status_subscribers . insert ( sub_cid , sub ) ;
2022-01-22 10:25:39 +00:00
} ,
2022-01-22 16:24:45 +00:00
Some ( ConnectionManagerOpcode ::UnsubWifiStats ) = > msg_blocking_scalar_unpack! ( msg , s0 , s1 , s2 , s3 , {
// note: this routine largely untested, could have some errors around the ordering of the blocking return vs the disconnect call.
let sid = [ s0 as u32 , s1 as u32 , s2 as u32 , s3 as u32 ] ;
let mut valid_sid : Option < xous ::CID > = None ;
for ( & cid , & sub ) in status_subscribers . iter ( ) {
if sub . sid = = sid {
valid_sid = Some ( cid )
}
}
xous ::return_scalar ( msg . sender , 1 ) . expect ( " couldn't ack unsub " ) ;
if let Some ( cid ) = valid_sid {
status_subscribers . remove ( & cid ) ;
unsafe { xous ::disconnect ( cid ) . expect ( " couldn't remove wifi status subscriber from our CID list that is limited to 32 items total. Suspect issue with ordering of disconnect vs blocking return... " ) ; }
}
} ) ,
2022-01-09 06:03:20 +00:00
// stop is blocking because we need to ensure the previous poll has finished before moving on, otherwise,
// we could get a double-run condition
Some ( ConnectionManagerOpcode ::Stop ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
run = false ;
xous ::return_scalar ( msg . sender , 0 ) . expect ( " couldn't ack stop " ) ;
} ) ,
Some ( ConnectionManagerOpcode ::Quit ) = > msg_blocking_scalar_unpack! ( msg , _ , _ , _ , _ , {
xous ::return_scalar ( msg . sender , 0 ) . unwrap ( ) ;
log ::warn! ( " exiting connection manager " ) ;
break ;
} ) ,
None = > {
log ::error! ( " couldn't convert opcode: {:?} " , msg ) ;
}
}
}
unsafe { xous ::disconnect ( self_cid ) . ok ( ) } ;
xous ::destroy_server ( sid ) . unwrap ( ) ;
}