Skip to content

Commit e5a4d26

Browse files
authored
Support for the ESP-IDF framework
Smoke tested on esp32c3 dev board. I've also tested a similar patch backported to v0.4.9 with much greater functionality including tokio + mio with other patches I've been working on and it's fully working. Closes #379
1 parent 9ce984d commit e5a4d26

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

src/lib.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,16 @@ impl Type {
270270
pub const DCCP: Type = Type(sys::SOCK_DCCP);
271271

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

277277
/// Type corresponding to `SOCK_RAW`.
278-
#[cfg(all(feature = "all", not(target_os = "redox")))]
279-
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "redox")))))]
278+
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
279+
#[cfg_attr(
280+
docsrs,
281+
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
282+
)]
280283
pub const RAW: Type = Type(sys::SOCK_RAW);
281284
}
282285

@@ -374,6 +377,7 @@ impl RecvFlags {
374377
///
375378
/// On Unix this corresponds to the `MSG_TRUNC` flag.
376379
/// On Windows this corresponds to the `WSAEMSGSIZE` error code.
380+
#[cfg(not(target_os = "espidf"))]
377381
pub const fn is_truncated(self) -> bool {
378382
self.0 & sys::MSG_TRUNC != 0
379383
}
@@ -428,6 +432,7 @@ pub struct TcpKeepalive {
428432
target_os = "redox",
429433
target_os = "solaris",
430434
target_os = "nto",
435+
target_os = "espidf",
431436
)))]
432437
interval: Option<Duration>,
433438
#[cfg(not(any(
@@ -436,6 +441,7 @@ pub struct TcpKeepalive {
436441
target_os = "solaris",
437442
target_os = "windows",
438443
target_os = "nto",
444+
target_os = "espidf",
439445
)))]
440446
retries: Option<u32>,
441447
}
@@ -450,6 +456,7 @@ impl TcpKeepalive {
450456
target_os = "redox",
451457
target_os = "solaris",
452458
target_os = "nto",
459+
target_os = "espidf",
453460
)))]
454461
interval: None,
455462
#[cfg(not(any(
@@ -458,6 +465,7 @@ impl TcpKeepalive {
458465
target_os = "solaris",
459466
target_os = "windows",
460467
target_os = "nto",
468+
target_os = "espidf",
461469
)))]
462470
retries: None,
463471
}

src/socket.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ fn set_common_flags(socket: Socket) -> io::Result<Socket> {
790790
target_os = "linux",
791791
target_os = "netbsd",
792792
target_os = "openbsd",
793+
target_os = "espidf",
793794
))
794795
))]
795796
socket._set_cloexec(true)?;
@@ -1108,8 +1109,11 @@ impl Socket {
11081109
/// For more information about this option, see [`set_header_included`].
11091110
///
11101111
/// [`set_header_included`]: Socket::set_header_included
1111-
#[cfg(all(feature = "all", not(target_os = "redox")))]
1112-
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "redox")))))]
1112+
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
1113+
#[cfg_attr(
1114+
docsrs,
1115+
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
1116+
)]
11131117
pub fn header_included(&self) -> io::Result<bool> {
11141118
unsafe {
11151119
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IP, sys::IP_HDRINCL)
@@ -1132,8 +1136,11 @@ impl Socket {
11321136
any(target_os = "fuchsia", target_os = "illumos", target_os = "solaris"),
11331137
allow(rustdoc::broken_intra_doc_links)
11341138
)]
1135-
#[cfg(all(feature = "all", not(target_os = "redox")))]
1136-
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "redox")))))]
1139+
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
1140+
#[cfg_attr(
1141+
docsrs,
1142+
doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf")))))
1143+
)]
11371144
pub fn set_header_included(&self, included: bool) -> io::Result<()> {
11381145
unsafe {
11391146
setsockopt(
@@ -1237,6 +1244,7 @@ impl Socket {
12371244
target_os = "redox",
12381245
target_os = "solaris",
12391246
target_os = "nto",
1247+
target_os = "espidf",
12401248
)))]
12411249
pub fn join_multicast_v4_n(
12421250
&self,
@@ -1268,6 +1276,7 @@ impl Socket {
12681276
target_os = "redox",
12691277
target_os = "solaris",
12701278
target_os = "nto",
1279+
target_os = "espidf",
12711280
)))]
12721281
pub fn leave_multicast_v4_n(
12731282
&self,
@@ -1300,6 +1309,7 @@ impl Socket {
13001309
target_os = "redox",
13011310
target_os = "fuchsia",
13021311
target_os = "nto",
1312+
target_os = "espidf",
13031313
)))]
13041314
pub fn join_ssm_v4(
13051315
&self,
@@ -1335,6 +1345,7 @@ impl Socket {
13351345
target_os = "redox",
13361346
target_os = "fuchsia",
13371347
target_os = "nto",
1348+
target_os = "espidf",
13381349
)))]
13391350
pub fn leave_ssm_v4(
13401351
&self,
@@ -1512,6 +1523,7 @@ impl Socket {
15121523
target_os = "solaris",
15131524
target_os = "haiku",
15141525
target_os = "nto",
1526+
target_os = "espidf",
15151527
)))]
15161528
pub fn set_recv_tos(&self, recv_tos: bool) -> io::Result<()> {
15171529
unsafe {
@@ -1540,6 +1552,7 @@ impl Socket {
15401552
target_os = "solaris",
15411553
target_os = "haiku",
15421554
target_os = "nto",
1555+
target_os = "espidf",
15431556
)))]
15441557
pub fn recv_tos(&self) -> io::Result<bool> {
15451558
unsafe {
@@ -1755,6 +1768,7 @@ impl Socket {
17551768
target_os = "redox",
17561769
target_os = "solaris",
17571770
target_os = "haiku",
1771+
target_os = "espidf",
17581772
)))]
17591773
pub fn recv_tclass_v6(&self) -> io::Result<bool> {
17601774
unsafe {
@@ -1777,6 +1791,7 @@ impl Socket {
17771791
target_os = "redox",
17781792
target_os = "solaris",
17791793
target_os = "haiku",
1794+
target_os = "espidf",
17801795
)))]
17811796
pub fn set_recv_tclass_v6(&self, recv_tclass: bool) -> io::Result<()> {
17821797
unsafe {

src/sys/unix.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ pub(crate) use libc::{AF_INET, AF_INET6, AF_UNIX};
8181
// Used in `Type`.
8282
#[cfg(all(feature = "all", target_os = "linux"))]
8383
pub(crate) use libc::SOCK_DCCP;
84-
#[cfg(all(feature = "all", not(target_os = "redox")))]
84+
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
8585
pub(crate) use libc::SOCK_RAW;
86-
#[cfg(feature = "all")]
86+
#[cfg(all(feature = "all", not(target_os = "espidf")))]
8787
pub(crate) use libc::SOCK_SEQPACKET;
8888
pub(crate) use libc::{SOCK_DGRAM, SOCK_STREAM};
8989
// Used in `Protocol`.
@@ -111,8 +111,10 @@ pub(crate) use libc::{
111111
sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t,
112112
};
113113
// Used in `RecvFlags`.
114+
#[cfg(not(any(target_os = "redox", target_os = "espidf")))]
115+
pub(crate) use libc::MSG_TRUNC;
114116
#[cfg(not(target_os = "redox"))]
115-
pub(crate) use libc::{MSG_TRUNC, SO_OOBINLINE};
117+
pub(crate) use libc::SO_OOBINLINE;
116118
// Used in `Socket`.
117119
#[cfg(not(target_os = "nto"))]
118120
pub(crate) use libc::ipv6_mreq as Ipv6Mreq;
@@ -125,6 +127,7 @@ pub(crate) use libc::ipv6_mreq as Ipv6Mreq;
125127
target_os = "redox",
126128
target_os = "solaris",
127129
target_os = "haiku",
130+
target_os = "espidf",
128131
)))]
129132
pub(crate) use libc::IPV6_RECVTCLASS;
130133
#[cfg(all(feature = "all", not(target_os = "redox")))]
@@ -140,6 +143,7 @@ pub(crate) use libc::IP_HDRINCL;
140143
target_os = "solaris",
141144
target_os = "haiku",
142145
target_os = "nto",
146+
target_os = "espidf",
143147
)))]
144148
pub(crate) use libc::IP_RECVTOS;
145149
#[cfg(not(any(
@@ -178,6 +182,7 @@ pub(crate) use libc::{
178182
target_os = "redox",
179183
target_os = "fuchsia",
180184
target_os = "nto",
185+
target_os = "espidf",
181186
)))]
182187
pub(crate) use libc::{
183188
ip_mreq_source as IpMreqSource, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP,
@@ -329,6 +334,7 @@ type IovLen = usize;
329334
target_os = "solaris",
330335
target_os = "tvos",
331336
target_os = "watchos",
337+
target_os = "espidf",
332338
))]
333339
type IovLen = c_int;
334340

