1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#include "epoll_manager.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#include <unistd.h>
19f857971dSopenharmony_ci
20f857971dSopenharmony_ci#include "devicestatus_define.h"
21f857971dSopenharmony_ci
22f857971dSopenharmony_ci#undef LOG_TAG
23f857971dSopenharmony_ci#define LOG_TAG "EpollManager"
24f857971dSopenharmony_ci
25f857971dSopenharmony_cinamespace OHOS {
26f857971dSopenharmony_cinamespace Msdp {
27f857971dSopenharmony_cinamespace DeviceStatus {
28f857971dSopenharmony_cinamespace {
29f857971dSopenharmony_ciconstexpr int32_t MAX_N_EVENTS { 64 };
30f857971dSopenharmony_ci} // namespace
31f857971dSopenharmony_ci
32f857971dSopenharmony_ciEpollManager::~EpollManager()
33f857971dSopenharmony_ci{
34f857971dSopenharmony_ci    Close();
35f857971dSopenharmony_ci}
36f857971dSopenharmony_ci
37f857971dSopenharmony_cibool EpollManager::Open()
38f857971dSopenharmony_ci{
39f857971dSopenharmony_ci    if (epollFd_ != -1) {
40f857971dSopenharmony_ci        return true;
41f857971dSopenharmony_ci    }
42f857971dSopenharmony_ci    epollFd_ = ::epoll_create1(EPOLL_CLOEXEC);
43f857971dSopenharmony_ci    if (epollFd_ == -1) {
44f857971dSopenharmony_ci        FI_HILOGE("epoll_create1 failed:%{public}s", ::strerror(errno));
45f857971dSopenharmony_ci        return false;
46f857971dSopenharmony_ci    }
47f857971dSopenharmony_ci    return true;
48f857971dSopenharmony_ci}
49f857971dSopenharmony_ci
50f857971dSopenharmony_civoid EpollManager::Close()
51f857971dSopenharmony_ci{
52f857971dSopenharmony_ci    if (epollFd_ != -1) {
53f857971dSopenharmony_ci        if (::close(epollFd_) != 0) {
54f857971dSopenharmony_ci            FI_HILOGE("close(%d) failed:%{public}s", epollFd_, ::strerror(errno));
55f857971dSopenharmony_ci        }
56f857971dSopenharmony_ci        epollFd_ = -1;
57f857971dSopenharmony_ci    }
58f857971dSopenharmony_ci}
59f857971dSopenharmony_ci
60f857971dSopenharmony_cibool EpollManager::Add(std::shared_ptr<IEpollEventSource> source)
61f857971dSopenharmony_ci{
62f857971dSopenharmony_ci    CHKPF(source);
63f857971dSopenharmony_ci    auto [iter, isNew] = sources_.emplace(source->GetFd(), source);
64f857971dSopenharmony_ci    if (!isNew) {
65f857971dSopenharmony_ci        FI_HILOGW("Epoll source(%d) has been added", source->GetFd());
66f857971dSopenharmony_ci        return true;
67f857971dSopenharmony_ci    }
68f857971dSopenharmony_ci    struct epoll_event ev {};
69f857971dSopenharmony_ci    ev.events = source->GetEvents();
70f857971dSopenharmony_ci    ev.data.ptr = source.get();
71f857971dSopenharmony_ci
72f857971dSopenharmony_ci    int32_t ret = ::epoll_ctl(epollFd_, EPOLL_CTL_ADD, source->GetFd(), &ev);
73f857971dSopenharmony_ci    if (ret != 0) {
74f857971dSopenharmony_ci        FI_HILOGE("epoll_ctl failed:%{public}s", ::strerror(errno));
75f857971dSopenharmony_ci        sources_.erase(iter);
76f857971dSopenharmony_ci        return false;
77f857971dSopenharmony_ci    }
78f857971dSopenharmony_ci    FI_HILOGI("Add epoll source(%d)", source->GetFd());
79f857971dSopenharmony_ci    return true;
80f857971dSopenharmony_ci}
81f857971dSopenharmony_ci
82f857971dSopenharmony_civoid EpollManager::Remove(std::shared_ptr<IEpollEventSource> source)
83f857971dSopenharmony_ci{
84f857971dSopenharmony_ci    CHKPV(source);
85f857971dSopenharmony_ci    auto iter = sources_.find(source->GetFd());
86f857971dSopenharmony_ci    if (iter == sources_.cend()) {
87f857971dSopenharmony_ci        FI_HILOGE("No epoll source(%d)", source->GetFd());
88f857971dSopenharmony_ci        return;
89f857971dSopenharmony_ci    }
90f857971dSopenharmony_ci    FI_HILOGI("Remove epoll source(%d)", source->GetFd());
91f857971dSopenharmony_ci    int32_t ret = ::epoll_ctl(epollFd_, EPOLL_CTL_DEL, source->GetFd(), nullptr);
92f857971dSopenharmony_ci    if (ret != 0) {
93f857971dSopenharmony_ci        FI_HILOGE("epoll_ctl failed:%{public}s", ::strerror(errno));
94f857971dSopenharmony_ci    }
95f857971dSopenharmony_ci    sources_.erase(iter);
96f857971dSopenharmony_ci}
97f857971dSopenharmony_ci
98f857971dSopenharmony_cibool EpollManager::Update(std::shared_ptr<IEpollEventSource> source)
99f857971dSopenharmony_ci{
100f857971dSopenharmony_ci    CHKPF(source);
101f857971dSopenharmony_ci    auto iter = sources_.find(source->GetFd());
102f857971dSopenharmony_ci    if (iter == sources_.cend()) {
103f857971dSopenharmony_ci        FI_HILOGE("No epoll source(%d)", source->GetFd());
104f857971dSopenharmony_ci        return false;
105f857971dSopenharmony_ci    }
106f857971dSopenharmony_ci    struct epoll_event ev {};
107f857971dSopenharmony_ci    ev.events = source->GetEvents();
108f857971dSopenharmony_ci    ev.data.ptr = source.get();
109f857971dSopenharmony_ci
110f857971dSopenharmony_ci    int32_t ret = ::epoll_ctl(epollFd_, EPOLL_CTL_MOD, source->GetFd(), &ev);
111f857971dSopenharmony_ci    if (ret != 0) {
112f857971dSopenharmony_ci        FI_HILOGE("epoll_ctl failed:%{public}s", ::strerror(errno));
113f857971dSopenharmony_ci        return false;
114f857971dSopenharmony_ci    }
115f857971dSopenharmony_ci    sources_.insert_or_assign(source->GetFd(), source);
116f857971dSopenharmony_ci    return true;
117f857971dSopenharmony_ci}
118f857971dSopenharmony_ci
119f857971dSopenharmony_ciint32_t EpollManager::Wait(struct epoll_event *events, int32_t maxevents)
120f857971dSopenharmony_ci{
121f857971dSopenharmony_ci    return WaitTimeout(events, maxevents, -1);
122f857971dSopenharmony_ci}
123f857971dSopenharmony_ci
124f857971dSopenharmony_ciint32_t EpollManager::WaitTimeout(struct epoll_event *events, int32_t maxevents, int32_t timeout)
125f857971dSopenharmony_ci{
126f857971dSopenharmony_ci    int32_t ret = ::epoll_wait(epollFd_, events, maxevents, timeout);
127f857971dSopenharmony_ci    if (ret < 0) {
128f857971dSopenharmony_ci        FI_HILOGE("epoll_wait failed:%{public}s", ::strerror(errno));
129f857971dSopenharmony_ci    } else if (ret == 0) {
130f857971dSopenharmony_ci        FI_HILOGE("epoll_wait timeout");
131f857971dSopenharmony_ci    }
132f857971dSopenharmony_ci    return ret;
133f857971dSopenharmony_ci}
134f857971dSopenharmony_ci
135f857971dSopenharmony_civoid EpollManager::Dispatch(const struct epoll_event &ev)
136f857971dSopenharmony_ci{
137f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
138f857971dSopenharmony_ci    if ((ev.events & EPOLLIN) == EPOLLIN) {
139f857971dSopenharmony_ci        DispatchOne(ev);
140f857971dSopenharmony_ci    } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) {
141f857971dSopenharmony_ci        FI_HILOGE("Epoll hangup:%{public}s", ::strerror(errno));
142f857971dSopenharmony_ci    }
143f857971dSopenharmony_ci}
144f857971dSopenharmony_ci
145f857971dSopenharmony_civoid EpollManager::DispatchOne(const struct epoll_event &ev)
146f857971dSopenharmony_ci{
147f857971dSopenharmony_ci    struct epoll_event evs[MAX_N_EVENTS];
148f857971dSopenharmony_ci    int32_t cnt = WaitTimeout(evs, MAX_N_EVENTS, 0);
149f857971dSopenharmony_ci
150f857971dSopenharmony_ci    for (int32_t index = 0; index < cnt; ++index) {
151f857971dSopenharmony_ci        IEpollEventSource *source = reinterpret_cast<IEpollEventSource *>(evs[index].data.ptr);
152f857971dSopenharmony_ci        CHKPC(source);
153f857971dSopenharmony_ci        if ((evs[index].events & EPOLLIN) == EPOLLIN) {
154f857971dSopenharmony_ci            source->Dispatch(evs[index]);
155f857971dSopenharmony_ci        } else if ((evs[index].events & (EPOLLHUP | EPOLLERR)) != 0) {
156f857971dSopenharmony_ci            FI_HILOGE("Epoll hangup:%{public}s", ::strerror(errno));
157f857971dSopenharmony_ci        }
158f857971dSopenharmony_ci    }
159f857971dSopenharmony_ci}
160f857971dSopenharmony_ci} // namespace DeviceStatus
161f857971dSopenharmony_ci} // namespace Msdp
162f857971dSopenharmony_ci} // namespace OHOS