1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#include <autoconf.h>
14#include <sel4test-driver/gen_config.h>
15#include <sel4/sel4.h>
16#include <vka/object.h>
17
18#include "../timer.h"
19
20#include <utils/util.h>
21
22static bool test_finished;
23
24typedef struct timer_test_data {
25    int curr_count;
26    int goal_count;
27} timer_test_data_t;
28
29static int test_callback(uintptr_t token)
30{
31    assert(token);
32    timer_test_data_t *test_data = (timer_test_data_t *) token;
33    test_data->curr_count++;
34    if (test_data->curr_count == test_data->goal_count) {
35        test_finished = true;
36    }
37    return 0;
38}
39
40int test_timer(driver_env_t env)
41{
42    uint64_t time = 0;
43    test_finished = false;
44    timer_test_data_t test_data = { .goal_count = 10 };
45
46    int error = tm_alloc_id_at(&env->tm, TIMER_ID);
47    test_assert_fatal(!error);
48
49    error = tm_register_cb(&env->tm, TIMEOUT_PERIODIC, 1 * NS_IN_S, 0, TIMER_ID,
50                           test_callback, (uintptr_t) &test_data);
51    test_assert_fatal(!error);
52
53    while (!test_finished) {
54        wait_for_timer_interrupt(env);
55        ZF_LOGV("Tick\n");
56        error = tm_update(&env->tm);
57        test_assert_fatal(!error);
58    }
59
60    error = tm_free_id(&env->tm, TIMER_ID);
61    test_assert_fatal(!error);
62
63    error = ltimer_reset(&env->ltimer);
64    test_assert_fatal(!error);
65
66    return sel4test_get_result();
67}
68DEFINE_TEST_BOOTSTRAP(TIMER0001, "Basic timer testing", test_timer, config_set(CONFIG_HAVE_TIMER))
69
70int
71test_gettime_timeout(driver_env_t env)
72{
73    int error = 0;
74    uint64_t start, end;
75    test_finished = false;
76    timer_test_data_t test_data = { .goal_count = 3 };
77
78    error = tm_alloc_id_at(&env->tm, TIMER_ID);
79    test_assert_fatal(!error);
80
81    start = timestamp(env);
82    error = tm_register_cb(&env->tm, TIMEOUT_PERIODIC, 1 * NS_IN_MS, 0, TIMER_ID,
83                           test_callback, (uintptr_t) &test_data);
84    test_assert_fatal(!error);
85
86    while (!test_finished) {
87        wait_for_timer_interrupt(env);
88        ZF_LOGV("Tick\n");
89        error = tm_update(&env->tm);
90        test_assert_fatal(!error);
91    }
92
93    end = timestamp(env);
94
95    test_gt(end, start);
96
97    error = tm_free_id(&env->tm, TIMER_ID);
98    test_assert_fatal(!error);
99
100    error = ltimer_reset(&env->ltimer);
101    test_assert_fatal(!error);
102
103    return sel4test_get_result();
104}
105DEFINE_TEST_BOOTSTRAP(TIMER0002, "Test that the timer moves between gettime and timeout calls", test_gettime_timeout,
106                      config_set(CONFIG_HAVE_TIMER))
107