1 /*
2 * Copyright (c) 2025 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 "device/device_profile_proxy.h"
17
18 #include <dlfcn.h>
19 #include <thread>
20
21 #include "pasteboard_hilog.h"
22
23 namespace OHOS {
24 namespace MiscServices {
25 constexpr const char *LIB_NAME = "libpasteboard_adapter.z.so";
26 constexpr const char *FUN_NAME_GET = "GetDeviceProfileAdapter";
27 constexpr const char *FUN_NAME_DEINIT = "DeinitDeviceProfileAdapter";
28
DeviceProfileProxy()29 DeviceProfileProxy::DeviceProfileProxy()
30 {
31 handler = dlopen(LIB_NAME, RTLD_NOW);
32 PASTEBOARD_CHECK_AND_RETURN_LOGE(handler != nullptr, PASTEBOARD_MODULE_COMMON,
33 "dlopen failed, msg=%{public}s", dlerror());
34 }
35
~DeviceProfileProxy()36 DeviceProfileProxy::~DeviceProfileProxy()
37 {
38 if (handler == nullptr) {
39 return;
40 }
41
42 auto func = reinterpret_cast<DeinitDeviceProfileAdapterFunc>(dlsym(handler, FUN_NAME_DEINIT));
43 if (func != nullptr) {
44 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_COMMON, "deinit adapter");
45 func();
46 std::this_thread::sleep_for(std::chrono::seconds(1));
47 } else {
48 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_COMMON, "dlsym failed, msg=%{public}s", dlerror());
49 }
50
51 dlclose(handler);
52 handler = nullptr;
53 }
54
GetAdapter()55 IDeviceProfileAdapter *DeviceProfileProxy::GetAdapter()
56 {
57 auto func = reinterpret_cast<GetDeviceProfileAdapterFunc>(dlsym(handler, FUN_NAME_GET));
58 PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(func != nullptr, nullptr, PASTEBOARD_MODULE_COMMON,
59 "dlsym failed, msg=%{public}s", dlerror());
60 return func();
61 }
62
63 } // namespace MiscServices
64 } // namespace OHOS
65