Skip to content

Commit 372c718

Browse files
bjarki-andreasencarlescufi
authored andcommitted
modem: pipe: Add explicit timeout to sync APIs
The modem pipe APIs include synchronous calls to open/close, which internally use a fixed timeout of 10 seconds. The timeout should be configurable through the APIs, anywhere from K_NO_WAIT to K_FOREVER. This commit adds timeout parameters to the open/close APIs, and updates in-tree usage of the open/close APIs to explicitly provide the previously implicit timeout of 10 seconds. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent def7f37 commit 372c718

File tree

13 files changed

+69
-61
lines changed

13 files changed

+69
-61
lines changed

drivers/gnss/gnss_luatos_air530z.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,21 @@ static int gnss_luatos_air530z_init(const struct device *dev)
188188

189189
luatos_air530z_init_dynamic_script(dev);
190190

191-
ret = modem_pipe_open(data->uart_pipe);
191+
ret = modem_pipe_open(data->uart_pipe, K_SECONDS(10));
192192
if (ret < 0) {
193193
return ret;
194194
}
195195

196196
ret = modem_chat_attach(&data->chat, data->uart_pipe);
197197
if (ret < 0) {
198-
modem_pipe_close(data->uart_pipe);
198+
modem_pipe_close(data->uart_pipe, K_SECONDS(10));
199199
return ret;
200200
}
201201

202202
ret = modem_chat_run_script(&data->chat, &init_script);
203203
if (ret < 0) {
204204
LOG_ERR("Failed to run init_script");
205-
modem_pipe_close(data->uart_pipe);
205+
modem_pipe_close(data->uart_pipe, K_SECONDS(10));
206206
return ret;
207207
}
208208

@@ -222,20 +222,20 @@ static int luatos_air530z_pm_resume(const struct device *dev)
222222
struct gnss_luatos_air530z_data *data = dev->data;
223223
int ret;
224224

225-
ret = modem_pipe_open(data->uart_pipe);
225+
ret = modem_pipe_open(data->uart_pipe, K_SECONDS(10));
226226
if (ret < 0) {
227227
return ret;
228228
}
229229

230230
ret = modem_chat_attach(&data->chat, data->uart_pipe);
231231
if (ret < 0) {
232-
modem_pipe_close(data->uart_pipe);
232+
modem_pipe_close(data->uart_pipe, K_SECONDS(10));
233233
return ret;
234234
}
235235

236236
ret = modem_chat_run_script(&data->chat, &init_script);
237237
if (ret < 0) {
238-
modem_pipe_close(data->uart_pipe);
238+
modem_pipe_close(data->uart_pipe, K_SECONDS(10));
239239
return ret;
240240
}
241241

@@ -251,7 +251,7 @@ static int luatos_air530z_pm_action(const struct device *dev, enum pm_device_act
251251
switch (action) {
252252
case PM_DEVICE_ACTION_SUSPEND:
253253
gpio_pin_set_dt(&config->on_off_gpio, 0);
254-
ret = modem_pipe_close(data->uart_pipe);
254+
ret = modem_pipe_close(data->uart_pipe, K_SECONDS(10));
255255
break;
256256

257257
case PM_DEVICE_ACTION_RESUME:

drivers/gnss/gnss_nmea_generic.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static int gnss_nmea_generic_resume(const struct device *dev)
6464
struct gnss_nmea_generic_data *data = dev->data;
6565
int ret;
6666

67-
ret = modem_pipe_open(data->uart_pipe);
67+
ret = modem_pipe_open(data->uart_pipe, K_SECONDS(10));
6868
if (ret < 0) {
6969
return ret;
7070
}
@@ -76,7 +76,7 @@ static int gnss_nmea_generic_resume(const struct device *dev)
7676
}
7777

7878
if (ret < 0) {
79-
modem_pipe_close(data->uart_pipe);
79+
modem_pipe_close(data->uart_pipe, K_SECONDS(10));
8080
}
8181
return ret;
8282
}

