1 /* 2 * Copyright 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.net.thread; 17 18 import android.annotation.FlaggedApi; 19 import android.annotation.NonNull; 20 import android.annotation.SystemApi; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import com.android.net.thread.flags.Flags; 25 26 import java.util.Objects; 27 28 /** 29 * Data interface for Thread device configuration. 30 * 31 * <p>An example usage of creating a {@link ThreadConfiguration} that turns on NAT64 feature based 32 * on an existing {@link ThreadConfiguration}: 33 * 34 * <pre>{@code 35 * ThreadConfiguration config = 36 * new ThreadConfiguration.Builder(existingConfig).setNat64Enabled(true).build(); 37 * }</pre> 38 * 39 * @see ThreadNetworkController#setConfiguration 40 * @see ThreadNetworkController#registerConfigurationCallback 41 * @see ThreadNetworkController#unregisterConfigurationCallback 42 * @hide 43 */ 44 @FlaggedApi(Flags.FLAG_CONFIGURATION_ENABLED) 45 @SystemApi 46 public final class ThreadConfiguration implements Parcelable { 47 private final boolean mBorderRouterEnabled; 48 private final boolean mNat64Enabled; 49 private final boolean mDhcpv6PdEnabled; 50 ThreadConfiguration(Builder builder)51 private ThreadConfiguration(Builder builder) { 52 this(builder.mBorderRouterEnabled, builder.mNat64Enabled, builder.mDhcpv6PdEnabled); 53 } 54 ThreadConfiguration( boolean borderRouterEnabled, boolean nat64Enabled, boolean dhcpv6PdEnabled)55 private ThreadConfiguration( 56 boolean borderRouterEnabled, boolean nat64Enabled, boolean dhcpv6PdEnabled) { 57 this.mBorderRouterEnabled = borderRouterEnabled; 58 this.mNat64Enabled = nat64Enabled; 59 this.mDhcpv6PdEnabled = dhcpv6PdEnabled; 60 } 61 62 /** 63 * Returns {@code true} if this device is operating as a Thread Border Router. 64 * 65 * <p>A Thread Border Router works on both Thread and infrastructure networks. For example, it 66 * can route packets between Thread and infrastructure networks (e.g. Wi-Fi or Ethernet), makes 67 * devices in both networks discoverable to each other, and accepts connections from external 68 * commissioner. 69 * 70 * <p>Note it costs significantly more power to operate as a Border Router, so this is typically 71 * only enabled for wired Android devices (e.g. TV or display). 72 * 73 * @hide 74 */ isBorderRouterEnabled()75 public boolean isBorderRouterEnabled() { 76 return mBorderRouterEnabled; 77 } 78 79 /** Returns {@code true} if NAT64 is enabled. */ isNat64Enabled()80 public boolean isNat64Enabled() { 81 return mNat64Enabled; 82 } 83 84 /** 85 * Returns {@code true} if DHCPv6 Prefix Delegation is enabled. 86 * 87 * @hide 88 */ isDhcpv6PdEnabled()89 public boolean isDhcpv6PdEnabled() { 90 return mDhcpv6PdEnabled; 91 } 92 93 @Override equals(Object other)94 public boolean equals(Object other) { 95 if (this == other) { 96 return true; 97 } else if (!(other instanceof ThreadConfiguration)) { 98 return false; 99 } else { 100 ThreadConfiguration otherConfig = (ThreadConfiguration) other; 101 return mBorderRouterEnabled == otherConfig.mBorderRouterEnabled 102 && mNat64Enabled == otherConfig.mNat64Enabled 103 && mDhcpv6PdEnabled == otherConfig.mDhcpv6PdEnabled; 104 } 105 } 106 107 @Override hashCode()108 public int hashCode() { 109 return Objects.hash(mBorderRouterEnabled, mNat64Enabled, mDhcpv6PdEnabled); 110 } 111 112 @Override toString()113 public String toString() { 114 StringBuilder sb = new StringBuilder(); 115 sb.append('{'); 116 sb.append("borderRouterEnabled=").append(mBorderRouterEnabled); 117 sb.append(", nat64Enabled=").append(mNat64Enabled); 118 sb.append(", dhcpv6PdEnabled=").append(mDhcpv6PdEnabled); 119 sb.append('}'); 120 return sb.toString(); 121 } 122 123 @Override describeContents()124 public int describeContents() { 125 return 0; 126 } 127 128 @Override writeToParcel(@onNull Parcel dest, int flags)129 public void writeToParcel(@NonNull Parcel dest, int flags) { 130 dest.writeBoolean(mBorderRouterEnabled); 131 dest.writeBoolean(mNat64Enabled); 132 dest.writeBoolean(mDhcpv6PdEnabled); 133 } 134 135 public static final @NonNull Creator<ThreadConfiguration> CREATOR = 136 new Creator<>() { 137 @Override 138 public ThreadConfiguration createFromParcel(Parcel in) { 139 ThreadConfiguration.Builder builder = new ThreadConfiguration.Builder(); 140 builder.setBorderRouterEnabled(in.readBoolean()); 141 builder.setNat64Enabled(in.readBoolean()); 142 builder.setDhcpv6PdEnabled(in.readBoolean()); 143 return builder.build(); 144 } 145 146 @Override 147 public ThreadConfiguration[] newArray(int size) { 148 return new ThreadConfiguration[size]; 149 } 150 }; 151 152 /** 153 * The builder for creating {@link ThreadConfiguration} objects. 154 * 155 * @hide 156 */ 157 @FlaggedApi(Flags.FLAG_SET_NAT64_CONFIGURATION_ENABLED) 158 @SystemApi 159 public static final class Builder { 160 // Thread in Android V is default to a Border Router device, so the default value here needs 161 // to be {@code true} to be compatible. 162 private boolean mBorderRouterEnabled = true; 163 164 private boolean mNat64Enabled = false; 165 private boolean mDhcpv6PdEnabled = false; 166 167 /** 168 * Creates a new {@link Builder} object with all features disabled. 169 * 170 * @hide 171 */ 172 @FlaggedApi(Flags.FLAG_SET_NAT64_CONFIGURATION_ENABLED) 173 @SystemApi Builder()174 public Builder() {} 175 176 /** 177 * Creates a new {@link Builder} object from a {@link ThreadConfiguration} object. 178 * 179 * @param config the Border Router configurations to be copied 180 * @hide 181 */ 182 @FlaggedApi(Flags.FLAG_SET_NAT64_CONFIGURATION_ENABLED) 183 @SystemApi Builder(@onNull ThreadConfiguration config)184 public Builder(@NonNull ThreadConfiguration config) { 185 Objects.requireNonNull(config); 186 187 mBorderRouterEnabled = config.mBorderRouterEnabled; 188 mNat64Enabled = config.mNat64Enabled; 189 mDhcpv6PdEnabled = config.mDhcpv6PdEnabled; 190 } 191 192 /** 193 * Enables or disables this device as a Border Router. 194 * 195 * <p>Defaults to {@code true} if this method is not called. 196 * 197 * @see ThreadConfiguration#isBorderRouterEnabled 198 * @hide 199 */ 200 @NonNull setBorderRouterEnabled(boolean enabled)201 public Builder setBorderRouterEnabled(boolean enabled) { 202 this.mBorderRouterEnabled = enabled; 203 return this; 204 } 205 206 /** 207 * Enables or disables NAT64 for the device. 208 * 209 * <p>Enabling this feature will allow Thread devices to connect to the internet/cloud over 210 * IPv4. 211 * 212 * @hide 213 */ 214 @FlaggedApi(Flags.FLAG_SET_NAT64_CONFIGURATION_ENABLED) 215 @SystemApi 216 @NonNull setNat64Enabled(boolean enabled)217 public Builder setNat64Enabled(boolean enabled) { 218 this.mNat64Enabled = enabled; 219 return this; 220 } 221 222 /** 223 * Enables or disables Prefix Delegation for the device. 224 * 225 * <p>Enabling this feature will allow Thread devices to connect to the internet/cloud over 226 * IPv6. 227 * 228 * @hide 229 */ 230 @NonNull setDhcpv6PdEnabled(boolean enabled)231 public Builder setDhcpv6PdEnabled(boolean enabled) { 232 this.mDhcpv6PdEnabled = enabled; 233 return this; 234 } 235 236 /** 237 * Creates a new {@link ThreadConfiguration} object. 238 * 239 * @hide 240 */ 241 @FlaggedApi(Flags.FLAG_SET_NAT64_CONFIGURATION_ENABLED) 242 @SystemApi 243 @NonNull build()244 public ThreadConfiguration build() { 245 return new ThreadConfiguration(this); 246 } 247 } 248 } 249