1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8
9 #define TIMEOUT K_MSEC(100)
10 #if !defined(CONFIG_BOARD_QEMU_X86)
11 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
12 #else
13 #define STACK_SIZE (640 + CONFIG_TEST_EXTRA_STACK_SIZE)
14 #endif
15 #define MAIL_LEN 64
16
17 /**
18 * @brief Tests for the mailbox kernel object
19 * @defgroup kernel_mbox_api Mailbox
20 * @ingroup all_tests
21 * @{
22 * @}
23 */
24
25 /**TESTPOINT: init via K_MBOX_DEFINE*/
26 K_MBOX_DEFINE(kmbox);
27
28 static struct k_mbox mbox;
29
30 static k_tid_t sender_tid, receiver_tid, random_tid;
31
32 static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
33 static K_THREAD_STACK_DEFINE(rtstack, STACK_SIZE);
34 static K_THREAD_STACK_DEFINE(tstack_1, STACK_SIZE);
35 static K_THREAD_STACK_ARRAY_DEFINE(waiting_get_stack, 5, STACK_SIZE);
36 static struct k_thread tdata, rtdata, async_tid, waiting_get_tid[5];
37
38 static struct k_sem end_sema, sync_sema;
39
40 static enum mmsg_type {
41 PUT_GET_NULL = 0,
42 PUT_GET_BUFFER,
43 ASYNC_PUT_GET_BUFFER,
44 ASYNC_PUT_GET_BLOCK,
45 TARGET_SOURCE_THREAD_BUFFER,
46 MAX_INFO_TYPE,
47 INCORRECT_RECEIVER_TID,
48 INCORRECT_TRANSMIT_TID,
49 TIMED_OUT_MBOX_GET,
50 MSG_TID_MISMATCH,
51 DISPOSE_SIZE_0_MSG,
52 ASYNC_PUT_TO_WAITING_GET,
53 GET_WAITING_PUT_INCORRECT_TID,
54 ASYNC_MULTIPLE_PUT,
55 MULTIPLE_WAITING_GET
56 } info_type;
57
58 static char data[MAX_INFO_TYPE][MAIL_LEN] = {
59 "send/recv an empty message",
60 "send/recv msg using a buffer",
61 "async send/recv msg using a memory block",
62 "specify target/source thread, using a memory block"
63 };
64
async_put_sema_give(void * p1,void * p2,void * p3)65 static void async_put_sema_give(void *p1, void *p2, void *p3)
66 {
67 k_sem_give(&sync_sema);
68 }
69
70
mbox_get_waiting_thread(void * p1,void * p2,void * p3)71 static void mbox_get_waiting_thread(void *p1, void *p2, void *p3)
72 {
73 int thread_number = POINTER_TO_INT(p1);
74 struct k_mbox *pmbox = p2;
75 struct k_mbox_msg mmsg = {0};
76
77 switch (thread_number) {
78 case 0:
79 mmsg.rx_source_thread = K_ANY;
80 break;
81
82 case 1:
83 mmsg.rx_source_thread = random_tid;
84 break;
85
86 case 2:
87 mmsg.rx_source_thread = receiver_tid;
88 break;
89
90 case 3:
91 mmsg.rx_source_thread = &async_tid;
92 break;
93
94 case 4:
95 mmsg.rx_source_thread = K_ANY;
96 break;
97
98 default:
99 break;
100 }
101
102 mmsg.size = 0;
103 zassert_true(k_mbox_get(pmbox, &mmsg, NULL, K_FOREVER) == 0,
104 "Failure at thread number %d", thread_number);
105
106 }
107
tmbox_put(struct k_mbox * pmbox)108 static void tmbox_put(struct k_mbox *pmbox)
109 {
110 struct k_mbox_msg mmsg = {0};
111
112 switch (info_type) {
113 case PUT_GET_NULL:
114 /**TESTPOINT: mbox sync put empty message*/
115 mmsg.info = PUT_GET_NULL;
116 mmsg.size = 0;
117 mmsg.tx_data = NULL;
118 mmsg.tx_target_thread = K_ANY;
119 k_mbox_put(pmbox, &mmsg, K_FOREVER);
120 break;
121 case PUT_GET_BUFFER:
122 __fallthrough;
123 case TARGET_SOURCE_THREAD_BUFFER:
124 /**TESTPOINT: mbox sync put buffer*/
125 mmsg.info = PUT_GET_BUFFER;
126 mmsg.size = sizeof(data[info_type]);
127 mmsg.tx_data = data[info_type];
128 if (info_type == TARGET_SOURCE_THREAD_BUFFER) {
129 mmsg.tx_target_thread = receiver_tid;
130 } else {
131 mmsg.tx_target_thread = K_ANY;
132 }
133 k_mbox_put(pmbox, &mmsg, K_FOREVER);
134 break;
135 case ASYNC_PUT_GET_BUFFER:
136 /**TESTPOINT: mbox async put buffer*/
137 mmsg.info = ASYNC_PUT_GET_BUFFER;
138 mmsg.size = sizeof(data[info_type]);
139 mmsg.tx_data = data[info_type];
140 mmsg.tx_target_thread = K_ANY;
141 k_mbox_async_put(pmbox, &mmsg, &sync_sema);
142 /*wait for msg being taken*/
143 k_sem_take(&sync_sema, K_FOREVER);
144 break;
145 case ASYNC_PUT_GET_BLOCK:
146 __fallthrough;
147 case INCORRECT_TRANSMIT_TID:
148 mmsg.tx_target_thread = random_tid;
149 zassert_true(k_mbox_put(pmbox,
150 &mmsg,
151 K_NO_WAIT) == -ENOMSG, NULL);
152 break;
153 case MSG_TID_MISMATCH:
154 /* keep one msg in the queue and try to get with a wrong tid */
155 mmsg.info = PUT_GET_NULL;
156 mmsg.size = 0;
157 mmsg.tx_data = NULL;
158 mmsg.tx_target_thread = sender_tid;
159 /* timeout because this msg wont be received with a _get*/
160 k_mbox_put(pmbox, &mmsg, TIMEOUT);
161 break;
162
163 case DISPOSE_SIZE_0_MSG:
164 /* Get a msg and dispose it by making the size = 0 */
165 mmsg.size = 0;
166 mmsg.tx_data = data[1];
167 mmsg.tx_block.data = NULL;
168 mmsg.tx_target_thread = K_ANY;
169 zassert_true(k_mbox_put(pmbox, &mmsg, K_FOREVER) == 0);
170 break;
171
172 case ASYNC_PUT_TO_WAITING_GET:
173 k_sem_take(&sync_sema, K_FOREVER);
174 mmsg.size = sizeof(data[0]);
175 mmsg.tx_data = data[0];
176 mmsg.tx_target_thread = K_ANY;
177 k_mbox_async_put(pmbox, &mmsg, NULL);
178 break;
179 case GET_WAITING_PUT_INCORRECT_TID:
180 k_sem_take(&sync_sema, K_FOREVER);
181 mmsg.size = sizeof(data[0]);
182 mmsg.tx_data = data[0];
183 mmsg.tx_target_thread = random_tid;
184 k_mbox_async_put(pmbox, &mmsg, &sync_sema);
185 break;
186 case ASYNC_MULTIPLE_PUT:
187 mmsg.size = sizeof(data[0]);
188 mmsg.tx_data = data[0];
189 mmsg.tx_target_thread = K_ANY;
190 k_mbox_async_put(pmbox, &mmsg, NULL);
191
192 mmsg.tx_data = data[1];
193 mmsg.tx_target_thread = &async_tid;
194 k_mbox_async_put(pmbox, &mmsg, NULL);
195
196 mmsg.tx_data = data[1];
197 mmsg.tx_target_thread = receiver_tid;
198 k_mbox_async_put(pmbox, &mmsg, NULL);
199
200 mmsg.tx_data = data[1];
201 mmsg.tx_target_thread = &async_tid;
202 k_mbox_async_put(pmbox, &mmsg, NULL);
203
204 mmsg.tx_data = data[2];
205 mmsg.tx_target_thread = receiver_tid;
206 k_mbox_async_put(pmbox, &mmsg, &sync_sema);
207
208 k_sem_take(&sync_sema, K_FOREVER);
209 break;
210
211 case MULTIPLE_WAITING_GET:
212 k_sem_take(&sync_sema, K_FOREVER);
213
214 mmsg.size = sizeof(data[0]);
215 mmsg.tx_data = data[0];
216 mmsg.tx_target_thread = K_ANY;
217 k_mbox_put(pmbox, &mmsg, K_NO_WAIT);
218
219 mmsg.tx_data = data[1];
220 mmsg.tx_target_thread = &async_tid;
221 k_mbox_put(pmbox, &mmsg, K_NO_WAIT);
222
223 mmsg.tx_data = data[1];
224 mmsg.tx_target_thread = receiver_tid;
225 k_mbox_put(pmbox, &mmsg, K_NO_WAIT);
226
227 mmsg.tx_data = data[1];
228 mmsg.tx_target_thread = &async_tid;
229 k_mbox_put(pmbox, &mmsg, K_NO_WAIT);
230
231 mmsg.tx_data = data[2];
232 mmsg.tx_target_thread = receiver_tid;
233 k_mbox_put(pmbox, &mmsg, K_NO_WAIT);
234
235 break;
236 default:
237 break;
238 }
239 }
240
tmbox_get(struct k_mbox * pmbox)241 static void tmbox_get(struct k_mbox *pmbox)
242 {
243 struct k_mbox_msg mmsg = {0};
244 char rxdata[MAIL_LEN];
245
246 switch (info_type) {
247 case PUT_GET_NULL:
248 /**TESTPOINT: mbox sync get buffer*/
249 mmsg.size = sizeof(rxdata);
250 mmsg.rx_source_thread = K_ANY;
251 /*verify return value*/
252 zassert_true(k_mbox_get(pmbox, &mmsg, rxdata, K_FOREVER) == 0,
253 NULL);
254 /*verify .info*/
255 zassert_equal(mmsg.info, PUT_GET_NULL);
256 /*verify .size*/
257 zassert_equal(mmsg.size, 0);
258 break;
259 case PUT_GET_BUFFER:
260 __fallthrough;
261 case TARGET_SOURCE_THREAD_BUFFER:
262 /**TESTPOINT: mbox sync get buffer*/
263 mmsg.size = sizeof(rxdata);
264 if (info_type == TARGET_SOURCE_THREAD_BUFFER) {
265 mmsg.rx_source_thread = sender_tid;
266 } else {
267 mmsg.rx_source_thread = K_ANY;
268 }
269 zassert_true(k_mbox_get(pmbox, &mmsg, rxdata, K_FOREVER) == 0,
270 NULL);
271 zassert_equal(mmsg.info, PUT_GET_BUFFER);
272 zassert_equal(mmsg.size, sizeof(data[info_type]));
273 /*verify rxdata*/
274 zassert_true(memcmp(rxdata, data[info_type], MAIL_LEN) == 0,
275 NULL);
276 break;
277 case ASYNC_PUT_GET_BUFFER:
278 /**TESTPOINT: mbox async get buffer*/
279 mmsg.size = sizeof(rxdata);
280 mmsg.rx_source_thread = K_ANY;
281 zassert_true(k_mbox_get(pmbox, &mmsg, NULL, K_FOREVER) == 0,
282 NULL);
283 zassert_equal(mmsg.info, ASYNC_PUT_GET_BUFFER);
284 zassert_equal(mmsg.size, sizeof(data[info_type]));
285 k_mbox_data_get(&mmsg, rxdata);
286 zassert_true(memcmp(rxdata, data[info_type], MAIL_LEN) == 0,
287 NULL);
288 break;
289 case ASYNC_PUT_GET_BLOCK:
290 __fallthrough;
291 case INCORRECT_RECEIVER_TID:
292 mmsg.rx_source_thread = random_tid;
293 zassert_true(k_mbox_get
294 (pmbox, &mmsg, NULL, K_NO_WAIT) == -ENOMSG,
295 NULL);
296 break;
297 case TIMED_OUT_MBOX_GET:
298 mmsg.rx_source_thread = random_tid;
299 zassert_true(k_mbox_get(pmbox, &mmsg, NULL, TIMEOUT) == -EAGAIN,
300 NULL);
301 break;
302 case MSG_TID_MISMATCH:
303 mmsg.rx_source_thread = random_tid;
304 zassert_true(k_mbox_get
305 (pmbox, &mmsg, NULL, K_NO_WAIT) == -ENOMSG, NULL);
306 break;
307
308 case DISPOSE_SIZE_0_MSG:
309 mmsg.rx_source_thread = K_ANY;
310 mmsg.size = 0;
311 zassert_true(k_mbox_get(pmbox, &mmsg, &rxdata, K_FOREVER) == 0,
312 NULL);
313 break;
314
315 case ASYNC_PUT_TO_WAITING_GET:
316
317 /* Create a new thread to trigger the semaphore needed for the
318 * async put.
319 */
320 k_thread_create(&async_tid, tstack_1, STACK_SIZE,
321 async_put_sema_give, NULL, NULL, NULL,
322 K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
323 mmsg.rx_source_thread = K_ANY;
324 mmsg.size = 0;
325 /* Here get is blocked until the thread we created releases
326 * the semaphore and the async put completes it operation.
327 */
328 zassert_true(k_mbox_get(pmbox, &mmsg, NULL, K_FOREVER) == 0,
329 NULL);
330 break;
331 case GET_WAITING_PUT_INCORRECT_TID:
332 /* Create a new thread to trigger the semaphore needed for the
333 * async put.
334 */
335 k_thread_create(&async_tid, tstack_1, STACK_SIZE,
336 async_put_sema_give, NULL, NULL, NULL,
337 K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
338 mmsg.rx_source_thread = &async_tid;
339 mmsg.size = 0;
340 /* Here the get is waiting for a async put to complete
341 * but the TIDs of the msgs doesn't match and hence
342 * causing a timeout.
343 */
344 zassert_true(k_mbox_get(pmbox, &mmsg, NULL, TIMEOUT) == -EAGAIN,
345 NULL);
346 /* clean up */
347 mmsg.rx_source_thread = K_ANY;
348 zassert_true(k_mbox_get(pmbox, &mmsg, NULL, TIMEOUT) == 0,
349 NULL);
350 break;
351
352 case ASYNC_MULTIPLE_PUT:
353 /* Async put has now populated the msgs. Now retrieve all
354 * the msgs from the mailbox.
355 */
356 mmsg.rx_source_thread = K_ANY;
357 mmsg.size = 0;
358 /* get K_any msg */
359 zassert_true(k_mbox_get(pmbox, &mmsg, NULL, TIMEOUT) == 0,
360 NULL);
361 /* get the msg specific to receiver_tid */
362 mmsg.rx_source_thread = sender_tid;
363 zassert_true(k_mbox_get
364 (pmbox, &mmsg, NULL, TIMEOUT) == 0, NULL);
365
366 /* get msg from async or random tid */
367 mmsg.rx_source_thread = K_ANY;
368 zassert_true(k_mbox_get
369 (pmbox, &mmsg, NULL, TIMEOUT) == 0, NULL);
370 break;
371 case MULTIPLE_WAITING_GET:
372 /* Create 5 threads who will wait on a mbox_get. */
373 for (uint32_t i = 0; i < 5; i++) {
374 k_thread_create(&waiting_get_tid[i],
375 waiting_get_stack[i],
376 STACK_SIZE,
377 mbox_get_waiting_thread,
378 INT_TO_POINTER(i), pmbox, NULL,
379 K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
380 }
381 /* Create a new thread to trigger the semaphore needed for the
382 * async put. This will trigger the start of the msg transfer.
383 */
384 k_thread_create(&async_tid, tstack_1, STACK_SIZE,
385 async_put_sema_give, NULL, NULL, NULL,
386 K_PRIO_PREEMPT(1), 0, K_NO_WAIT);
387 break;
388
389 default:
390 break;
391 }
392 }
393
394
395
396 /*entry of contexts*/
tmbox_entry(void * p1,void * p2,void * p3)397 static void tmbox_entry(void *p1, void *p2, void *p3)
398 {
399 tmbox_get((struct k_mbox *)p1);
400 k_sem_give(&end_sema);
401 }
402
tmbox(struct k_mbox * pmbox)403 static void tmbox(struct k_mbox *pmbox)
404 {
405 /*test case setup*/
406 k_sem_reset(&end_sema);
407 k_sem_reset(&sync_sema);
408
409 /**TESTPOINT: thread-thread data passing via mbox*/
410 sender_tid = k_current_get();
411 receiver_tid = k_thread_create(&tdata, tstack, STACK_SIZE,
412 tmbox_entry, pmbox, NULL, NULL,
413 K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
414
415 tmbox_put(pmbox);
416 k_sem_take(&end_sema, K_FOREVER);
417
418 /*test case teardown*/
419 k_thread_abort(receiver_tid);
420 }
421
422 /*test cases*/
ZTEST(mbox_api,test_mbox_kinit)423 ZTEST(mbox_api, test_mbox_kinit)
424 {
425 /**TESTPOINT: init via k_mbox_init*/
426 k_mbox_init(&mbox);
427 }
428
ZTEST(mbox_api,test_mbox_kdefine)429 ZTEST(mbox_api, test_mbox_kdefine)
430 {
431 info_type = PUT_GET_NULL;
432 tmbox(&kmbox);
433 }
434
thread_mbox_data_get_null(void * p1,void * p2,void * p3)435 static void thread_mbox_data_get_null(void *p1, void *p2, void *p3)
436 {
437 struct k_mbox_msg get_msg = {0};
438 char str_data[] = "it string for get msg test";
439
440 get_msg.size = 16;
441 get_msg.rx_source_thread = K_ANY;
442 get_msg.tx_block.data = str_data;
443 get_msg._syncing_thread = NULL;
444
445 k_mbox_data_get(&get_msg, NULL);
446 k_sem_give(&end_sema);
447 }
448
449 /**
450 *
451 * @brief Test k_mbox_data_get() API
452 *
453 * @details
454 * - Init a mbox and just invoke k_mbox_data_get() with different
455 * input to check robust of API.
456 *
457 * @see k_mbox_data_get()
458 *
459 * @ingroup kernel_mbox_api
460 */
ZTEST(mbox_api,test_mbox_data_get_null)461 ZTEST(mbox_api, test_mbox_data_get_null)
462 {
463 k_sem_reset(&end_sema);
464
465 receiver_tid = k_thread_create(&tdata, tstack, STACK_SIZE,
466 thread_mbox_data_get_null,
467 NULL, NULL, NULL,
468 K_PRIO_PREEMPT(0), 0,
469 K_NO_WAIT);
470 k_sem_take(&end_sema, K_FOREVER);
471 /*test case teardown*/
472 k_thread_abort(receiver_tid);
473 }
474
thread_mbox_get_block_data(void * p1,void * p2,void * p3)475 static void thread_mbox_get_block_data(void *p1, void *p2, void *p3)
476 {
477 k_sem_take(&sync_sema, K_FOREVER);
478 struct k_mbox_msg bdmsg = {0};
479
480 bdmsg.size = MAIL_LEN;
481 bdmsg.rx_source_thread = sender_tid;
482 bdmsg.tx_target_thread = receiver_tid;
483 bdmsg.tx_data = NULL;
484 bdmsg.tx_block.data = data;
485 bdmsg.tx_data = data;
486
487 zassert_equal(k_mbox_get((struct k_mbox *)p1, &bdmsg, p2, K_FOREVER),
488 0, NULL);
489
490 k_sem_give(&end_sema);
491 }
492
493 /* give a block data to API k_mbox_async_put */
thread_mbox_put_block_data(void * p1,void * p2,void * p3)494 static void thread_mbox_put_block_data(void *p1, void *p2, void *p3)
495 {
496 struct k_mbox_msg put_msg = {0};
497
498 put_msg.size = MAIL_LEN;
499 put_msg.tx_data = NULL;
500 put_msg.tx_block.data = p2;
501 put_msg.tx_target_thread = receiver_tid;
502 put_msg.rx_source_thread = sender_tid;
503
504 k_mbox_async_put((struct k_mbox *)p1, &put_msg, NULL);
505 }
506
507 /**
508 *
509 * @brief Test put and get mailbox with block data
510 *
511 * @details
512 * - Create two threads to put and get block data with
513 * specify thread ID and K_FOREVER for each other.
514 * - Check the result after finished exchange.
515 *
516 * @see k_mbox_init() k_mbox_async_put() k_mbox_get()
517 *
518 * @ingroup kernel_mbox_api
519 */
ZTEST(mbox_api,test_mbox_get_put_block_data)520 ZTEST(mbox_api, test_mbox_get_put_block_data)
521 {
522 struct k_mbox bdmbox;
523 /*test case setup*/
524 k_sem_reset(&end_sema);
525 k_sem_reset(&sync_sema);
526 k_mbox_init(&bdmbox);
527 char buff[MAIL_LEN];
528 char data_put[] = "mbox put data";
529
530 /**TESTPOINT: thread-thread data passing via mbox*/
531 sender_tid = k_current_get();
532 receiver_tid = k_thread_create(&rtdata, rtstack, STACK_SIZE,
533 thread_mbox_get_block_data,
534 &bdmbox, buff, NULL,
535 K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
536
537 sender_tid = k_thread_create(&tdata, tstack, STACK_SIZE,
538 thread_mbox_put_block_data,
539 &bdmbox, data_put, NULL,
540 K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
541 k_sem_give(&sync_sema);
542 k_sem_take(&end_sema, K_FOREVER);
543 /*abort receiver thread*/
544 k_thread_abort(receiver_tid);
545 k_thread_abort(sender_tid);
546
547 zassert_equal(memcmp(buff, data_put, sizeof(data_put)), 0,
548 NULL);
549 }
550
551 static ZTEST_BMEM char __aligned(4) buffer[8];
552
553 /**
554 *
555 * @brief Test mailbox enhance capabilities
556 *
557 * @details
558 * - Define and initialized a message queue and a mailbox
559 * - Verify the capability of message queue and mailbox
560 * - with same data.
561 *
562 * @ingroup kernel_mbox_api
563 *
564 * @see k_msgq_init() k_msgq_put() k_mbox_async_put() k_mbox_get()
565 */
ZTEST(mbox_api,test_enhance_capability)566 ZTEST(mbox_api, test_enhance_capability)
567 {
568 info_type = ASYNC_PUT_GET_BUFFER;
569 struct k_msgq msgq;
570
571 k_msgq_init(&msgq, buffer, 4, 2);
572 /* send buffer with message queue */
573 int ret = k_msgq_put(&msgq, &data[info_type], K_NO_WAIT);
574
575 zassert_equal(ret, 0, "message queue put successful");
576
577 /* send same buffer with mailbox */
578 tmbox(&mbox);
579 }
580
581 /*
582 *
583 * @brife Test any number of mailbox can be defined
584 *
585 * @details
586 * - Define multi mailbox and verify the mailbox whether as
587 * expected
588 * - Verify the mailbox can be used
589 *
590 */
ZTEST(mbox_api,test_define_multi_mbox)591 ZTEST(mbox_api, test_define_multi_mbox)
592 {
593 /**TESTPOINT: init via k_mbox_init*/
594 struct k_mbox mbox1, mbox2, mbox3;
595
596 k_mbox_init(&mbox1);
597 k_mbox_init(&mbox2);
598 k_mbox_init(&mbox3);
599
600 /* verify via send message */
601 info_type = PUT_GET_NULL;
602 tmbox(&mbox1);
603 tmbox(&mbox2);
604 tmbox(&mbox3);
605 }
606
ZTEST(mbox_api,test_mbox_put_get_null)607 ZTEST(mbox_api, test_mbox_put_get_null)
608 {
609 info_type = PUT_GET_NULL;
610 tmbox(&mbox);
611 }
612
ZTEST(mbox_api,test_mbox_put_get_buffer)613 ZTEST(mbox_api, test_mbox_put_get_buffer)
614 {
615 info_type = PUT_GET_BUFFER;
616 tmbox(&mbox);
617 }
618
ZTEST(mbox_api,test_mbox_async_put_get_buffer)619 ZTEST(mbox_api, test_mbox_async_put_get_buffer)
620 {
621 info_type = ASYNC_PUT_GET_BUFFER;
622 tmbox(&mbox);
623 }
624
ZTEST(mbox_api,test_mbox_async_put_get_block)625 ZTEST(mbox_api, test_mbox_async_put_get_block)
626 {
627 info_type = ASYNC_PUT_GET_BLOCK;
628 tmbox(&mbox);
629 }
630
ZTEST(mbox_api,test_mbox_target_source_thread_buffer)631 ZTEST(mbox_api, test_mbox_target_source_thread_buffer)
632 {
633 info_type = TARGET_SOURCE_THREAD_BUFFER;
634 tmbox(&mbox);
635 }
636
ZTEST(mbox_api,test_mbox_incorrect_receiver_tid)637 ZTEST(mbox_api, test_mbox_incorrect_receiver_tid)
638 {
639 info_type = INCORRECT_RECEIVER_TID;
640 tmbox(&mbox);
641 }
642
ZTEST(mbox_api,test_mbox_incorrect_transmit_tid)643 ZTEST(mbox_api, test_mbox_incorrect_transmit_tid)
644 {
645 info_type = INCORRECT_TRANSMIT_TID;
646 tmbox(&mbox);
647 }
648
ZTEST(mbox_api,test_mbox_timed_out_mbox_get)649 ZTEST(mbox_api, test_mbox_timed_out_mbox_get)
650 {
651 info_type = TIMED_OUT_MBOX_GET;
652 tmbox(&mbox);
653 }
654
ZTEST(mbox_api,test_mbox_msg_tid_mismatch)655 ZTEST(mbox_api, test_mbox_msg_tid_mismatch)
656 {
657 info_type = MSG_TID_MISMATCH;
658 tmbox(&mbox);
659 }
660
ZTEST(mbox_api,test_mbox_dispose_size_0_msg)661 ZTEST(mbox_api, test_mbox_dispose_size_0_msg)
662 {
663 info_type = DISPOSE_SIZE_0_MSG;
664 tmbox(&mbox);
665 }
666
ZTEST(mbox_api,test_mbox_async_put_to_waiting_get)667 ZTEST(mbox_api, test_mbox_async_put_to_waiting_get)
668 {
669 info_type = ASYNC_PUT_TO_WAITING_GET;
670 tmbox(&mbox);
671 }
672
ZTEST(mbox_api,test_mbox_get_waiting_put_incorrect_tid)673 ZTEST(mbox_api, test_mbox_get_waiting_put_incorrect_tid)
674 {
675 info_type = GET_WAITING_PUT_INCORRECT_TID;
676 tmbox(&mbox);
677 }
678
ZTEST(mbox_api,test_mbox_async_multiple_put)679 ZTEST(mbox_api, test_mbox_async_multiple_put)
680 {
681 info_type = ASYNC_MULTIPLE_PUT;
682 tmbox(&mbox);
683 }
684
ZTEST(mbox_api,test_mbox_multiple_waiting_get)685 ZTEST(mbox_api, test_mbox_multiple_waiting_get)
686 {
687 info_type = MULTIPLE_WAITING_GET;
688 tmbox(&mbox);
689
690 /* cleanup the sender threads */
691 for (int i = 0; i < 5 ; i++) {
692 k_thread_abort(&waiting_get_tid[i]);
693 }
694 }
695
setup_mbox_api(void)696 void *setup_mbox_api(void)
697 {
698 k_sem_init(&end_sema, 0, 1);
699 k_sem_init(&sync_sema, 0, 1);
700 k_mbox_init(&mbox);
701
702 return NULL;
703 }
704
705 ZTEST_SUITE(mbox_api, NULL, setup_mbox_api, NULL, NULL, NULL);
706