From fac6568cbb329e06b22bfe07993acb263f3762ab Mon Sep 17 00:00:00 2001 From: Michael Plass Date: Wed, 9 Aug 2017 14:13:48 -0700 Subject: Wifi connection hysteresis Making an automatic connection should not happen unless the RSSI is a little bit above the level at which a connection is considered to be "bad", to avoid flapping back and forth between wifi and cell. Bug: 64422319 Test: Unit tests, manual Change-Id: I9a148f2d66ea835d19696eb6317b682f6756f002 --- .../server/wifi/WifiConnectivityManager.java | 4 +- .../android/server/wifi/WifiNetworkSelector.java | 10 +-- .../server/wifi/WifiNetworkSelectorTest.java | 95 +++++++++------------- 3 files changed, 46 insertions(+), 63 deletions(-) diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java index 47c77e203..de63b251a 100644 --- a/service/java/com/android/server/wifi/WifiConnectivityManager.java +++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java @@ -559,9 +559,9 @@ public class WifiConnectivityManager { mConnectionAttemptTimeStamps = new LinkedList<>(); mMin5GHzRssi = context.getResources().getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz); + R.integer.config_wifi_framework_wifi_score_entry_rssi_threshold_5GHz); mMin24GHzRssi = context.getResources().getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz); + R.integer.config_wifi_framework_wifi_score_entry_rssi_threshold_24GHz); mBand5GHzBonus = context.getResources().getInteger( R.integer.config_wifi_framework_5GHz_preference_boost_factor); mCurrentConnectionBonus = context.getResources().getInteger( diff --git a/service/java/com/android/server/wifi/WifiNetworkSelector.java b/service/java/com/android/server/wifi/WifiNetworkSelector.java index 57779ad08..8fdf080cc 100644 --- a/service/java/com/android/server/wifi/WifiNetworkSelector.java +++ b/service/java/com/android/server/wifi/WifiNetworkSelector.java @@ -570,15 +570,15 @@ public class WifiNetworkSelector { mLocalLog = localLog; mThresholdQualifiedRssi24 = context.getResources().getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz); + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz); mThresholdQualifiedRssi5 = context.getResources().getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz); + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz); mThresholdMinimumRssi24 = context.getResources().getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz); + R.integer.config_wifi_framework_wifi_score_entry_rssi_threshold_24GHz); mThresholdMinimumRssi5 = context.getResources().getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz); + R.integer.config_wifi_framework_wifi_score_entry_rssi_threshold_5GHz); mEnableAutoJoinWhenAssociated = context.getResources().getBoolean( - R.bool.config_wifi_framework_enable_associated_network_selection); + R.bool.config_wifi_framework_enable_associated_network_selection); mStayOnNetworkMinimumTxRate = context.getResources().getInteger( R.integer.config_wifi_framework_min_tx_rate_for_staying_on_network); mStayOnNetworkMinimumRxRate = context.getResources().getInteger( diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java index 4965a35c2..3d3af364c 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java @@ -23,12 +23,12 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; import android.content.Context; -import android.content.res.Resources; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; import android.net.wifi.WifiInfo; import android.os.SystemClock; +import android.test.mock.MockResources; import android.test.suitebuilder.annotation.SmallTest; import android.util.LocalLog; import android.util.Pair; @@ -41,6 +41,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.mockito.Spy; import java.util.ArrayList; import java.util.HashSet; @@ -52,6 +53,8 @@ import java.util.List; @SmallTest public class WifiNetworkSelectorTest { + private static final int RSSI_BUMP = 1; + /** Sets up test. */ @Before public void setUp() throws Exception { @@ -68,22 +71,6 @@ public class WifiNetworkSelectorTest { mDummyEvaluator.setEvaluatorToSelectCandidate(true); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime()); - mThresholdMinimumRssi2G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz); - mThresholdMinimumRssi5G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz); - mThresholdQualifiedRssi2G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz); - mThresholdQualifiedRssi5G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz); - mStayOnNetworkMinimumTxRate = mResource.getInteger( - R.integer.config_wifi_framework_min_tx_rate_for_staying_on_network); - mStayOnNetworkMinimumRxRate = mResource.getInteger( - R.integer.config_wifi_framework_min_rx_rate_for_staying_on_network); - mThresholdSaturatedRssi2G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz); - mThresholdSaturatedRssi5G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz); } /** Cleans up test. */ @@ -145,7 +132,11 @@ public class WifiNetworkSelectorTest { private DummyNetworkEvaluator mDummyEvaluator = new DummyNetworkEvaluator(); @Mock private WifiConfigManager mWifiConfigManager; @Mock private Context mContext; - @Mock private Resources mResource; + + // For simulating the resources, we use a Spy on a MockResource + // (which is really more of a stub than a mock, in spite if its name). + // This is so that we get errors on any calls that we have not explicitly set up. + @Spy private MockResources mResource = new MockResources(); @Mock private WifiInfo mWifiInfo; @Mock private Clock mClock; private LocalLog mLocalLog; @@ -155,40 +146,32 @@ public class WifiNetworkSelectorTest { private int mThresholdQualifiedRssi5G; private int mStayOnNetworkMinimumTxRate; private int mStayOnNetworkMinimumRxRate; - private int mThresholdSaturatedRssi2G; - private int mThresholdSaturatedRssi5G; private void setupContext() { when(mContext.getResources()).thenReturn(mResource); } + private int setupIntegerResource(int resourceName, int value) { + doReturn(value).when(mResource).getInteger(resourceName); + return value; + } + private void setupResources() { - when(mResource.getBoolean( - R.bool.config_wifi_framework_enable_associated_network_selection)).thenReturn(true); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz)) - .thenReturn(-70); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz)) - .thenReturn(-73); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz)) - .thenReturn(-82); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz)) - .thenReturn(-85); - when(mResource.getInteger( - R.integer.config_wifi_framework_max_tx_rate_for_full_scan)) - .thenReturn(8); - when(mResource.getInteger( - R.integer.config_wifi_framework_max_rx_rate_for_full_scan)) - .thenReturn(8); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz)) - .thenReturn(-57); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz)) - .thenReturn(-60); + doReturn(true).when(mResource).getBoolean( + R.bool.config_wifi_framework_enable_associated_network_selection); + + mThresholdMinimumRssi2G = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_entry_rssi_threshold_24GHz, -79); + mThresholdMinimumRssi5G = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_entry_rssi_threshold_5GHz, -76); + mThresholdQualifiedRssi2G = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz, -73); + mThresholdQualifiedRssi5G = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz, -70); + mStayOnNetworkMinimumTxRate = setupIntegerResource( + R.integer.config_wifi_framework_min_tx_rate_for_staying_on_network, 16); + mStayOnNetworkMinimumRxRate = setupIntegerResource( + R.integer.config_wifi_framework_min_rx_rate_for_staying_on_network, 16); } private void setupWifiInfo() { @@ -279,7 +262,7 @@ public class WifiNetworkSelectorTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2437, 5180}; String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; - int[] levels = {mThresholdMinimumRssi2G + 1, mThresholdMinimumRssi5G + 1}; + int[] levels = {mThresholdMinimumRssi2G + RSSI_BUMP, mThresholdMinimumRssi5G + RSSI_BUMP}; int[] securities = {SECURITY_PSK, SECURITY_PSK}; // Make a network selection. @@ -318,7 +301,7 @@ public class WifiNetworkSelectorTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2437, 5180}; String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; - int[] levels = {mThresholdMinimumRssi2G + 1, mThresholdMinimumRssi5G + 1}; + int[] levels = {mThresholdMinimumRssi2G + RSSI_BUMP, mThresholdMinimumRssi5G + RSSI_BUMP}; int[] securities = {SECURITY_PSK, SECURITY_PSK}; // Make a network selection. @@ -483,7 +466,7 @@ public class WifiNetworkSelectorTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2437, 2457}; String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; - int[] levels = {mThresholdMinimumRssi2G + 20, mThresholdMinimumRssi2G + 1}; + int[] levels = {mThresholdMinimumRssi2G + 20, mThresholdMinimumRssi2G + RSSI_BUMP}; int[] securities = {SECURITY_PSK, SECURITY_PSK}; // Make a network selection to connect to test1. @@ -534,8 +517,8 @@ public class WifiNetworkSelectorTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4", "6c:f3:7f:ae:8c:f5"}; int[] freqs = {2437, 5180, 5181}; String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; - int[] levels = {mThresholdMinimumRssi2G + 1, mThresholdMinimumRssi5G + 1, - mThresholdMinimumRssi5G + 1}; + int[] levels = {mThresholdMinimumRssi2G + RSSI_BUMP, mThresholdMinimumRssi5G + RSSI_BUMP, + mThresholdMinimumRssi5G + RSSI_BUMP}; int[] securities = {SECURITY_PSK, SECURITY_PSK, SECURITY_PSK}; ScanDetailsAndWifiConfigs scanDetailsAndConfigs = @@ -581,7 +564,7 @@ public class WifiNetworkSelectorTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2437, 5180}; String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; - int[] levels = {mThresholdMinimumRssi2G + 1, mThresholdMinimumRssi5G + 1}; + int[] levels = {mThresholdMinimumRssi2G + RSSI_BUMP, mThresholdMinimumRssi5G + RSSI_BUMP}; int[] securities = {SECURITY_PSK, SECURITY_PSK}; ScanDetailsAndWifiConfigs scanDetailsAndConfigs = @@ -961,7 +944,7 @@ public class WifiNetworkSelectorTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2437, 5180}; String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; - int[] levels = {mThresholdMinimumRssi2G + 1, mThresholdMinimumRssi5G + 1}; + int[] levels = {mThresholdMinimumRssi2G + RSSI_BUMP, mThresholdMinimumRssi5G + RSSI_BUMP}; mDummyEvaluator.setEvaluatorToSelectCandidate(false); List scanDetails = WifiNetworkSelectorTestUtil.buildScanDetails( @@ -990,7 +973,7 @@ public class WifiNetworkSelectorTest { String[] bssids = {"6c:f3:7f:ae:8c:f3"}; int[] freqs = {2437, 5180}; String[] caps = {"[ESS]"}; - int[] levels = {mThresholdMinimumRssi2G + 1}; + int[] levels = {mThresholdMinimumRssi2G + RSSI_BUMP}; int[] securities = {SECURITY_NONE}; mDummyEvaluator.setEvaluatorToSelectCandidate(false); @@ -1027,7 +1010,7 @@ public class WifiNetworkSelectorTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2437, 5180}; String[] caps = {"[ESS]", "[ESS]"}; - int[] levels = {mThresholdMinimumRssi2G + 1, mThresholdMinimumRssi5G + 1}; + int[] levels = {mThresholdMinimumRssi2G + RSSI_BUMP, mThresholdMinimumRssi5G + RSSI_BUMP}; mDummyEvaluator.setEvaluatorToSelectCandidate(false); List scanDetails = WifiNetworkSelectorTestUtil.buildScanDetails( @@ -1055,7 +1038,7 @@ public class WifiNetworkSelectorTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2437, 5180}; String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; - int[] levels = {mThresholdMinimumRssi2G + 1, mThresholdMinimumRssi5G + 1}; + int[] levels = {mThresholdMinimumRssi2G + RSSI_BUMP, mThresholdMinimumRssi5G + RSSI_BUMP}; mDummyEvaluator.setEvaluatorToSelectCandidate(false); List scanDetails = WifiNetworkSelectorTestUtil.buildScanDetails( -- cgit v1.2.3