summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandy Pan <zpan@google.com>2016-05-25 00:20:28 +0000
committerandroid-build-merger <android-build-merger@google.com>2016-05-25 00:20:28 +0000
commit28eb5a8fe0851fb468f302aa8684dc0d3ada739e (patch)
tree58cd179b29e2dd821a82135afefa6fe44bd843fa
parent252a57a58d7173cc379ad18ea2942225c5a2cdbb (diff)
parentc2963eb07660a06592c60224279685166390217d (diff)
WifiConnectivityManager: enforce minimum single scan interval
am: c2963eb076 * commit 'c2963eb07660a06592c60224279685166390217d': WifiConnectivityManager: enforce minimum single scan interval Change-Id: I8e95d793334a7d4df96d3e1c61d05b12e226e5e8
-rw-r--r--service/java/com/android/server/wifi/WifiConnectivityManager.java15
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java88
2 files changed, 95 insertions, 8 deletions
diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java
index d99b65f8f..3071fbf12 100644
--- a/service/java/com/android/server/wifi/WifiConnectivityManager.java
+++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java
@@ -65,6 +65,7 @@ public class WifiConnectivityManager {
"WifiConnectivityManager Restart Scan";
private static final String TAG = "WifiConnectivityManager";
+ private static final long INVALID_TIME_STAMP = Long.MIN_VALUE;
// Periodic scan interval in milli-seconds. This is the scan
// performed when screen is on.
@VisibleForTesting
@@ -142,6 +143,7 @@ public class WifiConnectivityManager {
private int mTotalConnectivityAttemptsRateLimited = 0;
private String mLastConnectionAttemptBssid = null;
private int mPeriodicSingleScanInterval = PERIODIC_SCAN_INTERVAL_MS;
+ private long mLastPeriodicSingleScanTimeStamp = INVALID_TIME_STAMP;
// PNO settings
private int mMin5GHzRssi;
@@ -662,6 +664,18 @@ public class WifiConnectivityManager {
// Start a single scan and set up the interval for next single scan.
private void startPeriodicSingleScan() {
+ long currentTimeStamp = mClock.elapsedRealtime();
+
+ if (mLastPeriodicSingleScanTimeStamp != INVALID_TIME_STAMP) {
+ long msSinceLastScan = currentTimeStamp - mLastPeriodicSingleScanTimeStamp;
+ if (msSinceLastScan < PERIODIC_SCAN_INTERVAL_MS) {
+ localLog("Last periodic single scan started " + msSinceLastScan
+ + "ms ago, defer this new scan request.");
+ schedulePeriodicScanTimer(PERIODIC_SCAN_INTERVAL_MS - (int) msSinceLastScan);
+ return;
+ }
+ }
+
boolean isFullBandScan = true;
// If the WiFi traffic is heavy, only partial scan is initiated.
@@ -676,6 +690,7 @@ public class WifiConnectivityManager {
isFullBandScan = false;
}
+ mLastPeriodicSingleScanTimeStamp = currentTimeStamp;
startSingleScan(false, isFullBandScan);
schedulePeriodicScanTimer(mPeriodicSingleScanInterval);
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
index a2379330c..aa48cadf5 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
@@ -533,11 +533,17 @@ public class WifiConnectivityManagerTest {
*/
@Test
public void checkPeriodicScanIntervalWhenDisconnected() {
- when(mClock.elapsedRealtime()).thenReturn(CURRENT_SYSTEM_TIME_MS);
+ long currentTimeStamp = CURRENT_SYSTEM_TIME_MS;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
// Set screen to ON
mWifiConnectivityManager.handleScreenStateChanged(true);
+ // Wait for MAX_PERIODIC_SCAN_INTERVAL_MS so that any impact triggered
+ // by screen state change can settle
+ currentTimeStamp += WifiConnectivityManager.MAX_PERIODIC_SCAN_INTERVAL_MS;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
+
// Set WiFi to disconnected state to trigger periodic scan
mWifiConnectivityManager.handleConnectionStateChanged(
WifiConnectivityManager.WIFI_STATE_DISCONNECTED);
@@ -545,9 +551,12 @@ public class WifiConnectivityManagerTest {
// Get the first periodic scan interval
long firstIntervalMs = mAlarmManager
.getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG)
- - CURRENT_SYSTEM_TIME_MS;
+ - currentTimeStamp;
assertEquals(firstIntervalMs, WifiConnectivityManager.PERIODIC_SCAN_INTERVAL_MS);
+ currentTimeStamp += firstIntervalMs;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
+
// Now fire the first periodic scan alarm timer
mAlarmManager.dispatch(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG);
mLooper.dispatchAll();
@@ -555,11 +564,14 @@ public class WifiConnectivityManagerTest {
// Get the second periodic scan interval
long secondIntervalMs = mAlarmManager
.getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG)
- - CURRENT_SYSTEM_TIME_MS;
+ - currentTimeStamp;
// Verify the intervals are exponential back off
assertEquals(firstIntervalMs * 2, secondIntervalMs);
+ currentTimeStamp += secondIntervalMs;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
+
// Make sure we eventually stay at the maximum scan interval.
long intervalMs = 0;
for (int i = 0; i < 5; i++) {
@@ -567,7 +579,9 @@ public class WifiConnectivityManagerTest {
mLooper.dispatchAll();
intervalMs = mAlarmManager
.getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG)
- - CURRENT_SYSTEM_TIME_MS;
+ - currentTimeStamp;
+ currentTimeStamp += intervalMs;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
}
assertEquals(intervalMs, WifiConnectivityManager.MAX_PERIODIC_SCAN_INTERVAL_MS);
@@ -582,11 +596,17 @@ public class WifiConnectivityManagerTest {
*/
@Test
public void checkPeriodicScanIntervalWhenConnected() {
- when(mClock.elapsedRealtime()).thenReturn(CURRENT_SYSTEM_TIME_MS);
+ long currentTimeStamp = CURRENT_SYSTEM_TIME_MS;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
// Set screen to ON
mWifiConnectivityManager.handleScreenStateChanged(true);
+ // Wait for MAX_PERIODIC_SCAN_INTERVAL_MS so that any impact triggered
+ // by screen state change can settle
+ currentTimeStamp += WifiConnectivityManager.MAX_PERIODIC_SCAN_INTERVAL_MS;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
+
// Set WiFi to connected state to trigger periodic scan
mWifiConnectivityManager.handleConnectionStateChanged(
WifiConnectivityManager.WIFI_STATE_CONNECTED);
@@ -594,9 +614,12 @@ public class WifiConnectivityManagerTest {
// Get the first periodic scan interval
long firstIntervalMs = mAlarmManager
.getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG)
- - CURRENT_SYSTEM_TIME_MS;
+ - currentTimeStamp;
assertEquals(firstIntervalMs, WifiConnectivityManager.PERIODIC_SCAN_INTERVAL_MS);
+ currentTimeStamp += firstIntervalMs;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
+
// Now fire the first periodic scan alarm timer
mAlarmManager.dispatch(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG);
mLooper.dispatchAll();
@@ -604,11 +627,14 @@ public class WifiConnectivityManagerTest {
// Get the second periodic scan interval
long secondIntervalMs = mAlarmManager
.getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG)
- - CURRENT_SYSTEM_TIME_MS;
+ - currentTimeStamp;
// Verify the intervals are exponential back off
assertEquals(firstIntervalMs * 2, secondIntervalMs);
+ currentTimeStamp += secondIntervalMs;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
+
// Make sure we eventually stay at the maximum scan interval.
long intervalMs = 0;
for (int i = 0; i < 5; i++) {
@@ -616,13 +642,59 @@ public class WifiConnectivityManagerTest {
mLooper.dispatchAll();
intervalMs = mAlarmManager
.getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG)
- - CURRENT_SYSTEM_TIME_MS;
+ - currentTimeStamp;
+ currentTimeStamp += intervalMs;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
}
assertEquals(intervalMs, WifiConnectivityManager.MAX_PERIODIC_SCAN_INTERVAL_MS);
}
/**
+ * When screen on trigger two connection state change events back to back to
+ * verify that the minium scan interval is enforced.
+ *
+ * Expected behavior: WifiConnectivityManager start the second periodic single
+ * scan PERIODIC_SCAN_INTERVAL_MS after the first one.
+ */
+ @Test
+ public void checkMinimumPeriodicScanIntervalWhenScreenOn() {
+ long currentTimeStamp = CURRENT_SYSTEM_TIME_MS;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
+
+ // Set screen to ON
+ mWifiConnectivityManager.handleScreenStateChanged(true);
+
+ // Wait for MAX_PERIODIC_SCAN_INTERVAL_MS so that any impact triggered
+ // by screen state change can settle
+ currentTimeStamp += WifiConnectivityManager.MAX_PERIODIC_SCAN_INTERVAL_MS;
+ long firstScanTimeStamp = currentTimeStamp;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
+
+ // Set WiFi to connected state to trigger the periodic scan
+ mWifiConnectivityManager.handleConnectionStateChanged(
+ WifiConnectivityManager.WIFI_STATE_CONNECTED);
+
+ // Set the second scan attempt time stamp.
+ currentTimeStamp += 2000;
+ when(mClock.elapsedRealtime()).thenReturn(currentTimeStamp);
+
+ // Set WiFi to disconnected state to trigger another periodic scan
+ mWifiConnectivityManager.handleConnectionStateChanged(
+ WifiConnectivityManager.WIFI_STATE_DISCONNECTED);
+
+ // Get the second periodic scan actual time stamp
+ long secondScanTimeStamp = mAlarmManager
+ .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG);
+
+ // Verify that the second scan is scheduled PERIODIC_SCAN_INTERVAL_MS after the
+ // very first scan.
+ assertEquals(secondScanTimeStamp, firstScanTimeStamp
+ + WifiConnectivityManager.PERIODIC_SCAN_INTERVAL_MS);
+
+ }
+
+ /**
* Verify that we perform full band scan when the currently connected network's tx/rx success
* rate is low.
*