diff options
author | Randy Pan <zpan@google.com> | 2016-10-03 13:41:25 -0700 |
---|---|---|
committer | Randy Pan <zpan@google.com> | 2016-10-04 17:51:00 +0000 |
commit | 6c90568a19e32a1825e06a608e59d3f36daff9ca (patch) | |
tree | 45d5ddb127f5db8d0705c513aff52012e5556acd | |
parent | f5d99b0d7990488da938ea69be821c48f4bfa9b7 (diff) |
Move WifiNetworkSelector to WifiInjector
While there, move WifiInfo to be an input parameter of
WifiNetworkSelector#selectNetwork and make WifiInfo the
sole source for obtaining information of the currently
connected network.
Bug: 31383992
Test: Wifi framework unit tests
Change-Id: I107ba600165b008079cebf230865f8b2082740a8
7 files changed, 49 insertions, 49 deletions
diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java index 9386bb304..bd64e114e 100644 --- a/service/java/com/android/server/wifi/WifiConnectivityManager.java +++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java @@ -229,7 +229,7 @@ public class WifiConnectivityManager { localLog(listenerName + " onResults: start network selection"); WifiConfiguration candidate = - mNetworkSelector.selectNetwork(scanDetails, + mNetworkSelector.selectNetwork(scanDetails, mWifiInfo, mStateMachine.isConnected(), mStateMachine.isDisconnected(), mUntrustedConnectionAllowed); mWifiLastResortWatchdog.updateAvailableNetworks( diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index 53e089d56..711c03b54 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -83,6 +83,7 @@ public class WifiInjector { private final IpConfigStore mIpConfigStore; private final WifiConfigStoreLegacy mWifiConfigStoreLegacy; private final WifiConfigManager mWifiConfigManager; + private final WifiNetworkSelector mWifiNetworkSelector; private WifiScanner mWifiScanner; private final boolean mUseRealLogger; @@ -139,6 +140,7 @@ public class WifiInjector { mWifiConfigManager = new WifiConfigManager(mContext, mFrameworkFacade, mClock, UserManager.get(mContext), TelephonyManager.from(mContext), mWifiKeyStore, mWifiConfigStore, mWifiConfigStoreLegacy); + mWifiNetworkSelector = new WifiNetworkSelector(mContext, mWifiConfigManager, mClock); mWifiStateMachine = new WifiStateMachine(mContext, mFrameworkFacade, mWifiStateMachineHandlerThread.getLooper(), UserManager.get(mContext), @@ -322,4 +324,11 @@ public class WifiInjector { } return mWifiScanner; } + + /** + * Obtain an instance of WifiNetworkSelector. + */ + public WifiNetworkSelector getWifiNetworkSelector() { + return mWifiNetworkSelector; + } } diff --git a/service/java/com/android/server/wifi/WifiNetworkSelector.java b/service/java/com/android/server/wifi/WifiNetworkSelector.java index fec10eec7..9f19b32d7 100644 --- a/service/java/com/android/server/wifi/WifiNetworkSelector.java +++ b/service/java/com/android/server/wifi/WifiNetworkSelector.java @@ -56,10 +56,7 @@ public class WifiNetworkSelector { public static final int BSSID_BLACKLIST_EXPIRE_TIME_MS = 5 * 60 * 1000; private WifiConfigManager mWifiConfigManager; - private WifiInfo mWifiInfo; private Clock mClock; - private WifiConfiguration mCurrentNetwork = null; - private String mCurrentBssid = null; private static class BssidBlacklistStatus { // Number of times this BSSID has been rejected for association. public int counter; @@ -152,7 +149,10 @@ public class WifiNetworkSelector { mLocalLog.log(log); } - private boolean isCurrentNetworkSufficient(WifiConfiguration network) { + private boolean isCurrentNetworkSufficient(WifiInfo wifiInfo) { + WifiConfiguration network = + mWifiConfigManager.getConfiguredNetwork(wifiInfo.getNetworkId()); + // Currently connected? if (network == null) { localLog("No current connected network."); @@ -175,16 +175,16 @@ public class WifiNetworkSelector { } // 2.4GHz networks is not qualified. - if (mWifiInfo.is24GHz()) { + if (wifiInfo.is24GHz()) { localLog("Current network is 2.4GHz."); return false; } // Is the current network's singnal strength qualified? It can only // be a 5GHz network if we reach here. - int currentRssi = mWifiInfo.getRssi(); - if (mWifiInfo.is5GHz() && currentRssi < mThresholdQualifiedRssi5) { - localLog("Current network band=" + (mWifiInfo.is5GHz() ? "5GHz" : "2.4GHz") + int currentRssi = wifiInfo.getRssi(); + if (wifiInfo.is5GHz() && currentRssi < mThresholdQualifiedRssi5) { + localLog("Current network band=" + (wifiInfo.is5GHz() ? "5GHz" : "2.4GHz") + ", RSSI[" + currentRssi + "]-acceptable but not qualified."); return false; } @@ -192,7 +192,7 @@ public class WifiNetworkSelector { return true; } - private boolean isNetworkSelectionNeeded(List<ScanDetail> scanDetails, + private boolean isNetworkSelectionNeeded(List<ScanDetail> scanDetails, WifiInfo wifiInfo, boolean connected, boolean disconnected) { if (scanDetails.size() == 0) { localLog("Empty connectivity scan results. Skip network selection."); @@ -218,15 +218,15 @@ public class WifiNetworkSelector { } } - if (isCurrentNetworkSufficient(mCurrentNetwork)) { + if (isCurrentNetworkSufficient(wifiInfo)) { localLog("Current connected network already sufficient. Skip network selection."); return false; } else { localLog("Current connected network is not sufficient."); + return true; } } else if (disconnected) { - mCurrentNetwork = null; - mCurrentBssid = null; + return true; } else { // No network selection if WifiStateMachine is in a state other than // CONNECTED or DISCONNECTED. @@ -234,8 +234,6 @@ public class WifiNetworkSelector { + " Skip network selection."); return false; } - - return true; } /** @@ -446,7 +444,7 @@ public class WifiNetworkSelector { * */ @Nullable - public WifiConfiguration selectNetwork(List<ScanDetail> scanDetails, + public WifiConfiguration selectNetwork(List<ScanDetail> scanDetails, WifiInfo wifiInfo, boolean connected, boolean disconnected, boolean untrustedNetworkAllowed) { mConnectableNetworks.clear(); if (scanDetails.size() == 0) { @@ -454,17 +452,15 @@ public class WifiNetworkSelector { return null; } - if (mCurrentNetwork == null) { - mCurrentNetwork = - mWifiConfigManager.getConfiguredNetwork(mWifiInfo.getNetworkId()); - } + WifiConfiguration currentNetwork = + mWifiConfigManager.getConfiguredNetwork(wifiInfo.getNetworkId()); // Always get the current BSSID from WifiInfo in case that firmware initiated // roaming happened. - mCurrentBssid = mWifiInfo.getBSSID(); + String currentBssid = wifiInfo.getBSSID(); // Shall we start network selection at all? - if (!isNetworkSelectionNeeded(scanDetails, connected, disconnected)) { + if (!isNetworkSelectionNeeded(scanDetails, wifiInfo, connected, disconnected)) { return null; } @@ -490,7 +486,7 @@ public class WifiNetworkSelector { for (NetworkEvaluator registeredEvaluator : mEvaluators) { if (registeredEvaluator != null) { selectedNetwork = registeredEvaluator.evaluateNetworks(scanDetails, - mCurrentNetwork, mCurrentBssid, connected, + currentNetwork, currentBssid, connected, untrustedNetworkAllowed, mConnectableNetworks); if (selectedNetwork != null) { break; @@ -499,8 +495,6 @@ public class WifiNetworkSelector { } if (selectedNetwork != null) { - mCurrentNetwork = selectedNetwork; - mCurrentBssid = selectedNetwork.getNetworkSelectionStatus().getCandidate().BSSID; mLastNetworkSelectionTimeStamp = mClock.getElapsedSinceBootMillis(); } @@ -552,10 +546,8 @@ public class WifiNetworkSelector { return false; } - WifiNetworkSelector(Context context, WifiConfigManager configManager, - WifiInfo wifiInfo, Clock clock) { + WifiNetworkSelector(Context context, WifiConfigManager configManager, Clock clock) { mWifiConfigManager = configManager; - mWifiInfo = wifiInfo; mClock = clock; mThresholdQualifiedRssi24 = context.getResources().getInteger( diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java index 8e9b32364..2ca21b846 100644 --- a/service/java/com/android/server/wifi/WifiStateMachine.java +++ b/service/java/com/android/server/wifi/WifiStateMachine.java @@ -877,8 +877,7 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiRss mWifiDiagnostics = mWifiInjector.makeWifiDiagnostics(mWifiNative); mWifiInfo = new WifiInfo(); - mWifiNetworkSelector = new WifiNetworkSelector(mContext, mWifiConfigManager, - mWifiInfo, mWifiInjector.getClock()); + mWifiNetworkSelector = mWifiInjector.getWifiNetworkSelector(); mSupplicantStateTracker = mFacade.makeSupplicantStateTracker(context, mWifiConfigManager, getHandler()); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index 68a736bac..887895c71 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -218,7 +218,7 @@ public class WifiConnectivityManagerTest { candidateScanResult.BSSID = CANDIDATE_BSSID; candidate.getNetworkSelectionStatus().setCandidate(candidateScanResult); - when(ns.selectNetwork(anyObject(), anyBoolean(), anyBoolean(), + when(ns.selectNetwork(anyObject(), anyObject(), anyBoolean(), anyBoolean(), anyBoolean())).thenReturn(candidate); return ns; } @@ -501,7 +501,7 @@ public class WifiConnectivityManagerTest { */ @Test public void pnoRetryForLowRssiNetwork() { - when(mWifiNS.selectNetwork(anyObject(), anyBoolean(), anyBoolean(), + when(mWifiNS.selectNetwork(anyObject(), anyObject(), anyBoolean(), anyBoolean(), anyBoolean())).thenReturn(null); // Set screen to off @@ -556,7 +556,7 @@ public class WifiConnectivityManagerTest { @Test public void watchdogBitePnoGoodIncrementsMetrics() { // Qns returns no candidate after watchdog single scan. - when(mWifiNS.selectNetwork(anyObject(), anyBoolean(), anyBoolean(), + when(mWifiNS.selectNetwork(anyObject(), anyObject(), anyBoolean(), anyBoolean(), anyBoolean())).thenReturn(null); // Set screen to off diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java index a03e17457..5a1645915 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java @@ -55,8 +55,7 @@ public class WifiNetworkSelectorTest { mWifiConfigManager = getWifiConfigManager(); mWifiInfo = getWifiInfo(); - mWifiNetworkSelector = new WifiNetworkSelector(mContext, mWifiConfigManager, - mWifiInfo, mClock); + mWifiNetworkSelector = new WifiNetworkSelector(mContext, mWifiConfigManager, mClock); mWifiNetworkSelector.registerNetworkEvaluator(mDummyEvaluator, 1); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime()); @@ -199,7 +198,7 @@ public class WifiNetworkSelectorTest { freqs, caps, levels, securities, mWifiConfigManager, mClock); List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, - false, true, false); + mWifiInfo, false, true, false); assertEquals("Expect null configuration", null, candidate); } @@ -227,7 +226,7 @@ public class WifiNetworkSelectorTest { freqs, caps, levels, securities, mWifiConfigManager, mClock); List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, - false, true, false); + mWifiInfo, false, true, false); assertEquals("Expect null configuration", null, candidate); } @@ -256,14 +255,14 @@ public class WifiNetworkSelectorTest { freqs, caps, levels, securities, mWifiConfigManager, mClock); List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, - false, true, false); + mWifiInfo, false, true, false); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS - 2000); // Do another network selection with WSM in CONNECTED state. candidate = mWifiNetworkSelector.selectNetwork(scanDetails, - true, false, false); + mWifiInfo, true, false, false); assertEquals("Expect null configuration", null, candidate); } @@ -294,14 +293,14 @@ public class WifiNetworkSelectorTest { List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, - false, true, false); + mWifiInfo, false, true, false); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS - 2000); // Do another network selection with WSM in DISCONNECTED state. candidate = mWifiNetworkSelector.selectNetwork(scanDetails, - false, true, false); + mWifiInfo, false, true, false); ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); @@ -333,7 +332,7 @@ public class WifiNetworkSelectorTest { List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); // connect to test1 - mWifiNetworkSelector.selectNetwork(scanDetails, false, true, false); + mWifiNetworkSelector.selectNetwork(scanDetails, mWifiInfo, false, true, false); when(mWifiInfo.getNetworkId()).thenReturn(0); when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); when(mWifiInfo.is24GHz()).thenReturn(false); @@ -348,7 +347,7 @@ public class WifiNetworkSelectorTest { scanDetails = scanDetailsAndConfigs.getScanDetails(); WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, - true, false, false); + mWifiInfo, true, false, false); assertEquals("Expect null configuration", null, candidate); } @@ -379,7 +378,7 @@ public class WifiNetworkSelectorTest { WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); // connect to test1 - mWifiNetworkSelector.selectNetwork(scanDetails, false, true, false); + mWifiNetworkSelector.selectNetwork(scanDetails, mWifiInfo, false, true, false); when(mWifiInfo.getNetworkId()).thenReturn(0); when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); when(mWifiInfo.is24GHz()).thenReturn(true); @@ -389,7 +388,7 @@ public class WifiNetworkSelectorTest { // Do another network selection. WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, - true, false, false); + mWifiInfo, true, false, false); ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); @@ -424,7 +423,7 @@ public class WifiNetworkSelectorTest { WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); // connect to test1 - mWifiNetworkSelector.selectNetwork(scanDetails, false, true, false); + mWifiNetworkSelector.selectNetwork(scanDetails, mWifiInfo, false, true, false); when(mWifiInfo.getNetworkId()).thenReturn(0); when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); when(mWifiInfo.is24GHz()).thenReturn(false); @@ -435,7 +434,7 @@ public class WifiNetworkSelectorTest { // Do another network selection. WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, - true, false, false); + mWifiInfo, true, false, false); ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); @@ -469,7 +468,7 @@ public class WifiNetworkSelectorTest { WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); // connect to test1 - mWifiNetworkSelector.selectNetwork(scanDetails, false, true, false); + mWifiNetworkSelector.selectNetwork(scanDetails, mWifiInfo, false, true, false); when(mWifiInfo.getNetworkId()).thenReturn(0); when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); when(mWifiInfo.is24GHz()).thenReturn(false); @@ -481,7 +480,7 @@ public class WifiNetworkSelectorTest { // Do another network selection. WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, - true, false, false); + mWifiInfo, true, false, false); ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java index 24f383a1d..9de360812 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java @@ -340,6 +340,7 @@ public class WifiStateMachineTest { when(mWifiInjector.getWifiConfigManager()).thenReturn(mWifiConfigManager); when(mWifiInjector.getWifiSupplicantControl()).thenReturn(mWifiSupplicantControl); when(mWifiInjector.getWifiScanner()).thenReturn(mWifiScanner); + when(mWifiInjector.getWifiNetworkSelector()).thenReturn(mock(WifiNetworkSelector.class)); when(mWifiNative.getInterfaceName()).thenReturn("mockWlan"); when(mWifiSupplicantControl.getFrameworkNetworkId(anyInt())).thenReturn(0); |