Skip to content

Commit 71fbf6b

Browse files
committed
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 71fbf6b

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

src/lib.rs

Lines changed: 11 additions & 4 deletions
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

@@ -428,6 +431,7 @@ pub struct TcpKeepalive {
428431
target_os = "redox",
429432
target_os = "solaris",
430433
target_os = "nto",
434+
target_os = "espidf",
431435
)))]
432436
interval: Option<Duration>,
433437
#[cfg(not(any(
@@ -436,6 +440,7 @@ pub struct TcpKeepalive {
436440
target_os = "solaris",
437441
target_os = "windows",
438442
target_os = "nto",
443+
target_os = "espidf",
439444
)))]
440445
retries: Option<u32>,
441446
}
@@ -450,6 +455,7 @@ impl TcpKeepalive {
450455
target_os = "redox",
451456
target_os = "solaris",
452457
target_os = "nto",
458+
target_os = "espidf",
453459
)))]
454460
interval: None,
455461
#[cfg(not(any(
@@ -458,6 +464,7 @@ impl TcpKeepalive {
458464
target_os = "solaris",
459465
target_os = "windows",
460466
target_os = "nto",
467+
target_os = "espidf",
461468
)))]
462469
retries: None,
463470
}

src/socket.rs

Lines changed: 13 additions & 4 deletions
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,8 @@ 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(docsrs, doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))))]
11131114
pub fn header_included(&self) -> io::Result<bool> {
11141115
unsafe {
11151116
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IP, sys::IP_HDRINCL)
@@ -1132,8 +1133,8 @@ impl Socket {
11321133
any(target_os = "fuchsia", target_os = "illumos", target_os = "solaris"),
11331134
allow(rustdoc::broken_intra_doc_links)
11341135
)]
1135-
#[cfg(all(feature = "all", not(target_os = "redox")))]
1136-
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "redox")))))]
1136+
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
1137+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))))]
11371138
pub fn set_header_included(&self, included: bool) -> io::Result<()> {
11381139
unsafe {
11391140
setsockopt(
@@ -1237,6 +1238,7 @@ impl Socket {
12371238
target_os = "redox",
12381239
target_os = "solaris",
12391240
target_os = "nto",
1241+
target_os = "espidf",
12401242
)))]
12411243
pub fn join_multicast_v4_n(
12421244
&self,
@@ -1268,6 +1270,7 @@ impl Socket {
12681270
target_os = "redox",
12691271
target_os = "solaris",
12701272
target_os = "nto",
1273+
target_os = "espidf",
12711274
)))]
12721275
pub fn leave_multicast_v4_n(
12731276
&self,
@@ -1300,6 +1303,7 @@ impl Socket {
13001303
target_os = "redox",
13011304
target_os = "fuchsia",
13021305
target_os = "nto",
1306+
target_os = "espidf",
13031307
)))]
13041308
pub fn join_ssm_v4(
13051309
&self,
@@ -1335,6 +1339,7 @@ impl Socket {
13351339
target_os = "redox",
13361340
target_os = "fuchsia",
13371341
target_os = "nto",
1342+
target_os = "espidf",
13381343
)))]
13391344
pub fn leave_ssm_v4(
13401345
&self,
@@ -1512,6 +1517,7 @@ impl Socket {
15121517
target_os = "solaris",
15131518
target_os = "haiku",
15141519
target_os = "nto",
1520+
target_os = "espidf",
15151521
)))]
15161522
pub fn set_recv_tos(&self, recv_tos: bool) -> io::Result<()> {
15171523
unsafe {
@@ -1540,6 +1546,7 @@ impl Socket {
15401546
target_os = "solaris",
15411547
target_os = "haiku",
15421548
target_os = "nto",
1549+
target_os = "espidf",
15431550
)))]
15441551
pub fn recv_tos(&self) -> io::Result<bool> {
15451552
unsafe {
@@ -1755,6 +1762,7 @@ impl Socket {
17551762
target_os = "redox",
17561763
target_os = "solaris",
17571764
target_os = "haiku",
1765+
target_os = "espidf",
17581766
)))]
17591767
pub fn recv_tclass_v6(&self) -> io::Result<bool> {
17601768
unsafe {
@@ -1777,6 +1785,7 @@ impl Socket {
17771785
target_os = "redox",
17781786
target_os = "solaris",
17791787
target_os = "haiku",
1788+
target_os = "espidf",
17801789
)))]
17811790
pub fn set_recv_tclass_v6(&self, recv_tclass: bool) -> io::Result<()> {
17821791
unsafe {

src/sys/unix.rs

Lines changed: 27 additions & 6 deletions
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,11 +111,19 @@ 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;
118+
#[cfg(target_os = "espidf")]
119+
pub(crate) const MSG_TRUNC: libc::c_int = 4; // TODO: Expose in libc for ESP-IDF/LwIP
120+
// Used in `Socket`.
121+
// Used in `Socket`.
116122
// Used in `Socket`.
117123
#[cfg(not(target_os = "nto"))]
118124
pub(crate) use libc::ipv6_mreq as Ipv6Mreq;
125+
#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
126+
pub(crate) use libc::IP_HDRINCL;
119127
#[cfg(not(any(
120128
target_os = "dragonfly",
121129
target_os = "fuchsia",
@@ -125,6 +133,7 @@ pub(crate) use libc::ipv6_mreq as Ipv6Mreq;
125133
target_os = "redox",
126134
target_os = "solaris",
127135
target_os = "haiku",
136+
target_os = "espidf",
128137
)))]
129138
pub(crate) use libc::IPV6_RECVTCLASS;
130139
#[cfg(all(feature = "all", not(target_os = "redox")))]
@@ -140,6 +149,7 @@ pub(crate) use libc::IP_HDRINCL;
140149
target_os = "solaris",
141150
target_os = "haiku",
142151
target_os = "nto",
152+
target_os = "espidf",
143153
)))]
144154
pub(crate) use libc::IP_RECVTOS;
145155
#[cfg(not(any(
@@ -178,6 +188,7 @@ pub(crate) use libc::{
178188
target_os = "redox",
179189
target_os = "fuchsia",
180190
target_os = "nto",
191+
target_os = "espidf",
181192
)))]
182193
pub(crate) use libc::{
183194
ip_mreq_source as IpMreqSource, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP,
@@ -329,6 +340,7 @@ type IovLen = usize;
329340
target_os = "solaris",
330341
target_os = "tvos",
331342
target_os = "watchos",
343+
target_os = "espidf",
332344
))]
333345
type IovLen = c_int;
334346

