Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

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
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#include <zephyr/ztest.h>

/**
 * @brief Define and initialize test_pipe at compile time
 */
K_PIPE_DEFINE(test_pipe, 256, 4);
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
#define PIPE_SIZE (256)

K_PIPE_DEFINE(small_pipe, 10, 4);

K_THREAD_STACK_DEFINE(stack_1, STACK_SIZE);

K_SEM_DEFINE(get_sem, 0, 1);
K_SEM_DEFINE(put_sem, 1, 1);
K_SEM_DEFINE(sync_sem, 0, 1);
K_SEM_DEFINE(multiple_send_sem, 0, 1);

ZTEST_BMEM uint8_t tx_buffer[PIPE_SIZE + 1];
ZTEST_BMEM uint8_t rx_buffer[PIPE_SIZE + 1];

#define TOTAL_ELEMENTS (sizeof(single_elements) / sizeof(struct pipe_sequence))
#define TOTAL_WAIT_ELEMENTS (sizeof(wait_elements) / \
			     sizeof(struct pipe_sequence))
#define TOTAL_TIMEOUT_ELEMENTS (sizeof(timeout_elements) / \
				sizeof(struct pipe_sequence))

/* Minimum tx/rx size*/
/* the pipe will always pass */
#define NO_CONSTRAINT (0U)

/* Pipe will at least put one byte */
#define ATLEAST_1 (1U)

/* Pipe must put all data on the buffer */
#define ALL_BYTES (sizeof(tx_buffer))

#define RETURN_SUCCESS  (0)
#define TIMEOUT_VAL (K_MSEC(10))
#define TIMEOUT_200MSEC (K_MSEC(200))

/* encompassing structs */
struct pipe_sequence {
	uint32_t size;
	uint32_t min_size;
	uint32_t sent_bytes;
	int return_value;
};

static const struct pipe_sequence single_elements[] = {
	{ 0, ALL_BYTES, 0, 0 },
	{ 1, ALL_BYTES, 1, RETURN_SUCCESS },
	{ PIPE_SIZE - 1, ALL_BYTES, PIPE_SIZE - 1, RETURN_SUCCESS },
	{ PIPE_SIZE, ALL_BYTES, PIPE_SIZE, RETURN_SUCCESS },
	{ PIPE_SIZE + 1, ALL_BYTES, 0, -EIO },
	/* minimum 1 byte */
	/* {0, ATLEAST_1, 0, -EIO}, */
	{ 1, ATLEAST_1, 1, RETURN_SUCCESS },
	{ PIPE_SIZE - 1, ATLEAST_1, PIPE_SIZE - 1, RETURN_SUCCESS },
	{ PIPE_SIZE, ATLEAST_1, PIPE_SIZE, RETURN_SUCCESS },
	{ PIPE_SIZE + 1, ATLEAST_1, PIPE_SIZE, RETURN_SUCCESS },
	/* /\* any number of bytes *\/ */
	{ 0, NO_CONSTRAINT, 0, 0 },
	{ 1, NO_CONSTRAINT, 1, RETURN_SUCCESS },
	{ PIPE_SIZE - 1, NO_CONSTRAINT, PIPE_SIZE - 1, RETURN_SUCCESS },
	{ PIPE_SIZE, NO_CONSTRAINT, PIPE_SIZE, RETURN_SUCCESS },
	{ PIPE_SIZE + 1, NO_CONSTRAINT, PIPE_SIZE, RETURN_SUCCESS }
};

static const struct pipe_sequence multiple_elements[] = {
	{ PIPE_SIZE / 3, ALL_BYTES, PIPE_SIZE / 3, RETURN_SUCCESS, },
	{ PIPE_SIZE / 3, ALL_BYTES, PIPE_SIZE / 3, RETURN_SUCCESS, },
	{ PIPE_SIZE / 3, ALL_BYTES, PIPE_SIZE / 3, RETURN_SUCCESS, },
	{ PIPE_SIZE / 3, ALL_BYTES, 0, -EIO },

	{ PIPE_SIZE / 3, ATLEAST_1, PIPE_SIZE / 3, RETURN_SUCCESS },
	{ PIPE_SIZE / 3, ATLEAST_1, PIPE_SIZE / 3, RETURN_SUCCESS },
	{ PIPE_SIZE / 3, ATLEAST_1, PIPE_SIZE / 3, RETURN_SUCCESS },
	{ PIPE_SIZE / 3, ATLEAST_1, 1, RETURN_SUCCESS },
	{ PIPE_SIZE / 3, ATLEAST_1, 0, -EIO },

	{ PIPE_SIZE / 3, NO_CONSTRAINT, PIPE_SIZE / 3, RETURN_SUCCESS },
	{ PIPE_SIZE / 3, NO_CONSTRAINT, PIPE_SIZE / 3, RETURN_SUCCESS },
	{ PIPE_SIZE / 3, NO_CONSTRAINT, PIPE_SIZE / 3, RETURN_SUCCESS },
	{ PIPE_SIZE / 3, NO_CONSTRAINT, 1, RETURN_SUCCESS },
	{ PIPE_SIZE / 3, NO_CONSTRAINT, 0, RETURN_SUCCESS }
};

