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 * @hide 105 */ 106 @Retention(RetentionPolicy.SOURCE) 107 @IntDef(flag = true, prefix = {"FILTER_"}, value = { 108 FILTER_REGULATORY, 109 FILTER_CELLULAR_COEXISTENCE, 110 FILTER_CONCURRENCY, 111 }) 112 public @interface Filter {} 113 114 /** 115 * Wifi channel frequency in MHz. 116 */ 117 private int mFrequency; 118 119 /** 120 * Bitwise OR of modes (OP_MODE_*) allowed on this channel. 121 */ 122 private @OpMode int mOpModes; 123 WifiAvailableChannel(int freq, @OpMode int opModes)124 public WifiAvailableChannel(int freq, @OpMode int opModes) { 125 mFrequency = freq; 126 mOpModes = opModes; 127 } 128 WifiAvailableChannel(@onNull Parcel in)129 private WifiAvailableChannel(@NonNull Parcel in) { 130 readFromParcel(in); 131 } 132 readFromParcel(@onNull Parcel in)133 private void readFromParcel(@NonNull Parcel in) { 134 mFrequency = in.readInt(); 135 mOpModes = in.readInt(); 136 } 137 138 /** 139 * Get the channel frequency in MHz. 140 */ getFrequencyMhz()141 public int getFrequencyMhz() { 142 return mFrequency; 143 } 144 145 /** 146 * Get the operational modes allowed on a channel. 147 */ getOperationalModes()148 public @OpMode int getOperationalModes() { 149 return mOpModes; 150 } 151 152 /** 153 * Usable filter implies filter channels by regulatory constraints and 154 * other dynamic constraints like concurrency state and interference due 155 * to other radios like cellular. 156 * @hide 157 */ getUsableFilter()158 public static @Filter int getUsableFilter() { 159 return FILTER_REGULATORY 160 | FILTER_CONCURRENCY 161 | FILTER_CELLULAR_COEXISTENCE; 162 } 163 164 @Override describeContents()165 public int describeContents() { 166 return 0; 167 } 168 169 @Override equals(@ullable Object o)170 public boolean equals(@Nullable Object o) { 171 if (this == o) return true; 172 if (o == null || getClass() != o.getClass()) return false; 173 WifiAvailableChannel that = (WifiAvailableChannel) o; 174 return mFrequency == that.mFrequency 175 && mOpModes == that.mOpModes; 176 } 177 178 @Override hashCode()179 public int hashCode() { 180 return Objects.hash(mFrequency, mOpModes); 181 } 182 183 @Override toString()184 public String toString() { 185 StringBuilder sbuf = new StringBuilder(); 186 sbuf.append("mFrequency = ") 187 .append(mFrequency) 188 .append(", mOpModes = ") 189 .append(String.format("%x", mOpModes)); 190 return sbuf.toString(); 191 } 192 193 @Override writeToParcel(@onNull Parcel dest, int flags)194 public void writeToParcel(@NonNull Parcel dest, int flags) { 195 dest.writeInt(mFrequency); 196 dest.writeInt(mOpModes); 197 } 198 199 public static final @android.annotation.NonNull Creator<WifiAvailableChannel> CREATOR = 200 new Creator<WifiAvailableChannel>() { 201 @Override 202 public WifiAvailableChannel createFromParcel(@NonNull Parcel in) { 203 return new WifiAvailableChannel(in); 204 } 205 206 @Override 207 public WifiAvailableChannel[] newArray(int size) { 208 return new WifiAvailableChannel[size]; 209 } 210 }; 211 } 212