1111#define BYTES_TO_WRITE 16
1212#define BYTES_TO_READ BYTES_TO_WRITE
1313
14+ K_HEAP_DEFINE (mpool , PIPE_LEN * 1 );
15+
1416static ZTEST_DMEM unsigned char __aligned (4 ) data [ ] =
1517"abcd1234$%^&PIPEefgh5678!/?*EPIPijkl9012[]<>PEPImnop3456{}()IPEP" ;
1618BUILD_ASSERT (sizeof (data ) >= PIPE_LEN );
@@ -20,7 +22,7 @@ K_PIPE_DEFINE(kpipe, PIPE_LEN, 4);
2022K_PIPE_DEFINE (khalfpipe , (PIPE_LEN / 2 ), 4 );
2123K_PIPE_DEFINE (kpipe1 , PIPE_LEN , 4 );
2224K_PIPE_DEFINE (pipe_test_alloc , PIPE_LEN , 4 );
23- struct k_pipe pipe ;
25+ struct k_pipe pipe , pipe1 ;
2426
2527K_THREAD_STACK_DEFINE (tstack , STACK_SIZE );
2628K_THREAD_STACK_DEFINE (tstack1 , STACK_SIZE );
@@ -30,6 +32,17 @@ struct k_thread tdata1;
3032struct k_thread tdata2 ;
3133K_SEM_DEFINE (end_sema , 0 , 1 );
3234
35+ #ifdef CONFIG_64BIT
36+ #define SZ 256
37+ #else
38+ #define SZ 128
39+ #endif
40+ K_HEAP_DEFINE (test_pool , SZ * 4 );
41+
42+ struct mem_block {
43+ void * data ;
44+ };
45+
3346static void tpipe_put (struct k_pipe * ppipe , k_timeout_t timeout )
3447{
3548 size_t to_wt , wt_byte = 0 ;
@@ -90,6 +103,23 @@ static void tpipe_thread_thread(struct k_pipe *ppipe)
90103 k_thread_abort (tid );
91104}
92105
106+ static void tpipe_kthread_to_kthread (struct k_pipe * ppipe )
107+ {
108+ /**TESTPOINT: thread-thread data passing via pipe*/
109+ k_tid_t tid = k_thread_create (& tdata , tstack , STACK_SIZE ,
110+ tThread_entry , ppipe , NULL , NULL ,
111+ K_PRIO_PREEMPT (0 ), 0 , K_NO_WAIT );
112+
113+ tpipe_put (ppipe , K_NO_WAIT );
114+ k_sem_take (& end_sema , K_FOREVER );
115+
116+ k_sem_take (& end_sema , K_FOREVER );
117+ tpipe_get (ppipe , K_FOREVER );
118+
119+ /* clear the spawned thread avoid side effect */
120+ k_thread_abort (tid );
121+ }
122+
93123static void tpipe_put_no_wait (struct k_pipe * ppipe )
94124{
95125 size_t to_wt , wt_byte = 0 ;
@@ -104,6 +134,89 @@ static void tpipe_put_no_wait(struct k_pipe *ppipe)
104134 }
105135}
106136
137+ static void tpipe_put_small_size (struct k_pipe * ppipe , k_timeout_t timeout )
138+ {
139+ size_t to_wt , wt_byte = 0 ;
140+
141+ for (int i = 0 ; i < PIPE_LEN ; i += wt_byte ) {
142+ /**TESTPOINT: pipe put*/
143+ to_wt = (PIPE_LEN - i ) >= 24 ?
144+ 24 : (PIPE_LEN - i );
145+ k_pipe_put (ppipe , & data [i ], to_wt , & wt_byte , 1 , timeout );
146+ }
147+ }
148+
149+ static void tpipe_get_small_size (struct k_pipe * ppipe , k_timeout_t timeout )
150+ {
151+ unsigned char rx_data [PIPE_LEN ];
152+ size_t to_rd , rd_byte = 0 ;
153+
154+ /*get pipe data from "pipe_put"*/
155+ for (int i = 0 ; i < PIPE_LEN ; i += rd_byte ) {
156+ /**TESTPOINT: pipe get*/
157+ to_rd = (PIPE_LEN - i ) >= 24 ?
158+ 24 : (PIPE_LEN - i );
159+ zassert_false (k_pipe_get (ppipe , & rx_data [i ], to_rd ,
160+ & rd_byte , 1 , timeout ), NULL );
161+ }
162+ }
163+
164+
165+ /**
166+ * @brief Test Initialization and buffer allocation of pipe,
167+ * with various parameters
168+ * @see k_pipe_alloc_init(), k_pipe_cleanup()
169+ */
170+ void test_pipe_alloc (void )
171+ {
172+ int ret ;
173+
174+ zassert_false (k_pipe_alloc_init (& pipe_test_alloc , PIPE_LEN ), NULL );
175+
176+ tpipe_kthread_to_kthread (& pipe_test_alloc );
177+ k_pipe_cleanup (& pipe_test_alloc );
178+
179+ zassert_false (k_pipe_alloc_init (& pipe_test_alloc , 0 ), NULL );
180+ k_pipe_cleanup (& pipe_test_alloc );
181+
182+ ret = k_pipe_alloc_init (& pipe_test_alloc , 2048 );
183+ zassert_true (ret == - ENOMEM ,
184+ "resource pool max block size is not smaller then requested buffer" );
185+ }
186+
187+ static void thread_for_get_forever (void * p1 , void * p2 , void * p3 )
188+ {
189+ tpipe_get ((struct k_pipe * )p1 , K_FOREVER );
190+ }
191+
192+ void test_pipe_cleanup (void )
193+ {
194+ int ret ;
195+
196+ zassert_false (k_pipe_alloc_init (& pipe_test_alloc , PIPE_LEN ), NULL );
197+
198+ /**TESTPOINT: test if a dynamically allocated buffer can be freed*/
199+ ret = k_pipe_cleanup (& pipe_test_alloc );
200+ zassert_true ((ret == 0 ) && (pipe_test_alloc .buffer == NULL ),
201+ "Failed to free buffer with k_pipe_cleanup()." );
202+
203+ /**TESTPOINT: nothing to do with k_pipe_cleanup() for static buffer in pipe*/
204+ ret = k_pipe_cleanup (& kpipe );
205+ zassert_true ((ret == 0 ) && (kpipe .buffer != NULL ),
206+ "Static buffer should not be freed." );
207+
208+ zassert_false (k_pipe_alloc_init (& pipe_test_alloc , PIPE_LEN ), NULL );
209+
210+ k_tid_t tid = k_thread_create (& tdata , tstack , STACK_SIZE ,
211+ thread_for_get_forever , & pipe_test_alloc , NULL ,
212+ NULL , K_PRIO_PREEMPT (0 ), 0 , K_NO_WAIT );
213+ k_sleep (K_MSEC (100 ));
214+
215+ ret = k_pipe_cleanup (& pipe_test_alloc );
216+ zassert_true (ret == - EAGAIN , "k_pipe_cleanup() should not return with 0." );
217+ k_thread_abort (tid );
218+ }
219+
107220static void thread_handler (void * p1 , void * p2 , void * p3 )
108221{
109222 tpipe_put_no_wait ((struct k_pipe * )p1 );
@@ -162,7 +275,6 @@ static void thread_handler(void *p1, void *p2, void *p3)
162275void test_pipe_thread2thread (void )
163276{
164277 /**TESTPOINT: test k_pipe_init pipe*/
165-
166278 k_pipe_init (& pipe , data , PIPE_LEN );
167279 tpipe_thread_thread (& pipe );
168280
@@ -189,27 +301,85 @@ void test_pipe_user_thread2thread(void)
189301}
190302#endif
191303
304+ /**
305+ * @brief Test resource pool free
306+ * @see k_heap_alloc()
307+ */
308+ #ifdef CONFIG_USERSPACE
309+ void test_resource_pool_auto_free (void )
310+ {
311+ /* Pool has 2 blocks, both should succeed if kernel object and pipe
312+ * buffer are auto-freed when the allocating threads exit
313+ */
314+ zassert_true (k_heap_alloc (& test_pool , 64 , K_NO_WAIT ) != NULL , NULL );
315+ zassert_true (k_heap_alloc (& test_pool , 64 , K_NO_WAIT ) != NULL , NULL );
316+ }
317+ #endif
318+
192319static void tThread_half_pipe_put (void * p1 , void * p2 , void * p3 )
193320{
194321 tpipe_put ((struct k_pipe * )p1 , K_FOREVER );
195322}
196323
324+ static void tThread_half_pipe_get (void * p1 , void * p2 , void * p3 )
325+ {
326+ tpipe_get ((struct k_pipe * )p1 , K_FOREVER );
327+ }
328+
197329/**
198- * @brief Test get/ put with smaller pipe buffer
330+ * @brief Test put/get with smaller pipe buffer
199331 * @see k_pipe_put(), k_pipe_get()
200332 */
201- void test_half_pipe_get_put (void )
333+ void test_half_pipe_put_get (void )
202334{
203335 /**TESTPOINT: thread-thread data passing via pipe*/
204- k_tid_t tid = k_thread_create (& tdata , tstack , STACK_SIZE ,
336+ k_tid_t tid1 = k_thread_create (& tdata1 , tstack1 , STACK_SIZE ,
337+ tThread_half_pipe_get , & khalfpipe ,
338+ NULL , NULL , K_PRIO_PREEMPT (0 ),
339+ K_INHERIT_PERMS | K_USER , K_NO_WAIT );
340+
341+ k_tid_t tid2 = k_thread_create (& tdata2 , tstack2 , STACK_SIZE ,
342+ tThread_half_pipe_get , & khalfpipe ,
343+ NULL , NULL , K_PRIO_PREEMPT (0 ),
344+ K_INHERIT_PERMS | K_USER , K_NO_WAIT );
345+
346+ k_sleep (K_MSEC (100 ));
347+ tpipe_put_small_size (& khalfpipe , K_NO_WAIT );
348+
349+ /* clear the spawned thread avoid side effect */
350+ k_thread_abort (tid1 );
351+ k_thread_abort (tid2 );
352+ }
353+
354+ void test_pipe_get_put (void )
355+ {
356+ unsigned char rx_data [PIPE_LEN ];
357+ size_t rd_byte = 0 ;
358+ int ret ;
359+
360+ /* TESTPOINT: min_xfer > bytes_to_read */
361+ ret = k_pipe_get (& kpipe , & rx_data [0 ], 1 , & rd_byte , 24 , K_NO_WAIT );
362+ zassert_true (ret == - EINVAL , NULL );
363+ ret = k_pipe_get (& kpipe , & rx_data [0 ], 24 , NULL , 1 , K_NO_WAIT );
364+ zassert_true (ret == - EINVAL , NULL );
365+
366+ /**TESTPOINT: thread-thread data passing via pipe*/
367+ k_tid_t tid1 = k_thread_create (& tdata1 , tstack1 , STACK_SIZE ,
368+ tThread_half_pipe_put , & khalfpipe ,
369+ NULL , NULL , K_PRIO_PREEMPT (0 ),
370+ K_INHERIT_PERMS | K_USER , K_NO_WAIT );
371+
372+ k_tid_t tid2 = k_thread_create (& tdata2 , tstack2 , STACK_SIZE ,
205373 tThread_half_pipe_put , & khalfpipe ,
206374 NULL , NULL , K_PRIO_PREEMPT (0 ),
207375 K_INHERIT_PERMS | K_USER , K_NO_WAIT );
208376
209- tpipe_get (& khalfpipe , K_FOREVER );
377+ k_sleep (K_MSEC (100 ));
378+ tpipe_get_small_size (& khalfpipe , K_NO_WAIT );
210379
211380 /* clear the spawned thread avoid side effect */
212- k_thread_abort (tid );
381+ k_thread_abort (tid1 );
382+ k_thread_abort (tid2 );
213383}
214384
215385/**
0 commit comments