static const struct pipe_sequence wait_elements[] = {
	{            1, ALL_BYTES,             1, RETURN_SUCCESS },
	{ PIPE_SIZE - 1, ALL_BYTES, PIPE_SIZE - 1, RETURN_SUCCESS },
	{    PIPE_SIZE, ALL_BYTES,     PIPE_SIZE, RETURN_SUCCESS },
	{ PIPE_SIZE + 1, ALL_BYTES, PIPE_SIZE + 1, RETURN_SUCCESS },

	{ PIPE_SIZE - 1, ATLEAST_1, PIPE_SIZE - 1, RETURN_SUCCESS },
};

static const struct pipe_sequence timeout_elements[] = {
	{            0, ALL_BYTES, 0, 0 },
	{            1, ALL_BYTES, 0, -EAGAIN },
	{ PIPE_SIZE - 1, ALL_BYTES, 0, -EAGAIN },
	{    PIPE_SIZE, ALL_BYTES, 0, -EAGAIN },
	{ PIPE_SIZE + 1, ALL_BYTES, 0, -EAGAIN },

	{            1, ATLEAST_1, 0, -EAGAIN },
	{ PIPE_SIZE - 1, ATLEAST_1, 0, -EAGAIN },
	{    PIPE_SIZE, ATLEAST_1, 0, -EAGAIN },
	{ PIPE_SIZE + 1, ATLEAST_1, 0, -EAGAIN }
};

struct k_thread get_single_tid;

/* Helper functions */

uint32_t rx_buffer_check(char *buffer, uint32_t size)
{
	uint32_t index;

	for (index = 0U; index < size; index++) {
		if (buffer[index] != (char) index) {
			printk("buffer[index] = %d index = %d\n",
			       buffer[index], (char) index);
			return index;
		}
	}

	return size;
}


/******************************************************************************/
void pipe_put_single(void)
{
	uint32_t index;
	size_t written;
	int return_value;
	size_t min_xfer;

	for (index = 0U; index < TOTAL_ELEMENTS; index++) {
		k_sem_take(&put_sem, K_FOREVER);

		min_xfer = (single_elements[index].min_size == ALL_BYTES ?
			    single_elements[index].size :
			    single_elements[index].min_size);

		return_value = k_pipe_put(&test_pipe, &tx_buffer,
					  single_elements[index].size, &written,
					  min_xfer, K_NO_WAIT);

		zassert_true((return_value ==
			      single_elements[index].return_value),
			     " Return value of k_pipe_put mismatch at index = %d expected =%d received = %d\n",
			     index,
			     single_elements[index].return_value, return_value);

		zassert_true((written == single_elements[index].sent_bytes),
			     "Bytes written mismatch written is %d but expected is %d index = %d\n",
			     written,
			     single_elements[index].sent_bytes, index);

		k_sem_give(&get_sem);
	}

}

void pipe_get_single(void *p1, void *p2, void *p3)
{
	uint32_t index;
	size_t read;
	int return_value;
	size_t min_xfer;

	for (index = 0U; index < TOTAL_ELEMENTS; index++) {
		k_sem_take(&get_sem, K_FOREVER);

		/* reset the rx buffer for the next interation */
		(void)memset(rx_buffer, 0, sizeof(rx_buffer));

		min_xfer = (single_elements[index].min_size == ALL_BYTES ?
			    single_elements[index].size :
			    single_elements[index].min_size);

		return_value = k_pipe_get(&test_pipe, &rx_buffer,
					  single_elements[index].size, &read,
					  min_xfer, K_NO_WAIT);


		zassert_true((return_value ==
			      single_elements[index].return_value),
			     "Return value of k_pipe_get mismatch at index = %d expected =%d received = %d\n",
			     index, single_elements[index].return_value,
			     return_value);

		zassert_true((read == single_elements[index].sent_bytes),
			     "Bytes read mismatch read is %d but expected is %d index = %d\n",
			     read, single_elements[index].sent_bytes, index);

		zassert_true(rx_buffer_check(rx_buffer, read) == read,
			     "Bytes read are not matching at index= %d\n expected =%d but received= %d",
			     index, read, rx_buffer_check(rx_buffer, read));
		k_sem_give(&put_sem);
	}
	k_sem_give(&sync_sem);
}

