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 #ifndef DRIVER_MANAGER_H
17 #define DRIVER_MANAGER_H
18 
19 #include <cstdint>
20 #include <map>
21 
22 #include "iservstat_listener_hdi.h"
23 #include "singleton.h"
24 
25 #include "driver.h"
26 #include "iam_executor_iauth_driver_hdi.h"
27 #include "iam_executor_idriver_manager.h"
28 
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 using ServStatListenerStub = HDI::ServiceManager::V1_0::ServStatListenerStub;
33 using ServiceStatus = HDI::ServiceManager::V1_0::ServiceStatus;
34 class DriverManager : public Singleton<DriverManager> {
35 public:
36     DriverManager();
37     ~DriverManager() override = default;
38     int32_t Start(const std::map<std::string, HdiConfig> &hdiName2Config);
39     void OnFrameworkReady();
40     void OnAllHdiDisconnect();
41     void SubscribeHdiDriverStatus();
42     std::shared_ptr<Driver> GetDriverByServiceName(const std::string &serviceName);
43 
44 private:
45     class HdiServiceStatusListener;
46     bool HdiConfigIsValid(const std::map<std::string, HdiConfig> &hdiName2Config);
47     void SubscribeServiceStatus();
48     void SubscribeFrameworkReadyEvent();
49 
50     std::mutex mutex_;
51     std::map<std::string, std::shared_ptr<Driver>> serviceName2Driver_;
52     sptr<HdiServiceStatusListener> hdiServiceStatusListener_ {nullptr};
53 };
54 
55 class DriverManager::HdiServiceStatusListener : public ServStatListenerStub {
56 public:
57     using StatusCallback = std::function<void(const ServiceStatus &)>;
HdiServiceStatusListener(StatusCallback callback)58     explicit HdiServiceStatusListener(StatusCallback callback) : callback_(std::move(callback))
59     {
60     }
61     ~HdiServiceStatusListener() override = default;
OnReceive(const ServiceStatus & status)62     void OnReceive(const ServiceStatus &status) override
63     {
64         callback_(status);
65     }
66 
67 private:
68     StatusCallback callback_;
69 };
70 } // namespace UserAuth
71 } // namespace UserIam
72 } // namespace OHOS
73 
74 #endif // DRIVER_MANAGER_H