Skip to content

Commit ada89af

Browse files
committed
Initial support for NuttX
Define the essential types that for NuttX OS. Signed-off-by: Huang Qi <[email protected]>
1 parent 1b11393 commit ada89af

File tree

3 files changed

+225
-2
lines changed

3 files changed

+225
-2
lines changed

Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,30 @@ targets = [
8585
"riscv32gc-unknown-linux-gnu",
8686
"riscv32i-unknown-none-elf",
8787
"riscv32imac-unknown-none-elf",
88+
"riscv32imac-unknown-nuttx-elf",
8889
"riscv32imc-unknown-none-elf",
90+
"riscv32imc-unknown-nuttx-elf",
8991
"riscv64gc-unknown-freebsd",
9092
"riscv64gc-unknown-hermit",
9193
"riscv64gc-unknown-linux-gnu",
9294
"riscv64gc-unknown-linux-musl",
9395
"riscv64gc-unknown-none-elf",
9496
"riscv64imac-unknown-none-elf",
97+
"riscv64imac-unknown-nuttx-elf",
9598
"s390x-unknown-linux-gnu",
9699
"s390x-unknown-linux-musl",
97100
"sparc-unknown-linux-gnu",
98101
"sparc64-unknown-linux-gnu",
99102
"sparc64-unknown-netbsd",
100103
"sparcv9-sun-solaris",
101104
"thumbv6m-none-eabi",
105+
"thumbv6m-nuttx-eabi",
102106
"thumbv7em-none-eabi",
107+
"thumbv7em-nuttx-eabi",
103108
"thumbv7em-none-eabihf",
109+
"thumbv7em-nuttx-eabihf",
104110
"thumbv7m-none-eabi",
111+
"thumbv7m-nuttx-eabi",
105112
"thumbv7neon-linux-androideabi",
106113
"thumbv7neon-unknown-linux-gnueabihf",
107114
"wasm32-unknown-emscripten",

src/unix/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,11 @@ extern "C" {
326326
}
327327

328328
cfg_if! {
329-
if #[cfg(any(target_os = "l4re", target_os = "espidf"))] {
330-
// required libraries for L4Re and the ESP-IDF framework are linked externally, ATM
329+
if #[cfg(any(target_os = "l4re", target_os = "espidf", target_os = "nuttx"))] {
330+
// required libraries are linked externally for these platforms:
331+
// * L4Re
332+
// * ESP-IDF
333+
// * NuttX
331334
} else if #[cfg(feature = "std")] {
332335
// cargo build, don't pull in anything extra as the std dep
333336
// already pulls in all libs.
@@ -1610,6 +1613,9 @@ cfg_if! {
16101613
} else if #[cfg(target_os = "hurd")] {
16111614
mod hurd;
16121615
pub use self::hurd::*;
1616+
} else if #[cfg(target_os = "nuttx")] {
1617+
mod nuttx;
1618+
pub use self::nuttx::*;
16131619
} else {
16141620
// Unknown target_os
16151621
}