/******************************************************************************/
void pipe_put_multiple(void)
{
	uint32_t index;
	size_t written;
	int return_value;
	size_t min_xfer;

	for (index = 0U; index < TOTAL_ELEMENTS; index++) {

		min_xfer = (multiple_elements[index].min_size == ALL_BYTES ?
			    multiple_elements[index].size :
			    multiple_elements[index].min_size);

		return_value = k_pipe_put(&test_pipe, &tx_buffer,
					  multiple_elements[index].size,
					  &written,
					  min_xfer, K_NO_WAIT);

		zassert_true((return_value ==
			      multiple_elements[index].return_value),
			     "Return value of k_pipe_put mismatch at index = %d expected =%d received = %d\n",
			     index,
			     multiple_elements[index].return_value,
			     return_value);

		zassert_true((written == multiple_elements[index].sent_bytes),
			     "Bytes written mismatch written is %d but expected is %d index = %d\n",
			     written,
			     multiple_elements[index].sent_bytes, index);
		if (return_value != RETURN_SUCCESS) {
			k_sem_take(&multiple_send_sem, K_FOREVER);
		}

	}

}

void pipe_get_multiple(void *p1, void *p2, void *p3)
{
	uint32_t index;
	size_t read;
	int return_value;
	size_t min_xfer;

	for (index = 0U; index < TOTAL_ELEMENTS; index++) {


		/* reset the rx buffer for the next interation */
		(void)memset(rx_buffer, 0, sizeof(rx_buffer));

		min_xfer = (multiple_elements[index].min_size == ALL_BYTES ?
			    multiple_elements[index].size :
			    multiple_elements[index].min_size);

		return_value = k_pipe_get(&test_pipe, &rx_buffer,
					  multiple_elements[index].size, &read,
					  min_xfer, K_NO_WAIT);


		zassert_true((return_value ==
			      multiple_elements[index].return_value),
			     "Return value of k_pipe_get mismatch at index = %d expected =%d received = %d\n",
			     index, multiple_elements[index].return_value,
			     return_value);

		zassert_true((read == multiple_elements[index].sent_bytes),
			     "Bytes read mismatch read is %d but expected is %d index = %d\n",
			     read, multiple_elements[index].sent_bytes, index);

		zassert_true(rx_buffer_check(rx_buffer, read) == read,
			     "Bytes read are not matching at index= %d\n expected =%d but received= %d",
			     index, read, rx_buffer_check(rx_buffer, read));

		if (return_value != RETURN_SUCCESS) {
			k_sem_give(&multiple_send_sem);
		}

	}
	k_sem_give(&sync_sem);
}

/******************************************************************************/

void pipe_put_forever_wait(void)
{
	size_t written;
	int return_value;

	/* 1. fill the pipe. */
	return_value = k_pipe_put(&test_pipe, &tx_buffer,
				  PIPE_SIZE, &written,
				  PIPE_SIZE, K_FOREVER);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_put failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(written == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, written);


	/* wake up the get task */
	k_sem_give(&get_sem);
	/* 2. k_pipe_put() will force a context switch to the other thread. */
	return_value = k_pipe_put(&test_pipe, &tx_buffer,
				  PIPE_SIZE, &written,
				  PIPE_SIZE, K_FOREVER);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_put failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(written == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, written);

	/* 3. k_pipe_put() will force a context switch to the other thread. */
	return_value = k_pipe_put(&test_pipe, &tx_buffer,
				  PIPE_SIZE, &written,
				  ATLEAST_1, K_FOREVER);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_put failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(written == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, written);

}


void pipe_get_forever_wait(void *pi, void *p2, void *p3)
{
	size_t read;
	int return_value;

	/* get blocked until put forces the execution to come here */
	k_sem_take(&get_sem, K_FOREVER);

	/* k_pipe_get will force a context switch to the put function. */
	return_value = k_pipe_get(&test_pipe, &rx_buffer,
				  PIPE_SIZE, &read,
				  PIPE_SIZE, K_FOREVER);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_get failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(read == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, read);

	/* k_pipe_get will force a context switch to the other thread. */
	return_value = k_pipe_get(&test_pipe, &rx_buffer,
				  PIPE_SIZE, &read,
				  ATLEAST_1, K_FOREVER);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_get failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(read == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, read);

	/*3. last read to clear the pipe */
	return_value = k_pipe_get(&test_pipe, &rx_buffer,
				  PIPE_SIZE, &read,
				  ATLEAST_1, K_FOREVER);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_get failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(read == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, read);


	k_sem_give(&sync_sem);

}


