summaryrefslogtreecommitdiff
path: root/libs/os/src/test/callout_test.c
blob: a56efd507065c62f56bafd94bcb3c072ac77c0b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
 
#include "testutil/testutil.h"
#include "os/os.h"
#include "os_test_priv.h"
#include "os/os_eventq.h"
#include "os/os_callout.h"
#include "os/os_time.h"

/* Task 1 for sending */
#define CALLOUT_STACK_SIZE        (5120)
#define SEND_CALLOUT_TASK_PRIO        (1)
struct os_task callout_task_struct_send;
os_stack_t callout_task_stack_send[CALLOUT_STACK_SIZE];

#define RECEIVE_CALLOUT_TASK_PRIO        (2)
struct os_task callout_task_struct_receive;
os_stack_t callout_task_stack_receive[CALLOUT_STACK_SIZE];

/* Delearing variables for callout_func */
struct os_callout_func callout_func_test;

/* The event to be sent*/
struct os_eventq callout_evq;
struct os_event callout_ev;

/* The callout_stop task */
#define SEND_STOP_CALLOUT_TASK_PRIO        (3)
struct os_task callout_task_struct_stop_send;
os_stack_t callout_task_stack_stop_send[CALLOUT_STACK_SIZE];

#define RECEIVE_STOP_CALLOUT_TASK_PRIO        (4)
struct os_task callout_task_struct_stop_receive;
os_stack_t callout_task_stack_stop_receive[CALLOUT_STACK_SIZE];

/* Delearing variables for callout_stop_func */
#define MULTI_SIZE    (2)
struct os_callout_func callout_func_stop_test[MULTI_SIZE];

/* The event to be sent*/
struct os_eventq callout_stop_evq[MULTI_SIZE];
struct os_event callout_stop_ev;

/* Declearing varables for callout_speak */
#define SPEAK_CALLOUT_TASK_PRIO        (5)
struct os_task callout_task_struct_speak;
os_stack_t callout_task_stack_speak[CALLOUT_STACK_SIZE];

/* Declearing varaibles for listen */
#define LISTEN_CALLOUT_TASK_PRIO        (6)
struct os_task callout_task_struct_listen;
os_stack_t callout_task_stack_listen[CALLOUT_STACK_SIZE];

struct os_callout_func callout_func_speak;

/* Global variables to be used by the callout functions */
int p;
int q;
int t;
/* This is the function for callout_init*/
void
my_callout_func(void *arg)
{
    p = 4;
}

/* This is the function for callout_init of stop test_case*/
void
my_callout_stop_func(void *arg)
{
    q = 1;
}
/* This is the function for callout_init for speak test_case*/
void
my_callout_speak_func(void *arg)
{
    t = 2;
}

/* This is a callout task to send data */
void
callout_task_send(void *arg )
{
   int i;
    /* Should say whether callout is armed or not */
    i= os_callout_queued(&callout_func_test.cf_c);
    TEST_ASSERT(i == 0);

    /* Arm the callout */
    i = os_callout_reset(&callout_func_test.cf_c, OS_TICKS_PER_SEC/ 50);
    TEST_ASSERT_FATAL(i == 0);

    /* Should say whether callout is armed or not */
    i = os_callout_queued(&callout_func_test.cf_c);
    TEST_ASSERT(i == 1);

    /* Send the callout */ 
    os_time_delay(OS_TICKS_PER_SEC );
}

/* This is the callout to receive data */
void
callout_task_receive( void *arg)
{
    int i;
    struct os_event *event;
    os_time_t now;
    os_time_t tm;
    os_sr_t sr; 
    /* Recieve using the os_eventq_poll */
    event = os_eventq_poll(&callout_func_test.cf_c.c_evq, 1, OS_WAIT_FOREVER);
    TEST_ASSERT(event->ev_type ==  OS_EVENT_T_TIMER);
    TEST_ASSERT(event->ev_arg == NULL);

    /* Should say whether callout is armed or not */
    i = os_callout_queued(&callout_func_test.cf_c);
    TEST_ASSERT(i == 0);

    OS_ENTER_CRITICAL(sr);
    now = os_time_get();
    tm = os_callout_wakeup_ticks(now);
    TEST_ASSERT(tm == OS_TIMEOUT_NEVER);
    OS_EXIT_CRITICAL(sr);
    
    /* Finishes the test when OS has been started */
    os_test_restart();

}

/* This is callout to send the stop_callout */
void
callout_task_stop_send( void *arg)
{
    int k;
    int j;    
     /* Should say whether callout is armed or not */
    for(k = 0; k<MULTI_SIZE; k++){
        j = os_callout_queued(&callout_func_stop_test[k].cf_c);
        TEST_ASSERT(j == 0);
    }


    /* Show that  callout is not armed after calling callout_stop */
    for(k = 0; k<MULTI_SIZE; k++){
        os_callout_stop(&callout_func_stop_test[k].cf_c);
        j = os_callout_queued(&callout_func_stop_test[k].cf_c);
        TEST_ASSERT(j == 0);
    }
    /* Arm the callout */
    for(k = 0; k<MULTI_SIZE; k++){
        j = os_callout_reset(&callout_func_stop_test[k].cf_c, OS_TICKS_PER_SEC/ 50);
        TEST_ASSERT_FATAL(j == 0);
    }
    os_time_delay( OS_TICKS_PER_SEC );
}

