Remove redundant requestedState from IpServer#enable

IpServer#enable is called with both a non-null TetheringRequest and the
requestedState, but the requestedState is derived from the
TetheringRequest's connectivity scope. Remove this requestedState arg
and simply have IpServer read the connectivity scope itself.

Bug: 216524590
Test: covered by existing tests
Change-Id: I73227f5053da3a2ab35563268fc416b6dd9b1801
This commit is contained in:
Quang Anh Luong 2025-01-30 12:20:24 +09:00
commit ffb503e86e
3 changed files with 35 additions and 29 deletions

View file

@ -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:

View file

@ -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,

View file

@ -240,8 +240,8 @@ public class IpServerTest {
Set<LinkAddress> 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<IDhcpEventCallbacks> 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.
*