drivers/gnss/gnss_quectel_lcx6g.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static int quectel_lcx6g_resume(const struct device *dev)
205205

206206
quectel_lcx6g_await_pm_ready(dev);
207207

208-
ret = modem_pipe_open(data->uart_pipe);
208+
ret = modem_pipe_open(data->uart_pipe, K_SECONDS(10));
209209
if (ret < 0) {
210210
LOG_ERR("Failed to open pipe");
211211
return ret;
@@ -214,21 +214,21 @@ static int quectel_lcx6g_resume(const struct device *dev)
214214
ret = modem_chat_attach(&data->chat, data->uart_pipe);
215215
if (ret < 0) {
216216
LOG_ERR("Failed to attach chat");
217-
modem_pipe_close(data->uart_pipe);
217+
modem_pipe_close(data->uart_pipe, K_SECONDS(10));
218218
return ret;
219219
}
220220

221221
ret = modem_chat_run_script(&data->chat, &resume_script);
222222
if (ret < 0) {
223223
LOG_ERR("Failed to initialize GNSS");
224-
modem_pipe_close(data->uart_pipe);
224+
modem_pipe_close(data->uart_pipe, K_SECONDS(10));
225225
return ret;
226226
}
227227

228228
ret = quectel_lcx6g_configure_pps(dev);
229229
if (ret < 0) {
230230
LOG_ERR("Failed to configure PPS");
231-
modem_pipe_close(data->uart_pipe);
231+
modem_pipe_close(data->uart_pipe, K_SECONDS(10));
232232
return ret;
233233
}
234234

@@ -253,7 +253,7 @@ static int quectel_lcx6g_suspend(const struct device *dev)
253253
LOG_INF("Suspended");
254254
}
255255

256-
modem_pipe_close(data->uart_pipe);
256+
modem_pipe_close(data->uart_pipe, K_SECONDS(10));
257257
return ret;
258258
}
259259

@@ -268,7 +268,7 @@ static int quectel_lcx6g_turn_off(const struct device *dev)
268268

269269
LOG_INF("Powered off");
270270

271-
return modem_pipe_close(data->uart_pipe);
271+
return modem_pipe_close(data->uart_pipe, K_SECONDS(10));
272272
}
273273

274274
static int quectel_lcx6g_pm_action(const struct device *dev, enum pm_device_action action)

drivers/gnss/gnss_u_blox_m10.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ static int ubx_m10_resume(const struct device *dev)
9292
struct ubx_m10_data *data = dev->data;
9393
int ret;
9494

95-
ret = modem_pipe_open(data->uart_pipe);
95+
ret = modem_pipe_open(data->uart_pipe, K_SECONDS(10));
9696
if (ret < 0) {
9797
return ret;
9898
}
9999

100100
ret = modem_chat_attach(&data->chat, data->uart_pipe);
101101
if (ret < 0) {
102-
(void)modem_pipe_close(data->uart_pipe);
102+
(void)modem_pipe_close(data->uart_pipe, K_SECONDS(10));
103103
return ret;
104104
}
105105

@@ -110,7 +110,7 @@ static int ubx_m10_turn_off(const struct device *dev)
110110
{
111111
struct ubx_m10_data *data = dev->data;
112112

113-
return modem_pipe_close(data->uart_pipe);
113+
return modem_pipe_close(data->uart_pipe, K_SECONDS(10));
114114
}
115115

116116
static int ubx_m10_init_nmea0183_match(const struct device *dev)
@@ -203,7 +203,7 @@ static int ubx_m10_modem_module_change(const struct device *dev, bool change_fro
203203
}
204204

205205
if (ret < 0) {
206-
(void)modem_pipe_close(data->uart_pipe);
206+
(void)modem_pipe_close(data->uart_pipe, K_SECONDS(10));
207207
}
208208

209209
return ret;

