Skip to content

Commit 170ea22

Browse files
committed
Add preliminary support for zephyr embedded OS to libc and std.
1 parent ce5afa7 commit 170ea22

File tree

4 files changed

+218
-4
lines changed

4 files changed

+218
-4
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ const-extern-fn = []
3131
# use_std is deprecated, use `std` instead
3232
use_std = [ 'std' ]
3333

34-
[workspace]
35-
members = ["libc-test"]
34+
#[workspace]
35+
#members = ["libc-test"]

src/unix/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub type sighandler_t = ::size_t;
2929
pub type cc_t = ::c_uchar;
3030

3131
cfg_if! {
32-
if #[cfg(any(target_os = "espidf", target_os = "horizon"))] {
32+
if #[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "zephyr"))] {
3333
pub type uid_t = ::c_ushort;
3434
pub type gid_t = ::c_ushort;
3535
} else {

src/unix/newlib/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub type pthread_key_t = ::c_uint;
2727
pub type rlim_t = u32;
2828

2929
cfg_if! {
30-
if #[cfg(target_os = "horizon")] {
30+
if #[cfg(any(target_os = "horizon", target_os = "zephyr"))] {
3131
pub type sa_family_t = u16;
3232
} else {
3333
pub type sa_family_t = u8;
@@ -731,6 +731,9 @@ cfg_if! {
731731
} else if #[cfg(target_os = "horizon")] {
732732
mod horizon;
733733
pub use self::horizon::*;
734+
} else if #[cfg(target_os = "zephyr")] {
735+
mod zephyr;
736+
pub use self::zephyr::*;
734737
} else if #[cfg(target_arch = "arm")] {
735738
mod arm;
736739
pub use self::arm::*;

