1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.wifi; 18 19 import android.content.Context; 20 21 import com.android.modules.utils.build.SdkLevel; 22 import com.android.wifi.resources.R; 23 24 import java.io.FileDescriptor; 25 import java.io.PrintWriter; 26 import java.util.concurrent.atomic.AtomicBoolean; 27 import java.util.concurrent.atomic.AtomicInteger; 28 29 import javax.annotation.concurrent.ThreadSafe; 30 31 32 /** Global wifi service in-memory state that is not persisted. */ 33 @ThreadSafe 34 public class WifiGlobals { 35 36 /** 37 * Maximum allowable interval in milliseconds between polling for RSSI and linkspeed 38 * information. This is also used as the polling interval for WifiTrafficPoller, which updates 39 * its data activity on every CMD_RSSI_POLL. 40 */ 41 private static final int MAXIMUM_POLL_RSSI_INTERVAL_MSECS = 6000; 42 43 private final Context mContext; 44 45 private final AtomicInteger mPollRssiIntervalMillis = new AtomicInteger(-1); 46 private final AtomicBoolean mIpReachabilityDisconnectEnabled = new AtomicBoolean(true); 47 private final AtomicBoolean mIsBluetoothConnected = new AtomicBoolean(false); 48 49 // This is read from the overlay, cache it after boot up. 50 private final boolean mIsWpa3SaeUpgradeEnabled; 51 // This is read from the overlay, cache it after boot up. 52 private final boolean mIsWpa3SaeUpgradeOffloadEnabled; 53 // This is read from the overlay, cache it after boot up. 54 private final boolean mIsOweUpgradeEnabled; 55 // This is read from the overlay, cache it after boot up. 56 private final boolean mFlushAnqpCacheOnWifiToggleOffEvent; 57 // This is read from the overlay, cache it after boot up. 58 private final boolean mIsWpa3SaeH2eSupported; 59 // This is read from the overlay, cache it after boot up. 60 private final String mP2pDeviceNamePrefix; 61 // This is read from the overlay, cache it after boot up. 62 private final int mP2pDeviceNamePostfixNumDigits; 63 // This is read from the overlay, cache it after boot up. 64 private final int mClientModeImplNumLogRecs; 65 // This is read from the overlay, cache it after boot up. 66 private final boolean mSaveFactoryMacToConfigStoreEnabled; 67 private final int mWifiLowConnectedScoreThresholdToTriggerScanForMbb; 68 private final int mWifiLowConnectedScoreScanPeriodSeconds; 69 // This is read from the overlay, cache it after boot up. 70 private final boolean mWifiAllowInsecureEnterpriseConfiguration; 71 72 // This is set by WifiManager#setVerboseLoggingEnabled(int). 73 private boolean mIsShowKeyVerboseLoggingModeEnabled = false; 74 private boolean mIsUsingExternalScorer = false; 75 WifiGlobals(Context context)76 public WifiGlobals(Context context) { 77 mContext = context; 78 79 mIsWpa3SaeUpgradeEnabled = mContext.getResources() 80 .getBoolean(R.bool.config_wifiSaeUpgradeEnabled); 81 mIsWpa3SaeUpgradeOffloadEnabled = mContext.getResources() 82 .getBoolean(R.bool.config_wifiSaeUpgradeOffloadEnabled); 83 mIsOweUpgradeEnabled = mContext.getResources() 84 .getBoolean(R.bool.config_wifiOweUpgradeEnabled); 85 mFlushAnqpCacheOnWifiToggleOffEvent = mContext.getResources() 86 .getBoolean(R.bool.config_wifiFlushAnqpCacheOnWifiToggleOffEvent); 87 mIsWpa3SaeH2eSupported = mContext.getResources() 88 .getBoolean(R.bool.config_wifiSaeH2eSupported); 89 mP2pDeviceNamePrefix = mContext.getResources() 90 .getString(R.string.config_wifiP2pDeviceNamePrefix); 91 mP2pDeviceNamePostfixNumDigits = mContext.getResources() 92 .getInteger(R.integer.config_wifiP2pDeviceNamePostfixNumDigits); 93 mClientModeImplNumLogRecs = mContext.getResources() 94 .getInteger(R.integer.config_wifiClientModeImplNumLogRecs); 95 mSaveFactoryMacToConfigStoreEnabled = mContext.getResources() 96 .getBoolean(R.bool.config_wifiSaveFactoryMacToWifiConfigStore); 97 mWifiLowConnectedScoreThresholdToTriggerScanForMbb = mContext.getResources().getInteger( 98 R.integer.config_wifiLowConnectedScoreThresholdToTriggerScanForMbb); 99 mWifiLowConnectedScoreScanPeriodSeconds = mContext.getResources().getInteger( 100 R.integer.config_wifiLowConnectedScoreScanPeriodSeconds); 101 mWifiAllowInsecureEnterpriseConfiguration = mContext.getResources().getBoolean( 102 R.bool.config_wifiAllowInsecureEnterpriseConfigurationsForSettingsAndSUW); 103 } 104 105 /** Get the interval between RSSI polls, in milliseconds. */ getPollRssiIntervalMillis()106 public int getPollRssiIntervalMillis() { 107 int interval = mPollRssiIntervalMillis.get(); 108 if (interval > 0) { 109 return interval; 110 } else { 111 return Math.min( 112 mContext.getResources() 113 .getInteger(R.integer.config_wifiPollRssiIntervalMilliseconds), 114 MAXIMUM_POLL_RSSI_INTERVAL_MSECS); 115 } 116 } 117 118 /** Set the interval between RSSI polls, in milliseconds. */ setPollRssiIntervalMillis(int newPollIntervalMillis)119 public void setPollRssiIntervalMillis(int newPollIntervalMillis) { 120 mPollRssiIntervalMillis.set(newPollIntervalMillis); 121 } 122 123 /** Returns whether CMD_IP_REACHABILITY_LOST events should trigger disconnects. */ getIpReachabilityDisconnectEnabled()124 public boolean getIpReachabilityDisconnectEnabled() { 125 return mIpReachabilityDisconnectEnabled.get(); 126 } 127 128 /** Sets whether CMD_IP_REACHABILITY_LOST events should trigger disconnects. */ setIpReachabilityDisconnectEnabled(boolean enabled)129 public void setIpReachabilityDisconnectEnabled(boolean enabled) { 130 mIpReachabilityDisconnectEnabled.set(enabled); 131 } 132 133 /** Set whether bluetooth is enabled. */ setBluetoothEnabled(boolean isEnabled)134 public void setBluetoothEnabled(boolean isEnabled) { 135 // If BT was connected and then turned off, there is no CONNECTION_STATE_CHANGE message. 136 // So set mIsBluetoothConnected to false if we get a bluetooth disable while connected. 137 // But otherwise, Bluetooth being turned on doesn't mean that we're connected. 138 if (!isEnabled) { 139 mIsBluetoothConnected.set(false); 140 } 141 } 142 143 /** Set whether bluetooth is connected. */ setBluetoothConnected(boolean isConnected)144 public void setBluetoothConnected(boolean isConnected) { 145 mIsBluetoothConnected.set(isConnected); 146 } 147 148 /** Get whether bluetooth is connected */ isBluetoothConnected()149 public boolean isBluetoothConnected() { 150 return mIsBluetoothConnected.get(); 151 } 152 153 /** 154 * Helper method to check if Connected MAC Randomization is supported - onDown events are 155 * skipped if this feature is enabled (b/72459123). 156 * 157 * @return boolean true if Connected MAC randomization is supported, false otherwise 158 */ isConnectedMacRandomizationEnabled()159 public boolean isConnectedMacRandomizationEnabled() { 160 return mContext.getResources().getBoolean( 161 R.bool.config_wifi_connected_mac_randomization_supported); 162 } 163 164 /** 165 * Help method to check if WPA3 SAE auto-upgrade is enabled. 166 * 167 * @return boolean true if auto-upgrade is enabled, false otherwise. 168 */ isWpa3SaeUpgradeEnabled()169 public boolean isWpa3SaeUpgradeEnabled() { 170 return mIsWpa3SaeUpgradeEnabled; 171 } 172 173 /** 174 * Help method to check if WPA3 SAE auto-upgrade offload is enabled. 175 * 176 * @return boolean true if auto-upgrade offload is enabled, false otherwise. 177 */ isWpa3SaeUpgradeOffloadEnabled()178 public boolean isWpa3SaeUpgradeOffloadEnabled() { 179 return mIsWpa3SaeUpgradeOffloadEnabled; 180 } 181 182 /** 183 * Help method to check if OWE auto-upgrade is enabled. 184 * 185 * @return boolean true if auto-upgrade is enabled, false otherwise. 186 */ isOweUpgradeEnabled()187 public boolean isOweUpgradeEnabled() { 188 // OWE auto-upgrade is supported on S or newer releases. 189 return SdkLevel.isAtLeastS() && mIsOweUpgradeEnabled; 190 } 191 192 /** 193 * Help method to check if the setting to flush ANQP cache when Wi-Fi is toggled off. 194 * 195 * @return boolean true to flush ANQP cache on Wi-Fi toggle off event, false otherwise. 196 */ flushAnqpCacheOnWifiToggleOffEvent()197 public boolean flushAnqpCacheOnWifiToggleOffEvent() { 198 return mFlushAnqpCacheOnWifiToggleOffEvent; 199 } 200 201 /* 202 * Help method to check if WPA3 SAE Hash-to-Element is supported on this device. 203 * 204 * @return boolean true if supported;otherwise false. 205 */ isWpa3SaeH2eSupported()206 public boolean isWpa3SaeH2eSupported() { 207 return mIsWpa3SaeH2eSupported; 208 } 209 210 /** Set if show key verbose logging mode is enabled. */ setShowKeyVerboseLoggingModeEnabled(boolean enable)211 public void setShowKeyVerboseLoggingModeEnabled(boolean enable) { 212 mIsShowKeyVerboseLoggingModeEnabled = enable; 213 } 214 215 /** Check if show key verbose logging mode is enabled. */ getShowKeyVerboseLoggingModeEnabled()216 public boolean getShowKeyVerboseLoggingModeEnabled() { 217 return mIsShowKeyVerboseLoggingModeEnabled; 218 } 219 220 /** Set whether the external scorer is being used **/ setUsingExternalScorer(boolean isUsingExternalScorer)221 public void setUsingExternalScorer(boolean isUsingExternalScorer) { 222 mIsUsingExternalScorer = isUsingExternalScorer; 223 } 224 225 /** Get whether the external scorer is being used **/ isUsingExternalScorer()226 public boolean isUsingExternalScorer() { 227 return mIsUsingExternalScorer; 228 } 229 230 /** Get the prefix of the default wifi p2p device name. */ getWifiP2pDeviceNamePrefix()231 public String getWifiP2pDeviceNamePrefix() { 232 return mP2pDeviceNamePrefix; 233 } 234 235 /** Get the number of the default wifi p2p device name postfix digit. */ getWifiP2pDeviceNamePostfixNumDigits()236 public int getWifiP2pDeviceNamePostfixNumDigits() { 237 return mP2pDeviceNamePostfixNumDigits; 238 } 239 240 /** Get the number of log records to maintain. */ getClientModeImplNumLogRecs()241 public int getClientModeImplNumLogRecs() { 242 return mClientModeImplNumLogRecs; 243 } 244 245 /** Get whether to use the saved factory MAC address when available **/ isSaveFactoryMacToConfigStoreEnabled()246 public boolean isSaveFactoryMacToConfigStoreEnabled() { 247 return mSaveFactoryMacToConfigStoreEnabled; 248 } 249 250 /** Get the low score threshold to do scan for MBB when external scorer is not used. **/ getWifiLowConnectedScoreThresholdToTriggerScanForMbb()251 public int getWifiLowConnectedScoreThresholdToTriggerScanForMbb() { 252 return mWifiLowConnectedScoreThresholdToTriggerScanForMbb; 253 } 254 255 /** Get the minimum period between the extra scans triggered for MBB when score is low **/ getWifiLowConnectedScoreScanPeriodSeconds()256 public int getWifiLowConnectedScoreScanPeriodSeconds() { 257 return mWifiLowConnectedScoreScanPeriodSeconds; 258 } 259 260 /** Get whether or not insecure enterprise configuration is allowed. */ isInsecureEnterpriseConfigurationAllowed()261 public boolean isInsecureEnterpriseConfigurationAllowed() { 262 return mWifiAllowInsecureEnterpriseConfiguration; 263 } 264 265 /** Dump method for debugging */ dump(FileDescriptor fd, PrintWriter pw, String[] args)266 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 267 pw.println("Dump of WifiGlobals"); 268 pw.println("mPollRssiIntervalMillis=" + mPollRssiIntervalMillis.get()); 269 pw.println("mIpReachabilityDisconnectEnabled=" + mIpReachabilityDisconnectEnabled.get()); 270 pw.println("mIsBluetoothConnected=" + mIsBluetoothConnected.get()); 271 pw.println("mIsWpa3SaeUpgradeEnabled=" + mIsWpa3SaeUpgradeEnabled); 272 pw.println("mIsWpa3SaeUpgradeOffloadEnabled=" + mIsWpa3SaeUpgradeOffloadEnabled); 273 pw.println("mIsOweUpgradeEnabled=" + mIsOweUpgradeEnabled); 274 pw.println("mFlushAnqpCacheOnWifiToggleOffEvent=" + mFlushAnqpCacheOnWifiToggleOffEvent); 275 pw.println("mIsWpa3SaeH2eSupported=" + mIsWpa3SaeH2eSupported); 276 pw.println("mP2pDeviceNamePrefix=" + mP2pDeviceNamePrefix); 277 pw.println("mP2pDeviceNamePostfixNumDigits=" + mP2pDeviceNamePostfixNumDigits); 278 pw.println("mClientModeImplNumLogRecs=" + mClientModeImplNumLogRecs); 279 pw.println("mSaveFactoryMacToConfigStoreEnabled=" + mSaveFactoryMacToConfigStoreEnabled); 280 pw.println("mWifiLowConnectedScoreThresholdToTriggerScanForMbb=" 281 + mWifiLowConnectedScoreThresholdToTriggerScanForMbb); 282 pw.println("mWifiLowConnectedScoreScanPeriodSeconds=" 283 + mWifiLowConnectedScoreScanPeriodSeconds); 284 pw.println("mIsUsingExternalScorer=" 285 + mIsUsingExternalScorer); 286 pw.println("mWifiAllowInsecureEnterpriseConfiguratio" 287 + mWifiAllowInsecureEnterpriseConfiguration); 288 } 289 } 290