• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.wifi;
17 
18 import android.annotation.IntDef;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.util.Objects;
27 
28 /**
29  * Contains information about a Wifi channel and bitmask of Wifi operational modes allowed on that
30  * channel. Use {@link WifiManager#getAllowedChannels(int, int)} to retrieve the list of channels
31  * filtered by regulatory constraints. Use {@link WifiManager#getUsableChannels(int, int)} to
32  * retrieve the list of channels filtered by regulatory and dynamic constraints like concurrency and
33  * interference due to other radios.
34  */
35 public final class WifiAvailableChannel implements Parcelable {
36 
37     /**
38      * Wifi Infrastructure client (STA) operational mode.
39      */
40     public static final int OP_MODE_STA = 1 << 0;
41 
42     /**
43      * Wifi SoftAp (Mobile Hotspot) operational mode.
44      */
45     public static final int OP_MODE_SAP = 1 << 1;
46 
47     /**
48      * Wifi Direct client (CLI) operational mode.
49      */
50     public static final int OP_MODE_WIFI_DIRECT_CLI = 1 << 2;
51 
52     /**
53      * Wifi Direct Group Owner (GO) operational mode.
54      */
55     public static final int OP_MODE_WIFI_DIRECT_GO = 1 << 3;
56 
57     /**
58      * Wifi Aware (NAN) operational mode.
59      */
60     public static final int OP_MODE_WIFI_AWARE = 1 << 4;
61 
62     /**
63      * Wifi Tunneled Direct Link Setup (TDLS) operational mode.
64      */
65     public static final int OP_MODE_TDLS = 1 << 5;
66 
67     /**
68      * @hide
69      */
70     @Retention(RetentionPolicy.SOURCE)
71     @IntDef(flag = true, prefix = {"OP_MODE_"}, value = {
72             OP_MODE_STA,
73             OP_MODE_SAP,
74             OP_MODE_WIFI_DIRECT_CLI,
75             OP_MODE_WIFI_DIRECT_GO,
76             OP_MODE_WIFI_AWARE,
77             OP_MODE_TDLS,
78     })
79     public @interface OpMode {}
80 
81     /**
82      * Filter channel based on regulatory constraints.
83      * @hide
84      */
85     public static final int FILTER_REGULATORY = 0;
86 
87     /**
88      * Filter channel based on interference from cellular radio.
89      * @hide
90      */
91     public static final int FILTER_CELLULAR_COEXISTENCE = 1 << 0;
92 
93     /**
94      * Filter channel based on current concurrency state.
95      * @hide
96      */
97     public static final int FILTER_CONCURRENCY = 1 << 1;
98 
99     /**
100      * Filter channel for the Wi-Fi Aware instant communication mode.
101      * @hide
102      */
103     public static final int FILTER_NAN_INSTANT_MODE = 1 << 2;
104 
105     /**
106      * @hide
107      */
108     @Retention(RetentionPolicy.SOURCE)
109     @IntDef(flag = true, prefix = {"FILTER_"}, value = {
110             FILTER_REGULATORY,
111             FILTER_CELLULAR_COEXISTENCE,
112             FILTER_CONCURRENCY,
113             FILTER_NAN_INSTANT_MODE,
114     })
115     public @interface Filter {}
116 
117     /**
118      * Wifi channel frequency in MHz.
119      */
120     private int mFrequency;
121 
122     /**
123      * Bitwise OR of modes (OP_MODE_*) allowed on this channel.
124      */
125     private @OpMode int mOpModes;
126 
WifiAvailableChannel(int freq, @OpMode int opModes)127     public WifiAvailableChannel(int freq, @OpMode int opModes) {
128         mFrequency = freq;
129         mOpModes = opModes;
130     }
131 
WifiAvailableChannel(@onNull Parcel in)132     private WifiAvailableChannel(@NonNull Parcel in) {
133         readFromParcel(in);
134     }
135 
readFromParcel(@onNull Parcel in)136     private void readFromParcel(@NonNull Parcel in) {
137         mFrequency = in.readInt();
138         mOpModes = in.readInt();
139     }
140 
141     /**
142      * Get the channel frequency in MHz.
143      */
getFrequencyMhz()144     public int getFrequencyMhz() {
145         return mFrequency;
146     }
147 
148     /**
149      * Get the operational modes allowed on a channel.
150      */
getOperationalModes()151     public @OpMode int getOperationalModes() {
152         return mOpModes;
153     }
154 
155     /**
156      * Usable filter implies filter channels by regulatory constraints and
157      * other dynamic constraints like concurrency state and interference due
158      * to other radios like cellular.
159      * @hide
160      */
getUsableFilter()161     public static @Filter int getUsableFilter() {
162         return FILTER_REGULATORY
163                 | FILTER_CONCURRENCY
164                 | FILTER_CELLULAR_COEXISTENCE;
165     }
166 
167     @Override
describeContents()168     public int describeContents() {
169         return 0;
170     }
171 
172     @Override
equals(@ullable Object o)173     public boolean equals(@Nullable Object o) {
174         if (this == o) return true;
175         if (o == null || getClass() != o.getClass()) return false;
176         WifiAvailableChannel that = (WifiAvailableChannel) o;
177         return mFrequency == that.mFrequency
178                 && mOpModes == that.mOpModes;
179     }
180 
181     @Override
hashCode()182     public int hashCode() {
183         return Objects.hash(mFrequency, mOpModes);
184     }
185 
186     @Override
toString()187     public String toString() {
188         StringBuilder sbuf = new StringBuilder();
189         sbuf.append("mFrequency = ")
190             .append(mFrequency)
191             .append(", mOpModes = ")
192             .append(String.format("%x", mOpModes));
193         return sbuf.toString();
194     }
195 
196     @Override
writeToParcel(@onNull Parcel dest, int flags)197     public void writeToParcel(@NonNull Parcel dest, int flags) {
198         dest.writeInt(mFrequency);
199         dest.writeInt(mOpModes);
200     }
201 
202     public static final @android.annotation.NonNull Creator<WifiAvailableChannel> CREATOR =
203             new Creator<WifiAvailableChannel>() {
204                 @Override
205                 public WifiAvailableChannel createFromParcel(@NonNull Parcel in) {
206                     return new WifiAvailableChannel(in);
207                 }
208 
209                 @Override
210                 public WifiAvailableChannel[] newArray(int size) {
211                     return new WifiAvailableChannel[size];
212                 }
213             };
214 }
215