1 /*
2  * Copyright (C) 2021-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 OHOS_WIFI_DEVICE_SERVICE_IMPL_H
17 #define OHOS_WIFI_DEVICE_SERVICE_IMPL_H
18 
19 #include <mutex>
20 #include "wifi_errcode.h"
21 #include "wifi_msg.h"
22 #include "i_wifi_device_callback.h"
23 #ifdef OHOS_ARCH_LITE
24 #include "wifi_device_stub_lite.h"
25 #else
26 #include "system_ability.h"
27 #include "wifi_device_stub.h"
28 #include "iremote_object.h"
29 #include "bundle_constants.h"
30 #endif
31 
32 namespace OHOS {
33 namespace Wifi {
34 #ifdef OHOS_ARCH_LITE
35 enum ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
36 #endif
37 
38 class WifiDeviceServiceImpl : public WifiDeviceStub {
39 public:
40     WifiDeviceServiceImpl();
41 #ifdef OHOS_ARCH_LITE
42     static std::shared_ptr<WifiDeviceServiceImpl> GetInstance();
43     void OnStart();
44     void OnStop();
45 #else
46     explicit WifiDeviceServiceImpl(int instId);
47     static ErrCode OnBackup(MessageParcel& data, MessageParcel& reply);
48     static ErrCode OnRestore(MessageParcel& data, MessageParcel& reply);
49 #endif
50     virtual ~WifiDeviceServiceImpl();
51 
52     ErrCode EnableWifi() override;
53 
54     ErrCode DisableWifi() override;
55 
56     ErrCode InitWifiProtect(const WifiProtectType &protectType, const std::string &protectName) override;
57 
58     ErrCode GetWifiProtectRef(const WifiProtectMode &protectMode, const std::string &protectName) override;
59 
60     ErrCode PutWifiProtectRef(const std::string &protectName) override;
61 
62     ErrCode IsHeldWifiProtectRef(const std::string &protectName, bool &isHoldProtect) override;
63 
64     ErrCode AddDeviceConfig(const WifiDeviceConfig &config, int &result, bool isCandidate) override;
65 
66     ErrCode UpdateDeviceConfig(const WifiDeviceConfig &config, int &result) override;
67 
68     ErrCode RemoveDevice(int networkId) override;
69 
70     ErrCode RemoveAllDevice() override;
71 
72     ErrCode SetTxPower(int power) override;
73 
74     ErrCode SetDpiMarkRule(const std::string &ifaceName, int uid, int protocol, int enable) override;
75 
76     ErrCode GetDeviceConfigs(std::vector<WifiDeviceConfig> &result, bool isCandidate) override;
77 
78     ErrCode EnableDeviceConfig(int networkId, bool attemptEnable) override;
79 
80     ErrCode DisableDeviceConfig(int networkId) override;
81 
82     ErrCode ConnectToNetwork(int networkId, bool isCandidate) override;
83 
84     ErrCode ConnectToDevice(const WifiDeviceConfig &config) override;
85 
86     ErrCode StartRoamToNetwork(const int networkId, const std::string bssid, const bool isCandidate) override;
87 
88     ErrCode StartConnectToUserSelectNetwork(int networkId, std::string bssid, bool isCandidate) override;
89 
90     ErrCode IsConnected(bool &isConnected) override;
91 
92     ErrCode ReConnect() override;
93 
94     ErrCode ReAssociate(void) override;
95 
96     ErrCode Disconnect(void) override;
97 
98     ErrCode StartWps(const WpsConfig &config) override;
99 
100     ErrCode CancelWps(void) override;
101 
102     ErrCode IsWifiActive(bool &bActive) override;
103 
104     ErrCode GetWifiState(int &state) override;
105 
106     ErrCode IsMeteredHotspot(bool &bMeteredHotspot) override;
107 
108     ErrCode GetLinkedInfo(WifiLinkedInfo &info) override;
109 
110     ErrCode GetDisconnectedReason(DisconnectedReason &reason) override;
111 
112     ErrCode GetIpInfo(IpInfo &info) override;
113 
114     ErrCode GetIpv6Info(IpV6Info &info) override;
115 
116     ErrCode SetCountryCode(const std::string &countryCode) override;
117 
118     ErrCode GetCountryCode(std::string &countryCode) override;
119 
120     ErrCode SetAppFrozen(std::set<int> pidList, bool isFrozen) override;
121 
122     ErrCode ResetAllFrozenApp() override;
123 
124     ErrCode DisableAutoJoin(const std::string &conditionName) override;
125 
126     ErrCode EnableAutoJoin(const std::string &conditionName) override;
127 
128     ErrCode RegisterAutoJoinCondition(const std::string &conditionName,
129                                       const std::function<bool()> &autoJoinCondition) override;
130 
131     ErrCode DeregisterAutoJoinCondition(const std::string &conditionName) override;
132 
133     ErrCode RegisterFilterBuilder(const FilterTag &filterTag, const std::string &builderName,
134                                   const FilterBuilder &filterBuilder) override;
135 
136     ErrCode DeregisterFilterBuilder(const FilterTag &filterTag, const std::string &builderName) override;
137 
138     ErrCode RegisterCommonBuilder(const TagType &tagType, const std::string &tagName,
139                                const CommonBuilder &commonBuilder) override;
140 
141     ErrCode DeregisterCommonBuilder(const TagType &tagType, const std::string &tagName) override;
142 
143 #ifdef OHOS_ARCH_LITE
144     ErrCode RegisterCallBack(const std::shared_ptr<IWifiDeviceCallBack> &callback,
145         const std::vector<std::string> &event) override;
146 #else
147     ErrCode RegisterCallBack(const sptr<IWifiDeviceCallBack> &callback, const std::vector<std::string> &event) override;
148 #endif
149 
150     ErrCode GetSignalLevel(const int &rssi, const int &band, int &level) override;
151 
152     ErrCode GetSupportedFeatures(long &features) override;
153 
154     ErrCode GetDeviceMacAddress(std::string &result) override;
155 
156     bool SetLowLatencyMode(bool enabled) override;
157 
158     ErrCode RemoveCandidateConfig(int networkId) override;
159 
160     ErrCode RemoveCandidateConfig(const WifiDeviceConfig &config) override;
161 
162     bool IsRemoteDied(void) override;
163 
164     ErrCode IsBandTypeSupported(int bandType, bool &supported) override;
165 
166     ErrCode Get5GHzChannelList(std::vector<int> &result) override;
167 
168     ErrCode StartPortalCertification() override;
169 
170     static void SaBasicDump(std::string& result);
171 
172     ErrCode GetChangeDeviceConfig(ConfigChange& value, WifiDeviceConfig &config) override;
173 
174     ErrCode FactoryReset() override;
175 
176     ErrCode LimitSpeed(const int controlId, const int limitMode) override;
177 
178     ErrCode SetLowTxPower(const WifiLowPowerParam wifiLowPowerParam) override;
179 
180     ErrCode EnableHiLinkHandshake(bool uiFlag, std::string &bssid, WifiDeviceConfig &deviceConfig) override;
181 
182     ErrCode EnableSemiWifi() override;
183 
184     ErrCode GetWifiDetailState(WifiDetailState &state) override;
185 
186     ErrCode SetSatelliteState(const int state) override;
187 
188     ErrCode GetDeviceConfig(const int &networkId, WifiDeviceConfig &config) override;
189 private:
190     bool Init();
191     ErrCode CheckCanEnableWifi(void);
192     bool IsStaServiceRunning();
193     bool IsScanServiceRunning();
194     bool CheckConfigEap(const WifiDeviceConfig &config);
195     bool CheckConfigPwd(const WifiDeviceConfig &config);
196     bool CheckConfigWapi(const WifiDeviceConfig &config);
197     ErrCode CheckCallingUid(int &uid);
198     bool IsWifiBrokerProcess(int uid);
199     ErrCode CheckRemoveCandidateConfig(void);
200     void SetWifiConnectedMode(void);
201     ErrCode HilinkGetMacAddress(WifiDeviceConfig &deviceConfig, std::string &currentMac);
202 #ifndef OHOS_ARCH_LITE
203     bool InitWifiBrokerProcessInfo(const WifiDeviceConfig &config);
204 #endif
205     void ReplaceConfigWhenCandidateConnected(std::vector<WifiDeviceConfig> &result);
206     void updateStaDeviceMacAddress(WifiDeviceConfig &config);
207     int ProcessPermissionVerify(const std::string &appId, const std::string &packageName);
208 private:
209     static constexpr int MAX_PRESHAREDKEY_LEN = 63;
210     static constexpr int MAX_HEX_LEN = 64;
211     static constexpr int MIN_PSK_LEN = 8;
212     static constexpr int MIN_SAE_LEN = 1;
213     static constexpr int WEP_KEY_LEN1 = 5;
214     static constexpr int WEP_KEY_LEN2 = 13;
215     static constexpr int WEP_KEY_LEN3 = 16;
216 
217 #ifdef OHOS_ARCH_LITE
218     static std::mutex g_instanceLock;
219     static std::shared_ptr<WifiDeviceServiceImpl> g_instance;
220     ServiceRunningState mState;
221 #endif
222 };
223 }  // namespace Wifi
224 }  // namespace OHOS
225 #endif