@@ -471,10 +477,11 @@ impl_debug!(
471477
libc::SOCK_DGRAM,
472478
#[cfg(all(feature = "all", target_os = "linux"))]
473479
libc::SOCK_DCCP,
474-
#[cfg(not(target_os = "redox"))]
480+
#[cfg(not(any(target_os = "redox", target_os = "espidf")))]
475481
libc::SOCK_RAW,
476-
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
482+
#[cfg(not(any(target_os = "redox", target_os = "haiku", target_os = "espidf")))]
477483
libc::SOCK_RDM,
484+
#[cfg(not(target_os = "espidf"))]
478485
libc::SOCK_SEQPACKET,
479486
/* TODO: add these optional bit OR-ed flags:
480487
#[cfg(any(
@@ -538,6 +545,7 @@ impl RecvFlags {
538545
/// On Unix this corresponds to the `MSG_EOR` flag.
539546
///
540547
/// [`SEQPACKET`]: Type::SEQPACKET
548+
#[cfg(not(target_os = "espidf"))]
541549
pub const fn is_end_of_record(self) -> bool {
542550
self.0 & libc::MSG_EOR != 0
543551
}
@@ -556,11 +564,13 @@ impl RecvFlags {
556564
#[cfg(not(target_os = "redox"))]
557565
impl std::fmt::Debug for RecvFlags {
558566
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
559-
f.debug_struct("RecvFlags")
560-
.field("is_end_of_record", &self.is_end_of_record())
561-
.field("is_out_of_band", &self.is_out_of_band())
562-
.field("is_truncated", &self.is_truncated())
563-
.finish()
567+
let mut s = f.debug_struct("RecvFlags");
568+
#[cfg(not(target_os = "espidf"))]
569+
s.field("is_end_of_record", &self.is_end_of_record());
570+
s.field("is_out_of_band", &self.is_out_of_band());
571+
#[cfg(not(target_os = "espidf"))]
572+
s.field("is_truncated", &self.is_truncated());
573+
s.finish()
564574
}
565575
}
566576

@@ -1264,6 +1274,7 @@ pub(crate) fn from_in6_addr(addr: in6_addr) -> Ipv6Addr {
12641274
target_os = "redox",
12651275
target_os = "solaris",
12661276
target_os = "nto",
1277+
target_os = "espidf",
12671278
)))]
12681279
pub(crate) const fn to_mreqn(
12691280
multiaddr: &Ipv4Addr,

0 commit comments

Comments
 (0)