xref: /nrf52832-nimble/packages/NimBLE-latest/porting/npl/linux/test/test_npl_sem.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 /**
21   Unit tests for the Semaphore api (ble_npl_sem):
22 
23   ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens);
24   ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem);
25   ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, uint32_t timeout);
26   uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem);
27 */
28 
29 #include "test_util.h"
30 #include "nimble/nimble_npl.h"
31 //#include "os/os.h"
32 
33 #define TEST_ITERATIONS   10
34 
35 #define TASK1_PRIO        1
36 #define TASK2_PRIO        1
37 
38 #define TASK1_STACK_SIZE  1028
39 #define TASK2_STACK_SIZE  1028
40 
41 static struct ble_npl_task    task1;
42 static struct ble_npl_task    task2;
43 
44 static ble_npl_stack_t task1_stack[TASK1_STACK_SIZE];
45 static ble_npl_stack_t task2_stack[TASK2_STACK_SIZE];
46 
47 struct ble_npl_sem task1_sem;
48 struct ble_npl_sem task2_sem;
49 
50 /* Task 1 handler function */
51 void *
task1_handler(void * arg)52 task1_handler(void *arg)
53 {
54     for (int i = 0; i < TEST_ITERATIONS; i++)
55     {
56 	/* Release semaphore to task 2 */
57         SuccessOrQuit(ble_npl_sem_release(&task1_sem),
58 		      "ble_npl_sem_release: error releasing task2_sem.");
59 
60 	/* Wait for semaphore from task 2 */
61         SuccessOrQuit(ble_npl_sem_pend(&task2_sem, BLE_NPL_TIME_FOREVER),
62 		      "ble_npl_sem_pend: error waiting for task2_sem.");
63     }
64 
65     printf("All tests passed\n");
66     exit(PASS);
67 
68     return NULL;
69 }
70 
71 /* Task 2 handler function */
72 void *
task2_handler(void * arg)73 task2_handler(void *arg)
74 {
75     while(1)
76     {
77         /* Wait for semaphore from task1 */
78         SuccessOrQuit(ble_npl_sem_pend(&task1_sem, BLE_NPL_TIME_FOREVER),
79 		      "ble_npl_sem_pend: error waiting for task1_sem.");
80 
81 	    /* Release task2 semaphore */
82         SuccessOrQuit(ble_npl_sem_release(&task2_sem),
83 		      "ble_npl_sem_release: error releasing task1_sem.");
84     }
85 
86     return NULL;
87 }
88 
89 
90 /* Initialize task 1 exposed data objects */
91 void
task1_init(void)92 task1_init(void)
93 {
94     /* Initialize task1 semaphore */
95     SuccessOrQuit(ble_npl_sem_init(&task1_sem, 0),
96 		  "ble_npl_sem_init: task1 returned error.");
97 }
98 
99 /* Initialize task 2 exposed data objects */
100 void
task2_init(void)101 task2_init(void)
102 {
103     /* Initialize task1 semaphore */
104     SuccessOrQuit(ble_npl_sem_init(&task2_sem, 0),
105 		  "ble_npl_sem_init: task2 returned error.");
106 }
107 
108 /**
109  * init_app_tasks
110  *
111  * This function performs initializations that are required before tasks run.
112  *
113  * @return int 0 success; error otherwise.
114  */
115 static int
init_app_tasks(void)116 init_app_tasks(void)
117 {
118     /*
119      * Call task specific initialization functions to initialize any shared objects
120      * before initializing the tasks with the OS.
121      */
122     task1_init();
123     task2_init();
124 
125     /*
126      * Initialize tasks 1 and 2 with the OS.
127      */
128     ble_npl_task_init(&task1, "task1", task1_handler, NULL, TASK1_PRIO,
129 		 BLE_NPL_WAIT_FOREVER, task1_stack, TASK1_STACK_SIZE);
130 
131     ble_npl_task_init(&task2, "task2", task2_handler, NULL, TASK2_PRIO,
132 		 BLE_NPL_WAIT_FOREVER, task2_stack, TASK2_STACK_SIZE);
133 
134     return 0;
135 }
136 
137 /**
138  * main
139  *
140  * The main function for the application. This function initializes the system and packages,
141  * calls the application specific task initialization function, then waits and dispatches
142  * events from the OS default event queue in an infinite loop.
143  */
144 int
main(int argc,char ** arg)145 main(int argc, char **arg)
146 {
147     /* Initialize application specific tasks */
148     init_app_tasks();
149 
150     while (1)
151     {
152         ble_npl_eventq_run(ble_npl_eventq_dflt_get());
153     }
154     /* main never returns */
155 }
156