/******************************************************************************/
void pipe_put_timeout(void)
{
	size_t written;
	int return_value;

	/* 1. fill the pipe. */
	return_value = k_pipe_put(&test_pipe, &tx_buffer,
				  PIPE_SIZE, &written,
				  PIPE_SIZE, TIMEOUT_VAL);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_put failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(written == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, written);


	/* pipe put cant be satisfied and thus timeout */
	return_value = k_pipe_put(&test_pipe, &tx_buffer,
				  PIPE_SIZE, &written,
				  PIPE_SIZE, TIMEOUT_VAL);

	zassert_true(return_value == -EAGAIN,
		     "k_pipe_put failed expected = -EAGAIN received = %d\n",
		     return_value);

	zassert_true(written == 0,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, written);

	/* Try once more with 1 byte pipe put cant be satisfied and
	 * thus timeout.
	 */
	return_value = k_pipe_put(&test_pipe, &tx_buffer,
				  PIPE_SIZE, &written,
				  ATLEAST_1, TIMEOUT_VAL);

	zassert_true(return_value == -EAGAIN,
		     "k_pipe_put failed expected = -EAGAIN received = %d\n",
		     return_value);

	zassert_true(written == 0,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, written);

	k_sem_give(&get_sem);

	/* 2. pipe_get thread will now accept this data  */
	return_value = k_pipe_put(&test_pipe, &tx_buffer,
				  PIPE_SIZE, &written,
				  PIPE_SIZE, TIMEOUT_VAL);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_put failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(written == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, written);

	/* 3. pipe_get thread will now accept this data  */
	return_value = k_pipe_put(&test_pipe, &tx_buffer,
				  PIPE_SIZE, &written,
				  ATLEAST_1, TIMEOUT_VAL);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_put failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(written == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, written);
}


void pipe_get_timeout(void *pi, void *p2, void *p3)
{
	size_t read;
	int return_value;

	/* get blocked until put forces the execution to come here */
	k_sem_take(&get_sem, K_FOREVER);

	/* k_pipe_get will do a context switch to the put function. */
	return_value = k_pipe_get(&test_pipe, &rx_buffer,
				  PIPE_SIZE, &read,
				  PIPE_SIZE, TIMEOUT_VAL);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_get failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(read == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, read);

	/* k_pipe_get will do a context switch to the put function. */
	return_value = k_pipe_get(&test_pipe, &rx_buffer,
				  PIPE_SIZE, &read,
				  ATLEAST_1, TIMEOUT_VAL);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_get failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(read == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, read);

	/* cleanup the pipe */
	return_value = k_pipe_get(&test_pipe, &rx_buffer,
				  PIPE_SIZE, &read,
				  ATLEAST_1, TIMEOUT_VAL);

	zassert_true(return_value == RETURN_SUCCESS,
		     "k_pipe_get failed expected = 0 received = %d\n",
		     return_value);

	zassert_true(read == PIPE_SIZE,
		     "k_pipe_put written failed expected = %d received = %d\n",
		     PIPE_SIZE, read);

	k_sem_give(&sync_sem);
}


/******************************************************************************/
void pipe_get_on_empty_pipe(void)
{
	size_t read;
	int return_value;
	uint32_t read_size;
	uint32_t size_array[] = { 1, PIPE_SIZE - 1, PIPE_SIZE, PIPE_SIZE + 1 };

	for (int i = 0; i < 4; i++) {
		read_size = size_array[i];
		return_value = k_pipe_get(&test_pipe, &rx_buffer,
					  read_size, &read,
					  read_size, K_NO_WAIT);

		zassert_true(return_value == -EIO,
			     "k_pipe_get failed expected = -EIO received = %d\n",
			     return_value);

		return_value = k_pipe_get(&test_pipe, &rx_buffer,
					  read_size, &read,
					  ATLEAST_1, K_NO_WAIT);

		zassert_true(return_value == -EIO,
			     "k_pipe_get failed expected = -EIO received = %d\n",
			     return_value);

		return_value = k_pipe_get(&test_pipe, &rx_buffer,
					  read_size, &read,
					  NO_CONSTRAINT, K_NO_WAIT);

		zassert_true(return_value == RETURN_SUCCESS,
			     "k_pipe_get failed expected = 0 received = %d\n",
			     return_value);

		zassert_true(read == 0,
			     "k_pipe_put written failed expected = %d received = %d\n",
			     PIPE_SIZE, read);
	}

}


/******************************************************************************/
void pipe_put_forever_timeout(void)
{
	uint32_t index;
	size_t written;
	int return_value;
	size_t min_xfer;

	/* using this to synchronize the 2 threads  */
	k_sem_take(&put_sem, K_FOREVER);

	for (index = 0U; index < TOTAL_WAIT_ELEMENTS; index++) {

		min_xfer = (wait_elements[index].min_size == ALL_BYTES ?
			    wait_elements[index].size :
			    wait_elements[index].min_size);

		return_value = k_pipe_put(&test_pipe, &tx_buffer,
					  wait_elements[index].size, &written,
					  min_xfer, K_FOREVER);

		zassert_true((return_value ==
			      wait_elements[index].return_value),
			     "Return value of k_pipe_put mismatch at index = %d expected =%d received = %d\n",
			     index, wait_elements[index].return_value,
			     return_value);

		zassert_true((written == wait_elements[index].sent_bytes),
			     "Bytes written mismatch written is %d but expected is %d index = %d\n",
			     written, wait_elements[index].sent_bytes, index);

	}

}

