1 /*
2  * Copyright (C) 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 #ifndef NCI_NATIVE_SELECTOR_H
17 #define NCI_NATIVE_SELECTOR_H
18 
19 #include <memory>
20 #include <string>
21 #include "inci_native_interface.h"
22 
23 namespace OHOS {
24 namespace NFC {
25 namespace NCI {
26 class NciNativeSelector {
27 public:
28     static NciNativeSelector &GetInstance();
29 
30     /**
31      * @brief Get the ptr of INciCeInterface
32      * @return thr ptr of INciCeInterface
33      */
34     std::shared_ptr<INciCeInterface> GetNciCeInterface();
35 
36     /**
37      * @brief Get the ptr of INciNfccInterface
38      * @return thr ptr of INciNfccInterface
39      */
40     std::shared_ptr<INciNfccInterface> GetNciNfccInterface();
41 
42     /**
43      * @brief Get the ptr of INciTagInterface
44      * @return thr ptr of INciTagInterface
45      */
46     std::shared_ptr<INciTagInterface> GetNciTagInterface();
47 
48 private:
49     class NciLibsLoader {
50     public:
51         explicit NciLibsLoader(const std::string &newInterfaceSymbol = "NewInterface",
52             const std::string &deleteInterfaceSymbol = "DeleteInterface");
53 
54         ~NciLibsLoader();
55 
56         NciLibsLoader(const NciLibsLoader &) = delete;
57         NciLibsLoader &operator=(NciLibsLoader &) = delete;
58 
59         /**
60          * @brief dlopen native so
61          * @param True if success, otherwise false.
62          */
63         bool LoadLib();
64 
65         /**
66          * @brief dlclose native so
67          * @param True if success, otherwise false.
68          */
69         bool CloseLib();
70 
71         /**
72          * @brief dlsym symbol
73          * @param The ptr of INciNativeInterface
74          */
75         std::shared_ptr<INciNativeInterface> NewInstance();
76 
77     private:
78         void *handle_{nullptr};
79         std::string libPath_;
80         std::string newInterfaceSymbol_;
81         std::string deleteInterfaceSymbol_;
82     };
83 
84     NciNativeSelector();
85     NciNativeSelector(const NciNativeSelector &) = delete;
86     NciNativeSelector &operator=(NciNativeSelector &) = delete;
87 
88     /**
89      * @brief Init natvie interface
90      */
91     void InitNativeInterface();
92 
93     /**
94      * @brief Get the ptr of INciNativeInterface
95      * @return thr ptr of INciNativeInterface
96      */
97     std::shared_ptr<INciNativeInterface> GetNciNativeInterface();
98 
99     std::shared_ptr<INciNativeInterface> nativeInterface_;
100     static inline std::unique_ptr<NciLibsLoader> loader_;
101 };
102 }  // namespace NCI
103 }  // namespace NFC
104 }  // namespace OHOS
105 
106 #endif