Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support Zephyr OS #3184

Closed
wants to merge 10 commits into from
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ cfg_if! {

mod xous;
pub use xous::*;
} else if #[cfg(target_os = "zephyr")] {
mod zephyr;
pub use zephyr::*;
} else {
// non-supported targets: empty...
}
Expand Down
2 changes: 1 addition & 1 deletion src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub type sighandler_t = ::size_t;
pub type cc_t = ::c_uchar;

cfg_if! {
if #[cfg(any(target_os = "espidf", target_os = "horizon"))] {
if #[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "zephyr"))] {
pub type uid_t = ::c_ushort;
pub type gid_t = ::c_ushort;
} else if #[cfg(target_os = "nto")] {
Expand Down
5 changes: 4 additions & 1 deletion src/unix/newlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub type pthread_key_t = ::c_uint;
pub type rlim_t = u32;

cfg_if! {
if #[cfg(target_os = "horizon")] {
if #[cfg(any(target_os = "horizon", target_os = "zephyr"))] {
pub type sa_family_t = u16;
} else {
pub type sa_family_t = u8;
Expand Down Expand Up @@ -730,6 +730,9 @@ cfg_if! {
} else if #[cfg(target_os = "horizon")] {
mod horizon;
pub use self::horizon::*;
} else if #[cfg(target_os = "zephyr")] {
mod zephyr;
pub use self::zephyr::*;
} else if #[cfg(target_os = "vita")] {
mod vita;
pub use self::vita::*;
Expand Down
212 changes: 212 additions & 0 deletions src/unix/newlib/zephyr/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
#![allow(dead_code)]

pub type c_char = i8;
pub type c_long = i32;
pub type c_ulong = u32;

pub type wchar_t = ::c_uint;

// pub type u_char = ::c_uchar;
// pub type u_short = ::c_ushort;
// pub type u_int = ::c_uint;
// pub type u_long = c_ulong;
// pub type ushort = ::c_ushort;
// pub type uint = ::c_uint;
// pub type ulong = c_ulong;
pub type clock_t = c_ulong;
// pub type daddr_t = c_long;
// pub type caddr_t = *mut c_char;
// pub type sbintime_t = ::c_longlong;
// pub type sigset_t = ::c_ulong;

s! {
pub struct cmsghdr {
pub cmsg_len: ::socklen_t,
pub cmsg_level: ::c_int,
pub cmsg_type: ::c_int,
}

pub struct msghdr {
pub msg_name: *mut ::c_void,
pub msg_namelen: ::socklen_t,
pub msg_iov: *mut ::iovec,
pub msg_iovlen: ::c_int,
pub msg_control: *mut ::c_void,
pub msg_controllen: ::size_t,
pub msg_flags: ::c_int,
}

pub struct sockaddr {
pub sa_family: ::sa_family_t,
pub data: [::c_char; NET_SOCKADDR_MAX_SIZE], // previously [:14]
}

pub struct sockaddr_in6 {
pub sin6_family: ::sa_family_t,
pub sin6_port: ::in_port_t,
pub sin6_addr: ::in6_addr,
pub sin6_scope_id: u8,
}

pub struct sockaddr_in {
pub sin_family: ::sa_family_t,
pub sin_port: ::in_port_t,
pub sin_addr: ::in_addr,
}

pub struct sockaddr_storage {
pub ss_family: ::sa_family_t,
pub data: [::c_char; NET_SOCKADDR_MAX_SIZE]
}

pub struct sockaddr_un {
pub sun_family: ::sa_family_t,
pub sun_path: [::c_char; NET_SOCKADDR_MAX_SIZE]
}
}

pub const NET_SOCKADDR_MAX_SIZE: ::size_t = crate::mem::size_of::<sockaddr_in6>();

pub const PF_UNSPEC: c_long = 0;
/**< Unspecified protocol family. */
pub const PF_INET: c_long = 1;
/**< IP protocol family version 4. */
pub const PF_INET6: c_long = 2;
/**< IP protocol family version 6. */
pub const PF_PACKET: c_long = 3;
/**< Packet family. */
pub const PF_CAN: c_long = 4;
/**< Controller Area Network. */
pub const PF_NET_MGMT: c_long = 5;
/**< Network management info. */
pub const PF_LOCAL: c_long = 6;
/**< Inter-process communication */
pub const PF_UNIX: c_long = PF_LOCAL;
/**< Inter-process communication */

