1 /* 2 * Copyright (C) 2019 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 17 package android.net.wifi; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.net.MacAddress; 23 import android.os.Build; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import androidx.annotation.RequiresApi; 28 29 import com.android.internal.util.Preconditions; 30 import com.android.modules.utils.build.SdkLevel; 31 32 import java.util.Objects; 33 34 /** 35 * A class representing information about SoftAp. 36 * {@see WifiManager} 37 * 38 * @hide 39 */ 40 @SystemApi 41 public final class SoftApInfo implements Parcelable { 42 43 /** 44 * AP Channel bandwidth is invalid. 45 * 46 * @see #getBandwidth() 47 */ 48 public static final int CHANNEL_WIDTH_INVALID = 0; 49 50 /** 51 * AP Channel bandwidth is 20 MHZ but no HT. 52 * 53 * @see #getBandwidth() 54 */ 55 public static final int CHANNEL_WIDTH_20MHZ_NOHT = 1; 56 57 /** 58 * AP Channel bandwidth is 20 MHZ. 59 * 60 * @see #getBandwidth() 61 */ 62 public static final int CHANNEL_WIDTH_20MHZ = 2; 63 64 /** 65 * AP Channel bandwidth is 40 MHZ. 66 * 67 * @see #getBandwidth() 68 */ 69 public static final int CHANNEL_WIDTH_40MHZ = 3; 70 71 /** 72 * AP Channel bandwidth is 80 MHZ. 73 * 74 * @see #getBandwidth() 75 */ 76 public static final int CHANNEL_WIDTH_80MHZ = 4; 77 78 /** 79 * AP Channel bandwidth is 160 MHZ, but 80MHZ + 80MHZ. 80 * 81 * @see #getBandwidth() 82 */ 83 public static final int CHANNEL_WIDTH_80MHZ_PLUS_MHZ = 5; 84 85 /** 86 * AP Channel bandwidth is 160 MHZ. 87 * 88 * @see #getBandwidth() 89 */ 90 public static final int CHANNEL_WIDTH_160MHZ = 6; 91 92 /** 93 * AP Channel bandwidth is 2160 MHZ. 94 * 95 * @see #getBandwidth() 96 */ 97 public static final int CHANNEL_WIDTH_2160MHZ = 7; 98 99 /** 100 * AP Channel bandwidth is 4320 MHZ. 101 * 102 * @see #getBandwidth() 103 */ 104 public static final int CHANNEL_WIDTH_4320MHZ = 8; 105 106 /** 107 * AP Channel bandwidth is 6480 MHZ. 108 * 109 * @see #getBandwidth() 110 */ 111 public static final int CHANNEL_WIDTH_6480MHZ = 9; 112 113 /** 114 * AP Channel bandwidth is 8640 MHZ. 115 * 116 * @see #getBandwidth() 117 */ 118 public static final int CHANNEL_WIDTH_8640MHZ = 10; 119 120 /** The frequency which AP resides on. */ 121 private int mFrequency = 0; 122 123 @WifiAnnotations.Bandwidth 124 private int mBandwidth = CHANNEL_WIDTH_INVALID; 125 126 /** The MAC Address which AP resides on. */ 127 @Nullable 128 private MacAddress mBssid; 129 130 /** The identifier of the AP instance which AP resides on with current info. */ 131 @Nullable 132 private String mApInstanceIdentifier; 133 134 /** 135 * The operational mode of the AP. 136 */ 137 private @WifiAnnotations.WifiStandard int mWifiStandard = ScanResult.WIFI_STANDARD_UNKNOWN; 138 139 /** 140 * The current shutdown timeout millis which applied on Soft AP. 141 */ 142 private long mIdleShutdownTimeoutMillis; 143 144 /** 145 * Get the frequency which AP resides on. 146 */ getFrequency()147 public int getFrequency() { 148 return mFrequency; 149 } 150 151 /** 152 * Set the frequency which AP resides on. 153 * @hide 154 */ setFrequency(int freq)155 public void setFrequency(int freq) { 156 mFrequency = freq; 157 } 158 159 /** 160 * Get AP Channel bandwidth. 161 * 162 * @return One of {@link #CHANNEL_WIDTH_20MHZ}, {@link #CHANNEL_WIDTH_40MHZ}, 163 * {@link #CHANNEL_WIDTH_80MHZ}, {@link #CHANNEL_WIDTH_160MHZ}, 164 * {@link #CHANNEL_WIDTH_80MHZ_PLUS_MHZ}, {@link #CHANNEL_WIDTH_2160MHZ}, 165 * {@link #CHANNEL_WIDTH_4320MHZ}, {@link #CHANNEL_WIDTH_6480MHZ}, 166 * {@link #CHANNEL_WIDTH_8640MHZ}, or {@link #CHANNEL_WIDTH_INVALID}. 167 */ 168 @WifiAnnotations.Bandwidth getBandwidth()169 public int getBandwidth() { 170 return mBandwidth; 171 } 172 173 /** 174 * Set AP Channel bandwidth. 175 * @hide 176 */ setBandwidth(@ifiAnnotations.Bandwidth int bandwidth)177 public void setBandwidth(@WifiAnnotations.Bandwidth int bandwidth) { 178 mBandwidth = bandwidth; 179 } 180 181 /** 182 * Get the MAC address (BSSID) of the AP. Null when AP disabled. 183 */ 184 @RequiresApi(Build.VERSION_CODES.S) 185 @Nullable getBssid()186 public MacAddress getBssid() { 187 if (!SdkLevel.isAtLeastS()) { 188 throw new UnsupportedOperationException(); 189 } 190 return getBssidInternal(); 191 } 192 193 /** 194 * @hide 195 */ 196 @Nullable getBssidInternal()197 public MacAddress getBssidInternal() { 198 return mBssid; 199 } 200 201 /** 202 * Set the MAC address which AP resides on. 203 * <p> 204 * <li>If not set, defaults to null.</li> 205 * @param bssid BSSID, The caller is responsible for avoiding collisions. 206 * @throws IllegalArgumentException when the given BSSID is the all-zero or broadcast MAC 207 * address. 208 * 209 * @hide 210 */ setBssid(@ullable MacAddress bssid)211 public void setBssid(@Nullable MacAddress bssid) { 212 if (bssid != null) { 213 Preconditions.checkArgument(!bssid.equals(WifiManager.ALL_ZEROS_MAC_ADDRESS)); 214 Preconditions.checkArgument(!bssid.equals(MacAddress.BROADCAST_ADDRESS)); 215 } 216 mBssid = bssid; 217 } 218 219 /** 220 * Set the operational mode of the AP. 221 * 222 * @param wifiStandard values from {@link ScanResult}'s {@code WIFI_STANDARD_} 223 * @hide 224 */ setWifiStandard(@ifiAnnotations.WifiStandard int wifiStandard)225 public void setWifiStandard(@WifiAnnotations.WifiStandard int wifiStandard) { 226 mWifiStandard = wifiStandard; 227 } 228 229 /** 230 * Get the operational mode of the AP. 231 * @return valid values from {@link ScanResult}'s {@code WIFI_STANDARD_} 232 */ 233 @RequiresApi(Build.VERSION_CODES.S) getWifiStandard()234 public @WifiAnnotations.WifiStandard int getWifiStandard() { 235 if (!SdkLevel.isAtLeastS()) { 236 throw new UnsupportedOperationException(); 237 } 238 return getWifiStandardInternal(); 239 } 240 241 /** 242 * @hide 243 */ getWifiStandardInternal()244 public @WifiAnnotations.WifiStandard int getWifiStandardInternal() { 245 return mWifiStandard; 246 } 247 248 /** 249 * Set the AP instance identifier. 250 * @hide 251 */ setApInstanceIdentifier(@onNull String apInstanceIdentifier)252 public void setApInstanceIdentifier(@NonNull String apInstanceIdentifier) { 253 mApInstanceIdentifier = apInstanceIdentifier; 254 } 255 256 /** 257 * Get the AP instance identifier. 258 * 259 * The AP instance identifier is a unique identity which can be used to 260 * associate the {@link SoftApInfo} to a specific {@link WifiClient} 261 * - see {@link WifiClient#getApInstanceIdentifier()} 262 * 263 * @hide 264 */ 265 @Nullable getApInstanceIdentifier()266 public String getApInstanceIdentifier() { 267 return mApInstanceIdentifier; 268 } 269 270 271 /** 272 * Set current shutdown timeout millis which applied on Soft AP. 273 * @hide 274 */ setAutoShutdownTimeoutMillis(long idleShutdownTimeoutMillis)275 public void setAutoShutdownTimeoutMillis(long idleShutdownTimeoutMillis) { 276 mIdleShutdownTimeoutMillis = idleShutdownTimeoutMillis; 277 } 278 279 /** 280 * Get auto shutdown timeout in millis. 281 * 282 * The shutdown timeout value is configured by 283 * {@link SoftApConfiguration.Builder#setAutoShutdownEnabled(int)} or 284 * the default timeout setting defined in device overlays. 285 * 286 * A value of 0 means that auto shutdown is disabled. 287 * {@see SoftApConfiguration#isAutoShutdownEnabled()} 288 */ getAutoShutdownTimeoutMillis()289 public long getAutoShutdownTimeoutMillis() { 290 return mIdleShutdownTimeoutMillis; 291 } 292 293 /** 294 * @hide 295 */ SoftApInfo(@ullable SoftApInfo source)296 public SoftApInfo(@Nullable SoftApInfo source) { 297 if (source != null) { 298 mFrequency = source.mFrequency; 299 mBandwidth = source.mBandwidth; 300 mBssid = source.mBssid; 301 mWifiStandard = source.mWifiStandard; 302 mApInstanceIdentifier = source.mApInstanceIdentifier; 303 mIdleShutdownTimeoutMillis = source.mIdleShutdownTimeoutMillis; 304 } 305 } 306 307 /** 308 * @hide 309 */ SoftApInfo()310 public SoftApInfo() { 311 } 312 313 @Override 314 /** Implement the Parcelable interface. */ describeContents()315 public int describeContents() { 316 return 0; 317 } 318 319 @Override 320 /** Implement the Parcelable interface */ writeToParcel(@onNull Parcel dest, int flags)321 public void writeToParcel(@NonNull Parcel dest, int flags) { 322 dest.writeInt(mFrequency); 323 dest.writeInt(mBandwidth); 324 dest.writeParcelable(mBssid, flags); 325 dest.writeInt(mWifiStandard); 326 dest.writeString(mApInstanceIdentifier); 327 dest.writeLong(mIdleShutdownTimeoutMillis); 328 } 329 330 @NonNull 331 /** Implement the Parcelable interface */ 332 public static final Creator<SoftApInfo> CREATOR = new Creator<SoftApInfo>() { 333 public SoftApInfo createFromParcel(Parcel in) { 334 SoftApInfo info = new SoftApInfo(); 335 info.mFrequency = in.readInt(); 336 info.mBandwidth = in.readInt(); 337 info.mBssid = in.readParcelable(MacAddress.class.getClassLoader()); 338 info.mWifiStandard = in.readInt(); 339 info.mApInstanceIdentifier = in.readString(); 340 info.mIdleShutdownTimeoutMillis = in.readLong(); 341 return info; 342 } 343 344 public SoftApInfo[] newArray(int size) { 345 return new SoftApInfo[size]; 346 } 347 }; 348 349 @NonNull 350 @Override toString()351 public String toString() { 352 StringBuilder sbuf = new StringBuilder(); 353 sbuf.append("SoftApInfo{"); 354 sbuf.append("bandwidth= ").append(mBandwidth); 355 sbuf.append(", frequency= ").append(mFrequency); 356 if (mBssid != null) sbuf.append(",bssid=").append(mBssid.toString()); 357 sbuf.append(", wifiStandard= ").append(mWifiStandard); 358 sbuf.append(", mApInstanceIdentifier= ").append(mApInstanceIdentifier); 359 sbuf.append(", mIdleShutdownTimeoutMillis= ").append(mIdleShutdownTimeoutMillis); 360 sbuf.append("}"); 361 return sbuf.toString(); 362 } 363 364 @Override equals(@onNull Object o)365 public boolean equals(@NonNull Object o) { 366 if (this == o) return true; 367 if (!(o instanceof SoftApInfo)) return false; 368 SoftApInfo softApInfo = (SoftApInfo) o; 369 return mFrequency == softApInfo.mFrequency 370 && mBandwidth == softApInfo.mBandwidth 371 && Objects.equals(mBssid, softApInfo.mBssid) 372 && mWifiStandard == softApInfo.mWifiStandard 373 && Objects.equals(mApInstanceIdentifier, softApInfo.mApInstanceIdentifier) 374 && mIdleShutdownTimeoutMillis == softApInfo.mIdleShutdownTimeoutMillis; 375 } 376 377 @Override hashCode()378 public int hashCode() { 379 return Objects.hash(mFrequency, mBandwidth, mBssid, mWifiStandard, mApInstanceIdentifier, 380 mIdleShutdownTimeoutMillis); 381 } 382 } 383