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