1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#include "hdf_device_event_manager.h"
17c29fa5a6Sopenharmony_ci
18c29fa5a6Sopenharmony_ci#include <dlfcn.h>
19c29fa5a6Sopenharmony_ci#include <unistd.h>
20c29fa5a6Sopenharmony_ci
21c29fa5a6Sopenharmony_ci#include "hdf_device_event_dispatch.h"
22c29fa5a6Sopenharmony_ci#include "mmi_log.h"
23c29fa5a6Sopenharmony_ci
24c29fa5a6Sopenharmony_ci#undef MMI_LOG_TAG
25c29fa5a6Sopenharmony_ci#define MMI_LOG_TAG "HdfDeviceEventManager"
26c29fa5a6Sopenharmony_ci
27c29fa5a6Sopenharmony_ciusing namespace OHOS::HiviewDFX;
28c29fa5a6Sopenharmony_cinamespace OHOS {
29c29fa5a6Sopenharmony_cinamespace MMI {
30c29fa5a6Sopenharmony_cinamespace {
31c29fa5a6Sopenharmony_ciconstexpr int32_t INPUT_PARAM_FIRST { 0 };
32c29fa5a6Sopenharmony_ciconstexpr int32_t INPUT_PARAM_SECOND { 1 };
33c29fa5a6Sopenharmony_ci} // namespace
34c29fa5a6Sopenharmony_civoid HdfDeviceEventManager::ConnectHDFInit()
35c29fa5a6Sopenharmony_ci{
36c29fa5a6Sopenharmony_ci    std::string name = "mmi-hdf";
37c29fa5a6Sopenharmony_ci    inputInterface_ = IInputInterfaces::Get(true);
38c29fa5a6Sopenharmony_ci    if (inputInterface_ == nullptr) {
39c29fa5a6Sopenharmony_ci        MMI_HILOGE("The inputInterface_ is nullptr");
40c29fa5a6Sopenharmony_ci        return;
41c29fa5a6Sopenharmony_ci    }
42c29fa5a6Sopenharmony_ci    thread_ = std::thread([this] { injectThread_.InjectFunc(); });
43c29fa5a6Sopenharmony_ci    pthread_setname_np(thread_.native_handle(), name.c_str());
44c29fa5a6Sopenharmony_ci    int32_t ret = inputInterface_->OpenInputDevice(TOUCH_DEV_ID);
45c29fa5a6Sopenharmony_ci    if (ret == HDF_SUCCESS) {
46c29fa5a6Sopenharmony_ci        ret = inputInterface_->GetInputDevice(TOUCH_DEV_ID, iDevInfo_);
47c29fa5a6Sopenharmony_ci        if (ret != HDF_SUCCESS) {
48c29fa5a6Sopenharmony_ci            MMI_HILOGE("Get input device failed");
49c29fa5a6Sopenharmony_ci            return;
50c29fa5a6Sopenharmony_ci        }
51c29fa5a6Sopenharmony_ci        callback_ = new (std::nothrow) HdfDeviceEventDispatch(iDevInfo_.attrSet.axisInfo[ABS_MT_POSITION_X].max,
52c29fa5a6Sopenharmony_ci            iDevInfo_.attrSet.axisInfo[ABS_MT_POSITION_Y].max);
53c29fa5a6Sopenharmony_ci        if (callback_ == nullptr) {
54c29fa5a6Sopenharmony_ci            MMI_HILOGE("The callback_ is nullptr");
55c29fa5a6Sopenharmony_ci            return;
56c29fa5a6Sopenharmony_ci        }
57c29fa5a6Sopenharmony_ci        ret = inputInterface_->RegisterReportCallback(TOUCH_DEV_ID, callback_);
58c29fa5a6Sopenharmony_ci        MMI_HILOGD("RegisterReportCallback ret:%{public}d", ret);
59c29fa5a6Sopenharmony_ci    } else {
60c29fa5a6Sopenharmony_ci        MMI_HILOGE("Open input device failed");
61c29fa5a6Sopenharmony_ci    }
62c29fa5a6Sopenharmony_ci}
63c29fa5a6Sopenharmony_ci} // namespace MMI
64c29fa5a6Sopenharmony_ci} // namespace OHOS
65c29fa5a6Sopenharmony_ci
66c29fa5a6Sopenharmony_ciint32_t main() __attribute__((no_sanitize("cfi")))
67c29fa5a6Sopenharmony_ci{
68c29fa5a6Sopenharmony_ci    int sleepSeconds = 1;
69c29fa5a6Sopenharmony_ci    sleep(sleepSeconds);
70c29fa5a6Sopenharmony_ci    OHOS::MMI::HdfDeviceEventManager iHdfDeviceEventManager;
71c29fa5a6Sopenharmony_ci    iHdfDeviceEventManager.ConnectHDFInit();
72c29fa5a6Sopenharmony_ci
73c29fa5a6Sopenharmony_ci    int32_t pid = getpid();
74c29fa5a6Sopenharmony_ci    MMI_HILOGI("Current pid:%{public}d", pid);
75c29fa5a6Sopenharmony_ci    void *notifyProcessStatus = nullptr;
76c29fa5a6Sopenharmony_ci    int32_t(* notifyProcessStatusFunc)(int32_t, int32_t, int32_t) = nullptr;
77c29fa5a6Sopenharmony_ci    void *libMemMgrClientHandle = dlopen("libmemmgrclient.z.so", RTLD_NOW);
78c29fa5a6Sopenharmony_ci    if (!libMemMgrClientHandle) {
79c29fa5a6Sopenharmony_ci        MMI_HILOGE("%{public}s, dlopen libmemmgrclient failed.", __func__);
80c29fa5a6Sopenharmony_ci        goto nextStep;
81c29fa5a6Sopenharmony_ci    }
82c29fa5a6Sopenharmony_ci
83c29fa5a6Sopenharmony_ci    notifyProcessStatus = (dlsym(libMemMgrClientHandle, "notify_process_status"));
84c29fa5a6Sopenharmony_ci    if (!notifyProcessStatus) {
85c29fa5a6Sopenharmony_ci        MMI_HILOGE("%{public}s, dlsym notify_process_status failed.", __func__);
86c29fa5a6Sopenharmony_ci        dlclose(libMemMgrClientHandle);
87c29fa5a6Sopenharmony_ci        goto nextStep;
88c29fa5a6Sopenharmony_ci    }
89c29fa5a6Sopenharmony_ci    notifyProcessStatusFunc = reinterpret_cast<int32_t(*)(int32_t, int32_t, int32_t)>(notifyProcessStatus);
90c29fa5a6Sopenharmony_ci    if (notifyProcessStatusFunc(pid, OHOS::MMI::INPUT_PARAM_FIRST, OHOS::MMI::INPUT_PARAM_SECOND) != 0) {
91c29fa5a6Sopenharmony_ci        MMI_HILOGE("%{public}s, get device memory failed.", __func__);
92c29fa5a6Sopenharmony_ci    }
93c29fa5a6Sopenharmony_ci    dlclose(libMemMgrClientHandle);
94c29fa5a6Sopenharmony_ci    MMI_HILOGI("notify_process_status execute end");
95c29fa5a6Sopenharmony_ci
96c29fa5a6Sopenharmony_cinextStep:
97c29fa5a6Sopenharmony_ci    MMI_HILOGI("Start thread loop");
98c29fa5a6Sopenharmony_ci    static std::int32_t usleepTime = 1500000;
99c29fa5a6Sopenharmony_ci    while (true) {
100c29fa5a6Sopenharmony_ci        usleep(usleepTime);
101c29fa5a6Sopenharmony_ci    }
102c29fa5a6Sopenharmony_ci    return 0;
103c29fa5a6Sopenharmony_ci}
104