2021-10-08 12:01:13 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
|
|
|
|
|
pub mod api;
|
2022-01-14 07:27:24 +00:00
|
|
|
use com::Ipv4Conf;
|
|
|
|
|
use xous::{CID, send_message, Message};
|
|
|
|
|
use xous_ipc::Buffer;
|
|
|
|
|
use num_traits::*;
|
2021-10-08 12:01:13 +00:00
|
|
|
|
2021-10-30 18:07:55 +00:00
|
|
|
pub mod protocols;
|
|
|
|
|
pub use protocols::*;
|
2021-10-17 21:55:42 +00:00
|
|
|
pub use smoltcp::time::Duration;
|
2021-10-19 21:18:54 +00:00
|
|
|
pub use api::*;
|
|
|
|
|
pub use smoltcp::wire::IpEndpoint;
|
2021-10-17 20:02:55 +00:00
|
|
|
|
|
|
|
|
/// NetConn is a crate-level structure that just counts the number of connections from this process to
|
|
|
|
|
/// the Net server. It's not mean to be created by user-facing code, so the visibility is (crate).
|
|
|
|
|
pub(crate) struct NetConn {
|
2021-10-08 12:01:13 +00:00
|
|
|
conn: CID,
|
|
|
|
|
}
|
2021-10-17 20:02:55 +00:00
|
|
|
impl NetConn {
|
|
|
|
|
pub(crate) fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
2021-10-08 12:01:13 +00:00
|
|
|
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
|
|
|
|
let conn = xns.request_connection_blocking(api::SERVER_NAME_NET).expect("Can't connect to Net server");
|
2021-10-17 20:02:55 +00:00
|
|
|
Ok(NetConn {
|
|
|
|
|
conn,
|
2021-10-08 12:01:13 +00:00
|
|
|
})
|
|
|
|
|
}
|
2021-10-17 20:02:55 +00:00
|
|
|
pub(crate) fn conn(&self) -> CID {
|
|
|
|
|
self.conn
|
|
|
|
|
}
|
2021-10-08 12:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use core::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
|
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
2021-10-17 20:02:55 +00:00
|
|
|
impl Drop for NetConn {
|
2021-10-08 12:01:13 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
|
// the connection to the server side must be reference counted, so that multiple instances of this object within
|
|
|
|
|
// a single process do not end up de-allocating the CID on other threads before they go out of scope.
|
|
|
|
|
// Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object".
|
2022-01-05 12:01:54 +00:00
|
|
|
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed);
|
2021-10-08 12:01:13 +00:00
|
|
|
if REFCOUNT.load(Ordering::Relaxed) == 0 {
|
|
|
|
|
unsafe{xous::disconnect(self.conn).unwrap();}
|
|
|
|
|
}
|
|
|
|
|
// if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance),
|
|
|
|
|
// de-allocate those items here. They don't need a reference count because they are object-specific
|
|
|
|
|
}
|
2021-10-17 20:02:55 +00:00
|
|
|
}
|
2022-01-14 07:27:24 +00:00
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
2022-01-22 10:25:39 +00:00
|
|
|
/// This is the function that the system should be using to check the wifi state -- it will read
|
|
|
|
|
/// the cached value from the connection manager. The direct call to the COM could cause too much
|
|
|
|
|
/// congestion.
|
|
|
|
|
pub fn read_wifi_state(&self) -> Result<com::WlanStatus, xous::Error> {
|
|
|
|
|
let status = com::WlanStatusIpc::default();
|
|
|
|
|
let mut buf = Buffer::into_buf(status).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
buf.lend_mut(self.netconn.conn(), Opcode::GetWifiStats.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
|
|
|
|
let response = buf.to_original::<com::WlanStatusIpc, _>().unwrap();
|
|
|
|
|
Ok(com::WlanStatus::from_ipc(response))
|
|
|
|
|
}
|
2022-01-14 07:27:24 +00:00
|
|
|
}
|