Skip to content

Commit 49fbfbb

Browse files
youvedeep-singhAnas Nashif
authored andcommitted
tests: kernel: posix: timer: POSIX timer test.
Added test for POSIX timer APIs. Signed-off-by: Youvedeep Singh <[email protected]>
1 parent 8d040f1 commit 49fbfbb

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
2+
project(NONE)
3+
4+
target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/include/posix)
5+
FILE(GLOB app_sources src/*.c)
6+
target_sources(app PRIVATE ${app_sources})

tests/kernel/posix/timer/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_TEST=y
2+
CONFIG_PTHREAD_IPC=y
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <time.h>
8+
#include <pthread.h>
9+
#include <errno.h>
10+
#include <tc_util.h>
11+
12+
#define SECS_TO_SLEEP 2
13+
#define DURATION_SECS 1
14+
#define DURATION_NSECS 0
15+
#define PERIOD_SECS 0
16+
#define PERIOD_NSECS 100000000
17+
18+
static int exp_count;
19+
20+
void handler(union sigval val)
21+
{
22+
TC_PRINT("Handler Signal value :%d for %d times\n", val.sival_int,
23+
++exp_count);
24+
}
25+
26+
void main(void)
27+
{
28+
int ret, status = TC_FAIL;
29+
struct sigevent sig;
30+
timer_t timerid;
31+
struct itimerspec value, ovalue;
32+
struct timespec ts, te;
33+
s64_t nsecs_elapsed, secs_elapsed, total_secs_timer;
34+
35+
sig.sigev_notify = SIGEV_SIGNAL;
36+
sig.sigev_notify_function = handler;
37+
sig.sigev_value.sival_int = 20;
38+
sig.sigev_notify_attributes = NULL;
39+
TC_START("POSIX timer test\n");
40+
ret = timer_create(CLOCK_MONOTONIC, &sig, &timerid);
41+
42+
if (ret == 0) {
43+
value.it_value.tv_sec = DURATION_SECS;
44+
value.it_value.tv_nsec = DURATION_NSECS;
45+
value.it_interval.tv_sec = PERIOD_SECS;
46+
value.it_interval.tv_nsec = PERIOD_NSECS;
47+
ret = timer_settime(timerid, 0, &value, &ovalue);
48+
clock_gettime(CLOCK_MONOTONIC, &ts);
49+
50+
if (ret == 0) {
51+
sleep(SECS_TO_SLEEP);
52+
} else {
53+
TC_PRINT("posix timer failed to start, error %d\n",
54+
errno);
55+
goto test_end;
56+
}
57+
58+
clock_gettime(CLOCK_MONOTONIC, &te);
59+
timer_delete(timerid);
60+
} else {
61+
TC_ERROR("POSIX timer create failed with %d\n", errno);
62+
goto test_end;
63+
}
64+
65+
if (te.tv_nsec >= ts.tv_nsec) {
66+
secs_elapsed = te.tv_sec - ts.tv_sec;
67+
nsecs_elapsed = te.tv_nsec - ts.tv_nsec;
68+
} else {
69+
nsecs_elapsed = NSEC_PER_SEC + te.tv_nsec - ts.tv_nsec;
70+
secs_elapsed = (te.tv_sec - ts.tv_sec - 1);
71+
}
72+
73+
total_secs_timer = (u64_t) (value.it_value.tv_sec * NSEC_PER_SEC +
74+
value.it_value.tv_nsec + exp_count *
75+
(value.it_interval.tv_sec * NSEC_PER_SEC +
76+
value.it_interval.tv_nsec)) / NSEC_PER_SEC;
77+
78+
status = (total_secs_timer == secs_elapsed) ? TC_PASS : TC_FAIL;
79+
80+
test_end:
81+
TC_END_REPORT(status);
82+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tests:
2+
test:
3+
tags: core

0 commit comments

Comments
 (0)