• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::string fdStr = "";
53     std::string typStr = "";
54     for (size_t i = 0; i < fds.size(); i++) {
55         fds[i] = timerfd_create(alarm_to_clock_id[i], 0);
56         if (fds[i] < 0) {
57             TIME_HILOGE(TIME_MODULE_SERVICE, "timerfd_create %{public}d  failed: %{public}s",
58                 static_cast<int>(i), strerror(errno));
59             close(epollfd);
60             for (size_t j = 0; j < i; j++) {
61                 close(fds[j]);
62             }
63             return nullptr;
64         }
65         fdStr += std::to_string(fds[i]) + " ";
66         typStr += std::to_string(i) + " ";
67     }
68     TIME_HILOGW(TIME_MODULE_SERVICE, "create fd:[%{public}s], typ:[%{public}s]", fdStr.c_str(), typStr.c_str());
69 
70     std::shared_ptr<TimerHandler> handler = std::shared_ptr<TimerHandler>(new TimerHandler(fds, epollfd));
71     for (size_t i = 0; i < fds.size(); i++) {
72         epoll_event event {};
73         event.events = EPOLLIN | EPOLLWAKEUP;
74         event.data.u32 = i;
75 
76         int err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i], &event);
77         if (err < 0) {
78             TIME_HILOGE(TIME_MODULE_SERVICE, "epoll_ctl(EPOLL_CTL_ADD) failed: %{public}s", strerror(errno));
79             return nullptr;
80         }
81     }
82     itimerspec spec {};
83 
84     int err = timerfd_settime(fds[ALARM_TYPE_COUNT], TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &spec, nullptr);
85     TIME_HILOGW(TIME_MODULE_SERVICE, "settime fd: %{public}d, res: %{public}d, errno: %{public}s",
86         fds[ALARM_TYPE_COUNT], err, strerror(errno));
87     if (err < 0 && errno != ECANCELED) {
88         return nullptr;
89     }
90 
91     return handler;
92 }
93 
TimerHandler(const TimerFds & fds,int epollfd)94 TimerHandler::TimerHandler(const TimerFds &fds, int epollfd)
95     : fds_ {fds}, epollFd_ {epollfd}
96 {
97 }
98 
~TimerHandler()99 TimerHandler::~TimerHandler()
100 {
101     for (auto fd : fds_) {
102         epoll_ctl(epollFd_, EPOLL_CTL_DEL, fd, nullptr);
103         close(fd);
104     }
105     close(epollFd_);
106 }
107 
Set(uint32_t type,std::chrono::nanoseconds when,std::chrono::steady_clock::time_point bootTime)108 int TimerHandler::Set(uint32_t type, std::chrono::nanoseconds when, std::chrono::steady_clock::time_point bootTime)
109 {
110     if (static_cast<size_t>(type) > ALARM_TYPE_COUNT) {
111         errno = EINVAL;
112         return -1;
113     }
114 
115     auto second = std::chrono::duration_cast<std::chrono::seconds>(when);
116     TIME_SIMPLIFY_HILOGI(TIME_MODULE_SERVICE, "typ:%{public}d trig: %{public}lld %{public}lld, bt: %{public}lld",
117                          type, second.count(), (when - second).count(), bootTime.time_since_epoch().count());
118     timespec ts {second.count(), (when - second).count()};
119     itimerspec spec {timespec {}, ts};
120     int ret = timerfd_settime(fds_[type], TFD_TIMER_ABSTIME, &spec, nullptr);
121     if (ret != 0) {
122         TIME_HILOGE(TIME_MODULE_SERVICE, "Set timer to kernel. ret: %{public}d. error: %{public}s.",
123                     ret, strerror(errno));
124     }
125     return ret;
126 }
127 
WaitForAlarm()128 uint32_t TimerHandler::WaitForAlarm()
129 {
130     epoll_event events[N_TIMER_FDS];
131 
132     int nevents = 0;
133     do {
134         nevents = epoll_wait(epollFd_, events, N_TIMER_FDS, -1);
135     } while (nevents < 0 && errno == EINTR);
136 
137     uint32_t result = 0;
138     for (int i = 0; i < nevents; i++) {
139         uint32_t alarm_idx = events[i].data.u32;
140         uint64_t unused;
141         ssize_t err = read(fds_[alarm_idx], &unused, sizeof(unused));
142         if (err < 0) {
143             if (alarm_idx == ALARM_TYPE_COUNT && errno == ECANCELED) {
144                 result |= ALARM_TIME_CHANGE_MASK;
145             } else {
146                 return err;
147             }
148         } else {
149             result |= (1 << alarm_idx);
150         }
151     }
152     return result;
153 }
154 } // MiscServices
155 } // OHOS