1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "hdc_register.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include <dlfcn.h>
19eace7efcSopenharmony_ci#include <unistd.h>
20eace7efcSopenharmony_ci
21eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h"
22eace7efcSopenharmony_ci
23eace7efcSopenharmony_cinamespace OHOS::AbilityRuntime {
24eace7efcSopenharmony_ciusing StartRegister = void (*)(const std::string& processName, const std::string& pkgName, bool isDebug,
25eace7efcSopenharmony_ci    const HdcRegisterCallback& callback);
26eace7efcSopenharmony_ciusing StopRegister = void (*)();
27eace7efcSopenharmony_ci
28eace7efcSopenharmony_ciHdcRegister::~HdcRegister()
29eace7efcSopenharmony_ci{
30eace7efcSopenharmony_ci    StopHdcRegister();
31eace7efcSopenharmony_ci}
32eace7efcSopenharmony_ci
33eace7efcSopenharmony_ciHdcRegister& HdcRegister::Get()
34eace7efcSopenharmony_ci{
35eace7efcSopenharmony_ci    static HdcRegister hdcRegister;
36eace7efcSopenharmony_ci    return hdcRegister;
37eace7efcSopenharmony_ci}
38eace7efcSopenharmony_ci
39eace7efcSopenharmony_civoid HdcRegister::StartHdcRegister(const std::string& bundleName, const std::string& processName, bool debugApp,
40eace7efcSopenharmony_ci    HdcRegisterCallback callback)
41eace7efcSopenharmony_ci{
42eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
43eace7efcSopenharmony_ci
44eace7efcSopenharmony_ci    registerHandler_ = dlopen("libhdc_register.z.so", RTLD_LAZY);
45eace7efcSopenharmony_ci    if (registerHandler_ == nullptr) {
46eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "null registerHandler_");
47eace7efcSopenharmony_ci        return;
48eace7efcSopenharmony_ci    }
49eace7efcSopenharmony_ci    auto startRegister = reinterpret_cast<StartRegister>(dlsym(registerHandler_, "StartConnect"));
50eace7efcSopenharmony_ci    if (startRegister == nullptr) {
51eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "null StartConnect");
52eace7efcSopenharmony_ci        return;
53eace7efcSopenharmony_ci    }
54eace7efcSopenharmony_ci    startRegister(processName, bundleName, debugApp, callback);
55eace7efcSopenharmony_ci}
56eace7efcSopenharmony_ci
57eace7efcSopenharmony_civoid HdcRegister::StopHdcRegister()
58eace7efcSopenharmony_ci{
59eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
60eace7efcSopenharmony_ci    if (registerHandler_ == nullptr) {
61eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "null registerHandler_");
62eace7efcSopenharmony_ci        return;
63eace7efcSopenharmony_ci    }
64eace7efcSopenharmony_ci    auto stopRegister = reinterpret_cast<StopRegister>(dlsym(registerHandler_, "StopConnect"));
65eace7efcSopenharmony_ci    if (stopRegister != nullptr) {
66eace7efcSopenharmony_ci        stopRegister();
67eace7efcSopenharmony_ci    } else {
68eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "null StopConnect");
69eace7efcSopenharmony_ci    }
70eace7efcSopenharmony_ci    dlclose(registerHandler_);
71eace7efcSopenharmony_ci    registerHandler_ = nullptr;
72eace7efcSopenharmony_ci}
73eace7efcSopenharmony_ci} // namespace OHOS::AbilityRuntime
74