void pipe_get_forever_timeout(void *p1, void *p2, void *p3)
{
	uint32_t index;
	size_t read;
	int return_value;
	size_t min_xfer;

	/* using this to synchronize the 2 threads  */
	k_sem_give(&put_sem);
	for (index = 0U; index < TOTAL_WAIT_ELEMENTS; index++) {

		min_xfer = (wait_elements[index].min_size == ALL_BYTES ?
			    wait_elements[index].size :
			    wait_elements[index].min_size);

		return_value = k_pipe_get(&test_pipe, &rx_buffer,
					  wait_elements[index].size, &read,
					  min_xfer, K_FOREVER);


		zassert_true((return_value ==
			      wait_elements[index].return_value),
			     "Return value of k_pipe_get mismatch at index = %d expected =%d received = %d\n",
			     index, wait_elements[index].return_value,
			     return_value);

		zassert_true((read == wait_elements[index].sent_bytes),
			     "Bytes read mismatch read is %d but expected is %d index = %d\n",
			     read, wait_elements[index].sent_bytes, index);


	}
	k_sem_give(&sync_sem);
}

/******************************************************************************/
void pipe_put_get_timeout(void)
{
	uint32_t index;
	size_t read;
	int return_value;
	size_t min_xfer;

	for (index = 0U; index < TOTAL_TIMEOUT_ELEMENTS; index++) {

		min_xfer = (timeout_elements[index].min_size == ALL_BYTES ?
			    timeout_elements[index].size :
			    timeout_elements[index].min_size);

		return_value = k_pipe_get(&test_pipe, &rx_buffer,
					  timeout_elements[index].size, &read,
					  min_xfer, TIMEOUT_200MSEC);


		zassert_true((return_value ==
			      timeout_elements[index].return_value),
			     "Return value of k_pipe_get mismatch at index = %d expected =%d received = %d\n",
			     index, timeout_elements[index].return_value,
			     return_value);

		zassert_true((read == timeout_elements[index].sent_bytes),
			     "Bytes read mismatch read is %d but expected is %d index = %d\n",
			     read, timeout_elements[index].sent_bytes, index);


	}

}

/******************************************************************************/
ZTEST_BMEM bool valid_fault;
void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf)
{
	printk("Caught system error -- reason %d\n", reason);
	if (valid_fault) {
		valid_fault = false; /* reset back to normal */
		ztest_test_pass();
	} else {
		printk("PROJECT EXECUTION FAILED\n");
		k_fatal_halt(reason);
	}
}
/******************************************************************************/
/* Test case entry points */

/**
 * @brief Verify pipe with 1 element insert.
 *
 * @ingroup kernel_pipe_tests
 *
 * @details
 * Test Objective:
 * - transfer sequences of bytes of data in whole.
 *
 * Testing techniques:
 * - function and block box testing,Interface testing,
 * Dynamic analysis and testing.
 *
 * Prerequisite Conditions:
 * - CONFIG_TEST_USERSPACE.
 *
 * Input Specifications:
 * - N/A
 *
 * Test Procedure:
 * -# Define and initialize a pipe at compile time.
 * -# Using a sub thread to get pipe data in whole,
 * and check if the data is correct with expected.
 * -# Using main thread to put data in whole,
 * check if the return is correct with expected.
 *
 * Expected Test Result:
 * - The pipe put/get whole data is correct.
 *
 * Pass/Fail Criteria:
 * - Successful if check points in test procedure are all passed, otherwise failure.
 *
 * Assumptions and Constraints:
 * - N/A
 *
 * @see k_pipe_put(), k_pipe_get()
 */
ZTEST_USER(pipe, test_pipe_on_single_elements)
{
	/* initialize the tx buffer */
	for (int i = 0; i < sizeof(tx_buffer); i++) {
		tx_buffer[i] = i;
	}

	k_thread_create(&get_single_tid, stack_1, STACK_SIZE,
			pipe_get_single, NULL, NULL, NULL,
			K_PRIO_PREEMPT(0), K_INHERIT_PERMS | K_USER,
			K_NO_WAIT);

	pipe_put_single();
	k_sem_take(&sync_sem, K_FOREVER);
	k_thread_abort(&get_single_tid);
	ztest_test_pass();
}

/**
 * @brief Test when multiple items are present in the pipe
 * @details transfer sequences of bytes of data in part.
 * - Using a sub thread to get data part.
 * - Using main thread to put data part by part
 * @ingroup kernel_pipe_tests
 * @see k_pipe_put()
 */