include/zephyr/modem/pipe.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ void modem_pipe_init(struct modem_pipe *pipe, void *data, const struct modem_pip
8787
* @brief Open pipe
8888
*
8989
* @param pipe Pipe instance
90+
* @param timeout Timeout waiting for pipe to open
9091
*
9192
* @retval 0 if pipe was successfully opened or was already open
9293
* @retval -errno code otherwise
@@ -95,7 +96,7 @@ void modem_pipe_init(struct modem_pipe *pipe, void *data, const struct modem_pip
9596
* It may block the calling thread, which in the case of the system workqueue
9697
* can result in a deadlock until this call times out waiting for the pipe to be open.
9798
*/
98-
int modem_pipe_open(struct modem_pipe *pipe);
99+
int modem_pipe_open(struct modem_pipe *pipe, k_timeout_t timeout);
99100

100101
/**
101102
* @brief Open pipe asynchronously
@@ -163,6 +164,7 @@ void modem_pipe_release(struct modem_pipe *pipe);
163164
* @brief Close pipe
164165
*
165166
* @param pipe Pipe instance
167+
* @param timeout Timeout waiting for pipe to close
166168
*
167169
* @retval 0 if pipe open was called closed or pipe was already closed
168170
* @retval -errno code otherwise
@@ -171,7 +173,7 @@ void modem_pipe_release(struct modem_pipe *pipe);
171173
* It may block the calling thread, which in the case of the system workqueue
172174
* can result in a deadlock until this call times out waiting for the pipe to be closed.
173175
*/
174-
int modem_pipe_close(struct modem_pipe *pipe);
176+
int modem_pipe_close(struct modem_pipe *pipe, k_timeout_t timeout);
175177

176178
/**
177179
* @brief Close pipe asynchronously

subsys/modem/modem_pipe.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ static uint32_t pipe_test_events(struct modem_pipe *pipe, uint32_t events)
3535
return k_event_test(&pipe->event, events);
3636
}
3737

38-
static uint32_t pipe_await_events(struct modem_pipe *pipe, uint32_t events)
38+
static uint32_t pipe_await_events(struct modem_pipe *pipe,
39+
uint32_t events,
40+
k_timeout_t timeout)
3941
{
40-
return k_event_wait(&pipe->event, events, false, K_MSEC(10000));
42+
return k_event_wait(&pipe->event, events, false, timeout);
4143
}
4244

4345
static void pipe_post_events(struct modem_pipe *pipe, uint32_t events)
@@ -88,7 +90,7 @@ void modem_pipe_init(struct modem_pipe *pipe, void *data, const struct modem_pip
8890
k_event_init(&pipe->event);
8991
}
9092

91-
int modem_pipe_open(struct modem_pipe *pipe)
93+
int modem_pipe_open(struct modem_pipe *pipe, k_timeout_t timeout)
9294
{
9395
int ret;
9496

@@ -101,7 +103,7 @@ int modem_pipe_open(struct modem_pipe *pipe)
101103
return ret;
102104
}
103105

104-
if (!pipe_await_events(pipe, PIPE_EVENT_OPENED_BIT)) {
106+
if (!pipe_await_events(pipe, PIPE_EVENT_OPENED_BIT, timeout)) {
105107
return -EAGAIN;
106108
}
107109

@@ -156,7 +158,7 @@ void modem_pipe_release(struct modem_pipe *pipe)
156158
pipe_set_callback(pipe, NULL, NULL);
157159
}
158160

159-
int modem_pipe_close(struct modem_pipe *pipe)
161+
int modem_pipe_close(struct modem_pipe *pipe, k_timeout_t timeout)
160162
{
161163
int ret;
162164

@@ -169,7 +171,7 @@ int modem_pipe_close(struct modem_pipe *pipe)
169171
return ret;
170172
}
171173

172-
if (!pipe_await_events(pipe, PIPE_EVENT_CLOSED_BIT)) {
174+
if (!pipe_await_events(pipe, PIPE_EVENT_CLOSED_BIT, timeout)) {
173175
return -EAGAIN;
174176
}
175177

tests/subsys/modem/backends/tty/src/main.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static void *test_modem_backend_tty_setup(void)
108108

109109
tty_pipe = modem_backend_tty_init(&tty_backend, &config);
110110
modem_pipe_attach(tty_pipe, modem_pipe_callback_handler, NULL);
111-
__ASSERT_NO_MSG(modem_pipe_open(tty_pipe) == 0);
111+
__ASSERT_NO_MSG(modem_pipe_open(tty_pipe, K_SECONDS(10)) == 0);
112112
return NULL;
113113
}
114114

@@ -119,7 +119,7 @@ static void test_modem_backend_tty_before(void *f)
119119

120120
static void test_modem_backend_tty_teardown(void *f)
121121
{
122-
modem_pipe_close(tty_pipe);
122+
modem_pipe_close(tty_pipe, K_SECONDS(10));
123123
}
124124

125125
/*************************************************************************************************/
@@ -129,12 +129,12 @@ ZTEST(modem_backend_tty_suite, test_close_open)
129129
{
130130
bool result;
131131

132-
zassert_ok(modem_pipe_close(tty_pipe), "Failed to close pipe");
133-
zassert_ok(modem_pipe_close(tty_pipe), "Pipe should already be closed");
134-
zassert_ok(modem_pipe_open(tty_pipe), "Failed to open pipe");
132+
zassert_ok(modem_pipe_close(tty_pipe, K_SECONDS(10)), "Failed to close pipe");
133+
zassert_ok(modem_pipe_close(tty_pipe, K_SECONDS(10)), "Pipe should already be closed");
134+
zassert_ok(modem_pipe_open(tty_pipe, K_SECONDS(10)), "Failed to open pipe");
135135
result = atomic_test_bit(&tty_pipe_events, TEST_MODEM_BACKEND_TTY_PIPE_EVENT_TIDLE_BIT);
136136
zassert_true(result, "Transmit idle event should be set");
137-
zassert_ok(modem_pipe_open(tty_pipe), "Pipe should already be open");
137+
zassert_ok(modem_pipe_open(tty_pipe, K_SECONDS(10)), "Pipe should already be open");
138138
}
139139

140140
ZTEST(modem_backend_tty_suite, test_receive_ready_event_not_raised)

tests/subsys/modem/backends/uart/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ static void test_modem_backend_uart_before(void *f)
172172
prng_reset();
173173
ring_buf_reset(&transmit_ring_buf);
174174
k_sem_reset(&receive_ready_sem);
175-
__ASSERT_NO_MSG(modem_pipe_open(pipe) == 0);
175+
__ASSERT_NO_MSG(modem_pipe_open(pipe, K_SECONDS(10)) == 0);
176176
}
177177

178178
static void test_modem_backend_uart_after(void *f)
179179
{
180-
__ASSERT_NO_MSG(modem_pipe_close(pipe) == 0);
180+
__ASSERT_NO_MSG(modem_pipe_close(pipe, K_SECONDS(10)) == 0);
181181
}
182182

183183
/*************************************************************************************************/

tests/subsys/modem/modem_chat/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ static void *test_modem_chat_setup(void)
280280
};
281281

