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