1 /*
2 * Copyright (c) 2022 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 "driver.h"
17
18 #include "executor_mgr_wrapper.h"
19 #include "iam_check.h"
20 #include "iam_logger.h"
21 #include "iam_ptr.h"
22 #include "iauth_driver_hdi.h"
23 #include "iauth_executor_hdi.h"
24
25 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_EXECUTOR
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
Driver(const std::string & serviceName,HdiConfig hdiConfig)30 Driver::Driver(const std::string &serviceName, HdiConfig hdiConfig) : serviceName_(serviceName), hdiConfig_(hdiConfig)
31 {
32 }
33
OnHdiConnect()34 void Driver::OnHdiConnect()
35 {
36 IAM_LOGI("start");
37 std::lock_guard<std::mutex> lock(mutex_);
38 if (hdiConnected_) {
39 IAM_LOGI("already connected skip");
40 return;
41 }
42 std::vector<std::shared_ptr<IAuthExecutorHdi>> executorHdiList;
43 IF_FALSE_LOGE_AND_RETURN(hdiConfig_.driver != nullptr);
44 hdiConfig_.driver->GetExecutorList(executorHdiList);
45 IAM_LOGI("executorHdiList length is %{public}zu", executorHdiList.size());
46 auto executorMgrWrapper = Common::MakeShared<ExecutorMgrWrapper>();
47 IF_FALSE_LOGE_AND_RETURN(executorMgrWrapper != nullptr);
48 hdiConnected_ = true;
49 for (const auto &executorHdi : executorHdiList) {
50 if (executorHdi == nullptr) {
51 IAM_LOGI("executorHdi is nullptr, skip");
52 continue;
53 }
54 auto executor = Common::MakeShared<Executor>(executorMgrWrapper, executorHdi, hdiConfig_.id);
55 if (executor == nullptr) {
56 IAM_LOGE("MakeShared failed");
57 continue;
58 }
59 executorList_.push_back(executor);
60 executor->OnHdiConnect();
61 IAM_LOGI("add executor %{public}s success", executor->GetDescription());
62 }
63 IAM_LOGI("success");
64 }
65
OnHdiDisconnect()66 void Driver::OnHdiDisconnect()
67 {
68 IAM_LOGI("start");
69 std::lock_guard<std::mutex> lock(mutex_);
70 hdiConnected_ = false;
71 for (const auto &executor : executorList_) {
72 if (executor == nullptr) {
73 IAM_LOGE("executor is null");
74 continue;
75 }
76 executor->OnHdiDisconnect();
77 }
78 executorList_.clear();
79 IAM_LOGI("success");
80 }
81
OnFrameworkReady()82 void Driver::OnFrameworkReady()
83 {
84 IAM_LOGI("start");
85 std::lock_guard<std::mutex> lock(mutex_);
86 for (const auto &executor : executorList_) {
87 if (executor == nullptr) {
88 IAM_LOGE("executor is null");
89 continue;
90 }
91 executor->OnFrameworkReady();
92 }
93 IAM_LOGI("success");
94 }
95 } // namespace UserAuth
96 } // namespace UserIam
97 } // namespace OHOS
98