282282
mock_pipe = modem_backend_mock_init(&mock, &mock_config);
283-
zassert(modem_pipe_open(mock_pipe) == 0, "Failed to open mock pipe");
283+
zassert(modem_pipe_open(mock_pipe, K_SECONDS(10)) == 0, "Failed to open mock pipe");
284284
zassert(modem_chat_attach(&cmd, mock_pipe) == 0, "Failed to attach pipe mock to modem CMD");
285285
return NULL;
286286
}

tests/subsys/modem/modem_cmux/src/main.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ static void *test_modem_cmux_setup(void)
283283
};
284284

285285
bus_mock_pipe = modem_backend_mock_init(&bus_mock, &bus_mock_config);
286-
__ASSERT_NO_MSG(modem_pipe_open(bus_mock_pipe) == 0);
286+
__ASSERT_NO_MSG(modem_pipe_open(bus_mock_pipe, K_SECONDS(10)) == 0);
287287

288288
/* Connect CMUX */
289289
__ASSERT_NO_MSG(modem_cmux_attach(&cmux, bus_mock_pipe) == 0);
@@ -731,9 +731,9 @@ ZTEST(modem_cmux, test_modem_cmux_disconnect_connect)
731731
ZTEST(modem_cmux, test_modem_cmux_disconnect_connect_sync)
732732
{
733733
modem_backend_mock_prime(&bus_mock, &transaction_dlci1_disc);
734-
zassert_true(modem_pipe_close(dlci1_pipe) == 0, "Failed to close DLCI1");
734+
zassert_true(modem_pipe_close(dlci1_pipe, K_SECONDS(10)) == 0, "Failed to close DLCI1");
735735
modem_backend_mock_prime(&bus_mock, &transaction_dlci2_disc);
736-
zassert_true(modem_pipe_close(dlci2_pipe) == 0, "Failed to close DLCI2");
736+
zassert_true(modem_pipe_close(dlci2_pipe, K_SECONDS(10)) == 0, "Failed to close DLCI2");
737737
modem_backend_mock_prime(&bus_mock, &transaction_control_cld);
738738
zassert_true(modem_cmux_disconnect(&cmux) == 0, "Failed to disconnect CMUX");
739739
zassert_true(modem_cmux_disconnect(&cmux) == -EALREADY,
@@ -745,21 +745,25 @@ ZTEST(modem_cmux, test_modem_cmux_disconnect_connect_sync)
745745
"Should already be connected");
746746

747747
modem_backend_mock_prime(&bus_mock, &transaction_dlci1_sabm);
748-
zassert_true(modem_pipe_open(dlci1_pipe) == 0, "Failed to open DLCI1 pipe");
748+
zassert_true(modem_pipe_open(dlci1_pipe, K_SECONDS(10)) == 0,
749+
"Failed to open DLCI1 pipe");
749750
modem_backend_mock_prime(&bus_mock, &transaction_dlci2_sabm);
750-
zassert_true(modem_pipe_open(dlci2_pipe) == 0, "Failed to open DLCI2 pipe");
751+
zassert_true(modem_pipe_open(dlci2_pipe, K_SECONDS(10)) == 0,
752+
"Failed to open DLCI2 pipe");
751753
}
752754

