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 "FACE_AUTH_SA" 21 22namespace OHOS { 23namespace UserIam { 24namespace FaceAuth { 25SaCommandManager &SaCommandManager::GetInstance() 26{ 27 static SaCommandManager manager; 28 return manager; 29} 30 31void 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 49void 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 67UserAuth::ResultCode SaCommandManager::ProcessSaCommands(std::shared_ptr<FaceAuthAllInOneExecutorHdi> 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 86void SaCommandManager::OnHdiDisconnect(std::shared_ptr<FaceAuthAllInOneExecutorHdi> 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 FaceAuth 98} // namespace UserIam 99} // namespace OHOS