main: reset all tcp socket before MCU reset

This commit is contained in:
topquark12 2021-01-26 15:26:54 +08:00
parent f42ba56f51
commit 0202363c70
1 changed files with 263 additions and 254 deletions

View File

@ -186,6 +186,7 @@ fn main() -> ! {
net::run(clocks, dp.ETHERNET_MAC, dp.ETHERNET_DMA, eth_pins, hwaddr, ipv4_config.clone(), |iface| { net::run(clocks, dp.ETHERNET_MAC, dp.ETHERNET_DMA, eth_pins, hwaddr, ipv4_config.clone(), |iface| {
Server::<Session>::run(iface, |server| { Server::<Session>::run(iface, |server| {
leds.r1.off(); leds.r1.off();
let mut should_reset = false;
loop { loop {
let mut new_ipv4_config = None; let mut new_ipv4_config = None;
@ -202,8 +203,8 @@ fn main() -> ! {
warn!("poll: {:?}", e); warn!("poll: {:?}", e);
}); });
let mut reset_flag = false;
// TCP protocol handling // TCP protocol handling
if ! should_reset {
server.for_each(|mut socket, session| { server.for_each(|mut socket, session| {
if ! socket.is_active() { if ! socket.is_active() {
let _ = socket.listen(TCP_PORT); let _ = socket.listen(TCP_PORT);
@ -422,7 +423,7 @@ fn main() -> ! {
for i in 0..CHANNELS { for i in 0..CHANNELS {
channels.power_down(i); channels.power_down(i);
} }
reset_flag = true; should_reset = true;
// SCB::sys_reset(); // SCB::sys_reset();
} }
Command::Dfu => { Command::Dfu => {
@ -432,7 +433,7 @@ fn main() -> ! {
unsafe { unsafe {
dfu::set_dfu_trigger(); dfu::set_dfu_trigger();
} }
reset_flag = true; should_reset = true;
// SCB::sys_reset(); // SCB::sys_reset();
} }
} }
@ -458,13 +459,21 @@ fn main() -> ! {
} }
} }
}); });
} else {
if reset_flag == true { // Should reset, close all TCP sockets.
let mut sockets_active = 0;
server.for_each(|mut socket, _| { server.for_each(|mut socket, _| {
socket.close(); if socket.is_active() {
socket.abort();
sockets_active += 1;
}
}); });
// Must let loop run one more cycle to poll server in order for RST to be sent,
// this makes sure system does not reset right after socket.abort() is called.
if sockets_active == 0 {
SCB::sys_reset(); SCB::sys_reset();
} }
}
// Apply new IPv4 address/gateway // Apply new IPv4 address/gateway
new_ipv4_config.take() new_ipv4_config.take()