ZTEST_USER(pipe, test_pipe_on_multiple_elements)
{
	k_thread_create(&get_single_tid, stack_1, STACK_SIZE,
			pipe_get_multiple, NULL, NULL, NULL,
			K_PRIO_PREEMPT(0), K_INHERIT_PERMS | K_USER,
			K_NO_WAIT);

	pipe_put_multiple();
	k_sem_take(&sync_sem, K_FOREVER);
	k_thread_abort(&get_single_tid);
	ztest_test_pass();
}

/**
 * @brief Test when multiple items are present with wait
 * @ingroup kernel_pipe_tests
 * @see k_pipe_put()
 */
ZTEST_USER(pipe, test_pipe_forever_wait)
{
	k_thread_create(&get_single_tid, stack_1, STACK_SIZE,
			pipe_get_forever_wait, NULL, NULL, NULL,
			K_PRIO_PREEMPT(0), K_INHERIT_PERMS | K_USER,
			K_NO_WAIT);

	pipe_put_forever_wait();
	k_sem_take(&sync_sem, K_FOREVER);
	k_thread_abort(&get_single_tid);
	ztest_test_pass();
}

/**
 * @brief Test pipes with timeout
 *
 * @ingroup kernel_pipe_tests
 *
 * @details
 * Test Objective:
 *	- Check if the kernel support supplying a timeout parameter
 * indicating the maximum amount of time a process will wait.
 *
 * Testing techniques:
 * - function and block box testing,Interface testing,
 * Dynamic analysis and testing.
 *
 * Prerequisite Conditions:
 * - CONFIG_TEST_USERSPACE.
 *
 * Input Specifications:
 * - N/A
 *
 * Test Procedure:
 * -# Create a sub thread to get data from a pipe.
 * -# In the sub thread, Set K_MSEC(10) as timeout for k_pipe_get().
 * and check the data which get from pipe if correct.
 * -# In main thread, use k_pipe_put to put data.
 * and check the return of k_pipe_put.
 *
 * Expected Test Result:
 * - The pipe can set timeout and works well.
 *
 * Pass/Fail Criteria:
 * - Successful if check points in test procedure are all passed, otherwise failure.
 *
 * Assumptions and Constraints:
 * - N/A
 *
 * @see k_pipe_put()
 */
ZTEST_USER(pipe, test_pipe_timeout)
{
	k_thread_create(&get_single_tid, stack_1, STACK_SIZE,
			pipe_get_timeout, NULL, NULL, NULL,
			K_PRIO_PREEMPT(0), K_INHERIT_PERMS | K_USER,
			K_NO_WAIT);

	pipe_put_timeout();
	k_sem_take(&sync_sem, K_FOREVER);
	k_thread_abort(&get_single_tid);
	ztest_test_pass();
}

/**
 * @brief Test pipe get from a empty pipe
 * @ingroup kernel_pipe_tests
 * @see k_pipe_get()
 */
ZTEST_USER(pipe, test_pipe_get_on_empty_pipe)
{
	pipe_get_on_empty_pipe();
	ztest_test_pass();
}

/**
 * @brief Test the pipe_get with K_FOREVER as timeout.
 * @details Testcase is similar to test_pipe_on_single_elements()
 * but with K_FOREVER as timeout.
 * @ingroup kernel_pipe_tests
 * @see k_pipe_put()
 */
ZTEST_USER(pipe, test_pipe_forever_timeout)
{
	k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(0));

	k_thread_create(&get_single_tid, stack_1, STACK_SIZE,
			pipe_get_forever_timeout, NULL, NULL, NULL,
			K_PRIO_PREEMPT(0), K_INHERIT_PERMS | K_USER,
			K_NO_WAIT);

	pipe_put_forever_timeout();
	k_sem_take(&sync_sem, K_FOREVER);
	ztest_test_pass();
}

/**
 * @brief k_pipe_get timeout test
 * @ingroup kernel_pipe_tests
 * @see k_pipe_get()
 */
ZTEST_USER(pipe, test_pipe_get_timeout)
{
	pipe_put_get_timeout();

	ztest_test_pass();
}

/**
 * @brief Test pipe get of invalid size
 * @ingroup kernel_pipe_tests
 * @see k_pipe_get()
 */
ZTEST_USER(pipe, test_pipe_get_invalid_size)
{
	size_t read;
	int ret;

	valid_fault = true;
	ret = k_pipe_get(&test_pipe, &rx_buffer,
		   0, &read,
		   1, TIMEOUT_200MSEC);

	zassert_equal(ret, -EINVAL,
		      "fault didn't occur for min_xfer <= bytes_to_read");
}

/**
 * @brief Test pipe get returns immediately if >= min_xfer is available
 * @ingroup kernel_pipe_tests
 * @see k_pipe_get()
 */