@@ -471,10 +483,11 @@ impl_debug!(
471483
libc::SOCK_DGRAM,
472484
#[cfg(all(feature = "all", target_os = "linux"))]
473485
libc::SOCK_DCCP,
474-
#[cfg(not(target_os = "redox"))]
486+
#[cfg(not(any(target_os = "redox", target_os = "espidf")))]
475487
libc::SOCK_RAW,
476-
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
488+
#[cfg(not(any(target_os = "redox", target_os = "haiku", target_os = "espidf")))]
477489
libc::SOCK_RDM,
490+
#[cfg(not(target_os = "espidf"))]
478491
libc::SOCK_SEQPACKET,
479492
/* TODO: add these optional bit OR-ed flags:
480493
#[cfg(any(
@@ -539,7 +552,14 @@ impl RecvFlags {
539552
///
540553
/// [`SEQPACKET`]: Type::SEQPACKET
541554
pub const fn is_end_of_record(self) -> bool {
542-
self.0 & libc::MSG_EOR != 0
555+
// TODO: Expose this constant in libc for the ESP-IDF/LwIP framework
556+
#[cfg(target_os = "espidf")]
557+
const MSG_EOR: libc::c_int = 8;
558+
559+
#[cfg(not(target_os = "espidf"))]
560+
use libc::MSG_EOR;
561+
562+
self.0 & MSG_EOR != 0
543563
}
544564

545565
/// Check if the message contains out-of-band data.
@@ -1264,6 +1284,7 @@ pub(crate) fn from_in6_addr(addr: in6_addr) -> Ipv6Addr {
12641284
target_os = "redox",
12651285
target_os = "solaris",
12661286
target_os = "nto",
1287+
target_os = "espidf",
12671288
)))]
12681289
pub(crate) const fn to_mreqn(
12691290
multiaddr: &Ipv4Addr,

0 commit comments

Comments
 (0)