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 "sensor_illumination_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_TAG "FINGERPRINT_AUTH_SA"
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace FingerprintAuth {
30 namespace {
31 constexpr uint32_t MAX_RADIUS_LEN = 300;
32 constexpr uint32_t MAX_X_Y_VALUE = 1000; // center X and Y in per thousand
CreateSensorIlluminationManager()33 std::shared_ptr<SensorIlluminationManager> CreateSensorIlluminationManager()
34 {
35     auto manager = Common::MakeShared<SensorIlluminationManager>();
36     IF_FALSE_LOGE_AND_RETURN_VAL(manager != nullptr, nullptr);
37 
38     std::vector<SaCommandId> commandIds = { SaCommandId::ENABLE_SENSOR_ILLUMINATION,
39         SaCommandId::DISABLE_SENSOR_ILLUMINATION, SaCommandId::TURN_ON_SENSOR_ILLUMINATION,
40         SaCommandId::TURN_OFF_SENSOR_ILLUMINATION };
41     SaCommandManager::GetInstance().RegisterSaCommandProcessor(commandIds, manager);
42     return manager;
43 }
44 } // namespace
45 
GetInstance()46 std::shared_ptr<SensorIlluminationManager> SensorIlluminationManager::GetInstance()
47 {
48     static auto manager = CreateSensorIlluminationManager();
49     if (manager == nullptr) {
50         IAM_LOGE("SensorIlluminationManager is null");
51     }
52     return manager;
53 }
54 
ProcessSaCommand(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor,const SaCommand & command)55 UserAuth::ResultCode SensorIlluminationManager::ProcessSaCommand(
56     std::shared_ptr<FingerprintAllInOneExecutorHdi> executor, const SaCommand &command)
57 {
58     IF_FALSE_LOGE_AND_RETURN_VAL(executor != nullptr, UserAuth::GENERAL_ERROR);
59     std::lock_guard<std::mutex> lock(mutex_);
60 
61     UserAuth::ResultCode result = UserAuth::GENERAL_ERROR;
62     switch (command.id) {
63         case SaCommandId::ENABLE_SENSOR_ILLUMINATION:
64             result = EnableSensorIllumination(executor, command.param);
65             break;
66         case SaCommandId::DISABLE_SENSOR_ILLUMINATION:
67             result = DisableSensorIllumination(executor, command.param);
68             break;
69         case SaCommandId::TURN_ON_SENSOR_ILLUMINATION:
70             result = TurnOnSensorIllumination(executor, command.param);
71             break;
72         case SaCommandId::TURN_OFF_SENSOR_ILLUMINATION:
73             result = TurnOffSensorIllumination(executor, command.param);
74             break;
75         default:
76             IAM_LOGE("command id %{public}d not match", command.id);
77     }
78     return result;
79 }
80 
OnHdiDisconnect(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor)81 void SensorIlluminationManager::OnHdiDisconnect(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor)
82 {
83     IF_FALSE_LOGE_AND_RETURN(executor != nullptr);
84 
85     std::lock_guard<std::mutex> lock(mutex_);
86 
87     if (executorInProc_ != executor) {
88         return;
89     }
90 
91     IAM_LOGI("start");
92     executorInProc_ = nullptr;
93 
94     if (taskInProc_ != nullptr) {
95         UserAuth::ResultCode disableResultCode = taskInProc_->DisableSensorIllumination();
96         if (disableResultCode != UserAuth::ResultCode::SUCCESS) {
97             IAM_LOGE("turn off sensor illumination fail");
98         }
99         taskInProc_ = nullptr;
100     }
101     IAM_LOGI("success");
102 }
103 
EnableSensorIllumination(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor,const SaCommandParam param)104 UserAuth::ResultCode SensorIlluminationManager::EnableSensorIllumination(
105     std::shared_ptr<FingerprintAllInOneExecutorHdi> executor, const SaCommandParam param)
106 {
107     if (executorInProc_ != nullptr) {
108         IAM_LOGE("another executor is using this module");
109         return UserAuth::GENERAL_ERROR;
110     }
111 
112     if (taskInProc_ != nullptr) {
113         IAM_LOGE("another task is running");
114         return UserAuth::GENERAL_ERROR;
115     }
116 
117     IAM_LOGI("start");
118     auto acquireRet = ServiceExManager::GetInstance().Acquire();
119     IF_FALSE_LOGE_AND_RETURN_VAL(acquireRet == UserAuth::SUCCESS, UserAuth::GENERAL_ERROR);
120 
121     auto taskInProc = ServiceExManager::GetInstance().GetSensorIlluminationTask();
122     if (taskInProc == nullptr) {
123         ServiceExManager::GetInstance().Release();
124         IAM_LOGE("failed to get task");
125         return UserAuth::GENERAL_ERROR;
126     }
127     taskInProc->RegisterDestructCallback([]() {
128         IAM_LOGI("task destruct");
129         ServiceExManager::GetInstance().Release();
130     });
131     IF_FALSE_LOGE_AND_RETURN_VAL(param.enableSensorIllumination.centerX < MAX_X_Y_VALUE, UserAuth::GENERAL_ERROR);
132     IF_FALSE_LOGE_AND_RETURN_VAL(param.enableSensorIllumination.centerY < MAX_X_Y_VALUE, UserAuth::GENERAL_ERROR);
133     IF_FALSE_LOGE_AND_RETURN_VAL(param.enableSensorIllumination.radius < MAX_RADIUS_LEN, UserAuth::GENERAL_ERROR);
134 
135     UserAuth::ResultCode enableResultCode = taskInProc->EnableSensorIllumination(param.enableSensorIllumination.centerX,
136         param.enableSensorIllumination.centerY, param.enableSensorIllumination.radius,
137         param.enableSensorIllumination.color);
138     IF_FALSE_LOGE_AND_RETURN_VAL(enableResultCode == UserAuth::ResultCode::SUCCESS, UserAuth::GENERAL_ERROR);
139 
140     executorInProc_ = executor;
141     taskInProc_ = taskInProc;
142     IAM_LOGI("success");
143     return UserAuth::SUCCESS;
144 }
145 
DisableSensorIllumination(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor,const SaCommandParam param)146 UserAuth::ResultCode SensorIlluminationManager::DisableSensorIllumination(
147     std::shared_ptr<FingerprintAllInOneExecutorHdi> executor, const SaCommandParam param)
148 {
149     if (executorInProc_ != executor) {
150         IAM_LOGE("illumination is not enabled for this executor");
151         return UserAuth::GENERAL_ERROR;
152     }
153 
154     IAM_LOGI("start");
155     executorInProc_ = nullptr;
156 
157     if (taskInProc_ != nullptr) {
158         UserAuth::ResultCode disableResultCode = taskInProc_->DisableSensorIllumination();
159         if (disableResultCode != UserAuth::ResultCode::SUCCESS) {
160             IAM_LOGE("turn off sensor illumination fail");
161         }
162         taskInProc_ = nullptr;
163     }
164     IAM_LOGI("success");
165     return UserAuth::SUCCESS;
166 }
167 
TurnOnSensorIllumination(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor,const SaCommandParam param)168 UserAuth::ResultCode SensorIlluminationManager::TurnOnSensorIllumination(
169     std::shared_ptr<FingerprintAllInOneExecutorHdi> executor, const SaCommandParam param)
170 {
171     if (executorInProc_ != executor) {
172         IAM_LOGE("illumination is not enabled for this executor");
173         return UserAuth::GENERAL_ERROR;
174     }
175 
176     if (taskInProc_ == nullptr) {
177         IAM_LOGE("no task is running");
178         return UserAuth::GENERAL_ERROR;
179     }
180 
181     UserAuth::ResultCode turnOnResultCode = taskInProc_->TurnOnSensorIllumination();
182     IF_FALSE_LOGE_AND_RETURN_VAL(turnOnResultCode == UserAuth::ResultCode::SUCCESS, UserAuth::GENERAL_ERROR);
183 
184     return UserAuth::SUCCESS;
185 }
186 
TurnOffSensorIllumination(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor,const SaCommandParam param)187 UserAuth::ResultCode SensorIlluminationManager::TurnOffSensorIllumination(
188     std::shared_ptr<FingerprintAllInOneExecutorHdi> executor, const SaCommandParam param)
189 {
190     if (executorInProc_ != executor) {
191         IAM_LOGE("illumination is not enabled for this executor");
192         return UserAuth::GENERAL_ERROR;
193     }
194 
195     if (taskInProc_ == nullptr) {
196         IAM_LOGE("no task is running");
197         return UserAuth::GENERAL_ERROR;
198     }
199 
200     UserAuth::ResultCode turnOffResultCode = taskInProc_->TurnOffSensorIllumination();
201     IF_FALSE_LOGE_AND_RETURN_VAL(turnOffResultCode == UserAuth::ResultCode::SUCCESS, UserAuth::GENERAL_ERROR);
202 
203     return UserAuth::SUCCESS;
204 }
205 } // namespace FingerprintAuth
206 } // namespace UserIam
207 } // namespace OHOS