1 /* 2 * Copyright (c) 2022 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 "hdc_register.h" 17 18 #include <dlfcn.h> 19 #include <unistd.h> 20 21 #include "hilog_wrapper.h" 22 23 namespace OHOS::AbilityRuntime { 24 using StartRegister = void (*)(const std::string& pkgName); 25 using StopRegister = void (*)(); 26 ~HdcRegister()27HdcRegister::~HdcRegister() 28 { 29 StopHdcRegister(); 30 } 31 Get()32HdcRegister& HdcRegister::Get() 33 { 34 static HdcRegister hdcRegister; 35 return hdcRegister; 36 } 37 StartHdcRegister(const std::string & bundleName)38void HdcRegister::StartHdcRegister(const std::string& bundleName) 39 { 40 HILOG_DEBUG("HdcRegister::StartHdcRegister begin"); 41 42 registerHandler_ = dlopen("libhdc_register.z.so", RTLD_LAZY); 43 if (registerHandler_ == nullptr) { 44 HILOG_ERROR("HdcRegister::StartHdcRegister failed to open register library"); 45 return; 46 } 47 auto startRegister = reinterpret_cast<StartRegister>(dlsym(registerHandler_, "StartConnect")); 48 if (startRegister == nullptr) { 49 HILOG_ERROR("HdcRegister::StartHdcRegister failed to find symbol 'StartConnect'"); 50 return; 51 } 52 startRegister(bundleName); 53 HILOG_DEBUG("HdcRegister::StartHdcRegister end"); 54 } 55 StopHdcRegister()56void HdcRegister::StopHdcRegister() 57 { 58 HILOG_DEBUG("HdcRegister::StopHdcRegister begin"); 59 if (registerHandler_ == nullptr) { 60 HILOG_ERROR("HdcRegister::StopHdcRegister registerHandler_ is nullptr"); 61 return; 62 } 63 auto stopRegister = reinterpret_cast<StopRegister>(dlsym(registerHandler_, "StopConnect")); 64 if (stopRegister != nullptr) { 65 stopRegister(); 66 } else { 67 HILOG_ERROR("HdcRegister::StopHdcRegister failed to find symbol 'StopConnect'"); 68 } 69 dlclose(registerHandler_); 70 registerHandler_ = nullptr; 71 HILOG_DEBUG("HdcRegister::StopHdcRegister end"); 72 } 73 } // namespace OHOS::AbilityRuntime 74