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