Zephyr API Documentation 4.0.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
fdtable.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Linaro Limited
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef ZEPHYR_INCLUDE_SYS_FDTABLE_H_
7#define ZEPHYR_INCLUDE_SYS_FDTABLE_H_
8
9#include <stdarg.h>
10#include <time.h>
11
12/* FIXME: For native_posix ssize_t, off_t. */
13#include <sys/types.h>
14#include <zephyr/kernel.h>
15#include <zephyr/sys/util.h>
16
17/* File mode bits */
18#define ZVFS_MODE_IFMT 0170000
19#define ZVFS_MODE_UNSPEC 0000000
20#define ZVFS_MODE_IFIFO 0010000
21#define ZVFS_MODE_IFCHR 0020000
22#define ZVFS_MODE_IMSGQ 0030000
23#define ZVFS_MODE_IFDIR 0040000
24#define ZVFS_MODE_IFSEM 0050000
25#define ZVFS_MODE_IFBLK 0060000
26#define ZVFS_MODE_IFSHM 0070000
27#define ZVFS_MODE_IFREG 0100000
28#define ZVFS_MODE_IFLNK 0120000
29#define ZVFS_MODE_IFSOCK 0140000
30
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
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/* FIXME: use k_off_t and k_ssize_t to avoid the POSIX->Zephyr->POSIX dependency cycle */
43#ifdef CONFIG_NEWLIB_LIBC
44#ifndef _OFF_T_DECLARED
45typedef __off_t off_t;
46#define _OFF_T_DECLARED
47#endif
48#ifndef _SSIZE_T_DECLARED
49typedef _ssize_t ssize_t;
50#define _SSIZE_T_DECLARED
51#endif
52#endif
53
59 union {
60 ssize_t (*read)(void *obj, void *buf, size_t sz);
61 ssize_t (*read_offs)(void *obj, void *buf, size_t sz, size_t offset);
62 };
63 union {
64 ssize_t (*write)(void *obj, const void *buf, size_t sz);
65 ssize_t (*write_offs)(void *obj, const void *buf, size_t sz, size_t offset);
66 };
67 union {
68 int (*close)(void *obj);
69 int (*close2)(void *obj, int fd);
70 };
71 int (*ioctl)(void *obj, unsigned int request, va_list args);
72};
73
85
102void zvfs_finalize_typed_fd(int fd, void *obj, const struct fd_op_vtable *vtable, uint32_t mode);
103
114static inline void zvfs_finalize_fd(int fd, void *obj, const struct fd_op_vtable *vtable)
115{
117}
118
130int zvfs_alloc_fd(void *obj, const struct fd_op_vtable *vtable);
131
140void zvfs_free_fd(int fd);
141
158void *zvfs_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err);
159
172void *zvfs_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable,
173 struct k_mutex **lock);
174
190bool zvfs_get_obj_lock_and_cond(void *obj, const struct fd_op_vtable *vtable, struct k_mutex **lock,
191 struct k_condvar **cond);
192
205static inline int zvfs_fdtable_call_ioctl(const struct fd_op_vtable *vtable, void *obj,
206 unsigned long request, ...)
207{
208 va_list args;
209 int res;
210
211 va_start(args, request);
212 res = vtable->ioctl(obj, request, args);
213 va_end(args);
214
215 return res;
216}
217
219 int fd;
220 short events;
221 short revents;
222};
223
224__syscall int zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout);
225
227 uint32_t bitset[(CONFIG_ZVFS_OPEN_MAX + 31) / 32];
228};
229
231#define ZVFS_FD_SETSIZE (sizeof(((struct zvfs_fd_set *)0)->bitset) * 8)
232
233void ZVFS_FD_CLR(int fd, struct zvfs_fd_set *fdset);
234int ZVFS_FD_ISSET(int fd, struct zvfs_fd_set *fdset);
235void ZVFS_FD_SET(int fd, struct zvfs_fd_set *fdset);
236void ZVFS_FD_ZERO(struct zvfs_fd_set *fdset);
237
238__syscall int zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
239 struct zvfs_fd_set *ZRESTRICT writefds,
240 struct zvfs_fd_set *ZRESTRICT errorfds,
241 const struct timespec *ZRESTRICT timeout, const void *ZRESTRICT sigmask);
242
251enum {
252 /* Codes below 0x100 are reserved for fcntl() codes. */
262
263 /* Codes above 0x5400 and below 0x5500 are reserved for termios, FIO, etc */
266};
267
268#ifdef __cplusplus
269}
270#endif
271
272#include <zephyr/syscalls/fdtable.h>
273
274#endif /* ZEPHYR_INCLUDE_SYS_FDTABLE_H_ */
int zvfs_alloc_fd(void *obj, const struct fd_op_vtable *vtable)
Allocate file descriptor for underlying I/O object.
void ZVFS_FD_CLR(int fd, struct zvfs_fd_set *fdset)
int zvfs_reserve_fd(void)
Reserve file descriptor.
void * zvfs_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable, struct k_mutex **lock)
Get underlying object pointer and vtable pointer from file descriptor.
static void zvfs_finalize_fd(int fd, void *obj, const struct fd_op_vtable *vtable)
Finalize creation of file descriptor.
Definition fdtable.h:114
int zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout)
void zvfs_finalize_typed_fd(int fd, void *obj, const struct fd_op_vtable *vtable, uint32_t mode)
Finalize creation of file descriptor, with type.
#define ZVFS_MODE_UNSPEC
Definition fdtable.h:19
void ZVFS_FD_ZERO(struct zvfs_fd_set *fdset)
bool zvfs_get_obj_lock_and_cond(void *obj, const struct fd_op_vtable *vtable, struct k_mutex **lock, struct k_condvar **cond)
Get the mutex and condition variable associated with the given object and vtable.
void zvfs_free_fd(int fd)
Release reserved file descriptor.
static int zvfs_fdtable_call_ioctl(const struct fd_op_vtable *vtable, void *obj, unsigned long request,...)
Call ioctl vmethod on an object using varargs.
Definition fdtable.h:205
int ZVFS_FD_ISSET(int fd, struct zvfs_fd_set *fdset)
void * zvfs_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err)
Get underlying object pointer from file descriptor.
int zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds, struct zvfs_fd_set *ZRESTRICT writefds, struct zvfs_fd_set *ZRESTRICT errorfds, const struct timespec *ZRESTRICT timeout, const void *ZRESTRICT sigmask)
@ ZFD_IOCTL_SET_LOCK
Definition fdtable.h:258
@ ZFD_IOCTL_FIONREAD
Definition fdtable.h:264
@ ZFD_IOCTL_POLL_PREPARE
Definition fdtable.h:255
@ ZFD_IOCTL_FSYNC
Definition fdtable.h:253
@ ZFD_IOCTL_LSEEK
Definition fdtable.h:254
@ ZFD_IOCTL_FIONBIO
Definition fdtable.h:265
@ ZFD_IOCTL_POLL_OFFLOAD
Definition fdtable.h:257
@ ZFD_IOCTL_POLL_UPDATE
Definition fdtable.h:256
@ ZFD_IOCTL_TRUNCATE
Definition fdtable.h:260
@ ZFD_IOCTL_STAT
Definition fdtable.h:259
@ ZFD_IOCTL_MMAP
Definition fdtable.h:261
void ZVFS_FD_SET(int fd, struct zvfs_fd_set *fdset)
#define ZRESTRICT
Definition common.h:36
Public kernel APIs.
__SIZE_TYPE__ ssize_t
Definition types.h:28
__INTPTR_TYPE__ off_t
Definition types.h:36
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
File descriptor virtual method table.
Definition fdtable.h:58
int(* close2)(void *obj, int fd)
Definition fdtable.h:69
ssize_t(* write_offs)(void *obj, const void *buf, size_t sz, size_t offset)
Definition fdtable.h:65
ssize_t(* read_offs)(void *obj, void *buf, size_t sz, size_t offset)
Definition fdtable.h:61
int(* close)(void *obj)
Definition fdtable.h:68
ssize_t(* read)(void *obj, void *buf, size_t sz)
Definition fdtable.h:60
ssize_t(* write)(void *obj, const void *buf, size_t sz)
Definition fdtable.h:64
int(* ioctl)(void *obj, unsigned int request, va_list args)
Definition fdtable.h:71
Definition kernel.h:3106
Mutex Structure.
Definition kernel.h:2994
Definition _timespec.h:22
Definition fdtable.h:226
uint32_t bitset[(CONFIG_ZVFS_OPEN_MAX+31)/32]
Definition fdtable.h:227
Definition fdtable.h:218
short events
Definition fdtable.h:220
short revents
Definition fdtable.h:221
int fd
Definition fdtable.h:219
Misc utilities.