aboutsummaryrefslogtreecommitdiff
path: root/embassy-net
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-07-21 16:24:48 -0500
committerxoviat <[email protected]>2023-07-21 16:24:48 -0500
commit2cdd593290ea318d0ca9d71a270ac3b63e30470e (patch)
tree9dd8df6d49c26195c281e609a728d20a2ed716ec /embassy-net
parentc675208b8a90bee39e99c8cd3bb620b99c439482 (diff)
parent4d1d125f4157084668a949f9bc24e4417628f9fe (diff)
Merge branch 'main' of https://github.com/embassy-rs/embassy into mac
Diffstat (limited to 'embassy-net')
-rw-r--r--embassy-net/src/lib.rs104
1 files changed, 83 insertions, 21 deletions
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs
index ad98d7f68..81c751a2c 100644
--- a/embassy-net/src/lib.rs
+++ b/embassy-net/src/lib.rs
@@ -490,30 +490,78 @@ impl<D: Driver + 'static> Stack<D> {
490} 490}
491 491
492#[cfg(feature = "igmp")] 492#[cfg(feature = "igmp")]
493impl<D: Driver + smoltcp::phy::Device + 'static> Stack<D> { 493impl<D: Driver + 'static> Stack<D> {
494 /// Join a multicast group.
495 pub async fn join_multicast_group<T>(&self, addr: T) -> Result<bool, MulticastError>
496 where
497 T: Into<IpAddress>,
498 {
499 let addr = addr.into();
500
501 poll_fn(move |cx| self.poll_join_multicast_group(addr, cx)).await
502 }
503
494 /// Join a multicast group. 504 /// Join a multicast group.
495 pub fn join_multicast_group<T>(&self, addr: T) -> Result<bool, MulticastError> 505 ///
506 /// When the send queue is full, this method will return `Poll::Pending`
507 /// and register the current task to be notified when the queue has space available.
508 pub fn poll_join_multicast_group<T>(&self, addr: T, cx: &mut Context<'_>) -> Poll<Result<bool, MulticastError>>
496 where 509 where
497 T: Into<IpAddress>, 510 T: Into<IpAddress>,
498 { 511 {
499 let addr = addr.into(); 512 let addr = addr.into();
500 513
501 self.with_mut(|s, i| { 514 self.with_mut(|s, i| {
502 s.iface 515 let mut smoldev = DriverAdapter {
503 .join_multicast_group(&mut i.device, addr, instant_to_smoltcp(Instant::now())) 516 cx: Some(cx),
517 inner: &mut i.device,
518 };
519
520 match s
521 .iface
522 .join_multicast_group(&mut smoldev, addr, instant_to_smoltcp(Instant::now()))
523 {
524 Ok(announce_sent) => Poll::Ready(Ok(announce_sent)),
525 Err(MulticastError::Exhausted) => Poll::Pending,
526 Err(other) => Poll::Ready(Err(other)),
527 }
504 }) 528 })
505 } 529 }
506 530
507 /// Leave a multicast group. 531 /// Leave a multicast group.
508 pub fn leave_multicast_group<T>(&self, addr: T) -> Result<bool, MulticastError> 532 pub async fn leave_multicast_group<T>(&self, addr: T) -> Result<bool, MulticastError>
533 where
534 T: Into<IpAddress>,
535 {
536 let addr = addr.into();
537
538 poll_fn(move |cx| self.poll_leave_multicast_group(addr, cx)).await
539 }
540
541 /// Leave a multicast group.
542 ///
543 /// When the send queue is full, this method will return `Poll::Pending`
544 /// and register the current task to be notified when the queue has space available.
545 pub fn poll_leave_multicast_group<T>(&self, addr: T, cx: &mut Context<'_>) -> Poll<Result<bool, MulticastError>>
509 where 546 where
510 T: Into<IpAddress>, 547 T: Into<IpAddress>,
511 { 548 {
512 let addr = addr.into(); 549 let addr = addr.into();
513 550
514 self.with_mut(|s, i| { 551 self.with_mut(|s, i| {
515 s.iface 552 let mut smoldev = DriverAdapter {
516 .leave_multicast_group(&mut i.device, addr, instant_to_smoltcp(Instant::now())) 553 cx: Some(cx),
554 inner: &mut i.device,
555 };
556
557 match s
558 .iface
559 .leave_multicast_group(&mut smoldev, addr, instant_to_smoltcp(Instant::now()))
560 {
561 Ok(leave_sent) => Poll::Ready(Ok(leave_sent)),
562 Err(MulticastError::Exhausted) => Poll::Pending,
563 Err(other) => Poll::Ready(Err(other)),
564 }
517 }) 565 })
518 } 566 }
519 567
@@ -542,11 +590,14 @@ impl<D: Driver + 'static> Inner<D> {
542 590
543 debug!(" IP address: {}", config.address); 591 debug!(" IP address: {}", config.address);
544 s.iface.update_ip_addrs(|addrs| { 592 s.iface.update_ip_addrs(|addrs| {
545 if addrs.is_empty() { 593 if let Some((index, _)) = addrs
546 addrs.push(IpCidr::Ipv4(config.address)).unwrap(); 594 .iter()
547 } else { 595 .enumerate()
548 addrs[0] = IpCidr::Ipv4(config.address); 596 .find(|(_, &addr)| matches!(addr, IpCidr::Ipv4(_)))
597 {
598 addrs.remove(index);
549 } 599 }
600 addrs.push(IpCidr::Ipv4(config.address)).unwrap();
550 }); 601 });
551 602
552 #[cfg(feature = "medium-ethernet")] 603 #[cfg(feature = "medium-ethernet")]
@@ -581,11 +632,14 @@ impl<D: Driver + 'static> Inner<D> {
581 632
582 debug!(" IP address: {}", config.address); 633 debug!(" IP address: {}", config.address);
583 s.iface.update_ip_addrs(|addrs| { 634 s.iface.update_ip_addrs(|addrs| {
584 if addrs.is_empty() { 635 if let Some((index, _)) = addrs
585 addrs.push(IpCidr::Ipv6(config.address)).unwrap(); 636 .iter()
586 } else { 637 .enumerate()
587 addrs[0] = IpCidr::Ipv6(config.address); 638 .find(|(_, &addr)| matches!(addr, IpCidr::Ipv6(_)))
639 {
640 addrs.remove(index);
588 } 641 }
642 addrs.push(IpCidr::Ipv6(config.address)).unwrap();
589 }); 643 });
590 644
591 #[cfg(feature = "medium-ethernet")] 645 #[cfg(feature = "medium-ethernet")]
@@ -653,13 +707,21 @@ impl<D: Driver + 'static> Inner<D> {
653 socket.set_retry_config(config.retry_config); 707 socket.set_retry_config(config.retry_config);
654 } 708 }
655 709
656 #[allow(unused)] // used only with dhcp 710 #[cfg(feature = "dhcpv4")]
657 fn unapply_config(&mut self, s: &mut SocketStack) { 711 fn unapply_config_v4(&mut self, s: &mut SocketStack) {
658 #[cfg(feature = "medium-ethernet")] 712 #[cfg(feature = "medium-ethernet")]
659 let medium = self.device.capabilities().medium; 713 let medium = self.device.capabilities().medium;
660
661 debug!("Lost IP configuration"); 714 debug!("Lost IP configuration");
662 s.iface.update_ip_addrs(|ip_addrs| ip_addrs.clear()); 715 s.iface.update_ip_addrs(|ip_addrs| {
716 #[cfg(feature = "proto-ipv4")]
717 if let Some((index, _)) = ip_addrs
718 .iter()
719 .enumerate()
720 .find(|(_, &addr)| matches!(addr, IpCidr::Ipv4(_)))
721 {
722 ip_addrs.remove(index);
723 }
724 });
663 #[cfg(feature = "medium-ethernet")] 725 #[cfg(feature = "medium-ethernet")]
664 if medium == Medium::Ethernet { 726 if medium == Medium::Ethernet {
665 #[cfg(feature = "proto-ipv4")] 727 #[cfg(feature = "proto-ipv4")]
@@ -706,7 +768,7 @@ impl<D: Driver + 'static> Inner<D> {
706 if self.link_up { 768 if self.link_up {
707 match socket.poll() { 769 match socket.poll() {
708 None => {} 770 None => {}
709 Some(dhcpv4::Event::Deconfigured) => self.unapply_config(s), 771 Some(dhcpv4::Event::Deconfigured) => self.unapply_config_v4(s),
710 Some(dhcpv4::Event::Configured(config)) => { 772 Some(dhcpv4::Event::Configured(config)) => {
711 let config = StaticConfigV4 { 773 let config = StaticConfigV4 {
712 address: config.address, 774 address: config.address,
@@ -718,7 +780,7 @@ impl<D: Driver + 'static> Inner<D> {
718 } 780 }
719 } else if old_link_up { 781 } else if old_link_up {
720 socket.reset(); 782 socket.reset();
721 self.unapply_config(s); 783 self.unapply_config_v4(s);
722 } 784 }
723 } 785 }
724 //if old_link_up || self.link_up { 786 //if old_link_up || self.link_up {