1 /* 2 * Copyright (C) 2016 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.nl80211; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.SystemApi; 23 import android.net.MacAddress; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 import android.util.Log; 27 28 import com.android.internal.annotations.VisibleForTesting; 29 30 import java.lang.annotation.Retention; 31 import java.lang.annotation.RetentionPolicy; 32 import java.util.ArrayList; 33 import java.util.Arrays; 34 import java.util.List; 35 36 /** 37 * Raw scan result data from the wificond daemon. 38 * 39 * @hide 40 */ 41 @SystemApi 42 public final class NativeScanResult implements Parcelable { 43 private static final String TAG = "NativeScanResult"; 44 45 /** @hide */ 46 @VisibleForTesting 47 public byte[] ssid; 48 /** @hide */ 49 @VisibleForTesting 50 public byte[] bssid; 51 /** @hide */ 52 @VisibleForTesting 53 public byte[] infoElement; 54 /** @hide */ 55 @VisibleForTesting 56 public int frequency; 57 /** @hide */ 58 @VisibleForTesting 59 public int signalMbm; 60 /** @hide */ 61 @VisibleForTesting 62 public long tsf; 63 /** @hide */ 64 @VisibleForTesting 65 @BssCapabilityBits public int capability; 66 /** @hide */ 67 @VisibleForTesting 68 public boolean associated; 69 /** @hide */ 70 @VisibleForTesting 71 public List<RadioChainInfo> radioChainInfos; 72 73 /** 74 * Returns the SSID raw byte array of the AP represented by this scan result. 75 * 76 * @return A byte array. 77 */ getSsid()78 @NonNull public byte[] getSsid() { 79 return ssid; 80 } 81 82 /** 83 * Returns the MAC address (BSSID) of the AP represented by this scan result. 84 * 85 * @return a MacAddress or null on error. 86 */ getBssid()87 @Nullable public MacAddress getBssid() { 88 try { 89 return MacAddress.fromBytes(bssid); 90 } catch (IllegalArgumentException e) { 91 Log.e(TAG, "Illegal argument " + Arrays.toString(bssid), e); 92 return null; 93 } 94 } 95 96 /** 97 * Returns the raw bytes of the information element advertised by the AP represented by this 98 * scan result. 99 * 100 * @return A byte array, possibly null or containing an invalid TLV configuration. 101 */ getInformationElements()102 @NonNull public byte[] getInformationElements() { 103 return infoElement; 104 } 105 106 /** 107 * Returns the frequency (in MHz) on which the AP represented by this scan result was observed. 108 * 109 * @return The frequency in MHz. 110 */ getFrequencyMhz()111 public int getFrequencyMhz() { 112 return frequency; 113 } 114 115 /** 116 * Return the signal strength of probe response/beacon in (100 * dBm). 117 * 118 * @return Signal strenght in (100 * dBm). 119 */ getSignalMbm()120 public int getSignalMbm() { 121 return signalMbm; 122 } 123 124 /** 125 * Return the TSF (Timing Synchronization Function) of the received probe response/beacon. 126 * @return 127 */ getTsf()128 public long getTsf() { 129 return tsf; 130 } 131 132 /** 133 * Return a boolean indicating whether or not we're associated to the AP represented by this 134 * scan result. 135 * 136 * @return A boolean indicating association. 137 */ isAssociated()138 public boolean isAssociated() { 139 return associated; 140 } 141 142 /** @hide */ 143 @Retention(RetentionPolicy.SOURCE) 144 @IntDef(flag = true, prefix = {"BSS_CAPABILITY_"}, 145 value = {BSS_CAPABILITY_ESS, 146 BSS_CAPABILITY_IBSS, 147 BSS_CAPABILITY_CF_POLLABLE, 148 BSS_CAPABILITY_CF_POLL_REQUEST, 149 BSS_CAPABILITY_PRIVACY, 150 BSS_CAPABILITY_SHORT_PREAMBLE, 151 BSS_CAPABILITY_PBCC, 152 BSS_CAPABILITY_CHANNEL_AGILITY, 153 BSS_CAPABILITY_SPECTRUM_MANAGEMENT, 154 BSS_CAPABILITY_QOS, 155 BSS_CAPABILITY_SHORT_SLOT_TIME, 156 BSS_CAPABILITY_APSD, 157 BSS_CAPABILITY_RADIO_MANAGEMENT, 158 BSS_CAPABILITY_DSSS_OFDM, 159 BSS_CAPABILITY_DELAYED_BLOCK_ACK, 160 BSS_CAPABILITY_IMMEDIATE_BLOCK_ACK, 161 BSS_CAPABILITY_DMG_ESS, 162 BSS_CAPABILITY_DMG_IBSS 163 }) 164 public @interface BssCapabilityBits { } 165 166 /** 167 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): ESS. 168 */ 169 public static final int BSS_CAPABILITY_ESS = 0x1 << 0; 170 /** 171 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): IBSS. 172 */ 173 public static final int BSS_CAPABILITY_IBSS = 0x1 << 1; 174 /** 175 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): CF Pollable. 176 */ 177 public static final int BSS_CAPABILITY_CF_POLLABLE = 0x1 << 2; 178 /** 179 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): CF-Poll Request. 180 */ 181 public static final int BSS_CAPABILITY_CF_POLL_REQUEST = 0x1 << 3; 182 /** 183 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): Privacy. 184 */ 185 public static final int BSS_CAPABILITY_PRIVACY = 0x1 << 4; 186 /** 187 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): Short Preamble. 188 */ 189 public static final int BSS_CAPABILITY_SHORT_PREAMBLE = 0x1 << 5; 190 /** 191 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): PBCC. 192 */ 193 public static final int BSS_CAPABILITY_PBCC = 0x1 << 6; 194 /** 195 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): Channel Agility. 196 */ 197 public static final int BSS_CAPABILITY_CHANNEL_AGILITY = 0x1 << 7; 198 /** 199 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): Spectrum Management. 200 */ 201 public static final int BSS_CAPABILITY_SPECTRUM_MANAGEMENT = 0x1 << 8; 202 /** 203 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): QoS. 204 */ 205 public static final int BSS_CAPABILITY_QOS = 0x1 << 9; 206 /** 207 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): Short Slot Time. 208 */ 209 public static final int BSS_CAPABILITY_SHORT_SLOT_TIME = 0x1 << 10; 210 /** 211 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): APSD. 212 */ 213 public static final int BSS_CAPABILITY_APSD = 0x1 << 11; 214 /** 215 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): Radio Management. 216 */ 217 public static final int BSS_CAPABILITY_RADIO_MANAGEMENT = 0x1 << 12; 218 /** 219 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): DSSS-OFDM. 220 */ 221 public static final int BSS_CAPABILITY_DSSS_OFDM = 0x1 << 13; 222 /** 223 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): Delayed Block Ack. 224 */ 225 public static final int BSS_CAPABILITY_DELAYED_BLOCK_ACK = 0x1 << 14; 226 /** 227 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): Immediate Block Ack. 228 */ 229 public static final int BSS_CAPABILITY_IMMEDIATE_BLOCK_ACK = 0x1 << 15; 230 /** 231 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): DMG ESS. 232 * In DMG bits 0 and 1 are parsed together, where ESS=0x3 and IBSS=0x1 233 */ 234 public static final int BSS_CAPABILITY_DMG_ESS = 0x3; 235 /** 236 * BSS capability bit (see IEEE Std 802.11: 9.4.1.4): DMG IBSS. 237 */ 238 public static final int BSS_CAPABILITY_DMG_IBSS = 0x1; 239 240 /** 241 * Returns the capabilities of the AP repseresented by this scan result as advertised in the 242 * received probe response or beacon. 243 * 244 * This is a bit mask describing the capabilities of a BSS. See IEEE Std 802.11: 9.4.1.4: one 245 * of the {@code BSS_CAPABILITY_*} flags. 246 * 247 * @return a bit mask of capabilities. 248 */ getCapabilities()249 @BssCapabilityBits public int getCapabilities() { 250 return capability; 251 } 252 253 /** 254 * Returns details of the signal received on each radio chain for the AP represented by this 255 * scan result in a list of {@link RadioChainInfo} elements. 256 * 257 * @return A list of {@link RadioChainInfo} - possibly empty in case of error. 258 */ getRadioChainInfos()259 @NonNull public List<RadioChainInfo> getRadioChainInfos() { 260 return radioChainInfos; 261 } 262 263 /** 264 * Construct an empty native scan result. 265 */ NativeScanResult()266 public NativeScanResult() { } 267 268 /** implement Parcelable interface */ 269 @Override describeContents()270 public int describeContents() { 271 return 0; 272 } 273 274 /** implement Parcelable interface */ 275 @Override writeToParcel(@onNull Parcel out, int flags)276 public void writeToParcel(@NonNull Parcel out, int flags) { 277 out.writeByteArray(ssid); 278 out.writeByteArray(bssid); 279 out.writeByteArray(infoElement); 280 out.writeInt(frequency); 281 out.writeInt(signalMbm); 282 out.writeLong(tsf); 283 out.writeInt(capability); 284 out.writeInt(associated ? 1 : 0); 285 out.writeTypedList(radioChainInfos); 286 } 287 288 /** implement Parcelable interface */ 289 @NonNull public static final Parcelable.Creator<NativeScanResult> CREATOR = 290 new Parcelable.Creator<NativeScanResult>() { 291 @Override 292 public NativeScanResult createFromParcel(Parcel in) { 293 NativeScanResult result = new NativeScanResult(); 294 result.ssid = in.createByteArray(); 295 if (result.ssid == null) { 296 result.ssid = new byte[0]; 297 } 298 result.bssid = in.createByteArray(); 299 if (result.bssid == null) { 300 result.bssid = new byte[0]; 301 } 302 result.infoElement = in.createByteArray(); 303 if (result.infoElement == null) { 304 result.infoElement = new byte[0]; 305 } 306 result.frequency = in.readInt(); 307 result.signalMbm = in.readInt(); 308 result.tsf = in.readLong(); 309 result.capability = in.readInt(); 310 result.associated = (in.readInt() != 0); 311 result.radioChainInfos = new ArrayList<>(); 312 in.readTypedList(result.radioChainInfos, RadioChainInfo.CREATOR); 313 return result; 314 } 315 316 @Override 317 public NativeScanResult[] newArray(int size) { 318 return new NativeScanResult[size]; 319 } 320 }; 321 } 322