ZTEST_USER(pipe, test_pipe_get_min_xfer)
{
	int res;
	size_t bytes_written = 0;
	size_t bytes_read = 0;
	char buf[8] = {};

	res = k_pipe_put(&test_pipe, "Hi!", 3, &bytes_written,
			 3 /* min_xfer */, K_FOREVER);
	zassert_equal(res, 0, "did not write entire message");
	zassert_equal(bytes_written, 3, "did not write entire message");

	res = k_pipe_get(&test_pipe, buf, sizeof(buf), &bytes_read,
			 1 /* min_xfer */, K_FOREVER);
	zassert_equal(res, 0, "did not read at least one byte");
	zassert_equal(bytes_read, 3, "did not read all bytes available");
}

/**
 * @brief Test pipe put returns immediately if >= min_xfer is available
 * @ingroup kernel_pipe_tests
 * @see k_pipe_put()
 */
ZTEST_USER(pipe, test_pipe_put_min_xfer)
{
	int res;
	size_t bytes_written = 0;

	/* write 6 bytes into the pipe, so that 2 bytes are still free */
	for (size_t i = 0; i < 2; ++i) {
		bytes_written = 0;
		res = k_pipe_put(&test_pipe, "Hi!", 3, &bytes_written,
				 3 /* min_xfer */, K_FOREVER);
		zassert_equal(res, 0, "did not write entire message");
		zassert_equal(bytes_written, 3, "did not write entire message");
	}

	/* attempt to write 3 bytes, but allow success if >= 1 byte */
	bytes_written = 0;
	res = k_pipe_put(&test_pipe, "Hi!", 3, &bytes_written,
			 1 /* min_xfer */, K_FOREVER);
	zassert_equal(res, 0, "did not write min_xfer");
	zassert_true(bytes_written >= 1, "did not write min_xfer");

	/* flush the pipe so other test can write to this pipe */
	k_pipe_flush(&test_pipe);
}

/**
 * @brief Test defining and initializing pipes at run time
 *
 * @ingroup kernel_pipe_tests
 *
 * @details
 * Test Objective:
 * - Check if the kernel provided a mechanism for defining and
 * initializing pipes at run time.
 *
 * Testing techniques:
 * - function and block box testing,Interface testing,
 * Dynamic analysis and testing.
 *
 * Prerequisite Conditions:
 * - CONFIG_TEST_USERSPACE.
 *
 * Input Specifications:
 * - N/A
 *
 * Test Procedure:
 * -# Define and initialize a pipe at run time
 * -# Using this pipe to transfer data.
 * -# Check the pipe get/put operation.
 *
 * Expected Test Result:
 * - Pipe can be defined and initialized at run time.
 *
 * Pass/Fail Criteria:
 * - Successful if check points in test procedure are all passed, otherwise failure.
 *
 * Assumptions and Constraints:
 * - N/A
 *
 * @see k_pipe_init()
 */
ZTEST(pipe, test_pipe_define_at_runtime)
{
	unsigned char ring_buffer[PIPE_SIZE];
	struct k_pipe pipe;
	size_t written, read;

	/*Define and initialize pipe at run time*/
	k_pipe_init(&pipe, ring_buffer, sizeof(ring_buffer));

	/* initialize the tx buffer */
	for (int i = 0; i < sizeof(tx_buffer); i++) {
		tx_buffer[i] = i;
	}

	/*Using test_pipe which define and initialize at run time*/
	zassert_equal(k_pipe_put(&pipe, &tx_buffer,
			PIPE_SIZE, &written,
			PIPE_SIZE, K_NO_WAIT), RETURN_SUCCESS, NULL);

	/* Returned without waiting; zero data bytes were written. */
	zassert_equal(k_pipe_put(&pipe, &tx_buffer,
			PIPE_SIZE, &written,
			PIPE_SIZE, K_NO_WAIT), -EIO, NULL);

	/* Waiting period timed out. */
	zassert_equal(k_pipe_put(&pipe, &tx_buffer,
			PIPE_SIZE, &written,
			PIPE_SIZE, TIMEOUT_VAL), -EAGAIN, NULL);

	zassert_equal(k_pipe_get(&pipe, &rx_buffer,
			PIPE_SIZE, &read,
			PIPE_SIZE, K_NO_WAIT), RETURN_SUCCESS, NULL);

	zassert_true(rx_buffer_check(rx_buffer, read) == read,
			"Bytes read are not match.");

	/* Returned without waiting; zero data bytes were read. */
	zassert_equal(k_pipe_get(&pipe, &rx_buffer,
			PIPE_SIZE, &read,
			PIPE_SIZE, K_NO_WAIT), -EIO, NULL);
	/* Waiting period timed out. */
	zassert_equal(k_pipe_get(&pipe, &rx_buffer,
			PIPE_SIZE, &read,
			PIPE_SIZE, TIMEOUT_VAL), -EAGAIN, NULL);
}

