diff options
| author | Leon Camus <[email protected]> | 2023-03-08 12:37:00 +0100 |
|---|---|---|
| committer | Leon Camus <[email protected]> | 2023-03-08 12:37:00 +0100 |
| commit | e484cb1b875adc7a0865299df5167f94302fcf49 (patch) | |
| tree | cac633b95faf9c20312a70adedc807175241f8d0 /embassy-net | |
| parent | 993875e11fc58a476b1e08d4aba752b83bb2b885 (diff) | |
refactor: Multicast method modifiers on stack to public
revert: udp.rs
Diffstat (limited to 'embassy-net')
| -rw-r--r-- | embassy-net/src/lib.rs | 6 | ||||
| -rw-r--r-- | embassy-net/src/udp.rs | 47 |
2 files changed, 18 insertions, 35 deletions
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 05bbd07f2..7b9d0e773 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs | |||
| @@ -306,7 +306,7 @@ impl<D: Driver + 'static> Stack<D> { | |||
| 306 | 306 | ||
| 307 | #[cfg(feature = "igmp")] | 307 | #[cfg(feature = "igmp")] |
| 308 | impl<D: Driver + smoltcp::phy::Device + 'static> Stack<D> { | 308 | impl<D: Driver + smoltcp::phy::Device + 'static> Stack<D> { |
| 309 | pub(crate) fn join_multicast_group<T>(&self, addr: T) -> Result<bool, smoltcp::iface::MulticastError> | 309 | pub fn join_multicast_group<T>(&self, addr: T) -> Result<bool, smoltcp::iface::MulticastError> |
| 310 | where | 310 | where |
| 311 | T: Into<IpAddress>, | 311 | T: Into<IpAddress>, |
| 312 | { | 312 | { |
| @@ -318,7 +318,7 @@ impl<D: Driver + smoltcp::phy::Device + 'static> Stack<D> { | |||
| 318 | }) | 318 | }) |
| 319 | } | 319 | } |
| 320 | 320 | ||
| 321 | pub(crate) fn leave_multicast_group<T>(&self, addr: T) -> Result<bool, smoltcp::iface::MulticastError> | 321 | pub fn leave_multicast_group<T>(&self, addr: T) -> Result<bool, smoltcp::iface::MulticastError> |
| 322 | where | 322 | where |
| 323 | T: Into<IpAddress>, | 323 | T: Into<IpAddress>, |
| 324 | { | 324 | { |
| @@ -330,7 +330,7 @@ impl<D: Driver + smoltcp::phy::Device + 'static> Stack<D> { | |||
| 330 | }) | 330 | }) |
| 331 | } | 331 | } |
| 332 | 332 | ||
| 333 | pub(crate) fn has_multicast_group<T: Into<IpAddress>>(&self, addr: T) -> bool { | 333 | pub fn has_multicast_group<T: Into<IpAddress>>(&self, addr: T) -> bool { |
| 334 | self.socket.borrow().iface.has_multicast_group(addr) | 334 | self.socket.borrow().iface.has_multicast_group(addr) |
| 335 | } | 335 | } |
| 336 | } | 336 | } |
diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs index 12bdbf402..0ee8c6e19 100644 --- a/embassy-net/src/udp.rs +++ b/embassy-net/src/udp.rs | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | use core::cell::RefCell; | ||
| 1 | use core::future::poll_fn; | 2 | use core::future::poll_fn; |
| 2 | use core::mem; | 3 | use core::mem; |
| 3 | use core::task::Poll; | 4 | use core::task::Poll; |
| @@ -7,7 +8,7 @@ use smoltcp::iface::{Interface, SocketHandle}; | |||
| 7 | use smoltcp::socket::udp::{self, PacketMetadata}; | 8 | use smoltcp::socket::udp::{self, PacketMetadata}; |
| 8 | use smoltcp::wire::{IpEndpoint, IpListenEndpoint}; | 9 | use smoltcp::wire::{IpEndpoint, IpListenEndpoint}; |
| 9 | 10 | ||
| 10 | use crate::Stack; | 11 | use crate::{SocketStack, Stack}; |
| 11 | 12 | ||
| 12 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] | 13 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] |
| 13 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 14 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -25,13 +26,13 @@ pub enum Error { | |||
| 25 | NoRoute, | 26 | NoRoute, |
| 26 | } | 27 | } |
| 27 | 28 | ||
| 28 | pub struct UdpSocket<'a, D: Driver> { | 29 | pub struct UdpSocket<'a> { |
| 29 | stack: &'a Stack<D>, | 30 | stack: &'a RefCell<SocketStack>, |
| 30 | handle: SocketHandle, | 31 | handle: SocketHandle, |
| 31 | } | 32 | } |
| 32 | 33 | ||
| 33 | impl<'a, D: Driver> UdpSocket<'a, D> { | 34 | impl<'a> UdpSocket<'a> { |
| 34 | pub fn new( | 35 | pub fn new<D: Driver>( |
| 35 | stack: &'a Stack<D>, | 36 | stack: &'a Stack<D>, |
| 36 | rx_meta: &'a mut [PacketMetadata], | 37 | rx_meta: &'a mut [PacketMetadata], |
| 37 | rx_buffer: &'a mut [u8], | 38 | rx_buffer: &'a mut [u8], |
| @@ -49,7 +50,10 @@ impl<'a, D: Driver> UdpSocket<'a, D> { | |||
| 49 | udp::PacketBuffer::new(tx_meta, tx_buffer), | 50 | udp::PacketBuffer::new(tx_meta, tx_buffer), |
| 50 | )); | 51 | )); |
| 51 | 52 | ||
| 52 | Self { stack, handle } | 53 | Self { |
| 54 | stack: &stack.socket, | ||
| 55 | handle, | ||
| 56 | } | ||
| 53 | } | 57 | } |
| 54 | 58 | ||
| 55 | pub fn bind<T>(&mut self, endpoint: T) -> Result<(), BindError> | 59 | pub fn bind<T>(&mut self, endpoint: T) -> Result<(), BindError> |
| @@ -60,7 +64,7 @@ impl<'a, D: Driver> UdpSocket<'a, D> { | |||
| 60 | 64 | ||
| 61 | if endpoint.port == 0 { | 65 | if endpoint.port == 0 { |
| 62 | // If user didn't specify port allocate a dynamic port. | 66 | // If user didn't specify port allocate a dynamic port. |
| 63 | endpoint.port = self.stack.socket.borrow_mut().get_local_port(); | 67 | endpoint.port = self.stack.borrow_mut().get_local_port(); |
| 64 | } | 68 | } |
| 65 | 69 | ||
| 66 | match self.with_mut(|s, _| s.bind(endpoint)) { | 70 | match self.with_mut(|s, _| s.bind(endpoint)) { |
| @@ -71,13 +75,13 @@ impl<'a, D: Driver> UdpSocket<'a, D> { | |||
| 71 | } | 75 | } |
| 72 | 76 | ||
| 73 | fn with<R>(&self, f: impl FnOnce(&udp::Socket, &Interface) -> R) -> R { | 77 | fn with<R>(&self, f: impl FnOnce(&udp::Socket, &Interface) -> R) -> R { |
| 74 | let s = &*self.stack.socket.borrow(); | 78 | let s = &*self.stack.borrow(); |
| 75 | let socket = s.sockets.get::<udp::Socket>(self.handle); | 79 | let socket = s.sockets.get::<udp::Socket>(self.handle); |
| 76 | f(socket, &s.iface) | 80 | f(socket, &s.iface) |
| 77 | } | 81 | } |
| 78 | 82 | ||
| 79 | fn with_mut<R>(&self, f: impl FnOnce(&mut udp::Socket, &mut Interface) -> R) -> R { | 83 | fn with_mut<R>(&self, f: impl FnOnce(&mut udp::Socket, &mut Interface) -> R) -> R { |
| 80 | let s = &mut *self.stack.socket.borrow_mut(); | 84 | let s = &mut *self.stack.borrow_mut(); |
| 81 | let socket = s.sockets.get_mut::<udp::Socket>(self.handle); | 85 | let socket = s.sockets.get_mut::<udp::Socket>(self.handle); |
| 82 | let res = f(socket, &mut s.iface); | 86 | let res = f(socket, &mut s.iface); |
| 83 | s.waker.wake(); | 87 | s.waker.wake(); |
| @@ -139,29 +143,8 @@ impl<'a, D: Driver> UdpSocket<'a, D> { | |||
| 139 | } | 143 | } |
| 140 | } | 144 | } |
| 141 | 145 | ||
| 142 | #[cfg(feature = "igmp")] | 146 | impl Drop for UdpSocket<'_> { |
| 143 | impl<'a, D: Driver + smoltcp::phy::Device + 'static> UdpSocket<'a, D> { | ||
| 144 | pub fn join_multicast_group<T>(&self, addr: T) -> Result<bool, smoltcp::iface::MulticastError> | ||
| 145 | where | ||
| 146 | T: Into<smoltcp::wire::IpAddress>, | ||
| 147 | { | ||
| 148 | self.stack.join_multicast_group(addr) | ||
| 149 | } | ||
| 150 | |||
| 151 | pub fn leave_multicast_group<T>(&self, addr: T) -> Result<bool, smoltcp::iface::MulticastError> | ||
| 152 | where | ||
| 153 | T: Into<smoltcp::wire::IpAddress>, | ||
| 154 | { | ||
| 155 | self.stack.leave_multicast_group(addr) | ||
| 156 | } | ||
| 157 | |||
| 158 | pub fn has_multicast_group<T: Into<smoltcp::wire::IpAddress>>(&self, addr: T) -> bool { | ||
| 159 | self.stack.has_multicast_group(addr) | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | impl<D: Driver> Drop for UdpSocket<'_, D> { | ||
| 164 | fn drop(&mut self) { | 147 | fn drop(&mut self) { |
| 165 | self.stack.socket.borrow_mut().sockets.remove(self.handle); | 148 | self.stack.borrow_mut().sockets.remove(self.handle); |
| 166 | } | 149 | } |
| 167 | } | 150 | } |
