Skip to content

Commit

Permalink
fix: EdmEvent connectEvent insert channel only for event protocol type (
Browse files Browse the repository at this point in the history
#70)

* Only insert channel for the correct protocol

* remove peer from socketmap on error
  • Loading branch information
KennethKnudsen97 authored Oct 30, 2023
1 parent bab0e46 commit 7f09a6a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
12 changes: 6 additions & 6 deletions ublox-short-range/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 18 additions & 1 deletion ublox-short-range/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ pub struct SocketMap {
peer_map: heapless::FnvIndexMap<PeerHandle, SocketHandle, 4>,
}

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()
Expand All @@ -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)
}
}
}

Expand All @@ -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(())
Expand Down
22 changes: 17 additions & 5 deletions ublox-short-range/src/wifi/udp_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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(())
}
Expand Down

0 comments on commit 7f09a6a

Please sign in to comment.