src/unix/newlib/zephyr/mod.rs

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
pub type c_char = i8;
2+
pub type c_long = i32;
3+
pub type c_ulong = u32;
4+
5+
pub type wchar_t = ::c_uint;
6+
7+
// pub type u_char = ::c_uchar;
8+
// pub type u_short = ::c_ushort;
9+
// pub type u_int = ::c_uint;
10+
// pub type u_long = c_ulong;
11+
// pub type ushort = ::c_ushort;
12+
// pub type uint = ::c_uint;
13+
// pub type ulong = c_ulong;
14+
pub type clock_t = c_ulong;
15+
// pub type daddr_t = c_long;
16+
// pub type caddr_t = *mut c_char;
17+
// pub type sbintime_t = ::c_longlong;
18+
// pub type sigset_t = ::c_ulong;
19+
20+
s! {
21+
pub struct cmsghdr {
22+
pub cmsg_len: ::socklen_t,
23+
pub cmsg_level: ::c_int,
24+
pub cmsg_type: ::c_int,
25+
}
26+
27+
pub struct msghdr {
28+
pub msg_name: *mut ::c_void,
29+
pub msg_namelen: ::socklen_t,
30+
pub msg_iov: *mut ::iovec,
31+
pub msg_iovlen: ::c_int,
32+
pub msg_control: *mut ::c_void,
33+
pub msg_controllen: ::size_t,
34+
pub msg_flags: ::c_int,
35+
}
36+
37+
pub struct sockaddr {
38+
pub sa_family: ::sa_family_t,
39+
pub data: [::c_char; NET_SOCKADDR_MAX_SIZE], // previously [:14]
40+
}
41+
42+
pub struct sockaddr_in6 {
43+
pub sin6_family: ::sa_family_t,
44+
pub sin6_port: ::in_port_t,
45+
pub sin6_addr: ::in6_addr,
46+
pub sin6_scope_id: u8,
47+
}
48+
49+
pub struct sockaddr_in {
50+
pub sin_family: ::sa_family_t,
51+
pub sin_port: ::in_port_t,
52+
pub sin_addr: ::in_addr,
53+
}
54+
55+
pub struct sockaddr_storage {
56+
pub ss_family: ::sa_family_t,
57+
pub data: [::c_char; NET_SOCKADDR_MAX_SIZE]
58+
}
59+
60+
pub struct sockaddr_un {
61+
pub sun_family: ::sa_family_t,
62+
pub sun_path: [::c_char; NET_SOCKADDR_MAX_SIZE]
63+
}
64+
}
65+
66+
pub const NET_SOCKADDR_MAX_SIZE: size_t = crate::mem::size_of::<sockaddr_in6>();
67+
68+
pub const PF_UNSPEC: ::c_long = 0;
69+
/**< Unspecified protocol family. */
70+
pub const PF_INET: ::c_long = 1;
71+
/**< IP protocol family version 4. */
72+
pub const PF_INET6: ::c_long = 2;
73+
/**< IP protocol family version 6. */
74+
pub const PF_PACKET: ::c_long = 3;
75+
/**< Packet family. */
76+
pub const PF_CAN: ::c_long = 4;
77+
/**< Controller Area Network. */
78+
pub const PF_NET_MGMT: ::c_long = 5;
79+
/**< Network management info. */
80+
pub const PF_LOCAL: ::c_long = 6;
81+
/**< Inter-process communication */
82+
pub const PF_UNIX: ::c_long = PF_LOCAL;
83+
/**< Inter-process communication */
84+
85+
pub const AF_UNSPEC: ::c_long = PF_UNSPEC;
86+
/**< Unspecified address family. */
87+
pub const AF_INET: ::c_long = PF_INET;
88+
/**< IP protocol family version 4. */
89+
pub const AF_INET6: ::c_long = PF_INET6;
90+
/**< IP protocol family version 6. */
91+
pub const AF_PACKET: ::c_long = PF_PACKET;
92+
/**< Packet family. */
93+
pub const AF_CAN: ::c_long = PF_CAN;
94+
/**< Controller Area Network. */
95+
pub const AF_NET_MGMT: ::c_long = PF_NET_MGMT;
96+
/**< Network management info. */
97+
pub const AF_LOCAL: ::c_long = PF_LOCAL;
98+
/**< Inter-process communication */
99+
pub const AF_UNIX: ::c_long = PF_UNIX;
100+
/**< Inter-process communication */
101+
102+
pub const FIONBIO: ::c_ulong = 2147772030;
103+
104+
pub const ZSOCK_POLLIN: ::c_short = 0x1;
105+
pub const ZSOCK_POLLPRI: ::c_short = 0x2;
106+
pub const ZSOCK_POLLHUP: ::c_short = 0x4;
107+
pub const ZSOCK_POLLERR: ::c_short = 0x8;
108+
pub const ZSOCK_POLLOUT: ::c_short = 0x10;
109+
pub const ZSOCK_POLLNVAL: ::c_short = 0x20;
110+
111+
pub const POLLIN: ::c_short = ZSOCK_POLLIN;
112+
pub const POLLPRI: ::c_short = ZSOCK_POLLPRI;
113+
pub const POLLHUP: ::c_short = ZSOCK_POLLHUP;
114+
pub const POLLERR: ::c_short = ZSOCK_POLLERR;
115+
pub const POLLOUT: ::c_short = ZSOCK_POLLOUT;
116+
pub const POLLNVAL: ::c_short = ZSOCK_POLLNVAL;
117+
118+
pub const _SC_PAGESIZE: ::c_int = 8;
119+
pub const _SC_GETPW_R_SIZE_MAX: ::c_int = 51;
120+
121+
pub const SIGHUP: ::c_int = 1; /* hangup */
122+
pub const SIGINT: ::c_int = 2; /* interrupt */
123+
pub const SIGQUIT: ::c_int = 3; /* quit */
124+
pub const SIGILL: ::c_int = 4; /* illegal instruction (not reset when caught) */
125+
pub const SIGTRAP: ::c_int = 5; /* trace trap (not reset when caught) */
126+
pub const SIGIOT: ::c_int = 6; /* IOT instruction */
127+
pub const SIGABRT: ::c_int = 6; /* used by abort, replace SIGIOT in the future */
128+
pub const SIGEMT: ::c_int = 7; /* EMT instruction */
129+
pub const SIGFPE: ::c_int = 8; /* floating point exception */
130+
pub const SIGKILL: ::c_int = 9; /* kill (cannot be caught or ignored) */
131+
pub const SIGBUS: ::c_int = 10; /* bus error */
132+
pub const SIGSEGV: ::c_int = 11; /* segmentation violation */
133+
pub const SIGSYS: ::c_int = 12; /* bad argument to system call */
134+
pub const SIGPIPE: ::c_int = 13; /* write on a pipe with no one to read it */
135+
pub const SIGALRM: ::c_int = 14; /* alarm clock */
136+
pub const SIGTERM: ::c_int = 15; /* software termination signal from kill */
137+
pub const SOL_SOCKET: ::c_int = 0xfff;
138+
139+
pub const ZSOCK_MSG_PEEK: ::c_long = 0x02;
140+
pub const ZSOCK_MSG_TRUNC: ::c_long = 0x20;
141+
pub const ZSOCK_MSG_DONTWAIT: ::c_long = 0x40;
142+
pub const ZSOCK_MSG_WAITALL: ::c_long = 0x100;
143+
144+
pub const MSG_PEEK: ::c_long = ZSOCK_MSG_PEEK;
145+
pub const MSG_TRUNC: ::c_long = ZSOCK_MSG_TRUNC;
146+
pub const MSG_DONTWAIT: ::c_long = ZSOCK_MSG_DONTWAIT;
147+
pub const MSG_WAITALL: ::c_long = ZSOCK_MSG_WAITALL;
148+
149+
pub const DNS_EAI_BADFLAGS: ::c_int = -1;
150+
pub const DNS_EAI_NONAME: ::c_int = -2;
151+
pub const DNS_EAI_AGAIN: ::c_int = -3;
152+
pub const DNS_EAI_FAIL: ::c_int = -4;
153+
pub const DNS_EAI_NODATA: ::c_int = -5;
154+
pub const DNS_EAI_FAMILY: ::c_int = -6;
155+
pub const DNS_EAI_SOCKTYPE: ::c_int = -7;
156+
pub const DNS_EAI_SERVICE: ::c_int = -8;
157+
pub const DNS_EAI_ADDRFAMILY: ::c_int = -9;
158+
pub const DNS_EAI_MEMORY: ::c_int = -10;
159+
pub const DNS_EAI_SYSTEM: ::c_int = -11;
160+
pub const DNS_EAI_OVERFLOW: ::c_int = -12;
161+
pub const DNS_EAI_INPROGRESS: ::c_int = -100;
162+
pub const DNS_EAI_CANCELED: ::c_int = -101;
163+
pub const DNS_EAI_NOTCANCELED: ::c_int = -102;
164+
pub const DNS_EAI_ALLDONE: ::c_int = -103;
165+
pub const DNS_EAI_IDN_ENCODE: ::c_int = -105;
166+
167+
/** POSIX wrapper for @ref DNS_EAI_BADFLAGS */
168+
pub const EAI_BADFLAGS: ::c_int = DNS_EAI_BADFLAGS;
169+
/** POSIX wrapper for @ref DNS_EAI_NONAME */
170+
pub const EAI_NONAME: ::c_int = DNS_EAI_NONAME;
171+
/** POSIX wrapper for @ref DNS_EAI_AGAIN */
172+
pub const EAI_AGAIN: ::c_int = DNS_EAI_AGAIN;
173+
/** POSIX wrapper for @ref DNS_EAI_FAIL */
174+
pub const EAI_FAIL: ::c_int = DNS_EAI_FAIL;
175+
/** POSIX wrapper for @ref DNS_EAI_NODATA */
176+
pub const EAI_NODATA: ::c_int = DNS_EAI_NODATA;
177+
/** POSIX wrapper for @ref DNS_EAI_MEMORY */
178+
pub const EAI_MEMORY: ::c_int = DNS_EAI_MEMORY;
179+
/** POSIX wrapper for @ref DNS_EAI_SYSTEM */
180+
pub const EAI_SYSTEM: ::c_int = DNS_EAI_SYSTEM;
181+
/** POSIX wrapper for @ref DNS_EAI_SERVICE */
182+
pub const EAI_SERVICE: ::c_int = DNS_EAI_SERVICE;
183+
/** POSIX wrapper for @ref DNS_EAI_SOCKTYPE */
184+
pub const EAI_SOCKTYPE: ::c_int = DNS_EAI_SOCKTYPE;
185+
/** POSIX wrapper for @ref DNS_EAI_FAMILY */
186+
pub const EAI_FAMILY: ::c_int = DNS_EAI_FAMILY;
187+
188+
pub const PTHREAD_STACK_MIN: ::size_t = 768;
189+
190+
pub const RTLD_DEFAULT: *mut ::c_void = 0 as *mut ::c_void;
191+
192+
extern "C" {
193+
pub fn pthread_create(
194+
native: *mut ::pthread_t,
195+
attr: *const ::pthread_attr_t,
196+
f: extern "C" fn(_: *mut ::c_void) -> *mut ::c_void,
197+
value: *mut ::c_void,
198+
) -> ::c_int;
199+
200+
// pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
201+
//
202+
// #[link_name = "lwip_sendmsg"]
203+
// pub fn sendmsg(s: ::c_int, msg: *const ::msghdr, flags: ::c_int) -> ::ssize_t;
204+
// #[link_name = "lwip_recvmsg"]
205+
// pub fn recvmsg(s: ::c_int, msg: *mut ::msghdr, flags: ::c_int) -> ::ssize_t;
206+
//
207+
// pub fn eventfd(initval: ::c_uint, flags: ::c_int) -> ::c_int;
208+
}
209+
210+
pub use crate::unix::newlib::generic::{sigset_t, stat};
211+
use size_t;

0 commit comments

Comments
 (0)