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 "executor.h"
17 
18 #include <sstream>
19 
20 #include "framework_executor_callback.h"
21 #include "iam_check.h"
22 #include "iam_common_defines.h"
23 #include "iam_logger.h"
24 #include "iam_mem.h"
25 #include "iam_para2str.h"
26 #include "iam_ptr.h"
27 
28 #define LOG_TAG "USER_AUTH_EXECUTOR"
29 
30 namespace OHOS {
31 namespace UserIam {
32 namespace UserAuth {
Executor(std::shared_ptr<ExecutorMgrWrapper> executorMgrWrapper,std::shared_ptr<IAuthExecutorHdi> executorHdi,uint16_t hdiId)33 Executor::Executor(std::shared_ptr<ExecutorMgrWrapper> executorMgrWrapper,
34     std::shared_ptr<IAuthExecutorHdi> executorHdi, uint16_t hdiId)
35     : executorMgrWrapper_(executorMgrWrapper),
36       executorHdi_(executorHdi),
37       hdiId_(hdiId)
38 {
39     auto hdi = executorHdi_;
40     IF_FALSE_LOGE_AND_RETURN(hdi != nullptr);
41     ExecutorInfo executorInfo = {};
42     IF_FALSE_LOGE_AND_RETURN(hdi->GetExecutorInfo(executorInfo) == ResultCode::SUCCESS);
43     authType_ = executorInfo.authType;
44     executorRole_ = executorInfo.executorRole;
45     std::ostringstream ss;
46     uint32_t combineExecutorId =
47         Common::CombineUint16ToUint32(hdiId_, static_cast<uint16_t>(executorInfo.executorSensorHint));
48     const uint32_t uint32HexWidth = 8;
49     ss << "Executor(Id:0x" << std::setfill('0') << std::setw(uint32HexWidth) << std::hex << combineExecutorId
50         << ", role:" << executorRole_ << ", authType:" << authType_ << ")";
51     description_ = ss.str();
52 }
53 
OnHdiConnect()54 void Executor::OnHdiConnect()
55 {
56     IAM_LOGI("%{public}s start", GetDescription());
57     // register resource pool depends on hdi connect, after hdi connect re-register resource pool
58     OnFrameworkReady();
59 }
60 
OnHdiDisconnect()61 void Executor::OnHdiDisconnect()
62 {
63     IAM_LOGI("%{public}s start", GetDescription());
64     {
65         std::lock_guard<std::recursive_mutex> lock(mutex_);
66         executorHdi_ = nullptr;
67     }
68     RespondCallbackOnDisconnect();
69     UnregisterExecutorCallback();
70 }
71 
OnFrameworkReady()72 void Executor::OnFrameworkReady()
73 {
74     IAM_LOGI("%{public}s start", GetDescription());
75     ExecutorInfo executorInfo = {};
76     auto hdi = GetExecutorHdi();
77     if (hdi == nullptr) {
78         IAM_LOGE("executorHdi_ is disconnected, skip framework ready process");
79         return;
80     }
81     ResultCode ret = hdi->GetExecutorInfo(executorInfo);
82     if (ret != ResultCode::SUCCESS) {
83         IAM_LOGE("Get executor info failed");
84         return;
85     }
86     RegisterExecutorCallback(executorInfo);
87 }
88 
RegisterExecutorCallback(ExecutorInfo & executorInfo)89 void Executor::RegisterExecutorCallback(ExecutorInfo &executorInfo)
90 {
91     IAM_LOGI("%{public}s start", GetDescription());
92     std::lock_guard<std::recursive_mutex> lockRegister(registerMutex_);
93     uint32_t combineExecutorId =
94         Common::CombineUint16ToUint32(hdiId_, static_cast<uint16_t>(executorInfo.executorSensorHint));
95     executorInfo.executorSensorHint = combineExecutorId;
96     std::shared_ptr<ExecutorRegisterCallback> executorCallback = nullptr;
97     bool isExecutorRegistered = false;
98     {
99         std::lock_guard<std::recursive_mutex> lockCallback(mutex_);
100         if (executorCallback_ == nullptr) {
101             executorCallback_ = Common::MakeShared<FrameworkExecutorCallback>(weak_from_this());
102             IF_FALSE_LOGE_AND_RETURN(executorCallback_ != nullptr);
103         }
104         executorCallback = executorCallback_;
105 
106         if (executorIndex_.has_value()) {
107             isExecutorRegistered = true;
108         }
109     }
110     if (isExecutorRegistered) {
111         IAM_LOGI("%{public}s executor already registered, try unregister", GetDescription());
112         UnregisterExecutorCallback();
113     }
114     IF_FALSE_LOGE_AND_RETURN(executorMgrWrapper_ != nullptr);
115     uint64_t executorIndex = executorMgrWrapper_->Register(executorInfo, executorCallback);
116     IF_FALSE_LOGE_AND_RETURN(executorIndex != INVALID_EXECUTOR_INDEX);
117     {
118         std::lock_guard<std::recursive_mutex> lockExecutorIndex(mutex_);
119         executorIndex_ = executorIndex;
120     }
121     IAM_LOGI("%{public}s register executor callback ok, executor index %{public}s", GetDescription(),
122         GET_MASKED_STRING(executorIndex).c_str());
123 }
124 
UnregisterExecutorCallback()125 void Executor::UnregisterExecutorCallback()
126 {
127     IAM_LOGI("%{public}s start", GetDescription());
128     std::lock_guard<std::recursive_mutex> lockRegister(registerMutex_);
129     uint64_t executorIndex = 0;
130     {
131         std::lock_guard<std::recursive_mutex> lockExecutorIndex(mutex_);
132         if (!executorIndex_.has_value()) {
133             IAM_LOGI("not registered, no need unregister");
134             return;
135         }
136         executorIndex = executorIndex_.value();
137         executorIndex_ = std::nullopt;
138     }
139 
140     IF_FALSE_LOGE_AND_RETURN(executorMgrWrapper_ != nullptr);
141     executorMgrWrapper_->Unregister(executorIndex);
142     IAM_LOGI("%{public}s unregister executor callback ok, executor index %{public}s", GetDescription(),
143         GET_MASKED_STRING(executorIndex).c_str());
144 }
145 
AddCommand(std::shared_ptr<IAsyncCommand> command)146 void Executor::AddCommand(std::shared_ptr<IAsyncCommand> command)
147 {
148     IAM_LOGI("%{public}s start", GetDescription());
149     IF_FALSE_LOGE_AND_RETURN(command != nullptr);
150     std::lock_guard<std::recursive_mutex> lock(mutex_);
151     command2Respond_.insert(command);
152 }
153 
RemoveCommand(std::shared_ptr<IAsyncCommand> command)154 void Executor::RemoveCommand(std::shared_ptr<IAsyncCommand> command)
155 {
156     IAM_LOGI("%{public}s start", GetDescription());
157     std::lock_guard<std::recursive_mutex> lock(mutex_);
158     command2Respond_.erase(command);
159 }
160 
RespondCallbackOnDisconnect()161 void Executor::RespondCallbackOnDisconnect()
162 {
163     IAM_LOGI("%{public}s start", GetDescription());
164     std::set<std::shared_ptr<IAsyncCommand>> command2NotifyOnHdiDisconnect;
165     {
166         // cmd->OnHdiDisconnect will invoke RemoveCommand thus modify command2Respond_, make a copy before call
167         std::lock_guard<std::recursive_mutex> lock(mutex_);
168         command2NotifyOnHdiDisconnect =
169             std::set<std::shared_ptr<IAsyncCommand>>(command2Respond_.begin(), command2Respond_.end());
170     }
171 
172     for (const auto &cmd : command2NotifyOnHdiDisconnect) {
173         if (cmd == nullptr) {
174             IAM_LOGE("cmd is null");
175             continue;
176         }
177         cmd->OnHdiDisconnect();
178     }
179     IAM_LOGI("success");
180 }
181 
GetExecutorHdi()182 std::shared_ptr<IAuthExecutorHdi> Executor::GetExecutorHdi()
183 {
184     std::lock_guard<std::recursive_mutex> lock(mutex_);
185     return executorHdi_;
186 }
187 
GetDescription()188 const char *Executor::GetDescription()
189 {
190     return description_.c_str();
191 }
192 
GetAuthType() const193 int32_t Executor::GetAuthType() const
194 {
195     return authType_;
196 }
197 
GetExecutorRole() const198 int32_t Executor::GetExecutorRole() const
199 {
200     return executorRole_;
201 }
202 } // namespace UserAuth
203 } // namespace UserIam
204 } // namespace OHOS
205