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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 | /* * Copyright (c) 2012-2014 Wind River Systems, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @file * @brief Test microkernel target pipe APIs * * This modules tests the following target pipe routines: * * task_pipe_put() * task_pipe_get() * * The following target pipe routine does not yet have a test case: * task_pipe_block_put() */ #include <zephyr.h> #include <tc_util.h> #include <misc/util.h> #define ONE_SECOND (sys_clock_ticks_per_sec) #define IRQ_PRIORITY 3 #define PIPE_SIZE 256 /* This must match the value in the MDEF file */ typedef struct { int size; /* number of bytes to send/receive */ K_PIPE_OPTION options; /* options for task_pipe_XXX() APIs */ int sent; /* expected # of bytes sent */ int rcode; /* expected return code */ } SIZE_EXPECT; static char txBuffer[PIPE_SIZE + 32]; static char rxBuffer[PIPE_SIZE + 32]; static SIZE_EXPECT all_N[] = { {0, _ALL_N, 0, RC_FAIL}, {1, _ALL_N, 1, RC_OK}, {PIPE_SIZE - 1, _ALL_N, PIPE_SIZE - 1, RC_OK}, {PIPE_SIZE, _ALL_N, PIPE_SIZE, RC_OK}, {PIPE_SIZE + 1, _ALL_N, 0, RC_FAIL} }; static SIZE_EXPECT many_all_N[] = { {PIPE_SIZE / 3, _ALL_N, PIPE_SIZE / 3, RC_OK,}, {PIPE_SIZE / 3, _ALL_N, PIPE_SIZE / 3, RC_OK,}, {PIPE_SIZE / 3, _ALL_N, PIPE_SIZE / 3, RC_OK,}, {PIPE_SIZE / 3, _ALL_N, 0, RC_FAIL} }; static SIZE_EXPECT one_to_N[] = { {0, _1_TO_N, 0, RC_FAIL}, {1, _1_TO_N, 1, RC_OK}, {PIPE_SIZE - 1, _1_TO_N, PIPE_SIZE - 1, RC_OK}, {PIPE_SIZE, _1_TO_N, PIPE_SIZE, RC_OK}, {PIPE_SIZE + 1, _1_TO_N, PIPE_SIZE, RC_OK} }; static SIZE_EXPECT many_one_to_N[] = { {PIPE_SIZE / 3, _1_TO_N, PIPE_SIZE / 3, RC_OK}, {PIPE_SIZE / 3, _1_TO_N, PIPE_SIZE / 3, RC_OK}, {PIPE_SIZE / 3, _1_TO_N, PIPE_SIZE / 3, RC_OK}, {PIPE_SIZE / 3, _1_TO_N, 1, RC_OK}, {PIPE_SIZE / 3, _1_TO_N, 0, RC_FAIL} }; static SIZE_EXPECT zero_to_N[] = { {0, _0_TO_N, 0, RC_FAIL}, {1, _0_TO_N, 1, RC_OK}, {PIPE_SIZE - 1, _0_TO_N, PIPE_SIZE - 1, RC_OK}, {PIPE_SIZE, _0_TO_N, PIPE_SIZE, RC_OK}, {PIPE_SIZE + 1, _0_TO_N, PIPE_SIZE, RC_OK} }; static SIZE_EXPECT many_zero_to_N[] = { {PIPE_SIZE / 3, _0_TO_N, PIPE_SIZE / 3, RC_OK}, {PIPE_SIZE / 3, _0_TO_N, PIPE_SIZE / 3, RC_OK}, {PIPE_SIZE / 3, _0_TO_N, PIPE_SIZE / 3, RC_OK}, {PIPE_SIZE / 3, _0_TO_N, 1, RC_OK}, {PIPE_SIZE / 3, _0_TO_N, 0, RC_OK} }; /* * With the following 'wait' cases, the pipe buffer may be bypassed. It is * thus possible to transmit and receive via the pipe more bytes than its * buffer would allow. */ static SIZE_EXPECT wait_all_N[] = { {0, _ALL_N, 0, RC_FAIL}, {1, _ALL_N, 1, RC_OK}, {PIPE_SIZE - 1, _ALL_N, PIPE_SIZE - 1, RC_OK}, {PIPE_SIZE, _ALL_N, PIPE_SIZE, RC_OK}, {PIPE_SIZE + 1, _ALL_N, PIPE_SIZE + 1, RC_OK} }; static SIZE_EXPECT wait_one_to_N[] = { {0, _1_TO_N, 0, RC_FAIL}, {1, _1_TO_N, 1, RC_OK}, {PIPE_SIZE - 1, _1_TO_N, PIPE_SIZE - 1, RC_OK}, {PIPE_SIZE, _1_TO_N, PIPE_SIZE, RC_OK}, {PIPE_SIZE + 1, _1_TO_N, PIPE_SIZE + 1, RC_OK} }; static SIZE_EXPECT timeout_cases[] = { {0, _ALL_N, 0, RC_FAIL}, {1, _ALL_N, 0, RC_TIME}, {PIPE_SIZE - 1, _ALL_N, 0 , RC_TIME}, {PIPE_SIZE, _ALL_N, 0, RC_TIME}, {PIPE_SIZE + 1, _ALL_N, 0, RC_TIME}, {0, _1_TO_N, 0, RC_FAIL}, {1, _1_TO_N, 0, RC_TIME}, {PIPE_SIZE - 1, _1_TO_N, 0, RC_TIME}, {PIPE_SIZE, _1_TO_N, 0, RC_TIME}, {PIPE_SIZE + 1, _1_TO_N, 0, RC_TIME}, {0, _0_TO_N, 0, RC_FAIL}, {1, _0_TO_N, 0, RC_FAIL}, {PIPE_SIZE - 1, _0_TO_N, 0, RC_FAIL}, {PIPE_SIZE, _0_TO_N, 0, RC_FAIL}, {PIPE_SIZE + 1, _0_TO_N, 0, RC_FAIL} }; extern ksem_t regSem; extern ksem_t altSem; extern ksem_t counterSem; extern kpipe_t pipeId _GENERIC_SECTION(_k_pipe_ptr); /** * * @brief Initialize objects used in this microkernel test suite * * @return N/A */ void microObjectsInit(void) { int i; /* loop counter */ for (i = 0; i < sizeof(rxBuffer); i++) { txBuffer[i] = (char) i; } } /** * * @brief Check the contents of the receive buffer * * @param buffer pointer to buffer to check * @param size number of bytes to check * * @return <size> on success, index of wrong character on failure */ int receiveBufferCheck(char *buffer, int size) { int j; /* loop counter */ for (j = 0; j < size; j++) { if (buffer[j] != (char) j) { return j; } } return size; } /** * * @brief Helper routine to pipePutTest() * * @param singleItems testcase list (one item in the pipe) * @param nSingles number of items in testcase * @param manyItems testcase list (many items in the pipe) * @param nMany number of items in testcase * * @return TC_PASS on success, TC_FAIL on failure */ int pipePutHelperWork(SIZE_EXPECT *singleItems, int nSingles, SIZE_EXPECT *manyItems, int nMany) { int i; /* loop counter */ int j; /* loop counter */ int rv; /* value returned from task_pipe_get() */ int index; /* index to corrupted byte in buffer */ int bytesReceived; /* number of bytes received */ for (i = 0; i < nSingles; i++) { (void)task_sem_take(altSem, TICKS_UNLIMITED); for (j = 0; j < sizeof(rxBuffer); j++) { rxBuffer[j] = 0; } rv = task_pipe_get(pipeId, rxBuffer, singleItems[i].size, &bytesReceived, singleItems[i].options, TICKS_NONE); if (rv != singleItems[i].rcode) { TC_ERROR("task_pipe_get(%d bytes) : Expected %d not %d.\n" " bytesReceived = %d\n", singleItems[i].size, singleItems[i].rcode, rv, bytesReceived); return TC_FAIL; } if (bytesReceived != singleItems[i].sent) { TC_ERROR("task_pipe_get(%d) : " "Expected %d bytes to be received, not %d\n", singleItems[i].size, singleItems[i].sent, bytesReceived); return TC_FAIL; } index = receiveBufferCheck(rxBuffer, bytesReceived); if (index != bytesReceived) { TC_ERROR("pipePutHelper: rxBuffer[%d] is %d, not %d\n", index, rxBuffer[index], index); return TC_FAIL; } task_sem_give(counterSem); task_sem_give(regSem); } /* * Get items from the pipe. There should be more than one item * stored in it. */ (void)task_sem_take(altSem, TICKS_UNLIMITED); for (i = 0; i < nMany; i++) { for (j = 0; j < sizeof(rxBuffer); j++) { rxBuffer[j] = 0; } rv = task_pipe_get(pipeId, rxBuffer, manyItems[i].size, &bytesReceived, manyItems[i].options, TICKS_NONE); if (rv != manyItems[i].rcode) { TC_ERROR("task_pipe_get(%d bytes) : Expected %d not %d.\n" " bytesReceived = %d, iteration: %d\n", manyItems[i].size, manyItems[i].rcode, rv, bytesReceived, i+1); return TC_FAIL; } if (bytesReceived != manyItems[i].sent) { TC_ERROR("task_pipe_get(%d) : " "Expected %d bytes to be received, not %d\n", manyItems[i].size, manyItems[i].sent, bytesReceived); return TC_FAIL; } index = receiveBufferCheck(rxBuffer, bytesReceived); if (index != bytesReceived) { TC_ERROR("pipeGetHelper: rxBuffer[%d] is %d, not %d\n", index, rxBuffer[index], index); return TC_FAIL; } task_sem_give(counterSem); } task_sem_give(regSem); /* Wake the RegressionTask */ return TC_PASS; } /** * * @brief Helper routine to pipePutTest() * * @return TC_PASS on success, TC_FAIL on failure */ int pipePutHelper(void) { int rv; /* return value from pipePutHelperWork() */ rv = pipePutHelperWork(all_N, ARRAY_SIZE(all_N), many_all_N, ARRAY_SIZE(many_all_N)); if (rv != TC_PASS) { TC_ERROR("Failed on all_N/many_all_N test\n"); return TC_FAIL; } rv = pipePutHelperWork(one_to_N, ARRAY_SIZE(one_to_N), many_one_to_N, ARRAY_SIZE(many_one_to_N)); if (rv != TC_PASS) { TC_ERROR("Failed on _1_TO_N/many_1_TO_N test\n"); return TC_FAIL; } rv = pipePutHelperWork(zero_to_N, ARRAY_SIZE(zero_to_N), many_zero_to_N, ARRAY_SIZE(many_zero_to_N)); if (rv != TC_PASS) { TC_ERROR("Failed on _0_TO_N/many_0_TO_N test\n"); return TC_FAIL; } return TC_PASS; } /** * * @brief Test task_pipe_put(TICKS_NONE) * * This routine tests the task_pipe_put(TICKS_NONE) API. * * @param singleItems testcase list (one item in the pipe) * @param nSingles number of items in testcase * @param manyItems testcase list (many items in the pipe) * @param nMany number of items in testcase * * @return TC_PASS on success, TC_FAIL on failure */ int pipePutTestWork(SIZE_EXPECT *singleItems, int nSingles, SIZE_EXPECT *manyItems, int nMany) { int rv; /* return code from task_pipe_put() */ int i; /* loop counter */ int bytesWritten; /* # of bytes sent to pipe */ int nitem; /* expected item number processed by the other task */ task_sem_reset(counterSem); for (i = 0; i < nSingles; i++) { rv = task_pipe_put(pipeId, txBuffer, singleItems[i].size, &bytesWritten, singleItems[i].options, TICKS_NONE); if (rv != singleItems[i].rcode) { TC_ERROR("task_pipe_put(%d) : Expected %d not %d.\n" " bytesWritten = %d, Iteration: %d\n", singleItems[i].size, singleItems[i].rcode, rv, bytesWritten, i + 1); return TC_FAIL; } if (bytesWritten != singleItems[i].sent) { TC_ERROR("task_pipe_put(%d) : " "Expected %d bytes to be written, not %d\n", singleItems[i].size, singleItems[i].sent, bytesWritten); return TC_FAIL; } task_sem_give(altSem); (void)task_sem_take(regSem, TICKS_UNLIMITED); nitem = task_sem_count_get(counterSem) - 1; if (nitem != i) { TC_ERROR("Expected item number is %d, not %d\n", i, nitem); return TC_FAIL; } } /* This time, more than one item will be in the pipe at a time */ task_sem_reset(counterSem); for (i = 0; i < nMany; i++) { rv = task_pipe_put(pipeId, txBuffer, manyItems[i].size, &bytesWritten, manyItems[i].options, TICKS_NONE); if (rv != manyItems[i].rcode) { TC_ERROR("task_pipe_put(%d) : Expected %d not %d.\n" " bytesWritten = %d, iteration: %d\n", manyItems[i].size, manyItems[i].rcode, rv, bytesWritten, i + 1); return TC_FAIL; } if (bytesWritten != manyItems[i].sent) { TC_ERROR("task_pipe_put(%d) : " "Expected %d bytes to be written, not %d\n", manyItems[i].size, manyItems[i].sent, bytesWritten); return TC_FAIL; } } task_sem_give(altSem); /* Wake the alternate task */ /* wait for other task reading all the items from pipe */ (void)task_sem_take(regSem, TICKS_UNLIMITED); if (task_sem_count_get(counterSem) != nMany) { TC_ERROR("Expected number of items %d, not %d\n", nMany, task_sem_count_get(counterSem)); return TC_FAIL; } return TC_PASS; } /** * * @brief Test task_pipe_put(TICKS_NONE) * * This routine tests the task_pipe_put(TICKS_NONE) API. * * @return TC_PASS on success, TC_FAIL on failure */ int pipePutTest(void) { int rv; /* return value from pipePutTestWork() */ rv = pipePutTestWork(all_N, ARRAY_SIZE(all_N), many_all_N, ARRAY_SIZE(many_all_N)); if (rv != TC_PASS) { TC_ERROR("Failed on _ALL_N/many_ALL_N test\n"); return TC_FAIL; } rv = pipePutTestWork(one_to_N, ARRAY_SIZE(one_to_N), many_one_to_N, ARRAY_SIZE(many_one_to_N)); if (rv != TC_PASS) { TC_ERROR("Failed on _1_TO_N/many_1_TO_N test\n"); return TC_FAIL; } rv = pipePutTestWork(zero_to_N, ARRAY_SIZE(zero_to_N), many_zero_to_N, ARRAY_SIZE(many_zero_to_N)); if (rv != TC_PASS) { TC_ERROR("Failed on _0_TO_N/many_0_TO_N test\n"); return TC_FAIL; } return TC_PASS; } /** * * @brief Help test task_pipe_put(TICKS_UNLIMITED) * * @return TC_PASS on success, TC_FAIL on failure */ int pipePutWaitHelper(void) { int i; /* loop counter */ int rv; /* return value from task_pipe_get*/ int bytesRead; /* # of bytes read from task_pipe_get() */ /* Wait until test is ready */ (void)task_sem_take(altSem, TICKS_UNLIMITED); /* * 1. task_pipe_get(TICKS_UNLIMITED) will force a context * switch to RegressionTask(). */ rv = task_pipe_get(pipeId, rxBuffer, PIPE_SIZE, &bytesRead, _ALL_N, TICKS_UNLIMITED); if ((rv != RC_OK) || (bytesRead != PIPE_SIZE)) { TC_ERROR("Expected return code %d, not %d\n" "Expected %d bytes to be read, not %d\n", RC_OK, rv, PIPE_SIZE, bytesRead); return TC_FAIL; } /* * 2. task_pipe_get(TICKS_UNLIMITED) will force a context * switch to RegressionTask(). */ rv = task_pipe_get(pipeId, rxBuffer, PIPE_SIZE, &bytesRead, _1_TO_N, TICKS_UNLIMITED); if ((rv != RC_OK) || (bytesRead != PIPE_SIZE)) { TC_ERROR("Expected return code %d, not %d\n" "Expected %d bytes to be read, not %d\n", RC_OK, rv, PIPE_SIZE, bytesRead); return TC_FAIL; } /* * Before emptying the pipe, check that task_pipe_get(TICKS_UNLIMITED) * fails when using the _0_TO_N option. */ rv = task_pipe_get(pipeId, rxBuffer, PIPE_SIZE / 2, &bytesRead, _0_TO_N, TICKS_UNLIMITED); if (rv != RC_FAIL) { TC_ERROR("Expected return code %d, not %d\n", RC_FAIL, rv); return TC_FAIL; } /* 3. Empty the pipe in two reads */ for (i = 0; i < 2; i++) { rv = task_pipe_get(pipeId, rxBuffer, PIPE_SIZE / 2, &bytesRead, _0_TO_N, TICKS_NONE); if ((rv != RC_OK) || (bytesRead != PIPE_SIZE / 2)) { TC_ERROR("Expected return code %d, not %d\n" "Expected %d bytes to be read, not %d\n", RC_OK, rv, PIPE_SIZE / 2, bytesRead); return TC_FAIL; } } task_sem_give(regSem); return TC_PASS; } /** * * @brief Test task_pipe_put(TICKS_UNLIMITED) * * @return TC_PASS on success, TC_FAIL on failure */ int pipePutWaitTest(void) { int rv; /* return code from task_pipe_put() */ int bytesWritten; /* # of bytes written to pipe */ /* 1. Fill the pipe */ rv = task_pipe_put(pipeId, txBuffer, PIPE_SIZE, &bytesWritten, _ALL_N, TICKS_UNLIMITED); if ((rv != RC_OK) || (bytesWritten != PIPE_SIZE)) { TC_ERROR("Return code: expected %d, got %d\n" "Bytes written: expected %d, got %d\n", RC_OK, rv, PIPE_SIZE, bytesWritten); return TC_FAIL; } task_sem_give(altSem); /* Wake the alternate task */ /* 2. task_pipe_put() will force a context switch to AlternateTask(). */ rv = task_pipe_put(pipeId, txBuffer, PIPE_SIZE, &bytesWritten, _ALL_N, TICKS_UNLIMITED); if ((rv != RC_OK) || (bytesWritten != PIPE_SIZE)) { TC_ERROR("Return code: expected %d, got %d\n" "Bytes written: expected %d, got %d\n", RC_OK, rv, PIPE_SIZE, bytesWritten); return TC_FAIL; } /* 3. task_pipe_put() will force a context switch to AlternateTask(). */ rv = task_pipe_put(pipeId, txBuffer, PIPE_SIZE, &bytesWritten, _1_TO_N, TICKS_UNLIMITED); if ((rv != RC_OK) || (bytesWritten != PIPE_SIZE)) { TC_ERROR("Return code: expected %d, got %d\n" "Bytes written: expected %d, got %d\n", RC_OK, rv, PIPE_SIZE, bytesWritten); return TC_FAIL; } /* This should return immediately as _0_TO_N with a wait is an error. */ rv = task_pipe_put(pipeId, txBuffer, PIPE_SIZE, &bytesWritten, _0_TO_N, TICKS_UNLIMITED); if ((rv != RC_FAIL) || (bytesWritten != 0)) { TC_ERROR("Return code: expected %d, got %d\n" "Bytes written: expected %d, got %d\n", RC_FAIL, rv, 0, bytesWritten); return TC_FAIL; } /* Wait for AlternateTask()'s pipePutWaitHelper() to finish */ (void)task_sem_take(regSem, TICKS_UNLIMITED); return TC_PASS; } /** * * @brief Test task_pipe_get(timeout) * * @return TC_PASS on success, TC_FAIL on failure */ int pipePutTimeoutHelper(void) { int i; /* loop counter */ int rv; /* return value from task_pipe_get() */ int bytesRead; /* # of bytes read from task_pipe_get() */ /* Wait until test is ready */ (void)task_sem_take(altSem, TICKS_UNLIMITED); /* * 1. task_pipe_get(timeout) will force a context * switch to RegressionTask() */ rv = task_pipe_get(pipeId, rxBuffer, PIPE_SIZE, &bytesRead, _ALL_N, ONE_SECOND); if ((rv != RC_OK) || (bytesRead != PIPE_SIZE)) { TC_ERROR("Expected return code %d, not %d\n" "Expected %d bytes to be read, not %d\n", RC_OK, rv, PIPE_SIZE, bytesRead); return TC_FAIL; } /* * 2. task_pipe_get(timeout) will force a context * switch to RegressionTask(). */ rv = task_pipe_get(pipeId, rxBuffer, PIPE_SIZE, &bytesRead, _1_TO_N, ONE_SECOND); if ((rv != RC_OK) || (bytesRead != PIPE_SIZE)) { TC_ERROR("Expected return code %d, not %d\n" "Expected %d bytes to be read, not %d\n", RC_OK, rv, PIPE_SIZE, bytesRead); return TC_FAIL; } /* * Before emptying the pipe, check that task_pipe_get(timeout) fails when * using the _0_TO_N option. */ rv = task_pipe_get(pipeId, rxBuffer, PIPE_SIZE / 2, &bytesRead, _0_TO_N, ONE_SECOND); if (rv != RC_FAIL) { TC_ERROR("Expected return code %d, not %d\n", RC_FAIL, rv); return TC_FAIL; } /* 3. Empty the pipe in two reads */ for (i = 0; i < 2; i++) { rv = task_pipe_get(pipeId, rxBuffer, PIPE_SIZE / 2, &bytesRead, _0_TO_N, TICKS_NONE); if ((rv != RC_OK) || (bytesRead != PIPE_SIZE / 2)) { TC_ERROR("Expected return code %d, not %d\n" "Expected %d bytes to be read, not %d\n", RC_OK, rv, PIPE_SIZE / 2, bytesRead); return TC_FAIL; } } task_sem_give(regSem); return TC_PASS; } /** * * @brief Test task_pipe_put(timeout) * * @return TC_PASS on success, TC_FAIL on failure */ int pipePutTimeoutTest(void) { int rv; /* return code from task_pipe_put() */ int bytesWritten; /* # of bytes written to task_pipe_put() */ /* 1. Fill the pipe */ rv = task_pipe_put(pipeId, txBuffer, PIPE_SIZE, &bytesWritten, _ALL_N, ONE_SECOND); if ((rv != RC_OK) || (bytesWritten != PIPE_SIZE)) { TC_ERROR("Return code: expected %d, got %d\n" "Bytes written: expected %d, got %d\n", RC_OK, rv, PIPE_SIZE, bytesWritten); return TC_FAIL; } /* Timeout while waiting to put data into the pipe */ rv = task_pipe_put(pipeId, txBuffer, PIPE_SIZE, &bytesWritten, _ALL_N, ONE_SECOND); if ((rv != RC_TIME) || (bytesWritten != 0)) { TC_ERROR("Return code: expected %d, got %d\n" "Bytes written: expected %d, got %d\n", RC_TIME, rv, 0, bytesWritten); return TC_FAIL; } rv = task_pipe_put(pipeId, txBuffer, PIPE_SIZE, &bytesWritten, _1_TO_N, ONE_SECOND); if ((rv != RC_TIME) || (bytesWritten != 0)) { TC_ERROR("Return code: expected %d, got %d\n" "Bytes written: expected %d, got %d\n", RC_TIME, rv, 0, bytesWritten); return TC_FAIL; } task_sem_give(altSem); /* Wake the alternate task */ /* 2. task_pipe_put() will force a context switch to AlternateTask(). */ rv = task_pipe_put(pipeId, txBuffer, PIPE_SIZE, &bytesWritten, _ALL_N, ONE_SECOND); if ((rv != RC_OK) || (bytesWritten != PIPE_SIZE)) { TC_ERROR("Return code: expected %d, got %d\n" "Bytes written: expected %d, got %d\n", RC_OK, rv, PIPE_SIZE, bytesWritten); return TC_FAIL; } /* 3. task_pipe_put() will force a context switch to AlternateTask(). */ rv = task_pipe_put(pipeId, txBuffer, PIPE_SIZE, &bytesWritten, _1_TO_N, ONE_SECOND); if ((rv != RC_OK) || (bytesWritten != PIPE_SIZE)) { TC_ERROR("Return code: expected %d, got %d\n" "Bytes written: expected %d, got %d\n", RC_OK, rv, PIPE_SIZE, bytesWritten); return TC_FAIL; } /* This should return immediately as _0_TO_N with a wait is an error. */ rv = task_pipe_put(pipeId, txBuffer, PIPE_SIZE, &bytesWritten, _0_TO_N, TICKS_UNLIMITED); if ((rv != RC_FAIL) || (bytesWritten != 0)) { TC_ERROR("Return code: expected %d, got %d\n" "Bytes written: expected %d, got %d\n", RC_FAIL, rv, 0, bytesWritten); return TC_FAIL; } /* Wait for AlternateTask()'s pipePutWaitHelper() to finish */ (void)task_sem_take(regSem, TICKS_UNLIMITED); return TC_PASS; } /** * * @brief Routine to test task_pipe_get(TICKS_NONE) * * This routine tests the task_pipe_get(TICKS_NONE) API. Some of this * functionality has already been tested while testing task_pipe_put(). As * a result, the only remaining functionality that needs to be checked are * attempts to get data from an empty pipe. * * @return TC_PASS on success, TC_FAIL on failure */ int pipeGetTest(void) { int i; /* loop counter */ int j; /* loop counter */ int rv; /* return code from task_pipe_get() */ int bytesRead; /* # of bytes read from task_pipe_get() */ int size[] ={1, PIPE_SIZE - 1, PIPE_SIZE, PIPE_SIZE + 1}; K_PIPE_OPTION options[] = {_ALL_N, _1_TO_N}; for (j = 0; j < ARRAY_SIZE(options); j++) { for (i = 0; i < ARRAY_SIZE(size); i++) { rv = task_pipe_get(pipeId, rxBuffer, size[i], &bytesRead, options[j], TICKS_NONE); if (rv != RC_FAIL) { TC_ERROR("Expected return code %d, not %d\n", RC_FAIL, rv); return TC_FAIL; } } } for (i = 0; i < ARRAY_SIZE(size); i++) { rv = task_pipe_get(pipeId, rxBuffer, size[i], &bytesRead, _0_TO_N, TICKS_NONE); if (rv != RC_OK) { TC_ERROR("Expected return code %d, not %d\n", RC_OK, rv); return TC_FAIL; } if (bytesRead != 0) { TC_ERROR("Expected <bytesRead> %d, not %d\n", 0, bytesRead); return TC_FAIL; } } return TC_PASS; } /** * * @brief Test task_pipe_get(TICKS_UNLIMITED) * * @param items testcase list for task_pipe_get(TICKS_UNLIMITED) * @param nItems number of items in list * * @return TC_PASS on success, TC_FAIL on failure */ int pipeGetWaitHelperWork(SIZE_EXPECT *items, int nItems) { int i; /* loop counter */ int rv; /* return value from task_pipe_put() */ int bytesSent; /* # of bytes sent to task_pipe_put() */ for (i = 0; i < nItems; i++) { /* * Pipe should be empty. Most calls to task_pipe_get(TICKS_UNLIMITED) * should block until the call to task_pipe_put() is performed in the * routine pipeGetWaitHelperWork(). */ bytesSent = 0; rv = task_pipe_put(pipeId, rxBuffer, items[i].size, &bytesSent, items[i].options, TICKS_UNLIMITED); if ((rv != items[i].rcode) || (bytesSent != items[i].sent)) { TC_ERROR("Expected return value %d, got %d\n" "Expected bytesSent = %d, got %d\n", items[i].rcode, rv, 0, bytesSent); return TC_FAIL; } } return TC_PASS; } /** * * @brief Test task_pipe_get(TICKS_UNLIMITED) * * @return TC_PASS on success, TC_FAIL on failure */ int pipeGetWaitHelper(void) { int rv; /* return coded form pipeGetWaitHelperWork() */ (void)task_sem_take(altSem, TICKS_UNLIMITED); rv = pipeGetWaitHelperWork(wait_all_N, ARRAY_SIZE(wait_all_N)); if (rv != TC_PASS) { TC_ERROR("Failed on _ALL_N test\n"); return TC_FAIL; } rv = pipeGetWaitHelperWork(wait_one_to_N, ARRAY_SIZE(wait_one_to_N)); if (rv != TC_PASS) { TC_ERROR("Failed on _1_TO_N test\n"); return TC_FAIL; } return TC_PASS; } /** * * @brief Test task_pipe_get(TICKS_UNLIMITED) * * @param items testcase list for task_pipe_get(TICKS_UNLIMITED) * @param nItems number of items in list * * @return TC_PASS on success, TC_FAIL on failure */ int pipeGetWaitTestWork(SIZE_EXPECT *items, int nItems) { int i; /* loop counter */ int rv; /* return code from task_pipe_get() */ int bytesRead; /* # of bytes read from task_pipe_get() */ for (i = 0; i < nItems; i++) { /* * Pipe should be empty. Most calls to task_pipe_get(TICKS_UNLIMITED) * should block until the call to task_pipe_put() is performed in the * routine pipeGetWaitHelperWork(). */ rv = task_pipe_get(pipeId, rxBuffer, items[i].size, &bytesRead, items[i].options, TICKS_UNLIMITED); if ((rv != items[i].rcode) || (bytesRead != items[i].sent)) { TC_ERROR("Expected return value %d, got %d\n" "Expected bytesRead = %d, got %d\n", items[i].rcode, rv, 0, bytesRead); return TC_FAIL; } } return TC_PASS; } /** * * @brief Test task_pipe_get(TICKS_UNLIMITED) * * @return TC_PASS on success, TC_FAIL on failure */ int pipeGetWaitTest(void) { int rv; /* return code from pipeGetWaitTestWork() */ int bytesRead; /* # of bytes read from task_pipe_get_waitait() */ task_sem_give(altSem); /* Wake AlternateTask */ rv = pipeGetWaitTestWork(wait_all_N, ARRAY_SIZE(wait_all_N)); if (rv != TC_PASS) { TC_ERROR("Failed on _ALL_N test\n"); return TC_FAIL; } rv = pipeGetWaitTestWork(wait_one_to_N, ARRAY_SIZE(wait_one_to_N)); if (rv != TC_PASS) { TC_ERROR("Failed on _1_TO_N test\n"); return TC_FAIL; } rv = task_pipe_get(pipeId, rxBuffer, PIPE_SIZE, &bytesRead, _0_TO_N, TICKS_UNLIMITED); if (rv != RC_FAIL) { TC_ERROR("Expected return code of %d, not %d\n", RC_FAIL, rv); return TC_FAIL; } return TC_PASS; } /** * * @brief Test remaining task_pipe_get(timeout) functionality * * @return TC_PASS on success, TC_FAIL on failure */ int pipeGetTimeoutTest(void) { int i; /* loop counter */ int rv; /* return value from task_pipe_get() */ int bytesRead; /* # of bytes read from task_pipe_get() */ for (i = 0; i < ARRAY_SIZE(timeout_cases); i++) { rv = task_pipe_get(pipeId, rxBuffer, timeout_cases[i].size, &bytesRead, timeout_cases[i].options, ONE_SECOND); if ((rv != timeout_cases[i].rcode) || (bytesRead != timeout_cases[i].sent)) { TC_ERROR("Expected return code %d, got %d\n" "Expected <bytesRead> %d, got %d\n" "Iteration %d\n", timeout_cases[i].rcode, rv, timeout_cases[i].sent, bytesRead, i + 1); return TC_FAIL; } } return TC_PASS; } /** * * @brief Alternate task in the test suite * * This routine runs at a lower priority than RegressionTask(). * * @return TC_PASS or TC_FAIL */ int AlternateTask(void) { int rv; /* return code from each set of test cases */ rv = pipePutHelper(); if (rv != TC_PASS) { return TC_FAIL; } rv = pipePutWaitHelper(); if (rv != TC_PASS) { return TC_FAIL; } rv = pipePutTimeoutHelper(); if (rv != TC_PASS) { return TC_FAIL; } /* * There is no pipeGetHelper() as the task_pipe_get() checks have * either been done in pipePutHelper(), or pipeGetTest(). */ rv = pipeGetWaitHelper(); if (rv != TC_PASS) { return TC_FAIL; } /* * There is no pipeGetTimeoutHelper() as the task_pipe_get(timeout) checks * have either been done in pipePutTimeoutHelper() or * pipeGetTimeoutTest(). */ return TC_PASS; } /** * * @brief Main task in the test suite * * This is the entry point to the pipe test suite. * * @return TC_PASS or TC_FAIL */ int RegressionTask(void) { int tcRC; /* test case return code */ microObjectsInit(); TC_PRINT("Testing task_pipe_put(TICKS_NONE) ...\n"); tcRC = pipePutTest(); if (tcRC != TC_PASS) { return TC_FAIL; } TC_PRINT("Testing task_pipe_put(TICKS_UNLIMITED) ...\n"); tcRC = pipePutWaitTest(); if (tcRC != TC_PASS) { return TC_FAIL; } TC_PRINT("Testing task_pipe_put(timeout) ...\n"); tcRC = pipePutTimeoutTest(); if (tcRC != TC_PASS) { return TC_FAIL; } TC_PRINT("Testing task_pipe_get(TICKS_NONE) ...\n"); tcRC = pipeGetTest(); if (tcRC != TC_PASS) { return TC_FAIL; } TC_PRINT("Testing task_pipe_get(TICKS_UNLIMITED) ...\n"); tcRC = pipeGetWaitTest(); if (tcRC != TC_PASS) { return TC_FAIL; } TC_PRINT("Testing task_pipe_get(timeout) ...\n"); tcRC = pipeGetTimeoutTest(); if (tcRC != TC_PASS) { return TC_FAIL; } return TC_PASS; } |