753755
ZTEST(modem_cmux, test_modem_cmux_dlci_close_open_sync)
754756
{
755757
modem_backend_mock_prime(&bus_mock, &transaction_dlci1_disc);
756-
zassert_true(modem_pipe_close(dlci1_pipe) == 0, "Failed to close DLCI1");
758+
zassert_true(modem_pipe_close(dlci1_pipe, K_SECONDS(10)) == 0, "Failed to close DLCI1");
757759
modem_backend_mock_prime(&bus_mock, &transaction_dlci2_disc);
758-
zassert_true(modem_pipe_close(dlci2_pipe) == 0, "Failed to close DLCI2");
760+
zassert_true(modem_pipe_close(dlci2_pipe, K_SECONDS(10)) == 0, "Failed to close DLCI2");
759761
modem_backend_mock_prime(&bus_mock, &transaction_dlci1_sabm);
760-
zassert_true(modem_pipe_open(dlci1_pipe) == 0, "Failed to open DLCI1 pipe");
762+
zassert_true(modem_pipe_open(dlci1_pipe, K_SECONDS(10)) == 0,
763+
"Failed to open DLCI1 pipe");
761764
modem_backend_mock_prime(&bus_mock, &transaction_dlci2_sabm);
762-
zassert_true(modem_pipe_open(dlci2_pipe) == 0, "Failed to open DLCI2 pipe");
765+
zassert_true(modem_pipe_open(dlci2_pipe, K_SECONDS(10)) == 0,
766+
"Failed to open DLCI2 pipe");
763767
}
764768

765769
ZTEST(modem_cmux, test_modem_cmux_prevent_work_while_released)

0 commit comments

Comments
 (0)