Skip to content

Commit b942bc8

Browse files
author
imarkov
committed
Support for the ESP-IDF framework (Xtensa and RiscV arch)
1 parent a1e0f47 commit b942bc8

File tree

3 files changed

+70
-25
lines changed

3 files changed

+70
-25
lines changed

src/unix/mod.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ pub const ATF_PUBL: ::c_int = 0x08;
297297
pub const ATF_USETRAILERS: ::c_int = 0x10;
298298

299299
cfg_if! {
300-
if #[cfg(target_os = "l4re")] {
301-
// required libraries for L4Re are linked externally, ATM
300+
if #[cfg(any(target_os = "l4re", target_os = "espidf"))] {
301+
// required libraries for L4Re and the ESP-IDF framework are linked externally, ATM
302302
} else if #[cfg(feature = "std")] {
303303
// cargo build, don't pull in anything extra as the libstd dep
304304
// already pulls in all libs.
@@ -576,6 +576,7 @@ extern "C" {
576576
)))]
577577
#[cfg_attr(target_os = "netbsd", link_name = "__socket30")]
578578
#[cfg_attr(target_os = "illumos", link_name = "__xnet_socket")]
579+
#[cfg_attr(target_os = "espidf", link_name = "lwip_socket")]
579580
pub fn socket(domain: ::c_int, ty: ::c_int, protocol: ::c_int) -> ::c_int;
580581
#[cfg(not(all(
581582
libc_cfg_target_vendor,
@@ -587,11 +588,13 @@ extern "C" {
587588
link_name = "connect$UNIX2003"
588589
)]
589590
#[cfg_attr(target_os = "illumos", link_name = "__xnet_connect")]
591+
#[cfg_attr(target_os = "espidf", link_name = "lwip_connect")]
590592
pub fn connect(socket: ::c_int, address: *const sockaddr, len: socklen_t) -> ::c_int;
591593
#[cfg_attr(
592594
all(target_os = "macos", target_arch = "x86"),
593595
link_name = "listen$UNIX2003"
594596
)]
597+
#[cfg_attr(target_os = "espidf", link_name = "lwip_listen")]
595598
pub fn listen(socket: ::c_int, backlog: ::c_int) -> ::c_int;
596599
#[cfg(not(all(
597600
libc_cfg_target_vendor,
@@ -602,6 +605,7 @@ extern "C" {
602605
all(target_os = "macos", target_arch = "x86"),
603606
link_name = "accept$UNIX2003"
604607
)]
608+
#[cfg_attr(target_os = "espidf", link_name = "lwip_accept")]
605609
pub fn accept(socket: ::c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> ::c_int;
606610
#[cfg(not(all(
607611
libc_cfg_target_vendor,
@@ -612,6 +616,7 @@ extern "C" {
612616
all(target_os = "macos", target_arch = "x86"),
613617
link_name = "getpeername$UNIX2003"
614618
)]
619+
#[cfg_attr(target_os = "espidf", link_name = "lwip_getpeername")]
615620
pub fn getpeername(
616621
socket: ::c_int,
617622
address: *mut sockaddr,
@@ -626,11 +631,13 @@ extern "C" {
626631
all(target_os = "macos", target_arch = "x86"),
627632
link_name = "getsockname$UNIX2003"
628633
)]
634+
#[cfg_attr(target_os = "espidf", link_name = "lwip_getsockname")]
629635
pub fn getsockname(
630636
socket: ::c_int,
631637
address: *mut sockaddr,
632638
address_len: *mut socklen_t,
633639
) -> ::c_int;
640+
#[cfg_attr(target_os = "espidf", link_name = "lwip_setsockopt")]
634641
pub fn setsockopt(
635642
socket: ::c_int,
636643
level: ::c_int,
@@ -659,6 +666,7 @@ extern "C" {
659666
link_name = "sendto$UNIX2003"
660667
)]
661668
#[cfg_attr(target_os = "illumos", link_name = "__xnet_sendto")]
669+
#[cfg_attr(target_os = "espidf", link_name = "lwip_sendto")]
662670
pub fn sendto(
663671
socket: ::c_int,
664672
buf: *const ::c_void,
@@ -667,6 +675,7 @@ extern "C" {
667675
addr: *const sockaddr,
668676
addrlen: socklen_t,
669677
) -> ::ssize_t;
678+
#[cfg_attr(target_os = "espidf", link_name = "lwip_shutdown")]
670679
pub fn shutdown(socket: ::c_int, how: ::c_int) -> ::c_int;
671680

672681
#[cfg_attr(
@@ -1084,6 +1093,7 @@ extern "C" {
10841093
all(target_os = "macos", target_arch = "x86"),
10851094
link_name = "pthread_rwlock_init$UNIX2003"
10861095
)]
1096+
#[cfg_attr(target_os = "espidf", link_name = "pthread_mutex_init")]
10871097
pub fn pthread_rwlock_init(
10881098
lock: *mut pthread_rwlock_t,
10891099
attr: *const pthread_rwlockattr_t,
@@ -1092,36 +1102,45 @@ extern "C" {
10921102
all(target_os = "macos", target_arch = "x86"),
10931103
link_name = "pthread_rwlock_destroy$UNIX2003"
10941104
)]
1105+
#[cfg_attr(target_os = "espidf", link_name = "pthread_mutex_destroy")]
10951106
pub fn pthread_rwlock_destroy(lock: *mut pthread_rwlock_t) -> ::c_int;
10961107
#[cfg_attr(
10971108
all(target_os = "macos", target_arch = "x86"),
10981109
link_name = "pthread_rwlock_rdlock$UNIX2003"
10991110
)]
1111+
#[cfg_attr(target_os = "espidf", link_name = "pthread_mutex_lock")]
11001112
pub fn pthread_rwlock_rdlock(lock: *mut pthread_rwlock_t) -> ::c_int;
11011113
#[cfg_attr(
11021114
all(target_os = "macos", target_arch = "x86"),
11031115
link_name = "pthread_rwlock_tryrdlock$UNIX2003"
11041116
)]
1117+
#[cfg_attr(target_os = "espidf", link_name = "pthread_mutex_trylock")]
11051118
pub fn pthread_rwlock_tryrdlock(lock: *mut pthread_rwlock_t) -> ::c_int;
11061119
#[cfg_attr(
11071120
all(target_os = "macos", target_arch = "x86"),
11081121
link_name = "pthread_rwlock_wrlock$UNIX2003"
11091122
)]
1123+
#[cfg_attr(target_os = "espidf", link_name = "pthread_mutex_lock")]
11101124
pub fn pthread_rwlock_wrlock(lock: *mut pthread_rwlock_t) -> ::c_int;
11111125
#[cfg_attr(
11121126
all(target_os = "macos", target_arch = "x86"),
11131127
link_name = "pthread_rwlock_trywrlock$UNIX2003"
11141128
)]
1129+
#[cfg_attr(target_os = "espidf", link_name = "pthread_mutex_trylock")]
11151130
pub fn pthread_rwlock_trywrlock(lock: *mut pthread_rwlock_t) -> ::c_int;
11161131
#[cfg_attr(
11171132
all(target_os = "macos", target_arch = "x86"),
11181133
link_name = "pthread_rwlock_unlock$UNIX2003"
11191134
)]
1135+
#[cfg_attr(target_os = "espidf", link_name = "pthread_mutex_unlock")]
11201136
pub fn pthread_rwlock_unlock(lock: *mut pthread_rwlock_t) -> ::c_int;
1137+
#[cfg_attr(target_os = "espidf", link_name = "pthread_mutexattr_init")]
11211138
pub fn pthread_rwlockattr_init(attr: *mut pthread_rwlockattr_t) -> ::c_int;
1139+
#[cfg_attr(target_os = "espidf", link_name = "pthread_mutexattr_destroy")]
11221140
pub fn pthread_rwlockattr_destroy(attr: *mut pthread_rwlockattr_t) -> ::c_int;
11231141

