1 /*
2 * Copyright (c) 2023 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 "service_ex_manager.h"
17
18 #include <dlfcn.h>
19
20 #include "iam_check.h"
21 #include "iam_logger.h"
22
23 #define LOG_LABEL UserIam::Common::LABEL_FACE_AUTH_SA
24
25 namespace OHOS {
26 namespace UserIam {
27 namespace FaceAuth {
28 namespace {
29 const char *SO_NAME = "libfaceauthservice_ex.z.so";
30 const char *GET_SCREEN_BRIGHTNESS_TASK_SYMBOL_NAME = "_ZN4OHOS7UserIam8FaceAuth23GetScreenBrightnessTaskEv";
31 using GetScreenBrightnessTaskFunc = std::shared_ptr<IScreenBrightnessTask> (*)();
32 } // namespace
33
GetInstance()34 ServiceExManager &ServiceExManager::GetInstance()
35 {
36 static ServiceExManager manager;
37 return manager;
38 }
39
Acquire()40 UserAuth::ResultCode ServiceExManager::Acquire()
41 {
42 std::lock_guard<std::recursive_mutex> lock(recursiveMutex_);
43 IAM_LOGI("start");
44 if (!isOpened_) {
45 handle_ = dlopen(SO_NAME, RTLD_LAZY);
46 if (handle_ == nullptr) {
47 IAM_LOGE("failed to open so, error: %{public}s", dlerror());
48 return UserAuth::GENERAL_ERROR;
49 }
50 isOpened_ = true;
51 IAM_LOGI("dlopen success");
52 }
53
54 IAM_LOGI("success");
55 return UserAuth::SUCCESS;
56 }
57
Release()58 void ServiceExManager::Release()
59 {
60 std::lock_guard<std::recursive_mutex> lock(recursiveMutex_);
61 IAM_LOGI("in this version, so is not released");
62 }
63
GetScreenBrightnessTask()64 std::shared_ptr<IScreenBrightnessTask> ServiceExManager::GetScreenBrightnessTask()
65 {
66 std::lock_guard<std::recursive_mutex> lock(recursiveMutex_);
67 IF_FALSE_LOGE_AND_RETURN_VAL(handle_ != nullptr, nullptr);
68
69 GetScreenBrightnessTaskFunc getFunc =
70 reinterpret_cast<GetScreenBrightnessTaskFunc>(dlsym(handle_, GET_SCREEN_BRIGHTNESS_TASK_SYMBOL_NAME));
71 if (getFunc == nullptr) {
72 IAM_LOGE("failed to find symbol, error: %{public}s", dlerror());
73 return nullptr;
74 }
75
76 return getFunc();
77 }
78 } // namespace FaceAuth
79 } // namespace UserIam
80 } // namespace OHOS