Skip to content
34 changes: 22 additions & 12 deletions core/shared/platform/include/platform_wasi_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,6 @@ assert_wasi_layout(offsetof(__wasi_subscription_t, userdata) == 0, "witx calcula
assert_wasi_layout(offsetof(__wasi_subscription_t, u) == 8, "witx calculated offset");

/* keep syncing with wasi_socket_ext.h */
typedef enum {
/* Used only for sock_addr_resolve hints */
SOCKET_ANY = -1,
SOCKET_DGRAM = 0,
SOCKET_STREAM,
} __wasi_sock_type_t;

typedef uint16_t __wasi_ip_port_t;

Expand Down Expand Up @@ -589,20 +583,36 @@ typedef struct __wasi_addr_t {
} addr;
} __wasi_addr_t;

typedef enum { INET4 = 0, INET6, INET_UNSPEC } __wasi_address_family_t;
/* Force 32-bit wire width for cross-boundary fields */
typedef int32_t __wasi_sock_type_t;
enum { SOCKET_ANY = -1, SOCKET_DGRAM = 0, SOCKET_STREAM = 1 };

typedef int32_t __wasi_address_family_t;
enum { INET4 = 0, INET6 = 1, INET_UNSPEC = 2 };

typedef struct __wasi_addr_info_t {
__wasi_addr_t addr;
__wasi_addr_t addr;
__wasi_sock_type_t type;
} __wasi_addr_info_t;

typedef struct __wasi_addr_info_hints_t {
__wasi_sock_type_t type;
__wasi_address_family_t family;
// this is to workaround lack of optional parameters
uint8_t hints_enabled;
__wasi_sock_type_t type; // 4 bytes
__wasi_address_family_t family; // 4 bytes
uint8_t hints_enabled; // 1 byte
uint8_t _pad[3]; // enforce layout
} __wasi_addr_info_hints_t;

assert_wasi_layout(sizeof(__wasi_sock_type_t) == 4, "sock_type must be 4 bytes");
assert_wasi_layout(sizeof(__wasi_address_family_t) == 4, "addr_family must be 4 bytes");

assert_wasi_layout(sizeof(__wasi_addr_info_hints_t) == 12, "hints_t must be 12 bytes");
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, type) == 0, "hints.type@0");
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, family) == 4, "hints.family@4");
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, hints_enabled) == 8, "hints.enabled@8");

assert_wasi_layout(offsetof(__wasi_addr_info_t, type) == sizeof(__wasi_addr_t),
"addr_info.type follows addr");

#undef assert_wasi_layout

/* clang-format on */
Expand Down
2 changes: 2 additions & 0 deletions core/shared/platform/zephyr/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ typedef struct timespec os_timespec;
#define CLOCK_REALTIME 1
#endif

#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC 4
#endif

static inline int
os_sched_yield(void)
Expand Down
30 changes: 29 additions & 1 deletion core/shared/platform/zephyr/zephyr_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include <zephyr/fs/fs_sys.h>
#include <zephyr/fs/littlefs.h>

#include <zephyr/net/socket.h>
#include <zephyr/posix/fcntl.h> // F_GETFL, F_SETFL, O_NONBLOCK

/* Notes:
* This is the implementation of a POSIX-like file system interface for Zephyr.
* To manage our file descriptors, we created a struct `zephyr_fs_desc` that
Expand Down Expand Up @@ -273,6 +276,16 @@ os_file_get_fdflags(os_file_handle handle, __wasi_fdflags_t *flags)
{
struct zephyr_fs_desc *ptr = NULL;

/* Sockets: reflect current non-blocking state */
if (handle->is_sock) {
int cur = zsock_fcntl(handle->fd, F_GETFL, 0);
if (cur < 0)
return convert_errno(errno);
if (cur & O_NONBLOCK)
*flags |= __WASI_FDFLAG_NONBLOCK;
return __WASI_ESUCCESS;
}

if (os_is_virtual_fd(handle->fd)) {
*flags = 0;
return __WASI_ESUCCESS;
Expand All @@ -296,6 +309,21 @@ os_file_get_fdflags(os_file_handle handle, __wasi_fdflags_t *flags)
__wasi_errno_t
os_file_set_fdflags(os_file_handle handle, __wasi_fdflags_t flags)
{
/* Sockets: set/clear O_NONBLOCK */
if (handle->is_sock) {
int cur = zsock_fcntl(handle->fd, F_GETFL, 0);
if (cur < 0)
return convert_errno(errno);

bool want_nb = (flags & __WASI_FDFLAG_NONBLOCK) != 0;
int newf = want_nb ? (cur | O_NONBLOCK) : (cur & ~O_NONBLOCK);

if (zsock_fcntl(handle->fd, F_SETFL, newf) < 0) {
return convert_errno(errno);
}
return __WASI_ESUCCESS;
}

if (os_is_virtual_fd(handle->fd)) {
return __WASI_ESUCCESS;
}
Expand Down Expand Up @@ -1195,4 +1223,4 @@ bool
os_is_stderr_handle(os_file_handle handle)
{
return (handle == (os_file_handle)stderr);
}
}
Loading
Loading