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_H
17 #define OHOS_WIFI_SERVICE_H
18 
19 #include <codecvt>
20 #include "wifi_internal_msg.h"
21 #include "sta_auto_connect_service.h"
22 #include "sta_monitor.h"
23 #include "sta_state_machine.h"
24 #include "network_selection.h"
25 #ifndef OHOS_ARCH_LITE
26 #include "i_wifi_country_code_change_listener.h"
27 #include "sta_app_acceleration.h"
28 #endif
29 
30 namespace OHOS {
31 namespace Wifi {
32 class StaService {
33     FRIEND_GTEST(StaService);
34 public:
35     explicit StaService(int instId = 0);
36     virtual ~StaService();
37     /**
38      * @Description  Initialize StaService module.
39      *
40      * @param callbacks - sta service callback
41      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
42      */
43     virtual ErrCode InitStaService(const std::vector<StaServiceCallback> &callbacks);
44     /**
45      * @Description  Enable wifi
46      *
47      * @Output: Return operating results to Interface Service after enable wifi
48                successfully through callback function instead of returning
49                result immediately.
50      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
51      */
52     virtual ErrCode EnableStaService();
53     /**
54      * @Description  Disable wifi
55      *
56      * @Output: Return operating results to Interface Service after enable wifi
57                 successfully through callback function instead of returning
58                 result immediately.
59      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
60      */
61     virtual ErrCode DisableStaService() const;
62     /**
63      * @Description  Connect to a new network
64      *
65      * @param config - the configuration of network which is going to connect.(in)
66      * @Output: Return operating results to Interface Service after enable wifi
67                 successfully through callback function instead of returning
68                 result immediately.
69      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
70      */
71     virtual ErrCode ConnectToDevice(const WifiDeviceConfig &config) const;
72     /**
73      * @Description  Connecting to a specified network.
74      *
75      * @param networkId - interior saved network index.(in)
76      * @param type - select network type: SelectedType
77      * @Output: Return operating results to Interface Service after enable wifi
78                 successfully through callback function instead of returning
79                 result immediately.
80      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
81      */
82     virtual ErrCode ConnectToNetwork(int networkId, int type = NETWORK_SELECTED_BY_USER) const;
83 
84     /**
85      * @Description roam to target bssid
86      *
87      * @param networkId - target networkId
88      * @param bssid - target bssid
89      * @return ErrCode - operation result
90      */
91     virtual ErrCode StartRoamToNetwork(const int networkId, const std::string bssid) const;
92 
93     /**
94      * @Description connect to user select ssid and bssid network
95      *
96      * @param networkId - target networkId
97      * @param bssid - target bssid
98      * @return ErrCode - operation result
99      */
100     virtual ErrCode StartConnectToUserSelectNetwork(int networkId, std::string bssid) const;
101 
102     /**
103      * @Description  Disconnect to the network
104      *
105      * @Output: Return operating results to Interface Service after enable wifi
106                 successfully through callback function instead of returning
107                 result immediately.
108      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
109      */
110     virtual ErrCode Disconnect() const;
111     /**
112      * @Description  ReAssociate network
113      *
114      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
115      */
116     virtual ErrCode ReAssociate() const;
117     /**
118      * @Description Add a specified candidate hotspot configuration.
119      *
120      * @param uid - call app uid
121      * @param config - WifiDeviceConfig object
122      * @param netWorkId - the network id of the hotspot configuration.(out)
123      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
124      */
125     virtual ErrCode AddCandidateConfig(const int uid, const WifiDeviceConfig &config, int& netWorkId) const;
126     /**
127      * @Description Connect to a candidate specified network.
128      *
129      * @param uid - call app uid
130      * @param networkId - the candidate device network id
131      * @Return ErrCode - operation result
132      */
133     virtual ErrCode ConnectToCandidateConfig(const int uid, const int networkId) const;
134     /**
135      * @Description Remove the wifi candidate device config equals to input network id
136      *
137      * @param uid - call app uid
138      * @param networkId - the candidate device network id
139      * @return ErrCode - operation result
140      */
141     virtual ErrCode RemoveCandidateConfig(const int uid, const int networkId) const;
142     /**
143      * @Description Remove all the wifi candidate device config equals to input uid
144      *
145      * @param uid - call app uid
146      * @return ErrCode - operation result
147      */
148     virtual ErrCode RemoveAllCandidateConfig(const int uid) const;
149     /**
150      * @Description  Update a network to config
151      *
152      * @param config -The Network info(in)
153      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
154      */
155     virtual int AddDeviceConfig(const WifiDeviceConfig &config) const;
156     /**
157      * @Description Update a network to config.
158      *
159      * @param config -The Network info(in)
160      * @Return success: networkId  fail: -1
161      */
162     virtual int UpdateDeviceConfig(const WifiDeviceConfig &config) const;
163     /**
164      * @Description  Remove network config.
165      *
166      * @param networkId -The NetworkId is going to be removed.(in)
167      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
168      */
169     virtual ErrCode RemoveDevice(int networkId) const;
170     /**
171      * @Description  Remove all network configs.
172      *
173      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
174      */
175     virtual ErrCode RemoveAllDevice() const;
176     /**
177      * @Description  Enable WI-FI device Configuration.
178      *
179      * @param networkId - The NetworkId (in)
180      * @param networkId - if set true, disable other device config (in)
181      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
182      */
183     virtual ErrCode EnableDeviceConfig(int networkId, bool attemptEnable) const;
184     /**
185      * @Description Disable WI-FI device Configuration.
186      *
187      * @param networkId - device Configuration's network id
188      * @return ErrCode - success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
189      */
190     virtual ErrCode DisableDeviceConfig(int networkId) const;
191     /**
192      * @Description  Start WPS Connection
193      *
194      * @Output: Return operating results to Interface Service after enable wifi
195                 successfully through callback function instead of returning
196                 result immediately.
197      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
198      */
199     virtual ErrCode StartWps(const WpsConfig &config) const;
200     /**
201      * @Description  Close WPS Connection
202      *
203      * @Output: Return operating results to Interface Service after enable wifi
204                 successfully through callback function instead of returning
205                 result immediately.
206      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
207      */
208     virtual ErrCode CancelWps() const;
209 
210     /**
211      * @Description  ConnectivityManager process scan results.
212      *
213      * @Output: Return operating results to Interface Service after enable wifi
214                 successfully through callback function instead of returning
215                 result immediately.
216      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
217      */
218     virtual ErrCode AutoConnectService(const std::vector<InterScanInfo> &scanInfos);
219     /**
220      * @Description Register sta callback function
221      *
222      * @param callbacks - Callback function pointer storage structure
223      */
224     virtual void RegisterStaServiceCallback(const std::vector<StaServiceCallback> &callbacks) const;
225 
226     /**
227      * @Description Register sta callback function
228      *
229      * @param callbacks - Callback function pointer storage structure
230      */
231     virtual void UnRegisterStaServiceCallback(const StaServiceCallback &callbacks) const;
232 
233     /**
234      * @Description  Reconnect network
235      *
236      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
237      */
238     virtual ErrCode ReConnect() const;
239 
240     /**
241      * @Description  Set suspend mode to wpa
242      *
243      * @param mode - true for suspend mode, false for resume mode
244      *
245      * @Return success: WIFI_OPT_SUCCESS, fail: WIFI_OPT_FAILED
246      */
247     virtual ErrCode SetSuspendMode(bool mode) const;
248 
249     /**
250      * @Description  Set power mode to wpa
251      *
252      * @param mode - true for power mode, false for resume mode
253      *
254      * @Return success: WIFI_OPT_SUCCESS, fail: WIFI_OPT_FAILED
255      */
256     virtual ErrCode SetPowerMode(bool mode) const;
257 
258     /**
259      * @Description systemabilitychanged
260      *
261      * @param mode: true for setup, false for shutdown.
262      * @return WifiErrorNo
263      */
264     virtual ErrCode OnSystemAbilityChanged(int systemAbilityid, bool add);
265     /**
266      * @Description Screen State (On/Off) Change Handler
267      *
268      */
269     virtual void HandleScreenStatusChanged(int screenState);
270 
271     /**
272      * @Description  disable auto join.
273      *
274      * @param conditionName autoJoinDisabled condition.
275      * @return WifiErrorNo
276      */
277     virtual ErrCode DisableAutoJoin(const std::string &conditionName);
278 
279     /**
280      * @Description  enable auto join.
281      *
282      * @param conditionName autoJoinDisabled condition.
283      * @return WifiErrorNo
284      */
285     virtual ErrCode EnableAutoJoin(const std::string &conditionName);
286 
287     /**
288      * @Description  register auto join condition.
289      *
290      * @param conditionName the name of condition.
291      * @param autoJoinCondition condition.
292      * @return WifiErrorNo
293      */
294     virtual ErrCode RegisterAutoJoinCondition(const std::string &conditionName,
295                                               const std::function<bool()> &autoJoinCondition);
296     /**
297      * @Description  deregister auto join condition.
298      *
299      * @param conditionName the name of condition.
300      * @return WifiErrorNo
301      */
302     virtual ErrCode DeregisterAutoJoinCondition(const std::string &conditionName);
303 
304     /**
305      * @Description  register external filter builder.
306      *
307      * @param filterTag filterTag which define where the filter should be inserted.
308      * @param filterName the name of the filter to build.
309      * @param filterBuilder filter builder.
310      * @return WifiErrorNo
311      */
312     virtual ErrCode RegisterFilterBuilder(const FilterTag &filterTag, const std::string &filterName,
313                                           const FilterBuilder &filterBuilder);
314     /**
315      * @Description  deregister external filter builder.
316      *
317      * @param filterTag filterTag which define where the filter should be inserted.
318      * @param filterName the name of the filter to build.
319      * @return WifiErrorNo
320      */
321     virtual ErrCode DeregisterFilterBuilder(const FilterTag &filterTag, const std::string &filterName);
322 
323     /**
324      * Register the common builder function
325      *
326      * @param TagType scoreTag which define where the score or filter should be inserted.
327      * @param tagName the score or filter name.
328      * @param CommonBuilder CommonBuilder function.
329      */
330     virtual ErrCode RegisterCommonBuilder(const TagType &tagType, const std::string &tagName,
331                                const CommonBuilder &commonBuilder);
332 
333     /**
334      * Deregister the common builder function
335      *
336      * @param TagType TagType which define where the score or filter should be inserted.
337      * @param tagName the score or filte name.
338      */
339     virtual ErrCode DeregisterCommonBuilder(const TagType &tagType, const std::string &tagName);
340 
341     /**
342      * @Description start portal certification.
343      *
344      * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED
345      */
346     virtual ErrCode StartPortalCertification();
347 
348     /**
349      * @Description Handle foreground app changed action.
350      *
351      * @param bundleName app name.
352      * @param uid app uid.
353      * @param pid app pid.
354      * @param state app state.
355      * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED
356      */
357 #ifndef OHOS_ARCH_LITE
358     virtual ErrCode HandleForegroundAppChangedAction(const AppExecFwk::AppStateData &appStateData);
359 
360     /**
361      * @Description Set EnhanceService
362      *
363      * @param enhanceService IEnhanceService object
364      * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED
365      */
366     virtual ErrCode SetEnhanceService(IEnhanceService* enhanceService);
367 #endif
368     /**
369      * @Description enable hilink
370      *
371      * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED
372      */
373     virtual ErrCode EnableHiLinkHandshake(const WifiDeviceConfig &config, const std::string &cmd);
374 
375     /**
376      * @Description deliver mac
377      *
378      * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED
379      */
380     virtual ErrCode DeliverStaIfaceData(const std::string &currentMac);
381 private:
382     void NotifyDeviceConfigChange(ConfigChange value) const;
383     int FindDeviceConfig(const WifiDeviceConfig &config, WifiDeviceConfig &outConfig) const;
384     std::string ConvertString(const std::u16string &wideText) const;
385     int32_t GetDataSlotId(int32_t slotId) const;
386     std::string GetImsi(int32_t slotId) const;
387     std::string GetPlmn(int32_t slotId) const;
388     std::string GetMcc(const std::string &imsi) const;
389     std::string GetMnc(const std::string &imsi, const int mncLen) const;
390     void UpdateEapConfig(const WifiDeviceConfig &config, WifiEapConfig &wifiEapConfig) const;
391 #ifndef OHOS_ARCH_LITE
392     void GetStaControlInfo();
393     bool IsAppInCandidateFilterList(int uid) const;
394 #endif
395 
396 private:
397 #ifndef OHOS_ARCH_LITE
398     class WifiCountryCodeChangeObserver : public IWifiCountryCodeChangeListener {
399     public:
WifiCountryCodeChangeObserver(const std::string & name,StateMachine & stateMachineObj)400         WifiCountryCodeChangeObserver(const std::string &name, StateMachine &stateMachineObj)
401             : IWifiCountryCodeChangeListener(name, stateMachineObj) {}
402         ~WifiCountryCodeChangeObserver() override = default;
403         ErrCode OnWifiCountryCodeChanged(const std::string &wifiCountryCode) override;
404         std::string GetListenerModuleName() override;
405     };
406     std::shared_ptr<IWifiCountryCodeChangeListener> m_staObserver;
407 #endif
408     StaStateMachine *pStaStateMachine;
409     StaMonitor *pStaMonitor;
410     StaAutoConnectService *pStaAutoConnectService;
411 #ifndef OHOS_ARCH_LITE
412     StaAppAcceleration *pStaAppAcceleration;
413 #endif
414     int m_instId;
415     std::vector<std::string> sta_candidate_trust_list;
416     bool m_connMangerStatus = true;
417 };
418 }  // namespace Wifi
419 }  // namespace OHOS
420 #endif