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 "face_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_executor_iexecute_callback.h"
30 #include "iam_logger.h"
31
32 #include "face_auth_defines.h"
33 #include "sa_command_manager.h"
34
35 #define LOG_LABEL UserIam::Common::LABEL_FACE_AUTH_SA
36
37 namespace OHOS {
38 namespace UserIam {
39 namespace FaceAuth {
40
FaceAuthExecutorCallbackHdi(std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback,FaceCallbackHdiType faceCallbackHdiType)41 FaceAuthExecutorCallbackHdi::FaceAuthExecutorCallbackHdi(
42 std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback, FaceCallbackHdiType faceCallbackHdiType)
43 : frameworkCallback_(frameworkCallback), faceCallbackHdiType_(faceCallbackHdiType)
44 {
45 }
46
DoVibrator()47 void FaceAuthExecutorCallbackHdi::DoVibrator()
48 {
49 IAM_LOGI("begin");
50 static const char *faceAuthEffect = "haptic.fail";
51 bool faceEffectState = false;
52 int32_t ret = Sensors::IsSupportEffect(faceAuthEffect, &faceEffectState);
53 if (ret != 0) {
54 IAM_LOGE("call IsSupportEffect fail %{public}d", ret);
55 return;
56 }
57 if (!faceEffectState) {
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(faceAuthEffect);
66 if (ret != 0) {
67 IAM_LOGE("call StartVibrator fail %{public}d", ret);
68 return;
69 }
70 IAM_LOGI("end");
71 }
72
OnResult(int32_t result,const std::vector<uint8_t> & extraInfo)73 int32_t FaceAuthExecutorCallbackHdi::OnResult(int32_t result, const std::vector<uint8_t> &extraInfo)
74 {
75 IAM_LOGI("OnResult %{public}d", result);
76 UserAuth::ResultCode retCode = ConvertResultCode(result);
77 IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
78 if ((faceCallbackHdiType_ == FACE_CALLBACK_AUTH) && (retCode == UserAuth::FAIL)) {
79 DoVibrator();
80 }
81 frameworkCallback_->OnResult(retCode, extraInfo);
82 return HDF_SUCCESS;
83 }
84
OnTip(int32_t tip,const std::vector<uint8_t> & extraInfo)85 int32_t FaceAuthExecutorCallbackHdi::OnTip(int32_t tip, const std::vector<uint8_t> &extraInfo)
86 {
87 IAM_LOGI("OnTip %{public}d", tip);
88 IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
89 frameworkCallback_->OnAcquireInfo(tip, extraInfo);
90 return HDF_SUCCESS;
91 }
92
ConvertResultCode(const int32_t in)93 UserAuth::ResultCode FaceAuthExecutorCallbackHdi::ConvertResultCode(const int32_t in)
94 {
95 ResultCode hdiIn = static_cast<ResultCode>(in);
96 if (hdiIn > ResultCode::VENDOR_RESULT_CODE_BEGIN) {
97 IAM_LOGI("vendor hdi result code %{public}d, no covert", hdiIn);
98 return static_cast<UserAuth::ResultCode>(in);
99 }
100
101 static const std::map<ResultCode, UserAuth::ResultCode> data = {
102 { ResultCode::SUCCESS, UserAuth::ResultCode::SUCCESS },
103 { ResultCode::FAIL, UserAuth::ResultCode::FAIL },
104 { ResultCode::GENERAL_ERROR, UserAuth::ResultCode::GENERAL_ERROR },
105 { ResultCode::CANCELED, UserAuth::ResultCode::CANCELED },
106 { ResultCode::TIMEOUT, UserAuth::ResultCode::TIMEOUT },
107 { ResultCode::BUSY, UserAuth::ResultCode::BUSY },
108 { ResultCode::INVALID_PARAMETERS, UserAuth::ResultCode::INVALID_PARAMETERS },
109 { ResultCode::LOCKED, UserAuth::ResultCode::LOCKED },
110 { ResultCode::NOT_ENROLLED, UserAuth::ResultCode::NOT_ENROLLED },
111 // should be UserAuth::ResultCode::OPERATION_NOT_SUPPORT
112 { ResultCode::OPERATION_NOT_SUPPORT, UserAuth::ResultCode::FAIL },
113 };
114
115 UserAuth::ResultCode out;
116 auto iter = data.find(hdiIn);
117 if (iter == data.end()) {
118 out = UserAuth::ResultCode::GENERAL_ERROR;
119 IAM_LOGE("convert hdi undefined result code %{public}d to framework result code %{public}d", in, out);
120 return out;
121 }
122 out = iter->second;
123 IAM_LOGI("covert hdi result code %{public}d to framework result code %{public}d", hdiIn, out);
124 return out;
125 }
126 } // namespace FaceAuth
127 } // namespace UserIam
128 } // namespace OHOS
129