aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src
diff options
context:
space:
mode:
authorsodo <[email protected]>2024-01-02 01:37:00 +0900
committersodo <[email protected]>2024-01-02 13:34:22 +0900
commit6ee153a3e2eec284c0d9d87f31801265c0604f74 (patch)
tree8b801cbd15f9ad5052d5942c731e75736dc9d7eb /embassy-net/src
parentb7cd7952c890f585ff876c622482534e5d58d4a4 (diff)
parent0be9b0599aaf2e425d76ec7852ff4b3535defddf (diff)
Merge remote-tracking branch 'origin'
Diffstat (limited to 'embassy-net/src')
-rw-r--r--embassy-net/src/lib.rs9
-rw-r--r--embassy-net/src/udp.rs30
2 files changed, 29 insertions, 10 deletions
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs
index bf1468642..2cef2232c 100644
--- a/embassy-net/src/lib.rs
+++ b/embassy-net/src/lib.rs
@@ -5,6 +5,9 @@
5#![warn(missing_docs)] 5#![warn(missing_docs)]
6#![doc = include_str!("../README.md")] 6#![doc = include_str!("../README.md")]
7 7
8//! ## Feature flags
9#![doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#)]
10
8#[cfg(not(any(feature = "proto-ipv4", feature = "proto-ipv6")))] 11#[cfg(not(any(feature = "proto-ipv4", feature = "proto-ipv6")))]
9compile_error!("You must enable at least one of the following features: proto-ipv4, proto-ipv6"); 12compile_error!("You must enable at least one of the following features: proto-ipv4, proto-ipv6");
10 13
@@ -411,10 +414,12 @@ impl<D: Driver> Stack<D> {
411 /// ```ignore 414 /// ```ignore
412 /// let config = embassy_net::Config::dhcpv4(Default::default()); 415 /// let config = embassy_net::Config::dhcpv4(Default::default());
413 ///// Init network stack 416 ///// Init network stack
414 /// let stack = &*make_static!(embassy_net::Stack::new( 417 /// static RESOURCES: StaticCell<embassy_net::StackResources<2> = StaticCell::new();
418 /// static STACK: StaticCell<embassy_net::Stack> = StaticCell::new();
419 /// let stack = &*STACK.init(embassy_net::Stack::new(
415 /// device, 420 /// device,
416 /// config, 421 /// config,
417 /// make_static!(embassy_net::StackResources::<2>::new()), 422 /// RESOURCES.init(embassy_net::StackResources::new()),
418 /// seed 423 /// seed
419 /// )); 424 /// ));
420 /// // Launch network task that runs `stack.run().await` 425 /// // Launch network task that runs `stack.run().await`
diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs
index 61058c1ba..a22cd8827 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))]
29pub enum Error { 29pub 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))]
39pub enum RecvError {
40 /// Provided buffer was smaller than the received packet.
41 Truncated,
42}
43
36/// An UDP socket. 44/// An UDP socket.
37pub struct UdpSocket<'a> { 45pub 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 })
@@ -213,6 +222,11 @@ impl<'a> UdpSocket<'a> {
213 pub fn payload_send_capacity(&self) -> usize { 222 pub fn payload_send_capacity(&self) -> usize {
214 self.with(|s, _| s.payload_send_capacity()) 223 self.with(|s, _| s.payload_send_capacity())
215 } 224 }
225
226 /// Set the hop limit field in the IP header of sent packets.
227 pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) {
228 self.with_mut(|s, _| s.set_hop_limit(hop_limit))
229 }
216} 230}
217 231
218impl Drop for UdpSocket<'_> { 232impl Drop for UdpSocket<'_> {