15c735df2Sopenharmony_ci/* 25c735df2Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 35c735df2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45c735df2Sopenharmony_ci * you may not use this file except in compliance with the License. 55c735df2Sopenharmony_ci * You may obtain a copy of the License at 65c735df2Sopenharmony_ci * 75c735df2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85c735df2Sopenharmony_ci * 95c735df2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105c735df2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115c735df2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125c735df2Sopenharmony_ci * See the License for the specific language governing permissions and 135c735df2Sopenharmony_ci * limitations under the License. 145c735df2Sopenharmony_ci */ 155c735df2Sopenharmony_ci 165c735df2Sopenharmony_ci#ifndef LBS_DYNAMIC_LOADER_H 175c735df2Sopenharmony_ci#define LBS_DYNAMIC_LOADER_H 185c735df2Sopenharmony_ci 195c735df2Sopenharmony_ci#include <dlfcn.h> 205c735df2Sopenharmony_ci 215c735df2Sopenharmony_ci#include "location_log.h" 225c735df2Sopenharmony_ci 235c735df2Sopenharmony_cinamespace OHOS { 245c735df2Sopenharmony_cinamespace Location { 255c735df2Sopenharmony_citemplate<typename FuncType> 265c735df2Sopenharmony_ciclass DynamicLibraryLoader { 275c735df2Sopenharmony_ci typedef FuncType* (*funcEntry)(); 285c735df2Sopenharmony_cipublic: 295c735df2Sopenharmony_ci explicit DynamicLibraryLoader(const std::string& libPath) 305c735df2Sopenharmony_ci { 315c735df2Sopenharmony_ci libPath_ = libPath; 325c735df2Sopenharmony_ci handle_ = dlopen(libPath.c_str(), RTLD_LAZY); 335c735df2Sopenharmony_ci if (!handle_) { 345c735df2Sopenharmony_ci LBSLOGE(COMMON_UTILS, "%{public}s open so [%{public}s] failed, error: %{public}s", 355c735df2Sopenharmony_ci __func__, libPath.c_str(), dlerror()); 365c735df2Sopenharmony_ci } 375c735df2Sopenharmony_ci LBSLOGI(COMMON_UTILS, "%{public}s open so [%{public}s] success.", __func__, libPath.c_str()); 385c735df2Sopenharmony_ci } 395c735df2Sopenharmony_ci 405c735df2Sopenharmony_ci ~DynamicLibraryLoader() 415c735df2Sopenharmony_ci { 425c735df2Sopenharmony_ci if (handle_) { 435c735df2Sopenharmony_ci LBSLOGI(COMMON_UTILS, "%{public}s close so [%{public}s]", __func__, libPath_.c_str()); 445c735df2Sopenharmony_ci#ifndef TEST_CASES_ENABLED 455c735df2Sopenharmony_ci dlclose(handle_); 465c735df2Sopenharmony_ci handle_ = nullptr; 475c735df2Sopenharmony_ci#endif 485c735df2Sopenharmony_ci } 495c735df2Sopenharmony_ci } 505c735df2Sopenharmony_ci 515c735df2Sopenharmony_ci funcEntry GetFunction(const std::string& funcName) 525c735df2Sopenharmony_ci { 535c735df2Sopenharmony_ci if (!handle_) { 545c735df2Sopenharmony_ci return nullptr; 555c735df2Sopenharmony_ci } 565c735df2Sopenharmony_ci auto funcPtr = dlsym(handle_, funcName.c_str()); 575c735df2Sopenharmony_ci char* error = dlerror(); 585c735df2Sopenharmony_ci if (error != nullptr || funcPtr == nullptr) { 595c735df2Sopenharmony_ci LBSLOGE(COMMON_UTILS, "%{public}s, [%{public}s] dlsym error, error: %{public}s", 605c735df2Sopenharmony_ci __func__, libPath_.c_str(), error); 615c735df2Sopenharmony_ci return nullptr; 625c735df2Sopenharmony_ci } 635c735df2Sopenharmony_ci return (funcEntry)(funcPtr); 645c735df2Sopenharmony_ci } 655c735df2Sopenharmony_ci 665c735df2Sopenharmony_ciprivate: 675c735df2Sopenharmony_ci std::string libPath_; 685c735df2Sopenharmony_ci void* handle_ = nullptr; 695c735df2Sopenharmony_ci}; 705c735df2Sopenharmony_ci} // namespace Location 715c735df2Sopenharmony_ci} // namespace OHOS 725c735df2Sopenharmony_ci#endif // LBS_DYNAMIC_LOADER_H