Skip to content

Commit

Permalink
Initial support for NuttX
Browse files Browse the repository at this point in the history
Define the essential types that for NuttX OS.

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
  • Loading branch information
no1wudi committed Sep 18, 2024
1 parent a6386af commit 12f1663
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 3 deletions.
4 changes: 3 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const ALLOWED_CFGS: &'static [&'static str] = &[
const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[
(
"target_os",
&["switch", "aix", "ohos", "hurd", "rtems", "visionos"],
&[
"switch", "aix", "ohos", "hurd", "rtems", "visionos", "nuttx",
],
),
("target_env", &["illumos", "wasi", "aix", "ohos"]),
(
Expand Down
10 changes: 8 additions & 2 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,11 @@ extern "C" {
}

cfg_if! {
if #[cfg(any(target_os = "l4re", target_os = "espidf"))] {
// required libraries for L4Re and the ESP-IDF framework are linked externally, ATM
if #[cfg(any(target_os = "l4re", target_os = "espidf", target_os = "nuttx"))] {
// required libraries are linked externally for these platforms:
// * L4Re
// * ESP-IDF
// * NuttX
} else if #[cfg(feature = "std")] {
// cargo build, don't pull in anything extra as the std dep
// already pulls in all libs.
Expand Down Expand Up @@ -1610,6 +1613,9 @@ cfg_if! {
} else if #[cfg(target_os = "hurd")] {
mod hurd;
pub use self::hurd::*;
} else if #[cfg(target_os = "nuttx")] {
mod nuttx;
pub use self::nuttx::*;
} else {
// Unknown target_os
}
Expand Down
210 changes: 210 additions & 0 deletions src/unix/nuttx/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
use c_void;
use timespec;

pub type blkcnt_t = u64;
pub type blksize_t = i16;
pub type c_char = i8;
pub type c_long = isize;
pub type c_ulong = usize;
pub type cc_t = u8;
pub type clock_t = i64;
pub type dev_t = i32;
pub type fsblkcnt_t = u64;
pub type locale_t = *mut i8;
pub type mode_t = u32;
pub type nfds_t = u32;
pub type off_t = i64;
pub type pthread_key_t = i32;
pub type pthread_mutexattr_t = u8;
pub type pthread_rwlockattr_t = i32;
pub type pthread_t = i32;
pub type rlim_t = i64;
pub type sa_family_t = u16;
pub type socklen_t = u32;
pub type speed_t = usize;
pub type suseconds_t = i32;
pub type tcflag_t = u32;
pub type time_t = i64;
pub type wchar_t = i32;

s! {
pub struct stat {
pub st_dev: dev_t,
pub st_ino: u64,
pub st_mode: mode_t,
pub st_nlink: u64,
pub st_uid: u32,
pub st_gid: u32,
pub st_rdev: dev_t,
pub st_size: off_t,
pub st_atim: timespec,
pub st_mtim: timespec,
pub st_ctim: timespec,
pub st_blksize: blksize_t,
pub st_blocks: i64,
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
}

pub struct sockaddr {
pub sa_family: sa_family_t,
pub sa_data: [u8; 14],
}

pub struct passwd {
pub pw_name: *const c_char,
pub pw_uid: u32,
pub pw_gid: u32,
pub pw_gecos: *const c_char,
pub pw_dir: *const c_char,
pub pw_shell: *const c_char,
__reserved: [usize; __DEFAULT_RESERVED_SIZE__]
}

pub struct sem_t { __val: [usize; __SEM_SIZE__] }

pub struct pthread_attr_t { __val: [usize; __PTHREAD_ATTR_SIZE__] }

pub struct pthread_mutex_t { __val: [usize; __PTHREAD_MUTEX_SIZE__] }

pub struct pthread_cond_t { __val: [usize; __PTHREAD_COND_SIZE__] }

pub struct pthread_condattr_t { __val: [usize; __PTHREAD_CONDATTR_SIZE__] }

pub struct Dl_info {
pub dli_fname: *const c_char,
pub dli_fbase: *mut c_void,
pub dli_sname: *const c_char,
pub dli_saddr: *mut c_void,
}

pub struct lconv {
pub decimal_point: *const c_char,
pub thousands_sep: *const c_char,
pub grouping: *const c_char,
pub int_curr_symbol: *const c_char,
pub currency_symbol: *const c_char,
pub mon_decimal_point: *const c_char,
pub mon_thousands_sep: *const c_char,
pub mon_grouping: *const c_char,
pub positive_sign: *const c_char,
pub negative_sign: *const c_char,
pub int_frac_digits: i8,
pub frac_digits: i8,
pub p_cs_precedes: i8,
pub p_sep_by_space: i8,
pub n_cs_precedes: i8,
pub n_sep_by_space: i8,
pub p_sign_posn: i8,
pub n_sign_posn: i8,
pub int_n_cs_precedes: i8,
pub int_n_sep_by_space: i8,
pub int_n_sign_posn: i8,
pub int_p_cs_precedes: i8,
pub int_p_sep_by_space: i8,
pub int_p_sign_posn: i8,
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
}

pub struct tm {
pub tm_sec: i32,
pub tm_min: i32,
pub tm_hour: i32,
pub tm_mday: i32,
pub tm_mon: i32,
pub tm_year: i32,
pub tm_wday: i32,
pub tm_yday: i32,
pub tm_isdst: i32,
pub tm_gmtoff: isize,
pub tm_zone: *const i8,
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
}

pub struct addrinfo {
pub ai_flags: i32,
pub ai_family: i32,
pub ai_socktype: i32,
pub ai_protocol: i32,
pub ai_addrlen: socklen_t,
pub ai_addr: *mut sockaddr,
pub ai_canonname: *mut c_char,
pub ai_next: *mut addrinfo,
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
}

pub struct pthread_rwlock_t {
__val: [usize; __PTHREAD_RWLOCK_SIZE__],
}

pub struct statvfs {
pub f_bsize: usize,
pub f_frsize: usize,
pub f_blocks: fsblkcnt_t,
pub f_bfree: fsblkcnt_t,
pub f_bavail: fsblkcnt_t,
pub f_files: fsblkcnt_t,
pub f_ffree: fsblkcnt_t,
pub f_favail: fsblkcnt_t,
pub f_fsid: usize,
pub f_flag: usize,
pub f_namemax: usize,
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
}

pub struct dirent {
pub d_type: u8,
pub d_name: [u8; __NAME_MAX__ + 1],
}

pub struct fd_set {
__val: [u32; __FDSET_SIZE__],
}

pub struct sigset_t {
__val: [u32; __SIGSET_SIZE__],
}

pub struct sigaction {
pub sa_handler: usize,
pub sa_mask: sigset_t,
pub sa_flags: i32,
pub sa_user: usize,
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
}

pub struct termios {
pub c_iflag: tcflag_t,
pub c_oflag: tcflag_t,
pub c_cflag: tcflag_t,
pub c_lflag: tcflag_t,
pub c_cc: [cc_t; 12],
pub c_speed: speed_t,
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
}
}

// Reserved two pointer size for reserved area for some structures.
// This ensures that the size of these structures is large enough
// if more fields are added in the NuttX side.
//
// These structures are that defined by POSIX but only necessary fields are included,
// for example, struct passwd, https://pubs.opengroup.org/onlinepubs/009695399/basedefs/pwd.h.html,
// POSIX only defines following fields in struct passwd:
// char *pw_name User's login name.
// uid_t pw_uid Numerical user ID.
// gid_t pw_gid Numerical group ID.
// char *pw_dir Initial working directory.
// char *pw_shell Program to use as shell.
// Other fields can be different depending on the implementation.

const __DEFAULT_RESERVED_SIZE__: usize = 2;

const __PTHREAD_ATTR_SIZE__: usize = 5;
const __PTHREAD_MUTEX_SIZE__: usize = 9;
const __PTHREAD_COND_SIZE__: usize = 7;
const __PTHREAD_CONDATTR_SIZE__: usize = 5;
const __PTHREAD_RWLOCK_SIZE__: usize = 17;
const __SEM_SIZE__: usize = 6;
const __NAME_MAX__: usize = 64;
const __FDSET_SIZE__: usize = 10;
const __SIGSET_SIZE__: usize = 8;

0 comments on commit 12f1663

Please sign in to comment.