src/unix/nuttx/mod.rs

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
use c_void;
2+
use timespec;
3+
4+
pub type blkcnt_t = u64;
5+
pub type blksize_t = i16;
6+
pub type c_char = i8;
7+
pub type c_long = isize;
8+
pub type c_ulong = usize;
9+
pub type cc_t = u8;
10+
pub type clock_t = i64;
11+
pub type dev_t = i32;
12+
pub type fsblkcnt_t = u64;
13+
pub type locale_t = *mut i8;
14+
pub type mode_t = u32;
15+
pub type nfds_t = u32;
16+
pub type off_t = i64;
17+
pub type pthread_key_t = i32;
18+
pub type pthread_mutexattr_t = u8;
19+
pub type pthread_rwlockattr_t = i32;
20+
pub type pthread_t = i32;
21+
pub type rlim_t = i64;
22+
pub type sa_family_t = u16;
23+
pub type socklen_t = u32;
24+
pub type speed_t = usize;
25+
pub type suseconds_t = i32;
26+
pub type tcflag_t = u32;
27+
pub type time_t = i64;
28+
pub type wchar_t = i32;
29+
30+
s! {
31+
pub struct stat {
32+
pub st_dev: dev_t,
33+
pub st_ino: u64,
34+
pub st_mode: mode_t,
35+
pub st_nlink: u64,
36+
pub st_uid: u32,
37+
pub st_gid: u32,
38+
pub st_rdev: dev_t,
39+
pub st_size: off_t,
40+
pub st_atim: timespec,
41+
pub st_mtim: timespec,
42+
pub st_ctim: timespec,
43+
pub st_blksize: blksize_t,
44+
pub st_blocks: i64,
45+
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
46+
}
47+
48+
pub struct sockaddr {
49+
pub sa_family: sa_family_t,
50+
pub sa_data: [u8; 14],
51+
}
52+
53+
pub struct passwd {
54+
pub pw_name: *const c_char,
55+
pub pw_uid: u32,
56+
pub pw_gid: u32,
57+
pub pw_gecos: *const c_char,
58+
pub pw_dir: *const c_char,
59+
pub pw_shell: *const c_char,
60+
__reserved: [usize; __DEFAULT_RESERVED_SIZE__]
61+
}
62+
63+
pub struct sem_t { __val: [usize; __SEM_SIZE__] }
64+
65+
pub struct pthread_attr_t { __val: [usize; __PTHREAD_ATTR_SIZE__] }
66+
67+
pub struct pthread_mutex_t { __val: [usize; __PTHREAD_MUTEX_SIZE__] }
68+
69+
pub struct pthread_cond_t { __val: [usize; __PTHREAD_COND_SIZE__] }
70+
71+
pub struct pthread_condattr_t { __val: [usize; __PTHREAD_CONDATTR_SIZE__] }
72+
73+
pub struct Dl_info {
74+
pub dli_fname: *const c_char,
75+
pub dli_fbase: *mut c_void,
76+
pub dli_sname: *const c_char,
77+
pub dli_saddr: *mut c_void,
78+
}
79+
80+
pub struct lconv {
81+
pub decimal_point: *const c_char,
82+
pub thousands_sep: *const c_char,
83+
pub grouping: *const c_char,
84+
pub int_curr_symbol: *const c_char,
85+
pub currency_symbol: *const c_char,
86+
pub mon_decimal_point: *const c_char,
87+
pub mon_thousands_sep: *const c_char,
88+
pub mon_grouping: *const c_char,
89+
pub positive_sign: *const c_char,
90+
pub negative_sign: *const c_char,
91+
pub int_frac_digits: i8,
92+
pub frac_digits: i8,
93+
pub p_cs_precedes: i8,
94+
pub p_sep_by_space: i8,
95+
pub n_cs_precedes: i8,
96+
pub n_sep_by_space: i8,
97+
pub p_sign_posn: i8,
98+
pub n_sign_posn: i8,
99+
pub int_n_cs_precedes: i8,
100+
pub int_n_sep_by_space: i8,
101+
pub int_n_sign_posn: i8,
102+
pub int_p_cs_precedes: i8,
103+
pub int_p_sep_by_space: i8,
104+
pub int_p_sign_posn: i8,
105+
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
106+
}
107+
108+
pub struct tm {
109+
pub tm_sec: i32,
110+
pub tm_min: i32,
111+
pub tm_hour: i32,
112+
pub tm_mday: i32,
113+
pub tm_mon: i32,
114+
pub tm_year: i32,
115+
pub tm_wday: i32,
116+
pub tm_yday: i32,
117+
pub tm_isdst: i32,
118+
pub tm_gmtoff: isize,
119+
pub tm_zone: *const i8,
120+
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
121+
}
122+
123+
pub struct addrinfo {
124+
pub ai_flags: i32,
125+
pub ai_family: i32,
126+
pub ai_socktype: i32,
127+
pub ai_protocol: i32,
128+
pub ai_addrlen: socklen_t,
129+
pub ai_addr: *mut sockaddr,
130+
pub ai_canonname: *mut c_char,
131+
pub ai_next: *mut addrinfo,
132+
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
133+
}
134+
135+
pub struct pthread_rwlock_t {
136+
__val: [usize; __PTHREAD_RWLOCK_SIZE__],
137+
}
138+
139+
pub struct statvfs {
140+
pub f_bsize: usize,
141+
pub f_frsize: usize,
142+
pub f_blocks: fsblkcnt_t,
143+
pub f_bfree: fsblkcnt_t,
144+
pub f_bavail: fsblkcnt_t,
145+
pub f_files: fsblkcnt_t,
146+
pub f_ffree: fsblkcnt_t,
147+
pub f_favail: fsblkcnt_t,
148+
pub f_fsid: usize,
149+
pub f_flag: usize,
150+
pub f_namemax: usize,
151+
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
152+
}
153+
154+
pub struct dirent {
155+
pub d_type: u8,
156+
pub d_name: [u8; __NAME_MAX__ + 1],
157+
}
158+
159+
pub struct fd_set {
160+
__val: [u32; __FDSET_SIZE__],
161+
}
162+
163+
pub struct sigset_t {
164+
__val: [u32; __SIGSET_SIZE__],
165+
}
166+
167+
pub struct sigaction {
168+
pub sa_handler: usize,
169+
pub sa_mask: sigset_t,
170+
pub sa_flags: i32,
171+
pub sa_user: usize,
172+
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
173+
}
174+
175+
pub struct termios {
176+
pub c_iflag: tcflag_t,
177+
pub c_oflag: tcflag_t,
178+
pub c_cflag: tcflag_t,
179+
pub c_lflag: tcflag_t,
180+
pub c_cc: [cc_t; 12],
181+
pub c_speed: speed_t,
182+
__reserved: [usize; __DEFAULT_RESERVED_SIZE__],
183+
}
184+
}
185+
186+
// Reserved two pointer size for reserved area for some structures.
187+
// This ensures that the size of these structures is large enough
188+
// if more fields are added in the NuttX side.
189+
//
190+
// These structures are that defined by POSIX but only necessary fields are included,
191+
// for example, struct passwd, https://pubs.opengroup.org/onlinepubs/009695399/basedefs/pwd.h.html,
192+
// POSIX only defines following fields in struct passwd:
193+
// char *pw_name User's login name.
194+
// uid_t pw_uid Numerical user ID.
195+
// gid_t pw_gid Numerical group ID.
196+
// char *pw_dir Initial working directory.
197+
// char *pw_shell Program to use as shell.
198+
// Other fields can be different depending on the implementation.
199+
200+
const __DEFAULT_RESERVED_SIZE__: usize = 2;
201+
202+
const __PTHREAD_ATTR_SIZE__: usize = 5;
203+
const __PTHREAD_MUTEX_SIZE__: usize = 9;
204+
const __PTHREAD_COND_SIZE__: usize = 7;
205+
const __PTHREAD_CONDATTR_SIZE__: usize = 5;
206+
const __PTHREAD_RWLOCK_SIZE__: usize = 17;
207+
const __SEM_SIZE__: usize = 6;
208+
const __NAME_MAX__: usize = 64;
209+
const __FDSET_SIZE__: usize = 10;
210+
const __SIGSET_SIZE__: usize = 8;

0 commit comments

Comments
 (0)