8
8
#include <unistd.h>
9
9
10
10
#include <zephyr/ztest.h>
11
+ #include <zephyr/logging/log.h>
11
12
12
13
#define SECS_TO_SLEEP 2
13
14
#define DURATION_SECS 1
14
15
#define DURATION_NSECS 0
15
16
#define PERIOD_SECS 0
16
17
#define PERIOD_NSECS 100000000
17
18
19
+ #define TEST_SIGNAL_VAL SIGTSTP
20
+
21
+ LOG_MODULE_REGISTER (timer_test );
22
+
18
23
static int exp_count ;
24
+ static timer_t timerid = -1 ;
19
25
20
26
void handler (union sigval val )
21
27
{
22
- printk ("Handler Signal value :%d for %d times\n" , val .sival_int ,
23
- ++ exp_count );
28
+ ++ exp_count ;
29
+ LOG_DBG ("Handler Signal value %d for %d times" , val .sival_int , exp_count );
30
+ zassert_equal (val .sival_int , TEST_SIGNAL_VAL );
24
31
}
25
32
26
33
void test_timer (int sigev_notify )
27
34
{
28
- int ret ;
29
- struct sigevent sig = { 0 };
30
- timer_t timerid ;
35
+ struct sigevent sig = {0 };
31
36
struct itimerspec value , ovalue ;
32
37
struct timespec ts , te ;
33
38
int64_t nsecs_elapsed , secs_elapsed ;
34
39
35
40
exp_count = 0 ;
36
41
sig .sigev_notify = sigev_notify ;
37
42
sig .sigev_notify_function = handler ;
38
- sig .sigev_value .sival_int = 20 ;
39
-
40
- if (sigev_notify == SIGEV_SIGNAL )
41
- printk ("POSIX timer test SIGEV_SIGNAL\n" );
42
- else
43
- printk ("POSIX timer test SIGEV_THREAD\n" );
44
-
45
- ret = timer_create (CLOCK_MONOTONIC , & sig , & timerid );
43
+ sig .sigev_value .sival_int = TEST_SIGNAL_VAL ;
46
44
47
45
/*TESTPOINT: Check if timer is created successfully*/
48
- zassert_false ( ret , "POSIX timer create failed" );
46
+ zassert_ok ( timer_create ( CLOCK_MONOTONIC , & sig , & timerid ) );
49
47
50
48
value .it_value .tv_sec = DURATION_SECS ;
51
49
value .it_value .tv_nsec = DURATION_NSECS ;
52
50
value .it_interval .tv_sec = PERIOD_SECS ;
53
51
value .it_interval .tv_nsec = PERIOD_NSECS ;
54
- ret = timer_settime (timerid , 0 , & value , & ovalue );
52
+ zassert_ok ( timer_settime (timerid , 0 , & value , & ovalue ) );
55
53
usleep (100 * USEC_PER_MSEC );
56
- ret = timer_gettime (timerid , & value );
57
- zassert_false (ret , "Failed to get time to expire." );
58
-
59
- if (ret == 0 ) {
60
- printk ("Timer fires every %d secs and %d nsecs\n" ,
61
- (int ) value .it_interval .tv_sec ,
62
- (int ) value .it_interval .tv_nsec );
63
- printk ("Time remaining to fire %d secs and %d nsecs\n" ,
64
- (int ) value .it_value .tv_sec ,
65
- (int ) value .it_value .tv_nsec );
66
- }
67
-
68
- clock_gettime (CLOCK_MONOTONIC , & ts );
69
-
70
54
/*TESTPOINT: Check if timer has started successfully*/
71
- zassert_false ( ret , "POSIX timer failed to start" );
55
+ zassert_ok ( timer_gettime ( timerid , & value ) );
72
56
73
- sleep (SECS_TO_SLEEP );
57
+ LOG_DBG ("Timer fires every %d secs and %d nsecs" , (int )value .it_interval .tv_sec ,
58
+ (int )value .it_interval .tv_nsec );
59
+ LOG_DBG ("Time remaining to fire %d secs and %d nsecs" , (int )value .it_value .tv_sec ,
60
+ (int )value .it_value .tv_nsec );
74
61
62
+ clock_gettime (CLOCK_MONOTONIC , & ts );
63
+ sleep (SECS_TO_SLEEP );
75
64
clock_gettime (CLOCK_MONOTONIC , & te );
76
- zassert_equal (ret , 0 , "Number of timer overruns is incorrect" );
77
- timer_delete (timerid );
78
65
79
66
if (te .tv_nsec >= ts .tv_nsec ) {
80
67
secs_elapsed = te .tv_sec - ts .tv_sec ;
@@ -95,34 +82,44 @@ void test_timer(int sigev_notify)
95
82
exp_count , expected_signal_count );
96
83
}
97
84
98
- ZTEST (timer , test_timer )
85
+ ZTEST (timer , test_SIGEV_SIGNAL )
99
86
{
100
87
test_timer (SIGEV_SIGNAL );
88
+ }
89
+
90
+ ZTEST (timer , test_SIGEV_THREAD )
91
+ {
101
92
test_timer (SIGEV_THREAD );
102
93
}
103
94
104
95
ZTEST (timer , test_timer_overrun )
105
96
{
106
- timer_t timerid ;
107
97
struct sigevent sig = { 0 };
108
98
struct itimerspec value ;
109
99
110
100
sig .sigev_notify = SIGEV_NONE ;
111
101
112
- timer_create (CLOCK_MONOTONIC , & sig , & timerid );
102
+ zassert_ok ( timer_create (CLOCK_MONOTONIC , & sig , & timerid ) );
113
103
114
104
/*Set the timer to expire every 500 milliseconds*/
115
105
value .it_interval .tv_sec = 0 ;
116
106
value .it_interval .tv_nsec = 500000000 ;
117
107
value .it_value .tv_sec = 0 ;
118
108
value .it_value .tv_nsec = 500000000 ;
119
- timer_settime (timerid , 0 , & value , NULL );
109
+ zassert_ok ( timer_settime (timerid , 0 , & value , NULL ) );
120
110
k_sleep (K_MSEC (2500 ));
121
111
122
- int overruns = timer_getoverrun (timerid );
112
+ zassert_equal (timer_getoverrun (timerid ), 4 , "Number of overruns is incorrect" );
113
+ }
114
+
115
+ static void after (void * arg )
116
+ {
117
+ ARG_UNUSED (arg );
123
118
124
- timer_delete (timerid );
125
- zassert_equal (overruns , 4 , "Number of overruns is incorrect" );
119
+ if (timerid != -1 ) {
120
+ (void )timer_delete (timerid );
121
+ timerid = -1 ;
122
+ }
126
123
}
127
124
128
- ZTEST_SUITE (timer , NULL , NULL , NULL , NULL , NULL );
125
+ ZTEST_SUITE (timer , NULL , NULL , NULL , after , NULL );
0 commit comments