diff options
Diffstat (limited to 'embassy-net')
| -rw-r--r-- | embassy-net/Cargo.toml | 2 | ||||
| -rw-r--r-- | embassy-net/src/udp.rs | 25 |
2 files changed, 18 insertions, 9 deletions
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"] | |||
| 60 | defmt = { version = "0.3", optional = true } | 60 | defmt = { version = "0.3", optional = true } |
| 61 | log = { version = "0.4.14", optional = true } | 61 | log = { version = "0.4.14", optional = true } |
| 62 | 62 | ||
| 63 | smoltcp = { git = "https://github.com/smoltcp-rs/smoltcp.git", rev = "b57e2f9e70e82a13f31d5ea17e55232c11cc2b2d", default-features = false, features = [ | 63 | smoltcp = { version = "0.11.0", default-features = false, features = [ |
| 64 | "socket", | 64 | "socket", |
| 65 | "async", | 65 | "async", |
| 66 | ] } | 66 | ] } |
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 { | |||
| 26 | /// Error returned by [`UdpSocket::recv_from`] and [`UdpSocket::send_to`]. | 26 | /// Error returned by [`UdpSocket::recv_from`] and [`UdpSocket::send_to`]. |
| 27 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] | 27 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] |
| 28 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 28 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 29 | pub enum Error { | 29 | pub enum SendError { |
| 30 | /// No route to host. | 30 | /// No route to host. |
| 31 | NoRoute, | 31 | NoRoute, |
| 32 | /// Socket not bound to an outgoing port. | 32 | /// Socket not bound to an outgoing port. |
| 33 | SocketNotBound, | 33 | SocketNotBound, |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | /// Error returned by [`UdpSocket::recv_from`] and [`UdpSocket::send_to`]. | ||
| 37 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] | ||
| 38 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 39 | pub enum RecvError { | ||
| 40 | /// Provided buffer was smaller than the received packet. | ||
| 41 | Truncated, | ||
| 42 | } | ||
| 43 | |||
| 36 | /// An UDP socket. | 44 | /// An UDP socket. |
| 37 | pub struct UdpSocket<'a> { | 45 | pub struct UdpSocket<'a> { |
| 38 | stack: &'a RefCell<SocketStack>, | 46 | stack: &'a RefCell<SocketStack>, |
| @@ -103,7 +111,7 @@ impl<'a> UdpSocket<'a> { | |||
| 103 | /// This method will wait until a datagram is received. | 111 | /// This method will wait until a datagram is received. |
| 104 | /// | 112 | /// |
| 105 | /// Returns the number of bytes received and the remote endpoint. | 113 | /// Returns the number of bytes received and the remote endpoint. |
| 106 | pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> { | 114 | pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), RecvError> { |
| 107 | poll_fn(move |cx| self.poll_recv_from(buf, cx)).await | 115 | poll_fn(move |cx| self.poll_recv_from(buf, cx)).await |
| 108 | } | 116 | } |
| 109 | 117 | ||
| @@ -114,10 +122,11 @@ impl<'a> UdpSocket<'a> { | |||
| 114 | /// | 122 | /// |
| 115 | /// When a datagram is received, this method will return `Poll::Ready` with the | 123 | /// When a datagram is received, this method will return `Poll::Ready` with the |
| 116 | /// number of bytes received and the remote endpoint. | 124 | /// number of bytes received and the remote endpoint. |
| 117 | pub fn poll_recv_from(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll<Result<(usize, IpEndpoint), Error>> { | 125 | pub fn poll_recv_from(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll<Result<(usize, IpEndpoint), RecvError>> { |
| 118 | self.with_mut(|s, _| match s.recv_slice(buf) { | 126 | self.with_mut(|s, _| match s.recv_slice(buf) { |
| 119 | Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))), | 127 | Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))), |
| 120 | // No data ready | 128 | // No data ready |
| 129 | Err(udp::RecvError::Truncated) => Poll::Ready(Err(RecvError::Truncated)), | ||
| 121 | Err(udp::RecvError::Exhausted) => { | 130 | Err(udp::RecvError::Exhausted) => { |
| 122 | s.register_recv_waker(cx.waker()); | 131 | s.register_recv_waker(cx.waker()); |
| 123 | Poll::Pending | 132 | Poll::Pending |
| @@ -129,8 +138,8 @@ impl<'a> UdpSocket<'a> { | |||
| 129 | /// | 138 | /// |
| 130 | /// This method will wait until the datagram has been sent. | 139 | /// This method will wait until the datagram has been sent. |
| 131 | /// | 140 | /// |
| 132 | /// When the remote endpoint is not reachable, this method will return `Err(Error::NoRoute)` | 141 | /// When the remote endpoint is not reachable, this method will return `Err(SendError::NoRoute)` |
| 133 | pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error> | 142 | pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), SendError> |
| 134 | where | 143 | where |
| 135 | T: Into<IpEndpoint>, | 144 | T: Into<IpEndpoint>, |
| 136 | { | 145 | { |
| @@ -146,7 +155,7 @@ impl<'a> UdpSocket<'a> { | |||
| 146 | /// and register the current task to be notified when the buffer has space available. | 155 | /// and register the current task to be notified when the buffer has space available. |
| 147 | /// | 156 | /// |
| 148 | /// When the remote endpoint is not reachable, this method will return `Poll::Ready(Err(Error::NoRoute))`. | 157 | /// When the remote endpoint is not reachable, this method will return `Poll::Ready(Err(Error::NoRoute))`. |
| 149 | pub fn poll_send_to<T>(&self, buf: &[u8], remote_endpoint: T, cx: &mut Context<'_>) -> Poll<Result<(), Error>> | 158 | pub fn poll_send_to<T>(&self, buf: &[u8], remote_endpoint: T, cx: &mut Context<'_>) -> Poll<Result<(), SendError>> |
| 150 | where | 159 | where |
| 151 | T: Into<IpEndpoint>, | 160 | T: Into<IpEndpoint>, |
| 152 | { | 161 | { |
| @@ -160,9 +169,9 @@ impl<'a> UdpSocket<'a> { | |||
| 160 | Err(udp::SendError::Unaddressable) => { | 169 | Err(udp::SendError::Unaddressable) => { |
| 161 | // If no sender/outgoing port is specified, there is not really "no route" | 170 | // If no sender/outgoing port is specified, there is not really "no route" |
| 162 | if s.endpoint().port == 0 { | 171 | if s.endpoint().port == 0 { |
| 163 | Poll::Ready(Err(Error::SocketNotBound)) | 172 | Poll::Ready(Err(SendError::SocketNotBound)) |
| 164 | } else { | 173 | } else { |
| 165 | Poll::Ready(Err(Error::NoRoute)) | 174 | Poll::Ready(Err(SendError::NoRoute)) |
| 166 | } | 175 | } |
| 167 | } | 176 | } |
| 168 | }) | 177 | }) |
