1 /*
2  * Copyright (C) 2021 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 OHOS_WIFI_SERVICE_MANAGER_H
17 #define OHOS_WIFI_SERVICE_MANAGER_H
18 
19 #include <mutex>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "ista_service.h"
24 #include "iscan_service.h"
25 #ifdef FEATURE_AP_SUPPORT
26 #include "i_ap_service.h"
27 #endif
28 #ifdef FEATURE_P2P_SUPPORT
29 #include "ip2p_service.h"
30 #endif
31 #include "ienhance_service.h"
32 #ifdef FEATURE_SELF_CURE_SUPPORT
33 #include "iself_cure_service.h"
34 #endif
35 #ifdef FEATURE_WIFI_PRO_SUPPORT
36 #include "iwifi_pro_service.h"
37 #endif
38 
39 namespace OHOS {
40 namespace Wifi {
41 struct StaServiceHandle {
42     std::map<int, IStaService *> pService;
StaServiceHandleStaServiceHandle43     StaServiceHandle()
44     {}
~StaServiceHandleStaServiceHandle45     ~StaServiceHandle()
46     {}
ClearStaServiceHandle47     void Clear()
48     {
49         pService.clear();
50     }
51 };
52 
53 #ifdef FEATURE_WIFI_PRO_SUPPORT
54 struct WifiProServiceHandle {
55     std::map<int, IWifiProService *> pService;
WifiProServiceHandleWifiProServiceHandle56     WifiProServiceHandle()
57     {}
~WifiProServiceHandleWifiProServiceHandle58     ~WifiProServiceHandle()
59     {}
ClearWifiProServiceHandle60     void Clear()
61     {
62         pService.clear();
63     }
64 };
65 #endif
66 
67 #ifdef FEATURE_SELF_CURE_SUPPORT
68 struct SelfCureServiceHandle {
69     std::map<int, ISelfCureService *> pService;
SelfCureServiceHandleSelfCureServiceHandle70     SelfCureServiceHandle()
71     {}
~SelfCureServiceHandleSelfCureServiceHandle72     ~SelfCureServiceHandle()
73     {}
ClearSelfCureServiceHandle74     void Clear()
75     {
76         pService.clear();
77     }
78 };
79 #endif
80 
81 struct ScanServiceHandle {
82     std::map<int, IScanService *> pService;
ScanServiceHandleScanServiceHandle83     ScanServiceHandle()
84     {}
~ScanServiceHandleScanServiceHandle85     ~ScanServiceHandle()
86     {}
ClearScanServiceHandle87     void Clear()
88     {
89         pService.clear();
90     }
91 };
92 
93 #ifdef FEATURE_AP_SUPPORT
94 struct ApServiceHandle {
95     std::map<int, IApService *> pService;
ApServiceHandleApServiceHandle96     ApServiceHandle()
97     {}
~ApServiceHandleApServiceHandle98     ~ApServiceHandle()
99     {}
ClearApServiceHandle100     void Clear()
101     {
102         pService.clear();
103     }
104 };
105 #endif
106 
107 #ifdef FEATURE_P2P_SUPPORT
108 struct P2pServiceHandle {
109     IP2pService *pService;
P2pServiceHandleP2pServiceHandle110     P2pServiceHandle() : pService(nullptr)
111     {}
~P2pServiceHandleP2pServiceHandle112     ~P2pServiceHandle()
113     {}
ClearP2pServiceHandle114     void Clear()
115     {
116         pService = nullptr;
117     }
118 };
119 #endif
120 struct EnhanceServiceHandle {
121     void *handle;
122     IEnhanceService *(*create)();
123     void *(*destroy)(IEnhanceService *);
124     IEnhanceService *pService;
EnhanceServiceHandleEnhanceServiceHandle125     EnhanceServiceHandle() : handle(nullptr), create(nullptr), destroy(nullptr), pService(nullptr)
126     {}
~EnhanceServiceHandleEnhanceServiceHandle127     ~EnhanceServiceHandle()
128     {}
ClearEnhanceServiceHandle129     void Clear()
130     {
131         handle = nullptr;
132         create = nullptr;
133         destroy = nullptr;
134         pService = nullptr;
135     }
136 };
137 class WifiServiceManager {
138 public:
139     WifiServiceManager();
140     ~WifiServiceManager();
141 
142     /**
143      * @Description Initialize the mapping between feature service names and SO paths
144      *
145      * @return int - init result, when 0 means success, other means some fails happened
146      */
147     int Init();
148 
149     /**
150      * @Description Check preload config, maybe need preload feature service
151      *
152      * @return int - 0 need preload; other no need preload
153      */
154     int CheckPreLoadService(void);
155 
156     /**
157      * @Description Check the feature service. If the service is not loaded, continue to load the service
158      *
159      * @param name - feature service name
160      * @param bCreate - whether create the service instance
161      * @return int - 0 success; -1 feature service name not correct or load service failed
162      */
163     int CheckAndEnforceService(const std::string &name, int instId = 0, bool bCreate = true);
164 
165     /**
166      * @Description Get the Sta Service Inst object
167      *
168      * @return IStaService* - sta service pointer, if sta not supported, nullptr is returned
169      */
170     IStaService *GetStaServiceInst(int instId = 0);
171 
172 #ifdef FEATURE_SELF_CURE_SUPPORT
173     /**
174      * @Description Get the SelfCure Service Inst object
175      *
176      * @return ISelfCureService* - self cure service pointer, if self cure not supported, nullptr is returned
177      */
178     ISelfCureService *GetSelfCureServiceInst(int instId = 0);
179 #endif
180 
181 #ifdef FEATURE_WIFI_PRO_SUPPORT
182     /**
183      * @Description Get the WifiPro Service Inst object
184      *
185      * @return IWifiProService* - wifi pro service pointer, if wifi pro not supported, nullptr is returned
186      */
187     IWifiProService *GetWifiProServiceInst(int32_t instId);
188 #endif
189 
190     /**
191      * @Description Get the Scan Service Inst object
192      *
193      * @return IScanService* - scan service pointer, if scan not supported, nullptr is returned
194      */
195     IScanService *GetScanServiceInst(int instId = 0);
196 
197 #ifdef FEATURE_AP_SUPPORT
198     /**
199      * @Description set hotspots config
200      *
201      * @return true false
202      */
203     bool ApServiceSetHotspotConfig(const HotspotConfig &config, int id);
204     /**
205      * @Description Get the Ap Service Inst object
206      *
207      * @return IApService* - ap service pointer, if ap not supported, nullptr is returned
208      */
209     IApService *GetApServiceInst(int id = 0);
210 #endif
211 
212 #ifdef FEATURE_P2P_SUPPORT
213     /**
214      * @Description Get the P2P Service Inst object
215      *
216      * @return IP2pService* - p2p service pointer, if p2p not supported, nullptr is returned
217      */
218     IP2pService *GetP2pServiceInst(void);
219 #endif
220     /**
221      * @Description Get the Enhance Service Inst object
222      *
223      * @return IEnhanceService* - Enhance service pointer, if Enhance not supported, nullptr is returned
224      */
225     IEnhanceService *GetEnhanceServiceInst(void);
226     /**
227      * @Description unload a feature service
228      *
229      * @param name - feature service name
230      * @return int - 0 success
231      */
232     int UnloadService(const std::string &name, int id = 0);
233 
234     /**
235      * @Description Uninstall all loaded feature services
236      *
237      */
238     void UninstallAllService();
239     static WifiServiceManager &GetInstance();
240 
241 private:
242     int GetServiceDll(const std::string &name, std::string &dlname);
243     int LoadStaService(const std::string &dlname, int instId, bool bCreate);
244     int UnloadStaService(bool bPreLoad, int instId = 0);
245 #ifdef FEATURE_WIFI_PRO_SUPPORT
246     int32_t LoadWifiProService(bool bCreate, int32_t instId = 0);
247     int32_t UnloadWifiProService(bool bPreLoad, int32_t instId = 0);
248 #endif
249 #ifdef FEATURE_SELF_CURE_SUPPORT
250     int LoadSelfCureService(const std::string &dlname, bool bCreate);
251     int UnloadSelfCureService(bool bPreLoad, int instId = 0);
252 #endif
253     int LoadScanService(const std::string &dlname, bool bCreate);
254     int UnloadScanService(bool bPreLoad, int instId = 0);
255 #ifdef FEATURE_AP_SUPPORT
256     int LoadApService(const std::string &dlname, bool bCreate);
257     int UnloadApService(bool bPreLoad, int id = 0);
258 #endif
259 #ifdef FEATURE_P2P_SUPPORT
260     int LoadP2pService(const std::string &dlname, bool bCreate);
261     int UnloadP2pService(bool bPreLoad);
262 #endif
263     int LoadEnhanceService(const std::string &dlname, bool bCreate);
264     int UnloadEnhanceService(bool bPreLoad);
265 private:
266     std::mutex mStaMutex;
267     std::mutex mSelfCureMutex;
268     std::mutex mWifiProMutex;
269     std::mutex mScanMutex;
270     std::mutex mP2pMutex;
271     std::mutex mApMutex;
272     std::mutex mEnhanceMutex;
273     std::unordered_map<std::string, std::string> mServiceDllMap;
274     StaServiceHandle mStaServiceHandle;
275 #ifdef FEATURE_WIFI_PRO_SUPPORT
276     WifiProServiceHandle mWifiProServiceHandle;
277 #endif
278 #ifdef FEATURE_SELF_CURE_SUPPORT
279     SelfCureServiceHandle mSelfCureServiceHandle;
280 #endif
281     ScanServiceHandle mScanServiceHandle;
282 #ifdef FEATURE_AP_SUPPORT
283     ApServiceHandle mApServiceHandle;
284 #endif
285 #ifdef FEATURE_P2P_SUPPORT
286     P2pServiceHandle mP2pServiceHandle;
287 #endif
288     EnhanceServiceHandle mEnhanceServiceHandle;
289 };
290 } // namespace Wifi
291 } // namespace OHOS
292 #endif