From a512614de535a6fb934ab848a1adc7b714f52eae Mon Sep 17 00:00:00 2001 From: linuswck Date: Wed, 11 Sep 2024 12:08:37 +0800 Subject: [PATCH] net: add option not to open sock when it is polled - fix a bug, which mcu can get stuck in State::HardReset indefinitely --- src/main.rs | 10 +++++----- src/net/net.rs | 16 +++++++++------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index d1b3ab4..a6150ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -167,7 +167,7 @@ fn main() -> ! { } net::net::for_each(|mut socket, id| { - if net::net::eth_is_socket_active(socket) && net::net::eth_is_socket_connected(socket) { + if net::net::eth_is_socket_active(socket, true) && net::net::eth_is_socket_connected(socket) { if active_report[id] { if net::net::eth_can_sock_send(socket) { net::cmd_handler::send_status_report( @@ -191,7 +191,7 @@ fn main() -> ! { let mut oldest_connected_sock_id: usize = 0; net::net::for_each(|mut socket, id| { - if net::net::eth_is_socket_active(socket) && net::net::eth_is_socket_connected(socket) { + if net::net::eth_is_socket_active(socket, true) && net::net::eth_is_socket_connected(socket) { if net::net::eth_can_sock_recv(socket) && net::net::eth_can_sock_send(socket) { let bytes = net::net::eth_recv(eth_data_buffer, socket); if bytes != 0 { @@ -289,7 +289,7 @@ fn main() -> ! { laser.power_down(); thermostat.power_down(); net::net::for_each(|mut socket, _| { - if net::net::eth_is_socket_active(socket) { + if net::net::eth_is_socket_active(socket, false) { net::cmd_handler::send_response( eth_data_buffer, net::cmd_handler::ResponseEnum::HardReset, @@ -308,7 +308,7 @@ fn main() -> ! { laser.power_down(); thermostat.power_down(); net::net::for_each(|mut socket, _| { - if net::net::eth_is_socket_active(socket) { + if net::net::eth_is_socket_active(socket, false) { net::cmd_handler::send_response( eth_data_buffer, net::cmd_handler::ResponseEnum::Dfu, @@ -326,7 +326,7 @@ fn main() -> ! { thermostat.power_down(); let mut any_socket_alive = false; net::net::for_each(|socket, _| { - if net::net::eth_is_socket_active(socket) { + if net::net::eth_is_socket_active(socket, false) { net::net::eth_close_socket(socket); any_socket_alive = true; } diff --git a/src/net/net.rs b/src/net/net.rs index 0c23451..e71f27f 100644 --- a/src/net/net.rs +++ b/src/net/net.rs @@ -268,14 +268,16 @@ impl ServerHandle { socket.state() == State::Established } - pub fn poll_socket_status(&mut self, socket_handles: SocketHandle) -> bool { + pub fn poll_socket_status(&mut self, socket_handles: SocketHandle, reopen_sock: bool) -> bool { let socket = self.socket_set.get_mut::(socket_handles); if !socket.is_listening() && !socket.is_open() || socket.state() == State::CloseWait { - socket.abort(); - socket.listen(self.socket_addr).ok(); - info!("Disconnected... Reopening listening socket."); + if reopen_sock { + socket.abort(); + socket.listen(self.socket_addr).ok(); + info!("Disconnected... Reopening listening socket."); + } return false; - } else if socket.state() == State::Closed || socket.state() == State::Closing { + } else if socket.state() == State::Closed || socket.state() == State::Closing || socket.is_listening() { return false; } return true; @@ -485,10 +487,10 @@ pub fn eth_is_socket_connected(socket_handles: SocketHandle) -> bool { } } -pub fn eth_is_socket_active(socket_handles: SocketHandle) -> bool { +pub fn eth_is_socket_active(socket_handles: SocketHandle, reopen_sock: bool) -> bool { unsafe { if let Some(ref mut server_handle) = SERVER_HANDLE { - server_handle.poll_socket_status(socket_handles) + server_handle.poll_socket_status(socket_handles, reopen_sock) } else { panic!("eth_is_socket_active is called before init"); }