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 "sa_command_manager.h"
17
18 #include "iam_check.h"
19
20 #define LOG_TAG "FINGERPRINT_AUTH_SA"
21
22 namespace OHOS {
23 namespace UserIam {
24 namespace FingerprintAuth {
GetInstance()25 SaCommandManager &SaCommandManager::GetInstance()
26 {
27 static SaCommandManager manager;
28 return manager;
29 }
30
RegisterSaCommandProcessor(std::vector<SaCommandId> commandIds,std::shared_ptr<ISaCommandProcessor> processor)31 void SaCommandManager::RegisterSaCommandProcessor(std::vector<SaCommandId> commandIds,
32 std::shared_ptr<ISaCommandProcessor> processor)
33 {
34 IF_FALSE_LOGE_AND_RETURN(processor != nullptr);
35
36 std::unique_lock<std::shared_mutex> lock(mutex_);
37
38 processors_.insert(processor);
39
40 for (const auto &commandId : commandIds) {
41 if (commandId2Processors_.find(commandId) == commandId2Processors_.end()) {
42 commandId2Processors_[commandId] = std::set<std::shared_ptr<ISaCommandProcessor>>();
43 }
44
45 commandId2Processors_[commandId].insert(processor);
46 }
47 }
48
UnregisterSaCommandProcessor(std::vector<SaCommandId> commandIds,std::shared_ptr<ISaCommandProcessor> processor)49 void SaCommandManager::UnregisterSaCommandProcessor(std::vector<SaCommandId> commandIds,
50 std::shared_ptr<ISaCommandProcessor> processor)
51 {
52 IF_FALSE_LOGE_AND_RETURN(processor != nullptr);
53
54 std::unique_lock<std::shared_mutex> lock(mutex_);
55
56 processors_.erase(processor);
57
58 for (const auto &commandId : commandIds) {
59 if (commandId2Processors_.find(commandId) == commandId2Processors_.end()) {
60 continue;
61 }
62
63 commandId2Processors_[commandId].erase(processor);
64 }
65 }
66
ProcessSaCommands(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor,const std::vector<SaCommand> & commands)67 UserAuth::ResultCode SaCommandManager::ProcessSaCommands(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor,
68 const std::vector<SaCommand> &commands)
69 {
70 std::shared_lock<std::shared_mutex> lock(mutex_);
71
72 for (const auto &command : commands) {
73 IAM_LOGI("process command %{public}d", command.id);
74 auto it = commandId2Processors_.find(static_cast<SaCommandId>(command.id));
75 IF_FALSE_LOGE_AND_RETURN_VAL(it != commandId2Processors_.end(), UserAuth::GENERAL_ERROR);
76 for (const auto &processor : commandId2Processors_[static_cast<SaCommandId>(command.id)]) {
77 IF_FALSE_LOGE_AND_RETURN_VAL(processor != nullptr, UserAuth::GENERAL_ERROR);
78 UserAuth::ResultCode result = processor->ProcessSaCommand(executor, command);
79 IF_FALSE_LOGE_AND_RETURN_VAL(result == UserAuth::SUCCESS, UserAuth::GENERAL_ERROR);
80 }
81 }
82
83 return UserAuth::SUCCESS;
84 }
85
OnHdiDisconnect(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor)86 void SaCommandManager::OnHdiDisconnect(std::shared_ptr<FingerprintAllInOneExecutorHdi> executor)
87 {
88 std::shared_lock<std::shared_mutex> lock(mutex_);
89
90 for (const auto &processor : processors_) {
91 IF_FALSE_LOGE_AND_RETURN(processor != nullptr);
92 processor->OnHdiDisconnect(executor);
93 }
94
95 return;
96 }
97 } // namespace FingerprintAuth
98 } // namespace UserIam
99 } // namespace OHOS