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 "custom_command.h"
17 
18 #include <chrono>
19 #include <cstdint>
20 #include <future>
21 #include <string>
22 
23 #include "refbase.h"
24 
25 #include "iam_executor_framework_types.h"
26 #include "iam_check.h"
27 #include "iam_logger.h"
28 #include "iam_ptr.h"
29 #include "iam_executor_iauth_executor_hdi.h"
30 
31 #define LOG_TAG "USER_AUTH_EXECUTOR"
32 
33 namespace OHOS {
34 namespace UserIam {
35 namespace UserAuth {
CustomCommand(std::weak_ptr<Executor> executor,const Attributes & attributes)36 CustomCommand::CustomCommand(std::weak_ptr<Executor> executor, const Attributes &attributes)
37     : AsyncCommandBase("CUSTOM", 0, executor, nullptr),
38       attributes_(Common::MakeShared<Attributes>(attributes.Serialize()))
39 {
40 }
41 
SendRequest()42 ResultCode CustomCommand::SendRequest()
43 {
44     IAM_LOGI("%{public}s send request start", GetDescription());
45     IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR);
46 
47     auto hdi = GetExecutorHdi();
48     IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
49 
50     future_ = promise_.get_future();
51     uint32_t commandId = 0;
52     bool getAuthPropertyModeRet = attributes_->GetUint32Value(Attributes::ATTR_PROPERTY_MODE, commandId);
53     IF_FALSE_LOGE_AND_RETURN_VAL(getAuthPropertyModeRet == true, ResultCode::GENERAL_ERROR);
54 
55     std::vector<uint8_t> extraInfo;
56     bool getExtraInfoRet = attributes_->GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
57     IF_FALSE_LOGE_AND_RETURN_VAL(getExtraInfoRet == true, ResultCode::GENERAL_ERROR);
58 
59     ResultCode ret = hdi->SendCommand(static_cast<UserAuth::PropertyMode>(commandId), extraInfo, shared_from_this());
60     if (ret != ResultCode::SUCCESS) {
61         IAM_LOGE("%{public}s send command result fail ret = %{public}d", GetDescription(), ret);
62         OnResult(ret);
63         return ret;
64     }
65 
66     IAM_LOGI("%{public}s send command result success", GetDescription());
67     return ResultCode::SUCCESS;
68 }
69 
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)70 void CustomCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
71 {
72     IAM_LOGI("%{public}s on result start", GetDescription());
73     SetResult(result);
74 }
75 
OnAcquireInfoInner(int32_t acquire,const std::vector<uint8_t> & extraInfo)76 void CustomCommand::OnAcquireInfoInner(int32_t acquire, const std::vector<uint8_t> &extraInfo)
77 {
78     IAM_LOGE("%{public}s not support", GetDescription());
79 }
80 
OnMessageInner(int destRole,const std::vector<uint8_t> & msg)81 void CustomCommand::OnMessageInner(int destRole, const std::vector<uint8_t> &msg)
82 {
83     IAM_LOGE("%{public}s not support", GetDescription());
84 }
85 
GetResult()86 ResultCode CustomCommand::GetResult()
87 {
88     if (!future_.valid()) {
89         IAM_LOGE("%{public}s get result before request send, error", GetDescription());
90         return ResultCode::GENERAL_ERROR;
91     }
92     IAM_LOGI("%{public}s begin wait future", GetDescription());
93     static const std::chrono::seconds maxWaitTime(1);
94     auto ret = future_.wait_for(maxWaitTime);
95     if (ret != std::future_status::ready) {
96         IAM_LOGE("%{public}s future timeout", GetDescription());
97         return ResultCode::TIMEOUT;
98     }
99     IAM_LOGI("%{public}s get result %{public}d", GetDescription(), result_);
100     return result_;
101 }
102 
SetResult(ResultCode resultCode)103 void CustomCommand::SetResult(ResultCode resultCode)
104 {
105     result_ = resultCode;
106     promise_.set_value();
107 }
108 } // namespace UserAuth
109 } // namespace UserIam
110 } // namespace OHOS
111