modem: backend: tty: Support building with any C library

Ensure we call into the host C library open/close/read/write
independently of which embedded C library the code is built with.

We do this by:
a) Using the native simulator nsi_host* trampolines when we just want
   to call straight into the host libC.
b) Building in the native_simulator runner context (and therefore with
   the host C library) two functions which we call from the embedded
   side.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
This commit is contained in:
Alberto Escolar Piedras
2025-12-19 16:10:08 +01:00
committed by Fabio Baltieri
parent 98233a966f
commit 6ef08d1221
4 changed files with 56 additions and 16 deletions

View File

@@ -3,7 +3,10 @@
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_MODEM_BACKEND_TTY modem_backend_tty.c)
if(CONFIG_MODEM_BACKEND_TTY)
zephyr_library_sources(modem_backend_tty.c)
target_sources(native_simulator INTERFACE modem_backend_tty_bottom.c)
endif()
zephyr_library_sources_ifdef(CONFIG_MODEM_BACKEND_UART modem_backend_uart.c)
zephyr_library_sources_ifdef(CONFIG_MODEM_BACKEND_UART_ISR modem_backend_uart_isr.c)
if(CONFIG_MODEM_BACKEND_UART_ASYNC)

View File

@@ -5,13 +5,12 @@
*/
#include <zephyr/modem/backend/tty.h>
#include "modem_backend_tty_bottom.h"
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(modem_backend_tty, CONFIG_MODEM_MODULES_LOG_LEVEL);
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include <nsi_host_trampolines.h>
#include <string.h>
#define MODEM_BACKEND_TTY_THREAD_PRIO (10)
@@ -23,25 +22,21 @@ LOG_MODULE_REGISTER(modem_backend_tty, CONFIG_MODEM_MODULES_LOG_LEVEL);
static void modem_backend_tty_routine(void *p1, void *p2, void *p3)
{
struct modem_backend_tty *backend = (struct modem_backend_tty *)p1;
struct pollfd pd;
ARG_UNUSED(p2);
ARG_UNUSED(p3);
pd.fd = backend->tty_fd;
pd.events = POLLIN;
/* Run until run flag is cleared. Check every MODEM_BACKEND_TTY_THREAD_RUN_PERIOD_MS */
while (atomic_test_bit(&backend->state, MODEM_BACKEND_TTY_STATE_RUN_BIT)) {
/* Clear events */
pd.revents = 0;
if (poll(&pd, 1, MODEM_BACKEND_TTY_THREAD_RUN_PERIOD_MS) < 0) {
int ret = modem_backend_tty_poll_bottom(backend->tty_fd);
if (ret < 0) {
LOG_ERR("Poll operation failed");
break;
}
if (pd.revents & POLLIN) {
if (ret) {
modem_pipe_notify_receive_ready(&backend->pipe);
}
@@ -57,7 +52,7 @@ static int modem_backend_tty_open(void *data)
return -EALREADY;
}
backend->tty_fd = open(backend->tty_path, (O_RDWR | O_NONBLOCK), 0644);
backend->tty_fd = modem_backend_tty_open_bottom(backend->tty_path);
if (backend->tty_fd < 0) {
return -EPERM;
}
@@ -75,7 +70,7 @@ static int modem_backend_tty_transmit(void *data, const uint8_t *buf, size_t siz
struct modem_backend_tty *backend = (struct modem_backend_tty *)data;
int ret;
ret = write(backend->tty_fd, buf, size);
ret = nsi_host_write(backend->tty_fd, buf, size);
modem_pipe_notify_transmit_idle(&backend->pipe);
return ret;
}
@@ -85,7 +80,7 @@ static int modem_backend_tty_receive(void *data, uint8_t *buf, size_t size)
int ret;
struct modem_backend_tty *backend = (struct modem_backend_tty *)data;
ret = read(backend->tty_fd, buf, size);
ret = nsi_host_read(backend->tty_fd, buf, size);
return (ret < 0) ? 0 : ret;
}
@@ -98,7 +93,7 @@ static int modem_backend_tty_close(void *data)
}
k_thread_join(&backend->thread, K_MSEC(MODEM_BACKEND_TTY_THREAD_RUN_PERIOD_MS * 2));
close(backend->tty_fd);
nsi_host_close(backend->tty_fd);
modem_pipe_notify_closed(&backend->pipe);
return 0;
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2022 Trackunit Corporation
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <fcntl.h>
#include <poll.h>
int modem_backend_tty_poll_bottom(int fd)
{
struct pollfd pd = { .fd = fd, .events = POLLIN };
int ret;
ret = poll(&pd, 1, 0);
if ((ret > 0) && !(pd.revents & POLLIN)) {
/* we are only interested in POLLIN events */
ret = 0;
}
return ret;
}
int modem_backend_tty_open_bottom(const char *tty_path)
{
return open(tty_path, (O_RDWR | O_NONBLOCK), 0644);
}

View File

@@ -0,0 +1,13 @@
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_MODEM_BACKEND_UART_TTY_BOTTOM_
#define ZEPHYR_MODEM_BACKEND_UART_TTY_BOTTOM_
int modem_backend_tty_poll_bottom(int fd);
int modem_backend_tty_open_bottom(const char *tty_path);
#endif /* ZEPHYR_MODEM_BACKEND_UART_TTY_BOTTOM_ */