/**
 * @brief Helper thread to test k_pipe_flush() and k_pipe_buffer_flush()
 *
 * This helper thread attempts to write 50 bytes to the pipe identified by
 * [p1], which has an internal buffer size of 10. This helper thread is
 * expected to fill the internal buffer, and then block until it can complete
 * the write.
 */
void test_pipe_flush_helper(void *p1, void *p2, void *p3)
{
	struct k_pipe *pipe = (struct k_pipe *)p1;
	char    buffer[50];
	size_t  i;
	size_t  bytes_written;
	int     rv;

	for (i = 0; i < sizeof(buffer); i++) {
		buffer[i] = i;
	}

	rv = k_pipe_put(pipe, buffer, sizeof(buffer), &bytes_written,
			sizeof(buffer), K_FOREVER);

	zassert_true(rv == 0, "k_pipe_put() failed with %d", rv);
	zassert_true(bytes_written == sizeof(buffer),
		     "Expected %zu bytes written, not %zu",
		     sizeof(buffer), bytes_written);
}

/**
 * @brief Test flushing a pipe
 *
 * @ingroup kernel_pipe_tests
 *
 * @details
 * Test Objective:
 * - Check if the kernel flushes a pipe properly at runtime.
 *
 * Testing techniques:
 * - function and block box testing,Interface testing,
 * Dynamic analysis and testing.
 *
 * Prerequisite Conditions:
 * - CONFIG_TEST_USERSPACE.
 *
 * Input Specifications:
 * - N/A
 *
 * Test Procedure:
 * -# Define and initialize a pipe at run time
 * -# Use this pipe to transfer data.
 * -# Have a thread fill and block on writing to the pipe
 * -# Flush the pipe and check that the pipe is completely empty
 * -# Have a thread fill and block on writing to the pipe
 * -# Flush only the pipe's buffer and check the results
 *
 * Expected Test Result:
 * - Reading from the pipe after k_pipe_flush() results in no data to read.
 * - Reading from the pipe after k_pipe_buffer_flush() results in read data,
 *   but missing the original data that was in the buffer prior to the flush.
 *
 * Pass/Fail Criteria:
 * - Successful if check points in test procedure are all passed, otherwise
 * failure.
 *
 * Assumptions and Constraints:
 * - N/A
 */
ZTEST(pipe, test_pipe_flush)
{
	unsigned char  results_buffer[50];
	size_t  bytes_read;
	size_t  i;
	int     rv;

	memset(results_buffer, 0, sizeof(results_buffer));

	(void)k_thread_create(&get_single_tid, stack_1, STACK_SIZE,
			      test_pipe_flush_helper, &small_pipe, NULL, NULL,
			      K_PRIO_PREEMPT(0), K_INHERIT_PERMS | K_USER,
			      K_NO_WAIT);

	k_sleep(K_MSEC(50));        /* give helper thread time to execute */

	/* Completely flush the pipe */

	k_pipe_flush(&small_pipe);

	rv = k_pipe_get(&small_pipe, results_buffer, sizeof(results_buffer),
			&bytes_read, 0, K_MSEC(100));

	zassert_true(rv == 0, "k_pipe_get() failed with %d\n", rv);
	zassert_true(bytes_read == 0,
		     "k_pipe_get() unexpectedly read %zu bytes\n", bytes_read);

	rv = k_thread_join(&get_single_tid, K_MSEC(50));
	zassert_true(rv == 0, "Wait for helper thread failed (%d)", rv);

	(void)k_thread_create(&get_single_tid, stack_1, STACK_SIZE,
			      test_pipe_flush_helper, &small_pipe, NULL, NULL,
			      K_PRIO_PREEMPT(0), K_INHERIT_PERMS | K_USER,
			      K_NO_WAIT);

	k_sleep(K_MSEC(50));        /* give helper thread time to execute */

	/*
	 * Only flush the pipe's buffer. This is expected to leave 40 bytes
	 * left to receive.
	 */

	k_pipe_buffer_flush(&small_pipe);

	rv = k_pipe_get(&small_pipe, results_buffer, sizeof(results_buffer),
			&bytes_read, 0, K_MSEC(100));

	zassert_true(rv == 0, "k_pipe_get() failed with %d\n", rv);
	zassert_true(bytes_read == 40,
		     "k_pipe_get() unexpectedly read %zu bytes\n", bytes_read);
	for (i = 0; i < 40; i++) {
		zassert_true(results_buffer[i] == (unsigned char)(i + 10),
			     "At offset %zd, expected byte %02x, not %02x\n",
			     i, (i + 10), results_buffer[i]);
	}

	rv = k_thread_join(&get_single_tid, K_MSEC(50));
	zassert_true(rv == 0, "Wait for helper thread failed (%d)", rv);
}