From 8be9f5de032bdbb3f79c5c987b19f85c71ec2f0a Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Wed, 26 Jun 2019 10:33:43 -0400 Subject: [PATCH] cleanup: include/: move misc/fdtable.h to sys/fdtable.h move misc/fdtable.h to sys/fdtable.h and create a shim for backward-compatibility. No functional changes to the headers. A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES. Related to #16539 Signed-off-by: Anas Nashif --- include/misc/fdtable.h | 149 +-------------------- include/sys/fdtable.h | 150 ++++++++++++++++++++++ lib/os/fdtable.c | 2 +- lib/posix/fs.c | 2 +- subsys/net/lib/sockets/sockets.c | 2 +- subsys/net/lib/sockets/sockets_can.c | 2 +- subsys/net/lib/sockets/sockets_internal.h | 2 +- subsys/net/lib/sockets/sockets_packet.c | 2 +- subsys/net/lib/sockets/sockets_tls.c | 2 +- 9 files changed, 164 insertions(+), 149 deletions(-) create mode 100644 include/sys/fdtable.h diff --git a/include/misc/fdtable.h b/include/misc/fdtable.h index 4608df90977..da750f029dd 100644 --- a/include/misc/fdtable.h +++ b/include/misc/fdtable.h @@ -1,150 +1,15 @@ /* - * Copyright (c) 2018 Linaro Limited + * Copyright (c) 2019 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ -#ifndef ZEPHYR_INCLUDE_POSIX_POSIX__FDTABLE_H_ -#define ZEPHYR_INCLUDE_POSIX_POSIX__FDTABLE_H_ +#ifndef ZEPHYR_INCLUDE_MISC_FDTABLE_H_ +#define ZEPHYR_INCLUDE_MISC_FDTABLE_H_ -#include -#include -/* FIXME: For native_posix ssize_t, off_t. */ -#include - -#ifdef __cplusplus -extern "C" { +#ifndef CONFIG_COMPAT_INCLUDES +#warning "This header file has moved, include instead." #endif -/** - * File descriptor virtual method table. - * All operations beyond read/write go thru ioctl method. - */ -struct fd_op_vtable { - ssize_t (*read)(void *obj, void *buf, size_t sz); - ssize_t (*write)(void *obj, const void *buf, size_t sz); - int (*ioctl)(void *obj, unsigned int request, va_list args); -}; +#include -/** - * @brief Reserve file descriptor. - * - * This function allows to reserve a space for file descriptor entry in - * the underlying table, and thus allows caller to fail fast if no free - * descriptor is available. If this function succeeds, z_finalize_fd() - * or z_free_fd() must be called mandatorily. - * - * @return Allocated file descriptor, or -1 in case of error (errno is set) - */ -int z_reserve_fd(void); - -/** - * @brief Finalize creation of file descriptor. - * - * This function should be called exactly once after z_reserve_fd(), and - * should not be called in any other case. - * - * @param fd File descriptor previously returned by z_reserve_fd() - * @param obj pointer to I/O object structure - * @param vtable pointer to I/O operation implementations for the object - */ -void z_finalize_fd(int fd, void *obj, const struct fd_op_vtable *vtable); - -/** - * @brief Allocate file descriptor for underlying I/O object. - * - * This function combines operations of z_reserve_fd() and z_finalize_fd() - * in one step, and provided for convenience. - * - * @param obj pointer to I/O object structure - * @param vtable pointer to I/O operation implementations for the object - * - * @return Allocated file descriptor, or -1 in case of error (errno is set) - */ -int z_alloc_fd(void *obj, const struct fd_op_vtable *vtable); - -/** - * @brief Release reserved file descriptor. - * - * This function may be called once after z_reserve_fd(), and should - * not be called in any other case. - * - * @param fd File descriptor previously returned by z_reserve_fd() - */ -void z_free_fd(int fd); - -/** - * @brief Get underlying object pointer from file descriptor. - * - * This function is useful for functions other than read/write/ioctl - * to look up underlying I/O object by fd, optionally checking its - * type (using vtable reference). If fd refers to invalid entry, - * NULL will be returned with errno set to EBADF. If fd is valid, - * but vtable param is not NULL and doesn't match object's vtable, - * NULL is returned and errno set to err param. - * - * @param fd File descriptor previously returned by z_reserve_fd() - * @param vtable Expected object vtable or NULL - * @param err errno value to set if object vtable doesn't match - * - * @return Object pointer or NULL, with errno set - */ -void *z_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err); - -/** - * @brief Get underlying object pointer and vtable pointer from file descriptor. - * - * @param fd File descriptor previously returned by z_reserve_fd() - * @param vtable A pointer to a pointer variable to store the vtable - * - * @return Object pointer or NULL, with errno set - */ -void *z_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable); - -/** - * @brief Call ioctl vmethod on an object using varargs. - * - * We need this helper function because ioctl vmethod is declared to - * take va_list and the only portable way to construct va_list is from - * function's ... parameters. - * - * @param vtable vtable containing ioctl function pointer - * @param obj Object to call ioctl on - * @param request ioctl request number - * @param ... Variadic arguments to ioctl - */ -static inline int z_fdtable_call_ioctl(const struct fd_op_vtable *vtable, void *obj, - unsigned long request, ...) -{ - va_list args; - int res; - - va_start(args, request); - res = vtable->ioctl(obj, request, args); - va_end(args); - - return res; -} - -/** - * Request codes for fd_op_vtable.ioctl(). - * - * Note that these codes are internal Zephyr numbers, for internal - * Zephyr operations (and subject to change without notice, not part - * of "stable ABI"). These are however expected to co-exist with - * "well-known" POSIX/Linux ioctl numbers, and not clash with them. - */ -enum { - /* Codes below 0x100 are reserved for fcntl() codes. */ - ZFD_IOCTL_CLOSE = 0x100, - ZFD_IOCTL_FSYNC, - ZFD_IOCTL_LSEEK, - ZFD_IOCTL_POLL_PREPARE, - ZFD_IOCTL_POLL_UPDATE, - ZFD_IOCTL_GETSOCKNAME, -}; - -#ifdef __cplusplus -} -#endif - -#endif /* ZEPHYR_INCLUDE_POSIX_POSIX__FDTABLE_H_ */ +#endif /* ZEPHYR_INCLUDE_MISC_FDTABLE_H_ */ diff --git a/include/sys/fdtable.h b/include/sys/fdtable.h new file mode 100644 index 00000000000..370f11b0f29 --- /dev/null +++ b/include/sys/fdtable.h @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2018 Linaro Limited + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef ZEPHYR_INCLUDE_SYS_FDTABLE_H_ +#define ZEPHYR_INCLUDE_SYS_FDTABLE_H_ + +#include +#include +/* FIXME: For native_posix ssize_t, off_t. */ +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * File descriptor virtual method table. + * All operations beyond read/write go thru ioctl method. + */ +struct fd_op_vtable { + ssize_t (*read)(void *obj, void *buf, size_t sz); + ssize_t (*write)(void *obj, const void *buf, size_t sz); + int (*ioctl)(void *obj, unsigned int request, va_list args); +}; + +/** + * @brief Reserve file descriptor. + * + * This function allows to reserve a space for file descriptor entry in + * the underlying table, and thus allows caller to fail fast if no free + * descriptor is available. If this function succeeds, z_finalize_fd() + * or z_free_fd() must be called mandatorily. + * + * @return Allocated file descriptor, or -1 in case of error (errno is set) + */ +int z_reserve_fd(void); + +/** + * @brief Finalize creation of file descriptor. + * + * This function should be called exactly once after z_reserve_fd(), and + * should not be called in any other case. + * + * @param fd File descriptor previously returned by z_reserve_fd() + * @param obj pointer to I/O object structure + * @param vtable pointer to I/O operation implementations for the object + */ +void z_finalize_fd(int fd, void *obj, const struct fd_op_vtable *vtable); + +/** + * @brief Allocate file descriptor for underlying I/O object. + * + * This function combines operations of z_reserve_fd() and z_finalize_fd() + * in one step, and provided for convenience. + * + * @param obj pointer to I/O object structure + * @param vtable pointer to I/O operation implementations for the object + * + * @return Allocated file descriptor, or -1 in case of error (errno is set) + */ +int z_alloc_fd(void *obj, const struct fd_op_vtable *vtable); + +/** + * @brief Release reserved file descriptor. + * + * This function may be called once after z_reserve_fd(), and should + * not be called in any other case. + * + * @param fd File descriptor previously returned by z_reserve_fd() + */ +void z_free_fd(int fd); + +/** + * @brief Get underlying object pointer from file descriptor. + * + * This function is useful for functions other than read/write/ioctl + * to look up underlying I/O object by fd, optionally checking its + * type (using vtable reference). If fd refers to invalid entry, + * NULL will be returned with errno set to EBADF. If fd is valid, + * but vtable param is not NULL and doesn't match object's vtable, + * NULL is returned and errno set to err param. + * + * @param fd File descriptor previously returned by z_reserve_fd() + * @param vtable Expected object vtable or NULL + * @param err errno value to set if object vtable doesn't match + * + * @return Object pointer or NULL, with errno set + */ +void *z_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err); + +/** + * @brief Get underlying object pointer and vtable pointer from file descriptor. + * + * @param fd File descriptor previously returned by z_reserve_fd() + * @param vtable A pointer to a pointer variable to store the vtable + * + * @return Object pointer or NULL, with errno set + */ +void *z_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable); + +/** + * @brief Call ioctl vmethod on an object using varargs. + * + * We need this helper function because ioctl vmethod is declared to + * take va_list and the only portable way to construct va_list is from + * function's ... parameters. + * + * @param vtable vtable containing ioctl function pointer + * @param obj Object to call ioctl on + * @param request ioctl request number + * @param ... Variadic arguments to ioctl + */ +static inline int z_fdtable_call_ioctl(const struct fd_op_vtable *vtable, void *obj, + unsigned long request, ...) +{ + va_list args; + int res; + + va_start(args, request); + res = vtable->ioctl(obj, request, args); + va_end(args); + + return res; +} + +/** + * Request codes for fd_op_vtable.ioctl(). + * + * Note that these codes are internal Zephyr numbers, for internal + * Zephyr operations (and subject to change without notice, not part + * of "stable ABI"). These are however expected to co-exist with + * "well-known" POSIX/Linux ioctl numbers, and not clash with them. + */ +enum { + /* Codes below 0x100 are reserved for fcntl() codes. */ + ZFD_IOCTL_CLOSE = 0x100, + ZFD_IOCTL_FSYNC, + ZFD_IOCTL_LSEEK, + ZFD_IOCTL_POLL_PREPARE, + ZFD_IOCTL_POLL_UPDATE, + ZFD_IOCTL_GETSOCKNAME, +}; + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SYS_FDTABLE_H_ */ diff --git a/lib/os/fdtable.c b/lib/os/fdtable.c index 8171606101a..d25e6593fa4 100644 --- a/lib/os/fdtable.c +++ b/lib/os/fdtable.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include struct fd_entry { diff --git a/lib/posix/fs.c b/lib/posix/fs.c index 0f0dd9dd366..466821deaf6 100644 --- a/lib/posix/fs.c +++ b/lib/posix/fs.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include BUILD_ASSERT_MSG(PATH_MAX >= MAX_FILE_NAME, "PATH_MAX is less than MAX_FILE_NAME"); diff --git a/subsys/net/lib/sockets/sockets.c b/subsys/net/lib/sockets/sockets.c index 92c75f9f59f..6360c60167d 100644 --- a/subsys/net/lib/sockets/sockets.c +++ b/subsys/net/lib/sockets/sockets.c @@ -16,7 +16,7 @@ LOG_MODULE_REGISTER(net_sock, CONFIG_NET_SOCKETS_LOG_LEVEL); #include #include #include -#include +#include #include #include "sockets_internal.h" diff --git a/subsys/net/lib/sockets/sockets_can.c b/subsys/net/lib/sockets/sockets_can.c index 555046ddb23..b54030c21ad 100644 --- a/subsys/net/lib/sockets/sockets_can.c +++ b/subsys/net/lib/sockets/sockets_can.c @@ -17,7 +17,7 @@ LOG_MODULE_REGISTER(net_sock_can, CONFIG_NET_SOCKETS_LOG_LEVEL); #include #include #include -#include +#include #include #include "sockets_internal.h" diff --git a/subsys/net/lib/sockets/sockets_internal.h b/subsys/net/lib/sockets/sockets_internal.h index 2ce016f3843..819c16b1530 100644 --- a/subsys/net/lib/sockets/sockets_internal.h +++ b/subsys/net/lib/sockets/sockets_internal.h @@ -7,7 +7,7 @@ #ifndef _SOCKETS_INTERNAL_H_ #define _SOCKETS_INTERNAL_H_ -#include +#include #define SOCK_EOF 1 #define SOCK_NONBLOCK 2 diff --git a/subsys/net/lib/sockets/sockets_packet.c b/subsys/net/lib/sockets/sockets_packet.c index 5b9a817f6fc..0e0aad0c52d 100644 --- a/subsys/net/lib/sockets/sockets_packet.c +++ b/subsys/net/lib/sockets/sockets_packet.c @@ -18,7 +18,7 @@ LOG_MODULE_REGISTER(net_sock_packet, CONFIG_NET_SOCKETS_LOG_LEVEL); #include #include #include -#include +#include #include "sockets_internal.h" diff --git a/subsys/net/lib/sockets/sockets_tls.c b/subsys/net/lib/sockets/sockets_tls.c index de95ec3a0a6..0f53e783203 100644 --- a/subsys/net/lib/sockets/sockets_tls.c +++ b/subsys/net/lib/sockets/sockets_tls.c @@ -17,7 +17,7 @@ LOG_MODULE_REGISTER(net_sock_tls, CONFIG_NET_SOCKETS_LOG_LEVEL); #include #include #include -#include +#include #if defined(CONFIG_MBEDTLS) #if !defined(CONFIG_MBEDTLS_CFG_FILE)