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 #ifndef OHOS_WIFIMANAGER_H
16 #define OHOS_WIFIMANAGER_H
17 
18 #include <string>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include "define.h"
22 #include "wifi_internal_msg.h"
23 #include "wifi_errcode.h"
24 #include "wifi_sta_manager.h"
25 #include "wifi_scan_manager.h"
26 #include "wifi_toggler_manager.h"
27 #include "wifi_event_handler.h"
28 #ifdef FEATURE_AP_SUPPORT
29 #include "wifi_hotspot_manager.h"
30 #endif
31 #ifdef FEATURE_P2P_SUPPORT
32 #include "wifi_p2p_manager.h"
33 #endif
34 #ifndef OHOS_ARCH_LITE
35 #include "wifi_event_subscriber_manager.h"
36 #include "wifi_app_state_aware.h"
37 #include "wifi_multi_vap_manager.h"
38 #endif
39 
40 namespace OHOS {
41 namespace Wifi {
42 /* init state */
43 enum InitStatus {
44     INIT_UNKNOWN = -1,
45     INIT_OK = 0,
46     CONFIG_CENTER_INIT_FAILED = 1,
47     AUTH_CENTER_INIT_FAILED = 2,
48     SERVICE_MANAGER_INIT_FAILED = 3,
49     EVENT_BROADCAST_INIT_FAILED = 4,
50     TASK_THREAD_INIT_FAILED = 5,
51     WIFI_COUNTRY_CODE_MANAGER_INIT_FAILED = 6,
52 };
53 
54 enum class WifiCloseServiceCode {
55     STA_SERVICE_CLOSE,
56     SCAN_SERVICE_CLOSE,
57     AP_SERVICE_CLOSE,
58     P2P_SERVICE_CLOSE,
59     SERVICE_THREAD_EXIT,
60     STA_MSG_OPENED,
61     STA_MSG_STOPED,
62 };
63 
64 struct WifiCloseServiceMsg {
65     WifiCloseServiceCode code;
66     int instId;
67 };
68 
69 constexpr uint32_t PROP_SUPPORT_SAPCOEXIST_LEN = 10;
70 const std::string SUPPORT_SAPCOEXIST_PROP = "const.wifi.support_sapcoexist";
71 const std::string SUPPORT_SAPCOEXIST = "true";
72 constexpr uint32_t SUPPORT_SAPCOEXIST_LEN = 7;
73 
74 class WifiManager {
75 public:
76     static WifiManager &GetInstance();
77     ~WifiManager();
78     /**
79      * @Description Initialize submodules and message processing threads.
80      *              1. Initializing the Configuration Center
81      *              2. Initialization permission management
82      *              3. Initializing Service Management
83      *              4. Initialization event broadcast
84      *              5. Initializing a Message Queue
85      *              6. Initialize the message processing thread
86      *
87      * @return int - Init result, when 0 means success, other means some fails happened.
88      */
89     int Init();
90 
91     /**
92      * @Description When exiting, the system exits each submodule and then exits the message processing thread.
93      *              1. Uninstall each feature service
94      *              2. Exit the event broadcast module
95      *              3. Wait for the message processing thread to exit
96      *
97      */
98     void Exit();
99 
100     /**
101      * @Description Get supported features
102      *
103      * @param features - output supported features
104      * @return int - operation result
105      */
106     int GetSupportedFeatures(long &features) const;
107 
108     /**
109      * @Description Add supported feature
110      *
111      * @param feature
112      */
113     void AddSupportedFeatures(WifiFeatures feature);
114     void PushServiceCloseMsg(WifiCloseServiceCode code, int instId = 0);
115     void AutoStartEnhanceService(void);
116     std::unique_ptr<WifiStaManager>& GetWifiStaManager();
117     std::unique_ptr<WifiScanManager>& GetWifiScanManager();
118     std::unique_ptr<WifiTogglerManager>& GetWifiTogglerManager();
119 #ifdef FEATURE_AP_SUPPORT
120     std::unique_ptr<WifiHotspotManager>& GetWifiHotspotManager();
121 #endif
122 #ifdef FEATURE_P2P_SUPPORT
123     std::unique_ptr<WifiP2pManager>& GetWifiP2pManager();
124 #endif
125 #ifndef OHOS_ARCH_LITE
126     std::unique_ptr<WifiEventSubscriberManager>& GetWifiEventSubscriberManager();
127     std::unique_ptr<WifiMultiVapManager>& GetWifiMultiVapManager();
128 #endif
129 #ifdef FEATURE_HPF_SUPPORT
130     void InstallPacketFilterProgram(int screenState, int instId);
131 #endif
132     void OnNativeProcessStatusChange(int status);
133 
134 private:
135     WifiManager();
136     void DealCloseServiceMsg();
137     void CheckAndStartSta();
138     void AutoStartServiceThread();
139     void InitPidfile(void);
140     void CheckSapcoExist(void);
141 private:
142     std::mutex initStatusMutex;
143     InitStatus mInitStatus;
144     long mSupportedFeatures;
145     bool g_supportsapcoexistflag;
146     std::unique_ptr<WifiEventHandler> mCloseServiceThread = nullptr;
147     std::unique_ptr<WifiEventHandler> mStartServiceThread = nullptr;
148     std::unique_ptr<WifiStaManager> wifiStaManager = nullptr;
149     std::unique_ptr<WifiScanManager> wifiScanManager = nullptr;
150     std::unique_ptr<WifiTogglerManager> wifiTogglerManager = nullptr;
151 #ifdef FEATURE_AP_SUPPORT
152     std::unique_ptr<WifiHotspotManager> wifiHotspotManager = nullptr;
153 #endif
154 #ifdef FEATURE_P2P_SUPPORT
155     std::unique_ptr<WifiP2pManager> wifiP2pManager = nullptr;
156 #endif
157 #ifndef OHOS_ARCH_LITE
158     std::unique_ptr<WifiEventSubscriberManager> wifiEventSubscriberManager = nullptr;
159     std::unique_ptr<WifiMultiVapManager> wifiMultiVapManager = nullptr;
160 #endif
161 };
162 } // namespace Wifi
163 } // namespace OHOS
164 #endif