Skip to content

Commit 2534141

Browse files
joerchankartben
authored andcommitted
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>
1 parent 995440b commit 2534141

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

drivers/modem/modem_cmd_handler.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,20 @@ int modem_cmd_handler_update_cmds(struct modem_cmd_handler_data *data,
486486
return 0;
487487
}
488488

489+
int modem_cmd_handler_await(struct modem_cmd_handler_data *data,
490+
struct k_sem *sem, k_timeout_t timeout)
491+
{
492+
int ret = k_sem_take(sem, timeout);
493+
494+
if (ret == 0) {
495+
ret = modem_cmd_handler_get_error(data);
496+
} else if (ret == -EAGAIN) {
497+
ret = -ETIMEDOUT;
498+
}
499+
500+
return ret;
501+
}
502+
489503
int modem_cmd_send_data_nolock(struct modem_iface *iface,
490504
const uint8_t *buf, size_t len)
491505
{

drivers/modem/modem_cmd_handler.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ int modem_cmd_handler_update_cmds(struct modem_cmd_handler_data *data,
158158
size_t handler_cmds_len,
159159
bool reset_error_flag);
160160

161+
/**
162+
* @brief Wait until semaphore is given
163+
*
164+
* This function does the same wait behavior as @ref modem_cmd_send, but can wait without sending
165+
* any command first. Useful for waiting for asynchronous responses.
166+
*
167+
* @param data: handler data to use
168+
* @param sem: wait for semaphore.
169+
* @param timeout: wait timeout.
170+
*
171+
* @retval 0 if ok, < 0 if error.
172+
*/
173+
int modem_cmd_handler_await(struct modem_cmd_handler_data *data,
174+
struct k_sem *sem, k_timeout_t timeout);
175+
161176
/**
162177
* @brief send data directly to interface w/o TX lock.
163178
*

0 commit comments

Comments
 (0)