11241142
#[cfg_attr(target_os = "illumos", link_name = "__xnet_getsockopt")]
1143+
#[cfg_attr(target_os = "espidf", link_name = "lwip_getsockopt")]
11251144
pub fn getsockopt(
11261145
sockfd: ::c_int,
11271146
level: ::c_int,
@@ -1147,6 +1166,7 @@ extern "C" {
11471166
target_vendor = "nintendo"
11481167
)))]
11491168
#[cfg_attr(target_os = "illumos", link_name = "__xnet_getaddrinfo")]
1169+
#[cfg_attr(target_os = "espidf", link_name = "lwip_getaddrinfo")]
11501170
pub fn getaddrinfo(
11511171
node: *const c_char,
11521172
service: *const c_char,
@@ -1158,6 +1178,7 @@ extern "C" {
11581178
target_arch = "powerpc",
11591179
target_vendor = "nintendo"
11601180
)))]
1181+
#[cfg_attr(target_os = "espidf", link_name = "lwip_freeaddrinfo")]
11611182
pub fn freeaddrinfo(res: *mut addrinfo);
11621183
pub fn gai_strerror(errcode: ::c_int) -> *const ::c_char;
11631184
#[cfg_attr(
@@ -1233,11 +1254,13 @@ extern "C" {
12331254
all(target_os = "macos", target_arch = "x86"),
12341255
link_name = "send$UNIX2003"
12351256
)]
1257+
#[cfg_attr(target_os = "espidf", link_name = "lwip_send")]
12361258
pub fn send(socket: ::c_int, buf: *const ::c_void, len: ::size_t, flags: ::c_int) -> ::ssize_t;
12371259
#[cfg_attr(
12381260
all(target_os = "macos", target_arch = "x86"),
12391261
link_name = "recv$UNIX2003"
12401262
)]
1263+
#[cfg_attr(target_os = "espidf", link_name = "lwip_recv")]
12411264
pub fn recv(socket: ::c_int, buf: *mut ::c_void, len: ::size_t, flags: ::c_int) -> ::ssize_t;
12421265
#[cfg_attr(
12431266
all(target_os = "macos", target_arch = "x86"),

