TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ns_test_helpers.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "ns_test_helpers.h"
9 
10 #include "os_wrapper/thread.h"
11 #include "os_wrapper/semaphore.h"
12 
13 #include "tfm_nspm_api.h"
14 
15 #define PS_TEST_TASK_STACK_SIZE (768)
16 
17 struct test_task_t {
19  struct test_result_t *ret;
20 };
21 
22 static void *test_semaphore;
23 
30 static void test_task_runner(void *arg)
31 {
32  struct test_task_t *test = arg;
33 
34 #ifdef TFM_NS_CLIENT_IDENTIFICATION
36 #endif /* TFM_NS_CLIENT_IDENTIFICATION */
37 
38  /* Call the test function */
39  test->func(test->ret);
40 
41  /* Release the semaphore to unblock the parent thread */
42  os_wrapper_semaphore_release(test_semaphore);
43 
44  /* Signal to the RTOS that the thread is finished */
46 }
47 
48 void tfm_ps_run_test(const char *thread_name, struct test_result_t *ret,
49  test_func_t *test_func)
50 {
51  void *current_thread_handle;
52  uint32_t current_thread_priority;
53  uint32_t err;
54  void *thread;
55  struct test_task_t test_task = { .func = test_func, .ret = ret };
56 
57  /* Create a binary semaphore with initial count of 0 tokens available */
58  test_semaphore = os_wrapper_semaphore_create(1, 0, "ps_tests_sema");
59  if (!test_semaphore) {
60  TEST_FAIL("Semaphore creation failed");
61  return;
62  }
63 
64  current_thread_handle = os_wrapper_thread_get_handle();
65  if (!current_thread_handle) {
66  os_wrapper_semaphore_delete(test_semaphore);
67  TEST_FAIL("Failed to get current thread ID");
68  return;
69  }
70 
71  err = os_wrapper_thread_get_priority(current_thread_handle,
72  &current_thread_priority);
73  if (err == OS_WRAPPER_ERROR) {
74  os_wrapper_semaphore_delete(test_semaphore);
75  TEST_FAIL("Failed to get current thread priority");
76  return;
77  }
78 
79  /* Start test thread */
80  thread = os_wrapper_thread_new(thread_name, PS_TEST_TASK_STACK_SIZE,
81  test_task_runner, &test_task,
82  current_thread_priority);
83  if (!thread) {
84  os_wrapper_semaphore_delete(test_semaphore);
85  TEST_FAIL("Failed to create test thread");
86  return;
87  }
88 
89  /* Signal semaphore, wait indefinitely until unblocked by child thread */
91 
92  /* At this point, it means the binary semaphore has been released by the
93  * test and re-acquired by this thread, so just finally release it and
94  * delete it
95  */
96  os_wrapper_semaphore_release(test_semaphore);
97 
98  os_wrapper_semaphore_delete(test_semaphore);
99 }
void * os_wrapper_thread_new(const char *name, int32_t stack_size, os_wrapper_thread_func func, void *arg, uint32_t priority)
Creates a new thread.
uint32_t tfm_nspm_register_client_id(void)
Reports the client ID of this task to TF-M.
void os_wrapper_thread_exit(void)
Exits the calling thread.
#define TEST_FAIL(info_msg)
void * os_wrapper_thread_get_handle(void)
Gets current thread handle.
void * os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count, const char *name)
Creates a new semaphore.
#define PS_TEST_TASK_STACK_SIZE
#define OS_WRAPPER_WAIT_FOREVER
Definition: common.h:19
void tfm_ps_run_test(const char *thread_name, struct test_result_t *ret, test_func_t *test_func)
Executes the given test function from the specified thread context.
struct test_result_t * ret
#define OS_WRAPPER_ERROR
Definition: common.h:18
uint32_t os_wrapper_thread_get_priority(void *handle, uint32_t *priority)
Gets thread priority.
uint32_t os_wrapper_semaphore_delete(void *handle)
Deletes the semaphore.
void test_func_t(struct test_result_t *ret)
test_func_t * func
uint32_t os_wrapper_semaphore_release(void *handle)
Releases the semaphore.
uint32_t os_wrapper_semaphore_acquire(void *handle, uint32_t timeout)
Acquires the semaphore.