drivers: modem: Add function to wait for sem and handle error

Add a helper function that waits for a semaphore and handles the
error conditions that may arise.

Signed-off-by: Joakim Andersson <joerchan@gmail.com>
This commit is contained in:
Joakim Andersson 2025-02-18 10:30:21 +01:00 committed by Benjamin Cabé
parent 995440b706
commit 25341410a7
2 changed files with 29 additions and 0 deletions

View File

@ -486,6 +486,20 @@ int modem_cmd_handler_update_cmds(struct modem_cmd_handler_data *data,
return 0;
}
int modem_cmd_handler_await(struct modem_cmd_handler_data *data,
struct k_sem *sem, k_timeout_t timeout)
{
int ret = k_sem_take(sem, timeout);
if (ret == 0) {
ret = modem_cmd_handler_get_error(data);
} else if (ret == -EAGAIN) {
ret = -ETIMEDOUT;
}
return ret;
}
int modem_cmd_send_data_nolock(struct modem_iface *iface,
const uint8_t *buf, size_t len)
{

View File

@ -158,6 +158,21 @@ int modem_cmd_handler_update_cmds(struct modem_cmd_handler_data *data,
size_t handler_cmds_len,
bool reset_error_flag);
/**
* @brief Wait until semaphore is given
*
* This function does the same wait behavior as @ref modem_cmd_send, but can wait without sending
* any command first. Useful for waiting for asynchronous responses.
*
* @param data: handler data to use
* @param sem: wait for semaphore.
* @param timeout: wait timeout.
*
* @retval 0 if ok, < 0 if error.
*/
int modem_cmd_handler_await(struct modem_cmd_handler_data *data,
struct k_sem *sem, k_timeout_t timeout);
/**
* @brief send data directly to interface w/o TX lock.
*