• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OnFrameworkDown();
41     void OnAllHdiDisconnect();
42     void SubscribeHdiDriverStatus();
43     std::shared_ptr<Driver> GetDriverByServiceName(const std::string &serviceName);
44 
45 private:
46     class HdiServiceStatusListener;
47     bool HdiConfigIsValid(const std::map<std::string, HdiConfig> &hdiName2Config);
48     void SubscribeServiceStatus();
49     void SubscribeFrameworkReadyEvent();
50 
51     std::mutex mutex_;
52     std::map<std::string, std::shared_ptr<Driver>> serviceName2Driver_;
53     sptr<HdiServiceStatusListener> hdiServiceStatusListener_ {nullptr};
54 };
55 
56 class DriverManager::HdiServiceStatusListener : public ServStatListenerStub {
57 public:
58     using StatusCallback = std::function<void(const ServiceStatus &)>;
HdiServiceStatusListener(StatusCallback callback)59     explicit HdiServiceStatusListener(StatusCallback callback) : callback_(std::move(callback))
60     {
61     }
62     ~HdiServiceStatusListener() override = default;
OnReceive(const ServiceStatus & status)63     void OnReceive(const ServiceStatus &status) override
64     {
65         callback_(status);
66     }
67 
68 private:
69     StatusCallback callback_;
70 };
71 } // namespace UserAuth
72 } // namespace UserIam
73 } // namespace OHOS
74 
75 #endif // DRIVER_MANAGER_H