pub const AF_UNSPEC: c_long = PF_UNSPEC;
/**< Unspecified address family. */
pub const AF_INET: c_long = PF_INET;
/**< IP protocol family version 4. */
pub const AF_INET6: c_long = PF_INET6;
/**< IP protocol family version 6. */
pub const AF_PACKET: c_long = PF_PACKET;
/**< Packet family. */
pub const AF_CAN: c_long = PF_CAN;
/**< Controller Area Network. */
pub const AF_NET_MGMT: c_long = PF_NET_MGMT;
/**< Network management info. */
pub const AF_LOCAL: c_long = PF_LOCAL;
/**< Inter-process communication */
pub const AF_UNIX: c_long = PF_UNIX;
/**< Inter-process communication */

pub const FIONBIO: ::c_ulong = 2147772030;

pub const ZSOCK_POLLIN: ::c_short = 0x1;
pub const ZSOCK_POLLPRI: ::c_short = 0x2;
pub const ZSOCK_POLLHUP: ::c_short = 0x4;
pub const ZSOCK_POLLERR: ::c_short = 0x8;
pub const ZSOCK_POLLOUT: ::c_short = 0x10;
pub const ZSOCK_POLLNVAL: ::c_short = 0x20;

pub const POLLIN: ::c_short = ZSOCK_POLLIN;
pub const POLLPRI: ::c_short = ZSOCK_POLLPRI;
pub const POLLHUP: ::c_short = ZSOCK_POLLHUP;
pub const POLLERR: ::c_short = ZSOCK_POLLERR;
pub const POLLOUT: ::c_short = ZSOCK_POLLOUT;
pub const POLLNVAL: ::c_short = ZSOCK_POLLNVAL;

pub const _SC_PAGESIZE: ::c_int = 8;
pub const _SC_GETPW_R_SIZE_MAX: ::c_int = 51;

pub const SIGHUP: ::c_int = 1; /* hangup */
pub const SIGINT: ::c_int = 2; /* interrupt */
pub const SIGQUIT: ::c_int = 3; /* quit */
pub const SIGILL: ::c_int = 4; /* illegal instruction (not reset when caught) */
pub const SIGTRAP: ::c_int = 5; /* trace trap (not reset when caught) */
pub const SIGIOT: ::c_int = 6; /* IOT instruction */
pub const SIGABRT: ::c_int = 6; /* used by abort, replace SIGIOT in the future */
pub const SIGEMT: ::c_int = 7; /* EMT instruction */
pub const SIGFPE: ::c_int = 8; /* floating point exception */
pub const SIGKILL: ::c_int = 9; /* kill (cannot be caught or ignored) */
pub const SIGBUS: ::c_int = 10; /* bus error */
pub const SIGSEGV: ::c_int = 11; /* segmentation violation */
pub const SIGSYS: ::c_int = 12; /* bad argument to system call */
pub const SIGPIPE: ::c_int = 13; /* write on a pipe with no one to read it */
pub const SIGALRM: ::c_int = 14; /* alarm clock */
pub const SIGTERM: ::c_int = 15; /* software termination signal from kill */
pub const SOL_SOCKET: ::c_int = 0xfff;

pub const ZSOCK_MSG_PEEK: c_long = 0x02;
pub const ZSOCK_MSG_TRUNC: c_long = 0x20;
pub const ZSOCK_MSG_DONTWAIT: c_long = 0x40;
pub const ZSOCK_MSG_WAITALL: c_long = 0x100;

pub const MSG_PEEK: c_long = ZSOCK_MSG_PEEK;
pub const MSG_TRUNC: c_long = ZSOCK_MSG_TRUNC;
pub const MSG_DONTWAIT: c_long = ZSOCK_MSG_DONTWAIT;
pub const MSG_WAITALL: c_long = ZSOCK_MSG_WAITALL;

pub const DNS_EAI_BADFLAGS: ::c_int = -1;
pub const DNS_EAI_NONAME: ::c_int = -2;
pub const DNS_EAI_AGAIN: ::c_int = -3;
pub const DNS_EAI_FAIL: ::c_int = -4;
pub const DNS_EAI_NODATA: ::c_int = -5;
pub const DNS_EAI_FAMILY: ::c_int = -6;
pub const DNS_EAI_SOCKTYPE: ::c_int = -7;
pub const DNS_EAI_SERVICE: ::c_int = -8;
pub const DNS_EAI_ADDRFAMILY: ::c_int = -9;
pub const DNS_EAI_MEMORY: ::c_int = -10;
pub const DNS_EAI_SYSTEM: ::c_int = -11;
pub const DNS_EAI_OVERFLOW: ::c_int = -12;
pub const DNS_EAI_INPROGRESS: ::c_int = -100;
pub const DNS_EAI_CANCELED: ::c_int = -101;
pub const DNS_EAI_NOTCANCELED: ::c_int = -102;
pub const DNS_EAI_ALLDONE: ::c_int = -103;
pub const DNS_EAI_IDN_ENCODE: ::c_int = -105;

