Skip to content
Closed
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
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
47 changes: 34 additions & 13 deletions core/shared/platform/zephyr/zephyr_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#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
// #include <zephyr/net/socket_select.h>

/* 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 +277,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 @@ -283,31 +297,38 @@ os_file_get_fdflags(os_file_handle handle, __wasi_fdflags_t *flags)
if ((ptr->file.flags & FS_O_APPEND) != 0) {
*flags |= __WASI_FDFLAG_APPEND;
}
/* Others flags:
* - __WASI_FDFLAG_DSYNC
* - __WASI_FDFLAG_RSYNC
* - __WASI_FDFLAG_SYNC
* - __WASI_FDFLAG_NONBLOCK
* Have no equivalent in Zephyr.
*/

return __WASI_ESUCCESS;
}

__wasi_errno_t
os_file_set_fdflags(os_file_handle handle, __wasi_fdflags_t flags)
{
if (os_is_virtual_fd(handle->fd)) {
/* 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;
}

struct zephyr_fs_desc *ptr = NULL;
/* Virtual stdio */
if (os_is_virtual_fd(handle->fd))
return __WASI_ESUCCESS;

/* Regular files: keep existing behavior */
struct zephyr_fs_desc *ptr = NULL;
GET_FILE_SYSTEM_DESCRIPTOR(handle->fd, ptr);

if ((flags & __WASI_FDFLAG_APPEND) != 0) {
if ((flags & __WASI_FDFLAG_APPEND) != 0)
ptr->file.flags |= FS_O_APPEND;
}
/* Same as above */

return __WASI_ESUCCESS;
}

Expand Down
Loading
Loading