1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <errno.h>
17570af302Sopenharmony_ci#include <signal.h>
18570af302Sopenharmony_ci#include <stdio.h>
19570af302Sopenharmony_ci#include <stdlib.h>
20570af302Sopenharmony_ci#include <time.h>
21570af302Sopenharmony_ci#include "test.h"
22570af302Sopenharmony_ci
23570af302Sopenharmony_ci#define Nanoseconds (1000000000)
24570af302Sopenharmony_cistatic int count = 0;
25570af302Sopenharmony_ci
26570af302Sopenharmony_ciextern int __timer_gettime64(timer_t, struct itimerspec *);
27570af302Sopenharmony_ci
28570af302Sopenharmony_civoid timerHandler(int sig)
29570af302Sopenharmony_ci{
30570af302Sopenharmony_ci    count++;
31570af302Sopenharmony_ci    return;
32570af302Sopenharmony_ci}
33570af302Sopenharmony_ci
34570af302Sopenharmony_civoid exception_handler(int sig)
35570af302Sopenharmony_ci{
36570af302Sopenharmony_ci    exit(t_status);
37570af302Sopenharmony_ci}
38570af302Sopenharmony_ci
39570af302Sopenharmony_ci/**
40570af302Sopenharmony_ci * @tc.name      : timer_gettime_0100
41570af302Sopenharmony_ci * @tc.desc      : Get the remaining time of the timer
42570af302Sopenharmony_ci * @tc.level     : Level 0
43570af302Sopenharmony_ci */
44570af302Sopenharmony_civoid timer_gettime_0100(void)
45570af302Sopenharmony_ci{
46570af302Sopenharmony_ci    timer_t timerid;
47570af302Sopenharmony_ci    struct sigevent sev;
48570af302Sopenharmony_ci    struct itimerspec its;
49570af302Sopenharmony_ci    struct itimerspec tmp;
50570af302Sopenharmony_ci
51570af302Sopenharmony_ci    sev.sigev_notify = SIGEV_SIGNAL;
52570af302Sopenharmony_ci    sev.sigev_signo = SIGUSR1;
53570af302Sopenharmony_ci    sev.sigev_value.sival_ptr = &timerid;
54570af302Sopenharmony_ci    signal(SIGUSR1, timerHandler);
55570af302Sopenharmony_ci
56570af302Sopenharmony_ci    if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) == -1) {
57570af302Sopenharmony_ci        t_error("%s timer create failed", __func__);
58570af302Sopenharmony_ci        return;
59570af302Sopenharmony_ci    }
60570af302Sopenharmony_ci
61570af302Sopenharmony_ci    its.it_value.tv_sec = 1;
62570af302Sopenharmony_ci    its.it_value.tv_nsec = 0;
63570af302Sopenharmony_ci    its.it_interval.tv_sec = 1;
64570af302Sopenharmony_ci    its.it_interval.tv_nsec = 0;
65570af302Sopenharmony_ci
66570af302Sopenharmony_ci    if (timer_settime(timerid, 0, &its, NULL) == -1) {
67570af302Sopenharmony_ci        t_error("%s timer set time failed", __func__);
68570af302Sopenharmony_ci        return;
69570af302Sopenharmony_ci    }
70570af302Sopenharmony_ci
71570af302Sopenharmony_ci    if (timer_gettime(timerid, &tmp) == -1 && errno == EINVAL) {
72570af302Sopenharmony_ci        t_error("%s timer gettimer failed", __func__);
73570af302Sopenharmony_ci    }
74570af302Sopenharmony_ci
75570af302Sopenharmony_ci    if (tmp.it_value.tv_sec >= 1 || tmp.it_value.tv_nsec >= Nanoseconds) {
76570af302Sopenharmony_ci        t_error("%s get time failed", __func__);
77570af302Sopenharmony_ci    }
78570af302Sopenharmony_ci
79570af302Sopenharmony_ci    while (count <= 0) {
80570af302Sopenharmony_ci        sleep(1);
81570af302Sopenharmony_ci    }
82570af302Sopenharmony_ci
83570af302Sopenharmony_ci    if (timer_delete(timerid) != 0) {
84570af302Sopenharmony_ci        t_error("%s timer_delete failed", __func__);
85570af302Sopenharmony_ci    }
86570af302Sopenharmony_ci}
87570af302Sopenharmony_ci
88570af302Sopenharmony_ci/**
89570af302Sopenharmony_ci * @tc.name      : timer_gettime_0200
90570af302Sopenharmony_ci * @tc.desc      : The return value of the function when the parameter is abnormal
91570af302Sopenharmony_ci * @tc.level     : Level 2
92570af302Sopenharmony_ci */
93570af302Sopenharmony_civoid timer_gettime_0200(void)
94570af302Sopenharmony_ci{
95570af302Sopenharmony_ci    int result = timer_gettime(NULL, NULL);
96570af302Sopenharmony_ci    if (result != -1 && errno != EINVAL) {
97570af302Sopenharmony_ci        t_error("%s failed result = %d", __func__, result);
98570af302Sopenharmony_ci    }
99570af302Sopenharmony_ci}
100570af302Sopenharmony_ci
101570af302Sopenharmony_ci/**
102570af302Sopenharmony_ci * @tc.name      : timer_gettime64_0200
103570af302Sopenharmony_ci * @tc.desc      : The return value of the function when the parameter is abnormal
104570af302Sopenharmony_ci * @tc.level     : Level 2
105570af302Sopenharmony_ci */
106570af302Sopenharmony_civoid timer_gettime64_0200(void)
107570af302Sopenharmony_ci{
108570af302Sopenharmony_ci    int result = __timer_gettime64(NULL, NULL);
109570af302Sopenharmony_ci    if (result != -1 && errno != EINVAL) {
110570af302Sopenharmony_ci        t_error("%s failed result = %d", __func__, result);
111570af302Sopenharmony_ci    }
112570af302Sopenharmony_ci}
113570af302Sopenharmony_ci
114570af302Sopenharmony_ciint main(int argc, char *argv[])
115570af302Sopenharmony_ci{
116570af302Sopenharmony_ci    timer_gettime_0100();
117570af302Sopenharmony_ci    timer_gettime_0200();
118570af302Sopenharmony_ci    timer_gettime64_0200();
119570af302Sopenharmony_ci    return t_status;
120570af302Sopenharmony_ci}