• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-2023 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_SELF_CURE_STATE_MACHINE_H
17 #define OHOS_SELF_CURE_STATE_MACHINE_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include "define.h"
22 #include "wifi_log.h"
23 #include "wifi_errcode.h"
24 #include "state_machine.h"
25 #include "self_cure_common.h"
26 #include "self_cure_service_callback.h"
27 #include "sta_service_callback.h"
28 #include "ista_service.h"
29 #include "ip2p_service_callbacks.h"
30 #include "wifi_scan_msg.h"
31 #include "iself_cure_service.h"
32 #include "wifi_service_manager.h"
33 #include "wifi_msg.h"
34 #include <fcntl.h>
35 #include "arp_checker.h"
36 #include "self_cure_msg.h"
37 #include "wifi_common_util.h"
38 #include "wifi_net_observer.h"
39 
40 namespace OHOS {
41 namespace Wifi {
42 constexpr int CURRENT_RSSI_INIT = -200;
43 constexpr int MAX_SELF_CURE_CNT_INVALID_IP = 3;
44 constexpr int VEC_POS_0 = 0;
45 constexpr int VEC_POS_1 = 1;
46 constexpr int VEC_POS_2 = 2;
47 constexpr int TRY_TIMES = 3;
48 constexpr int STATIC_IP_ADDR = 156;
49 constexpr int IP_ADDR_LIMIT = 255;
50 constexpr int IP_ADDR_SIZE = 4;
51 constexpr int NET_MASK_LENGTH = 24;
52 constexpr int SELF_CURE_FAILED_ONE_CNT = 1;
53 constexpr int SELF_CURE_FAILED_TWO_CNT = 2;
54 constexpr int SELF_CURE_FAILED_THREE_CNT = 3;
55 constexpr int SELF_CURE_FAILED_FOUR_CNT = 4;
56 constexpr int SELF_CURE_FAILED_FIVE_CNT = 5;
57 constexpr int SELF_CURE_FAILED_SIX_CNT = 6;
58 constexpr int SELF_CURE_FAILED_SEVEN_CNT = 7;
59 constexpr int DNS_FAILED_CNT = 2;
60 
61 class SelfCureStateMachine : public StateMachine {
62     FRIEND_GTEST(SelfCureStateMachine);
63 
64 public:
65     explicit SelfCureStateMachine(int instId = 0);
66     ~SelfCureStateMachine();
67     using selfCureSmHandleFunc = void (SelfCureStateMachine::*)(InternalMessagePtr msg);
68     using SelfCureSmHandleFuncMap = std::map<int, selfCureSmHandleFunc>;
69 
70     /* *
71      * @Description  Definition of DefaultState class in SelfCureStateMachine.
72      *
73      */
74     class DefaultState : public State {
75     public:
76         explicit DefaultState(SelfCureStateMachine *selfCureStateMachine);
77         ~DefaultState() override;
78         void GoInState() override;
79         void GoOutState() override;
80         bool ExecuteStateMsg(InternalMessagePtr msg) override;
81         void HandleDhcpOfferPacketRcv(const IpInfo &info);
82         void HandleP2pEnhanceStateChange(int state);
83     private:
84         SelfCureStateMachine *pSelfCureStateMachine_;
85     };
86 
87     /* *
88      * @Description  Definition of ConnectedMonitorState class in SelfCureStateMachine.
89      *
90      */
91     class ConnectedMonitorState : public State {
92     public:
93         explicit ConnectedMonitorState(SelfCureStateMachine *selfCureStateMachine);
94         ~ConnectedMonitorState() override;
95         void GoInState() override;
96         void GoOutState() override;
97         bool ExecuteStateMsg(InternalMessagePtr msg) override;
98         using selfCureCmsHandleFunc = std::function<void(InternalMessagePtr msg)>;
99         using SelfCureCmsHandleFuncMap = std::map<int, selfCureCmsHandleFunc>;
100 
101     private:
102         SelfCureStateMachine *pSelfCureStateMachine_;
103         int lastSignalLevel_ = -1;
104         std::string lastConnectedBssid_;
105         bool isMobileHotspot_ = false;
106         bool isIpv4DnsEnabled_ = false;
107         bool isGatewayInvalid_ = false;
108         std::string configAuthType_ = "-1";
109         bool isHasInternetRecently_ = false;
110         bool isPortalUnthenEver_ = false;
111         bool isUserSetStaticIpConfig_ = false;
112         bool isWifiSwitchAllowed_ = false;
113         int lastDnsFailedCnt_ = 0;
114         SelfCureCmsHandleFuncMap selfCureCmsHandleFuncMap_;
115         int InitSelfCureCmsHandleMap();
116         void HandleResetupSelfCure(InternalMessagePtr msg);
117         void HandlePeriodicArpDetection(InternalMessagePtr msg);
118         void HandleNetworkConnect(InternalMessagePtr msg);
119         void HandleNetworkDisconnect(InternalMessagePtr msg);
120         void HandleRssiLevelChange(InternalMessagePtr msg);
121         void TransitionToSelfCureState(int reason);
122         void HandleArpDetectionFailed(InternalMessagePtr msg);
123         bool SetupSelfCureMonitor();
124         void UpdateInternetAccessHistory();
125         void RequestReassocWithFactoryMac();
126         void HandleInvalidIp(InternalMessagePtr msg);
127         void HandleInternetFailedDetected(InternalMessagePtr msg);
128         void HandleInternetFailedDetectedInner();
129         void HandleTcpQualityQuery(InternalMessagePtr msg);
130         void HandleGatewayChanged(InternalMessagePtr msg);
131         bool IsGatewayChanged();
132         void HandleDnsFailedMonitor(InternalMessagePtr msg);
133         bool IsNeedSelfCure();
134     };
135 
136     /* *
137      * @Description  Definition of DisconnectedMonitorState class in SelfCureStateMachine.
138      *
139      */
140     class DisconnectedMonitorState : public State {
141     public:
142         explicit DisconnectedMonitorState(SelfCureStateMachine *selfCureStateMachine);
143         ~DisconnectedMonitorState() override;
144         void GoInState() override;
145         void GoOutState() override;
146         bool ExecuteStateMsg(InternalMessagePtr msg) override;
147 
148     private:
149         void HandleWifi7BlacklistRecover(InternalMessagePtr msg);
150         void HandleWifi7WithoutMldBackoff(InternalMessagePtr msg);
151         void HandleWifi7MldBackoff(InternalMessagePtr msg);
152         void HandleNetworkConnectFailCount(InternalMessagePtr msg);
153         SelfCureStateMachine *pSelfCureStateMachine_;
154         bool isSetStaticIpConfig_ = false;
155     };
156 
157     /* *
158      * @Description  Definition of ConnectionSelfCureState class in SelfCureStateMachine.
159      *
160      */
161     class ConnectionSelfCureState : public State {
162     public:
163         explicit ConnectionSelfCureState(SelfCureStateMachine *selfCureStateMachine);
164         ~ConnectionSelfCureState() override;
165         void GoInState() override;
166         void GoOutState() override;
167         bool ExecuteStateMsg(InternalMessagePtr msg) override;
168 
169     private:
170         SelfCureStateMachine *pSelfCureStateMachine_;
171     };
172 
173     /* *
174      * @Description  Definition of InternetSelfCureState class in SelfCureStateMachine.
175      *
176      */
177     class InternetSelfCureState : public State {
178     public:
179         explicit InternetSelfCureState(SelfCureStateMachine *selfCureStateMachine);
180         ~InternetSelfCureState() override;
181         void GoInState() override;
182         void GoOutState() override;
183         bool ExecuteStateMsg(InternalMessagePtr msg) override;
184         using selfCureIssHandleFunc = std::function<void(InternalMessagePtr msg)>;
185         using SelfCureIssHandleFuncMap = std::map<int, selfCureIssHandleFunc>;
186 
187     private:
188         SelfCureStateMachine *pSelfCureStateMachine_;
189         int currentRssi_ = -1;
190         std::string currentBssid_ = "";
191         int currentAbnormalType_ = -1;
192         int currentSelfCureLevel_ = -1;
193         int renewDhcpCount_ = -1;
194         bool isHasInternetRecently_ = false;
195         bool isPortalUnthenEver_ = false;
196         bool isUserSetStaticIpConfig_ = false;
197         int64_t lastHasInetTime_ = 0;
198         bool isDelayedReassocSelfCure_ = false;
199         bool isDelayedRandMacReassocSelfCure_ = false;
200         bool isDelayedResetSelfCure_ = false;
201         bool isSetStaticIp4InvalidIp_ = false;
202         bool isConfigStaticIp4MultiDhcpServer_ = false;
203         std::string unConflictedIp_ = "";
204         int lastMultiGwSelfFailedType_ = -1;
205         bool isUsedMultiGwSelfcure_ = false;
206         std::string configAuthType_ = "";
207         bool isFinalSelfCureUsed_ = false;
208         std::vector<int> testedSelfCureLevel_;
209         WifiSelfCureHistoryInfo selfCureHistoryInfo_;
210         int selfCureForInvalidIpCnt_ = 0;
211         SelfCureIssHandleFuncMap selfCureIssHandleFuncMap_;
212         int InitSelfCureIssHandleMap();
213         void HandleInternetFailedSelfCure(InternalMessagePtr msg);
214         void HandleSelfCureWifiLink(InternalMessagePtr msg);
215         void HandleNetworkDisconnected(InternalMessagePtr msg);
216         void HandleInternetRecoveryConfirm(InternalMessagePtr msg);
217         void HandleRssiChangedEvent(InternalMessagePtr msg);
218         void HandleP2pDisconnected(InternalMessagePtr msg);
219         void HandlePeriodicArpDetecte(InternalMessagePtr msg);
220         void HandleArpFailedDetected(InternalMessagePtr msg);
221         void HandleHttpReachableRecv(InternalMessagePtr msg);
222         void HandleSelfCureResultFailed(InternalMessagePtr msg);
223         void SelectSelfCureByFailedReason(int internetFailedType);
224         int SelectBestSelfCureSolution(int internetFailedType);
225         int SelectBestSelfCureSolutionExt(int internetFailedType);
226         void SelfCureWifiLink(int requestCureLevel);
227         bool SelectedSelfCureAcceptable();
228         void SelfCureForRandMacReassoc(int requestCureLevel);
229         void SelfCureForReset(int requestCureLevel);
230         bool ConfirmInternetSelfCure(int currentCureLevel);
231         void HandleConfirmInternetSelfCureFailed(int currentCureLevel);
232         void HandleInternetFailedAndUserSetStaticIp(int internetFailedType);
233         bool HasBeenTested(int cureLevel);
234         void HandleHttpUnreachableFinally();
235         void HandleHttpReachableAfterSelfCure(int currentCureLevel);
236         void HandleSelfCureFailedForRandMacReassoc();
237         void HandleRssiChanged();
238         void HandleDelayedResetSelfCure();
239         void SelfCureForInvalidIp();
240         void SelfCureForReassoc(int requestCureLevel);
241         void SelfcureForMultiGateway(InternalMessagePtr msg);
242         bool IsNeedMultiGatewaySelfcure();
243         void SelfCureForStaticIp(int requestCureLevel);
244         void RequestUseStaticIpConfig(IpInfo &dhcpResult);
245         IpInfo GetNextTestDhcpResults();
246         IpInfo GetRecordDhcpResults();
247         void InitCurrentGateway();
248     };
249 
250     /* *
251      * @Description  Definition of Wifi6SelfCureState class in SelfCureStateMachine.
252      *
253      */
254     class Wifi6SelfCureState : public State {
255     public:
256         explicit Wifi6SelfCureState(SelfCureStateMachine *selfCureStateMachine);
257         ~Wifi6SelfCureState() override;
258         void GoInState() override;
259         void GoOutState() override;
260         bool ExecuteStateMsg(InternalMessagePtr msg) override;
261 
262     private:
263         SelfCureStateMachine *pSelfCureStateMachine_;
264         int wifi6HtcArpDetectionFailedCnt_ = 0;
265         int wifi6ArpDetectionFailedCnt_ = 0;
266         int32_t internetValue_ = 0;
267         bool isForceHttpCheck_ = true;
268         void PeriodicWifi6WithHtcArpDetect(InternalMessagePtr msg);
269         void PeriodicWifi6WithoutHtcArpDetect(InternalMessagePtr msg);
270         void HandleWifi6WithHtcArpFail(InternalMessagePtr msg);
271         void HandleWifi6WithoutHtcArpFail(InternalMessagePtr msg);
272         void Wifi6ReassocSelfcure();
273     };
274 
275     /* *
276      * @Description  Definition of NoInternetState class in SelfCureStateMachine.
277      *
278      */
279     class NoInternetState : public State {
280     public:
281         explicit NoInternetState(SelfCureStateMachine *selfCureStateMachine);
282         ~NoInternetState() override;
283         void GoInState() override;
284         void GoOutState() override;
285         bool ExecuteStateMsg(InternalMessagePtr msg) override;
286 
287     private:
288         void HandleArpFailedDetected(InternalMessagePtr msg);
289         SelfCureStateMachine *pSelfCureStateMachine_;
290     };
291 
292     ErrCode Initialize();
293     void SetHttpMonitorStatus(bool isHttpReachable);
294     bool IsSelfCureOnGoing();
295     bool IsSelfCureL2Connecting();
296     void StopSelfCureWifi(int32_t status);
297     bool CheckSelfCureWifiResult(int event);
298     void HandleP2pConnChanged(const WifiP2pLinkedInfo &info);
299     bool IsWifiSelfcureDone();
300 private:
301 
302     /* *
303      * @Description  Destruct state.
304      *
305      */
306     template <typename T>
ParsePointer(T * & pointer)307     inline void ParsePointer(T *&pointer)
308     {
309         if (pointer != nullptr) {
310             delete pointer;
311             pointer = nullptr;
312         }
313     }
314 
315     /**
316      * @Description  Build state tree
317      *
318      */
319     void BuildStateTree();
320 
321     /**
322      * @Description  Determine whether it is empty during initialization
323      *
324      */
325     template <typename T>
JudgmentEmpty(T * & pointer)326     inline ErrCode JudgmentEmpty(T *&pointer)
327     {
328         if (pointer == nullptr) {
329             return WIFI_OPT_FAILED;
330         }
331         return WIFI_OPT_SUCCESS;
332     }
333 
334     /**
335      * @Description  Initializing state of Self Cure.
336      *
337      */
338     ErrCode InitSelfCureStates();
339     void SendBlaListToDriver(int blaListType);
340     std::string BlackListToString(std::map<std::string, WifiCategoryBlackListInfo> &map);
341     std::string ParseWifiCategoryBlackListInfo(std::pair<std::string, WifiCategoryBlackListInfo> iter);
342     bool AgeOutWifiCategoryBlack(int blaListType);
343     void AgeOutWifiConnectFailList();
344     int GetCurSignalLevel();
345     bool IsHttpReachable();
346     int GetLegalIpConfiguration(IpInfo &dhcpResults);
347     bool CanArpReachable();
348     bool DoSlowArpTest(const std::string& testIpAddr);
349     bool IsIpAddressInvalid();
350     bool IsUseFactoryMac();
351     bool IsNeedWifiReassocUseDeviceMac();
352     bool IfP2pConnected();
353     bool ShouldTransToWifi6SelfCure(InternalMessagePtr msg, std::string currConnectedBssid);
354     int GetWifi7SelfCureType(int connectFailTimes, WifiLinkedInfo &info);
355     void ShouldTransToWifi7SelfCure(WifiLinkedInfo &info);
356     void HandleWifiBlackListUpdateMsg();
357     int GetScanRssi(std::string currentBssid, const std::vector<WifiScanInfo> scanResults);
358     int GetCurrentRssi();
359     std::string GetCurrentBssid();
360     bool IsWifi6Network(std::string currConnectedBssid);
361     void PeriodicArpDetection();
362     bool IsSuppOnCompletedState();
363     bool IfPeriodicArpDetection();
364     std::string GetAuthType();
365     int GetIpAssignment(AssignIpMethod &ipAssignment);
366     time_t GetLastHasInternetTime();
367     uint32_t GetNetworkStatusHistory();
368     std::string GetSelfCureHistoryInfo();
369     int SetSelfCureHistoryInfo(const std::string selfCureHistory);
370     int GetIsReassocWithFactoryMacAddress();
371     int SetIsReassocWithFactoryMacAddress(int isReassocWithFactoryMacAddress);
372     bool IsCustNetworkSelfCure();
373     ErrCode GetCurrentWifiDeviceConfig(WifiDeviceConfig &config);
374     void HandleNetworkConnected();
375     bool UpdateConnSelfCureFailedHistory();
376     void RecoverySoftAp();
377     bool IsSoftApSsidSameWithWifi(const HotspotConfig& curApConfig);
378     void CheckConflictIpForSoftAp();
379     static bool IsEncryptedAuthType(const std::string authType);
380     bool DoArpTest(std::string& ipAddress, std::string& gateway);
381     void RequestArpConflictTest();
382     bool IfMultiGateway();
383     bool IsSettingsPage();
384     bool IsMultiDhcpOffer();
385     void ClearDhcpOffer();
386     bool CheckSelfCureConnectState();
387     void CheckSelfCureReassocState();
388     void CheckSelfCureDisconnectState();
389     void UpdateSelfcureState(int currentCureLevel, bool isSelfCureOnGoing);
390     void HandleSelfCureNormal();
391     void HandleSelfCureException(int reasonCode);
392     void HandleSelfCureDisconnectException();
393     void StopSelfCureDelay(int status, int delay);
394     void HandleSceStopSelfCure(int status);
395     void SetSelfCureWifiTimeOut(SelfCureState wifiSelfCureState);
396     void ResetSelfCureParam();
397     void NotifySelfCureCompleted(int status);
398     void HandleConnectFailed();
399     void ForceStopSelfCure();
400 
401 private:
402     SelfCureSmHandleFuncMap selfCureSmHandleFuncMap_;
403     std::map<std::string, SelfCureServiceCallback> mSelfCureCallback_;
404     DefaultState *pDefaultState_;
405     ConnectedMonitorState *pConnectedMonitorState_;
406     DisconnectedMonitorState *pDisconnectedMonitorState_;
407     ConnectionSelfCureState *pConnectionSelfCureState_;
408     InternetSelfCureState *pInternetSelfCureState_;
409     Wifi6SelfCureState *pWifi6SelfCureState_;
410     NoInternetState *pNoInternetState_;
411 
412     int instId_;
413     bool isHttpReachable_ = false;
414     int useWithRandMacAddress_ = 0;
415     std::atomic<bool> isSelfCureOnGoing_ = false;
416     std::atomic<bool> isP2pConnected_ = false;
417     std::atomic<bool> isNotAllowSelfcure_ = true;
418     int arpDetectionFailedCnt_ = 0;
419     int selfCureReason_ = -1;
420     int noTcpRxCounter_ = 0;
421     uint32_t connectNetworkRetryCnt_ = 0;
422     bool isInternetUnknown_ = false;
423     int noAutoConnCounter_ = 0;
424     int noAutoConnReason_ = -1;
425     bool isStaticIpCureSuccess_ = false;
426     bool isWifi6ArpSuccess_ = false;
427     bool isHasTestWifi6Reassoc_ = false;
428     bool isReassocSelfCureWithRealMacAddress_ = false;
429     int64_t connectedTime_ = 0;
430     std::mutex dhcpFailedBssidLock_;
431     std::vector<std::string> dhcpFailedBssids_;
432     std::vector<std::string> dhcpFailedConfigKeys_;
433     std::map<std::string, int> autoConnectFailedNetworksRssi_;
434     std::atomic<bool> isWifiBackground_ = false;
435     sptr<NetStateObserver> mNetWorkDetect_;
436     bool isP2pEnhanceConnected_ = false;
437     bool isInternetFailureDetected_ = false;
438     std::atomic<bool> isSelfcureDone_ = false;
439     DetailedState selfCureNetworkLastState_ = DetailedState::IDLE;
440     WifiState selfCureWifiLastState_ = WifiState::UNKNOWN;
441     SelfCureState selfCureL2State_ = SelfCureState::SCE_WIFI_INVALID_STATE;
442     std::mutex detectionMtx_;
443     std::condition_variable detectionCond_;
444 };
445 } // namespace Wifi
446 } // namespace OHOS
447 #endif