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 #include "wifi_library_utils.h" 26 #ifdef FEATURE_AP_SUPPORT 27 #include "i_ap_service.h" 28 #endif 29 #ifdef FEATURE_P2P_SUPPORT 30 #include "ip2p_service.h" 31 #endif 32 #include "ienhance_service.h" 33 #ifdef FEATURE_SELF_CURE_SUPPORT 34 #include "iself_cure_service.h" 35 #endif 36 #ifdef FEATURE_WIFI_PRO_SUPPORT 37 #include "iwifi_pro_service.h" 38 #endif 39 40 namespace OHOS { 41 namespace Wifi { 42 struct StaServiceHandle { 43 std::map<int, IStaService *> pService; StaServiceHandleStaServiceHandle44 StaServiceHandle() 45 {} ~StaServiceHandleStaServiceHandle46 ~StaServiceHandle() 47 {} ClearStaServiceHandle48 void Clear() 49 { 50 pService.clear(); 51 } 52 }; 53 54 #ifdef FEATURE_WIFI_PRO_SUPPORT 55 struct WifiProServiceHandle { 56 std::map<int, IWifiProService *> pService; WifiProServiceHandleWifiProServiceHandle57 WifiProServiceHandle() 58 {} ~WifiProServiceHandleWifiProServiceHandle59 ~WifiProServiceHandle() 60 {} ClearWifiProServiceHandle61 void Clear() 62 { 63 pService.clear(); 64 } 65 }; 66 #endif 67 68 #ifdef FEATURE_SELF_CURE_SUPPORT 69 struct SelfCureServiceHandle { 70 std::map<int, ISelfCureService *> pService; SelfCureServiceHandleSelfCureServiceHandle71 SelfCureServiceHandle() 72 {} ~SelfCureServiceHandleSelfCureServiceHandle73 ~SelfCureServiceHandle() 74 {} ClearSelfCureServiceHandle75 void Clear() 76 { 77 pService.clear(); 78 } 79 }; 80 #endif 81 82 struct ScanServiceHandle { 83 std::map<int, IScanService *> pService; ScanServiceHandleScanServiceHandle84 ScanServiceHandle() 85 {} ~ScanServiceHandleScanServiceHandle86 ~ScanServiceHandle() 87 {} ClearScanServiceHandle88 void Clear() 89 { 90 pService.clear(); 91 } 92 }; 93 94 #ifdef FEATURE_AP_SUPPORT 95 struct ApServiceHandle { 96 std::map<int, IApService *> pService; ApServiceHandleApServiceHandle97 ApServiceHandle() 98 {} ~ApServiceHandleApServiceHandle99 ~ApServiceHandle() 100 {} ClearApServiceHandle101 void Clear() 102 { 103 pService.clear(); 104 } 105 }; 106 #endif 107 108 #ifdef FEATURE_P2P_SUPPORT 109 struct P2pServiceHandle { 110 IP2pService *pService; P2pServiceHandleP2pServiceHandle111 P2pServiceHandle() : pService(nullptr) 112 {} ~P2pServiceHandleP2pServiceHandle113 ~P2pServiceHandle() 114 {} ClearP2pServiceHandle115 void Clear() 116 { 117 pService = nullptr; 118 } 119 }; 120 #endif 121 struct EnhanceServiceHandle { 122 void *handle; 123 IEnhanceService *(*create)(); 124 void *(*destroy)(IEnhanceService *); 125 IEnhanceService *pService; EnhanceServiceHandleEnhanceServiceHandle126 EnhanceServiceHandle() : handle(nullptr), create(nullptr), destroy(nullptr), pService(nullptr) 127 {} ~EnhanceServiceHandleEnhanceServiceHandle128 ~EnhanceServiceHandle() 129 {} ClearEnhanceServiceHandle130 void Clear() 131 { 132 handle = nullptr; 133 create = nullptr; 134 destroy = nullptr; 135 pService = nullptr; 136 } 137 }; 138 class WifiServiceManager { 139 public: 140 WifiServiceManager(); 141 ~WifiServiceManager(); 142 143 /** 144 * @Description Initialize the mapping between feature service names and SO paths 145 * 146 * @return int - init result, when 0 means success, other means some fails happened 147 */ 148 int Init(); 149 150 /** 151 * @Description Check preload config, maybe need preload feature service 152 * 153 * @return int - 0 need preload; other no need preload 154 */ 155 int CheckPreLoadService(void); 156 157 /** 158 * @Description Check the feature service. If the service is not loaded, continue to load the service 159 * 160 * @param name - feature service name 161 * @param bCreate - whether create the service instance 162 * @return int - 0 success; -1 feature service name not correct or load service failed 163 */ 164 int CheckAndEnforceService(const std::string &name, int instId = 0, bool bCreate = true); 165 166 /** 167 * @Description Get the Sta Service Inst object 168 * 169 * @return IStaService* - sta service pointer, if sta not supported, nullptr is returned 170 */ 171 IStaService *GetStaServiceInst(int instId = 0); 172 173 #ifdef FEATURE_SELF_CURE_SUPPORT 174 /** 175 * @Description Get the SelfCure Service Inst object 176 * 177 * @return ISelfCureService* - self cure service pointer, if self cure not supported, nullptr is returned 178 */ 179 ISelfCureService *GetSelfCureServiceInst(int instId = 0); 180 #endif 181 182 #ifdef FEATURE_WIFI_PRO_SUPPORT 183 /** 184 * @Description Get the WifiPro Service Inst object 185 * 186 * @return IWifiProService* - wifi pro service pointer, if wifi pro not supported, nullptr is returned 187 */ 188 IWifiProService *GetWifiProServiceInst(int32_t instId); 189 #endif 190 191 /** 192 * @Description Get the Scan Service Inst object 193 * 194 * @return IScanService* - scan service pointer, if scan not supported, nullptr is returned 195 */ 196 IScanService *GetScanServiceInst(int instId = 0); 197 198 #ifdef FEATURE_AP_SUPPORT 199 /** 200 * @Description set hotspots config 201 * 202 * @return true false 203 */ 204 bool ApServiceSetHotspotConfig(const HotspotConfig &config, int id); 205 /** 206 * @Description Get the Ap Service Inst object 207 * 208 * @return IApService* - ap service pointer, if ap not supported, nullptr is returned 209 */ 210 IApService *GetApServiceInst(int id = 0); 211 #endif 212 213 #ifdef FEATURE_P2P_SUPPORT 214 /** 215 * @Description Get the P2P Service Inst object 216 * 217 * @return IP2pService* - p2p service pointer, if p2p not supported, nullptr is returned 218 */ 219 IP2pService *GetP2pServiceInst(void); 220 #endif 221 /** 222 * @Description Get the Enhance Service Inst object 223 * 224 * @return IEnhanceService* - Enhance service pointer, if Enhance not supported, nullptr is returned 225 */ 226 IEnhanceService *GetEnhanceServiceInst(void); 227 /** 228 * @Description unload a feature service 229 * 230 * @param name - feature service name 231 * @return int - 0 success 232 */ 233 int UnloadService(const std::string &name, int id = 0); 234 235 /** 236 * @Description Uninstall all loaded feature services 237 * 238 */ 239 void UninstallAllService(); 240 static WifiServiceManager &GetInstance(); 241 242 private: 243 int GetServiceDll(const std::string &name, std::string &dlname); 244 int LoadStaService(const std::string &dlname, int instId, bool bCreate); 245 int UnloadStaService(bool bPreLoad, int instId = 0); 246 #ifdef FEATURE_WIFI_PRO_SUPPORT 247 int32_t LoadWifiProService(bool bCreate, int32_t instId = 0); 248 int32_t UnloadWifiProService(bool bPreLoad, int32_t instId = 0); 249 #endif 250 #ifdef FEATURE_SELF_CURE_SUPPORT 251 int LoadSelfCureService(const std::string &dlname, bool bCreate); 252 int UnloadSelfCureService(bool bPreLoad, int instId = 0); 253 #endif 254 int LoadScanService(const std::string &dlname, bool bCreate); 255 int UnloadScanService(bool bPreLoad, int instId = 0); 256 #ifdef FEATURE_AP_SUPPORT 257 int LoadApService(const std::string &dlname, bool bCreate); 258 int UnloadApService(bool bPreLoad, int id = 0); 259 #endif 260 #ifdef FEATURE_P2P_SUPPORT 261 int LoadP2pService(const std::string &dlname, bool bCreate); 262 int UnloadP2pService(bool bPreLoad); 263 #endif 264 int LoadEnhanceService(const std::string &dlname, bool bCreate); 265 int UnloadEnhanceService(bool bPreLoad); 266 private: 267 std::mutex mStaMutex; 268 std::mutex mSelfCureMutex; 269 std::mutex mWifiProMutex; 270 std::mutex mScanMutex; 271 std::mutex mP2pMutex; 272 std::mutex mApMutex; 273 std::mutex mEnhanceMutex; 274 std::unordered_map<std::string, std::string> mServiceDllMap; 275 StaServiceHandle mStaServiceHandle; 276 #ifdef FEATURE_WIFI_PRO_SUPPORT 277 WifiProServiceHandle mWifiProServiceHandle; 278 #endif 279 #ifdef FEATURE_SELF_CURE_SUPPORT 280 SelfCureServiceHandle mSelfCureServiceHandle; 281 #endif 282 ScanServiceHandle mScanServiceHandle; 283 #ifdef FEATURE_AP_SUPPORT 284 ApServiceHandle mApServiceHandle; 285 #endif 286 #ifdef FEATURE_P2P_SUPPORT 287 P2pServiceHandle mP2pServiceHandle; 288 #endif 289 EnhanceServiceHandle mEnhanceServiceHandle; 290 }; 291 292 #ifdef FEATURE_AP_SUPPORT 293 class WifiApServiceUtil { 294 public: WifiApServiceUtil()295 WifiApServiceUtil() : wifiLibraryUtils_ ("libwifi_ap_service.z.so", libApServiceHandle_, false) {} ~WifiApServiceUtil()296 ~WifiApServiceUtil() {} 297 IApService *CreateApInterface(int id); 298 void DestroyApInterface(IApService *apService); 299 private: 300 WifiLibraryUtils wifiLibraryUtils_; 301 static void* libApServiceHandle_; 302 }; 303 #endif 304 305 #ifdef FEATURE_P2P_SUPPORT 306 class WifiP2PServiceUtil { 307 public: WifiP2PServiceUtil()308 WifiP2PServiceUtil() : wifiLibraryUtils_ ("libwifi_p2p_service.z.so", libP2pServiceHandle_, false) {} ~WifiP2PServiceUtil()309 ~WifiP2PServiceUtil() {} 310 IP2pService *CreateP2pInterface(); 311 void DestroyP2pInterface(IP2pService *p2pService); 312 private: 313 WifiLibraryUtils wifiLibraryUtils_; 314 static void* libP2pServiceHandle_; 315 }; 316 #endif 317 } // namespace Wifi 318 } // namespace OHOS 319 #endif