1484543d1Sopenharmony_ci/*
2484543d1Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3484543d1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4484543d1Sopenharmony_ci * you may not use this file except in compliance with the License.
5484543d1Sopenharmony_ci * You may obtain a copy of the License at
6484543d1Sopenharmony_ci *
7484543d1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8484543d1Sopenharmony_ci *
9484543d1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10484543d1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11484543d1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12484543d1Sopenharmony_ci * See the License for the specific language governing permissions and
13484543d1Sopenharmony_ci * limitations under the License.
14484543d1Sopenharmony_ci */
15484543d1Sopenharmony_ci
16484543d1Sopenharmony_ci#ifndef __FFRT_EVENT_HANDLER_ADAPTER_H__
17484543d1Sopenharmony_ci#define __FFRT_EVENT_HANDLER_ADAPTER_H__
18484543d1Sopenharmony_ci#include <dlfcn.h>
19484543d1Sopenharmony_ci#include <string>
20484543d1Sopenharmony_ci#include <mutex>
21484543d1Sopenharmony_ci#include "dfx/log/ffrt_log_api.h"
22484543d1Sopenharmony_ci#include "c/type_def.h"
23484543d1Sopenharmony_ci
24484543d1Sopenharmony_cinamespace ffrt {
25484543d1Sopenharmony_ci#if (defined(__aarch64__) || defined(__x86_64__))
26484543d1Sopenharmony_ciconstexpr const char* EVENTHANDLER_LIB_PATH = "/system/lib64/chipset-pub-sdk/libeventhandler.z.so";
27484543d1Sopenharmony_ci#else
28484543d1Sopenharmony_ciconstexpr const char* EVENTHANDLER_LIB_PATH = "/system/lib/chipset-pub-sdk/libeventhandler.z.so";
29484543d1Sopenharmony_ci#endif
30484543d1Sopenharmony_ci
31484543d1Sopenharmony_cienum class Priority : uint32_t {
32484543d1Sopenharmony_ci    // The highest priority queue, should be distributed until the tasks in the queue are completed.
33484543d1Sopenharmony_ci    VIP = 0,
34484543d1Sopenharmony_ci    // Event that should be distributed at once if possible.
35484543d1Sopenharmony_ci    IMMEDIATE,
36484543d1Sopenharmony_ci    // High priority event, sorted by handle time, should be distributed before low priority event.
37484543d1Sopenharmony_ci    HIGH,
38484543d1Sopenharmony_ci    // Normal event, sorted by handle time.
39484543d1Sopenharmony_ci    LOW,
40484543d1Sopenharmony_ci    // Event that should be distributed only if no other event right now.
41484543d1Sopenharmony_ci    IDLE,
42484543d1Sopenharmony_ci};
43484543d1Sopenharmony_ci
44484543d1Sopenharmony_cistruct TaskOptions {
45484543d1Sopenharmony_ci    std::string dfxName_;
46484543d1Sopenharmony_ci    int64_t delayTime_;
47484543d1Sopenharmony_ci    Priority priority_;
48484543d1Sopenharmony_ci    uintptr_t taskId_;
49484543d1Sopenharmony_ci    TaskOptions(std::string dfxName, int64_t delayTime, Priority priority, uintptr_t taskId)
50484543d1Sopenharmony_ci        : dfxName_(dfxName), delayTime_(delayTime), priority_(priority), taskId_(taskId) {}
51484543d1Sopenharmony_ci};
52484543d1Sopenharmony_ci
53484543d1Sopenharmony_civoid* GetMainEventHandlerForFFRT();
54484543d1Sopenharmony_civoid* GetCurrentEventHandlerForFFRT();
55484543d1Sopenharmony_cibool PostTaskByFFRT(void* handler, const std::function<void()>& callback, const TaskOptions& task);
56484543d1Sopenharmony_ciint RemoveTaskForFFRT(void* handler, const uintptr_t taskId);
57484543d1Sopenharmony_ciint AddFdListenerByFFRT(void* handler, uint32_t fd, uint32_t event, void* data, ffrt_poller_cb cb);
58484543d1Sopenharmony_ciint RemoveFdListenerByFFRT(void* handler, uint32_t fd);
59484543d1Sopenharmony_ci
60484543d1Sopenharmony_ciusing GetMainEventHandlerType = decltype(GetMainEventHandlerForFFRT)*;
61484543d1Sopenharmony_ciusing GetCurrentEventHandlerType = decltype(GetCurrentEventHandlerForFFRT)*;
62484543d1Sopenharmony_ciusing PostTaskType = decltype(PostTaskByFFRT)*;
63484543d1Sopenharmony_ciusing RemoveTaskType = decltype(RemoveTaskForFFRT)*;
64484543d1Sopenharmony_ciusing AddFdListenerType = decltype(AddFdListenerByFFRT)*;
65484543d1Sopenharmony_ciusing RemoveFdListenerType = decltype(RemoveFdListenerByFFRT)*;
66484543d1Sopenharmony_ci
67484543d1Sopenharmony_ciclass EventHandlerAdapter {
68484543d1Sopenharmony_cipublic:
69484543d1Sopenharmony_ci    EventHandlerAdapter()
70484543d1Sopenharmony_ci    {
71484543d1Sopenharmony_ci        std::lock_guard<std::mutex> guard(mutex_);
72484543d1Sopenharmony_ci        Load();
73484543d1Sopenharmony_ci    }
74484543d1Sopenharmony_ci
75484543d1Sopenharmony_ci    ~EventHandlerAdapter()
76484543d1Sopenharmony_ci    {
77484543d1Sopenharmony_ci    }
78484543d1Sopenharmony_ci
79484543d1Sopenharmony_ci    static EventHandlerAdapter* Instance()
80484543d1Sopenharmony_ci    {
81484543d1Sopenharmony_ci        static EventHandlerAdapter instance;
82484543d1Sopenharmony_ci        return &instance;
83484543d1Sopenharmony_ci    }
84484543d1Sopenharmony_ci
85484543d1Sopenharmony_ci    Priority ConvertPriority(ffrt_queue_priority_t priority)
86484543d1Sopenharmony_ci    {
87484543d1Sopenharmony_ci        return static_cast<Priority>(priority + 1);
88484543d1Sopenharmony_ci    }
89484543d1Sopenharmony_ci
90484543d1Sopenharmony_ci    GetMainEventHandlerType GetMainEventHandler = nullptr;
91484543d1Sopenharmony_ci    GetCurrentEventHandlerType GetCurrentEventHandler = nullptr;
92484543d1Sopenharmony_ci    PostTaskType PostTask = nullptr;
93484543d1Sopenharmony_ci    RemoveTaskType RemoveTask = nullptr;
94484543d1Sopenharmony_ci    AddFdListenerType AddFdListener = nullptr;
95484543d1Sopenharmony_ci    RemoveFdListenerType RemoveFdListener = nullptr;
96484543d1Sopenharmony_ci
97484543d1Sopenharmony_ciprivate:
98484543d1Sopenharmony_ci    void Load()
99484543d1Sopenharmony_ci    {
100484543d1Sopenharmony_ci        if (handle_ != nullptr) {
101484543d1Sopenharmony_ci            return;
102484543d1Sopenharmony_ci        }
103484543d1Sopenharmony_ci
104484543d1Sopenharmony_ci        handle_ = dlopen(EVENTHANDLER_LIB_PATH, RTLD_NOW | RTLD_LOCAL);
105484543d1Sopenharmony_ci        if (handle_ == nullptr) {
106484543d1Sopenharmony_ci            FFRT_LOGE("eventhandler lib handle is null.");
107484543d1Sopenharmony_ci            return;
108484543d1Sopenharmony_ci        }
109484543d1Sopenharmony_ci
110484543d1Sopenharmony_ci        GetMainEventHandler = reinterpret_cast<GetMainEventHandlerType>(
111484543d1Sopenharmony_ci            dlsym(handle_, "GetMainEventHandlerForFFRT"));
112484543d1Sopenharmony_ci        if (GetMainEventHandler == nullptr) {
113484543d1Sopenharmony_ci            FFRT_LOGE("get GetMainEventHandlerForFFRT symbol fail.");
114484543d1Sopenharmony_ci            return;
115484543d1Sopenharmony_ci        }
116484543d1Sopenharmony_ci
117484543d1Sopenharmony_ci        GetCurrentEventHandler = reinterpret_cast<GetCurrentEventHandlerType>(
118484543d1Sopenharmony_ci            dlsym(handle_, "GetCurrentEventHandlerForFFRT"));
119484543d1Sopenharmony_ci        if (GetCurrentEventHandler == nullptr) {
120484543d1Sopenharmony_ci            FFRT_LOGE("get GetCurrentEventHandlerForFFRT symbol fail.");
121484543d1Sopenharmony_ci            return;
122484543d1Sopenharmony_ci        }
123484543d1Sopenharmony_ci
124484543d1Sopenharmony_ci        PostTask = reinterpret_cast<PostTaskType>(
125484543d1Sopenharmony_ci            dlsym(handle_, "PostTaskByFFRT"));
126484543d1Sopenharmony_ci        if (PostTask == nullptr) {
127484543d1Sopenharmony_ci            FFRT_LOGE("get PostTaskByFFRT symbol fail.");
128484543d1Sopenharmony_ci            return;
129484543d1Sopenharmony_ci        }
130484543d1Sopenharmony_ci
131484543d1Sopenharmony_ci        RemoveTask = reinterpret_cast<RemoveTaskType>(
132484543d1Sopenharmony_ci            dlsym(handle_, "RemoveTaskForFFRT"));
133484543d1Sopenharmony_ci        if (RemoveTask == nullptr) {
134484543d1Sopenharmony_ci            FFRT_LOGE("get RemoveTaskForFFRT symbol fail.");
135484543d1Sopenharmony_ci            return;
136484543d1Sopenharmony_ci        }
137484543d1Sopenharmony_ci
138484543d1Sopenharmony_ci        AddFdListener = reinterpret_cast<AddFdListenerType>(
139484543d1Sopenharmony_ci            dlsym(handle_, "AddFdListenerByFFRT"));
140484543d1Sopenharmony_ci        if (AddFdListener == nullptr) {
141484543d1Sopenharmony_ci            FFRT_LOGE("get AddFdListenerByFFRT symbol fail.");
142484543d1Sopenharmony_ci            return;
143484543d1Sopenharmony_ci        }
144484543d1Sopenharmony_ci
145484543d1Sopenharmony_ci        RemoveFdListener = reinterpret_cast<RemoveFdListenerType>(
146484543d1Sopenharmony_ci            dlsym(handle_, "RemoveFdListenerByFFRT"));
147484543d1Sopenharmony_ci        if (RemoveFdListener == nullptr) {
148484543d1Sopenharmony_ci            FFRT_LOGE("get RemoveFdListenerByFFRT symbol fail.");
149484543d1Sopenharmony_ci            return;
150484543d1Sopenharmony_ci        }
151484543d1Sopenharmony_ci    }
152484543d1Sopenharmony_ci
153484543d1Sopenharmony_ci    void* handle_ = nullptr;
154484543d1Sopenharmony_ci    std::mutex mutex_;
155484543d1Sopenharmony_ci};
156484543d1Sopenharmony_ci}  // namespace ffrt
157484543d1Sopenharmony_ci#endif // __FFRT_EVENT_HANDLER_ADAPTER_H__