diff --git a/Tethering/src/android/net/ip/IpServer.java b/Tethering/src/android/net/ip/IpServer.java index 8dd7ef2ce4..609d759e5b 100644 --- a/Tethering/src/android/net/ip/IpServer.java +++ b/Tethering/src/android/net/ip/IpServer.java @@ -429,9 +429,11 @@ public class IpServer extends StateMachineShim { return Collections.unmodifiableList(mDhcpLeases); } - /** Enable this IpServer. IpServer state machine will be tethered or localHotspot state. */ - public void enable(final int requestedState, @NonNull final TetheringRequest request) { - sendMessage(CMD_TETHER_REQUESTED, requestedState, 0, request); + /** + * Enable this IpServer. IpServer state machine will be tethered or localHotspot state based on + * the connectivity scope of the TetheringRequest. */ + public void enable(@NonNull final TetheringRequest request) { + sendMessage(CMD_TETHER_REQUESTED, 0, 0, request); } /** Stop this IpServer. After this is called this IpServer should not be used any more. */ @@ -1053,13 +1055,13 @@ public class IpServer extends StateMachineShim { case CMD_TETHER_REQUESTED: mLastError = TETHER_ERROR_NO_ERROR; mTetheringRequest = (TetheringRequest) message.obj; - switch (message.arg1) { - case STATE_LOCAL_ONLY: - maybeConfigureStaticIp((TetheringRequest) message.obj); + switch (mTetheringRequest.getConnectivityScope()) { + case CONNECTIVITY_SCOPE_LOCAL: + maybeConfigureStaticIp(mTetheringRequest); transitionTo(mLocalHotspotState); break; - case STATE_TETHERED: - maybeConfigureStaticIp((TetheringRequest) message.obj); + case CONNECTIVITY_SCOPE_GLOBAL: + maybeConfigureStaticIp(mTetheringRequest); transitionTo(mTetheredState); break; default: diff --git a/Tethering/src/com/android/networkstack/tethering/Tethering.java b/Tethering/src/com/android/networkstack/tethering/Tethering.java index 3f4c2e8ea8..21943b0422 100644 --- a/Tethering/src/com/android/networkstack/tethering/Tethering.java +++ b/Tethering/src/com/android/networkstack/tethering/Tethering.java @@ -1167,9 +1167,7 @@ public class Tethering { // // This code cannot race with untether() because they both run on the handler thread. mPendingTetheringRequests.remove(request.getTetheringType()); - final int requestedState = request.getConnectivityScope() == CONNECTIVITY_SCOPE_GLOBAL - ? IpServer.STATE_TETHERED : IpServer.STATE_LOCAL_ONLY; - tetherState.ipServer.enable(requestedState, request); + tetherState.ipServer.enable(request); if (request.getRequestType() == REQUEST_TYPE_PLACEHOLDER) { TerribleErrorLog.logTerribleError(TetheringStatsLog::write, "Started tethering with placeholder request: " + request, diff --git a/Tethering/tests/unit/src/android/net/ip/IpServerTest.java b/Tethering/tests/unit/src/android/net/ip/IpServerTest.java index c5c3ad13d9..84b301fc0a 100644 --- a/Tethering/tests/unit/src/android/net/ip/IpServerTest.java +++ b/Tethering/tests/unit/src/android/net/ip/IpServerTest.java @@ -240,8 +240,8 @@ public class IpServerTest { Set upstreamAddresses, boolean usingLegacyDhcp, boolean usingBpfOffload) throws Exception { initStateMachine(interfaceType, usingLegacyDhcp, usingBpfOffload); - dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_TETHERED, 0, - mock(TetheringRequest.class)); + dispatchCommand(IpServer.CMD_TETHER_REQUESTED, 0, 0, + createMockTetheringRequest(CONNECTIVITY_SCOPE_GLOBAL)); verify(mBpfCoordinator).addIpServer(mIpServer); if (upstreamIface != null) { InterfaceParams interfaceParams = mDependencies.getInterfaceParams(upstreamIface); @@ -346,8 +346,8 @@ public class IpServerTest { public void canBeTetheredAsBluetooth() throws Exception { initStateMachine(TETHERING_BLUETOOTH); - dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_TETHERED, 0, - mock(TetheringRequest.class)); + dispatchCommand(IpServer.CMD_TETHER_REQUESTED, 0, 0, + createMockTetheringRequest(CONNECTIVITY_SCOPE_GLOBAL)); InOrder inOrder = inOrder(mCallback, mNetd, mRoutingCoordinatorManager); if (isAtLeastT()) { inOrder.verify(mRoutingCoordinatorManager) @@ -402,8 +402,8 @@ public class IpServerTest { public void canBeTetheredAsUsb() throws Exception { initStateMachine(TETHERING_USB); - dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_TETHERED, 0, - mock(TetheringRequest.class)); + dispatchCommand(IpServer.CMD_TETHER_REQUESTED, 0, 0, + createMockTetheringRequest(CONNECTIVITY_SCOPE_GLOBAL)); InOrder inOrder = inOrder(mCallback, mNetd, mRoutingCoordinatorManager); inOrder.verify(mRoutingCoordinatorManager).requestStickyDownstreamAddress(anyInt(), eq(CONNECTIVITY_SCOPE_GLOBAL), any()); @@ -426,8 +426,8 @@ public class IpServerTest { public void canBeTetheredAsWifiP2p_NotUsingDedicatedIp() throws Exception { initStateMachine(TETHERING_WIFI_P2P); - dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_LOCAL_ONLY, 0, - mock(TetheringRequest.class)); + dispatchCommand(IpServer.CMD_TETHER_REQUESTED, 0, 0, + createMockTetheringRequest(CONNECTIVITY_SCOPE_LOCAL)); InOrder inOrder = inOrder(mCallback, mNetd, mRoutingCoordinatorManager); inOrder.verify(mRoutingCoordinatorManager).requestStickyDownstreamAddress(anyInt(), eq(CONNECTIVITY_SCOPE_LOCAL), any()); @@ -451,8 +451,8 @@ public class IpServerTest { initStateMachine(TETHERING_WIFI_P2P, false /* usingLegacyDhcp */, DEFAULT_USING_BPF_OFFLOAD, true /* shouldEnableWifiP2pDedicatedIp */); - dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_LOCAL_ONLY, 0, - mock(TetheringRequest.class)); + dispatchCommand(IpServer.CMD_TETHER_REQUESTED, 0, 0, + createMockTetheringRequest(CONNECTIVITY_SCOPE_LOCAL)); InOrder inOrder = inOrder(mCallback, mNetd, mRoutingCoordinatorManager); // When using WiFi P2p dedicated IP, the IpServer just picks the IP address without // requesting for it at RoutingCoordinatorManager. @@ -632,8 +632,8 @@ public class IpServerTest { initStateMachine(TETHERING_USB); doThrow(RemoteException.class).when(mNetd).tetherInterfaceAdd(IFACE_NAME); - dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_TETHERED, 0, - mock(TetheringRequest.class)); + dispatchCommand(IpServer.CMD_TETHER_REQUESTED, 0, 0, + createMockTetheringRequest(CONNECTIVITY_SCOPE_GLOBAL)); InOrder usbTeardownOrder = inOrder(mNetd, mCallback); usbTeardownOrder.verify(mNetd).interfaceSetCfg( argThat(cfg -> IFACE_NAME.equals(cfg.ifName))); @@ -719,8 +719,8 @@ public class IpServerTest { @Test public void startsDhcpServerOnNcm() throws Exception { initStateMachine(TETHERING_NCM); - dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_LOCAL_ONLY, 0, - mock(TetheringRequest.class)); + dispatchCommand(IpServer.CMD_TETHER_REQUESTED, 0, 0, + createMockTetheringRequest(CONNECTIVITY_SCOPE_LOCAL)); dispatchTetherConnectionChanged(UPSTREAM_IFACE); assertDhcpStarted(new IpPrefix("192.168.42.0/24")); @@ -729,8 +729,8 @@ public class IpServerTest { @Test public void testOnNewPrefixRequest() throws Exception { initStateMachine(TETHERING_NCM); - dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_LOCAL_ONLY, 0, - mock(TetheringRequest.class)); + dispatchCommand(IpServer.CMD_TETHER_REQUESTED, 0, 0, + createMockTetheringRequest(CONNECTIVITY_SCOPE_LOCAL)); final IDhcpEventCallbacks eventCallbacks; final ArgumentCaptor dhcpEventCbsCaptor = @@ -919,8 +919,8 @@ public class IpServerTest { doNothing().when(mDependencies).makeDhcpServer(any(), mDhcpParamsCaptor.capture(), cbCaptor.capture()); initStateMachine(TETHERING_WIFI); - dispatchCommand(IpServer.CMD_TETHER_REQUESTED, STATE_TETHERED, 0, - mock(TetheringRequest.class)); + dispatchCommand(IpServer.CMD_TETHER_REQUESTED, 0, 0, + createMockTetheringRequest(CONNECTIVITY_SCOPE_GLOBAL)); verify(mDhcpServer, never()).startWithCallbacks(any(), any()); // No stop dhcp server because dhcp server is not created yet. @@ -966,6 +966,12 @@ public class IpServerTest { assertDhcpServingParams(mDhcpParamsCaptor.getValue(), expectedPrefix); } + private TetheringRequest createMockTetheringRequest(int connectivityScope) { + TetheringRequest request = mock(TetheringRequest.class); + when(request.getConnectivityScope()).thenReturn(connectivityScope); + return request; + } + /** * Send a command to the state machine under test, and run the event loop to idle. *