• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LABEL UserIam::Common::LABEL_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     std::ostringstream ss;
45     uint32_t combineExecutorId =
46         Common::CombineUint16ToUint32(hdiId_, static_cast<uint16_t>(executorInfo.executorSensorHint));
47     const uint32_t uint32HexWidth = 8;
48     ss << "Executor(Id:0x" << std::setfill('0') << std::setw(uint32HexWidth) << std::hex << combineExecutorId << ")";
49     description_ = ss.str();
50 }
51 
OnHdiConnect()52 void Executor::OnHdiConnect()
53 {
54     IAM_LOGI("%{public}s start", GetDescription());
55     // register resource pool depends on hdi connect, after hid connect re-register resource pool
56     OnFrameworkReady();
57 }
58 
OnHdiDisconnect()59 void Executor::OnHdiDisconnect()
60 {
61     IAM_LOGI("%{public}s start", GetDescription());
62     executorHdi_ = nullptr;
63     RespondCallbackOnDisconnect();
64 }
65 
OnFrameworkReady()66 void Executor::OnFrameworkReady()
67 {
68     IAM_LOGI("%{public}s start", GetDescription());
69     ExecutorInfo executorInfo = {};
70     auto hdi = executorHdi_;
71     if (hdi == nullptr) {
72         IAM_LOGE("executorHdi_ is disconnected, skip framework ready process");
73         return;
74     }
75     ResultCode ret = hdi->GetExecutorInfo(executorInfo);
76     if (ret != ResultCode::SUCCESS) {
77         IAM_LOGE("Get executor info failed");
78         return;
79     }
80     RegisterExecutorCallback(executorInfo);
81 }
82 
RegisterExecutorCallback(ExecutorInfo & executorInfo)83 void Executor::RegisterExecutorCallback(ExecutorInfo &executorInfo)
84 {
85     IAM_LOGI("%{public}s start", GetDescription());
86     uint32_t combineExecutorId =
87         Common::CombineUint16ToUint32(hdiId_, static_cast<uint16_t>(executorInfo.executorSensorHint));
88     executorInfo.executorSensorHint = combineExecutorId;
89     if (executorCallback_ == nullptr) {
90         std::lock_guard<std::mutex> lock(mutex_);
91         if (executorCallback_ == nullptr) {
92             auto localExecutorCallback = Common::MakeShared<FrameworkExecutorCallback>(weak_from_this());
93             IF_FALSE_LOGE_AND_RETURN(localExecutorCallback != nullptr);
94             executorCallback_ = localExecutorCallback;
95         }
96     }
97     IF_FALSE_LOGE_AND_RETURN(executorMgrWrapper_ != nullptr);
98     executorMgrWrapper_->Register(executorInfo, executorCallback_);
99     IAM_LOGI("register executor callback ok, executor id %{public}s",
100         GET_MASKED_STRING(executorInfo.executorSensorHint).c_str());
101 }
102 
AddCommand(std::shared_ptr<IAsyncCommand> command)103 void Executor::AddCommand(std::shared_ptr<IAsyncCommand> command)
104 {
105     IAM_LOGI("%{public}s start", GetDescription());
106     IF_FALSE_LOGE_AND_RETURN(command != nullptr);
107     std::lock_guard<std::mutex> lock(mutex_);
108     command2Respond_.insert(command);
109 }
110 
RemoveCommand(std::shared_ptr<IAsyncCommand> command)111 void Executor::RemoveCommand(std::shared_ptr<IAsyncCommand> command)
112 {
113     IAM_LOGI("%{public}s start", GetDescription());
114     std::lock_guard<std::mutex> lock(mutex_);
115     command2Respond_.erase(command);
116 }
117 
RespondCallbackOnDisconnect()118 void Executor::RespondCallbackOnDisconnect()
119 {
120     IAM_LOGI("%{public}s start", GetDescription());
121     std::set<std::shared_ptr<IAsyncCommand>> command2NotifyOnHdiDisconnect;
122     {
123         // cmd->OnHdiDisconnect will invoke RemoveCommand thus modify command2Respond_, make a copy before call
124         // OnHdiDisconnect
125         std::lock_guard<std::mutex> lock(mutex_);
126         command2NotifyOnHdiDisconnect =
127             std::set<std::shared_ptr<IAsyncCommand>>(command2Respond_.begin(), command2Respond_.end());
128     }
129 
130     for (const auto &cmd : command2NotifyOnHdiDisconnect) {
131         if (cmd == nullptr) {
132             IAM_LOGE("cmd is null");
133             continue;
134         }
135         cmd->OnHdiDisconnect();
136     }
137     IAM_LOGI("success");
138 }
139 
GetExecutorHdi()140 std::shared_ptr<IAuthExecutorHdi> Executor::GetExecutorHdi()
141 {
142     return executorHdi_;
143 }
144 
GetDescription()145 const char *Executor::GetDescription()
146 {
147     return description_.c_str();
148 }
149 
GetAuthType() const150 int32_t Executor::GetAuthType() const
151 {
152     return authType_;
153 }
154 } // namespace UserAuth
155 } // namespace UserIam
156 } // namespace OHOS
157