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         executor->OnHdiConnect();
65         IAM_LOGI("add executor %{public}s success", executor->GetDescription());
66     }
67     IAM_LOGI("success");
68 }
69 
OnHdiDisconnect()70 void Driver::OnHdiDisconnect()
71 {
72     IAM_LOGI("start");
73     std::lock_guard<std::mutex> lock(mutex_);
74     hdiConnected_ = false;
75     for (const auto &executor : executorList_) {
76         if (executor == nullptr) {
77             IAM_LOGE("executor is null");
78             continue;
79         }
80         executor->OnHdiDisconnect();
81     }
82     executorList_.clear();
83 
84     IF_FALSE_LOGE_AND_RETURN(hdiConfig_.driver != nullptr);
85     hdiConfig_.driver->OnHdiDisconnect();
86     IAM_LOGI("success");
87 }
88 
OnFrameworkReady()89 void Driver::OnFrameworkReady()
90 {
91     IAM_LOGI("start");
92     std::lock_guard<std::mutex> lock(mutex_);
93     for (const auto &executor : executorList_) {
94         if (executor == nullptr) {
95             IAM_LOGE("executor is null");
96             continue;
97         }
98         executor->OnFrameworkReady();
99     }
100     IAM_LOGI("success");
101 }
102 } // namespace UserAuth
103 } // namespace UserIam
104 } // namespace OHOS
105