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 }
Create()39 std::shared_ptr<TimerHandler> TimerHandler::Create()
40 {
41 int epollfd;
42 TimerFds fds;
43
44 epollfd = epoll_create(fds.size());
45 if (epollfd < 0) {
46 TIME_HILOGE(TIME_MODULE_SERVICE, "epoll_create %{public}d failed: %{public}s",
47 static_cast<int>(fds.size()), strerror(errno));
48 return nullptr;
49 }
50
51 for (size_t i = 0; i < fds.size(); i++) {
52 fds[i] = timerfd_create(alarm_to_clock_id[i], 0);
53 if (fds[i] < 0) {
54 TIME_HILOGE(TIME_MODULE_SERVICE, "timerfd_create %{public}d failed: %{public}s",
55 static_cast<int>(i), strerror(errno));
56 close(epollfd);
57 for (size_t j = 0; j < i; j++) {
58 close(fds[j]);
59 }
60 return nullptr;
61 }
62 }
63
64 std::shared_ptr<TimerHandler> handler = std::shared_ptr<TimerHandler>(new TimerHandler(fds, epollfd));
65 for (size_t i = 0; i < fds.size(); i++) {
66 epoll_event event {};
67 event.events = EPOLLIN | EPOLLWAKEUP;
68 event.data.u32 = i;
69
70 int err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i], &event);
71 if (err < 0) {
72 TIME_HILOGE(TIME_MODULE_SERVICE, "epoll_ctl(EPOLL_CTL_ADD) failed: %{public}s", strerror(errno));
73 return nullptr;
74 }
75 }
76 itimerspec spec {};
77
78 int err = timerfd_settime(fds[ALARM_TYPE_COUNT], TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &spec, nullptr);
79 if (err < 0) {
80 TIME_HILOGE(TIME_MODULE_SERVICE, "timerfd_settime() failed: %{public}s", strerror(errno));
81 return nullptr;
82 }
83
84 return handler;
85 }
86
TimerHandler(const TimerFds & fds,int epollfd)87 TimerHandler::TimerHandler(const TimerFds &fds, int epollfd)
88 : fds_ {fds}, epollFd_ {epollfd}
89 {
90 }
91
~TimerHandler()92 TimerHandler::~TimerHandler()
93 {
94 for (auto fd : fds_) {
95 epoll_ctl(epollFd_, EPOLL_CTL_DEL, fd, nullptr);
96 close(fd);
97 }
98 close(epollFd_);
99 }
100
Set(uint32_t type,std::chrono::nanoseconds when)101 int TimerHandler::Set(uint32_t type, std::chrono::nanoseconds when)
102 {
103 if (static_cast<size_t>(type) > ALARM_TYPE_COUNT) {
104 errno = EINVAL;
105 return -1;
106 }
107
108 auto second = std::chrono::duration_cast<std::chrono::seconds>(when);
109 TIME_HILOGI(TIME_MODULE_SERVICE, "triggerTime second: %{public}lld, triggerTime nanosecond: %{public}lld",
110 second.count(), (when - second).count());
111 timespec ts {second.count(), (when - second).count()};
112 itimerspec spec {timespec {}, ts};
113 return timerfd_settime(fds_[type], TFD_TIMER_ABSTIME, &spec, nullptr);
114 }
115
WaitForAlarm()116 uint32_t TimerHandler::WaitForAlarm()
117 {
118 epoll_event events[N_TIMER_FDS];
119
120 int nevents = 0;
121 do {
122 nevents = epoll_wait(epollFd_, events, N_TIMER_FDS, -1);
123 } while (nevents < 0 && errno == EINTR);
124
125 uint32_t result = 0;
126 for (int i = 0; i < nevents; i++) {
127 uint32_t alarm_idx = events[i].data.u32;
128 uint64_t unused;
129 ssize_t err = read(fds_[alarm_idx], &unused, sizeof(unused));
130 if (err < 0) {
131 if (alarm_idx == ALARM_TYPE_COUNT && errno == ECANCELED) {
132 result |= ALARM_TIME_CHANGE_MASK;
133 } else {
134 return err;
135 }
136 } else {
137 result |= (1 << alarm_idx);
138 }
139 }
140 return result;
141 }
142 } // MiscServices
143 } // OHOS