Skip to content

Commit

Permalink
Support for the ESP-IDF framework
Browse files Browse the repository at this point in the history
  • Loading branch information
imarkov authored and ivmarkov committed Aug 4, 2022
1 parent d97e78a commit afeb723
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
29 changes: 21 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,16 @@ impl Type {
pub const DGRAM: Type = Type(sys::SOCK_DGRAM);

/// Type corresponding to `SOCK_SEQPACKET`.
#[cfg(feature = "all")]
#[cfg_attr(docsrs, doc(cfg(feature = "all")))]
#[cfg(all(feature = "all", not(target_os = "espidf")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "espidf")))))]
pub const SEQPACKET: Type = Type(sys::SOCK_SEQPACKET);

/// Type corresponding to `SOCK_RAW`.
#[cfg(all(feature = "all", not(target_os = "redox")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "redox")))))]
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
)]
pub const RAW: Type = Type(sys::SOCK_RAW);
}

Expand Down Expand Up @@ -328,9 +331,14 @@ impl<'a> DerefMut for MaybeUninitSlice<'a> {
#[derive(Debug, Clone)]
pub struct TcpKeepalive {
time: Option<Duration>,
#[cfg(not(any(target_os = "redox", target_os = "solaris")))]
#[cfg(not(any(target_os = "redox", target_os = "solaris", target_os = "espidf")))]
interval: Option<Duration>,
#[cfg(not(any(target_os = "redox", target_os = "solaris", target_os = "windows")))]
#[cfg(not(any(
target_os = "redox",
target_os = "solaris",
target_os = "windows",
target_os = "espidf"
)))]
retries: Option<u32>,
}

