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 "pin_auth_executor_callback_hdi.h"
17
18 #include <cinttypes>
19 #include <hdf_base.h>
20
21 #ifdef SENSORS_MISCDEVICE_ENABLE
22 #include "vibrator_agent.h"
23 #endif
24
25 #include "iam_logger.h"
26 #include "iam_common_defines.h"
27 #include "i_inputer_data_impl.h"
28 #include "inputer_get_data_proxy.h"
29 #include "pin_auth_hdi.h"
30
31 #define LOG_LABEL UserIam::Common::LABEL_PIN_AUTH_SA
32
33 namespace OHOS {
34 namespace UserIam {
35 namespace PinAuth {
36 namespace {
37 /* tip indicates that the password authentication result is returned */
38 constexpr int32_t TIP_AUTH_PASS_NOTICE = 1;
39 };
40
PinAuthExecutorCallbackHdi(std::shared_ptr<UserIam::UserAuth::IExecuteCallback> frameworkCallback,std::shared_ptr<PinAuthExecutorHdi> pinAuthExecutorHdi,uint32_t tokenId,bool isEnroll)41 PinAuthExecutorCallbackHdi::PinAuthExecutorCallbackHdi(std::shared_ptr<UserIam::UserAuth::IExecuteCallback>
42 frameworkCallback, std::shared_ptr<PinAuthExecutorHdi> pinAuthExecutorHdi, uint32_t tokenId, bool isEnroll)
43 : frameworkCallback_(frameworkCallback), pinAuthExecutorHdi_(pinAuthExecutorHdi),
44 tokenId_(tokenId), isEnroll_(isEnroll) {}
45
46 #ifdef SENSORS_MISCDEVICE_ENABLE
DoVibrator()47 void PinAuthExecutorCallbackHdi::DoVibrator()
48 {
49 IAM_LOGI("begin");
50 static const char *pinAuthEffect = "haptic.fail";
51 bool pinEffectState = false;
52 int32_t ret = Sensors::IsSupportEffect(pinAuthEffect, &pinEffectState);
53 if (ret != 0) {
54 IAM_LOGE("call IsSupportEffect fail %{public}d", ret);
55 return;
56 }
57 if (!pinEffectState) {
58 IAM_LOGE("effect not support");
59 return;
60 }
61 if (!Sensors::SetUsage(USAGE_PHYSICAL_FEEDBACK)) {
62 IAM_LOGE("call SetUsage fail");
63 return;
64 }
65 ret = Sensors::StartVibrator(pinAuthEffect);
66 if (ret != 0) {
67 IAM_LOGE("call StartVibrator fail %{public}d", ret);
68 return;
69 }
70 IAM_LOGI("end");
71 }
72 #endif
73
OnResult(int32_t code,const std::vector<uint8_t> & extraInfo)74 int32_t PinAuthExecutorCallbackHdi::OnResult(int32_t code, const std::vector<uint8_t>& extraInfo)
75 {
76 IAM_LOGI("OnResult %{public}d", code);
77 UserAuth::ResultCode retCode = ConvertResultCode(code);
78 #ifdef SENSORS_MISCDEVICE_ENABLE
79 if ((!isEnroll_) && (retCode == UserAuth::FAIL)) {
80 DoVibrator();
81 }
82 #else
83 IAM_LOGE("vibrator not support");
84 #endif
85
86 /* OnAcquireInfo api is used to return auth result in advance */
87 if ((!isEnroll_) && (retCode == UserAuth::SUCCESS)) {
88 IAM_LOGI("use OnAcquireInfo to return auth succ");
89 frameworkCallback_->OnAcquireInfo(TIP_AUTH_PASS_NOTICE, extraInfo);
90 }
91 frameworkCallback_->OnResult(retCode, extraInfo);
92 return HDF_SUCCESS;
93 }
94
OnGetData(uint64_t scheduleId,const std::vector<uint8_t> & salt,uint64_t authSubType)95 int32_t PinAuthExecutorCallbackHdi::OnGetData(uint64_t scheduleId, const std::vector<uint8_t> &salt,
96 uint64_t authSubType)
97 {
98 IAM_LOGI("Start tokenId_ is %{public}u", tokenId_);
99 if (OnGetDataV1_1(scheduleId, salt, authSubType, 0) != HDF_SUCCESS) {
100 IAM_LOGE("invoke OnGetDataV1_1 fail");
101 return HDF_FAILURE;
102 }
103 return HDF_SUCCESS;
104 }
105
OnGetDataV1_1(uint64_t scheduleId,const std::vector<uint8_t> & algoParameter,uint64_t authSubType,uint32_t algoVersion)106 int32_t PinAuthExecutorCallbackHdi::OnGetDataV1_1(uint64_t scheduleId, const std::vector<uint8_t> &algoParameter,
107 uint64_t authSubType, uint32_t algoVersion)
108 {
109 IAM_LOGI("Start tokenId_ is %{public}u", tokenId_);
110 sptr<InputerGetData> inputer = PinAuthManager::GetInstance().GetInputerLock(tokenId_);
111 if (inputer == nullptr) {
112 IAM_LOGE("inputer is nullptr");
113 return HDF_FAILURE;
114 }
115 sptr<IInputerDataImpl> iInputerDataImpl(new (std::nothrow) IInputerDataImpl(scheduleId, pinAuthExecutorHdi_));
116 if (iInputerDataImpl == nullptr) {
117 IAM_LOGE("iInputerDataImpl is nullptr");
118 }
119
120 inputer->OnGetData(authSubType, algoParameter, iInputerDataImpl, algoVersion, isEnroll_);
121 return HDF_SUCCESS;
122 }
123
ConvertResultCode(const int32_t in)124 UserAuth::ResultCode PinAuthExecutorCallbackHdi::ConvertResultCode(const int32_t in)
125 {
126 UserAuth::ResultCode hdiIn = static_cast<UserAuth::ResultCode>(in);
127 if (hdiIn < UserAuth::ResultCode::SUCCESS || hdiIn > UserAuth::ResultCode::LOCKED) {
128 IAM_LOGE("convert hdi undefined result code %{public}d to framework result code GENERAL_ERROR", hdiIn);
129 return UserAuth::ResultCode::GENERAL_ERROR;
130 }
131
132 IAM_LOGI("covert hdi result code %{public}d to framework result code", hdiIn);
133 return hdiIn;
134 }
135 } // PinAuth
136 } // UserIam
137 } // OHOS