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 <mutex>
17#include "utils_log.h"
18#include "common_event_sys_errors.h"
19#include "io_event_reactor.h"
20#include "io_event_handler.h"
21
22namespace OHOS {
23namespace Utils {
24IOEventHandler::IOEventHandler()
25    :prev_(nullptr), next_(nullptr), fd_(IO_EVENT_INVALID_FD), events_(Events::EVENT_NONE),
26    cb_(nullptr), enabled_(false) {}
27
28IOEventHandler::IOEventHandler(int fd, EventId events, const EventCallback& cb)
29    :prev_(nullptr), next_(nullptr), fd_(fd), events_(events), cb_(cb), enabled_(false) {}
30
31IOEventHandler::~IOEventHandler()
32{
33    if (prev_ != nullptr) {
34        prev_->next_ = next_;
35    }
36
37    if (next_ != nullptr) {
38        next_->prev_ = prev_;
39    }
40
41    prev_ = nullptr;
42    next_ = nullptr;
43}
44
45bool IOEventHandler::Start(IOEventReactor* reactor)
46{
47    UTILS_LOGD("%{public}s: Try add handler-%{public}p to reactor-%{public}p.", \
48               __FUNCTION__, reinterpret_cast<void*>(this), reinterpret_cast<void*>(reactor));
49    ErrCode res = reactor->AddHandler(this);
50    if (res != EVENT_SYS_ERR_OK) {
51        UTILS_LOGE("%{public}s: Try add handler failed.", __FUNCTION__);
52        return false;
53    }
54
55    return true;
56}
57
58bool IOEventHandler::Stop(IOEventReactor* reactor)
59{
60    UTILS_LOGD("%{public}s: Try remove handler-%{public}p from reactor-%{public}p.", \
61               __FUNCTION__, reinterpret_cast<void*>(this), reinterpret_cast<void*>(reactor));
62    ErrCode res = reactor->RemoveHandler(this);
63    if (res != EVENT_SYS_ERR_OK) {
64        UTILS_LOGE("%{public}s: Try remove handler failed.", __FUNCTION__);
65        return false;
66    }
67
68    return true;
69}
70
71bool IOEventHandler::Update(IOEventReactor* reactor)
72{
73    UTILS_LOGD("%{public}s: Try update handler-%{public}p to reactor-%{public}p.", \
74               __FUNCTION__, reinterpret_cast<void*>(this), reinterpret_cast<void*>(reactor));
75    ErrCode res = reactor->UpdateHandler(this);
76    if (res != EVENT_SYS_ERR_OK) {
77        UTILS_LOGE("%{public}s: Try update handler failed.", __FUNCTION__);
78        return false;
79    }
80
81    return true;
82}
83
84void IOEventHandler::EnableRead()
85{
86    events_ |= Events::EVENT_READ;
87}
88
89void IOEventHandler::EnableWrite()
90{
91    events_ |= Events::EVENT_WRITE;
92}
93
94void IOEventHandler::DisableWrite()
95{
96    events_ &= ~Events::EVENT_WRITE;
97}
98void IOEventHandler::DisableAll()
99{
100    events_ = Events::EVENT_NONE;
101}
102
103} // Utils
104} // OHOS