Skip to content

Commit 650c336

Browse files
committed
posix: implement parts of posix device io not in iso c
Implement the components of the POSIX_DEVICE_IO Option Group that are not already implemented by any C89-conformant C library. Signed-off-by: Chris Friedt <[email protected]>
1 parent c60f322 commit 650c336

21 files changed

+707
-388
lines changed

include/zephyr/net/socket.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,10 @@ static inline int zsock_ioctl_wrapper(int sock, unsigned long request, ...)
625625
* it may conflict with generic POSIX ``poll()`` function).
626626
* @endrst
627627
*/
628-
__syscall int zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout);
628+
static inline int zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout)
629+
{
630+
return zvfs_poll((struct zvfs_pollfd *)fds, nfds, timeout);
631+
}
629632

630633
/**
631634
* @brief Get various socket options

include/zephyr/net/socket_select.h

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
* @{
2020
*/
2121

22+
#include <time.h>
23+
2224
#include <zephyr/toolchain.h>
2325
#include <zephyr/net/socket_types.h>
26+
#include <zephyr/sys/fdtable.h>
2427

2528
#ifdef __cplusplus
2629
extern "C" {
@@ -47,10 +50,16 @@ typedef struct zsock_fd_set {
4750
* it may conflict with generic POSIX ``select()`` function).
4851
* @endrst
4952
*/
50-
__syscall int zsock_select(int nfds, zsock_fd_set *readfds,
51-
zsock_fd_set *writefds,
52-
zsock_fd_set *exceptfds,
53-
struct zsock_timeval *timeout);
53+
static inline int zsock_select(int nfds, zsock_fd_set *readfds, zsock_fd_set *writefds,
54+
zsock_fd_set *exceptfds, struct zsock_timeval *timeout)
55+
{
56+
struct timespec to = {
57+
.tv_sec = (timeout == NULL) ? 0 : timeout->tv_sec,
58+
.tv_nsec = (long)((timeout == NULL) ? 0 : timeout->tv_usec * NSEC_PER_USEC)};
59+
60+
return zvfs_select(nfds, (struct zvfs_fd_set *)readfds, (struct zvfs_fd_set *)writefds,
61+
(struct zvfs_fd_set *)exceptfds, (timeout == NULL) ? NULL : &to, NULL);
62+
}
5463

5564
/** Number of file descriptors which can be added to zsock_fd_set */
5665
#define ZSOCK_FD_SETSIZE (sizeof(((zsock_fd_set *)0)->bitset) * 8)
@@ -67,7 +76,10 @@ __syscall int zsock_select(int nfds, zsock_fd_set *readfds,
6776
* if :kconfig:option:`CONFIG_POSIX_API` is defined.
6877
* @endrst
6978
*/
70-
void ZSOCK_FD_ZERO(zsock_fd_set *set);
79+
static inline void ZSOCK_FD_ZERO(zsock_fd_set *set)
80+
{
81+
ZVFS_FD_ZERO((struct zvfs_fd_set *)set);
82+
}
7183

7284
/**
7385
* @brief Check whether socket is a member of fd_set
@@ -81,7 +93,10 @@ void ZSOCK_FD_ZERO(zsock_fd_set *set);
8193
* if :kconfig:option:`CONFIG_POSIX_API` is defined.
8294
* @endrst
8395
*/
84-
int ZSOCK_FD_ISSET(int fd, zsock_fd_set *set);
96+
static inline int ZSOCK_FD_ISSET(int fd, zsock_fd_set *set)
97+
{
98+
return ZVFS_FD_ISSET(fd, (struct zvfs_fd_set *)set);
99+
}
85100

86101
/**
87102
* @brief Remove socket from fd_set
@@ -95,7 +110,10 @@ int ZSOCK_FD_ISSET(int fd, zsock_fd_set *set);
95110
* if :kconfig:option:`CONFIG_POSIX_API` is defined.
96111
* @endrst
97112
*/
98-
void ZSOCK_FD_CLR(int fd, zsock_fd_set *set);
113+
static inline void ZSOCK_FD_CLR(int fd, zsock_fd_set *set)
114+
{
115+
ZVFS_FD_CLR(fd, (struct zvfs_fd_set *)set);
116+
}
99117

100118
/**
101119
* @brief Add socket to fd_set
@@ -109,7 +127,10 @@ void ZSOCK_FD_CLR(int fd, zsock_fd_set *set);
109127
* if :kconfig:option:`CONFIG_POSIX_API` is defined.
110128
* @endrst
111129
*/
112-
void ZSOCK_FD_SET(int fd, zsock_fd_set *set);
130+
static inline void ZSOCK_FD_SET(int fd, zsock_fd_set *set)
131+
{
132+
ZVFS_FD_SET(fd, (struct zvfs_fd_set *)set);
133+
}
113134

114135
/** @cond INTERNAL_HIDDEN */
115136

@@ -153,8 +174,6 @@ static inline void FD_SET(int fd, zsock_fd_set *set)
153174
}
154175
#endif
155176

156-
#include <zephyr/syscalls/socket_select.h>
157-
158177
/**
159178
* @}
160179
*/

include/zephyr/posix/fcntl.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
#define ZEPHYR_POSIX_FCNTL_H_
99

1010
#ifdef CONFIG_PICOLIBC
11-
#define O_CREAT 0x0040
11+
#define O_CREAT 0x0040
12+
#define O_TRUNC 0x0200
13+
#define O_APPEND 0x0400
1214
#else
13-
#define O_CREAT 0x0200
15+
#define O_APPEND 0x0008
16+
#define O_CREAT 0x0200
17+
#define O_TRUNC 0x0400
1418
#endif
1519

1620
#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
@@ -19,7 +23,6 @@
1923
#define O_WRONLY 01
2024
#define O_RDWR 02
2125

22-
#define O_APPEND 0x0400
2326
#define O_EXCL 0x0800
2427
#define O_NONBLOCK 0x4000
2528

include/zephyr/posix/sys/select.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66
#ifndef ZEPHYR_INCLUDE_POSIX_SYS_SELECT_H_
77
#define ZEPHYR_INCLUDE_POSIX_SYS_SELECT_H_
88

9-
#include <zephyr/net/socket_types.h>
10-
#include <zephyr/net/socket_select.h>
9+
#include <signal.h>
10+
11+
#include <zephyr/posix/sys/time.h>
12+
#include <zephyr/sys/fdtable.h>
13+
#include <zephyr/toolchain.h>
1114

1215
#ifdef __cplusplus
1316
extern "C" {
1417
#endif
1518

16-
#define fd_set zsock_fd_set
17-
#define FD_SETSIZE ZSOCK_FD_SETSIZE
18-
#define FD_ZERO ZSOCK_FD_ZERO
19-
#define FD_SET ZSOCK_FD_SET
20-
#define FD_CLR ZSOCK_FD_CLR
21-
#define FD_ISSET ZSOCK_FD_ISSET
22-
23-
struct timeval;
19+
typedef struct zvfs_fd_set fd_set;
2420

25-
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
21+
int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
22+
const struct timespec *timeout, const sigset_t *sigmask);
23+
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
24+
void FD_CLR(int fd, fd_set *fdset);
25+
int FD_ISSET(int fd, fd_set *fdset);
26+
void FD_SET(int fd, fd_set *fdset);
27+
void FD_ZERO(fd_set *fdset);
2628

2729
#ifdef __cplusplus
2830
}

