Skip to content

Commit

Permalink
modem: pipe: Add TRANSMIT_IDLE event
Browse files Browse the repository at this point in the history
Add transmit idle event to modem_pipe_event enum. This will
allow modules to await transmit idle before trying to transmit
more data, instead of blindly calling modem_pipe_transmit in
a loop until all data has been accepted.

This will reduce the time spent trying to transmit data while
the backend is blocked.

Similarly to the RECEIVE_READY event, backends will call
modem_pipe_notify_transmit_idle() to indicate that transmit
is idle, and the TRANSMIT_IDLE event will be reinvoked when
the modem pipe is attached to synchronize the state of the
pipe with the user of it.

Additionally, the TRANSMIT_IDLE event is also invoked when the
modem is opened to further help synchronization with the user
of the pipe.

Signed-off-by: Bjarki Arge Andreasen <[email protected]>
  • Loading branch information
bjarki-andreasen authored and fabiobaltieri committed Jan 11, 2024
1 parent 0f95b6f commit 516af3c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion include/zephyr/modem/pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
enum modem_pipe_event {
MODEM_PIPE_EVENT_OPENED = 0,
MODEM_PIPE_EVENT_RECEIVE_READY,
MODEM_PIPE_EVENT_TRANSMIT_IDLE,
MODEM_PIPE_EVENT_CLOSED,
};

Expand Down Expand Up @@ -73,7 +74,8 @@ struct modem_pipe {
enum modem_pipe_state state;
struct k_mutex lock;
struct k_condvar condvar;
bool receive_ready_pending;
uint8_t receive_ready_pending : 1;
uint8_t transmit_idle_pending : 1;
};

/**
Expand Down Expand Up @@ -213,6 +215,15 @@ void modem_pipe_notify_closed(struct modem_pipe *pipe);
*/
void modem_pipe_notify_receive_ready(struct modem_pipe *pipe);

/**
* @brief Notify user of pipe that pipe has no more data to transmit
*
* @param pipe Pipe instance
*
* @note Invoked from instance which initialized the pipe instance
*/
void modem_pipe_notify_transmit_idle(struct modem_pipe *pipe);

/**
* @endcond
*/
Expand Down
21 changes: 21 additions & 0 deletions subsys/modem/modem_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void modem_pipe_init(struct modem_pipe *pipe, void *data, struct modem_pipe_api
pipe->user_data = NULL;
pipe->state = MODEM_PIPE_STATE_CLOSED;
pipe->receive_ready_pending = false;
pipe->transmit_idle_pending = true;

k_mutex_init(&pipe->lock);
k_condvar_init(&pipe->condvar);
Expand Down Expand Up @@ -82,6 +83,10 @@ void modem_pipe_attach(struct modem_pipe *pipe, modem_pipe_api_callback callback
pipe->callback(pipe, MODEM_PIPE_EVENT_RECEIVE_READY, pipe->user_data);
}

if (pipe->transmit_idle_pending && (pipe->callback != NULL)) {
pipe->callback(pipe, MODEM_PIPE_EVENT_TRANSMIT_IDLE, pipe->user_data);
}

k_mutex_unlock(&pipe->lock);
}

Expand All @@ -97,6 +102,7 @@ int modem_pipe_transmit(struct modem_pipe *pipe, const uint8_t *buf, size_t size
}

ret = pipe->api->transmit(pipe->data, buf, size);
pipe->transmit_idle_pending = false;
k_mutex_unlock(&pipe->lock);
return ret;
}
Expand Down Expand Up @@ -179,6 +185,7 @@ void modem_pipe_notify_opened(struct modem_pipe *pipe)

if (pipe->callback != NULL) {
pipe->callback(pipe, MODEM_PIPE_EVENT_OPENED, pipe->user_data);
pipe->callback(pipe, MODEM_PIPE_EVENT_TRANSMIT_IDLE, pipe->user_data);
}

k_condvar_signal(&pipe->condvar);
Expand All @@ -190,6 +197,7 @@ void modem_pipe_notify_closed(struct modem_pipe *pipe)
k_mutex_lock(&pipe->lock, K_FOREVER);
pipe->state = MODEM_PIPE_STATE_CLOSED;
pipe->receive_ready_pending = false;
pipe->transmit_idle_pending = true;

if (pipe->callback != NULL) {
pipe->callback(pipe, MODEM_PIPE_EVENT_CLOSED, pipe->user_data);
Expand All @@ -211,3 +219,16 @@ void modem_pipe_notify_receive_ready(struct modem_pipe *pipe)

k_mutex_unlock(&pipe->lock);
}

void modem_pipe_notify_transmit_idle(struct modem_pipe *pipe)
{
k_mutex_lock(&pipe->lock, K_FOREVER);

pipe->transmit_idle_pending = true;

if (pipe->callback != NULL) {
pipe->callback(pipe, MODEM_PIPE_EVENT_TRANSMIT_IDLE, pipe->user_data);
}

k_mutex_unlock(&pipe->lock);
}

0 comments on commit 516af3c

Please sign in to comment.