/* This is the callout to receive stop_callout data */
void
callout_task_stop_receive( void *arg )
{
    int k;
    struct os_event *event;
    /* Recieving using the os_eventq_poll */
    for(k=0; k<MULTI_SIZE; k++){
        event = os_eventq_poll(&callout_func_stop_test[k].cf_c.c_evq, 1,
           OS_WAIT_FOREVER);
        TEST_ASSERT(event->ev_type ==  OS_EVENT_T_TIMER);
        TEST_ASSERT(event->ev_arg == NULL);

     }
     
    /* Show that event is removed from the queued after calling callout_stop */
    for(k=0; k<MULTI_SIZE; k++){
        os_callout_stop(&callout_func_stop_test[k].cf_c);
        /* Testing that the event has been removed from queue */
        TEST_ASSERT_FATAL(1); 
     }
    /* Finishes the test when OS has been started */
    os_test_restart();

}

/* This is a callout task to send data */
void
callout_task_stop_speak( void *arg )
{
    int i;
    /* Arm the callout */
    i = os_callout_reset(&callout_func_speak.cf_c, OS_TICKS_PER_SEC/ 50);
    TEST_ASSERT_FATAL(i == 0);

    /* should say whether callout is armed or not */
    i = os_callout_queued(&callout_func_speak.cf_c);
    TEST_ASSERT(i == 1);

    os_callout_stop(&callout_func_speak.cf_c);
    
    /* Send the callout */ 
    os_time_delay(OS_TICKS_PER_SEC/ 100 );
    /* Finishes the test when OS has been started */
    os_test_restart();
}

void
callout_task_stop_listen( void *arg )
{
    os_eventq_get(callout_func_speak.cf_c.c_evq);
    TEST_ASSERT_FATAL(0);
}

/* Test case to test the basics of the callout */
TEST_CASE(callout_test)
{

    /* Initializing the OS */
    os_init();
    
    /* Initialize the sending task */
    os_task_init(&callout_task_struct_send, "callout_task_send",
        callout_task_send, NULL, SEND_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
        callout_task_stack_send, CALLOUT_STACK_SIZE);

    /* Initialize the receive task */
    os_task_init(&callout_task_struct_receive, "callout_task_receive",
        callout_task_receive, NULL, RECEIVE_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
        callout_task_stack_receive, CALLOUT_STACK_SIZE);

    os_eventq_init(&callout_evq);
    
    /* Initialize the callout function */
    os_callout_func_init(&callout_func_test, &callout_evq,
        my_callout_func, NULL);
    TEST_ASSERT(p == 4);

    /* Does not return until OS_restart is called */
    os_start();

}

/* Test case of the callout_task_stop */
TEST_CASE(callout_test_stop)
{
    int k;
    /* Initializing the OS */
    os_init();

    /* Initialize the sending task */
    os_task_init(&callout_task_struct_stop_send, "callout_task_stop_send",
        callout_task_stop_send, NULL, SEND_STOP_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
        callout_task_stack_stop_send, CALLOUT_STACK_SIZE);

    /* Initialize the receiving task */
    os_task_init(&callout_task_struct_stop_receive, "callout_task_stop_receive",
        callout_task_stop_receive, NULL, RECEIVE_STOP_CALLOUT_TASK_PRIO,
        OS_WAIT_FOREVER, callout_task_stack_stop_receive, CALLOUT_STACK_SIZE);

    for(k = 0; k< MULTI_SIZE; k++){
        os_eventq_init(&callout_stop_evq[k]);
    }
    
    /* Initialize the callout function */
    for(k = 0; k<MULTI_SIZE; k++){
        os_callout_func_init(&callout_func_stop_test[k], &callout_stop_evq[k],
           my_callout_stop_func, NULL);
         TEST_ASSERT(q == 1);

    }

    /* Does not return until OS_restart is called */
    os_start();

}

/* Test case to test case for speak and listen */
TEST_CASE(callout_test_speak)
{
    /* Initializing the OS */
    os_init();
    
    /* Initialize the sending task */
    os_task_init(&callout_task_struct_speak, "callout_task_speak",
        callout_task_stop_speak, NULL, SPEAK_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
        callout_task_stack_speak, CALLOUT_STACK_SIZE);

    /* Initialize the receive task */
    os_task_init(&callout_task_struct_listen, "callout_task_listen",
        callout_task_stop_listen, NULL, LISTEN_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
        callout_task_stack_listen, CALLOUT_STACK_SIZE);

    os_eventq_init(&callout_evq);
    
    /* Initialize the callout function */
    os_callout_func_init(&callout_func_speak, &callout_evq,
        my_callout_speak_func, NULL);
    TEST_ASSERT(t == 2);
    
    /* Does not return until OS_restart is called */
    os_start();

}

TEST_SUITE(os_callout_test_suite)
{   
    callout_test();
    callout_test_stop();
    callout_test_speak();
}