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 "async_command_base.h"
17
18 #include <atomic>
19 #include <cstdint>
20 #include <ostream>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24
25 #include "iam_check.h"
26 #include "iam_defines.h"
27 #include "iam_logger.h"
28 #include "iam_para2str.h"
29 #include "framework_types.h"
30
31 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_EXECUTOR
32
33 namespace OHOS {
34 namespace UserIam {
35 namespace UserAuth {
AsyncCommandBase(std::string type,uint64_t scheduleId,std::weak_ptr<Executor> executor,std::shared_ptr<ExecutorMessenger> executorMessenger)36 AsyncCommandBase::AsyncCommandBase(std::string type, uint64_t scheduleId, std::weak_ptr<Executor> executor,
37 std::shared_ptr<ExecutorMessenger> executorMessenger)
38 : scheduleId_(scheduleId),
39 executor_(executor),
40 executorMessenger_(executorMessenger)
41 {
42 auto commandId = GenerateCommandId();
43 std::ostringstream ss;
44 ss << "Command(type:" << type << ", id:" << commandId << ", scheduleId:" << GET_MASKED_STRING(scheduleId_) << ")";
45 description_ = ss.str();
46 }
47
OnHdiDisconnect()48 void AsyncCommandBase::OnHdiDisconnect()
49 {
50 IAM_LOGI("driver disconnect, %{public}s end process", GetDescription());
51 // Need new result code: hal invalid
52 OnResult(ResultCode::GENERAL_ERROR);
53 }
54
StartProcess()55 ResultCode AsyncCommandBase::StartProcess()
56 {
57 IAM_LOGI("%{public}s start process", GetDescription());
58 auto executor = executor_.lock();
59 if (executor == nullptr) {
60 IAM_LOGE("%{public}s executor has been released, start process fail", GetDescription());
61 return ResultCode::GENERAL_ERROR;
62 }
63 executor->AddCommand(shared_from_this());
64 ResultCode ret = SendRequest();
65 if (ret != ResultCode::SUCCESS) {
66 IAM_LOGE("%{public}s send request failed", GetDescription());
67 EndProcess();
68 return ret;
69 }
70 return ResultCode::SUCCESS;
71 }
72
OnResult(ResultCode result)73 void AsyncCommandBase::OnResult(ResultCode result)
74 {
75 std::vector<uint8_t> extraInfo;
76 OnResult(result, extraInfo);
77 }
78
OnResult(ResultCode result,const std::vector<uint8_t> & extraInfo)79 void AsyncCommandBase::OnResult(ResultCode result, const std::vector<uint8_t> &extraInfo)
80 {
81 std::lock_guard<std::mutex> guard(mutex_);
82 if (isFinished_) {
83 IAM_LOGE("command is finished, invocation of OnResult is invalid");
84 return;
85 }
86 isFinished_ = true;
87 OnResultInner(result, extraInfo);
88 EndProcess();
89 }
90
OnAcquireInfo(int32_t acquire,const std::vector<uint8_t> & extraInfo)91 void AsyncCommandBase::OnAcquireInfo(int32_t acquire, const std::vector<uint8_t> &extraInfo)
92 {
93 std::lock_guard<std::mutex> guard(mutex_);
94 if (isFinished_) {
95 IAM_LOGE("command is finished, invocation of OnAcquireInfo is invalid");
96 return;
97 }
98 OnAcquireInfoInner(acquire, extraInfo);
99 }
100
GetAuthType()101 int32_t AsyncCommandBase::GetAuthType()
102 {
103 auto executor = executor_.lock();
104 if (executor == nullptr) {
105 IAM_LOGE("%{public}s executor has been released, get executor type fail", GetDescription());
106 return INVALID_AUTH_TYPE;
107 }
108 return executor->GetAuthType();
109 }
110
EndProcess()111 void AsyncCommandBase::EndProcess()
112 {
113 IAM_LOGI("%{public}s end process", GetDescription());
114 auto executor = executor_.lock();
115 if (executor == nullptr) {
116 IAM_LOGI(
117 "%{public}s executor has been released, command has been removed, no need remove again", GetDescription());
118 return;
119 }
120 executor->RemoveCommand(shared_from_this());
121 }
122
GetDescription()123 const char *AsyncCommandBase::GetDescription()
124 {
125 return description_.c_str();
126 }
127
GenerateCommandId()128 uint32_t AsyncCommandBase::GenerateCommandId()
129 {
130 std::atomic<uint32_t> commandId = 0;
131 // commandId is only used in log, uint32 overflow or duplicate is ok
132 return ++commandId;
133 }
134
GetExecutorHdi()135 std::shared_ptr<IAuthExecutorHdi> AsyncCommandBase::GetExecutorHdi()
136 {
137 auto executor = executor_.lock();
138 if (executor == nullptr) {
139 IAM_LOGE("%{public}s executor has been released, get executor hdi fail", GetDescription());
140 return nullptr;
141 }
142 return executor->GetExecutorHdi();
143 }
144
MessengerSendData(uint64_t scheduleId,uint64_t transNum,ExecutorRole srcType,ExecutorRole dstType,std::shared_ptr<AuthMessage> msg)145 int32_t AsyncCommandBase::MessengerSendData(uint64_t scheduleId, uint64_t transNum, ExecutorRole srcType,
146 ExecutorRole dstType, std::shared_ptr<AuthMessage> msg)
147 {
148 auto messenger = executorMessenger_;
149 IF_FALSE_LOGE_AND_RETURN_VAL(messenger != nullptr, USERAUTH_ERROR);
150 return messenger->SendData(scheduleId, transNum, srcType, dstType, msg);
151 }
152
MessengerFinish(uint64_t scheduleId,ExecutorRole srcType,int32_t resultCode,std::shared_ptr<Attributes> finalResult)153 int32_t AsyncCommandBase::MessengerFinish(uint64_t scheduleId, ExecutorRole srcType, int32_t resultCode,
154 std::shared_ptr<Attributes> finalResult)
155 {
156 auto messenger = executorMessenger_;
157 IF_FALSE_LOGE_AND_RETURN_VAL(messenger != nullptr, USERAUTH_ERROR);
158 int32_t ret = messenger->Finish(scheduleId, srcType, resultCode, *finalResult);
159 executorMessenger_ = nullptr;
160 return ret;
161 }
162 } // namespace UserAuth
163 } // namespace UserIam
164 } // namespace OHOS
165