aboutsummaryrefslogtreecommitdiff
path: root/embassy-net
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
parentb7cd7952c890f585ff876c622482534e5d58d4a4 (diff)
parent0be9b0599aaf2e425d76ec7852ff4b3535defddf (diff)
Merge remote-tracking branch 'origin'
Diffstat (limited to 'embassy-net')
-rw-r--r--embassy-net/Cargo.toml19
-rw-r--r--embassy-net/src/lib.rs9
-rw-r--r--embassy-net/src/udp.rs30
3 files changed, 47 insertions, 11 deletions
diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml
index 0c07e3651..612a3d689 100644
--- a/embassy-net/Cargo.toml
+++ b/embassy-net/Cargo.toml
@@ -25,18 +25,34 @@ features = ["defmt", "tcp", "udp", "dns", "dhcpv4", "proto-ipv6", "medium-ethern
25default = [] 25default = []
26std = [] 26std = []
27 27
28## Enable defmt
28defmt = ["dep:defmt", "smoltcp/defmt", "embassy-net-driver/defmt", "heapless/defmt-03"] 29defmt = ["dep:defmt", "smoltcp/defmt", "embassy-net-driver/defmt", "heapless/defmt-03"]
29 30
31#! Many of the following feature flags are re-exports of smoltcp feature flags. See
32#! the [smoltcp feature flag documentation](https://github.com/smoltcp-rs/smoltcp#feature-flags)
33#! for more details
34
35## Enable UDP support
30udp = ["smoltcp/socket-udp"] 36udp = ["smoltcp/socket-udp"]
37## Enable TCP support
31tcp = ["smoltcp/socket-tcp"] 38tcp = ["smoltcp/socket-tcp"]
39## Enable DNS support
32dns = ["smoltcp/socket-dns", "smoltcp/proto-dns"] 40dns = ["smoltcp/socket-dns", "smoltcp/proto-dns"]
41## Enable DHCPv4 support
33dhcpv4 = ["proto-ipv4", "medium-ethernet", "smoltcp/socket-dhcpv4"] 42dhcpv4 = ["proto-ipv4", "medium-ethernet", "smoltcp/socket-dhcpv4"]
43## Enable DHCPv4 support with hostname
34dhcpv4-hostname = ["dhcpv4"] 44dhcpv4-hostname = ["dhcpv4"]
45## Enable IPv4 support
35proto-ipv4 = ["smoltcp/proto-ipv4"] 46proto-ipv4 = ["smoltcp/proto-ipv4"]
47## Enable IPv6 support
36proto-ipv6 = ["smoltcp/proto-ipv6"] 48proto-ipv6 = ["smoltcp/proto-ipv6"]
49## Enable the Ethernet medium
37medium-ethernet = ["smoltcp/medium-ethernet"] 50medium-ethernet = ["smoltcp/medium-ethernet"]
51## Enable the IP medium
38medium-ip = ["smoltcp/medium-ip"] 52medium-ip = ["smoltcp/medium-ip"]
53## Enable the IEEE 802.15.4 medium
39medium-ieee802154 = ["smoltcp/medium-ieee802154"] 54medium-ieee802154 = ["smoltcp/medium-ieee802154"]
55## Enable IGMP support
40igmp = ["smoltcp/proto-igmp"] 56igmp = ["smoltcp/proto-igmp"]
41 57
42[dependencies] 58[dependencies]
@@ -44,7 +60,7 @@ igmp = ["smoltcp/proto-igmp"]
44defmt = { version = "0.3", optional = true } 60defmt = { version = "0.3", optional = true }
45log = { version = "0.4.14", optional = true } 61log = { version = "0.4.14", optional = true }
46 62
47smoltcp = { git = "https://github.com/smoltcp-rs/smoltcp.git", rev = "b57e2f9e70e82a13f31d5ea17e55232c11cc2b2d", default-features = false, features = [ 63smoltcp = { version = "0.11.0", default-features = false, features = [
48 "socket", 64 "socket",
49 "async", 65 "async",
50] } 66] }
@@ -62,3 +78,4 @@ stable_deref_trait = { version = "1.2.0", default-features = false }
62futures = { version = "0.3.17", default-features = false, features = [ "async-await" ] } 78futures = { version = "0.3.17", default-features = false, features = [ "async-await" ] }
63atomic-pool = "1.0" 79atomic-pool = "1.0"
64embedded-nal-async = { version = "0.7.1" } 80embedded-nal-async = { version = "0.7.1" }
81document-features = "0.2.7"
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<'_> {