From f1a4de6338c25f146d96010f3f4aff5c56132d15 Mon Sep 17 00:00:00 2001 From: Kenneth Knudsen Date: Thu, 26 Oct 2023 08:45:23 +0200 Subject: [PATCH 1/2] Only insert channel for the correct protocol --- ublox-short-range/src/client.rs | 12 ++++++------ ublox-short-range/src/wifi/mod.rs | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/ublox-short-range/src/client.rs b/ublox-short-range/src/client.rs index 45adeea..d9c1543 100644 --- a/ublox-short-range/src/client.rs +++ b/ublox-short-range/src/client.rs @@ -727,8 +727,8 @@ where } } else { for (h, s) in sockets.iter_mut() { - match event.protocol { - Protocol::TCP => { + match (&event.protocol, s.get_type()) { + (Protocol::TCP, SocketType::Tcp) => { let mut tcp = TcpSocket::downcast(s)?; if tcp.endpoint() == Some(endpoint) { self.socket_map @@ -737,7 +737,7 @@ where tcp.set_state(TcpState::Connected(endpoint)); } } - Protocol::UDP => { + (Protocol::UDP, SocketType::Udp) => { let mut udp = UdpSocket::downcast(s)?; if udp.endpoint() == Some(endpoint) { self.socket_map @@ -772,8 +772,8 @@ where } } else { for (h, s) in sockets.iter_mut() { - match event.protocol { - Protocol::TCP => { + match (&event.protocol, s.get_type()) { + (Protocol::TCP, SocketType::Tcp) => { let mut tcp = TcpSocket::downcast(s)?; if tcp.endpoint() == Some(endpoint) { self.socket_map @@ -782,7 +782,7 @@ where tcp.set_state(TcpState::Connected(endpoint)); } } - Protocol::UDP => { + (Protocol::UDP, SocketType::Udp) => { let mut udp = UdpSocket::downcast(s)?; if udp.endpoint() == Some(endpoint) { self.socket_map diff --git a/ublox-short-range/src/wifi/mod.rs b/ublox-short-range/src/wifi/mod.rs index da5c18c..b570b1d 100644 --- a/ublox-short-range/src/wifi/mod.rs +++ b/ublox-short-range/src/wifi/mod.rs @@ -35,6 +35,19 @@ pub struct SocketMap { peer_map: heapless::FnvIndexMap, } +impl defmt::Format for SocketMap { + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "ChannelMap:\n"); + for (channel, socket) in self.channel_map.iter() { + defmt::write!(fmt, "channelId: {}, Handle: {}\n", channel.0, socket.0) + } + defmt::write!(fmt, "PeerMap:\n"); + for (peer, socket) in self.peer_map.iter() { + defmt::write!(fmt, "PeerId: {}, Handle: {}\n", peer.0, socket.0) + } + } +} + impl Default for SocketMap { fn default() -> Self { Self::new() @@ -57,7 +70,10 @@ impl SocketMap { defmt::trace!("[SOCK_MAP] {:?} tied to {:?}", socket_handle, channel_id); match self.channel_map.insert(channel_id, socket_handle) { Ok(_) => Ok(()), - Err(_) => Err(SocketMapError::Full), + Err(_) => { + defmt::error!("Failed inserting channel SocketMap full"); + Err(SocketMapError::Full) + } } } @@ -83,6 +99,7 @@ impl SocketMap { ) -> Result<(), SocketMapError> { defmt::trace!("[SOCK_MAP] {:?} tied to {:?}", socket_handle, peer); if self.peer_map.insert(peer, socket_handle).is_err() { + defmt::error!("Insert peer failed SocketMap is FULL"); return Err(SocketMapError::Full); }; Ok(()) From caebe54d7235b7b403ba4203c5a8091d38fae20d Mon Sep 17 00:00:00 2001 From: Kenneth Knudsen Date: Fri, 27 Oct 2023 12:52:32 +0200 Subject: [PATCH 2/2] remove peer from socketmap on error --- ublox-short-range/src/wifi/udp_stack.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/ublox-short-range/src/wifi/udp_stack.rs b/ublox-short-range/src/wifi/udp_stack.rs index b464545..60f7f61 100644 --- a/ublox-short-range/src/wifi/udp_stack.rs +++ b/ublox-short-range/src/wifi/udp_stack.rs @@ -61,6 +61,8 @@ where socket: &mut Self::UdpSocket, remote: SocketAddr, ) -> Result<(), Self::Error> { + let mut peer_handle = crate::command::PeerHandle(0); + if self.sockets.is_none() { defmt::error!("[UDP] Connecting socket Error: Missing socket set"); return Err(Error::Illegal); @@ -86,10 +88,13 @@ where .send_internal(&EdmAtCmdWrapper(ConnectPeer { url: &url }), true) .map_err(|_| Error::Unaddressable) { - Ok(resp) => self - .socket_map - .insert_peer(resp.peer_handle, *socket) - .map_err(|_| Error::InvalidSocket)?, + Ok(resp) => { + peer_handle = resp.peer_handle; + + self.socket_map + .insert_peer(resp.peer_handle, *socket) + .map_err(|_| Error::InvalidSocket)? + } Err(e) => { let mut udp = self @@ -109,7 +114,14 @@ where .state() == UdpState::Closed { - self.spin().map_err(|_| Error::Illegal)?; + match self.spin() { + Ok(_) => {} + Err(_) => { + defmt::error!("ERROR connection UDP removing peer"); + self.socket_map.remove_peer(&peer_handle); + return Err(Error::Illegal); + } + }; } Ok(()) }