[Thread] use immutable BorderRouterConfiguration

This CL refactors the code of ThreadNetworkControllerService to use
immutable BorderRouterConfiguration.

Bug: 358012289
Test: CI

Change-Id: Ifabf0144d5e8f5b5da2b8d64fc9e53c46f917dbb
This commit is contained in:
Handa Wang 2024-08-07 02:36:19 +00:00
commit ed24de9b64

View file

@ -238,7 +238,11 @@ final class ThreadNetworkControllerService extends IThreadNetworkController.Stub
mInfraIfController = infraIfController;
mUpstreamNetworkRequest = newUpstreamNetworkRequest();
mNetworkToInterface = new HashMap<Network, String>();
mBorderRouterConfig = new BorderRouterConfiguration();
mBorderRouterConfig =
new BorderRouterConfiguration.Builder()
.setIsBorderRoutingEnabled(true)
.setInfraInterfaceName(null)
.build();
mPersistentSettings = persistentSettings;
mNsdPublisher = nsdPublisher;
mUserManager = userManager;
@ -1228,40 +1232,54 @@ final class ThreadNetworkControllerService extends IThreadNetworkController.Stub
}
}
private void enableBorderRouting(String infraIfName) {
if (mBorderRouterConfig.isBorderRoutingEnabled
&& infraIfName.equals(mBorderRouterConfig.infraInterfaceName)) {
private void configureBorderRouter(BorderRouterConfiguration borderRouterConfig) {
if (mBorderRouterConfig.equals(borderRouterConfig)) {
return;
}
Log.i(TAG, "Enable border routing on AIL: " + infraIfName);
Log.i(
TAG,
"Configuring Border Router: " + mBorderRouterConfig + " -> " + borderRouterConfig);
mBorderRouterConfig = borderRouterConfig;
ParcelFileDescriptor infraIcmp6Socket = null;
if (mBorderRouterConfig.infraInterfaceName != null) {
try {
infraIcmp6Socket =
mInfraIfController.createIcmp6Socket(
mBorderRouterConfig.infraInterfaceName);
} catch (IOException e) {
Log.i(TAG, "Failed to create ICMPv6 socket on infra network interface", e);
}
}
try {
mBorderRouterConfig.infraInterfaceName = infraIfName;
mBorderRouterConfig.isBorderRoutingEnabled = true;
ParcelFileDescriptor infraIcmp6Socket =
mInfraIfController.createIcmp6Socket(infraIfName);
getOtDaemon()
.configureBorderRouter(
mBorderRouterConfig,
infraIcmp6Socket,
new ConfigureBorderRouterStatusReceiver());
} catch (RemoteException | IOException | ThreadNetworkException e) {
Log.w(TAG, "Failed to enable border routing", e);
} catch (RemoteException | ThreadNetworkException e) {
Log.w(TAG, "Failed to configure border router " + mBorderRouterConfig, e);
}
}
private void enableBorderRouting(String infraIfName) {
BorderRouterConfiguration borderRouterConfig =
newBorderRouterConfigBuilder(mBorderRouterConfig)
.setIsBorderRoutingEnabled(true)
.setInfraInterfaceName(infraIfName)
.build();
Log.i(TAG, "Enable border routing on AIL: " + infraIfName);
configureBorderRouter(borderRouterConfig);
}
private void disableBorderRouting() {
mUpstreamNetwork = null;
mBorderRouterConfig.infraInterfaceName = null;
mBorderRouterConfig.isBorderRoutingEnabled = false;
try {
getOtDaemon()
.configureBorderRouter(
mBorderRouterConfig,
null /* infraIcmp6Socket */,
new ConfigureBorderRouterStatusReceiver());
} catch (RemoteException | ThreadNetworkException e) {
Log.w(TAG, "Failed to disable border routing", e);
}
BorderRouterConfiguration borderRouterConfig =
newBorderRouterConfigBuilder(mBorderRouterConfig)
.setIsBorderRoutingEnabled(false)
.setInfraInterfaceName(null)
.build();
Log.i(TAG, "Disabling border routing");
configureBorderRouter(borderRouterConfig);
}
private void handleThreadInterfaceStateChanged(boolean isUp) {
@ -1362,6 +1380,13 @@ final class ThreadNetworkControllerService extends IThreadNetworkController.Stub
return builder.build();
}
private static BorderRouterConfiguration.Builder newBorderRouterConfigBuilder(
BorderRouterConfiguration brConfig) {
return new BorderRouterConfiguration.Builder()
.setIsBorderRoutingEnabled(brConfig.isBorderRoutingEnabled)
.setInfraInterfaceName(brConfig.infraInterfaceName);
}
private static final class CallbackMetadata {
private static long gId = 0;