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_service.h"
17 
18 #include <cstdint>
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 
26 #include "iam_executor_idriver_manager.h"
27 #include "ipc_skeleton.h"
28 #include "iremote_object.h"
29 #include "iservice_registry.h"
30 #include "refbase.h"
31 #include "system_ability.h"
32 #include "system_ability_definition.h"
33 
34 #include "iam_check.h"
35 #include "iam_logger.h"
36 #include "iam_para2str.h"
37 #include "iam_ptr.h"
38 
39 #include "fingerprint_auth_defines.h"
40 #include "fingerprint_auth_driver_hdi.h"
41 #include "fingerprint_auth_interface_adapter.h"
42 #include "sensor_illumination_manager.h"
43 
44 #define LOG_TAG "FINGERPRINT_AUTH_SA"
45 
46 namespace OHOS {
47 namespace UserIam {
48 namespace FingerprintAuth {
49 namespace UserAuth = OHOS::UserIam::UserAuth;
50 using namespace OHOS::UserIam;
51 namespace {
52 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(FingerprintAuthService::GetInstance().get());
53 const uint16_t FINGERPRINT_AUTH_DEFAULT_HDI_ID = 1;
54 const auto FINGERPRINT_AUTH_DEFAULT_HDI_ADAPTER = Common::MakeShared<FingerprintAuthInterfaceAdapter>();
55 auto FINGERPRINT_AUTH_DEFAULT_HDI = Common::MakeShared<FingerprintAuthDriverHdi>(FINGERPRINT_AUTH_DEFAULT_HDI_ADAPTER);
56 // serviceName and HdiConfig.id must be globally unique
57 const std::map<std::string, UserAuth::HdiConfig> HDI_NAME_2_CONFIG = {
58     { "fingerprint_auth_interface_service", { FINGERPRINT_AUTH_DEFAULT_HDI_ID, FINGERPRINT_AUTH_DEFAULT_HDI } },
59 };
60 const std::vector<std::shared_ptr<FingerprintAuthDriverHdi>> FINGERPRINT_AUTH_DRIVER_HDIS = {
61     FINGERPRINT_AUTH_DEFAULT_HDI
62 };
63 // setup brightness manager
64 const auto FINGER_SENSOR_ILLUMINATION_MANAGER = SensorIlluminationManager::GetInstance();
65 } // namespace
66 std::mutex FingerprintAuthService::mutex_;
67 std::shared_ptr<FingerprintAuthService> FingerprintAuthService::instance_ = nullptr;
68 
FingerprintAuthService()69 FingerprintAuthService::FingerprintAuthService() : SystemAbility(SUBSYS_USERIAM_SYS_ABILITY_FINGERPRINTAUTH, true)
70 {
71 }
72 
GetInstance()73 std::shared_ptr<FingerprintAuthService> FingerprintAuthService::GetInstance()
74 {
75     if (instance_ == nullptr) {
76         std::lock_guard<std::mutex> guard(mutex_);
77         if (instance_ == nullptr) {
78             instance_ = Common::MakeShared<FingerprintAuthService>();
79             if (instance_ == nullptr) {
80                 IAM_LOGE("make share failed");
81             }
82         }
83     }
84     return instance_;
85 }
86 
OnStart()87 void FingerprintAuthService::OnStart()
88 {
89     IAM_LOGI("start");
90     StartDriverManager();
91     IAM_LOGI("success");
92 }
93 
OnStop()94 void FingerprintAuthService::OnStop()
95 {
96     IAM_LOGE("service is persistent, OnStop is not implemented");
97     return;
98 }
99 
StartDriverManager()100 void FingerprintAuthService::StartDriverManager()
101 {
102     IAM_LOGI("start");
103     int32_t ret = UserAuth::IDriverManager::Start(HDI_NAME_2_CONFIG);
104     if (ret != UserAuth::ResultCode::SUCCESS) {
105         IAM_LOGE("start driver manager failed");
106     }
107 }
108 } // namespace FingerprintAuth
109 } // namespace UserIam
110 } // namespace OHOS
111