aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuliDi <[email protected]>2023-09-08 17:26:01 +0200
committerJuliDi <[email protected]>2023-09-08 17:26:01 +0200
commit3e0b752befd492229bfb4c6f9fd3213cfd69a0fc (patch)
tree90a72852b6d9331f8cb8b8b51c395426ae42f4bf
parent6070d61d8ce2d9ce416467ac6aac8ffdd89a59b9 (diff)
fix poll_fn, add documentation
-rw-r--r--embassy-net/src/lib.rs53
1 files changed, 41 insertions, 12 deletions
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs
index 9eea8cb4e..4922490d9 100644
--- a/embassy-net/src/lib.rs
+++ b/embassy-net/src/lib.rs
@@ -172,6 +172,7 @@ impl Config {
172 /// 172 ///
173 /// # Example 173 /// # Example
174 /// ```rust 174 /// ```rust
175 /// # use embassy_net::Config;
175 /// let _cfg = Config::dhcpv4(Default::default()); 176 /// let _cfg = Config::dhcpv4(Default::default());
176 /// ``` 177 /// ```
177 #[cfg(feature = "dhcpv4")] 178 #[cfg(feature = "dhcpv4")]
@@ -365,24 +366,52 @@ impl<D: Driver + 'static> Stack<D> {
365 v4_up || v6_up 366 v4_up || v6_up
366 } 367 }
367 368
368 /// Wait for the network stack to obtaine a valid IP configuration. 369 /// Wait for the network stack to obtain a valid IP configuration.
370 /// Returns instantly if [`Stack::is_config_up`] returns `true`.
371 ///
372 /// ## Watch out:
373 /// The Future is polled only when the [`Stack`] is running,
374 /// e.g. call `spawner.spawn(net_task(stack))`.
375 ///
376 /// `await`ing before will never yield!
377 ///
378 /// ## Example
379 /// ```ignore
380 /// let config = embassy_net::Config::dhcpv4(Default::default());
381 ///// Init network stack
382 /// let stack = &*make_static!(embassy_net::Stack::new(
383 /// device,
384 /// config,
385 /// make_static!(embassy_net::StackResources::<2>::new()),
386 /// seed
387 /// ));
388 /// // Launch network task
389 /// spawner.spawn(net_task(stack)).unwrap();
390 /// // Wait for DHCP config
391 /// stack.wait_config_up().await;
392 /// // use the network stack
393 /// // ...
394 /// ```
369 pub async fn wait_config_up(&self) { 395 pub async fn wait_config_up(&self) {
396 // If the config is up already, we can return immediately.
370 if self.is_config_up() { 397 if self.is_config_up() {
371 return; 398 return;
372 } 399 }
373 400
374 poll_fn(|cx| { 401 poll_fn(|cx| {
375 self.with_mut(|_, i| { 402 if self.is_config_up() {
376 debug!("poll_fn called"); 403 Poll::Ready(())
377 if self.is_config_up() { 404 } else {
378 debug!("poll_fn ready"); 405 // If the config is not up, we register a waker that is woken up
379 Poll::Ready(()) 406 // when a config is applied (static or DHCP).
380 } else { 407 trace!("Waiting for config up");
381 debug!("poll_fn pending"); 408
409 self.with_mut(|_, i| {
382 i.config_waker.register(cx.waker()); 410 i.config_waker.register(cx.waker());
383 Poll::Pending 411 });
384 } 412
385 }) 413 Poll::Pending
414 }
386 }) 415 })
387 .await; 416 .await;
388 } 417 }
@@ -731,7 +760,7 @@ impl<D: Driver + 'static> Inner<D> {
731 .get_mut::<smoltcp::socket::dns::Socket>(self.dns_socket) 760 .get_mut::<smoltcp::socket::dns::Socket>(self.dns_socket)
732 .update_servers(&dns_servers[..]); 761 .update_servers(&dns_servers[..]);
733 762
734 s.waker.wake(); 763 self.config_waker.wake();
735 } 764 }
736 765
737 fn poll(&mut self, cx: &mut Context<'_>, s: &mut SocketStack) { 766 fn poll(&mut self, cx: &mut Context<'_>, s: &mut SocketStack) {