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_TAG "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(std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback,
42 FaceCallbackHdiType faceCallbackHdiType)
43 : frameworkCallback_(frameworkCallback),
44 faceCallbackHdiType_(faceCallbackHdiType)
45 {
46 }
47
DoVibrator()48 void FaceAuthExecutorCallbackHdi::DoVibrator()
49 {
50 IAM_LOGI("begin");
51 static const char *faceAuthEffect = "haptic.notice.fail";
52 bool faceEffectState = false;
53 int32_t ret = Sensors::IsSupportEffect(faceAuthEffect, &faceEffectState);
54 if (ret != 0) {
55 IAM_LOGE("call IsSupportEffect fail %{public}d", ret);
56 return;
57 }
58 if (!faceEffectState) {
59 IAM_LOGE("effect not support");
60 return;
61 }
62 if (!Sensors::SetUsage(USAGE_PHYSICAL_FEEDBACK)) {
63 IAM_LOGE("call SetUsage fail");
64 return;
65 }
66 ret = Sensors::StartVibrator(faceAuthEffect);
67 if (ret != 0) {
68 IAM_LOGE("call StartVibrator fail %{public}d", ret);
69 return;
70 }
71 IAM_LOGI("end");
72 }
73
OnResult(int32_t result,const std::vector<uint8_t> & extraInfo)74 int32_t FaceAuthExecutorCallbackHdi::OnResult(int32_t result, const std::vector<uint8_t> &extraInfo)
75 {
76 IAM_LOGI("OnResult %{public}d", result);
77 UserAuth::ResultCode retCode = ConvertResultCode(result);
78 IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
79 if ((faceCallbackHdiType_ == FACE_CALLBACK_AUTH) && (retCode == UserAuth::FAIL)) {
80 DoVibrator();
81 }
82 frameworkCallback_->OnResult(retCode, extraInfo);
83 return HDF_SUCCESS;
84 }
85
OnTip(int32_t tip,const std::vector<uint8_t> & extraInfo)86 int32_t FaceAuthExecutorCallbackHdi::OnTip(int32_t tip, const std::vector<uint8_t> &extraInfo)
87 {
88 IAM_LOGI("OnTip %{public}d", tip);
89 IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
90 frameworkCallback_->OnAcquireInfo(tip, extraInfo);
91 return HDF_SUCCESS;
92 }
93
OnMessage(int32_t destRole,const std::vector<uint8_t> & msg)94 int32_t FaceAuthExecutorCallbackHdi::OnMessage(int32_t destRole, const std::vector<uint8_t> &msg)
95 {
96 IAM_LOGI("OnMessage destRole %{public}d msg len %{public}zu", destRole, msg.size());
97 IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
98 frameworkCallback_->OnMessage(destRole, msg);
99 return HDF_SUCCESS;
100 }
101
ConvertResultCode(const int32_t in)102 UserAuth::ResultCode FaceAuthExecutorCallbackHdi::ConvertResultCode(const int32_t in)
103 {
104 ResultCode hdiIn = static_cast<ResultCode>(in);
105 if (hdiIn > ResultCode::VENDOR_RESULT_CODE_BEGIN) {
106 IAM_LOGI("vendor hdi result code %{public}d, no covert", hdiIn);
107 return static_cast<UserAuth::ResultCode>(in);
108 }
109
110 static const std::map<ResultCode, UserAuth::ResultCode> data = {
111 { ResultCode::SUCCESS, UserAuth::ResultCode::SUCCESS },
112 { ResultCode::FAIL, UserAuth::ResultCode::FAIL },
113 { ResultCode::GENERAL_ERROR, UserAuth::ResultCode::GENERAL_ERROR },
114 { ResultCode::CANCELED, UserAuth::ResultCode::CANCELED },
115 { ResultCode::TIMEOUT, UserAuth::ResultCode::TIMEOUT },
116 { ResultCode::BUSY, UserAuth::ResultCode::BUSY },
117 { ResultCode::INVALID_PARAMETERS, UserAuth::ResultCode::INVALID_PARAMETERS },
118 { ResultCode::LOCKED, UserAuth::ResultCode::LOCKED },
119 { ResultCode::NOT_ENROLLED, UserAuth::ResultCode::NOT_ENROLLED },
120 // should be UserAuth::ResultCode::OPERATION_NOT_SUPPORT
121 { ResultCode::OPERATION_NOT_SUPPORT, UserAuth::ResultCode::FAIL },
122 };
123
124 UserAuth::ResultCode out;
125 auto iter = data.find(hdiIn);
126 if (iter == data.end()) {
127 out = UserAuth::ResultCode::GENERAL_ERROR;
128 IAM_LOGE("convert hdi undefined result code %{public}d to framework result code %{public}d", in, out);
129 return out;
130 }
131 out = iter->second;
132 IAM_LOGI("covert hdi result code %{public}d to framework result code %{public}d", hdiIn, out);
133 return out;
134 }
135 } // namespace FaceAuth
136 } // namespace UserIam
137 } // namespace OHOS
138