diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml index e6c5ea74f..612a3d689 100644 --- a/embassy-net/Cargo.toml +++ b/embassy-net/Cargo.toml @@ -60,7 +60,7 @@ igmp = ["smoltcp/proto-igmp"] defmt = { version = "0.3", optional = true } log = { version = "0.4.14", optional = true } -smoltcp = { git = "https://github.com/smoltcp-rs/smoltcp.git", rev = "b57e2f9e70e82a13f31d5ea17e55232c11cc2b2d", default-features = false, features = [ +smoltcp = { version = "0.11.0", default-features = false, features = [ "socket", "async", ] } diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs index 61058c1ba..d9df5b29d 100644 --- a/embassy-net/src/udp.rs +++ b/embassy-net/src/udp.rs @@ -26,13 +26,21 @@ pub enum BindError { /// Error returned by [`UdpSocket::recv_from`] and [`UdpSocket::send_to`]. #[derive(PartialEq, Eq, Clone, Copy, Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] -pub enum Error { +pub enum SendError { /// No route to host. NoRoute, /// Socket not bound to an outgoing port. SocketNotBound, } +/// Error returned by [`UdpSocket::recv_from`] and [`UdpSocket::send_to`]. +#[derive(PartialEq, Eq, Clone, Copy, Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +pub enum RecvError { + /// Provided buffer was smaller than the received packet. + Truncated, +} + /// An UDP socket. pub struct UdpSocket<'a> { stack: &'a RefCell, @@ -103,7 +111,7 @@ impl<'a> UdpSocket<'a> { /// This method will wait until a datagram is received. /// /// Returns the number of bytes received and the remote endpoint. - pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { + pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), RecvError> { poll_fn(move |cx| self.poll_recv_from(buf, cx)).await } @@ -114,10 +122,11 @@ impl<'a> UdpSocket<'a> { /// /// When a datagram is received, this method will return `Poll::Ready` with the /// number of bytes received and the remote endpoint. - pub fn poll_recv_from(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll> { + pub fn poll_recv_from(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll> { self.with_mut(|s, _| match s.recv_slice(buf) { Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))), // No data ready + Err(udp::RecvError::Truncated) => Poll::Ready(Err(RecvError::Truncated)), Err(udp::RecvError::Exhausted) => { s.register_recv_waker(cx.waker()); Poll::Pending @@ -129,8 +138,8 @@ impl<'a> UdpSocket<'a> { /// /// This method will wait until the datagram has been sent. /// - /// When the remote endpoint is not reachable, this method will return `Err(Error::NoRoute)` - pub async fn send_to(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error> + /// When the remote endpoint is not reachable, this method will return `Err(SendError::NoRoute)` + pub async fn send_to(&self, buf: &[u8], remote_endpoint: T) -> Result<(), SendError> where T: Into, { @@ -146,7 +155,7 @@ impl<'a> UdpSocket<'a> { /// and register the current task to be notified when the buffer has space available. /// /// When the remote endpoint is not reachable, this method will return `Poll::Ready(Err(Error::NoRoute))`. - pub fn poll_send_to(&self, buf: &[u8], remote_endpoint: T, cx: &mut Context<'_>) -> Poll> + pub fn poll_send_to(&self, buf: &[u8], remote_endpoint: T, cx: &mut Context<'_>) -> Poll> where T: Into, { @@ -160,9 +169,9 @@ impl<'a> UdpSocket<'a> { Err(udp::SendError::Unaddressable) => { // If no sender/outgoing port is specified, there is not really "no route" if s.endpoint().port == 0 { - Poll::Ready(Err(Error::SocketNotBound)) + Poll::Ready(Err(SendError::SocketNotBound)) } else { - Poll::Ready(Err(Error::NoRoute)) + Poll::Ready(Err(SendError::NoRoute)) } } })