/** POSIX wrapper for @ref DNS_EAI_BADFLAGS */
pub const EAI_BADFLAGS: ::c_int = DNS_EAI_BADFLAGS;
/** POSIX wrapper for @ref DNS_EAI_NONAME */
pub const EAI_NONAME: ::c_int = DNS_EAI_NONAME;
/** POSIX wrapper for @ref DNS_EAI_AGAIN */
pub const EAI_AGAIN: ::c_int = DNS_EAI_AGAIN;
/** POSIX wrapper for @ref DNS_EAI_FAIL */
pub const EAI_FAIL: ::c_int = DNS_EAI_FAIL;
/** POSIX wrapper for @ref DNS_EAI_NODATA */
pub const EAI_NODATA: ::c_int = DNS_EAI_NODATA;
/** POSIX wrapper for @ref DNS_EAI_MEMORY */
pub const EAI_MEMORY: ::c_int = DNS_EAI_MEMORY;
/** POSIX wrapper for @ref DNS_EAI_SYSTEM */
pub const EAI_SYSTEM: ::c_int = DNS_EAI_SYSTEM;
/** POSIX wrapper for @ref DNS_EAI_SERVICE */
pub const EAI_SERVICE: ::c_int = DNS_EAI_SERVICE;
/** POSIX wrapper for @ref DNS_EAI_SOCKTYPE */
pub const EAI_SOCKTYPE: ::c_int = DNS_EAI_SOCKTYPE;
/** POSIX wrapper for @ref DNS_EAI_FAMILY */
pub const EAI_FAMILY: ::c_int = DNS_EAI_FAMILY;

pub const PTHREAD_STACK_MIN: ::size_t = 768;

pub const RTLD_DEFAULT: *mut ::c_void = 0 as *mut ::c_void;

extern "C" {
pub fn pthread_create(
native: *mut ::pthread_t,
attr: *const ::pthread_attr_t,
f: extern "C" fn(_: *mut ::c_void) -> *mut ::c_void,
value: *mut ::c_void,
) -> ::c_int;

// pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
//
// #[link_name = "lwip_sendmsg"]
// pub fn sendmsg(s: ::c_int, msg: *const ::msghdr, flags: ::c_int) -> ::ssize_t;
// #[link_name = "lwip_recvmsg"]
// pub fn recvmsg(s: ::c_int, msg: *mut ::msghdr, flags: ::c_int) -> ::ssize_t;
//
// pub fn eventfd(initval: ::c_uint, flags: ::c_int) -> ::c_int;
}

pub use crate::unix::newlib::generic::{sigset_t, stat};
78 changes: 78 additions & 0 deletions src/zephyr/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//! Definitions found commonly among almost all Unix derivatives
//!
//! More functions and definitions can be found in the more specific modules
//! according to the platform in question.

pub type int8_t = i8;
pub type int16_t = i16;
pub type int32_t = i32;
pub type int64_t = i64;
pub type uint8_t = u8;
pub type uint16_t = u16;
pub type uint32_t = u32;
pub type uint64_t = u64;

cfg_if! {
if #[cfg(target_arch = "aarch64")] {
pub type c_char = u8;
} else {
pub type c_char = i8;
}
}
pub type c_schar = i8;
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
cfg_if! {
if #[cfg(target_pointer_width = "32")] {
pub type c_long = i32;
pub type c_ulong = u32;
} else if #[cfg(target_pointer_width = "64")] {
pub type c_long = i64;
pub type c_ulong = u64;
}
}
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;
pub type c_float = f32;
pub type c_double = f64;

pub type size_t = usize;
pub type ptrdiff_t = isize;
pub type intptr_t = isize;
pub type uintptr_t = usize;
pub type ssize_t = isize;

cfg_if! {
if #[cfg(libc_core_cvoid)] {
pub use ::ffi::c_void;
} else {
// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help
// enable more optimization opportunities around it recognizing things
// like malloc/free.
#[repr(u8)]
#[allow(missing_copy_implementations)]
#[allow(missing_debug_implementations)]
pub enum c_void {
// Two dummy variants so the #[repr] attribute can be used.
#[doc(hidden)]
__variant1,
#[doc(hidden)]
__variant2,
}
}
}

extern "C" {
pub fn strlen(cs: *const c_char) -> size_t;

pub fn memchr(cx: *const c_void, c: c_int, n: size_t) -> *mut c_void;
pub fn memcmp(cx: *const c_void, ct: *const c_void, n: size_t) -> c_int;
pub fn memcpy(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
pub fn memmove(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
pub fn memset(dest: *mut c_void, c: c_int, n: size_t) -> *mut c_void;
}