src/unix/newlib/xtensa/mod.rs renamed to src/unix/newlib/espidf/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,20 @@ pub const MSG_WAITALL: ::c_int = 0x02;
8484
pub const MSG_MORE: ::c_int = 0x10;
8585
pub const MSG_NOSIGNAL: ::c_int = 0x20;
8686

87+
pub const PTHREAD_STACK_MIN: ::size_t = 768;
88+
8789
extern "C" {
90+
pub fn pthread_create(
91+
native: *mut ::pthread_t,
92+
attr: *const ::pthread_attr_t,
93+
f: extern "C" fn(_: *mut ::c_void) -> *mut ::c_void,
94+
value: *mut ::c_void,
95+
) -> ::c_int;
96+
97+
pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
98+
99+
#[link_name = "lwip_sendmsg"]
88100
pub fn sendmsg(s: ::c_int, msg: *const ::msghdr, flags: ::c_int) -> ::ssize_t;
101+
#[link_name = "lwip_recvmsg"]
89102
pub fn recvmsg(s: ::c_int, msg: *mut ::msghdr, flags: ::c_int) -> ::ssize_t;
90-
91-
pub fn writev(s: ::c_int, iov: *const ::iovec, iovcnt: ::c_int) -> ::c_int;
92-
pub fn readv(fd: ::c_int, iov: *const ::iovec, iovcnt: ::c_int) -> ::ssize_t;
93103
}

src/unix/newlib/mod.rs

+32-20
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ s! {
3333
pub ai_protocol: ::c_int,
3434
pub ai_addrlen: socklen_t,
3535

36-
#[cfg(not(all(libc_cfg_target_vendor, target_arch = "powerpc",
37-
target_vendor = "nintendo")))]
38-
#[cfg(target_arch = "xtensa")]
36+
#[cfg(target_os = "espidf")]
3937
pub ai_addr: *mut sockaddr,
4038

4139
pub ai_canonname: *mut ::c_char,
4240

43-
#[cfg(not(all(libc_cfg_target_vendor, target_arch = "powerpc",
44-
target_vendor = "nintendo")))]
45-
#[cfg(not(target_arch = "xtensa"))]
41+
#[cfg(not(any(
42+
target_os = "espidf",
43+
all(libc_cfg_target_vendor, target_arch = "powerpc", target_vendor = "nintendo"))))]
4644
pub ai_addr: *mut sockaddr,
4745

