1 /*
2 * Copyright (c) 2022-2023 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 "fingerprint_auth_executor_callback_hdi.h"
17
18 #include <cstdint>
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <utility>
23 #include <vector>
24
25 #include "hdf_base.h"
26 #include "vibrator_agent.h"
27
28 #include "iam_check.h"
29 #include "iam_common_defines.h"
30 #include "iam_executor_iexecute_callback.h"
31 #include "iam_logger.h"
32
33 #include "fingerprint_auth_defines.h"
34 #include "fingerprint_auth_hdi.h"
35 #include "sa_command_manager.h"
36
37 #define LOG_TAG "FINGERPRINT_AUTH_SA"
38
39 namespace OHOS {
40 namespace UserIam {
41 namespace FingerprintAuth {
FingerprintAuthExecutorCallbackHdi(std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback,FingerCallbackHdiType fingerCallbackHdiType)42 FingerprintAuthExecutorCallbackHdi::FingerprintAuthExecutorCallbackHdi(
43 std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback, FingerCallbackHdiType fingerCallbackHdiType)
44 : frameworkCallback_(frameworkCallback),
45 fingerCallbackHdiType_(fingerCallbackHdiType)
46 {
47 }
48
DoVibrator()49 void FingerprintAuthExecutorCallbackHdi::DoVibrator()
50 {
51 IAM_LOGI("begin");
52 static const char *fingerAuthEffect = "haptic.fail";
53 bool fingerEffectState = false;
54 int32_t ret = Sensors::IsSupportEffect(fingerAuthEffect, &fingerEffectState);
55 if (ret != 0) {
56 IAM_LOGE("call IsSupportEffect fail %{public}d", ret);
57 return;
58 }
59 if (!fingerEffectState) {
60 IAM_LOGE("effect not support");
61 return;
62 }
63 if (!Sensors::SetUsage(USAGE_PHYSICAL_FEEDBACK)) {
64 IAM_LOGE("call SetUsage fail");
65 return;
66 }
67 ret = Sensors::StartVibrator(fingerAuthEffect);
68 if (ret != 0) {
69 IAM_LOGE("call StartVibrator fail %{public}d", ret);
70 return;
71 }
72 IAM_LOGI("end");
73 }
74
OnResult(int32_t result,const std::vector<uint8_t> & extraInfo)75 int32_t FingerprintAuthExecutorCallbackHdi::OnResult(int32_t result, const std::vector<uint8_t> &extraInfo)
76 {
77 IAM_LOGI("OnResult %{public}d", result);
78 UserAuth::ResultCode retCode = ConvertResultCode(result);
79 IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
80 if ((fingerCallbackHdiType_ == FINGER_CALLBACK_AUTH) && (retCode == UserAuth::FAIL)) {
81 DoVibrator();
82 }
83 frameworkCallback_->OnResult(retCode, extraInfo);
84 return HDF_SUCCESS;
85 }
86
OnTip(int32_t tip,const std::vector<uint8_t> & extraInfo)87 int32_t FingerprintAuthExecutorCallbackHdi::OnTip(int32_t tip, const std::vector<uint8_t> &extraInfo)
88 {
89 IAM_LOGI("OnTip %{public}d", tip);
90 IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
91 if (tip == FingerprintTipsCode::FINGERPRINT_AUTH_TIP_SINGLE_AUTH_RESULT) {
92 tip = UserAuth::USER_AUTH_TIP_SINGLE_AUTH_RESULT;
93 }
94 frameworkCallback_->OnAcquireInfo(tip, extraInfo);
95 return HDF_SUCCESS;
96 }
97
OnMessage(int32_t destRole,const std::vector<uint8_t> & msg)98 int32_t FingerprintAuthExecutorCallbackHdi::OnMessage(int32_t destRole, const std::vector<uint8_t> &msg)
99 {
100 IAM_LOGI("OnMessage destRole %{public}d msg len %{public}zu", destRole, msg.size());
101 IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
102 frameworkCallback_->OnMessage(destRole, msg);
103 return HDF_SUCCESS;
104 }
105
ConvertResultCode(const int32_t in)106 UserAuth::ResultCode FingerprintAuthExecutorCallbackHdi::ConvertResultCode(const int32_t in)
107 {
108 ResultCode hdiIn = static_cast<ResultCode>(in);
109 if (hdiIn > ResultCode::VENDOR_RESULT_CODE_BEGIN) {
110 IAM_LOGI("vendor hdi result code %{public}d, no covert", hdiIn);
111 return static_cast<UserAuth::ResultCode>(in);
112 }
113
114 static const std::map<ResultCode, UserAuth::ResultCode> data = {
115 { ResultCode::SUCCESS, UserAuth::ResultCode::SUCCESS },
116 { ResultCode::FAIL, UserAuth::ResultCode::FAIL },
117 { ResultCode::GENERAL_ERROR, UserAuth::ResultCode::GENERAL_ERROR },
118 { ResultCode::CANCELED, UserAuth::ResultCode::CANCELED },
119 { ResultCode::TIMEOUT, UserAuth::ResultCode::TIMEOUT },
120 { ResultCode::BUSY, UserAuth::ResultCode::BUSY },
121 { ResultCode::INVALID_PARAMETERS, UserAuth::ResultCode::INVALID_PARAMETERS },
122 { ResultCode::LOCKED, UserAuth::ResultCode::LOCKED },
123 { ResultCode::NOT_ENROLLED, UserAuth::ResultCode::NOT_ENROLLED },
124 // should be UserAuth::ResultCode::OPERATION_NOT_SUPPORT
125 { ResultCode::OPERATION_NOT_SUPPORT, UserAuth::ResultCode::FAIL },
126 };
127
128 UserAuth::ResultCode out;
129 auto iter = data.find(hdiIn);
130 if (iter == data.end()) {
131 out = UserAuth::ResultCode::GENERAL_ERROR;
132 IAM_LOGE("convert hdi undefined result code %{public}d to framework result code %{public}d", in, out);
133 return out;
134 }
135 out = iter->second;
136 IAM_LOGI("covert hdi result code %{public}d to framework result code %{public}d", hdiIn, out);
137 return out;
138 }
139 } // namespace FingerprintAuth
140 } // namespace UserIam
141 } // namespace OHOS
142