1 /* 2 * Copyright (C) 2024 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_BLOCK_CONNECT_SERVICE_H 16 #define OHOS_BLOCK_CONNECT_SERVICE_H 17 #include "wifi_logger.h" 18 #include "wifi_sta_hal_interface.h" 19 #include "wifi_settings.h" 20 #include "wifi_common_util.h" 21 #include "wifi_msg.h" 22 namespace OHOS { 23 namespace Wifi { 24 struct LastConnectedApInfo { 25 std::string bssid; 26 int64_t lastDisconnectTimestamp; 27 int alreadyConnectedCount; 28 }; 29 struct DisablePolicy { 30 int64_t disableTime; 31 int64_t disableCount; 32 WifiDeviceConfigStatus disableStatus; DisablePolicyDisablePolicy33 DisablePolicy(int64_t timeDuration, int count, WifiDeviceConfigStatus status) 34 { 35 disableTime = timeDuration; 36 disableCount = count; 37 disableStatus = status; 38 } 39 }; 40 class BlockConnectService { 41 public: 42 static BlockConnectService &GetInstance(); 43 // Constructor 44 BlockConnectService(); 45 46 // Destructor 47 ~BlockConnectService(); 48 49 void Exit(); 50 51 // Method to check if auto connect is enabled for a given WifiDeviceConfig 52 bool ShouldAutoConnect(const WifiDeviceConfig& config); 53 54 // Update the selection status of all saved networks and check if disabled networks have expired 55 bool UpdateAllNetworkSelectStatus(); 56 57 // Enable the selection status of a target network 58 bool EnableNetworkSelectStatus(int targetNetworkId); 59 60 // Clear the blocklist information of a target network with reason for wpa_supplicant disconnection 61 bool UpdateNetworkSelectStatus(int targetNetworkId, DisabledReason disableReason, int wpaReason); 62 63 // Clear the blocklist information of a target network 64 bool UpdateNetworkSelectStatus(int targetNetworkId, DisabledReason disableReason); 65 66 // Check if the given BSSID has frequent disconnects with the last connected network 67 // false - Not frequent disconnect true - Frequent disconnect 68 bool IsFrequentDisconnect(std::string bssid, int wpaReason, int locallyGenerated); 69 70 // Check if the given targetNetworkId is blocked due to wrong password 71 bool IsWrongPassword(int targetNetworkId); 72 73 // Enable all networks by entering settings 74 void OnReceiveSettingsEnterEvent(bool isEnter); 75 #ifdef FEATURE_WIFI_MDM_RESTRICTED_SUPPORT 76 // set thie blocklist information for mdm restrictedlist 77 bool UpdateNetworkSelectStatusForMdmRestrictedList(); 78 // Clear mdmRestrictedList from block connect 79 bool ClearBlockConnectForMdmRestrictedList(); 80 #endif 81 #ifndef OHOS_ARCH_LITE 82 // handle wifi stopped msg 83 void DealStaStopped(int instId); 84 85 // handle wifi connected failed msg 86 void NotifyWifiConnFailedInfo(int targetNetworkId, std::string bssid, DisabledReason disableReason); 87 88 // release all blacklist bssid 89 void ReleaseUnusableBssidSet(); 90 91 // check if the given bssid match the unusable bssid set 92 bool IsBssidMatchUnusableSet(std::string bssid); 93 94 // release all dhcp fail bssid 95 void ReleaseDhcpFailBssidSet(); 96 #endif 97 private: 98 DisablePolicy CalculateDisablePolicy(DisabledReason disableReason); 99 void EnableAllNetworksByEnteringSettings(std::vector<DisabledReason> enableReasons); 100 void LogDisabledConfig(const WifiDeviceConfig& config); 101 #ifndef OHOS_ARCH_LITE 102 void StartClearSetTimer(); 103 void StopClearSetTimer(); 104 void ClearSetTimerCallback(); 105 #endif 106 LastConnectedApInfo mLastConnectedApInfo; 107 std::map<DisabledReason, DisablePolicy> blockConnectPolicies; 108 std::vector<int> validReasons; 109 #ifndef OHOS_ARCH_LITE 110 std::set<std::string> autoJoinUnusableBssidSet_; 111 std::string curUnusableSsid_; 112 std::string curUnusableKeyMgmt_; 113 std::mutex bssidMutex_; 114 std::mutex clearSetTimerMutex_; 115 uint32_t clearSetTimerId_ {0}; 116 std::set<std::string> dhcpFailBssids_; 117 std::mutex dhcpFailMutex_; 118 #endif 119 }; 120 } 121 } 122 #endif