xref: /nrf52832-nimble/packages/NimBLE-latest/porting/npl/linux/test/test_npl_callout.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 ble_npl_callout api:
22 
23   void ble_npl_callout_init(struct ble_npl_callout *cf, struct ble_npl_eventq *evq,
24                        ble_npl_event_fn *ev_cb, void *ev_arg);
25   int ble_npl_callout_reset(struct ble_npl_callout *, int32_t);
26   int ble_npl_callout_queued(struct ble_npl_callout *c);
27   void ble_npl_callout_stop(struct ble_npl_callout *c);
28 */
29 
30 #include "test_util.h"
31 #include "nimble/nimble_npl.h"
32 
33 #define TEST_ARGS_VALUE  (55)
34 #define TEST_INTERVAL    (100)
35 
36 static bool                   s_tests_running = true;
37 static struct ble_npl_task    s_task;
38 static struct ble_npl_callout s_callout;
39 static int                    s_callout_args = TEST_ARGS_VALUE;
40 
41 static struct ble_npl_eventq  s_eventq;
42 
43 
on_callout(struct ble_npl_event * ev)44 void on_callout(struct ble_npl_event *ev)
45 {
46     VerifyOrQuit(ev->ev_arg == &s_callout_args,
47 		 "callout: wrong args passed");
48 
49     VerifyOrQuit(*(int*)ev->ev_arg == TEST_ARGS_VALUE,
50 		 "callout: args corrupted");
51 
52     s_tests_running = false;
53 }
54 
55 /**
56  * ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *evq,
57  *                 ble_npl_event_fn *ev_cb, void *ev_arg)
58  */
test_init()59 int test_init()
60 {
61     ble_npl_callout_init(&s_callout,
62 		    &s_eventq,
63 		    on_callout,
64 		    &s_callout_args);
65     return PASS;
66 }
67 
test_queued()68 int test_queued()
69 {
70   //VerifyOrQuit(ble_npl_callout_queued(&s_callout),
71   //	 "callout: not queued when expected");
72     return PASS;
73 }
74 
test_reset()75 int test_reset()
76 {
77     return ble_npl_callout_reset(&s_callout, TEST_INTERVAL);
78 }
79 
test_stop()80 int test_stop()
81 {
82     return PASS;
83 }
84 
85 
86 /**
87  * ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *evq,
88  *                 ble_npl_event_fn *ev_cb, void *ev_arg)
89  */
test_task_run(void * args)90 void *test_task_run(void *args)
91 {
92     SuccessOrQuit(test_init(),   "callout_init failed");
93     SuccessOrQuit(test_queued(), "callout_queued failed");
94     SuccessOrQuit(test_reset(),  "callout_reset failed");
95 
96     while (s_tests_running)
97     {
98         ble_npl_eventq_run(&s_eventq);
99     }
100 
101     printf("All tests passed\n");
102     exit(PASS);
103 
104     return NULL;
105 }
106 
main(void)107 int main(void)
108 {
109     ble_npl_eventq_init(&s_eventq);
110 
111     SuccessOrQuit(ble_npl_task_init(&s_task, "s_task", test_task_run,
112 			       NULL, 1, 0, NULL, 0),
113 		  "task: error initializing");
114 
115     while (1) {}
116 }
117