1use crate::iface::Context;
15use crate::time::Instant;
16
17#[cfg(feature = "socket-dhcpv4")]
18mod dhcpv4;
19#[cfg(feature = "socket-icmp")]
20mod icmp;
21#[cfg(feature = "socket-raw")]
22mod raw;
23#[cfg(feature = "socket-tcp")]
24mod tcp;
25#[cfg(feature = "socket-udp")]
26mod udp;
27
28#[cfg(feature = "async")]
29mod waker;
30
31#[cfg(feature = "socket-dhcpv4")]
32pub use self::dhcpv4::{Config as Dhcpv4Config, Dhcpv4Socket, Event as Dhcpv4Event};
33#[cfg(feature = "socket-icmp")]
34pub use self::icmp::{Endpoint as IcmpEndpoint, IcmpPacketMetadata, IcmpSocket, IcmpSocketBuffer};
35#[cfg(feature = "socket-raw")]
36pub use self::raw::{RawPacketMetadata, RawSocket, RawSocketBuffer};
37#[cfg(feature = "socket-tcp")]
38pub use self::tcp::{SocketBuffer as TcpSocketBuffer, State as TcpState, TcpSocket};
39#[cfg(feature = "socket-udp")]
40pub use self::udp::{UdpPacketMetadata, UdpSocket, UdpSocketBuffer};
41
42#[cfg(feature = "async")]
43pub(crate) use self::waker::WakerRegistration;
44
45#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy)]
47#[cfg_attr(feature = "defmt", derive(defmt::Format))]
48pub(crate) enum PollAt {
49 Now,
51 Time(Instant),
53 Ingress,
55}
56
57#[derive(Debug)]
68pub enum Socket<'a> {
69 #[cfg(feature = "socket-raw")]
70 Raw(RawSocket<'a>),
71 #[cfg(feature = "socket-icmp")]
72 Icmp(IcmpSocket<'a>),
73 #[cfg(feature = "socket-udp")]
74 Udp(UdpSocket<'a>),
75 #[cfg(feature = "socket-tcp")]
76 Tcp(TcpSocket<'a>),
77 #[cfg(feature = "socket-dhcpv4")]
78 Dhcpv4(Dhcpv4Socket),
79}
80
81impl<'a> Socket<'a> {
82 pub(crate) fn poll_at(&self, cx: &mut Context) -> PollAt {
83 match self {
84 #[cfg(feature = "socket-raw")]
85 Socket::Raw(s) => s.poll_at(cx),
86 #[cfg(feature = "socket-icmp")]
87 Socket::Icmp(s) => s.poll_at(cx),
88 #[cfg(feature = "socket-udp")]
89 Socket::Udp(s) => s.poll_at(cx),
90 #[cfg(feature = "socket-tcp")]
91 Socket::Tcp(s) => s.poll_at(cx),
92 #[cfg(feature = "socket-dhcpv4")]
93 Socket::Dhcpv4(s) => s.poll_at(cx),
94 }
95 }
96}
97
98pub trait AnySocket<'a>: Sized {
100 fn upcast(self) -> Socket<'a>;
101 fn downcast<'c>(socket: &'c mut Socket<'a>) -> Option<&'c mut Self>;
102}
103
104macro_rules! from_socket {
105 ($socket:ty, $variant:ident) => {
106 impl<'a> AnySocket<'a> for $socket {
107 fn upcast(self) -> Socket<'a> {
108 Socket::$variant(self)
109 }
110
111 fn downcast<'c>(socket: &'c mut Socket<'a>) -> Option<&'c mut Self> {
112 #[allow(unreachable_patterns)]
113 match socket {
114 Socket::$variant(socket) => Some(socket),
115 _ => None,
116 }
117 }
118 }
119 };
120}
121
122#[cfg(feature = "socket-raw")]
123from_socket!(RawSocket<'a>, Raw);
124#[cfg(feature = "socket-icmp")]
125from_socket!(IcmpSocket<'a>, Icmp);
126#[cfg(feature = "socket-udp")]
127from_socket!(UdpSocket<'a>, Udp);
128#[cfg(feature = "socket-tcp")]
129from_socket!(TcpSocket<'a>, Tcp);
130#[cfg(feature = "socket-dhcpv4")]
131from_socket!(Dhcpv4Socket, Dhcpv4);