diff --git a/config/zephyr/Kconfig b/config/zephyr/Kconfig index 7b0ee1d15062d5..7534fbd2e03040 100644 --- a/config/zephyr/Kconfig +++ b/config/zephyr/Kconfig @@ -30,7 +30,6 @@ menuconfig CHIP imply NET_IPV6 imply NET_CONFIG_NEED_IPV6 imply NET_SOCKETS - imply NET_SOCKETS_POSIX_NAMES if !ARCH_POSIX && CHIP_NRF_PLATFORM imply NETWORKING imply HWINFO imply FLASH diff --git a/src/include/platform/internal/GenericPlatformManagerImpl_Zephyr.h b/src/include/platform/internal/GenericPlatformManagerImpl_Zephyr.h index 70b382f961427e..22f53f49a193b6 100644 --- a/src/include/platform/internal/GenericPlatformManagerImpl_Zephyr.h +++ b/src/include/platform/internal/GenericPlatformManagerImpl_Zephyr.h @@ -31,7 +31,7 @@ #endif #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS -#include +#include // nogncheck #endif #include diff --git a/src/inet/BUILD.gn b/src/inet/BUILD.gn index ede5d383734566..b69c61281bbcd8 100644 --- a/src/inet/BUILD.gn +++ b/src/inet/BUILD.gn @@ -78,6 +78,12 @@ source_set("impl_header") { public_deps = [ "${chip_root}/src/system:system_config_header" ] } +source_set("inet_zephyr_socket") { + sources = [ "ZephyrSocket.h" ] + public_configs = [ "${chip_root}/src:includes" ] + public_deps = [ "${chip_root}/src/system:system_config_header" ] +} + static_library("inet") { output_name = "libInetLayer" @@ -167,12 +173,10 @@ static_library("inet") { } else { sources += [ "UDPEndPointImpl${chip_system_config_inet}.cpp" ] } + } - if (chip_system_config_inet == "Sockets") { - # TODO: dependency on this one is not clear as it is only ever - # enabled through CMakeLists.txt. Added here for completeness - sources += [ "ZephyrSocket.h" ] - } + if (current_os == "zephyr") { + public_deps += [ ":inet_zephyr_socket" ] } if (chip_with_nlfaultinjection) { diff --git a/src/inet/IPAddress.h b/src/inet/IPAddress.h index 03a40394b5e80b..7937878e92afd9 100644 --- a/src/inet/IPAddress.h +++ b/src/inet/IPAddress.h @@ -63,8 +63,8 @@ #endif // CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS -#include -#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS +#include "ZephyrSocket.h" // nogncheck +#endif #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT && INET_CONFIG_ENABLE_IPV4 #error Forbidden : native Open Thread implementation with IPV4 enabled diff --git a/src/inet/UDPEndPointImplSockets.cpp b/src/inet/UDPEndPointImplSockets.cpp index ed43dc0ecca8f3..811d8a50d6194a 100644 --- a/src/inet/UDPEndPointImplSockets.cpp +++ b/src/inet/UDPEndPointImplSockets.cpp @@ -39,9 +39,9 @@ #include #endif // CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS -#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS -#include -#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS +#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS || CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS +#include "ZephyrSocket.h" // nogncheck +#endif #include #include @@ -57,10 +57,6 @@ #define INADDR_ANY 0 #endif -#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS -#include "ZephyrSocket.h" -#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS - /* * Some systems define both IPV6_{ADD,DROP}_MEMBERSHIP and * IPV6_{JOIN,LEAVE}_GROUP while others only define diff --git a/src/inet/ZephyrSocket.h b/src/inet/ZephyrSocket.h index 93e4c055d8059e..24912ef7fe6557 100644 --- a/src/inet/ZephyrSocket.h +++ b/src/inet/ZephyrSocket.h @@ -1,4 +1,4 @@ -#/* +/* * * Copyright (c) 2020 Project CHIP Authors * @@ -17,23 +17,27 @@ /** * @file - * This header file defines BSD socket API functions which for various - * reasons have not (yet) been implemented or exposed in Zephyr. + * This header file includes Zephyr socket header and implements POSIX wrappers for APIs with + * `zsock_` prefix if necessary. It also defines BSD socket API functions which for various + * reasons have not been implemented or exposed in older Zephyr version. */ #pragma once -#if CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS -#include -#endif - #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS #include #endif + +#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS + +#if CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS +#include +#endif + static inline ssize_t recvmsg(int sock, struct msghdr * msg, int flags) { - // Zephyr doesn't implement recvmsg at all, but if the message vector size is > 0 we can simply + // Older Zephyr version doesn't implement recvmsg at all, but if the message vector size is > 0 we can simply // translate recvmsg to recvfrom which fills only the first of the provided buffers (although // we don't get control messages in such a case). @@ -51,3 +55,241 @@ static inline ssize_t recvmsg(int sock, struct msghdr * msg, int flags) return ret; } + +#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS + +#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_NAMES +extern "C" { + +/** POSIX wrapper for @ref zsock_fd_set */ +#define fd_set zsock_fd_set +/** POSIX wrapper for @ref ZSOCK_FD_SETSIZE */ +#define FD_SETSIZE ZSOCK_FD_SETSIZE + +/** POSIX wrapper for @ref ZSOCK_FD_ZERO */ +static inline void FD_ZERO(zsock_fd_set * set) +{ + ZSOCK_FD_ZERO(set); +} + +/** POSIX wrapper for @ref ZSOCK_FD_ISSET */ +static inline int FD_ISSET(int fd, zsock_fd_set * set) +{ + return ZSOCK_FD_ISSET(fd, set); +} + +/** POSIX wrapper for @ref ZSOCK_FD_CLR */ +static inline void FD_CLR(int fd, zsock_fd_set * set) +{ + ZSOCK_FD_CLR(fd, set); +} + +/** POSIX wrapper for @ref ZSOCK_FD_SET */ +static inline void FD_SET(int fd, zsock_fd_set * set) +{ + ZSOCK_FD_SET(fd, set); +} + +/** POSIX wrapper for @ref zsock_select */ +static inline int select(int nfds, zsock_fd_set * readfds, zsock_fd_set * writefds, zsock_fd_set * exceptfds, + struct timeval * timeout) +{ + return zsock_select(nfds, readfds, writefds, exceptfds, timeout); +} + +/** POSIX wrapper for @ref zsock_pollfd */ +#define pollfd zsock_pollfd + +/** POSIX wrapper for @ref zsock_socket */ +static inline int socket(int family, int type, int proto) +{ + return zsock_socket(family, type, proto); +} + +/** POSIX wrapper for @ref zsock_socketpair */ +static inline int socketpair(int family, int type, int proto, int sv[2]) +{ + return zsock_socketpair(family, type, proto, sv); +} + +/** POSIX wrapper for @ref zsock_close */ +static inline int close(int sock) +{ + return zsock_close(sock); +} + +/** POSIX wrapper for @ref zsock_shutdown */ +static inline int shutdown(int sock, int how) +{ + return zsock_shutdown(sock, how); +} + +/** POSIX wrapper for @ref zsock_bind */ +static inline int bind(int sock, const struct sockaddr * addr, socklen_t addrlen) +{ + return zsock_bind(sock, addr, addrlen); +} + +/** POSIX wrapper for @ref zsock_connect */ +static inline int connect(int sock, const struct sockaddr * addr, socklen_t addrlen) +{ + return zsock_connect(sock, addr, addrlen); +} + +/** POSIX wrapper for @ref zsock_listen */ +static inline int listen(int sock, int backlog) +{ + return zsock_listen(sock, backlog); +} + +/** POSIX wrapper for @ref zsock_accept */ +static inline int accept(int sock, struct sockaddr * addr, socklen_t * addrlen) +{ + return zsock_accept(sock, addr, addrlen); +} + +/** POSIX wrapper for @ref zsock_send */ +static inline ssize_t send(int sock, const void * buf, size_t len, int flags) +{ + return zsock_send(sock, buf, len, flags); +} + +/** POSIX wrapper for @ref zsock_recv */ +static inline ssize_t recv(int sock, void * buf, size_t max_len, int flags) +{ + return zsock_recv(sock, buf, max_len, flags); +} + +/** POSIX wrapper for @ref zsock_sendto */ +static inline ssize_t sendto(int sock, const void * buf, size_t len, int flags, const struct sockaddr * dest_addr, + socklen_t addrlen) +{ + return zsock_sendto(sock, buf, len, flags, dest_addr, addrlen); +} + +/** POSIX wrapper for @ref zsock_sendmsg */ +static inline ssize_t sendmsg(int sock, const struct msghdr * message, int flags) +{ + return zsock_sendmsg(sock, message, flags); +} + +/** POSIX wrapper for @ref zsock_recvfrom */ +static inline ssize_t recvfrom(int sock, void * buf, size_t max_len, int flags, struct sockaddr * src_addr, socklen_t * addrlen) +{ + return zsock_recvfrom(sock, buf, max_len, flags, src_addr, addrlen); +} + +/** POSIX wrapper for @ref zsock_recvmsg */ +static inline ssize_t recvmsg(int sock, struct msghdr * msg, int flags) +{ + return zsock_recvmsg(sock, msg, flags); +} + +/** POSIX wrapper for @ref zsock_poll */ +static inline int poll(struct zsock_pollfd * fds, int nfds, int timeout) +{ + return zsock_poll(fds, nfds, timeout); +} + +/** POSIX wrapper for @ref zsock_getsockopt */ +static inline int getsockopt(int sock, int level, int optname, void * optval, socklen_t * optlen) +{ + return zsock_getsockopt(sock, level, optname, optval, optlen); +} + +/** POSIX wrapper for @ref zsock_setsockopt */ +static inline int setsockopt(int sock, int level, int optname, const void * optval, socklen_t optlen) +{ + return zsock_setsockopt(sock, level, optname, optval, optlen); +} + +/** POSIX wrapper for @ref zsock_getpeername */ +static inline int getpeername(int sock, struct sockaddr * addr, socklen_t * addrlen) +{ + return zsock_getpeername(sock, addr, addrlen); +} + +/** POSIX wrapper for @ref zsock_getsockname */ +static inline int getsockname(int sock, struct sockaddr * addr, socklen_t * addrlen) +{ + return zsock_getsockname(sock, addr, addrlen); +} + +/** POSIX wrapper for @ref zsock_getaddrinfo */ +static inline int getaddrinfo(const char * host, const char * service, const struct zsock_addrinfo * hints, + struct zsock_addrinfo ** res) +{ + return zsock_getaddrinfo(host, service, hints, res); +} + +/** POSIX wrapper for @ref zsock_freeaddrinfo */ +static inline void freeaddrinfo(struct zsock_addrinfo * ai) +{ + zsock_freeaddrinfo(ai); +} + +/** POSIX wrapper for @ref zsock_gai_strerror */ +static inline const char * gai_strerror(int errcode) +{ + return zsock_gai_strerror(errcode); +} + +/** POSIX wrapper for @ref zsock_getnameinfo */ +static inline int getnameinfo(const struct sockaddr * addr, socklen_t addrlen, char * host, socklen_t hostlen, char * serv, + socklen_t servlen, int flags) +{ + return zsock_getnameinfo(addr, addrlen, host, hostlen, serv, servlen, flags); +} + +/** POSIX wrapper for @ref zsock_addrinfo */ +#define addrinfo zsock_addrinfo + +/** POSIX wrapper for @ref zsock_gethostname */ +static inline int gethostname(char * buf, size_t len) +{ + return zsock_gethostname(buf, len); +} + +/** POSIX wrapper for @ref zsock_inet_pton */ +static inline int inet_pton(sa_family_t family, const char * src, void * dst) +{ + return zsock_inet_pton(family, src, dst); +} + +/** POSIX wrapper for @ref zsock_inet_ntop */ +static inline char * inet_ntop(sa_family_t family, const void * src, char * dst, size_t size) +{ + return zsock_inet_ntop(family, src, dst, size); +} + +/** POSIX wrapper for @ref ZSOCK_POLLIN */ +#define POLLIN ZSOCK_POLLIN +/** POSIX wrapper for @ref ZSOCK_POLLOUT */ +#define POLLOUT ZSOCK_POLLOUT +/** POSIX wrapper for @ref ZSOCK_POLLERR */ +#define POLLERR ZSOCK_POLLERR +/** POSIX wrapper for @ref ZSOCK_POLLHUP */ +#define POLLHUP ZSOCK_POLLHUP +/** POSIX wrapper for @ref ZSOCK_POLLNVAL */ +#define POLLNVAL ZSOCK_POLLNVAL + +/** POSIX wrapper for @ref ZSOCK_MSG_PEEK */ +#define MSG_PEEK ZSOCK_MSG_PEEK +/** POSIX wrapper for @ref ZSOCK_MSG_CTRUNC */ +#define MSG_CTRUNC ZSOCK_MSG_CTRUNC +/** POSIX wrapper for @ref ZSOCK_MSG_TRUNC */ +#define MSG_TRUNC ZSOCK_MSG_TRUNC +/** POSIX wrapper for @ref ZSOCK_MSG_DONTWAIT */ +#define MSG_DONTWAIT ZSOCK_MSG_DONTWAIT +/** POSIX wrapper for @ref ZSOCK_MSG_WAITALL */ +#define MSG_WAITALL ZSOCK_MSG_WAITALL + +/** POSIX wrapper for @ref ZSOCK_SHUT_RD */ +#define SHUT_RD ZSOCK_SHUT_RD +/** POSIX wrapper for @ref ZSOCK_SHUT_WR */ +#define SHUT_WR ZSOCK_SHUT_WR +/** POSIX wrapper for @ref ZSOCK_SHUT_RDWR */ +#define SHUT_RDWR ZSOCK_SHUT_RDWR + +} // extern "C" +#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_NAMES diff --git a/src/system/BUILD.gn b/src/system/BUILD.gn index 3fff2a61e648da..50a3dd7d43e472 100644 --- a/src/system/BUILD.gn +++ b/src/system/BUILD.gn @@ -272,6 +272,10 @@ static_library("system") { sources += [ "SocketEvents.h" ] } + if (current_os == "zephyr") { + public_deps += [ "${chip_root}/src/inet:inet_zephyr_socket" ] + } + if (chip_with_nlfaultinjection) { sources += [ "SystemFaultInjection.cpp", diff --git a/src/system/SystemClock.h b/src/system/SystemClock.h index 4c691e1d2b7039..ea4d92df9e737b 100644 --- a/src/system/SystemClock.h +++ b/src/system/SystemClock.h @@ -37,7 +37,7 @@ #endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS || CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS -#include +#include // nogncheck #endif #include diff --git a/src/system/SystemConfig.h b/src/system/SystemConfig.h index 2fe0823922467e..f64a34aa8671b2 100644 --- a/src/system/SystemConfig.h +++ b/src/system/SystemConfig.h @@ -752,13 +752,30 @@ struct LwIPEvent; * Defaults to enabled on Zephyr platforms that do not enable Zephyr POSIX layer. */ #ifndef CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS -#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && defined(__ZEPHYR__) && defined(CONFIG_NET_SOCKETS_POSIX_NAMES) +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && defined(__ZEPHYR__) && !defined(CONFIG_POSIX_API) #define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS 1 #else #define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS 0 #endif #endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS +/** + * @def CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_NAMES + * + * @brief + * Use Zephyr socket API with `zsock_` prefix. + * + * Defaults to enabled if `CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS` is enabled, + * but `CONFIG_NET_SOCKETS_POSIX_NAMES` is disabled. + */ +#ifndef CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_NAMES +#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS && !defined(CONFIG_NET_SOCKETS_POSIX_NAMES) +#define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_NAMES 1 +#else +#define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_NAMES 0 +#endif +#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_NAMES + /** * @def CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS * diff --git a/src/system/SystemLayerImplSelect.h b/src/system/SystemLayerImplSelect.h index d2f8c880ee50da..dde12a3abff2f7 100644 --- a/src/system/SystemLayerImplSelect.h +++ b/src/system/SystemLayerImplSelect.h @@ -29,7 +29,7 @@ #endif #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS -#include +#include // nogncheck #endif #if CHIP_SYSTEM_CONFIG_POSIX_LOCKING