From 4d99c69e4aa818c1407789e4e21dfa9102cb533b Mon Sep 17 00:00:00 2001 From: Bjarki Arge Andreasen Date: Wed, 22 Nov 2023 07:33:38 +0100 Subject: [PATCH] modem: backend: tty: Implement transmit idle event Implement transmit idle notification for TTY backend. Since TTY has an "infinite" transmit buffer, we invoke transmit idle immediately after writing the data to the TTY file. The test suite for the TTY backend has been updated to match the new behavior. Signed-off-by: Bjarki Arge Andreasen --- subsys/modem/backends/modem_backend_tty.c | 5 ++++- tests/subsys/modem/backends/tty/src/main.c | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/subsys/modem/backends/modem_backend_tty.c b/subsys/modem/backends/modem_backend_tty.c index b8864fc3d91..8375183be48 100644 --- a/subsys/modem/backends/modem_backend_tty.c +++ b/subsys/modem/backends/modem_backend_tty.c @@ -73,8 +73,11 @@ static int modem_backend_tty_open(void *data) static int modem_backend_tty_transmit(void *data, const uint8_t *buf, size_t size) { struct modem_backend_tty *backend = (struct modem_backend_tty *)data; + int ret; - return write(backend->tty_fd, buf, size); + ret = write(backend->tty_fd, buf, size); + modem_pipe_notify_transmit_idle(&backend->pipe); + return ret; } static int modem_backend_tty_receive(void *data, uint8_t *buf, size_t size) diff --git a/tests/subsys/modem/backends/tty/src/main.c b/tests/subsys/modem/backends/tty/src/main.c index 49a1841486a..96ebdb2763d 100644 --- a/tests/subsys/modem/backends/tty/src/main.c +++ b/tests/subsys/modem/backends/tty/src/main.c @@ -22,7 +22,8 @@ #define TEST_MODEM_BACKEND_TTY_PIPE_EVENT_OPENED_BIT (0) #define TEST_MODEM_BACKEND_TTY_PIPE_EVENT_RRDY_BIT (1) -#define TEST_MODEM_BACKEND_TTY_PIPE_EVENT_CLOSED_BIT (2) +#define TEST_MODEM_BACKEND_TTY_PIPE_EVENT_TIDLE_BIT (2) +#define TEST_MODEM_BACKEND_TTY_PIPE_EVENT_CLOSED_BIT (3) #define TEST_MODEM_BACKEND_TTY_OP_DELAY (K_MSEC(1000)) @@ -72,6 +73,10 @@ static void modem_pipe_callback_handler(struct modem_pipe *pipe, enum modem_pipe atomic_set_bit(&tty_pipe_events, TEST_MODEM_BACKEND_TTY_PIPE_EVENT_RRDY_BIT); break; + case MODEM_PIPE_EVENT_TRANSMIT_IDLE: + atomic_set_bit(&tty_pipe_events, TEST_MODEM_BACKEND_TTY_PIPE_EVENT_TIDLE_BIT); + break; + case MODEM_PIPE_EVENT_CLOSED: atomic_set_bit(&tty_pipe_events, TEST_MODEM_BACKEND_TTY_PIPE_EVENT_CLOSED_BIT); break; @@ -122,9 +127,13 @@ static void test_modem_backend_tty_teardown(void *f) /*************************************************************************************************/ ZTEST(modem_backend_tty_suite, test_close_open) { + bool result; + zassert_ok(modem_pipe_close(tty_pipe), "Failed to close pipe"); zassert_ok(modem_pipe_close(tty_pipe), "Pipe should already be closed"); zassert_ok(modem_pipe_open(tty_pipe), "Failed to open pipe"); + result = atomic_test_bit(&tty_pipe_events, TEST_MODEM_BACKEND_TTY_PIPE_EVENT_TIDLE_BIT); + zassert_true(result, "Transmit idle event should be set"); zassert_ok(modem_pipe_open(tty_pipe), "Pipe should already be open"); } @@ -172,12 +181,19 @@ ZTEST(modem_backend_tty_suite, test_transmit) { int ret; char msg[] = "Test me buddy 2"; + bool result; + + result = atomic_test_bit(&tty_pipe_events, TEST_MODEM_BACKEND_TTY_PIPE_EVENT_TIDLE_BIT); + zassert_false(result, "Transmit idle event should not be set"); ret = modem_pipe_transmit(tty_pipe, msg, sizeof(msg)); zassert_true(ret == sizeof(msg), "Failed to transmit using pipe"); k_sleep(TEST_MODEM_BACKEND_TTY_OP_DELAY); + result = atomic_test_bit(&tty_pipe_events, TEST_MODEM_BACKEND_TTY_PIPE_EVENT_TIDLE_BIT); + zassert_true(result, "Transmit idle event should be set"); + ret = read(primary_fd, buffer1, sizeof(buffer1)); zassert_true(ret == sizeof(msg), "Read incorrect number of bytes"); ret = memcmp(msg, buffer1, sizeof(msg));