1 /*
2  * Copyright (c) 2023 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * This code checks the functionality of threads, synchronization primitives
9  * and device access from extensions.
10  * This test should be valid from both user and privileged modes.
11  */
12 
13 #include <stdint.h>
14 #include <zephyr/llext/symbol.h>
15 #include <zephyr/device.h>
16 #include <zephyr/kernel.h>
17 #include <zephyr/ztest_assert.h>
18 
19 #include "threads_kernel_objects_ext.h"
20 
21 /*
22  * Some platforms do not define any usable DT devices (not even the console).
23  * In those cases the device API test can't be executed.
24  */
25 #if DT_HAS_CHOSEN(zephyr_console) && DT_NODE_HAS_STATUS_OKAY(DT_CHOSEN(zephyr_console))
26 #define CONSOLE_DT_NODE DT_CHOSEN(zephyr_console)
27 #endif
28 
test_thread(void * arg0,void * arg1,void * arg2)29 void test_thread(void *arg0, void *arg1, void *arg2)
30 {
31 	printk("Take semaphore from test thread\n");
32 	k_sem_take(&my_sem, K_FOREVER);
33 
34 #ifdef CONSOLE_DT_NODE
35 	const struct device *const console_dev = DEVICE_DT_GET(CONSOLE_DT_NODE);
36 	const char *const console_name = DEVICE_DT_NAME(CONSOLE_DT_NODE);
37 	const struct device *binding_dev;
38 
39 	/* Ensure the console device was properly obtained at compile time */
40 	zassert_not_null(console_dev);
41 
42 	/* Try to get the same handle at runtime and verify they match */
43 	binding_dev = device_get_binding(console_name);
44 	zassert_equal(binding_dev, console_dev);
45 
46 	/* Verify device API functionality, console must be ready in CI tests */
47 	zassert_true(device_is_ready(console_dev));
48 #endif
49 }
50 
test_entry(void)51 void test_entry(void)
52 {
53 	printk("Give semaphore from main thread\n");
54 	zassert_not_null(&my_sem);
55 	k_sem_give(&my_sem);
56 
57 	printk("Creating thread\n");
58 	zassert_not_null(&my_thread);
59 	zassert_not_null(&my_thread_stack);
60 	k_tid_t tid = k_thread_create(&my_thread, (k_thread_stack_t *) &my_thread_stack,
61 				MY_THREAD_STACK_SIZE, &test_thread, NULL, NULL, NULL,
62 				MY_THREAD_PRIO, MY_THREAD_OPTIONS, K_FOREVER);
63 
64 	printk("Starting thread\n");
65 	k_thread_start(tid);
66 
67 	printk("Joining thread\n");
68 	k_thread_join(&my_thread, K_FOREVER);
69 	printk("Test thread joined\n");
70 }
71 EXPORT_SYMBOL(test_entry);
72