From 9d7489491984e86915b2cf4fac38a882de1c8cdb Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Mon, 8 Feb 2016 16:58:11 -0800 Subject: Disable all networks in supplicant Temporary changes to test if we can disable all the networks in supplicant conf file except the one we want to connect to. Changes: 1. Remove all existing native enableNetwork invocations and replace it with a single selectNetwork call in |WifiConfigStore.selectNetwork|. selectNetwork enables the provided network and disables all others in wpa_supplicant. 2. Enable all networks in wpa_supplicant before enabling PNO. These will be disabled when we come out of PNO and decide to conneect to one of them via selectNetwork. 3. Save the network status in networkHistory.txt and ignore the status stored in wpa_supplicant.conf. 4. Some other cleanups: a. Remove |enableNetworkWithoutBroadcast| definition/usages and replace with |selectNetworkWithoutBroadcast|. b. Add enableAllNetworksNative/disableAllNetworksNative methods to just enable the network in wpa_supplicant. c. Move all wpa_supplicant network enable/disable to a common API. d. Fixed all the unit-test expectations. BUG:26984166 TEST: `mmma frameworks/opt/net/wifi/tests && runtest frameworks-wifi` Change-Id: Ib5a089f054557071ece6be25aad03b6458c0217c --- .../com/android/server/wifi/WifiConfigManager.java | 145 ++++++++++++--------- .../java/com/android/server/wifi/WifiNative.java | 27 ++-- .../com/android/server/wifi/WifiStateMachine.java | 24 ++-- .../android/server/wifi/WifiConfigManagerTest.java | 15 +-- .../android/server/wifi/WifiStateMachineTest.java | 12 +- 5 files changed, 123 insertions(+), 100 deletions(-) diff --git a/service/java/com/android/server/wifi/WifiConfigManager.java b/service/java/com/android/server/wifi/WifiConfigManager.java index fd267d4bd..957456d9a 100644 --- a/service/java/com/android/server/wifi/WifiConfigManager.java +++ b/service/java/com/android/server/wifi/WifiConfigManager.java @@ -257,6 +257,9 @@ public class WifiConfigManager { private static final String CREATION_TIME_KEY = "CREATION_TIME"; private static final String UPDATE_TIME_KEY = "UPDATE_TIME"; static final String SHARED_KEY = "SHARED"; + private static final String NETWORK_SELECTION_STATUS_KEY = "NETWORK_SELECTION_STATUS"; + private static final String NETWORK_SELECTION_DISABLE_REASON_KEY = + "NETWORK_SELECTION_DISABLE_REASON"; private static final String SEPARATOR = ": "; private static final String NL = "\n"; @@ -982,6 +985,39 @@ public class WifiConfigManager { } } + /** + * Enable a network in wpa_supplicant. + */ + boolean enableNetworkNative(WifiConfiguration config) { + if (VDBG) localLog("enableNetworkNative: " + config); + if (!mWifiNative.enableNetwork(config.networkId)) { + loge("Enable network in wpa_supplicant failed on " + config.networkId); + return false; + } + config.status = Status.ENABLED; + return true; + } + + /** + * Enable all networks in wpa_supplicant. + */ + void enableAllNetworksNative() { + if (VDBG) localLog("enableAllNetworksNative"); + boolean networkEnabledStateChanged = false; + for (WifiConfiguration config : mConfiguredNetworks.valuesForCurrentUser()) { + if (config != null && !config.ephemeral + && !config.getNetworkSelectionStatus().isNetworkEnabled()) { + if (enableNetworkNative(config)) { + networkEnabledStateChanged = true; + } + } + } + if (networkEnabledStateChanged) { + mWifiNative.saveConfig(); + sendConfiguredNetworksChangedBroadcast(); + } + } + private boolean setNetworkPriorityNative(int netId, int priority) { return mWifiNative.setNetworkVariable(netId, WifiConfiguration.priorityVarName, Integer.toString(priority)); @@ -1065,14 +1101,12 @@ public class WifiConfigManager { if (updatePriorities) mWifiNative.saveConfig(); - else - mWifiNative.selectNetwork(config.networkId); updateLastConnectUid(config, uid); writeKnownNetworkHistory(); /* Enable the given network while disabling all other networks */ - enableNetworkWithoutBroadcast(config.networkId, true); + selectNetworkWithoutBroadcast(config.networkId); /* Avoid saving the config & sending a broadcast to prevent settings * from displaying a disabled list of networks */ @@ -1123,16 +1157,6 @@ public class WifiConfigManager { if (VDBG) localLogNetwork("WifiConfigManager: saveNetwork got it back netId=", netId); - /* enable a new network */ - if (newNetwork && netId != INVALID_NETWORK_ID) { - if (VDBG) localLogNetwork("WifiConfigManager: will enable netId=", netId); - - mWifiNative.enableNetwork(netId, false); - conf = mConfiguredNetworks.getForCurrentUser(netId); - if (conf != null) - conf.status = Status.ENABLED; - } - conf = mConfiguredNetworks.getForCurrentUser(netId); if (conf != null) { if (!conf.getNetworkSelectionStatus().isNetworkEnabled()) { @@ -1141,7 +1165,6 @@ public class WifiConfigManager { // reenable autojoin, since new information has been provided updateNetworkSelectionStatus(netId, WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLE); - enableNetworkWithoutBroadcast(conf.networkId, false); } if (VDBG) { loge("WifiConfigManager: saveNetwork got config back netId=" @@ -1772,9 +1795,9 @@ public class WifiConfigManager { if (config == null) { return false; } - - boolean ret = enableNetworkWithoutBroadcast(netId, disableOthers); + boolean ret = true; if (disableOthers) { + ret = selectNetworkWithoutBroadcast(netId); if (VDBG) localLogNetwork("enableNetwork(disableOthers=true, uid=" + uid + ") ", netId); updateLastConnectUid(getWifiConfiguration(netId), uid); writeKnownNetworkHistory(); @@ -1794,45 +1817,61 @@ public class WifiConfigManager { return ret; } - boolean enableNetworkWithoutBroadcast(int netId, boolean disableOthers) { + boolean selectNetworkWithoutBroadcast(int netId) { + if (VDBG) localLog("selectNetworkWithoutBroadcast: " + netId); final WifiConfiguration config = mConfiguredNetworks.getForCurrentUser(netId); if (config == null) { return false; } - - boolean ret = mWifiNative.enableNetwork(netId, disableOthers); - + if (!mWifiNative.selectNetwork(netId)) { + loge("Select network in wpa_supplicant failed on " + netId); + return false; + } config.status = Status.ENABLED; + markAllNetworksDisabledExcept(netId); + return true; + } - if (disableOthers) { - markAllNetworksDisabledExcept(netId); + /** + * Disable a network in wpa_supplicant. + */ + boolean disableNetworkNative(WifiConfiguration config) { + if (VDBG) localLog("disableNetworkNative: " + config); + if (!mWifiNative.disableNetwork(config.networkId)) { + loge("Disable network in wpa_supplicant failed on " + config.networkId); + return false; } - return ret; + config.status = Status.DISABLED; + return true; } - void disableAllNetworks() { + /** + * Disable all networks in wpa_supplicant. + */ + void disableAllNetworksNative() { if (VDBG) localLog("disableAllNetworks"); boolean networkDisabled = false; for (WifiConfiguration enabled : mConfiguredNetworks.getEnabledNetworksForCurrentUser()) { - if(mWifiNative.disableNetwork(enabled.networkId)) { + if (disableNetworkNative(enabled)) { networkDisabled = true; - enabled.status = Status.DISABLED; - } else { - loge("Disable network failed on " + enabled.networkId); } } - if (networkDisabled) { sendConfiguredNetworksChangedBroadcast(); } } + /** * Disable a network. Note that there is no saveConfig operation. * @param netId network to be disabled * @return {@code true} if it succeeds, {@code false} otherwise */ boolean disableNetwork(int netId) { - boolean ret = mWifiNative.disableNetwork(netId); + WifiConfiguration config = getWifiConfiguration(netId); + if (config == null) { + return false; + } + boolean ret = disableNetworkNative(config); if (ret) { mWifiStateMachine.registerNetworkDisabled(netId); } @@ -1954,13 +1993,6 @@ public class WifiConfigManager { } return false; } - //enable the network - if (!mWifiNative.enableNetwork(config.networkId, false)) { - localLog("fail to disable network: " + config.SSID + " With reason:" - + WifiConfiguration.NetworkSelectionStatus - .getNetworkDisableReasonString(reason)); - return false; - } networkStatus.setNetworkSelectionStatus(WifiConfiguration.NetworkSelectionStatus .NETWORK_SELECTION_ENABLED); networkStatus.setNetworkSelectionDisableReason(reason); @@ -1996,12 +2028,7 @@ public class WifiConfigManager { } if (networkStatus.isNetworkEnabled()) { - if (!mWifiNative.disableNetwork(config.networkId)) { - localLog("Fail to disable network: " + config.SSID + " With reason:" - + WifiConfiguration.NetworkSelectionStatus - .getNetworkDisableReasonString(reason)); - } - config.status = Status.DISABLED; + disableNetworkNative(config); sendConfiguredNetworksChangedBroadcast(config, WifiManager.CHANGE_REASON_CONFIG_CHANGE); localLog("Disable network " + config.SSID + " reason:" @@ -2218,16 +2245,6 @@ public class WifiConfigManager { loge("Failed to read network-id '" + result[0] + "'"); continue; } - if (result.length > 3) { - if (result[3].indexOf("[CURRENT]") != -1) - config.status = WifiConfiguration.Status.CURRENT; - else if (result[3].indexOf("[DISABLED]") != -1) - config.status = WifiConfiguration.Status.DISABLED; - else - config.status = WifiConfiguration.Status.ENABLED; - } else { - config.status = WifiConfiguration.Status.ENABLED; - } readNetworkVariables(config); @@ -2656,7 +2673,10 @@ public class WifiConfigManager { WifiConfiguration.KeyMgmt.strings); out.writeUTF(AUTH_KEY + SEPARATOR + allowedKeyManagementString + NL); - + out.writeUTF(NETWORK_SELECTION_STATUS_KEY + SEPARATOR + + status.getNetworkSelectionStatus() + NL); + out.writeUTF(NETWORK_SELECTION_DISABLE_REASON_KEY + SEPARATOR + + status.getNetworkSelectionDisableReason() + NL); if (status.getConnectChoice() != null) { out.writeUTF(CHOICE_KEY + SEPARATOR + status.getConnectChoice() + NL); @@ -2734,9 +2754,6 @@ public class WifiConfigManager { } else { lastSelectedConfiguration = selected.configKey(); mLastSelectedTimeStamp = System.currentTimeMillis(); - if (selected.status == Status.DISABLED) { - mWifiNative.enableNetwork(netId, false); - } updateNetworkSelectionStatus(netId, WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLE); if (VDBG) { @@ -2905,6 +2922,12 @@ public class WifiConfigManager { case PEER_CONFIGURATION_KEY: config.peerWifiConfiguration = value; break; + case NETWORK_SELECTION_STATUS_KEY: + networkStatus.setNetworkSelectionStatus(Integer.parseInt(value)); + break; + case NETWORK_SELECTION_DISABLE_REASON_KEY: + networkStatus.setNetworkSelectionDisableReason(Integer.parseInt(value)); + break; case CHOICE_KEY: networkStatus.setConnectChoice(value); break; @@ -4068,13 +4091,7 @@ public class WifiConfigManager { final List hiddenConfigurations = mConfiguredNetworks.handleUserSwitch(mWifiStateMachine.getCurrentUserId()); for (WifiConfiguration network : hiddenConfigurations) { - if (mWifiNative.disableNetwork(network.networkId)) { - network.status = Status.DISABLED; - } - } - - for (WifiConfiguration config : mConfiguredNetworks.valuesForCurrentUser()) { - enableNetworkWithoutBroadcast(config.networkId, false); + disableNetworkNative(network); } enableAllNetworks(); diff --git a/service/java/com/android/server/wifi/WifiNative.java b/service/java/com/android/server/wifi/WifiNative.java index bc27275d7..e9d59f9a7 100644 --- a/service/java/com/android/server/wifi/WifiNative.java +++ b/service/java/com/android/server/wifi/WifiNative.java @@ -460,21 +460,32 @@ public class WifiNative { + Thread.currentThread().getStackTrace()[6].getMethodName()); } - public boolean enableNetwork(int netId, boolean disableOthers) { - if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId) - + " disableOthers=" + disableOthers); - if (disableOthers) { - return doBooleanCommand("SELECT_NETWORK " + netId); - } else { - return doBooleanCommand("ENABLE_NETWORK " + netId); - } + + /** + * Enables a network in wpa_supplicant. + * @param netId - Network ID of the network to be enabled. + * @return true if command succeeded, false otherwise. + */ + public boolean enableNetwork(int netId) { + if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId)); + return doBooleanCommand("ENABLE_NETWORK " + netId); } + /** + * Disables a network in wpa_supplicant. + * @param netId - Network ID of the network to be disabled. + * @return true if command succeeded, false otherwise. + */ public boolean disableNetwork(int netId) { if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId)); return doBooleanCommand("DISABLE_NETWORK " + netId); } + /** + * Select a network in wpa_supplicant (Disables all others). + * @param netId - Network ID of the network to be selected. + * @return true if command succeeded, false otherwise. + */ public boolean selectNetwork(int netId) { if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId)); return doBooleanCommand("SELECT_NETWORK " + netId); diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java index be069983f..58a9edb8a 100644 --- a/service/java/com/android/server/wifi/WifiStateMachine.java +++ b/service/java/com/android/server/wifi/WifiStateMachine.java @@ -2485,6 +2485,9 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiPno void enableBackgroundScan(boolean enable) { if (enable) { mWifiConfigManager.enableAllNetworks(); + // Now enable all the networks in wpa_supplicant. These will be + // disabled when we connect to a network after PNO. + mWifiConfigManager.enableAllNetworksNative(); } List pnoList = mWifiConfigManager.retrieveDisconnectedWifiPnoNetworkList(enable); @@ -5920,7 +5923,7 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiPno if (mOperationalMode != CONNECT_MODE) { mWifiNative.disconnect(); - mWifiConfigManager.disableAllNetworks(); + mWifiConfigManager.disableAllNetworksNative(); if (mOperationalMode == SCAN_ONLY_WITH_WIFI_OFF_MODE) { setWifiState(WIFI_STATE_DISABLED); } @@ -6014,7 +6017,7 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiPno int mode = message.arg1; log("stop driver"); - mWifiConfigManager.disableAllNetworks(); + mWifiConfigManager.disableAllNetworksNative(); if (getCurrentState() != mDisconnectedState) { mWifiNative.disconnect(); @@ -6663,8 +6666,10 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiPno } else { WifiConfiguration curConfig = getCurrentWifiConfiguration(); if (curConfig != null && config != null) { - if (curConfig.priority < config.priority - && config.status == WifiConfiguration.Status.ENABLED) { + WifiConfiguration.NetworkSelectionStatus networkStatus = + config.getNetworkSelectionStatus(); + if (curConfig.priority < config.priority && networkStatus != null + && !networkStatus.isNetworkPermanentlyDisabled()) { // Interpret this as a connect attempt // Set the last selected configuration so as to allow the system to // stick the last user choice without persisting the choice @@ -6974,9 +6979,6 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiPno break; } - // Make sure the network is enabled, since supplicant will not reenable it - mWifiConfigManager.enableNetworkWithoutBroadcast(netId, false); - // If we're autojoining a network that the user or an app explicitly selected, // keep track of the UID that selected it. // TODO(b/26786318): Keep track of the lastSelectedConfiguration and the @@ -7159,9 +7161,6 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiPno mWifiNative.disconnect(); } - // Make sure the network is enabled, since supplicant will not reenable it - mWifiConfigManager.enableNetworkWithoutBroadcast(netId, false); - if (mWifiConfigManager.selectNetwork(config, /* updatePriorities = */ true, message.sendingUid) && mWifiNative.reconnect()) { lastConnectAttemptTimestamp = System.currentTimeMillis(); @@ -8574,8 +8573,6 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiPno setTargetBssid(config, bssid); mTargetNetworkId = netId; - // Make sure the network is enabled, since supplicant will not re-enable it - mWifiConfigManager.enableNetworkWithoutBroadcast(netId, false); if (deferForUserInput(message, netId, false)) { break; @@ -8811,8 +8808,7 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiPno case CMD_SET_OPERATIONAL_MODE: if (message.arg1 != CONNECT_MODE) { mOperationalMode = message.arg1; - - mWifiConfigManager.disableAllNetworks(); + mWifiConfigManager.disableAllNetworksNative(); if (mOperationalMode == SCAN_ONLY_WITH_WIFI_OFF_MODE) { mWifiP2pChannel.sendMessage(CMD_DISABLE_P2P_REQ); setWifiState(WIFI_STATE_DISABLED); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java index e690d6091..96790f78c 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java @@ -23,7 +23,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.mockito.Mockito.anyBoolean; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.anyObject; import static org.mockito.Mockito.anyString; @@ -363,7 +362,6 @@ public class WifiConfigManagerTest { @Test public void testEnableAllNetworks() throws Exception { addNetworks(); - when(mWifiNative.enableNetwork(anyInt(), anyBoolean())).thenReturn(true); for (int userId : USER_IDS) { switchUser(userId); @@ -406,6 +404,7 @@ public class WifiConfigManagerTest { // Try to select a network configuration. final WifiNative wifiNative = createNewWifiNativeMock(); + when(wifiNative.selectNetwork(config.networkId)).thenReturn(true); final boolean success = mConfigStore.selectNetwork(config, false, config.creatorUid); if (!WifiConfigurationUtil.isVisibleToAnyProfile(config, @@ -414,7 +413,7 @@ public class WifiConfigManagerTest { // nothing changed. assertFalse(success); verify(wifiNative, never()).selectNetwork(anyInt()); - verify(wifiNative, never()).enableNetwork(anyInt(), anyBoolean()); + verify(wifiNative, never()).enableNetwork(anyInt()); for (WifiConfiguration config2 : mConfiguredNetworks.valuesForAllUsers()) { assertEquals(WifiConfiguration.Status.ENABLED, config2.status); } @@ -425,10 +424,8 @@ public class WifiConfigManagerTest { assertTrue(success); verify(wifiNative).selectNetwork(config.networkId); verify(wifiNative, never()).selectNetwork(intThat(not(config.networkId))); - verify(wifiNative).enableNetwork(config.networkId, true); - verify(wifiNative, never()).enableNetwork(config.networkId, false); - verify(wifiNative, never()).enableNetwork(intThat(not(config.networkId)), - anyBoolean()); + verify(wifiNative, never()).enableNetwork(config.networkId); + verify(wifiNative, never()).enableNetwork(intThat(not(config.networkId))); for (WifiConfiguration config2 : mConfiguredNetworks.valuesForAllUsers()) { if (WifiConfigurationUtil.isVisibleToAnyProfile(config2, USER_PROFILES.get(userId)) @@ -739,8 +736,10 @@ public class WifiConfigManagerTest { if (neitherUserConfigs.contains(config)) { assertEquals(WifiConfiguration.Status.DISABLED, config.status); } else { - assertEquals(WifiConfiguration.Status.ENABLED, config.status); + // Only enabled in networkSelection. + assertTrue(config.getNetworkSelectionStatus().isNetworkEnabled()); } + } } } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java index c50e1da9b..332a3a3ef 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java @@ -616,13 +616,13 @@ public class WifiStateMachineTest { } private void enableNetworkAndVerifySuccess() throws Exception { - when(mWifiNative.enableNetwork(0, true)).thenReturn(true); + when(mWifiNative.selectNetwork(0)).thenReturn(true); mLooper.startAutoDispatch(); assertTrue(mWsm.syncEnableNetwork(mWsmAsyncChannel, 0, true)); mLooper.stopAutoDispatch(); - verify(mWifiNative).enableNetwork(0, true); + verify(mWifiNative).selectNetwork(0); } private void enableNetworkAndVerifyFailure() throws Exception { @@ -630,7 +630,7 @@ public class WifiStateMachineTest { assertFalse(mWsm.syncEnableNetwork(mWsmAsyncChannel, 0, true)); mLooper.stopAutoDispatch(); - verify(mWifiNative, never()).enableNetwork(anyInt(), anyBoolean()); + verify(mWifiNative, never()).selectNetwork(anyInt()); } /** @@ -748,7 +748,7 @@ public class WifiStateMachineTest { mWsm.syncEnableNetwork(mWsmAsyncChannel, 0, true); mLooper.stopAutoDispatch(); - verify(mWifiNative).enableNetwork(0, true); + verify(mWifiNative).selectNetwork(0); mWsm.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID); mLooper.dispatchAll(); @@ -783,7 +783,7 @@ public class WifiStateMachineTest { mWsm.syncEnableNetwork(mWsmAsyncChannel, 0, true); mLooper.stopAutoDispatch(); - verify(mWifiNative).enableNetwork(0, true); + verify(mWifiNative).selectNetwork(0); mWsm.sendMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, sBSSID); mLooper.dispatchAll(); @@ -812,7 +812,7 @@ public class WifiStateMachineTest { mWsm.syncEnableNetwork(mWsmAsyncChannel, 0, true); mLooper.stopAutoDispatch(); - verify(mWifiNative).enableNetwork(0, true); + verify(mWifiNative).selectNetwork(0); mWsm.sendMessage(WifiMonitor.NETWORK_DISCONNECTION_EVENT, 0, 0, sBSSID); mLooper.dispatchAll(); -- cgit v1.2.3