diff options
author | James Mattis <jmattis@google.com> | 2019-10-04 18:19:07 -0700 |
---|---|---|
committer | James Mattis <jmattis@google.com> | 2019-11-12 23:12:42 -0800 |
commit | f21ef6230e1695ceea90b5ac6f28fe0076179134 (patch) | |
tree | 0b2b48e56ead72aaffd0057de8cecc351bc7677d | |
parent | 14ecf80d80dc4c3072770a04f2646f96e48d1b0a (diff) |
Support for hotspot client visibility.
Adding support for visibility into clients that connect to an active hotspot.
Bug: 137309578
Test: atest FrameworksWifiTests:com.android.server.wifi.ActiveModeWardenTest
atest FrameworksWifiTests:com.android.server.wifi.SoftApManagerTest
atest FrameworksWifiTests:com.android.server.wifi.WifiServiceImplTest
atest FrameworksWifiTests:com.android.server.wifi.WificondControlTest
Tested manually on Pixel 3.
Change-Id: Ifab5be1c969106916412617043074c7b5bc9a2d1
Merged-In: Ifab5be1c969106916412617043074c7b5bc9a2d1
10 files changed, 242 insertions, 121 deletions
diff --git a/service/Android.mk b/service/Android.mk index b396214c7..6ebc194ce 100644 --- a/service/Android.mk +++ b/service/Android.mk @@ -28,7 +28,8 @@ LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/java $(wificond_aidl_path) LOCAL_SRC_FILES := $(call all-java-files-under, java) \ $(call all-Iaidl-files-under, java) \ $(call all-Iaidl-files-under, $(wificond_aidl_rel_path)) \ - $(call all-logtags-files-under, java) + $(call all-logtags-files-under, java) \ + $(wificond_aidl_rel_path)/com/android/server/wifi/wificond/NativeWifiClient.aidl LOCAL_JAVA_LIBRARIES := \ android.hidl.manager-V1.2-java \ diff --git a/service/java/com/android/server/wifi/ActiveModeWarden.java b/service/java/com/android/server/wifi/ActiveModeWarden.java index d118d833a..7894eda32 100644 --- a/service/java/com/android/server/wifi/ActiveModeWarden.java +++ b/service/java/com/android/server/wifi/ActiveModeWarden.java @@ -18,6 +18,7 @@ package com.android.server.wifi; import android.annotation.NonNull; import android.content.Context; +import android.net.wifi.WifiClient; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.os.BatteryStats; @@ -36,6 +37,7 @@ import com.android.server.wifi.WifiNative.StatusListener; import java.io.FileDescriptor; import java.io.PrintWriter; +import java.util.List; /** * This class provides the implementation for different WiFi operating modes. @@ -578,11 +580,11 @@ public class ActiveModeWarden { } @Override - public void onNumClientsChanged(int numClients) { + public void onConnectedClientsChanged(List<WifiClient> clients) { if (mSoftApCallback == null) { - Log.d(TAG, "SoftApCallback is null. Dropping NumClientsChanged event."); + Log.d(TAG, "SoftApCallback is null. Dropping ConnectedClientsChanged event."); } else if (mMode == WifiManager.IFACE_IP_MODE_TETHERED) { - mSoftApCallback.onNumClientsChanged(numClients); + mSoftApCallback.onConnectedClientsChanged(clients); } } } diff --git a/service/java/com/android/server/wifi/SoftApManager.java b/service/java/com/android/server/wifi/SoftApManager.java index b92dab78d..9e85dfeaa 100644 --- a/service/java/com/android/server/wifi/SoftApManager.java +++ b/service/java/com/android/server/wifi/SoftApManager.java @@ -24,7 +24,9 @@ import android.annotation.NonNull; import android.content.Context; import android.content.Intent; import android.database.ContentObserver; +import android.net.MacAddress; import android.net.wifi.ScanResult; +import android.net.wifi.WifiClient; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.os.Handler; @@ -45,9 +47,12 @@ import com.android.internal.util.WakeupMessage; import com.android.server.wifi.WifiNative.InterfaceCallback; import com.android.server.wifi.WifiNative.SoftApListener; import com.android.server.wifi.util.ApConfigUtil; +import com.android.server.wifi.wificond.NativeWifiClient; import java.io.FileDescriptor; import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; /** @@ -88,7 +93,7 @@ public class SoftApManager implements ActiveModeManager { private int mReportedFrequency = -1; private int mReportedBandwidth = -1; - private int mNumAssociatedStations = 0; + private List<WifiClient> mConnectedClients = new ArrayList<>(); private boolean mTimeoutEnabled = false; private final SarManager mSarManager; @@ -106,9 +111,9 @@ public class SoftApManager implements ActiveModeManager { } @Override - public void onNumAssociatedStationsChanged(int numStations) { - mStateMachine.sendMessage( - SoftApStateMachine.CMD_NUM_ASSOCIATED_STATIONS_CHANGED, numStations); + public void onConnectedClientsChanged(List<NativeWifiClient> clients) { + mStateMachine.sendMessage(SoftApStateMachine.CMD_ASSOCIATED_STATIONS_CHANGED, + clients); } @Override @@ -196,7 +201,7 @@ public class SoftApManager implements ActiveModeManager { } else { pw.println("mApConfig: null"); } - pw.println("mNumAssociatedStations: " + mNumAssociatedStations); + pw.println("mConnectedClients.size(): " + mConnectedClients.size()); pw.println("mTimeoutEnabled: " + mTimeoutEnabled); pw.println("mReportedFrequency: " + mReportedFrequency); pw.println("mReportedBandwidth: " + mReportedBandwidth); @@ -306,7 +311,7 @@ public class SoftApManager implements ActiveModeManager { public static final int CMD_START = 0; public static final int CMD_FAILURE = 2; public static final int CMD_INTERFACE_STATUS_CHANGED = 3; - public static final int CMD_NUM_ASSOCIATED_STATIONS_CHANGED = 4; + public static final int CMD_ASSOCIATED_STATIONS_CHANGED = 4; public static final int CMD_NO_ASSOCIATED_STATIONS_TIMEOUT = 5; public static final int CMD_TIMEOUT_TOGGLE_CHANGED = 6; public static final int CMD_INTERFACE_DESTROYED = 7; @@ -404,8 +409,8 @@ public class SoftApManager implements ActiveModeManager { private SoftApTimeoutEnabledSettingObserver mSettingObserver; /** - * Observer for timeout settings changes. - */ + * Observer for timeout settings changes. + */ private class SoftApTimeoutEnabledSettingObserver extends ContentObserver { SoftApTimeoutEnabledSettingObserver(Handler handler) { super(handler); @@ -461,35 +466,60 @@ public class SoftApManager implements ActiveModeManager { } /** - * Set number of stations associated with this soft AP - * @param numStations Number of connected stations + * Set stations associated with this soft AP + * @param clients The connected stations */ - private void setNumAssociatedStations(int numStations) { - if (mNumAssociatedStations == numStations) { + private void setConnectedClients(List<NativeWifiClient> clients) { + if (clients == null) { return; } - mNumAssociatedStations = numStations; - Log.d(TAG, "Number of associated stations changed: " + mNumAssociatedStations); + + List<WifiClient> convertedClients = createWifiClients(clients); + if (mConnectedClients.equals(convertedClients)) { + return; + } + + mConnectedClients = new ArrayList<>(convertedClients); + Log.d(TAG, "The connected wifi stations have changed with count: " + + clients.size()); if (mCallback != null) { - mCallback.onNumClientsChanged(mNumAssociatedStations); + mCallback.onConnectedClientsChanged(mConnectedClients); } else { - Log.e(TAG, "SoftApCallback is null. Dropping NumClientsChanged event."); + Log.e(TAG, + "SoftApCallback is null. Dropping ConnectedClientsChanged event." + ); } - mWifiMetrics.addSoftApNumAssociatedStationsChangedEvent(mNumAssociatedStations, + mWifiMetrics.addSoftApNumAssociatedStationsChangedEvent(mConnectedClients.size(), mMode); - if (mNumAssociatedStations == 0) { + if (mConnectedClients.size() == 0) { scheduleTimeoutMessage(); } else { cancelTimeoutMessage(); } } + private List<WifiClient> createWifiClients(List<NativeWifiClient> nativeClients) { + List<WifiClient> clients = new ArrayList<>(); + if (nativeClients == null || nativeClients.size() == 0) { + return clients; + } + + for (int i = 0; i < nativeClients.size(); i++) { + MacAddress macAddress = MacAddress.fromBytes(nativeClients.get(i).macAddress); + WifiClient client = new WifiClient(macAddress); + clients.add(client); + } + + return clients; + } + private void onUpChanged(boolean isUp) { if (isUp == mIfaceIsUp) { return; // no change } + mIfaceIsUp = isUp; if (isUp) { Log.d(TAG, "SoftAp is ready for use"); @@ -497,7 +527,7 @@ public class SoftApManager implements ActiveModeManager { WifiManager.WIFI_AP_STATE_ENABLING, 0); mWifiMetrics.incrementSoftApStartResult(true, 0); if (mCallback != null) { - mCallback.onNumClientsChanged(mNumAssociatedStations); + mCallback.onConnectedClientsChanged(mConnectedClients); } } else { // the interface was up, but goes down @@ -525,8 +555,8 @@ public class SoftApManager implements ActiveModeManager { mSarManager.setSapWifiState(WifiManager.WIFI_AP_STATE_ENABLED); - Log.d(TAG, "Resetting num stations on start"); - mNumAssociatedStations = 0; + Log.d(TAG, "Resetting connected clients on start"); + mConnectedClients = new ArrayList<>(); scheduleTimeoutMessage(); } @@ -540,7 +570,7 @@ public class SoftApManager implements ActiveModeManager { mSettingObserver.unregister(); } Log.d(TAG, "Resetting num stations on stop"); - setNumAssociatedStations(0); + setConnectedClients(new ArrayList<>()); cancelTimeoutMessage(); // Need this here since we are exiting |Started| state and won't handle any // future CMD_INTERFACE_STATUS_CHANGED events after this point @@ -574,13 +604,16 @@ public class SoftApManager implements ActiveModeManager { @Override public boolean processMessage(Message message) { switch (message.what) { - case CMD_NUM_ASSOCIATED_STATIONS_CHANGED: - if (message.arg1 < 0) { - Log.e(TAG, "Invalid number of associated stations: " + message.arg1); + case CMD_ASSOCIATED_STATIONS_CHANGED: + if (!(message.obj instanceof List)) { + Log.e(TAG, "Invalid type returned for" + + " CMD_ASSOCIATED_STATIONS_CHANGED"); break; } - Log.d(TAG, "Setting num stations on CMD_NUM_ASSOCIATED_STATIONS_CHANGED"); - setNumAssociatedStations(message.arg1); + + Log.d(TAG, "Setting connected stations on" + + " CMD_ASSOCIATED_STATIONS_CHANGED"); + setConnectedClients((List<NativeWifiClient>) message.obj); break; case CMD_SOFT_AP_CHANNEL_SWITCHED: mReportedFrequency = message.arg1; @@ -600,7 +633,7 @@ public class SoftApManager implements ActiveModeManager { if (!mTimeoutEnabled) { cancelTimeoutMessage(); } - if (mTimeoutEnabled && mNumAssociatedStations == 0) { + if (mTimeoutEnabled && mConnectedClients.size() == 0) { scheduleTimeoutMessage(); } break; @@ -617,7 +650,7 @@ public class SoftApManager implements ActiveModeManager { + " Dropping."); break; } - if (mNumAssociatedStations != 0) { + if (mConnectedClients.size() != 0) { Log.wtf(TAG, "Timeout message received but has clients. Dropping."); break; } diff --git a/service/java/com/android/server/wifi/WifiNative.java b/service/java/com/android/server/wifi/WifiNative.java index 3e5dc3c79..e010438fb 100644 --- a/service/java/com/android/server/wifi/WifiNative.java +++ b/service/java/com/android/server/wifi/WifiNative.java @@ -38,6 +38,7 @@ import com.android.internal.util.HexDump; import com.android.server.net.BaseNetworkObserver; import com.android.server.wifi.util.FrameParser; import com.android.server.wifi.util.NativeUtil; +import com.android.server.wifi.wificond.NativeWifiClient; import java.io.PrintWriter; import java.io.StringWriter; @@ -1522,9 +1523,9 @@ public class WifiNative { void onFailure(); /** - * Invoked when the number of associated stations changes. + * Invoked when the associated stations changes. */ - void onNumAssociatedStationsChanged(int numStations); + void onConnectedClientsChanged(List<NativeWifiClient> clients); /** * Invoked when the channel switch event happens. diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index 685bb479e..e2ad88702 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -69,6 +69,7 @@ import android.net.wifi.ISoftApCallback; import android.net.wifi.ITrafficStateCallback; import android.net.wifi.ScanResult; import android.net.wifi.WifiActivityEnergyInfo; +import android.net.wifi.WifiClient; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; @@ -228,7 +229,7 @@ public class WifiServiceImpl extends BaseWifiService { // mWifiApState and mWifiApNumClients, to match the constants (i.e. WIFI_AP_STATE_*) private int mWifiApState = WifiManager.WIFI_AP_STATE_DISABLED; private int mSoftApState = WifiManager.WIFI_AP_STATE_DISABLED; - private int mSoftApNumClients = 0; + private List<WifiClient> mTetheredSoftApConnectedClients = new ArrayList<>(); /** * Power profile @@ -1163,22 +1164,22 @@ public class WifiServiceImpl extends BaseWifiService { } /** - * Called when number of connected clients to soft AP changes. + * Called when the connected clients to soft AP changes. * - * @param numClients number of connected clients to soft AP + * @param clients connected clients to soft AP */ @Override - public void onNumClientsChanged(int numClients) { - mSoftApNumClients = numClients; + public void onConnectedClientsChanged(List<WifiClient> clients) { + mTetheredSoftApConnectedClients = new ArrayList<>(clients); Iterator<ISoftApCallback> iterator = mRegisteredSoftApCallbacks.getCallbacks().iterator(); while (iterator.hasNext()) { ISoftApCallback callback = iterator.next(); try { - callback.onNumClientsChanged(numClients); + callback.onConnectedClientsChanged(mTetheredSoftApConnectedClients); } catch (RemoteException e) { - Log.e(TAG, "onNumClientsChanged: remote exception -- " + e); + Log.e(TAG, "onConnectedClientsChanged: remote exception -- " + e); iterator.remove(); } } @@ -1222,7 +1223,7 @@ public class WifiServiceImpl extends BaseWifiService { // Update the client about the current state immediately after registering the callback try { callback.onStateChanged(mSoftApState, 0); - callback.onNumClientsChanged(mSoftApNumClients); + callback.onConnectedClientsChanged(mTetheredSoftApConnectedClients); } catch (RemoteException e) { Log.e(TAG, "registerSoftApCallback: remote exception -- " + e); } diff --git a/service/java/com/android/server/wifi/WificondControl.java b/service/java/com/android/server/wifi/WificondControl.java index bd756f643..08fc56375 100644 --- a/service/java/com/android/server/wifi/WificondControl.java +++ b/service/java/com/android/server/wifi/WificondControl.java @@ -47,12 +47,14 @@ import com.android.server.wifi.util.ScanResultUtil; import com.android.server.wifi.wificond.ChannelSettings; import com.android.server.wifi.wificond.HiddenNetwork; import com.android.server.wifi.wificond.NativeScanResult; +import com.android.server.wifi.wificond.NativeWifiClient; import com.android.server.wifi.wificond.PnoNetwork; import com.android.server.wifi.wificond.PnoSettings; import com.android.server.wifi.wificond.RadioChainInfo; import com.android.server.wifi.wificond.SingleScanSettings; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -183,8 +185,15 @@ public class WificondControl implements IBinder.DeathRecipient { } @Override - public void onNumAssociatedStationsChanged(int numStations) { - mSoftApListener.onNumAssociatedStationsChanged(numStations); + public void onConnectedClientsChanged(NativeWifiClient[] clients) { + if (mVerboseLoggingEnabled) { + Log.d(TAG, "onConnectedClientsChanged called with " + clients.length + " clients"); + for (int i = 0; i < clients.length; i++) { + Log.d(TAG, " mac " + clients[i].macAddress); + } + } + + mSoftApListener.onConnectedClientsChanged(Arrays.asList(clients)); } @Override diff --git a/tests/wifitests/src/com/android/server/wifi/ActiveModeWardenTest.java b/tests/wifitests/src/com/android/server/wifi/ActiveModeWardenTest.java index f52229c1d..6a3776a99 100644 --- a/tests/wifitests/src/com/android/server/wifi/ActiveModeWardenTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ActiveModeWardenTest.java @@ -21,10 +21,21 @@ import static com.android.server.wifi.ActiveModeManager.SCAN_WITHOUT_HIDDEN_NETW import static com.android.server.wifi.ActiveModeManager.SCAN_WITH_HIDDEN_NETWORKS; import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; import android.content.Context; import android.content.res.Resources; +import android.net.wifi.WifiClient; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.os.BatteryStats; @@ -46,6 +57,8 @@ import org.mockito.stubbing.Answer; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; /** * Unit tests for {@link com.android.server.wifi.ActiveModeWarden}. @@ -450,7 +463,7 @@ public class ActiveModeWardenTest { mLooper.dispatchAll(); verify(mSoftApStateMachineCallback, never()).onStateChanged(anyInt(), anyInt()); - verify(mSoftApStateMachineCallback, never()).onNumClientsChanged(anyInt()); + verify(mSoftApStateMachineCallback, never()).onConnectedClientsChanged(any()); } /** @@ -474,13 +487,13 @@ public class ActiveModeWardenTest { * Verifies that NumClientsChanged event is being passed from SoftApManager to WifiServiceImpl */ @Test - public void callsWifiServiceCallbackOnSoftApNumClientsChanged() throws Exception { - final int testNumClients = 3; + public void callsWifiServiceCallbackOnSoftApConnectedClientsChanged() throws Exception { + final List<WifiClient> testClients = new ArrayList(); enterSoftApActiveMode(); - mSoftApManagerCallback.onNumClientsChanged(testNumClients); + mSoftApManagerCallback.onConnectedClientsChanged(testClients); mLooper.dispatchAll(); - verify(mSoftApStateMachineCallback).onNumClientsChanged(testNumClients); + verify(mSoftApStateMachineCallback).onConnectedClientsChanged(testClients); } /** @@ -488,17 +501,16 @@ public class ActiveModeWardenTest { * WifiServiceImpl is null. */ @Test - public void testNullCallbackToWifiServiceImplForNumClientsChanged() throws Exception { - - final int testNumClients = 3; + public void testNullCallbackToWifiServiceImplForConnectedClientsChanged() throws Exception { + final List<WifiClient> testClients = new ArrayList(); //set the callback to null mActiveModeWarden.registerSoftApCallback(null); enterSoftApActiveMode(); - mSoftApManagerCallback.onNumClientsChanged(testNumClients); + mSoftApManagerCallback.onConnectedClientsChanged(testClients); - verify(mSoftApStateMachineCallback, never()).onNumClientsChanged(anyInt()); + verify(mSoftApStateMachineCallback, never()).onConnectedClientsChanged(any()); } /** diff --git a/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java b/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java index ed70ef5a2..cd2f9fa27 100644 --- a/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java @@ -31,15 +31,28 @@ import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED; import static com.android.server.wifi.LocalOnlyHotspotRequestInfo.HOTSPOT_NO_ERROR; import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.anyLong; +import static org.mockito.Mockito.anyString; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; import android.app.test.TestAlarmManager; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.database.ContentObserver; +import android.net.MacAddress; import android.net.Uri; import android.net.wifi.IApInterfaceEventCallback; +import android.net.wifi.WifiClient; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.os.UserHandle; @@ -50,14 +63,18 @@ import androidx.test.filters.SmallTest; import com.android.internal.R; import com.android.internal.util.WakeupMessage; +import com.android.server.wifi.wificond.NativeWifiClient; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Locale; @@ -74,6 +91,16 @@ public class SoftApManagerTest { private static final String TEST_INTERFACE_NAME = "testif0"; private static final String OTHER_INTERFACE_NAME = "otherif"; private static final int TEST_NUM_CONNECTED_CLIENTS = 4; + private static final MacAddress TEST_MAC_ADDRESS = MacAddress.fromString("22:33:44:55:66:77"); + private static final WifiClient TEST_CONNECTED_CLIENT = new WifiClient(TEST_MAC_ADDRESS); + private static final List<WifiClient> TEST_CONNECTED_CLIENTS = + new ArrayList(Arrays.asList(TEST_CONNECTED_CLIENT)); + private static final NativeWifiClient TEST_NATIVE_CLIENT = new NativeWifiClient() {{ + macAddress = TEST_MAC_ADDRESS.toByteArray(); + } + }; + private static final List<NativeWifiClient> TEST_CONNECTED_NATIVECLIENTS = + new ArrayList(Arrays.asList(TEST_NATIVE_CLIENT)); private final WifiConfiguration mDefaultApConfig = createDefaultApConfig(); @@ -825,17 +852,21 @@ public class SoftApManagerTest { } @Test - public void updatesNumAssociatedStations() throws Exception { + public void updatesConnectedClients() throws Exception { SoftApModeConfiguration apConfig = new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null); startSoftApAndVerifyEnabled(apConfig); - mSoftApListenerCaptor.getValue().onNumAssociatedStationsChanged( - TEST_NUM_CONNECTED_CLIENTS); + mSoftApListenerCaptor.getValue().onConnectedClientsChanged( + TEST_CONNECTED_NATIVECLIENTS); mLooper.dispatchAll(); - verify(mCallback).onNumClientsChanged(TEST_NUM_CONNECTED_CLIENTS); - verify(mWifiMetrics).addSoftApNumAssociatedStationsChangedEvent(TEST_NUM_CONNECTED_CLIENTS, + verify(mCallback).onConnectedClientsChanged( + Mockito.argThat((List<WifiClient> clients) -> + clients.contains(TEST_CONNECTED_CLIENT)) + ); + verify(mWifiMetrics).addSoftApNumAssociatedStationsChangedEvent( + TEST_CONNECTED_CLIENTS.size(), apConfig.getTargetMode()); } @@ -844,36 +875,41 @@ public class SoftApManagerTest { * trigger callbacks a second time. */ @Test - public void testDoesNotTriggerCallbackForSameNumberClientUpdate() throws Exception { + public void testDoesNotTriggerCallbackForSameClients() throws Exception { SoftApModeConfiguration apConfig = new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null); startSoftApAndVerifyEnabled(apConfig); - mSoftApListenerCaptor.getValue().onNumAssociatedStationsChanged( - TEST_NUM_CONNECTED_CLIENTS); + mSoftApListenerCaptor.getValue().onConnectedClientsChanged( + TEST_CONNECTED_NATIVECLIENTS); mLooper.dispatchAll(); // now trigger callback again, but we should have each method only called once - mSoftApListenerCaptor.getValue().onNumAssociatedStationsChanged( - TEST_NUM_CONNECTED_CLIENTS); + mSoftApListenerCaptor.getValue().onConnectedClientsChanged( + TEST_CONNECTED_NATIVECLIENTS); mLooper.dispatchAll(); - verify(mCallback).onNumClientsChanged(TEST_NUM_CONNECTED_CLIENTS); - verify(mWifiMetrics).addSoftApNumAssociatedStationsChangedEvent(TEST_NUM_CONNECTED_CLIENTS, + verify(mCallback).onConnectedClientsChanged( + Mockito.argThat((List<WifiClient> clients) -> + clients.contains(TEST_CONNECTED_CLIENT)) + ); + verify(mWifiMetrics) + .addSoftApNumAssociatedStationsChangedEvent( + TEST_CONNECTED_CLIENTS.size(), apConfig.getTargetMode()); } @Test - public void handlesInvalidNumAssociatedStations() throws Exception { + public void handlesInvalidConnectedClients() throws Exception { SoftApModeConfiguration apConfig = new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null); startSoftApAndVerifyEnabled(apConfig); /* Invalid values should be ignored */ - final int mInvalidNumClients = -1; - mSoftApListenerCaptor.getValue().onNumAssociatedStationsChanged(mInvalidNumClients); + final List<NativeWifiClient> mInvalidClients = null; + mSoftApListenerCaptor.getValue().onConnectedClientsChanged(mInvalidClients); mLooper.dispatchAll(); - verify(mCallback, never()).onNumClientsChanged(mInvalidNumClients); + verify(mCallback, never()).onConnectedClientsChanged(null); verify(mWifiMetrics, never()).addSoftApNumAssociatedStationsChangedEvent(anyInt(), anyInt()); } @@ -885,20 +921,23 @@ public class SoftApManagerTest { new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null); startSoftApAndVerifyEnabled(apConfig); - mSoftApListenerCaptor.getValue().onNumAssociatedStationsChanged( - TEST_NUM_CONNECTED_CLIENTS); + mSoftApListenerCaptor.getValue().onConnectedClientsChanged( + TEST_CONNECTED_NATIVECLIENTS); mLooper.dispatchAll(); - order.verify(mCallback).onNumClientsChanged(TEST_NUM_CONNECTED_CLIENTS); + verify(mCallback).onConnectedClientsChanged( + Mockito.argThat((List<WifiClient> clients) -> + clients.contains(TEST_CONNECTED_CLIENT)) + ); order.verify(mWifiMetrics).addSoftApNumAssociatedStationsChangedEvent( - TEST_NUM_CONNECTED_CLIENTS, apConfig.getTargetMode()); + TEST_CONNECTED_CLIENTS.size(), apConfig.getTargetMode()); // Verify timer is canceled at this point verify(mAlarmManager.getAlarmManager()).cancel(any(WakeupMessage.class)); mSoftApManager.stop(); mLooper.dispatchAll(); - order.verify(mCallback).onNumClientsChanged(0); + order.verify(mCallback).onConnectedClientsChanged(new ArrayList<>()); order.verify(mWifiMetrics).addSoftApNumAssociatedStationsChangedEvent(0, apConfig.getTargetMode()); // Verify timer is canceled after stop softap @@ -933,8 +972,8 @@ public class SoftApManagerTest { SoftApModeConfiguration apConfig = new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null); startSoftApAndVerifyEnabled(apConfig); - mSoftApListenerCaptor.getValue().onNumAssociatedStationsChanged( - TEST_NUM_CONNECTED_CLIENTS); + mSoftApListenerCaptor.getValue().onConnectedClientsChanged( + TEST_CONNECTED_NATIVECLIENTS); mLooper.dispatchAll(); // Verify timer is canceled @@ -947,16 +986,23 @@ public class SoftApManagerTest { new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null); startSoftApAndVerifyEnabled(apConfig); - mSoftApListenerCaptor.getValue().onNumAssociatedStationsChanged( - TEST_NUM_CONNECTED_CLIENTS); + mSoftApListenerCaptor.getValue().onConnectedClientsChanged( + TEST_CONNECTED_NATIVECLIENTS); mLooper.dispatchAll(); - verify(mCallback).onNumClientsChanged(TEST_NUM_CONNECTED_CLIENTS); + verify(mCallback).onConnectedClientsChanged( + Mockito.argThat((List<WifiClient> clients) -> + clients.contains(TEST_CONNECTED_CLIENT)) + ); // Verify timer is canceled at this point verify(mAlarmManager.getAlarmManager()).cancel(any(WakeupMessage.class)); - mSoftApListenerCaptor.getValue().onNumAssociatedStationsChanged(0); + List<NativeWifiClient> testClients = new ArrayList(); + mSoftApListenerCaptor.getValue().onConnectedClientsChanged(testClients); mLooper.dispatchAll(); - verify(mCallback, times(2)).onNumClientsChanged(0); + verify(mCallback).onConnectedClientsChanged( + Mockito.argThat((List<WifiClient> clients) -> + clients.contains(TEST_CONNECTED_CLIENT)) + ); // Verify timer is scheduled again verify(mAlarmManager.getAlarmManager(), times(2)).setExact(anyInt(), anyLong(), eq(mSoftApManager.SOFT_AP_SEND_MESSAGE_TIMEOUT_TAG), any(), any()); @@ -1032,11 +1078,12 @@ public class SoftApManagerTest { new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null); startSoftApAndVerifyEnabled(apConfig); // add some clients - mSoftApListenerCaptor.getValue().onNumAssociatedStationsChanged( - TEST_NUM_CONNECTED_CLIENTS); + mSoftApListenerCaptor.getValue().onConnectedClientsChanged( + TEST_CONNECTED_NATIVECLIENTS); mLooper.dispatchAll(); // remove all clients - mSoftApListenerCaptor.getValue().onNumAssociatedStationsChanged(0); + mSoftApListenerCaptor.getValue() + .onConnectedClientsChanged(new ArrayList<NativeWifiClient>()); mLooper.dispatchAll(); // Verify timer is not scheduled verify(mAlarmManager.getAlarmManager(), never()).setExact(anyInt(), anyLong(), @@ -1095,10 +1142,10 @@ public class SoftApManagerTest { mWifiNativeInterfaceCallbackCaptor.getValue().onUp(TEST_INTERFACE_NAME); mLooper.dispatchAll(); order.verify(mCallback).onStateChanged(WifiManager.WIFI_AP_STATE_ENABLED, 0); - order.verify(mCallback).onNumClientsChanged(0); + order.verify(mCallback).onConnectedClientsChanged(new ArrayList<>()); verify(mSarManager).setSapWifiState(WifiManager.WIFI_AP_STATE_ENABLED); verify(mContext, times(2)).sendStickyBroadcastAsUser(intentCaptor.capture(), - eq(UserHandle.ALL)); + eq(UserHandle.ALL)); List<Intent> capturedIntents = intentCaptor.getAllValues(); checkApStateChangedBroadcast(capturedIntents.get(0), WIFI_AP_STATE_ENABLING, WIFI_AP_STATE_DISABLED, HOTSPOT_NO_ERROR, TEST_INTERFACE_NAME, @@ -1113,8 +1160,8 @@ public class SoftApManagerTest { } private void checkApStateChangedBroadcast(Intent intent, int expectedCurrentState, - int expectedPrevState, int expectedErrorCode, - String expectedIfaceName, int expectedMode) { + int expectedPrevState, int expectedErrorCode, + String expectedIfaceName, int expectedMode) { int currentState = intent.getIntExtra(EXTRA_WIFI_AP_STATE, WIFI_AP_STATE_DISABLED); int prevState = intent.getIntExtra(EXTRA_PREVIOUS_WIFI_AP_STATE, WIFI_AP_STATE_DISABLED); int errorCode = intent.getIntExtra(EXTRA_WIFI_AP_FAILURE_REASON, HOTSPOT_NO_ERROR); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index a92043624..49c40a6d7 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -85,6 +85,7 @@ import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.ParceledListSlice; import android.content.res.Resources; +import android.net.MacAddress; import android.net.NetworkStack; import android.net.Uri; import android.net.wifi.IDppCallback; @@ -93,6 +94,7 @@ import android.net.wifi.IOnWifiUsabilityStatsListener; import android.net.wifi.ISoftApCallback; import android.net.wifi.ITrafficStateCallback; import android.net.wifi.ScanResult; +import android.net.wifi.WifiClient; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration.KeyMgmt; import android.net.wifi.WifiEnterpriseConfig; @@ -139,6 +141,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.ArgumentMatcher; import org.mockito.InOrder; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.mockito.Spy; @@ -202,6 +205,7 @@ public class WifiServiceImplTest { private SoftApCallback mStateMachineSoftApCallback; private ApplicationInfo mApplicationInfo; private static final String DPP_URI = "DPP:some_dpp_uri"; + private static final MacAddress TEST_FACTORY_MAC_ADDR = MacAddress.fromString(TEST_FACTORY_MAC); final ArgumentCaptor<BroadcastReceiver> mBroadcastReceiverCaptor = ArgumentCaptor.forClass(BroadcastReceiver.class); @@ -1756,7 +1760,7 @@ public class WifiServiceImplTest { mWifiServiceImpl.registerSoftApCallback(mAppBinder, mClientSoftApCallback, 1); mLooper.dispatchAll(); verify(mClientSoftApCallback, never()).onStateChanged(WIFI_AP_STATE_DISABLED, 0); - verify(mClientSoftApCallback, never()).onNumClientsChanged(0); + verify(mClientSoftApCallback, never()).onConnectedClientsChanged(any()); } @@ -1778,7 +1782,7 @@ public class WifiServiceImplTest { mWifiServiceImpl.registerSoftApCallback(binder, callback, callbackIdentifier); mLooper.dispatchAll(); verify(callback).onStateChanged(WIFI_AP_STATE_DISABLED, 0); - verify(callback).onNumClientsChanged(0); + verify(callback).onConnectedClientsChanged(Mockito.<WifiClient>anyList()); } /** @@ -1798,12 +1802,14 @@ public class WifiServiceImplTest { verify(mAnotherAppBinder).linkToDeath(any(), anyInt()); verify(mAnotherAppBinder, never()).unlinkToDeath(any(), anyInt()); - final int testNumClients = 4; - mStateMachineSoftApCallback.onNumClientsChanged(testNumClients); + final WifiClient testClient = new WifiClient(TEST_FACTORY_MAC_ADDR); + final List<WifiClient> testClients = new ArrayList(); + testClients.add(testClient); + mStateMachineSoftApCallback.onConnectedClientsChanged(testClients); mLooper.dispatchAll(); // Verify only the second callback is being called - verify(mClientSoftApCallback, never()).onNumClientsChanged(testNumClients); - verify(mAnotherSoftApCallback).onNumClientsChanged(testNumClients); + verify(mClientSoftApCallback, never()).onConnectedClientsChanged(testClients); + verify(mAnotherSoftApCallback).onConnectedClientsChanged(testClients); } /** @@ -1819,10 +1825,12 @@ public class WifiServiceImplTest { mWifiServiceImpl.unregisterSoftApCallback(callbackIdentifier); mLooper.dispatchAll(); - final int testNumClients = 4; - mStateMachineSoftApCallback.onNumClientsChanged(testNumClients); + final WifiClient testClient = new WifiClient(TEST_FACTORY_MAC_ADDR); + final List<WifiClient> testClients = new ArrayList(); + testClients.add(testClient); + mStateMachineSoftApCallback.onConnectedClientsChanged(testClients); mLooper.dispatchAll(); - verify(mClientSoftApCallback, never()).onNumClientsChanged(testNumClients); + verify(mClientSoftApCallback, never()).onConnectedClientsChanged(testClients); } /** @@ -1840,10 +1848,12 @@ public class WifiServiceImplTest { mWifiServiceImpl.unregisterSoftApCallback(differentCallbackIdentifier); mLooper.dispatchAll(); - final int testNumClients = 4; - mStateMachineSoftApCallback.onNumClientsChanged(testNumClients); + final WifiClient testClient = new WifiClient(TEST_FACTORY_MAC_ADDR); + final List<WifiClient> testClients = new ArrayList(); + testClients.add(testClient); + mStateMachineSoftApCallback.onConnectedClientsChanged(testClients); mLooper.dispatchAll(); - verify(mClientSoftApCallback).onNumClientsChanged(testNumClients); + verify(mClientSoftApCallback).onConnectedClientsChanged(testClients); } /** @@ -1858,16 +1868,16 @@ public class WifiServiceImplTest { callbackIdentifier); // Change state from default before registering the second callback - final int testNumClients = 4; + final List<WifiClient> testClients = new ArrayList(); mStateMachineSoftApCallback.onStateChanged(WIFI_AP_STATE_ENABLED, 0); - mStateMachineSoftApCallback.onNumClientsChanged(testNumClients); + mStateMachineSoftApCallback.onConnectedClientsChanged(testClients); // Register another callback and verify the new state is returned in the immediate callback final int anotherUid = 2; mWifiServiceImpl.registerSoftApCallback(mAppBinder, mAnotherSoftApCallback, anotherUid); mLooper.dispatchAll(); verify(mAnotherSoftApCallback).onStateChanged(WIFI_AP_STATE_ENABLED, 0); - verify(mAnotherSoftApCallback).onNumClientsChanged(testNumClients); + verify(mAnotherSoftApCallback).onConnectedClientsChanged(testClients); // unregister the fisrt callback mWifiServiceImpl.unregisterSoftApCallback(callbackIdentifier); @@ -1913,26 +1923,30 @@ public class WifiServiceImplTest { verify(mAppBinder).unlinkToDeath(drCaptor.getValue(), 0); // Verify callback is removed from the list as well - final int testNumClients = 4; - mStateMachineSoftApCallback.onNumClientsChanged(testNumClients); + final WifiClient testClient = new WifiClient(TEST_FACTORY_MAC_ADDR); + final List<WifiClient> testClients = new ArrayList(); + testClients.add(testClient); + mStateMachineSoftApCallback.onConnectedClientsChanged(testClients); mLooper.dispatchAll(); - verify(mClientSoftApCallback, never()).onNumClientsChanged(testNumClients); + verify(mClientSoftApCallback, never()).onConnectedClientsChanged(testClients); } /** * Verify that soft AP callback is called on NumClientsChanged event */ @Test - public void callsRegisteredCallbacksOnNumClientsChangedEvent() throws Exception { + public void callsRegisteredCallbacksOnConnectedClientsChangedEvent() throws Exception { setupClientModeImplHandlerForPost(); final int callbackIdentifier = 1; registerSoftApCallbackAndVerify(mClientSoftApCallback, callbackIdentifier); - final int testNumClients = 4; - mStateMachineSoftApCallback.onNumClientsChanged(testNumClients); + final WifiClient testClient = new WifiClient(TEST_FACTORY_MAC_ADDR); + final List<WifiClient> testClients = new ArrayList(); + testClients.add(testClient); + mStateMachineSoftApCallback.onConnectedClientsChanged(testClients); mLooper.dispatchAll(); - verify(mClientSoftApCallback).onNumClientsChanged(testNumClients); + verify(mClientSoftApCallback).onConnectedClientsChanged(testClients); } /** @@ -1955,12 +1969,12 @@ public class WifiServiceImplTest { * Ap events, even when no callbacks are registered. */ @Test - public void updatesSoftApStateAndNumClientsOnSoftApEvents() throws Exception { + public void updatesSoftApStateAndConnectedClientsOnSoftApEvents() throws Exception { setupClientModeImplHandlerForPost(); - final int testNumClients = 4; + final List<WifiClient> testClients = new ArrayList(); mStateMachineSoftApCallback.onStateChanged(WIFI_AP_STATE_ENABLED, 0); - mStateMachineSoftApCallback.onNumClientsChanged(testNumClients); + mStateMachineSoftApCallback.onConnectedClientsChanged(testClients); // Register callback after num clients and soft AP are changed. final int callbackIdentifier = 1; @@ -1968,7 +1982,7 @@ public class WifiServiceImplTest { callbackIdentifier); mLooper.dispatchAll(); verify(mClientSoftApCallback).onStateChanged(WIFI_AP_STATE_ENABLED, 0); - verify(mClientSoftApCallback).onNumClientsChanged(testNumClients); + verify(mClientSoftApCallback).onConnectedClientsChanged(testClients); } private class IntentFilterMatcher implements ArgumentMatcher<IntentFilter> { diff --git a/tests/wifitests/src/com/android/server/wifi/WificondControlTest.java b/tests/wifitests/src/com/android/server/wifi/WificondControlTest.java index ca48f38ae..f1c4f30e4 100644 --- a/tests/wifitests/src/com/android/server/wifi/WificondControlTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WificondControlTest.java @@ -63,6 +63,7 @@ import com.android.server.wifi.util.NativeUtil; import com.android.server.wifi.wificond.ChannelSettings; import com.android.server.wifi.wificond.HiddenNetwork; import com.android.server.wifi.wificond.NativeScanResult; +import com.android.server.wifi.wificond.NativeWifiClient; import com.android.server.wifi.wificond.PnoSettings; import com.android.server.wifi.wificond.RadioChainInfo; import com.android.server.wifi.wificond.SingleScanSettings; @@ -928,9 +929,9 @@ public class WificondControlTest { TEST_INTERFACE_NAME, mSoftApListener)); verify(mApInterface).registerCallback(apInterfaceCallbackCaptor.capture()); - int numStations = 5; - apInterfaceCallbackCaptor.getValue().onNumAssociatedStationsChanged(numStations); - verify(mSoftApListener).onNumAssociatedStationsChanged(eq(numStations)); + final NativeWifiClient[] testClients = new NativeWifiClient[]{}; + apInterfaceCallbackCaptor.getValue().onConnectedClientsChanged(testClients); + verify(mSoftApListener).onConnectedClientsChanged(Arrays.asList(testClients)); int channelFrequency = 2437; int channelBandwidth = IApInterfaceEventCallback.BANDWIDTH_20; |