1 /*
2 * Copyright (c) 2023 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 "epoll_manager.h"
17
18 #include <unistd.h>
19
20 #include "devicestatus_define.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "EpollManager"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 namespace {
29 constexpr int32_t MAX_N_EVENTS { 64 };
30 } // namespace
31
~EpollManager()32 EpollManager::~EpollManager()
33 {
34 Close();
35 }
36
Open()37 int32_t EpollManager::Open()
38 {
39 if (epollFd_ != -1) {
40 return RET_OK;
41 }
42 epollFd_ = ::epoll_create1(EPOLL_CLOEXEC);
43 if (epollFd_ == -1) {
44 FI_HILOGE("epoll_create1 failed:%{public}s", ::strerror(errno));
45 return RET_ERR;
46 }
47 return RET_OK;
48 }
49
Close()50 void EpollManager::Close()
51 {
52 if (epollFd_ != -1) {
53 if (::close(epollFd_) != 0) {
54 FI_HILOGE("close(%{public}d) failed:%{public}s", epollFd_, ::strerror(errno));
55 }
56 epollFd_ = -1;
57 }
58 }
59
Add(IEpollEventSource & source)60 int32_t EpollManager::Add(IEpollEventSource &source)
61 {
62 CALL_DEBUG_ENTER;
63 struct epoll_event ev {};
64 ev.events = source.GetEvents();
65 ev.data.ptr = &source;
66
67 int32_t ret = ::epoll_ctl(epollFd_, EPOLL_CTL_ADD, source.GetFd(), &ev);
68 if (ret != 0) {
69 FI_HILOGE("epoll_ctl failed:%{public}s", ::strerror(errno));
70 return RET_ERR;
71 }
72 return RET_OK;
73 }
74
Remove(IEpollEventSource & source)75 void EpollManager::Remove(IEpollEventSource &source)
76 {
77 CALL_DEBUG_ENTER;
78 int32_t ret = ::epoll_ctl(epollFd_, EPOLL_CTL_DEL, source.GetFd(), nullptr);
79 if (ret != 0) {
80 FI_HILOGE("epoll_ctl failed:%{public}s", ::strerror(errno));
81 }
82 }
83
Update(IEpollEventSource & source)84 int32_t EpollManager::Update(IEpollEventSource &source)
85 {
86 CALL_DEBUG_ENTER;
87 struct epoll_event ev {};
88 ev.events = source.GetEvents();
89 ev.data.ptr = &source;
90
91 int32_t ret = ::epoll_ctl(epollFd_, EPOLL_CTL_MOD, source.GetFd(), &ev);
92 if (ret != 0) {
93 FI_HILOGE("epoll_ctl failed:%{public}s", ::strerror(errno));
94 return RET_ERR;
95 }
96 return RET_OK;
97 }
98
Wait(struct epoll_event * events,int32_t maxevents)99 int32_t EpollManager::Wait(struct epoll_event *events, int32_t maxevents)
100 {
101 return WaitTimeout(events, maxevents, -1);
102 }
103
WaitTimeout(struct epoll_event * events,int32_t maxevents,int32_t timeout)104 int32_t EpollManager::WaitTimeout(struct epoll_event *events, int32_t maxevents, int32_t timeout)
105 {
106 int32_t ret = ::epoll_wait(epollFd_, events, maxevents, timeout);
107 if (ret < 0) {
108 FI_HILOGE("epoll_wait failed:%{public}s", ::strerror(errno));
109 } else if (ret == 0) {
110 FI_HILOGE("epoll_wait timeout");
111 }
112 return ret;
113 }
114
Dispatch(const struct epoll_event & ev)115 void EpollManager::Dispatch(const struct epoll_event &ev)
116 {
117 CALL_DEBUG_ENTER;
118 if ((ev.events & EPOLLIN) == EPOLLIN) {
119 DispatchOne(ev);
120 } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) {
121 FI_HILOGE("Epoll hangup:%{public}s", ::strerror(errno));
122 }
123 }
124
DispatchOne(const struct epoll_event & ev)125 void EpollManager::DispatchOne(const struct epoll_event &ev)
126 {
127 struct epoll_event evs[MAX_N_EVENTS];
128 int32_t cnt = WaitTimeout(evs, MAX_N_EVENTS, 0);
129
130 for (int32_t index = 0; index < cnt; ++index) {
131 IEpollEventSource *source = reinterpret_cast<IEpollEventSource *>(evs[index].data.ptr);
132 CHKPC(source);
133 if ((evs[index].events & EPOLLIN) == EPOLLIN) {
134 source->Dispatch(evs[index]);
135 } else if ((evs[index].events & (EPOLLHUP | EPOLLERR)) != 0) {
136 FI_HILOGE("Epoll hangup:%{public}s", ::strerror(errno));
137 }
138 }
139 }
140 } // namespace DeviceStatus
141 } // namespace Msdp
142 } // namespace OHOS