1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/kernel.h>
9 #include <string.h>
10
11 #include <zephyr/modem/cmux.h>
12 #include <modem_backend_mock.h>
13
14 /* CMUX state flags */
15 #define EVENT_CMUX_CONNECTED BIT(0)
16 #define EVENT_CMUX_DLCI1_OPEN BIT(1)
17 #define EVENT_CMUX_DLCI2_OPEN BIT(2)
18 #define EVENT_CMUX_DLCI1_CLOSED BIT(3)
19 #define EVENT_CMUX_DLCI2_CLOSED BIT(4)
20 #define EVENT_CMUX_DISCONNECTED BIT(5)
21 #define EVENT_CMUX_DLCI1_RX_DATA BIT(6)
22 #define EVENT_CMUX_DLCI2_RX_DATA BIT(7)
23
24 /* CMUX DTE variables */
25 static struct modem_cmux cmux_dte;
26 static uint8_t cmux_receive_buf[127];
27 static uint8_t cmux_transmit_buf[149];
28 static struct modem_cmux_dlci dlci1;
29 static struct modem_cmux_dlci dlci2;
30 static struct modem_pipe *dlci1_pipe;
31 static struct modem_pipe *dlci2_pipe;
32
33 /* CMUX DCE variables */
34 static struct modem_cmux cmux_dce;
35 static uint8_t cmux_receive_buf_dce[127];
36 static uint8_t cmux_transmit_buf_dce[149];
37 static struct modem_cmux_dlci dlci1_dce;
38 static struct modem_cmux_dlci dlci2_dce;
39 static struct modem_pipe *dlci1_pipe_dce;
40 static struct modem_pipe *dlci2_pipe_dce;
41
42 /* DTE & DCE Event */
43 static struct k_event cmux_event_dte;
44 static struct k_event cmux_event_dce;
45
46 /* Backend MOCK */
47 static struct modem_backend_mock bus_mock_dte;
48 static struct modem_backend_mock bus_mock_dce;
49 static uint8_t bus_mock_rx_buf[2048];
50 static uint8_t bus_mock_tx_buf[2048];
51 static uint8_t bus_mock_rx_buf_dce[2048];
52 static uint8_t bus_mock_tx_buf_dce[2048];
53 static struct modem_pipe *bus_mock_pipe;
54 static struct modem_pipe *bus_mock_pipe_dce;
55
56 static uint8_t dlci1_receive_buf[127];
57 static uint8_t dlci2_receive_buf[127];
58 static uint8_t dlci1_receive_buf_dce[127];
59 static uint8_t dlci2_receive_buf_dce[127];
60
61 static uint8_t buffer1[2048];
62 static uint8_t buffer2[2048];
63
test_dlci1_pipe_cb(struct modem_pipe * pipe,enum modem_pipe_event event,void * user_data)64 static void test_dlci1_pipe_cb(struct modem_pipe *pipe, enum modem_pipe_event event,
65 void *user_data)
66 {
67 switch (event) {
68 case MODEM_PIPE_EVENT_OPENED:
69 k_event_post(&cmux_event_dte, EVENT_CMUX_DLCI1_OPEN);
70 break;
71
72 case MODEM_PIPE_EVENT_CLOSED:
73 k_event_post(&cmux_event_dte, EVENT_CMUX_DLCI1_CLOSED);
74 break;
75
76 case MODEM_PIPE_EVENT_RECEIVE_READY:
77 k_event_post(&cmux_event_dte, EVENT_CMUX_DLCI1_RX_DATA);
78 break;
79
80 default:
81 break;
82 }
83 }
84
test_dlci2_pipe_cb(struct modem_pipe * pipe,enum modem_pipe_event event,void * user_data)85 static void test_dlci2_pipe_cb(struct modem_pipe *pipe, enum modem_pipe_event event,
86 void *user_data)
87 {
88 switch (event) {
89 case MODEM_PIPE_EVENT_OPENED:
90 k_event_post(&cmux_event_dte, EVENT_CMUX_DLCI2_OPEN);
91 break;
92
93 case MODEM_PIPE_EVENT_CLOSED:
94 k_event_post(&cmux_event_dte, EVENT_CMUX_DLCI2_CLOSED);
95 break;
96 case MODEM_PIPE_EVENT_RECEIVE_READY:
97 k_event_post(&cmux_event_dte, EVENT_CMUX_DLCI2_RX_DATA);
98 break;
99
100 default:
101 break;
102 }
103 }
104
test_dlci1_pipe_cb_dce(struct modem_pipe * pipe,enum modem_pipe_event event,void * user_data)105 static void test_dlci1_pipe_cb_dce(struct modem_pipe *pipe, enum modem_pipe_event event,
106 void *user_data)
107 {
108 switch (event) {
109 case MODEM_PIPE_EVENT_OPENED:
110 k_event_post(&cmux_event_dce, EVENT_CMUX_DLCI1_OPEN);
111 break;
112
113 case MODEM_PIPE_EVENT_CLOSED:
114 k_event_post(&cmux_event_dce, EVENT_CMUX_DLCI1_CLOSED);
115 break;
116
117 case MODEM_PIPE_EVENT_RECEIVE_READY:
118 k_event_post(&cmux_event_dce, EVENT_CMUX_DLCI1_RX_DATA);
119 break;
120
121 default:
122 break;
123 }
124 }
125
test_dlci2_pipe_cb_dce(struct modem_pipe * pipe,enum modem_pipe_event event,void * user_data)126 static void test_dlci2_pipe_cb_dce(struct modem_pipe *pipe, enum modem_pipe_event event,
127 void *user_data)
128 {
129 switch (event) {
130 case MODEM_PIPE_EVENT_OPENED:
131 k_event_post(&cmux_event_dce, EVENT_CMUX_DLCI2_OPEN);
132 break;
133
134 case MODEM_PIPE_EVENT_CLOSED:
135 k_event_post(&cmux_event_dce, EVENT_CMUX_DLCI2_CLOSED);
136 break;
137 case MODEM_PIPE_EVENT_RECEIVE_READY:
138 k_event_post(&cmux_event_dce, EVENT_CMUX_DLCI2_RX_DATA);
139 break;
140
141 default:
142 break;
143 }
144 }
145
146 /*************************************************************************************************/
147 /* DLCI2 AT CMUX frames */
148 /*************************************************************************************************/
149 static uint8_t cmux_frame_data_dlci2_at_cgdcont[] = {
150 0x41, 0x54, 0x2B, 0x43, 0x47, 0x44, 0x43, 0x4F, 0x4E, 0x54, 0x3D,
151 0x31, 0x2C, 0x22, 0x49, 0x50, 0x22, 0x2C, 0x22, 0x74, 0x72, 0x61,
152 0x63, 0x6B, 0x75, 0x6E, 0x69, 0x74, 0x2E, 0x6D, 0x32, 0x6D, 0x22};
153
154 static uint8_t cmux_frame_data_dlci2_at_newline[] = {0x0D, 0x0A};
155
156 /*************************************************************************************************/
157 /* DLCI1 AT CMUX frames */
158 /*************************************************************************************************/
159 static uint8_t cmux_frame_data_dlci1_at_at[] = {0x41, 0x54};
160
161 static uint8_t cmux_frame_data_dlci1_at_newline[] = {0x0D, 0x0A};
162
163 /*************************************************************************************************/
164 /* DLCI2 PPP CMUX frames */
165 /*************************************************************************************************/
166 static uint8_t cmux_frame_data_dlci2_ppp_52[] = {
167 0x7E, 0xFF, 0x7D, 0x23, 0xC0, 0x21, 0x7D, 0x21, 0x7D, 0x20, 0x7D, 0x20, 0x7D,
168 0x38, 0x7D, 0x22, 0x7D, 0x26, 0x7D, 0x20, 0x7D, 0x20, 0x7D, 0x20, 0x7D, 0x20,
169 0x7D, 0x23, 0x7D, 0x24, 0xC0, 0x23, 0x7D, 0x25, 0x7D, 0x26, 0x53, 0x96, 0x7D,
170 0x38, 0xAA, 0x7D, 0x27, 0x7D, 0x22, 0x7D, 0x28, 0x7D, 0x22, 0xD5, 0xA8, 0x7E};
171
172 static uint8_t cmux_frame_data_dlci2_ppp_18[] = {0x7E, 0xFF, 0x7D, 0x23, 0xC0, 0x21,
173 0x7D, 0x22, 0x7D, 0x21, 0x7D, 0x20,
174 0x7D, 0x24, 0x7D, 0x3C, 0x90, 0x7E};
175
test_cmux_ctrl_cb(struct modem_cmux * cmux,enum modem_cmux_event event,void * user_data)176 static void test_cmux_ctrl_cb(struct modem_cmux *cmux, enum modem_cmux_event event, void *user_data)
177 {
178 if (event == MODEM_CMUX_EVENT_CONNECTED) {
179 k_event_post(&cmux_event_dte, EVENT_CMUX_CONNECTED);
180 return;
181 }
182
183 if (event == MODEM_CMUX_EVENT_DISCONNECTED) {
184 k_event_post(&cmux_event_dte, EVENT_CMUX_DISCONNECTED);
185 return;
186 }
187 }
188
test_cmux_ctrl_cb_dce(struct modem_cmux * cmux,enum modem_cmux_event event,void * user_data)189 static void test_cmux_ctrl_cb_dce(struct modem_cmux *cmux, enum modem_cmux_event event,
190 void *user_data)
191 {
192 if (event == MODEM_CMUX_EVENT_CONNECTED) {
193 k_event_post(&cmux_event_dce, EVENT_CMUX_CONNECTED);
194 return;
195 }
196
197 if (event == MODEM_CMUX_EVENT_DISCONNECTED) {
198 k_event_post(&cmux_event_dce, EVENT_CMUX_DISCONNECTED);
199 return;
200 }
201 }
202
cmux_dte_init(void)203 static void cmux_dte_init(void)
204 {
205 struct modem_cmux_dlci_config dlci1_config = {
206 .dlci_address = 1,
207 .receive_buf = dlci1_receive_buf,
208 .receive_buf_size = sizeof(dlci1_receive_buf),
209 };
210
211 struct modem_cmux_dlci_config dlci2_config = {
212 .dlci_address = 2,
213 .receive_buf = dlci2_receive_buf,
214 .receive_buf_size = sizeof(dlci2_receive_buf),
215 };
216
217 struct modem_cmux_config cmux_config = {
218 .callback = test_cmux_ctrl_cb,
219 .user_data = NULL,
220 .receive_buf = cmux_receive_buf,
221 .receive_buf_size = sizeof(cmux_receive_buf),
222 .transmit_buf = cmux_transmit_buf,
223 .transmit_buf_size = ARRAY_SIZE(cmux_transmit_buf),
224 };
225
226 const struct modem_backend_mock_config bus_mock_config = {
227 .rx_buf = bus_mock_rx_buf,
228 .rx_buf_size = sizeof(bus_mock_rx_buf),
229 .tx_buf = bus_mock_tx_buf,
230 .tx_buf_size = sizeof(bus_mock_tx_buf),
231 .limit = 32,
232 };
233
234 modem_cmux_init(&cmux_dte, &cmux_config);
235 dlci1_pipe = modem_cmux_dlci_init(&cmux_dte, &dlci1, &dlci1_config);
236 dlci2_pipe = modem_cmux_dlci_init(&cmux_dte, &dlci2, &dlci2_config);
237 /* Init Backend DTE */
238 bus_mock_pipe = modem_backend_mock_init(&bus_mock_dte, &bus_mock_config);
239 __ASSERT_NO_MSG(modem_pipe_open(bus_mock_pipe, K_SECONDS(10)) == 0);
240 __ASSERT_NO_MSG(modem_cmux_attach(&cmux_dte, bus_mock_pipe) == 0);
241 modem_pipe_attach(dlci1_pipe, test_dlci1_pipe_cb, NULL);
242 modem_pipe_attach(dlci2_pipe, test_dlci2_pipe_cb, NULL);
243 }
244
cmux_dce_init(void)245 static void cmux_dce_init(void)
246 {
247 struct modem_cmux_dlci_config dlci1_config = {
248 .dlci_address = 1,
249 .receive_buf = dlci1_receive_buf_dce,
250 .receive_buf_size = sizeof(dlci1_receive_buf_dce),
251 };
252
253 struct modem_cmux_dlci_config dlci2_config = {
254 .dlci_address = 2,
255 .receive_buf = dlci2_receive_buf_dce,
256 .receive_buf_size = sizeof(dlci2_receive_buf_dce),
257 };
258
259 struct modem_cmux_config cmux_config_dce = {
260 .callback = test_cmux_ctrl_cb_dce,
261 .user_data = NULL,
262 .receive_buf = cmux_receive_buf_dce,
263 .receive_buf_size = sizeof(cmux_receive_buf_dce),
264 .transmit_buf = cmux_transmit_buf_dce,
265 .transmit_buf_size = ARRAY_SIZE(cmux_transmit_buf_dce),
266 };
267
268 const struct modem_backend_mock_config bus_mock_config = {
269 .rx_buf = bus_mock_rx_buf_dce,
270 .rx_buf_size = sizeof(bus_mock_rx_buf_dce),
271 .tx_buf = bus_mock_tx_buf_dce,
272 .tx_buf_size = sizeof(bus_mock_tx_buf_dce),
273 .limit = 32,
274 };
275
276 modem_cmux_init(&cmux_dce, &cmux_config_dce);
277 dlci1_pipe_dce = modem_cmux_dlci_init(&cmux_dce, &dlci1_dce, &dlci1_config);
278 dlci2_pipe_dce = modem_cmux_dlci_init(&cmux_dce, &dlci2_dce, &dlci2_config);
279 /* Init Backend DCE */
280 bus_mock_pipe_dce = modem_backend_mock_init(&bus_mock_dce, &bus_mock_config);
281 __ASSERT_NO_MSG(modem_pipe_open(bus_mock_pipe_dce, K_SECONDS(10)) == 0);
282 __ASSERT_NO_MSG(modem_cmux_attach(&cmux_dce, bus_mock_pipe_dce) == 0);
283 modem_pipe_attach(dlci1_pipe_dce, test_dlci1_pipe_cb_dce, NULL);
284 modem_pipe_attach(dlci2_pipe_dce, test_dlci2_pipe_cb_dce, NULL);
285 }
286
test_setup(void)287 static void *test_setup(void)
288 {
289 uint32_t events;
290
291 /* Init Event mask's */
292 k_event_init(&cmux_event_dte);
293 k_event_init(&cmux_event_dce);
294
295 /* Init CMUX, Pipe and Backend instances */
296 cmux_dte_init();
297 cmux_dce_init();
298
299 /* Create MOCK bridge */
300 modem_backend_mock_bridge(&bus_mock_dte, &bus_mock_dce);
301
302 /* Connect CMUX by DTE */
303 __ASSERT_NO_MSG(modem_cmux_connect_async(&cmux_dte) == 0);
304 events = k_event_wait(&cmux_event_dte, EVENT_CMUX_CONNECTED, false, K_MSEC(100));
305 __ASSERT_NO_MSG((events & EVENT_CMUX_CONNECTED));
306 events = k_event_wait(&cmux_event_dce, EVENT_CMUX_CONNECTED, false, K_MSEC(100));
307 __ASSERT_NO_MSG((events & EVENT_CMUX_CONNECTED));
308
309 /* Open DLCI channels init by DTE */
310 __ASSERT_NO_MSG(modem_pipe_open_async(dlci1_pipe) == 0);
311 events = k_event_wait(&cmux_event_dte, EVENT_CMUX_DLCI1_OPEN, false, K_MSEC(100));
312 __ASSERT_NO_MSG((events & EVENT_CMUX_DLCI1_OPEN));
313 events = k_event_wait(&cmux_event_dce, EVENT_CMUX_DLCI1_OPEN, false, K_MSEC(100));
314 __ASSERT_NO_MSG((events & EVENT_CMUX_DLCI1_OPEN));
315
316 __ASSERT_NO_MSG(modem_pipe_open_async(dlci2_pipe) == 0);
317 events = k_event_wait(&cmux_event_dte, EVENT_CMUX_DLCI2_OPEN, false, K_MSEC(100));
318 __ASSERT_NO_MSG((events & EVENT_CMUX_DLCI2_OPEN));
319 events = k_event_wait(&cmux_event_dce, EVENT_CMUX_DLCI2_OPEN, false, K_MSEC(100));
320 __ASSERT_NO_MSG((events & EVENT_CMUX_DLCI2_OPEN));
321
322 return NULL;
323 }
324
test_before(void * f)325 static void test_before(void *f)
326 {
327 /* Reset events */
328 k_event_clear(&cmux_event_dte, UINT32_MAX);
329 k_event_clear(&cmux_event_dce, UINT32_MAX);
330
331 /* Reset mock pipes */
332 modem_backend_mock_reset(&bus_mock_dte);
333 modem_backend_mock_reset(&bus_mock_dce);
334 }
335
ZTEST(modem_cmux_pair,test_modem_cmux_dce_receive_dlci2_at)336 ZTEST(modem_cmux_pair, test_modem_cmux_dce_receive_dlci2_at)
337 {
338 int ret;
339 uint32_t events;
340
341 modem_pipe_transmit(dlci2_pipe, cmux_frame_data_dlci2_at_cgdcont,
342 sizeof(cmux_frame_data_dlci2_at_cgdcont));
343
344 modem_pipe_transmit(dlci2_pipe, cmux_frame_data_dlci2_at_newline,
345 sizeof(cmux_frame_data_dlci2_at_newline));
346
347 k_msleep(100);
348
349 events = k_event_wait(&cmux_event_dce, EVENT_CMUX_DLCI2_RX_DATA, false, K_MSEC(100));
350 zassert_true((events & EVENT_CMUX_DLCI2_RX_DATA), "DLCI2 dce not rx data");
351
352 ret = modem_pipe_receive(dlci2_pipe_dce, buffer2, sizeof(buffer2));
353 zassert_true(ret == (sizeof(cmux_frame_data_dlci2_at_cgdcont) +
354 sizeof(cmux_frame_data_dlci2_at_newline)),
355 "Incorrect number of bytes received");
356
357 zassert_true(memcmp(buffer2, cmux_frame_data_dlci2_at_cgdcont,
358 sizeof(cmux_frame_data_dlci2_at_cgdcont)) == 0,
359 "Incorrect data received");
360
361 zassert_true(memcmp(&buffer2[sizeof(cmux_frame_data_dlci2_at_cgdcont)],
362 cmux_frame_data_dlci2_at_newline,
363 sizeof(cmux_frame_data_dlci2_at_newline)) == 0,
364 "Incorrect data received");
365 }
366
ZTEST(modem_cmux_pair,test_modem_cmux_dce_receive_dlci1_at)367 ZTEST(modem_cmux_pair, test_modem_cmux_dce_receive_dlci1_at)
368 {
369 int ret;
370 uint32_t events;
371
372 modem_pipe_transmit(dlci1_pipe, cmux_frame_data_dlci1_at_at,
373 sizeof(cmux_frame_data_dlci1_at_at));
374 modem_pipe_transmit(dlci1_pipe, cmux_frame_data_dlci1_at_newline,
375 sizeof(cmux_frame_data_dlci1_at_newline));
376
377 k_msleep(100);
378
379 events = k_event_wait(&cmux_event_dce, EVENT_CMUX_DLCI1_RX_DATA, false, K_MSEC(100));
380 zassert_true((events & EVENT_CMUX_DLCI1_RX_DATA), "DLCI1 dce not rx data");
381
382 ret = modem_pipe_receive(dlci1_pipe_dce, buffer1, sizeof(buffer1));
383 zassert_true(ret == (sizeof(cmux_frame_data_dlci1_at_at) +
384 sizeof(cmux_frame_data_dlci1_at_newline)),
385 "Incorrect number of bytes received");
386
387 zassert_true(memcmp(buffer1, cmux_frame_data_dlci1_at_at,
388 sizeof(cmux_frame_data_dlci1_at_at)) == 0,
389 "Incorrect data received");
390
391 zassert_true(memcmp(&buffer1[sizeof(cmux_frame_data_dlci1_at_at)],
392 cmux_frame_data_dlci1_at_newline,
393 sizeof(cmux_frame_data_dlci1_at_newline)) == 0,
394 "Incorrect data received");
395 }
396
ZTEST(modem_cmux_pair,test_modem_cmux_dce_receive_dlci2_ppp)397 ZTEST(modem_cmux_pair, test_modem_cmux_dce_receive_dlci2_ppp)
398 {
399 int ret;
400
401 uint32_t events;
402
403 modem_pipe_transmit(dlci2_pipe, cmux_frame_data_dlci2_ppp_52,
404 sizeof(cmux_frame_data_dlci2_ppp_52));
405 modem_pipe_transmit(dlci2_pipe, cmux_frame_data_dlci2_ppp_18,
406 sizeof(cmux_frame_data_dlci2_ppp_18));
407
408 k_msleep(100);
409
410 events = k_event_wait(&cmux_event_dce, EVENT_CMUX_DLCI2_RX_DATA, false, K_MSEC(100));
411 zassert_true((events & EVENT_CMUX_DLCI2_RX_DATA), "DLCI2 dce not rx data");
412
413 ret = modem_pipe_receive(dlci2_pipe_dce, buffer2, sizeof(buffer2));
414 zassert_true(ret == (sizeof(cmux_frame_data_dlci2_ppp_52) +
415 sizeof(cmux_frame_data_dlci2_ppp_18)),
416 "Incorrect number of bytes received");
417
418 zassert_true(memcmp(buffer2, cmux_frame_data_dlci2_ppp_52,
419 sizeof(cmux_frame_data_dlci2_ppp_52)) == 0,
420 "Incorrect data received");
421
422 zassert_true(memcmp(&buffer2[sizeof(cmux_frame_data_dlci2_ppp_52)],
423 cmux_frame_data_dlci2_ppp_18,
424 sizeof(cmux_frame_data_dlci2_ppp_18)) == 0,
425 "Incorrect data received");
426 }
427
ZTEST(modem_cmux_pair,test_modem_cmux_dce_transmit_dlci2_ppp)428 ZTEST(modem_cmux_pair, test_modem_cmux_dce_transmit_dlci2_ppp)
429 {
430 int ret;
431 uint32_t events;
432
433 ret = modem_pipe_transmit(dlci2_pipe_dce, cmux_frame_data_dlci2_ppp_52,
434 sizeof(cmux_frame_data_dlci2_ppp_52));
435
436 zassert_true(ret == sizeof(cmux_frame_data_dlci2_ppp_52), "Failed to send DLCI2 PPP 52 %d",
437 ret);
438 ret = modem_pipe_transmit(dlci2_pipe_dce, cmux_frame_data_dlci2_ppp_18,
439 sizeof(cmux_frame_data_dlci2_ppp_18));
440
441 zassert_true(ret == sizeof(cmux_frame_data_dlci2_ppp_18), "Failed to send DLCI2 PPP 18");
442
443 k_msleep(100);
444
445 events = k_event_wait(&cmux_event_dte, EVENT_CMUX_DLCI2_RX_DATA, false, K_MSEC(100));
446 zassert_true((events & EVENT_CMUX_DLCI2_RX_DATA), "DLCI2 dce not rx data");
447
448 ret = modem_pipe_receive(dlci2_pipe, buffer2, sizeof(buffer2));
449 zassert_true(ret == (sizeof(cmux_frame_data_dlci2_ppp_52) +
450 sizeof(cmux_frame_data_dlci2_ppp_18)),
451 "Incorrect number of bytes received");
452
453 zassert_true(memcmp(buffer2, cmux_frame_data_dlci2_ppp_52,
454 sizeof(cmux_frame_data_dlci2_ppp_52)) == 0,
455 "Incorrect data received");
456
457 zassert_true(memcmp(&buffer2[sizeof(cmux_frame_data_dlci2_ppp_52)],
458 cmux_frame_data_dlci2_ppp_18,
459 sizeof(cmux_frame_data_dlci2_ppp_18)) == 0,
460 "Incorrect data received");
461 }
462
ZTEST(modem_cmux_pair,test_modem_cmux_dlci1_close_open)463 ZTEST(modem_cmux_pair, test_modem_cmux_dlci1_close_open)
464 {
465 uint32_t events;
466
467 /* Close DLCI1 */
468 zassert_true(modem_pipe_close_async(dlci1_pipe) == 0, "Failed to close DLCI1 pipe");
469 k_msleep(100);
470 events = k_event_wait_all(&cmux_event_dte, (EVENT_CMUX_DLCI1_CLOSED), false, K_MSEC(100));
471
472 zassert_true((events & EVENT_CMUX_DLCI1_CLOSED), "DLCI1 not closed as expected");
473 events = k_event_wait_all(&cmux_event_dce, (EVENT_CMUX_DLCI1_CLOSED), false, K_MSEC(100));
474
475 zassert_true((events & EVENT_CMUX_DLCI1_CLOSED), "DLCI1 not closed as expected");
476
477 /* Open DLCI1 */
478 zassert_true(modem_pipe_open_async(dlci1_pipe) == 0, "Failed to open DLCI1 pipe");
479
480 k_msleep(100);
481
482 events = k_event_wait_all(&cmux_event_dte, (EVENT_CMUX_DLCI1_OPEN), false, K_MSEC(100));
483
484 zassert_true((events & EVENT_CMUX_DLCI1_OPEN), "DLCI1 not opened as expected");
485 events = k_event_wait_all(&cmux_event_dce, (EVENT_CMUX_DLCI1_OPEN), false, K_MSEC(100));
486
487 zassert_true((events & EVENT_CMUX_DLCI1_OPEN), "DLCI1 not opened as expected");
488 }
489
ZTEST(modem_cmux_pair,test_modem_cmux_disconnect_connect)490 ZTEST(modem_cmux_pair, test_modem_cmux_disconnect_connect)
491 {
492 uint32_t events;
493
494 /* Disconnect CMUX */
495 zassert_true(modem_pipe_close_async(dlci1_pipe) == 0, "Failed to close DLCI1");
496 zassert_true(modem_pipe_close_async(dlci2_pipe) == 0, "Failed to close DLCI2");
497 k_msleep(100);
498
499 events = k_event_wait_all(&cmux_event_dte,
500 (EVENT_CMUX_DLCI1_CLOSED | EVENT_CMUX_DLCI2_CLOSED), false,
501 K_MSEC(100));
502
503 zassert_true((events & EVENT_CMUX_DLCI1_CLOSED), "Failed to close DLCI1");
504 zassert_true((events & EVENT_CMUX_DLCI2_CLOSED), "Failed to close DLCI2");
505
506 /* Discard CMUX DLCI DISC commands */
507 modem_backend_mock_reset(&bus_mock_dte);
508 zassert_true(modem_cmux_disconnect_async(&cmux_dte) == 0, "Failed to disconnect CMUX");
509
510 k_msleep(100);
511
512 events = k_event_wait_all(&cmux_event_dte, (EVENT_CMUX_DISCONNECTED), false, K_MSEC(100));
513 zassert_true((events & EVENT_CMUX_DISCONNECTED), "Failed to disconnect CMUX");
514
515 /* Reconnect CMUX */
516 zassert_true(modem_cmux_connect_async(&cmux_dte) == 0, "Failed to connect CMUX");
517
518 k_msleep(100);
519
520 events = k_event_wait_all(&cmux_event_dte, (EVENT_CMUX_CONNECTED), false, K_MSEC(100));
521 zassert_true((events & EVENT_CMUX_CONNECTED), "Failed to connect CMUX");
522
523 /* Open DLCI1 */
524 zassert_true(modem_pipe_open_async(dlci1_pipe) == 0, "Failed to open DLCI1 pipe");
525
526 k_msleep(100);
527
528 events = k_event_wait_all(&cmux_event_dte, (EVENT_CMUX_DLCI1_OPEN), false, K_MSEC(100));
529
530 zassert_true((events & EVENT_CMUX_DLCI1_OPEN), "DLCI1 not opened as expected");
531
532 /* Open DLCI2 */
533 zassert_true(modem_pipe_open_async(dlci2_pipe) == 0, "Failed to open DLCI2 pipe");
534
535 k_msleep(100);
536
537 events = k_event_wait_all(&cmux_event_dte, (EVENT_CMUX_DLCI2_OPEN), false, K_MSEC(100));
538
539 zassert_true((events & EVENT_CMUX_DLCI2_OPEN), "DLCI1 not opened as expected");
540 }
541
ZTEST(modem_cmux_pair,test_modem_cmux_disconnect_connect_sync)542 ZTEST(modem_cmux_pair, test_modem_cmux_disconnect_connect_sync)
543 {
544 uint32_t events;
545
546 zassert_true(modem_pipe_close(dlci1_pipe, K_SECONDS(10)) == 0, "Failed to close DLCI1");
547 zassert_true(modem_pipe_close(dlci2_pipe, K_SECONDS(10)) == 0, "Failed to close DLCI2");
548 events = k_event_wait_all(&cmux_event_dce,
549 (EVENT_CMUX_DLCI1_CLOSED | EVENT_CMUX_DLCI2_CLOSED), false,
550 K_MSEC(100));
551 zassert_true((events & EVENT_CMUX_DLCI1_CLOSED), "DCE DLCI1 not closed as expected");
552 zassert_true((events & EVENT_CMUX_DLCI2_CLOSED), "DCE DLCI2 not closed as expected");
553
554 zassert_true(modem_cmux_disconnect(&cmux_dte) == 0, "Failed to disconnect CMUX");
555 zassert_true(modem_cmux_disconnect(&cmux_dte) == -EALREADY,
556 "Should already be disconnected");
557 zassert_true(modem_cmux_disconnect(&cmux_dce) == -EALREADY,
558 "Should already be disconnected");
559
560 k_msleep(100);
561
562 zassert_true(modem_cmux_connect(&cmux_dte) == 0, "Failed to connect CMUX");
563 zassert_true(modem_cmux_connect(&cmux_dte) == -EALREADY, "Should already be connected");
564 zassert_true(modem_cmux_connect(&cmux_dce) == -EALREADY, "Should already be connected");
565
566 zassert_true(modem_pipe_open(dlci1_pipe, K_SECONDS(10)) == 0, "Failed to open DLCI1 pipe");
567 zassert_true(modem_pipe_open(dlci2_pipe, K_SECONDS(10)) == 0, "Failed to open DLCI2 pipe");
568 events = k_event_wait_all(&cmux_event_dce, (EVENT_CMUX_DLCI1_OPEN | EVENT_CMUX_DLCI2_OPEN),
569 false, K_MSEC(100));
570 zassert_true((events & EVENT_CMUX_DLCI1_OPEN), "DCE DLCI1 not open as expected");
571 zassert_true((events & EVENT_CMUX_DLCI2_OPEN), "DCE DLCI2 not open as expected");
572 }
573
ZTEST(modem_cmux_pair,test_modem_cmux_dlci_close_open_sync)574 ZTEST(modem_cmux_pair, test_modem_cmux_dlci_close_open_sync)
575 {
576 uint32_t events;
577
578 zassert_true(modem_pipe_close(dlci1_pipe, K_SECONDS(10)) == 0, "Failed to close DLCI1");
579 zassert_true(modem_pipe_close(dlci2_pipe, K_SECONDS(10)) == 0, "Failed to close DLCI2");
580
581 events = k_event_wait_all(&cmux_event_dce,
582 (EVENT_CMUX_DLCI1_CLOSED | EVENT_CMUX_DLCI2_CLOSED), false,
583 K_MSEC(100));
584 zassert_true((events & EVENT_CMUX_DLCI1_CLOSED), "DCE DLCI1 not closed as expected");
585 zassert_true((events & EVENT_CMUX_DLCI2_CLOSED), "DCE DLCI2 not closed as expected");
586
587 zassert_true(modem_pipe_open(dlci1_pipe, K_SECONDS(10)) == 0, "Failed to open DLCI1 pipe");
588 zassert_true(modem_pipe_open(dlci2_pipe, K_SECONDS(10)) == 0, "Failed to open DLCI2 pipe");
589 /* Verify DCE side channels are open also */
590 events = k_event_wait_all(&cmux_event_dce, (EVENT_CMUX_DLCI1_OPEN | EVENT_CMUX_DLCI2_OPEN),
591 false, K_MSEC(100));
592 zassert_true((events & EVENT_CMUX_DLCI1_OPEN), "DCE DLCI1 not open as expected");
593 zassert_true((events & EVENT_CMUX_DLCI2_OPEN), "DCE DLCI2 not open as expected");
594 }
595
596 ZTEST_SUITE(modem_cmux_pair, NULL, test_setup, test_before, NULL, NULL);
597