Expand All @@ -339,9 +347,14 @@ impl TcpKeepalive {
pub const fn new() -> TcpKeepalive {
TcpKeepalive {
time: None,
#[cfg(not(any(target_os = "redox", target_os = "solaris")))]
#[cfg(not(any(target_os = "redox", target_os = "solaris", target_os = "espidf")))]
interval: None,
#[cfg(not(any(target_os = "redox", target_os = "solaris", target_os = "windows")))]
#[cfg(not(any(
target_os = "redox",
target_os = "solaris",
target_os = "windows",
target_os = "espidf"
)))]
retries: None,
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ impl From<SocketAddrV4> for SockAddr {
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
target_os = "openbsd",
target_os = "espidf"
))]
sin_len: 0,
};
Expand Down Expand Up @@ -269,7 +270,8 @@ impl From<SocketAddrV6> for SockAddr {
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
target_os = "openbsd",
target_os = "espidf"
))]
sin6_len: 0,
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
Expand Down
21 changes: 17 additions & 4 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ fn set_common_flags(socket: Socket) -> io::Result<Socket> {
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "espidf",
))
))]
socket._set_cloexec(true)?;
Expand Down Expand Up @@ -1015,8 +1016,11 @@ impl Socket {
/// For more information about this option, see [`set_header_included`].
///
/// [`set_header_included`]: Socket::set_header_included
#[cfg(all(feature = "all", not(target_os = "redox")))]
#[cfg_attr(docsrs, doc(all(feature = "all", not(target_os = "redox"))))]
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
#[cfg_attr(
docsrs,
doc(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))
)]
pub fn header_included(&self) -> io::Result<bool> {
unsafe {
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IP, sys::IP_HDRINCL)
Expand All @@ -1035,8 +1039,11 @@ impl Socket {
/// [raw(7)]: https://man7.org/linux/man-pages/man7/raw.7.html
/// [`IP_TTL`]: Socket::set_ttl
/// [`IP_TOS`]: Socket::set_tos
#[cfg(all(feature = "all", not(target_os = "redox")))]
#[cfg_attr(docsrs, doc(all(feature = "all", not(target_os = "redox"))))]
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
#[cfg_attr(
docsrs,
doc(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))
)]
pub fn set_header_included(&self, included: bool) -> io::Result<()> {
unsafe {
setsockopt(
Expand Down Expand Up @@ -1138,6 +1145,7 @@ impl Socket {
target_os = "openbsd",
target_os = "redox",
target_os = "solaris",
target_os = "espidf",
)))]
pub fn join_multicast_v4_n(
&self,
Expand Down Expand Up @@ -1167,6 +1175,7 @@ impl Socket {
target_os = "openbsd",
target_os = "redox",
target_os = "solaris",
target_os = "espidf",
)))]
pub fn leave_multicast_v4_n(
&self,
Expand Down Expand Up @@ -1196,6 +1205,7 @@ impl Socket {
target_os = "netbsd",
target_os = "redox",
target_os = "fuchsia",
target_os = "espidf",
)))]
pub fn join_ssm_v4(
&self,
Expand Down Expand Up @@ -1228,6 +1238,7 @@ impl Socket {
target_os = "netbsd",
target_os = "redox",
target_os = "fuchsia",
target_os = "espidf",
)))]
pub fn leave_ssm_v4(
&self,
Expand Down Expand Up @@ -1401,6 +1412,7 @@ impl Socket {
target_os = "openbsd",
target_os = "redox",
target_os = "solaris",
target_os = "espidf",
)))]
pub fn set_recv_tos(&self, recv_tos: bool) -> io::Result<()> {
let recv_tos = if recv_tos { 1 } else { 0 };
Expand All @@ -1427,6 +1439,7 @@ impl Socket {
target_os = "openbsd",
target_os = "redox",
target_os = "solaris",
target_os = "espidf",
)))]
pub fn recv_tos(&self) -> io::Result<bool> {
unsafe {
Expand Down
33 changes: 25 additions & 8 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ pub(crate) use libc::c_int;
// Used in `Domain`.
pub(crate) use libc::{AF_INET, AF_INET6};
// Used in `Type`.
#[cfg(all(feature = "all", not(target_os = "redox")))]
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
pub(crate) use libc::SOCK_RAW;
#[cfg(feature = "all")]
#[cfg(all(feature = "all", not(target_os = "espidf")))]
pub(crate) use libc::SOCK_SEQPACKET;
pub(crate) use libc::{SOCK_DGRAM, SOCK_STREAM};
// Used in `Protocol`.
Expand All @@ -72,10 +72,15 @@ pub(crate) use libc::{
sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t,
};
// Used in `RecvFlags`.
#[cfg(not(any(target_os = "redox", target_os = "espidf")))]
pub(crate) use libc::MSG_TRUNC;
#[cfg(not(target_os = "redox"))]
pub(crate) use libc::{MSG_TRUNC, SO_OOBINLINE};
// Used in `Socket`.
#[cfg(all(feature = "all", not(target_os = "redox")))]
pub(crate) use libc::SO_OOBINLINE;
#[cfg(target_os = "espidf")]
pub(crate) const MSG_TRUNC: libc::c_int = 4; // TODO: Expose in libc for ESP-IDF/LwIP
// Used in `Socket`.
// Used in `Socket`.
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
pub(crate) use libc::IP_HDRINCL;
#[cfg(not(any(
target_os = "fuchsia",
Expand All @@ -84,6 +89,7 @@ pub(crate) use libc::IP_HDRINCL;
target_os = "openbsd",
target_os = "redox",
target_os = "solaris",
target_os = "espidf",
)))]
pub(crate) use libc::IP_RECVTOS;
#[cfg(not(any(
Expand All @@ -109,6 +115,7 @@ pub(crate) use libc::{
target_os = "netbsd",
target_os = "redox",
target_os = "fuchsia",
target_os = "espidf",
)))]
pub(crate) use libc::{
ip_mreq_source as IpMreqSource, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP,
Expand Down Expand Up @@ -216,6 +223,7 @@ type IovLen = usize;
target_os = "netbsd",
target_os = "openbsd",
target_os = "solaris",
target_os = "espidf",
target_vendor = "apple",
))]
type IovLen = c_int;
Expand Down Expand Up @@ -355,10 +363,11 @@ impl_debug!(
Type,
libc::SOCK_STREAM,
libc::SOCK_DGRAM,
#[cfg(not(target_os = "redox"))]
#[cfg(not(any(target_os = "redox", target_os = "espidf")))]
libc::SOCK_RAW,
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
#[cfg(not(any(target_os = "redox", target_os = "haiku", target_os = "espidf")))]
libc::SOCK_RDM,
#[cfg(not(target_os = "espidf"))]
libc::SOCK_SEQPACKET,
/* TODO: add these optional bit OR-ed flags:
#[cfg(any(
Expand Down Expand Up @@ -403,7 +412,14 @@ impl RecvFlags {
///
/// On Unix this corresponds to the MSG_EOR flag.
pub const fn is_end_of_record(self) -> bool {
self.0 & libc::MSG_EOR != 0
// TODO: Expose this constant in libc for the ESP-IDF/LwIP framework
#[cfg(target_os = "espidf")]
const MSG_EOR: libc::c_int = 8;

#[cfg(not(target_os = "espidf"))]
use libc::MSG_EOR;

self.0 & MSG_EOR != 0
}

/// Check if the message contains out-of-band data.
Expand Down Expand Up @@ -1025,6 +1041,7 @@ pub(crate) fn from_in6_addr(addr: in6_addr) -> Ipv6Addr {
target_os = "openbsd",
target_os = "redox",
target_os = "solaris",
target_os = "espidf",
)))]
pub(crate) fn to_mreqn(
multiaddr: &Ipv4Addr,
Expand Down

0 comments on commit afeb723

Please sign in to comment.