• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ret = memset_s(&sev, sizeof(struct sigevent), 0, sizeof(struct sigevent));
69     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
70     sev.sigev_notify = SIGEV_THREAD;
71     sev.sigev_notify_function = TempSigHandler;
72     sev.sigev_value.sival_ptr = reinterpret_cast<void *>(TempSigHandler01);
73 
74     /* Start the timer */
75     its.it_value.tv_sec = 3; // 3, timer time 3 seconds.
76     its.it_value.tv_nsec = 0;
77     its.it_interval.tv_sec = its.it_value.tv_sec;
78     its.it_interval.tv_nsec = its.it_value.tv_nsec;
79     ret = timer_create(CLOCK_REALTIME, &sev, &timerid01);
80     LogPrintln("timer_settime %p: %d", timerid01, ret);
81     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
82 
83     ret = timer_settime(timerid01, 0, &its, nullptr);
84     LogPrintln("timer_create %p: %d", timerid01, ret);
85     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
86 
87     its.it_value.tv_sec = 4; // 4, timer time 4 seconds.
88     its.it_value.tv_nsec = 0;
89     its.it_interval.tv_sec = its.it_value.tv_sec;
90     its.it_interval.tv_nsec = its.it_value.tv_nsec;
91 
92     sev.sigev_value.sival_ptr = reinterpret_cast<void *>(TempSigHandler02);
93     ret = timer_create(CLOCK_REALTIME, &sev, &timerid02);
94     LogPrintln("timer_settime %p: %d", timerid02, ret);
95     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
96 
97     ret = timer_settime(timerid02, 0, &its, nullptr);
98     LogPrintln("timer_settime %p: %d", timerid02, ret);
99     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
100 
101     its.it_value.tv_sec = 5; // 5, timer time 5 seconds.
102     its.it_value.tv_nsec = 0;
103     its.it_interval.tv_sec = its.it_value.tv_sec;
104     its.it_interval.tv_nsec = its.it_value.tv_nsec;
105 
106     sleep(20); // 20, sleep seconds for timer.
107     ret = timer_delete(timerid01);
108     LogPrintln("timer_delete %p %d", timerid01, ret);
109     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
110 
111     ret = timer_delete(timerid02);
112     LogPrintln("timer_delete %p %d", timerid02, ret);
113     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
114 
115     ICUNIT_ASSERT_NOT_EQUAL(g_sigHdlCnt01, 0, g_sigHdlCnt01);
116     ICUNIT_ASSERT_NOT_EQUAL(g_sigHdlCnt02, 0, g_sigHdlCnt02);
117     return 0;
118 
119 }
120 
TimerTest005(void)121 void TimerTest005(void)
122 {
123     TEST_ADD_CASE(__FUNCTION__, TimerTest, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
124 }
125