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