Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Zephyr] Remove redundant config and add implementation of POSIX API wrappers if necessary #37007

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#endif

#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS
#include <zephyr/net/socket.h>
#include <inet/ZephyrSocket.h> // nogncheck
#endif

#include <zephyr/kernel.h>
Expand Down
14 changes: 9 additions & 5 deletions src/inet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/inet/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS

#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS
#include <zephyr/net/socket.h>
#include "ZephyrSocket.h" // nogncheck
#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS

#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT && INET_CONFIG_ENABLE_IPV4
Expand Down
6 changes: 1 addition & 5 deletions src/inet/UDPEndPointImplSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS

#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS
#include <zephyr/net/socket.h>
#include "ZephyrSocket.h" // nogncheck
#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS

#include <cerrno>
Expand All @@ -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
Expand Down
253 changes: 231 additions & 22 deletions src/inet/ZephyrSocket.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#/*
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
Expand All @@ -17,37 +17,246 @@

/**
* @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.
*/

#pragma once

#if CHIP_SYSTEM_CONFIG_USE_POSIX_SOCKETS
#include <sys/select.h>
#endif

#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKETS
#include <zephyr/net/socket.h>
#endif

#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)
{
// Zephyr 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).
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);
}

if (msg->msg_iovlen < 1)
{
errno = EMSGSIZE;
return -1;
}
/** 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);
}

ssize_t ret = recvfrom(sock, msg->msg_iov[0].iov_base, msg->msg_iov[0].iov_len, flags, static_cast<sockaddr *>(msg->msg_name),
&msg->msg_namelen);
/** POSIX wrapper for @ref zsock_getpeername */
static inline int getpeername(int sock, struct sockaddr * addr, socklen_t * addrlen)
{
return zsock_getpeername(sock, addr, addrlen);
}

if (ret >= 0)
msg->msg_controllen = 0;
/** POSIX wrapper for @ref zsock_getsockname */
static inline int getsockname(int sock, struct sockaddr * addr, socklen_t * addrlen)
{
return zsock_getsockname(sock, addr, addrlen);
}

return ret;
/** 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
1 change: 0 additions & 1 deletion src/platform/Ameba/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ struct ChipDeviceEvent;

#define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF 0
#define CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS 0
#define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS00
#define CHIP_SYSTEM_CONFIG_USE_LWIP 1
#define CHIP_SYSTEM_CONFIG_USE_SOCKETS 0
#define CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK 0
Expand Down
1 change: 0 additions & 1 deletion src/platform/Beken/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ struct ChipDeviceEvent;

#define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF 0
#define CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS 0
#define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS0 0
#define CHIP_SYSTEM_CONFIG_USE_LWIP 1
#define CHIP_SYSTEM_CONFIG_USE_SOCKETS 0
#define CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK 0
Expand Down
2 changes: 0 additions & 2 deletions src/platform/Zephyr/CHIPPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,4 @@

#define INET_CONFIG_UDP_SOCKET_MREQN 1

#define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS 0

#endif
5 changes: 0 additions & 5 deletions src/platform/nrfconnect/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,3 @@ struct ChipDeviceEvent;
#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX 1280
#endif // CONFIG_WIFI_NRF700X
#endif // CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX

// ========== Platform-specific Configuration Overrides =========

// Disable Zephyr Socket extensions module, as the Zephyr RTOS now implements recvmsg()
#define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS 0
2 changes: 0 additions & 2 deletions src/platform/nxp/zephyr/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@
#pragma once

#include <platform/Zephyr/SystemPlatformConfig.h>

#define CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS 0
4 changes: 4 additions & 0 deletions src/system/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading
Loading