Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement embedded_nal UpdFullStack #39

Merged
merged 3 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This document describes the changes to smoltcp-nal between releases.

# [Unreleased]
## Added
* Implemented full UDP socket

## Fixed

# [0.2.1]
Expand Down
64 changes: 63 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use embedded_nal;
use nanorand::Rng;
pub use smoltcp;

use embedded_nal::{TcpClientStack, UdpClientStack};
use embedded_nal::{TcpClientStack, UdpClientStack, UdpFullStack};
use embedded_time::duration::Milliseconds;
use smoltcp::{
iface::SocketHandle,
Expand Down Expand Up @@ -644,3 +644,65 @@ where
Ok(())
}
}

impl<'a, DeviceT, Clock> UdpFullStack for NetworkStack<'a, DeviceT, Clock>
where
DeviceT: for<'x> smoltcp::phy::Device<'x>,
Clock: embedded_time::Clock,
u32: From<Clock::T>,
{
/// Bind a UDP socket to a specific port.
fn bind(&mut self, socket: &mut UdpSocket, local_port: u16) -> Result<(), NetworkError> {
if self.is_ip_unspecified() {
return Err(NetworkError::NoIpAddress);
}

let local_address = self
.network_interface
.ip_addrs()
.iter()
.find(|item| matches!(item, smoltcp::wire::IpCidr::Ipv4(_)))
.unwrap()
.address();

let local_endpoint = IpEndpoint::new(local_address, local_port);

let internal_socket: &mut smoltcp::socket::UdpSocket =
self.network_interface.get_socket(socket.handle);
internal_socket
.bind(local_endpoint)
.map_err(|_| NetworkError::ConnectionFailure)?;
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

/// Send a packet to a remote host/port.
fn send_to(
&mut self,
socket: &mut Self::UdpSocket,
remote: embedded_nal::SocketAddr,
buffer: &[u8],
) -> embedded_nal::nb::Result<(), NetworkError> {
if self.is_ip_unspecified() {
return Err(embedded_nal::nb::Error::Other(NetworkError::NoIpAddress));
}

let destination = match remote {
embedded_nal::SocketAddr::V4(addr) => {
let octets = addr.ip().octets();
IpEndpoint::new(
IpAddress::v4(octets[0], octets[1], octets[2], octets[3]),
addr.port(),
)
}
// We only support IPv4.
_ => return Err(embedded_nal::nb::Error::Other(NetworkError::Unsupported)),
};

let internal_socket: &mut smoltcp::socket::UdpSocket =
self.network_interface.get_socket(socket.handle);
internal_socket
.send_slice(buffer, destination)
.map_err(|_| embedded_nal::nb::Error::Other(NetworkError::WriteFailure))
}
}
8 changes: 8 additions & 0 deletions src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ where
forward! {close(socket: S::UdpSocket) -> Result<(), S::Error>}
}

impl<'a, S> embedded_nal::UdpFullStack for NetworkStackProxy<'a, S>
where
S: embedded_nal::UdpFullStack,
{
forward! {send_to(socket: &mut S::UdpSocket, remote: embedded_nal::SocketAddr, buffer: &[u8]) -> embedded_nal::nb::Result<(), S::Error>}
forward! {bind(socket: &mut S::UdpSocket, local_port: u16) -> Result<(), S::Error>}
}

impl<'a, DeviceT, Clock> NetworkManager<'a, DeviceT, Clock>
where
DeviceT: for<'x> smoltcp::phy::Device<'x>,
Expand Down