• 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 "custom_command.h"
17 
18 #include <chrono>
19 #include <cstdint>
20 #include <future>
21 #include <string>
22 
23 #include "refbase.h"
24 
25 #include "framework_types.h"
26 #include "iam_check.h"
27 #include "iam_logger.h"
28 #include "iam_ptr.h"
29 #include "iauth_executor_hdi.h"
30 
31 #define LOG_LABEL UserIam::Common::LABEL_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 
GetResult()81 ResultCode CustomCommand::GetResult()
82 {
83     if (!future_.valid()) {
84         IAM_LOGE("%{public}s get result before request send, error", GetDescription());
85         return ResultCode::GENERAL_ERROR;
86     }
87     IAM_LOGI("%{public}s begin wait future", GetDescription());
88     static const std::chrono::seconds maxWaitTime(1);
89     auto ret = future_.wait_for(maxWaitTime);
90     if (ret != std::future_status::ready) {
91         IAM_LOGE("%{public}s future timeout", GetDescription());
92         return ResultCode::TIMEOUT;
93     }
94     IAM_LOGI("%{public}s get result %{public}d", GetDescription(), result_);
95     return result_;
96 }
97 
SetResult(ResultCode resultCode)98 void CustomCommand::SetResult(ResultCode resultCode)
99 {
100     result_ = resultCode;
101     promise_.set_value();
102 }
103 } // namespace UserAuth
104 } // namespace UserIam
105 } // namespace OHOS
106