diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2020-05-07 03:53:33 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-05-07 03:53:33 +0000 |
commit | 774554168ce878eeab890ae38a5297de501635e2 (patch) | |
tree | bee7aba492ad3421069656604b9a10ff66140527 | |
parent | 65fc81135c6ed806c584360346c841e7679daf75 (diff) | |
parent | 1fe289767f5098408dc2e3e7094f8b353ffa34de (diff) |
Merge "Add start/stop-softap shell command" into rvc-dev am: 1fe289767f
Change-Id: I3b9c48bdec81ea400bd6fe62fcbfc5eab4f5296b
-rw-r--r-- | service/java/com/android/server/wifi/WifiShellCommand.java | 74 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiShellCommandTest.java | 27 |
2 files changed, 101 insertions, 0 deletions
diff --git a/service/java/com/android/server/wifi/WifiShellCommand.java b/service/java/com/android/server/wifi/WifiShellCommand.java index ed955bab4..a80b14ad6 100644 --- a/service/java/com/android/server/wifi/WifiShellCommand.java +++ b/service/java/com/android/server/wifi/WifiShellCommand.java @@ -98,7 +98,9 @@ public class WifiShellCommand extends BasicShellCommandHandler { "set-verbose-logging", "set-wifi-enabled", "start-scan", + "start-softap", "status", + "stop-softap", }; private static final Map<String, Pair<NetworkRequest, ConnectivityManager.NetworkCallback>> @@ -303,6 +305,23 @@ public class WifiShellCommand extends BasicShellCommandHandler { return 0; } } + case "start-softap": { + SoftApConfiguration config = buildSoftApConfiguration(pw); + if (mWifiService.startTetheredHotspot(config)) { + pw.println("Soft AP started successfully"); + } else { + pw.println("Soft AP failed to start. Please check config parameters"); + } + return 0; + } + case "stop-softap": { + if (mWifiService.stopSoftAp()) { + pw.println("Soft AP stopped successfully"); + } else { + pw.println("Soft AP failed to stop"); + } + return 0; + } case "force-country-code": { boolean enabled = getNextArgRequiredTrueOrFalse("enabled", "disabled"); if (enabled) { @@ -722,6 +741,42 @@ public class WifiShellCommand extends BasicShellCommandHandler { return configuration; } + private SoftApConfiguration buildSoftApConfiguration(PrintWriter pw) { + String ssid = getNextArgRequired(); + String type = getNextArgRequired(); + SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder(); + configBuilder.setSsid("\"" + ssid + "\""); + if (TextUtils.equals(type, "wpa2")) { + configBuilder.setPassphrase(getNextArgRequired(), + SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); + } else if (TextUtils.equals(type, "open")) { + configBuilder.setPassphrase(null, SoftApConfiguration.SECURITY_TYPE_OPEN); + } else { + throw new IllegalArgumentException("Unknown network type " + type); + } + String option = getNextOption(); + while (option != null) { + if (option.equals("-b")) { + String preferredBand = getNextArgRequired(); + if (preferredBand.equals("2")) { + configBuilder.setBand(SoftApConfiguration.BAND_2GHZ); + } else if (preferredBand.equals("5")) { + configBuilder.setBand(SoftApConfiguration.BAND_5GHZ); + } else if (preferredBand.equals("6")) { + configBuilder.setBand(SoftApConfiguration.BAND_6GHZ); + } else if (preferredBand.equals("any")) { + configBuilder.setBand(SoftApConfiguration.BAND_ANY); + } else { + throw new IllegalArgumentException("Invalid band option " + preferredBand); + } + } else { + pw.println("Ignoring unknown option " + option); + } + option = getNextOption(); + } + return configBuilder.build(); + } + private WifiNetworkSuggestion buildSuggestion(PrintWriter pw) { String ssid = getNextArgRequired(); String type = getNextArgRequired(); @@ -940,6 +995,25 @@ public class WifiShellCommand extends BasicShellCommandHandler { pw.println(" reset-connected-score"); pw.println(" Turns on the default connected scorer."); pw.println(" Note: Will clear any external scorer set."); + pw.println(" start-softap <ssid> (open|wpa2) <passphrase> [-b 2|5|6|any]"); + pw.println(" Start softap with provided params"); + pw.println(" Note that the shell command doesn't activate internet tethering. In some " + + "devices, internet sharing is possible when Wi-Fi STA is also enabled and is" + + "associated to another AP with internet access."); + pw.println(" <ssid> - SSID of the network"); + pw.println(" open|wpa2 - Security type of the network."); + pw.println(" - Use 'open' for networks with no passphrase"); + pw.println(" - Use 'wpa2' for networks with passphrase"); + pw.println(" -b 2|5|6|any - select the preferred band."); + pw.println(" - Use '2' to select 2.4GHz band as the preferred band"); + pw.println(" - Use '5' to select 5GHz band as the preferred band"); + pw.println(" - Use '6' to select 6GHz band as the preferred band"); + pw.println(" - Use 'any' to indicate no band preference"); + pw.println(" Note: If the band option is not provided, 2.4GHz is the preferred band."); + pw.println(" The exact channel is auto-selected by FW unless overridden by " + + "force-softap-channel command"); + pw.println(" stop-softap"); + pw.println(" Stop softap (hotspot)"); } private void onHelpPrivileged(PrintWriter pw) { diff --git a/tests/wifitests/src/com/android/server/wifi/WifiShellCommandTest.java b/tests/wifitests/src/com/android/server/wifi/WifiShellCommandTest.java index a1897ded9..1f4af5cc0 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiShellCommandTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiShellCommandTest.java @@ -16,6 +16,7 @@ package com.android.server.wifi; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; @@ -29,6 +30,7 @@ import static org.mockito.Mockito.when; import android.content.Context; import android.net.ConnectivityManager; +import android.net.wifi.SoftApConfiguration; import android.os.Binder; import android.os.Process; @@ -37,6 +39,7 @@ import androidx.test.filters.SmallTest; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -396,4 +399,28 @@ public class WifiShellCommandTest extends WifiBaseTest { verify(mClientModeImpl, times(2)).hasNetworkRequestUserApprovedApp(TEST_PACKAGE); mWifiShellCommand.getOutPrintWriter().toString().contains("no"); } + + @Test + public void testStartSoftAp() { + mWifiShellCommand.exec( + new Binder(), new FileDescriptor(), new FileDescriptor(), new FileDescriptor(), + new String[]{"start-softap", "ap1", "wpa2", "xyzabc321", "-b", "5"}); + ArgumentCaptor<SoftApConfiguration> softApConfigurationCaptor = ArgumentCaptor.forClass( + SoftApConfiguration.class); + verify(mWifiService).startTetheredHotspot(softApConfigurationCaptor.capture()); + assertEquals(SoftApConfiguration.BAND_5GHZ, + softApConfigurationCaptor.getValue().getBand()); + assertEquals(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK, + softApConfigurationCaptor.getValue().getSecurityType()); + assertEquals("\"ap1\"", softApConfigurationCaptor.getValue().getSsid()); + assertEquals("xyzabc321", softApConfigurationCaptor.getValue().getPassphrase()); + } + + @Test + public void testStopSoftAp() { + mWifiShellCommand.exec( + new Binder(), new FileDescriptor(), new FileDescriptor(), new FileDescriptor(), + new String[]{"stop-softap"}); + verify(mWifiService).stopSoftAp(); + } } |