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 "identify_command.h"
17
18 #include "iam_check.h"
19 #include "iam_logger.h"
20 #include "iam_para2str.h"
21 #include "iam_ptr.h"
22
23 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_EXECUTOR
24
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
IdentifyCommand(std::weak_ptr<Executor> executor,uint64_t scheduleId,const Attributes & attributes,std::shared_ptr<ExecutorMessenger> executorMessenger)28 IdentifyCommand::IdentifyCommand(std::weak_ptr<Executor> executor, uint64_t scheduleId,
29 const Attributes &attributes, std::shared_ptr<ExecutorMessenger> executorMessenger)
30 : AsyncCommandBase("IDENTIFY", scheduleId, executor, executorMessenger),
31 attributes_(Common::MakeShared<Attributes>(attributes.Serialize())),
32 iamHitraceHelper_(Common::MakeShared<IamHitraceHelper>("IdentifyCommand"))
33 {
34 }
35
SendRequest()36 ResultCode IdentifyCommand::SendRequest()
37 {
38 IAM_LOGI("%{public}s send request start", GetDescription());
39 IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR);
40
41 auto hdi = GetExecutorHdi();
42 IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
43
44 uint32_t tokenId;
45 std::vector<uint8_t> extraInfo;
46 bool getTokenIdRet = attributes_->GetUint32Value(Attributes::ATTR_ACCESS_TOKEN_ID, tokenId);
47 IF_FALSE_LOGE_AND_RETURN_VAL(getTokenIdRet == true, ResultCode::GENERAL_ERROR);
48
49 IamHitraceHelper traceHelper("hdi Identify");
50 ResultCode ret = hdi->Identify(scheduleId_, tokenId, extraInfo, shared_from_this());
51 IAM_LOGI("%{public}s identify result %{public}d", GetDescription(), ret);
52 return ret;
53 }
54
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)55 void IdentifyCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
56 {
57 IAM_LOGI("%{public}s on result start", GetDescription());
58
59 std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
60 auto authAttributes = Common::MakeShared<Attributes>();
61 IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr);
62 bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result);
63 IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
64 bool setAuthResultRet =
65 authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo);
66 IF_FALSE_LOGE_AND_RETURN(setAuthResultRet == true);
67 iamHitraceHelper_ = nullptr;
68 int32_t ret = MessengerFinish(scheduleId_, ALL_IN_ONE, result, authAttributes);
69 if (ret != USERAUTH_SUCCESS) {
70 IAM_LOGE("%{public}s call finish fail", GetDescription());
71 return;
72 }
73 IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result);
74 }
75
OnAcquireInfoInner(int32_t acquire,const std::vector<uint8_t> & extraInfo)76 void IdentifyCommand::OnAcquireInfoInner(int32_t acquire, const std::vector<uint8_t> &extraInfo)
77 {
78 IAM_LOGI("%{public}s on acquire info start", GetDescription());
79
80 std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
81 auto msg = AuthMessage::As(nonConstExtraInfo);
82 IF_FALSE_LOGE_AND_RETURN(msg != nullptr);
83 int32_t ret = MessengerSendData(scheduleId_, transNum_, ALL_IN_ONE, SCHEDULER, msg);
84 ++transNum_;
85 if (ret != USERAUTH_SUCCESS) {
86 IAM_LOGE("%{public}s call SendData fail", GetDescription());
87 return;
88 }
89 IAM_LOGI("%{public}s call SendData success acquire %{public}d", GetDescription(), acquire);
90 }
91 } // namespace UserAuth
92 } // namespace UserIam
93 } // namespace OHOS
94