1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <unistd.h>
32 #include <signal.h>
33 #include <time.h>
34 #include <sys/time.h>
35 #include "osTest.h"
36 #include <stdio.h>
37 #include <string.h>
38 #include "lt_timer_test.h"
39
40 static int g_sigHdlCnt01;
41 static int g_sigHdlCnt02;
42 static int g_sigHdlCnt03;
43
TempSigHandler(union sigval v)44 static void TempSigHandler(union sigval v)
45 {
46 LogPrintln("This is TempSigHandler ...\r\n");
47 (*(void(*)(void))(v.sival_ptr))();
48 }
49
TempSigHandler01(void)50 static void TempSigHandler01(void)
51 {
52 g_sigHdlCnt01++;
53 }
54
TempSigHandler02(void)55 static void TempSigHandler02(void)
56 {
57 g_sigHdlCnt02++;
58 }
59
TimerTest(void)60 static int TimerTest(void)
61 {
62 timer_t timerid01, timerid02, timerid03;
63 struct sigevent sev;
64 struct itimerspec its;
65 int ret;
66 int i;
67
68 (void)memset(&sev, 0, sizeof(struct sigevent));
69 sev.sigev_notify = SIGEV_THREAD;
70 sev.sigev_notify_function = TempSigHandler;
71 sev.sigev_value.sival_ptr = (void *)TempSigHandler01;
72
73 /* Start the timer */
74 its.it_value.tv_sec = 3; // 3, timer time 3 seconds.
75 its.it_value.tv_nsec = 0;
76 its.it_interval.tv_sec = its.it_value.tv_sec;
77 its.it_interval.tv_nsec = its.it_value.tv_nsec;
78 ret = timer_create(CLOCK_REALTIME, &sev, &timerid01);
79 LogPrintln("timer_settime %p: %d", timerid01, ret);
80 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
81
82 ret = timer_settime(timerid01, 0, &its, nullptr);
83 LogPrintln("timer_create %p: %d", timerid01, ret);
84 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
85
86 its.it_value.tv_sec = 4; // 4, timer time 4 seconds.
87 its.it_value.tv_nsec = 0;
88 its.it_interval.tv_sec = its.it_value.tv_sec;
89 its.it_interval.tv_nsec = its.it_value.tv_nsec;
90
91 sev.sigev_value.sival_ptr = (void *)TempSigHandler02;
92 ret = timer_create(CLOCK_REALTIME, &sev, &timerid02);
93 LogPrintln("timer_settime %p: %d", timerid02, ret);
94 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
95
96 ret = timer_settime(timerid02, 0, &its, nullptr);
97 LogPrintln("timer_settime %p: %d", timerid02, ret);
98 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
99
100 its.it_value.tv_sec = 5; // 5, timer time 5 seconds.
101 its.it_value.tv_nsec = 0;
102 its.it_interval.tv_sec = its.it_value.tv_sec;
103 its.it_interval.tv_nsec = its.it_value.tv_nsec;
104
105 sleep(20); // 20, sleep seconds for timer.
106 ret = timer_delete(timerid01);
107 LogPrintln("timer_delete %p %d", timerid01, ret);
108 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
109
110 ret = timer_delete(timerid02);
111 LogPrintln("timer_delete %p %d", timerid02, ret);
112 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
113
114 ICUNIT_ASSERT_NOT_EQUAL(g_sigHdlCnt01, 0, g_sigHdlCnt01);
115 ICUNIT_ASSERT_NOT_EQUAL(g_sigHdlCnt02, 0, g_sigHdlCnt02);
116 return 0;
117
118 }
119
TimerTest005(void)120 void TimerTest005(void)
121 {
122 TEST_ADD_CASE(__FUNCTION__, TimerTest, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
123 }
124