1 /*
2  * Copyright (c) 2024 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 "collect_command.h"
17 
18 #include "iam_check.h"
19 #include "iam_common_defines.h"
20 #include "iam_executor_framework_types.h"
21 #include "iam_logger.h"
22 #include "iam_para2str.h"
23 #include "iam_ptr.h"
24 
25 #define LOG_TAG "USER_COLLECT_EXECUTOR"
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
CollectCommand(std::weak_ptr<Executor> executor,uint64_t scheduleId,const Attributes & attributes,std::shared_ptr<ExecutorMessenger> executorMessenger)30 CollectCommand::CollectCommand(std::weak_ptr<Executor> executor, uint64_t scheduleId,
31     const Attributes &attributes, std::shared_ptr<ExecutorMessenger> executorMessenger)
32     : AsyncCommandBase("COLLECT", scheduleId, executor, executorMessenger),
33       attributes_(Common::MakeShared<Attributes>(attributes.Serialize())),
34       iamHitraceHelper_(Common::MakeShared<IamHitraceHelper>("CollectCommand"))
35 {
36 }
37 
SendRequest()38 ResultCode CollectCommand::SendRequest()
39 {
40     IAM_LOGI("%{public}s send request start", GetDescription());
41     IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR);
42 
43     auto hdi = GetExecutorHdi();
44     IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
45 
46     uint32_t collectorTokenId = 0;
47     bool getCollectorTokenIdRet = attributes_->GetUint32Value(Attributes::ATTR_COLLECTOR_TOKEN_ID, collectorTokenId);
48     IF_FALSE_LOGE_AND_RETURN_VAL(getCollectorTokenIdRet == true, ResultCode::GENERAL_ERROR);
49     std::vector<uint8_t> extraInfo;
50     bool getExtraInfoRet = attributes_->GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
51     IF_FALSE_LOGE_AND_RETURN_VAL(getExtraInfoRet == true, ResultCode::GENERAL_ERROR);
52     IAM_LOGI("%{public}s collect message len %{public}zu", GetDescription(), extraInfo.size());
53 
54     IamHitraceHelper traceHelper("hdi collect");
55     ResultCode ret = hdi->Collect(scheduleId_, (CollectParam) { 0, collectorTokenId, extraInfo }, shared_from_this());
56     IAM_LOGI("%{public}s collect result %{public}d", GetDescription(), ret);
57     return ret;
58 }
59 
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)60 void CollectCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
61 {
62     IAM_LOGI("%{public}s on result start", GetDescription());
63 
64     std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
65     auto authAttributes = Common::MakeShared<Attributes>();
66     IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr);
67     bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result);
68     IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
69     bool setCollectResultRet =
70         authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo);
71     IF_FALSE_LOGE_AND_RETURN(setCollectResultRet == true);
72     iamHitraceHelper_ = nullptr;
73     int32_t ret = MessengerFinish(scheduleId_, result, authAttributes);
74     if (ret != USERAUTH_SUCCESS) {
75         IAM_LOGE("%{public}s call finish fail", GetDescription());
76         return;
77     }
78     IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result);
79 }
80 } // namespace UserAuth
81 } // namespace UserIam
82 } // namespace OHOS
83