Skip to content

Commit

Permalink
modem: backend: tty: Implement transmit idle event
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bjarki-andreasen authored and fabiobaltieri committed Jan 11, 2024
1 parent 8c6a9ee commit 4d99c69
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion subsys/modem/backends/modem_backend_tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion tests/subsys/modem/backends/tty/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 4d99c69

Please sign in to comment.