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 "screen_brightness_manager.h"
17
18 #include "iam_check.h"
19 #include "iam_logger.h"
20 #include "iam_ptr.h"
21
22 #include "sa_command_manager.h"
23 #include "service_ex_manager.h"
24
25 #define LOG_LABEL UserIam::Common::LABEL_FACE_AUTH_SA
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace FaceAuth {
30 namespace {
CreateScreenBrightnessManager()31 std::shared_ptr<ScreenBrightnessManager> CreateScreenBrightnessManager()
32 {
33 auto manager = Common::MakeShared<ScreenBrightnessManager>();
34 IF_FALSE_LOGE_AND_RETURN_VAL(manager != nullptr, nullptr);
35
36 std::vector<SaCommandId> commandIds = { SaCommandId::BEGIN_SCREEN_BRIGHTNESS_INCREASE,
37 SaCommandId::END_SCREEN_BRIGHTNESS_INCREASE };
38 SaCommandManager::GetInstance().RegisterSaCommandProcessor(commandIds, manager);
39 return manager;
40 }
41 } // namespace
42
GetInstance()43 std::shared_ptr<ScreenBrightnessManager> ScreenBrightnessManager::GetInstance()
44 {
45 static auto manager = CreateScreenBrightnessManager();
46 if (manager == nullptr) {
47 IAM_LOGE("ScreenBrightnessManager is null");
48 }
49 return manager;
50 }
51
GetCurrentTask()52 std::shared_ptr<IScreenBrightnessTask> ScreenBrightnessManager::GetCurrentTask()
53 {
54 return taskInProc_;
55 }
56
ProcessSaCommand(std::shared_ptr<FaceAuthExecutorHdi> executor,const SaCommand & command)57 UserAuth::ResultCode ScreenBrightnessManager::ProcessSaCommand(std::shared_ptr<FaceAuthExecutorHdi> executor,
58 const SaCommand &command)
59 {
60 IF_FALSE_LOGE_AND_RETURN_VAL(executor != nullptr, UserAuth::GENERAL_ERROR);
61
62 std::lock_guard<std::mutex> lock(mutex_);
63
64 UserAuth::ResultCode result = UserAuth::GENERAL_ERROR;
65 switch (command.id) {
66 case SaCommandId::BEGIN_SCREEN_BRIGHTNESS_INCREASE:
67 result = ProcessScreenBrightnessIncreaseBegin(executor, command.param);
68 break;
69 case SaCommandId::END_SCREEN_BRIGHTNESS_INCREASE:
70 result = ProcessScreenBrightnessIncreaseEnd(executor, command.param);
71 break;
72 default:
73 IAM_LOGE("command id %{public}d not match", command.id);
74 }
75 return result;
76 }
77
OnHdiDisconnect(std::shared_ptr<FaceAuthExecutorHdi> executor)78 void ScreenBrightnessManager::OnHdiDisconnect(std::shared_ptr<FaceAuthExecutorHdi> executor)
79 {
80 IF_FALSE_LOGE_AND_RETURN(executor != nullptr);
81
82 std::lock_guard<std::mutex> lock(mutex_);
83
84 if (executorInProc_ != executor) {
85 return;
86 }
87
88 IAM_LOGI("start");
89 executorInProc_ = nullptr;
90
91 if (taskInProc_ != nullptr) {
92 taskInProc_->Stop();
93 taskInProc_ = nullptr;
94 }
95 IAM_LOGI("success");
96 }
97
ProcessScreenBrightnessIncreaseBegin(std::shared_ptr<FaceAuthExecutorHdi> executor,const SaCommandParam param)98 UserAuth::ResultCode ScreenBrightnessManager::ProcessScreenBrightnessIncreaseBegin(
99 std::shared_ptr<FaceAuthExecutorHdi> executor, const SaCommandParam param)
100 {
101 if (executorInProc_ != nullptr) {
102 IAM_LOGE("another executor is using this module");
103 return UserAuth::GENERAL_ERROR;
104 }
105
106 if (taskInProc_ != nullptr) {
107 IAM_LOGE("another task is running");
108 return UserAuth::GENERAL_ERROR;
109 }
110
111 IAM_LOGI("start");
112 auto acquireRet = ServiceExManager::GetInstance().Acquire();
113 IF_FALSE_LOGE_AND_RETURN_VAL(acquireRet == UserAuth::SUCCESS, UserAuth::GENERAL_ERROR);
114
115 auto taskInProc = ServiceExManager::GetInstance().GetScreenBrightnessTask();
116 if (taskInProc == nullptr) {
117 ServiceExManager::GetInstance().Release();
118 IAM_LOGE("failed to get task");
119 return UserAuth::GENERAL_ERROR;
120 }
121 taskInProc->RegisterDestructCallback([]() {
122 IAM_LOGI("task destruct");
123 ServiceExManager::GetInstance().Release();
124 });
125 taskInProc->Start();
126
127 executorInProc_ = executor;
128 taskInProc_ = taskInProc;
129 IAM_LOGI("success");
130 return UserAuth::SUCCESS;
131 }
132
ProcessScreenBrightnessIncreaseEnd(std::shared_ptr<FaceAuthExecutorHdi> executor,const SaCommandParam param)133 UserAuth::ResultCode ScreenBrightnessManager::ProcessScreenBrightnessIncreaseEnd(
134 std::shared_ptr<FaceAuthExecutorHdi> executor, const SaCommandParam param)
135 {
136 if (executorInProc_ != executor) {
137 IAM_LOGE("another executor is using this module");
138 return UserAuth::GENERAL_ERROR;
139 }
140
141 IAM_LOGI("start");
142 executorInProc_ = nullptr;
143
144 if (taskInProc_ != nullptr) {
145 taskInProc_->Stop();
146 taskInProc_ = nullptr;
147 }
148
149 IAM_LOGI("success");
150 return UserAuth::SUCCESS;
151 }
152
153 } // namespace FaceAuth
154 } // namespace UserIam
155 } // namespace OHOS