include/zephyr/sys/fdtable.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <stdarg.h>
1010
#include <sys/types.h>
11+
#include <time.h>
12+
1113
/* FIXME: For native_posix ssize_t, off_t. */
1214
#include <zephyr/fs/fs.h>
1315
#include <zephyr/sys/mutex.h>
@@ -26,6 +28,13 @@
2628
#define ZVFS_MODE_IFLNK 0120000
2729
#define ZVFS_MODE_IFSOCK 0140000
2830

31+
#define ZVFS_POLLIN BIT(0)
32+
#define ZVFS_POLLPRI BIT(1)
33+
#define ZVFS_POLLOUT BIT(2)
34+
#define ZVFS_POLLERR BIT(3)
35+
#define ZVFS_POLLHUP BIT(4)
36+
#define ZVFS_POLLNVAL BIT(5)
37+
2938
#ifdef __cplusplus
3039
extern "C" {
3140
#endif
@@ -191,6 +200,28 @@ static inline int z_fdtable_call_ioctl(const struct fd_op_vtable *vtable, void *
191200
return res;
192201
}
193202

203+
struct zvfs_pollfd {
204+
int fd;
205+
short events;
206+
short revents;
207+
};
208+
209+
__syscall int zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout);
210+
211+
struct zvfs_fd_set {
212+
uint32_t bitset[(CONFIG_ZVFS_OPEN_MAX + 31) / 32];
213+
};
214+
215+
void ZVFS_FD_CLR(int fd, struct zvfs_fd_set *fdset);
216+
int ZVFS_FD_ISSET(int fd, struct zvfs_fd_set *fdset);
217+
void ZVFS_FD_SET(int fd, struct zvfs_fd_set *fdset);
218+
void ZVFS_FD_ZERO(struct zvfs_fd_set *fdset);
219+
220+
__syscall int zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
221+
struct zvfs_fd_set *ZRESTRICT writefds,
222+
struct zvfs_fd_set *ZRESTRICT errorfds,
223+
const struct timespec *ZRESTRICT timeout, const void *ZRESTRICT sigmask);
224+
194225
/**
195226
* Request codes for fd_op_vtable.ioctl().
196227
*
@@ -220,4 +251,6 @@ enum {
220251
}
221252
#endif
222253

254+
#include <zephyr/syscalls/fdtable.h>
255+
223256
#endif /* ZEPHYR_INCLUDE_SYS_FDTABLE_H_ */

lib/os/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ zephyr_sources(
1212
)
1313

1414
zephyr_sources_ifdef(CONFIG_FDTABLE fdtable.c)
15+
zephyr_syscall_header_ifdef(CONFIG_FDTABLE
16+
${ZEPHYR_BASE}/include/zephyr/sys/fdtable.h
17+
)
1518

1619
zephyr_sources_ifdef(CONFIG_CBPRINTF_COMPLETE cbprintf_complete.c)
1720
zephyr_sources_ifdef(CONFIG_CBPRINTF_NANO cbprintf_nano.c)

0 commit comments

Comments
 (0)