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 "iam_executor_iauth_driver_hdi.h"
23 #include "iam_executor_iauth_executor_hdi.h"
24
25 #define LOG_TAG "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 if (executorHdiList.empty()) {
47 IAM_LOGE("executorHdiList is empty, hdiConnected fail.");
48 return;
49 }
50 auto executorMgrWrapper = Common::MakeShared<ExecutorMgrWrapper>();
51 IF_FALSE_LOGE_AND_RETURN(executorMgrWrapper != nullptr);
52 hdiConnected_ = true;
53 for (const auto &executorHdi : executorHdiList) {
54 if (executorHdi == nullptr) {
55 IAM_LOGI("executorHdi is nullptr, skip");
56 continue;
57 }
58 auto executor = Common::MakeShared<Executor>(executorMgrWrapper, executorHdi, hdiConfig_.id);
59 if (executor == nullptr) {
60 IAM_LOGE("MakeShared failed");
61 continue;
62 }
63 executorList_.push_back(executor);
64 IAM_LOGI("add executor %{public}s success", executor->GetDescription());
65 }
66
67 if (!isFwkReady_) {
68 IAM_LOGE("fwk not ready, skip");
69 return;
70 }
71
72 RegisterExecutors();
73 }
74
OnHdiDisconnect()75 void Driver::OnHdiDisconnect()
76 {
77 IAM_LOGI("start");
78 std::lock_guard<std::mutex> lock(mutex_);
79 hdiConnected_ = false;
80 for (const auto &executor : executorList_) {
81 if (executor == nullptr) {
82 IAM_LOGE("executor is null");
83 continue;
84 }
85 executor->OnHdiDisconnect();
86 }
87 executorList_.clear();
88
89 IF_FALSE_LOGE_AND_RETURN(hdiConfig_.driver != nullptr);
90 hdiConfig_.driver->OnHdiDisconnect();
91 IAM_LOGI("success");
92 }
93
OnFrameworkDown()94 void Driver::OnFrameworkDown()
95 {
96 IAM_LOGI("start");
97 std::lock_guard<std::mutex> lock(mutex_);
98 isFwkReady_ = false;
99 IF_FALSE_LOGE_AND_RETURN(hdiConfig_.driver != nullptr);
100 hdiConfig_.driver->OnFrameworkDown();
101 IAM_LOGI("success");
102 }
103
OnFrameworkReady()104 void Driver::OnFrameworkReady()
105 {
106 IAM_LOGI("start");
107 std::lock_guard<std::mutex> lock(mutex_);
108 if (isFwkReady_) {
109 IAM_LOGI("already fwk ready, skip");
110 return;
111 }
112 isFwkReady_ = true;
113 if (!hdiConnected_) {
114 IAM_LOGE("hdi not connected, skip");
115 return;
116 }
117
118 RegisterExecutors();
119 }
120
RegisterExecutors()121 void Driver::RegisterExecutors()
122 {
123 for (const auto &executor : executorList_) {
124 if (executor == nullptr) {
125 IAM_LOGE("executor is null");
126 continue;
127 }
128 executor->Register();
129 }
130 IAM_LOGI("success");
131 }
132 } // namespace UserAuth
133 } // namespace UserIam
134 } // namespace OHOS
135