1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <unistd.h>
17 #include <cstring>
18 #include <sys/epoll.h>
19 #include <sys/timerfd.h>
20 #include <sys/time.h>
21 #include <sstream>
22 #include <fstream>
23 #include <iostream>
24 #include "timer_handler.h"
25 
26 namespace OHOS {
27 namespace MiscServices {
28 namespace {
29 static const uint32_t ALARM_TIME_CHANGE_MASK = 1 << 16;
30 static const clockid_t alarm_to_clock_id[N_TIMER_FDS] = {
31     CLOCK_REALTIME_ALARM,
32     CLOCK_REALTIME,
33     CLOCK_BOOTTIME_ALARM,
34     CLOCK_BOOTTIME,
35     CLOCK_MONOTONIC,
36     CLOCK_REALTIME,
37 };
38 }
39 
Create()40 std::shared_ptr<TimerHandler> TimerHandler::Create()
41 {
42     int epollfd;
43     TimerFds fds;
44 
45     epollfd = epoll_create(fds.size());
46     if (epollfd < 0) {
47             TIME_HILOGE(TIME_MODULE_SERVICE, "epoll_create %{public}d failed: %{public}s",
48                 static_cast<int>(fds.size()), strerror(errno));
49             return nullptr;
50     }
51 
52     for (size_t i = 0; i < fds.size(); i++) {
53         fds[i] = timerfd_create(alarm_to_clock_id[i], 0);
54         if (fds[i] < 0) {
55             TIME_HILOGE(TIME_MODULE_SERVICE, "timerfd_create %{public}d  failed: %{public}s",
56                 static_cast<int>(i), strerror(errno));
57             close(epollfd);
58             for (size_t j = 0; j < i; j++) {
59                 close(fds[j]);
60             }
61             return nullptr;
62         }
63     }
64 
65     std::shared_ptr<TimerHandler> handler = std::shared_ptr<TimerHandler>(new TimerHandler(fds, epollfd));
66     for (size_t i = 0; i < fds.size(); i++) {
67         epoll_event event {};
68         event.events = EPOLLIN | EPOLLWAKEUP;
69         event.data.u32 = i;
70 
71         int err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i], &event);
72         if (err < 0) {
73             TIME_HILOGE(TIME_MODULE_SERVICE, "epoll_ctl(EPOLL_CTL_ADD) failed: %{public}s", strerror(errno));
74             return nullptr;
75         }
76     }
77     itimerspec spec {};
78 
79     int err = timerfd_settime(fds[ALARM_TYPE_COUNT], TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &spec, nullptr);
80     if (err < 0) {
81         TIME_HILOGE(TIME_MODULE_SERVICE, "timerfd_settime() failed: %{public}s", strerror(errno));
82         return nullptr;
83     }
84 
85     return handler;
86 }
87 
TimerHandler(const TimerFds & fds,int epollfd)88 TimerHandler::TimerHandler(const TimerFds &fds, int epollfd)
89     : fds_ {fds}, epollFd_ {epollfd}
90 {
91 }
92 
~TimerHandler()93 TimerHandler::~TimerHandler()
94 {
95     for (auto fd : fds_) {
96         epoll_ctl(epollFd_, EPOLL_CTL_DEL, fd, nullptr);
97         close(fd);
98     }
99     close(epollFd_);
100 }
101 
Set(uint32_t type,std::chrono::nanoseconds when,std::chrono::steady_clock::time_point bootTime)102 int TimerHandler::Set(uint32_t type, std::chrono::nanoseconds when, std::chrono::steady_clock::time_point bootTime)
103 {
104     if (static_cast<size_t>(type) > ALARM_TYPE_COUNT) {
105         errno = EINVAL;
106         return -1;
107     }
108 
109     auto second = std::chrono::duration_cast<std::chrono::seconds>(when);
110     TIME_SIMPLIFY_HILOGI(TIME_MODULE_SERVICE, "typ:%{public}d trig: %{public}lld %{public}lld, bt: %{public}lld",
111                          type, second.count(), (when - second).count(), bootTime.time_since_epoch().count());
112     timespec ts {second.count(), (when - second).count()};
113     itimerspec spec {timespec {}, ts};
114     int ret = timerfd_settime(fds_[type], TFD_TIMER_ABSTIME, &spec, nullptr);
115     if (ret != 0) {
116         TIME_HILOGE(TIME_MODULE_SERVICE, "Set timer to kernel. ret: %{public}d. error: %{public}s.",
117                     ret, strerror(errno));
118     }
119     return ret;
120 }
121 
WaitForAlarm()122 uint32_t TimerHandler::WaitForAlarm()
123 {
124     epoll_event events[N_TIMER_FDS];
125 
126     int nevents = 0;
127     do {
128         nevents = epoll_wait(epollFd_, events, N_TIMER_FDS, -1);
129     } while (nevents < 0 && errno == EINTR);
130 
131     uint32_t result = 0;
132     for (int i = 0; i < nevents; i++) {
133         uint32_t alarm_idx = events[i].data.u32;
134         uint64_t unused;
135         ssize_t err = read(fds_[alarm_idx], &unused, sizeof(unused));
136         if (err < 0) {
137             if (alarm_idx == ALARM_TYPE_COUNT && errno == ECANCELED) {
138                 result |= ALARM_TIME_CHANGE_MASK;
139             } else {
140                 return err;
141             }
142         } else {
143             result |= (1 << alarm_idx);
144         }
145     }
146     return result;
147 }
148 } // MiscServices
149 } // OHOS