2021-05-05 21:39:33 +08:00
|
|
|
use super::{NetworkReference, UpdateState};
|
2021-05-05 01:52:41 +08:00
|
|
|
|
2021-05-05 21:39:33 +08:00
|
|
|
use crate::hardware::{CycleCounter, EthernetPhy};
|
2021-05-05 01:52:41 +08:00
|
|
|
|
|
|
|
pub struct NetworkProcessor {
|
|
|
|
stack: NetworkReference,
|
|
|
|
phy: EthernetPhy,
|
|
|
|
clock: CycleCounter,
|
|
|
|
network_was_reset: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkProcessor {
|
2021-05-05 21:39:33 +08:00
|
|
|
pub fn new(
|
|
|
|
stack: NetworkReference,
|
|
|
|
phy: EthernetPhy,
|
|
|
|
clock: CycleCounter,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
stack,
|
|
|
|
phy,
|
|
|
|
clock,
|
|
|
|
network_was_reset: false,
|
|
|
|
}
|
2021-05-05 01:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update(&mut self) -> UpdateState {
|
|
|
|
// Service the network stack to process any inbound and outbound traffic.
|
2021-05-05 21:39:33 +08:00
|
|
|
let now = self.clock.current_ms();
|
|
|
|
|
|
|
|
let result = match self.stack.lock(|stack| stack.poll(now)) {
|
2021-05-05 01:52:41 +08:00
|
|
|
Ok(true) => UpdateState::Updated,
|
|
|
|
Ok(false) => UpdateState::NoChange,
|
|
|
|
Err(err) => {
|
|
|
|
log::info!("Network error: {:?}", err);
|
|
|
|
UpdateState::Updated
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// If the PHY indicates there's no more ethernet link, reset the DHCP server in the network
|
|
|
|
// stack.
|
|
|
|
match self.phy.poll_link() {
|
|
|
|
true => self.network_was_reset = false,
|
|
|
|
|
|
|
|
// Only reset the network stack once per link reconnection. This prevents us from
|
|
|
|
// sending an excessive number of DHCP requests.
|
|
|
|
false if !self.network_was_reset => {
|
|
|
|
self.network_was_reset = true;
|
2021-05-05 21:39:33 +08:00
|
|
|
self.stack.lock(|stack| stack.handle_link_reset());
|
2021-05-05 01:52:41 +08:00
|
|
|
}
|
2021-05-05 21:39:33 +08:00
|
|
|
_ => {}
|
2021-05-05 01:52:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|