4846
pub ai_next: *mut addrinfo,
@@ -232,23 +230,37 @@ s! {
232230
// unverified constants
233231
align_const! {
234232
pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
235-
size: [0; __SIZEOF_PTHREAD_MUTEX_T],
233+
size: [__PTHREAD_INITIALIZER_BYTE; __SIZEOF_PTHREAD_MUTEX_T],
236234
};
237235
pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
238-
size: [0; __SIZEOF_PTHREAD_COND_T],
236+
size: [__PTHREAD_INITIALIZER_BYTE; __SIZEOF_PTHREAD_COND_T],
239237
};
240238
pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
241-
size: [0; __SIZEOF_PTHREAD_RWLOCK_T],
239+
size: [__PTHREAD_INITIALIZER_BYTE; __SIZEOF_PTHREAD_RWLOCK_T],
242240
};
243241
}
244242
pub const NCCS: usize = 32;
245-
pub const __SIZEOF_PTHREAD_ATTR_T: usize = 56;
246-
pub const __SIZEOF_PTHREAD_MUTEX_T: usize = 40;
247-
pub const __SIZEOF_PTHREAD_MUTEXATTR_T: usize = 4;
248-
pub const __SIZEOF_PTHREAD_COND_T: usize = 48;
249-
pub const __SIZEOF_PTHREAD_CONDATTR_T: usize = 4;
250-
pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = 56;
251-
pub const __SIZEOF_PTHREAD_RWLOCKATTR_T: usize = 8;
243+
cfg_if! {
244+
if #[cfg(target_os = "espidf")] {
245+
const __PTHREAD_INITIALIZER_BYTE: u8 = 0xff;
246+
pub const __SIZEOF_PTHREAD_ATTR_T: usize = 32;
247+
pub const __SIZEOF_PTHREAD_MUTEX_T: usize = 4;
248+
pub const __SIZEOF_PTHREAD_MUTEXATTR_T: usize = 12;
249+
pub const __SIZEOF_PTHREAD_COND_T: usize = 4;
250+
pub const __SIZEOF_PTHREAD_CONDATTR_T: usize = 8;
251+
pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = __SIZEOF_PTHREAD_MUTEX_T;
252+
pub const __SIZEOF_PTHREAD_RWLOCKATTR_T: usize = __SIZEOF_PTHREAD_MUTEXATTR_T;
253+
} else {
254+
const __PTHREAD_INITIALIZER_BYTE: u8 = 0;
255+
pub const __SIZEOF_PTHREAD_ATTR_T: usize = 56;
256+
pub const __SIZEOF_PTHREAD_MUTEX_T: usize = 40;
257+
pub const __SIZEOF_PTHREAD_MUTEXATTR_T: usize = 4;
258+
pub const __SIZEOF_PTHREAD_COND_T: usize = 48;
259+
pub const __SIZEOF_PTHREAD_CONDATTR_T: usize = 4;
260+
pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = 56;
261+
pub const __SIZEOF_PTHREAD_RWLOCKATTR_T: usize = 8;
262+
}
263+
}
252264
pub const __SIZEOF_PTHREAD_BARRIER_T: usize = 32;
253265
pub const __SIZEOF_PTHREAD_BARRIERATTR_T: usize = 4;
254266
pub const __PTHREAD_MUTEX_HAVE_PREV: usize = 1;
@@ -688,15 +700,15 @@ extern "C" {
688700
}
689701

690702
cfg_if! {
691-
if #[cfg(target_arch = "arm")] {
703+
if #[cfg(target_os = "espidf")] {
704+
mod espidf;
705+
pub use self::espidf::*;
706+
} else if #[cfg(target_arch = "arm")] {
692707
mod arm;
693708
pub use self::arm::*;
694709
} else if #[cfg(target_arch = "aarch64")] {
695710
mod aarch64;
696711
pub use self::aarch64::*;
697-
} else if #[cfg(target_arch = "xtensa")] {
698-
mod xtensa;
699-
pub use self::xtensa::*;
700712
} else if #[cfg(target_arch = "powerpc")] {
701713
mod powerpc;
702714
pub use self::powerpc::*;

0 commit comments

Comments
 (0)