2020-03-31 07:16:58 +08:00
|
|
|
use core::{
|
|
|
|
cell::RefCell,
|
|
|
|
task::Waker,
|
|
|
|
};
|
2020-04-01 04:34:32 +08:00
|
|
|
use alloc::vec::Vec;
|
2020-03-31 07:16:58 +08:00
|
|
|
use smoltcp::{
|
|
|
|
iface::EthernetInterface,
|
|
|
|
phy::Device,
|
2020-04-01 04:34:32 +08:00
|
|
|
socket::SocketSet,
|
2020-11-21 00:12:22 +08:00
|
|
|
time::{Duration, Instant},
|
2020-03-31 07:16:58 +08:00
|
|
|
};
|
|
|
|
use crate::task;
|
|
|
|
|
|
|
|
mod tcp_stream;
|
|
|
|
pub use tcp_stream::TcpStream;
|
|
|
|
|
2020-11-21 00:12:22 +08:00
|
|
|
pub trait LinkCheck {
|
|
|
|
type Link;
|
|
|
|
fn is_idle(&self) -> bool;
|
|
|
|
fn check_link_change(&mut self) -> Option<Self::Link>;
|
|
|
|
}
|
|
|
|
|
2020-03-31 07:16:58 +08:00
|
|
|
static mut SOCKETS: Option<Sockets> = None;
|
|
|
|
|
|
|
|
pub struct Sockets {
|
2021-02-04 05:41:32 +08:00
|
|
|
sockets: RefCell<SocketSet<'static>>,
|
2020-03-31 07:16:58 +08:00
|
|
|
wakers: RefCell<Vec<Waker>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sockets {
|
|
|
|
pub fn init(max_sockets: usize) {
|
|
|
|
let mut sockets_storage = Vec::with_capacity(max_sockets);
|
|
|
|
for _ in 0..max_sockets {
|
|
|
|
sockets_storage.push(None);
|
|
|
|
}
|
|
|
|
let sockets = RefCell::new(SocketSet::new(sockets_storage));
|
|
|
|
|
|
|
|
let wakers = RefCell::new(Vec::new());
|
2020-08-24 15:25:03 +08:00
|
|
|
|
2020-03-31 07:16:58 +08:00
|
|
|
let instance = Sockets {
|
|
|
|
sockets,
|
|
|
|
wakers,
|
|
|
|
};
|
2020-04-03 06:18:04 +08:00
|
|
|
// println!("sockets initialized");
|
2020-03-31 07:16:58 +08:00
|
|
|
unsafe { SOCKETS = Some(instance); }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Block and run executor indefinitely while polling the smoltcp
|
|
|
|
/// iface
|
2021-02-04 05:41:32 +08:00
|
|
|
pub fn run<'b, D: for<'d> Device<'d> + LinkCheck>(
|
|
|
|
iface: &mut EthernetInterface<'b, D>,
|
2020-04-03 06:18:04 +08:00
|
|
|
mut get_time: impl FnMut() -> Instant,
|
2020-04-17 02:42:21 +08:00
|
|
|
) -> ! {
|
2020-03-31 07:16:58 +08:00
|
|
|
task::block_on(async {
|
2020-11-21 00:12:22 +08:00
|
|
|
let mut last_link_check = Instant::from_millis(0);
|
|
|
|
const LINK_CHECK_INTERVAL: u64 = 500;
|
|
|
|
|
2020-03-31 07:16:58 +08:00
|
|
|
loop {
|
2020-04-03 06:18:04 +08:00
|
|
|
let instant = get_time();
|
|
|
|
Self::instance().poll(iface, instant);
|
2020-11-21 00:12:22 +08:00
|
|
|
|
|
|
|
let dev = iface.device_mut();
|
|
|
|
if dev.is_idle() && instant >= last_link_check + Duration::from_millis(LINK_CHECK_INTERVAL) {
|
|
|
|
dev.check_link_change();
|
|
|
|
last_link_check = instant;
|
|
|
|
}
|
|
|
|
|
2020-03-31 07:16:58 +08:00
|
|
|
task::r#yield().await;
|
|
|
|
}
|
2020-04-17 02:42:21 +08:00
|
|
|
})
|
2020-03-31 07:16:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn instance() -> &'static Self {
|
|
|
|
unsafe { SOCKETS.as_ref().expect("Sockets") }
|
|
|
|
}
|
2020-08-24 15:25:03 +08:00
|
|
|
|
2021-02-04 05:41:32 +08:00
|
|
|
fn poll<'b, D: for<'d> Device<'d>>(
|
2020-04-03 06:18:04 +08:00
|
|
|
&self,
|
2021-02-04 05:41:32 +08:00
|
|
|
iface: &mut EthernetInterface<'b, D>,
|
2020-04-03 06:18:04 +08:00
|
|
|
instant: Instant
|
|
|
|
) {
|
2020-03-31 07:16:58 +08:00
|
|
|
let processed = {
|
|
|
|
let mut sockets = self.sockets.borrow_mut();
|
2020-04-01 04:34:32 +08:00
|
|
|
match iface.poll(&mut sockets, instant) {
|
2020-03-31 07:16:58 +08:00
|
|
|
Ok(processed) => processed,
|
|
|
|
Err(_) => true,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if processed {
|
|
|
|
let mut wakers = self.wakers.borrow_mut();
|
|
|
|
for waker in wakers.drain(..) {
|
|
|
|
waker.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TODO: this was called through eg. TcpStream, another poll()
|
|
|
|
/// might want to send packets before sleeping for an interrupt.
|
|
|
|
pub(crate) fn register_waker(waker: Waker) {
|
2020-08-24 15:25:03 +08:00
|
|
|
let mut wakers = Self::instance().wakers.borrow_mut();
|
|
|
|
for (i, w) in wakers.iter().enumerate() {
|
|
|
|
if w.will_wake(&waker) {
|
|
|
|
let last = wakers.len() - 1;
|
|
|
|
wakers.swap(i, last);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wakers.push(waker);
|
2020-03-31 07:16:58 +08:00
|
|
|
}
|
|
|
|
}
|