From ed24de9b64c471cb938fc367385e51cc1d84b027 Mon Sep 17 00:00:00 2001 From: Handa Wang Date: Wed, 7 Aug 2024 02:36:19 +0000 Subject: [PATCH] [Thread] use immutable BorderRouterConfiguration This CL refactors the code of ThreadNetworkControllerService to use immutable BorderRouterConfiguration. Bug: 358012289 Test: CI Change-Id: Ifabf0144d5e8f5b5da2b8d64fc9e53c46f917dbb --- .../ThreadNetworkControllerService.java | 69 +++++++++++++------ 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/thread/service/java/com/android/server/thread/ThreadNetworkControllerService.java b/thread/service/java/com/android/server/thread/ThreadNetworkControllerService.java index 622b482a98..b621a6a51d 100644 --- a/thread/service/java/com/android/server/thread/ThreadNetworkControllerService.java +++ b/thread/service/java/com/android/server/thread/ThreadNetworkControllerService.java @@ -238,7 +238,11 @@ final class ThreadNetworkControllerService extends IThreadNetworkController.Stub mInfraIfController = infraIfController; mUpstreamNetworkRequest = newUpstreamNetworkRequest(); mNetworkToInterface = new HashMap(); - 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;