aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-net/src/lib.rs40
1 files changed, 28 insertions, 12 deletions
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs
index ef53fb905..a7b7efa87 100644
--- a/embassy-net/src/lib.rs
+++ b/embassy-net/src/lib.rs
@@ -260,7 +260,10 @@ pub struct Stack<'d> {
260pub(crate) struct Inner { 260pub(crate) struct Inner {
261 pub(crate) sockets: SocketSet<'static>, // Lifetime type-erased. 261 pub(crate) sockets: SocketSet<'static>, // Lifetime type-erased.
262 pub(crate) iface: Interface, 262 pub(crate) iface: Interface,
263 /// Waker used for triggering polls.
263 pub(crate) waker: WakerRegistration, 264 pub(crate) waker: WakerRegistration,
265 /// Waker used for waiting for link up or config up.
266 state_waker: WakerRegistration,
264 hardware_address: HardwareAddress, 267 hardware_address: HardwareAddress,
265 next_local_port: u16, 268 next_local_port: u16,
266 link_up: bool, 269 link_up: bool,
@@ -270,7 +273,6 @@ pub(crate) struct Inner {
270 static_v6: Option<StaticConfigV6>, 273 static_v6: Option<StaticConfigV6>,
271 #[cfg(feature = "dhcpv4")] 274 #[cfg(feature = "dhcpv4")]
272 dhcp_socket: Option<SocketHandle>, 275 dhcp_socket: Option<SocketHandle>,
273 config_waker: WakerRegistration,
274 #[cfg(feature = "dns")] 276 #[cfg(feature = "dns")]
275 dns_socket: SocketHandle, 277 dns_socket: SocketHandle,
276 #[cfg(feature = "dns")] 278 #[cfg(feature = "dns")]
@@ -326,6 +328,7 @@ pub fn new<'d, D: Driver, const SOCK: usize>(
326 sockets, 328 sockets,
327 iface, 329 iface,
328 waker: WakerRegistration::new(), 330 waker: WakerRegistration::new(),
331 state_waker: WakerRegistration::new(),
329 next_local_port, 332 next_local_port,
330 hardware_address, 333 hardware_address,
331 link_up: false, 334 link_up: false,
@@ -335,7 +338,6 @@ pub fn new<'d, D: Driver, const SOCK: usize>(
335 static_v6: None, 338 static_v6: None,
336 #[cfg(feature = "dhcpv4")] 339 #[cfg(feature = "dhcpv4")]
337 dhcp_socket: None, 340 dhcp_socket: None,
338 config_waker: WakerRegistration::new(),
339 #[cfg(feature = "dns")] 341 #[cfg(feature = "dns")]
340 dns_socket, 342 dns_socket,
341 #[cfg(feature = "dns")] 343 #[cfg(feature = "dns")]
@@ -421,10 +423,20 @@ impl<'d> Stack<'d> {
421 v4_up || v6_up 423 v4_up || v6_up
422 } 424 }
423 425
426 /// Wait for the network device to obtain a link signal.
427 pub async fn wait_link_up(&self) {
428 self.wait(|| self.is_link_up()).await
429 }
430
431 /// Wait for the network device to lose link signal.
432 pub async fn wait_link_down(&self) {
433 self.wait(|| !self.is_link_up()).await
434 }
435
424 /// Wait for the network stack to obtain a valid IP configuration. 436 /// Wait for the network stack to obtain a valid IP configuration.
425 /// 437 ///
426 /// ## Notes: 438 /// ## Notes:
427 /// - Ensure [`Stack::run`] has been called before using this function. 439 /// - Ensure [`Runner::run`] has been started before using this function.
428 /// 440 ///
429 /// - This function may never return (e.g. if no configuration is obtained through DHCP). 441 /// - This function may never return (e.g. if no configuration is obtained through DHCP).
430 /// The caller is supposed to handle a timeout for this case. 442 /// The caller is supposed to handle a timeout for this case.
@@ -451,13 +463,17 @@ impl<'d> Stack<'d> {
451 /// // ... 463 /// // ...
452 /// ``` 464 /// ```
453 pub async fn wait_config_up(&self) { 465 pub async fn wait_config_up(&self) {
454 // If the config is up already, we can return immediately. 466 self.wait(|| self.is_config_up()).await
455 if self.is_config_up() { 467 }
456 return;
457 }
458 468
459 poll_fn(|cx| { 469 /// Wait for the network stack to lose a valid IP configuration.
460 if self.is_config_up() { 470 pub async fn wait_config_down(&self) {
471 self.wait(|| !self.is_config_up()).await
472 }
473
474 fn wait<'a>(&'a self, mut predicate: impl FnMut() -> bool + 'a) -> impl Future<Output = ()> + 'a {
475 poll_fn(move |cx| {
476 if predicate() {
461 Poll::Ready(()) 477 Poll::Ready(())
462 } else { 478 } else {
463 // If the config is not up, we register a waker that is woken up 479 // If the config is not up, we register a waker that is woken up
@@ -465,13 +481,12 @@ impl<'d> Stack<'d> {
465 trace!("Waiting for config up"); 481 trace!("Waiting for config up");
466 482
467 self.with_mut(|i| { 483 self.with_mut(|i| {
468 i.config_waker.register(cx.waker()); 484 i.state_waker.register(cx.waker());
469 }); 485 });
470 486
471 Poll::Pending 487 Poll::Pending
472 } 488 }
473 }) 489 })
474 .await;
475 } 490 }
476 491
477 /// Get the current IPv4 configuration. 492 /// Get the current IPv4 configuration.
@@ -775,7 +790,7 @@ impl Inner {
775 .update_servers(&dns_servers[..count]); 790 .update_servers(&dns_servers[..count]);
776 } 791 }
777 792
778 self.config_waker.wake(); 793 self.state_waker.wake();
779 } 794 }
780 795
781 fn poll<D: Driver>(&mut self, cx: &mut Context<'_>, driver: &mut D) { 796 fn poll<D: Driver>(&mut self, cx: &mut Context<'_>, driver: &mut D) {
@@ -813,6 +828,7 @@ impl Inner {
813 // Print when changed 828 // Print when changed
814 if old_link_up != self.link_up { 829 if old_link_up != self.link_up {
815 info!("link_up = {:?}", self.link_up); 830 info!("link_up = {:?}", self.link_up);
831 self.state_waker.wake();
816 } 832 }
817 833
818 #[allow(unused_mut)] 834 #[allow(unused_mut)]