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
27 #include "iam_check.h"
28 #include "iam_executor_iexecute_callback.h"
29 #include "iam_logger.h"
30
31 #include "fingerprint_auth_defines.h"
32 #include "sa_command_manager.h"
33
34 #define LOG_LABEL UserIam::Common::LABEL_FINGERPRINT_AUTH_SA
35
36 namespace OHOS {
37 namespace UserIam {
38 namespace FingerprintAuth {
FingerprintAuthExecutorCallbackHdi(std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback)39 FingerprintAuthExecutorCallbackHdi::FingerprintAuthExecutorCallbackHdi(
40 std::shared_ptr<UserAuth::IExecuteCallback> frameworkCallback)
41 : frameworkCallback_(frameworkCallback)
42 {
43 }
44
OnResult(int32_t result,const std::vector<uint8_t> & extraInfo)45 int32_t FingerprintAuthExecutorCallbackHdi::OnResult(int32_t result, const std::vector<uint8_t> &extraInfo)
46 {
47 IAM_LOGI("OnResult %{public}d", result);
48 UserAuth::ResultCode retCode = ConvertResultCode(result);
49 IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
50 frameworkCallback_->OnResult(retCode, extraInfo);
51 return HDF_SUCCESS;
52 }
53
OnTip(int32_t tip,const std::vector<uint8_t> & extraInfo)54 int32_t FingerprintAuthExecutorCallbackHdi::OnTip(int32_t tip, const std::vector<uint8_t> &extraInfo)
55 {
56 IAM_LOGI("OnTip %{public}d", tip);
57 IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE);
58 frameworkCallback_->OnAcquireInfo(tip, extraInfo);
59 return HDF_SUCCESS;
60 }
61
ConvertResultCode(const int32_t in)62 UserAuth::ResultCode FingerprintAuthExecutorCallbackHdi::ConvertResultCode(const int32_t in)
63 {
64 ResultCode hdiIn = static_cast<ResultCode>(in);
65 if (hdiIn > ResultCode::VENDOR_RESULT_CODE_BEGIN) {
66 IAM_LOGI("vendor hdi result code %{public}d, no covert", hdiIn);
67 return static_cast<UserAuth::ResultCode>(in);
68 }
69
70 static const std::map<ResultCode, UserAuth::ResultCode> data = {
71 { ResultCode::SUCCESS, UserAuth::ResultCode::SUCCESS },
72 { ResultCode::FAIL, UserAuth::ResultCode::FAIL },
73 { ResultCode::GENERAL_ERROR, UserAuth::ResultCode::GENERAL_ERROR },
74 { ResultCode::CANCELED, UserAuth::ResultCode::CANCELED },
75 { ResultCode::TIMEOUT, UserAuth::ResultCode::TIMEOUT },
76 { ResultCode::BUSY, UserAuth::ResultCode::BUSY },
77 { ResultCode::INVALID_PARAMETERS, UserAuth::ResultCode::INVALID_PARAMETERS },
78 { ResultCode::LOCKED, UserAuth::ResultCode::LOCKED },
79 { ResultCode::NOT_ENROLLED, UserAuth::ResultCode::NOT_ENROLLED },
80 // should be UserAuth::ResultCode::OPERATION_NOT_SUPPORT
81 { ResultCode::OPERATION_NOT_SUPPORT, UserAuth::ResultCode::FAIL },
82 };
83
84 UserAuth::ResultCode out;
85 auto iter = data.find(hdiIn);
86 if (iter == data.end()) {
87 out = UserAuth::ResultCode::GENERAL_ERROR;
88 IAM_LOGE("convert hdi undefined result code %{public}d to framework result code %{public}d", in, out);
89 return out;
90 }
91 out = iter->second;
92 IAM_LOGI("covert hdi result code %{public}d to framework result code %{public}d", hdiIn, out);
93 return out;
94 }
95 } // namespace FingerprintAuth
96 } // namespace UserIam
97 } // namespace OHOS
98