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 <sys/epoll.h>
18 #include <sys/timerfd.h>
19 #include <sstream>
20 #include "timer_handler.h"
21
22 namespace OHOS {
23 namespace MiscServices {
24 namespace {
25 static constexpr uint32_t ALARM_TIME_CHANGE_MASK = 1 << 16;
26 static constexpr clockid_t alarm_to_clock_id[N_TIMER_FDS] = {
27 CLOCK_REALTIME_ALARM,
28 CLOCK_REALTIME,
29 CLOCK_BOOTTIME_ALARM,
30 CLOCK_BOOTTIME,
31 CLOCK_MONOTONIC,
32 CLOCK_REALTIME,
33 };
34 }
35
Create()36 std::shared_ptr<TimerHandler> TimerHandler::Create()
37 {
38 int epollfd;
39 TimerFds fds;
40
41 epollfd = epoll_create(fds.size());
42 if (epollfd < 0) {
43 TIME_HILOGE(TIME_MODULE_SERVICE, "epoll_create %{public}d failed: %{public}s",
44 static_cast<int>(fds.size()), strerror(errno));
45 return nullptr;
46 }
47
48 std::string fdStr = "";
49 std::string typStr = "";
50 for (size_t i = 0; i < fds.size(); i++) {
51 fds[i] = timerfd_create(alarm_to_clock_id[i], 0);
52 if (fds[i] < 0) {
53 TIME_HILOGE(TIME_MODULE_SERVICE, "timerfd_create %{public}d failed: %{public}s",
54 static_cast<int>(i), strerror(errno));
55 close(epollfd);
56 for (size_t j = 0; j < i; j++) {
57 close(fds[j]);
58 }
59 return nullptr;
60 }
61 fdStr += std::to_string(fds[i]) + " ";
62 typStr += std::to_string(i) + " ";
63 }
64 TIME_HILOGW(TIME_MODULE_SERVICE, "create fd:[%{public}s], typ:[%{public}s]", fdStr.c_str(), typStr.c_str());
65
66 std::shared_ptr<TimerHandler> handler = std::shared_ptr<TimerHandler>(new TimerHandler(fds, epollfd));
67 for (size_t i = 0; i < fds.size(); i++) {
68 epoll_event event {};
69 event.events = EPOLLIN | EPOLLWAKEUP;
70 event.data.u32 = i;
71
72 int err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i], &event);
73 if (err < 0) {
74 TIME_HILOGE(TIME_MODULE_SERVICE, "epoll_ctl(EPOLL_CTL_ADD) failed: %{public}s", strerror(errno));
75 return nullptr;
76 }
77 }
78 itimerspec spec {};
79
80 int err = timerfd_settime(fds[ALARM_TYPE_COUNT], TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &spec, nullptr);
81 TIME_HILOGW(TIME_MODULE_SERVICE, "settime fd: %{public}d, res: %{public}d, errno: %{public}s",
82 fds[ALARM_TYPE_COUNT], err, strerror(errno));
83 if (err < 0 && errno != ECANCELED) {
84 return nullptr;
85 }
86
87 return handler;
88 }
89
TimerHandler(const TimerFds & fds,int epollfd)90 TimerHandler::TimerHandler(const TimerFds &fds, int epollfd)
91 : fds_ {fds}, epollFd_ {epollfd}
92 {
93 }
94
~TimerHandler()95 TimerHandler::~TimerHandler()
96 {
97 for (auto fd : fds_) {
98 epoll_ctl(epollFd_, EPOLL_CTL_DEL, fd, nullptr);
99 close(fd);
100 }
101 close(epollFd_);
102 }
103
Set(uint32_t type,std::chrono::nanoseconds when,std::chrono::steady_clock::time_point bootTime)104 int TimerHandler::Set(uint32_t type, std::chrono::nanoseconds when, std::chrono::steady_clock::time_point bootTime)
105 {
106 if (static_cast<size_t>(type) > ALARM_TYPE_COUNT) {
107 errno = EINVAL;
108 return -1;
109 }
110
111 auto second = std::chrono::duration_cast<std::chrono::seconds>(when);
112 TIME_SIMPLIFY_HILOGW(TIME_MODULE_SERVICE, "typ:%{public}d trig: %{public}lld %{public}lld, bt: %{public}lld",
113 type, second.count(), (when - second).count(), bootTime.time_since_epoch().count());
114 timespec ts {second.count(), (when - second).count()};
115 itimerspec spec {timespec {}, ts};
116 int ret = timerfd_settime(fds_[type], TFD_TIMER_ABSTIME, &spec, nullptr);
117 if (ret != 0) {
118 TIME_HILOGE(TIME_MODULE_SERVICE, "Set timer to kernel. ret: %{public}d. error: %{public}s",
119 ret, strerror(errno));
120 }
121 return ret;
122 }
123
WaitForAlarm()124 uint32_t TimerHandler::WaitForAlarm()
125 {
126 epoll_event events[N_TIMER_FDS];
127
128 int nevents = 0;
129 do {
130 nevents = epoll_wait(epollFd_, events, N_TIMER_FDS, -1);
131 } while (nevents < 0 && errno == EINTR);
132
133 uint32_t result = 0;
134 for (int i = 0; i < nevents; i++) {
135 uint32_t alarm_idx = events[i].data.u32;
136 uint64_t unused;
137 ssize_t err = read(fds_[alarm_idx], &unused, sizeof(unused));
138 if (err < 0) {
139 if (alarm_idx == ALARM_TYPE_COUNT && errno == ECANCELED) {
140 result |= ALARM_TIME_CHANGE_MASK;
141 } else {
142 return err;
143 }
144 } else {
145 result |= (1 << alarm_idx);
146 }
147 }
148 return result;
149 }
150 } // MiscServices
151 } // OHOS