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);
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 
76 private:
77     DisablePolicy CalculateDisablePolicy(DisabledReason disableReason);
78     void EnableAllNetworksByEnteringSettings(std::vector<DisabledReason> enableReasons);
79     void LogDisabledConfig(const WifiDeviceConfig& config);
80     LastConnectedApInfo mLastConnectedApInfo;
81     std::map<DisabledReason, DisablePolicy> blockConnectPolicies;
82     std::vector<int> validReasons;
83 };
84 }
85 }
86 #endif