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.aware; 18 19 import static com.android.ranging.flags.Flags.FLAG_RANGING_RTT_ENABLED; 20 21 import android.annotation.FlaggedApi; 22 import android.annotation.IntDef; 23 import android.annotation.IntRange; 24 import android.annotation.SystemApi; 25 import android.net.wifi.WifiAnnotations; 26 import android.os.Build; 27 import android.os.Bundle; 28 import android.os.Parcel; 29 import android.os.Parcelable; 30 31 import androidx.annotation.RequiresApi; 32 33 import com.android.modules.utils.build.SdkLevel; 34 35 import java.lang.annotation.Retention; 36 import java.lang.annotation.RetentionPolicy; 37 38 /** 39 * The characteristics of the Wi-Fi Aware implementation. 40 */ 41 public final class Characteristics implements Parcelable { 42 /** @hide */ 43 public static final String KEY_MAX_SERVICE_NAME_LENGTH = "key_max_service_name_length"; 44 /** @hide */ 45 public static final String KEY_MAX_SERVICE_SPECIFIC_INFO_LENGTH = 46 "key_max_service_specific_info_length"; 47 /** @hide */ 48 public static final String KEY_MAX_MATCH_FILTER_LENGTH = "key_max_match_filter_length"; 49 /** @hide */ 50 public static final String KEY_SUPPORTED_DATA_PATH_CIPHER_SUITES = 51 "key_supported_data_path_cipher_suites"; 52 /** @hide */ 53 public static final String KEY_SUPPORTED_PAIRING_CIPHER_SUITES = 54 "key_supported_pairing_cipher_suites"; 55 /** @hide */ 56 public static final String KEY_IS_INSTANT_COMMUNICATION_MODE_SUPPORTED = 57 "key_is_instant_communication_mode_supported"; 58 /** @hide */ 59 public static final String KEY_MAX_NDP_NUMBER = "key_max_ndp_number"; 60 /** @hide */ 61 public static final String KEY_MAX_PUBLISH_NUMBER = "key_max_publish_number"; 62 /** @hide */ 63 public static final String KEY_MAX_SUBSCRIBE_NUMBER = "key_max_subscribe_number"; 64 /** @hide */ 65 public static final String KEY_MAX_NDI_NUMBER = "key_max_ndi_number"; 66 /** @hide */ 67 public static final String KEY_SUPPORT_NAN_PAIRING = "key_support_nan_pairing"; 68 /** @hide */ 69 public static final String KEY_SUPPORT_SUSPENSION = "key_support_suspension"; 70 /** @hide */ 71 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 72 public static final String KEY_SUPPORT_PERIODIC_RANGING = "key_support_periodic_ranging"; 73 /** @hide */ 74 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 75 public static final String KEY_MAX_SUPPORTED_RANGING_PKT_BANDWIDTH = 76 "key_max_supported_ranging_pkt_bandwidth"; 77 /** @hide */ 78 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 79 public static final String KEY_MAX_SUPPORTED_RX_CHAINS = "key_max_supported_rx_chains"; 80 81 private final Bundle mCharacteristics; 82 83 /** @hide : should not be created by apps */ Characteristics(Bundle characteristics)84 public Characteristics(Bundle characteristics) { 85 mCharacteristics = characteristics; 86 } 87 88 /** 89 * Returns the maximum string length that can be used to specify a Aware service name. Restricts 90 * the parameters of the {@link PublishConfig.Builder#setServiceName(String)} and 91 * {@link SubscribeConfig.Builder#setServiceName(String)}. 92 * 93 * @return A positive integer, maximum string length of Aware service name. 94 */ getMaxServiceNameLength()95 public int getMaxServiceNameLength() { 96 return mCharacteristics.getInt(KEY_MAX_SERVICE_NAME_LENGTH); 97 } 98 99 /** 100 * Returns the maximum length of byte array that can be used to specify a Aware service specific 101 * information field: the arbitrary load used in discovery or the message length of Aware 102 * message exchange. Restricts the parameters of the 103 * {@link PublishConfig.Builder#setServiceSpecificInfo(byte[])}, 104 * {@link SubscribeConfig.Builder#setServiceSpecificInfo(byte[])}, and 105 * {@link DiscoverySession#sendMessage(PeerHandle, int, byte[])} 106 * variants. 107 * 108 * @return A positive integer, maximum length of byte array for Aware messaging. 109 */ getMaxServiceSpecificInfoLength()110 public int getMaxServiceSpecificInfoLength() { 111 return mCharacteristics.getInt(KEY_MAX_SERVICE_SPECIFIC_INFO_LENGTH); 112 } 113 114 /** 115 * Returns the maximum length of byte array that can be used to specify a Aware match filter. 116 * Restricts the parameters of the 117 * {@link PublishConfig.Builder#setMatchFilter(java.util.List)} and 118 * {@link SubscribeConfig.Builder#setMatchFilter(java.util.List)}. 119 * 120 * @return A positive integer, maximum length of byte array for Aware discovery match filter. 121 */ getMaxMatchFilterLength()122 public int getMaxMatchFilterLength() { 123 return mCharacteristics.getInt(KEY_MAX_MATCH_FILTER_LENGTH); 124 } 125 126 /** 127 * Returns the maximum number of Aware data interfaces supported by the device. 128 * 129 * @return A positive integer, maximum number of Aware data interfaces supported by the device. 130 */ 131 @IntRange(from = 1) getNumberOfSupportedDataInterfaces()132 public int getNumberOfSupportedDataInterfaces() { 133 return mCharacteristics.getInt(KEY_MAX_NDI_NUMBER); 134 } 135 136 /** 137 * Returns the maximum number of Aware publish sessions supported by the device. 138 * Use {@link AwareResources#getAvailablePublishSessionsCount()} to get the number of available 139 * publish sessions which are not currently used by any app. 140 * 141 * @return A positive integer, maximum number of publish sessions supported by the device. 142 */ 143 @IntRange(from = 1) getNumberOfSupportedPublishSessions()144 public int getNumberOfSupportedPublishSessions() { 145 return mCharacteristics.getInt(KEY_MAX_PUBLISH_NUMBER); 146 } 147 148 /** 149 * Returns the maximum number of Aware subscribe session supported by the device. 150 * Use {@link AwareResources#getAvailableSubscribeSessionsCount()} to get the number of 151 * available subscribe sessions which are not currently used by any app. 152 * 153 * @return A positive integer, maximum number of subscribe sessions supported by the device. 154 */ 155 @IntRange(from = 1) getNumberOfSupportedSubscribeSessions()156 public int getNumberOfSupportedSubscribeSessions() { 157 return mCharacteristics.getInt(KEY_MAX_SUBSCRIBE_NUMBER); 158 } 159 160 /** 161 * Returns the maximum number of Aware data paths(also known as NDPs - NAN Data Paths) supported 162 * by the device. 163 * Use {@link AwareResources#getAvailableDataPathsCount()} to get the number of available Aware 164 * data paths which are not currently used by any app. 165 * 166 * @return A positive integer, maximum number of Aware data paths supported by the device. 167 */ 168 @IntRange(from = 1) getNumberOfSupportedDataPaths()169 public int getNumberOfSupportedDataPaths() { 170 return mCharacteristics.getInt(KEY_MAX_NDP_NUMBER); 171 } 172 173 /** 174 * Check if instant communication mode is supported by device. The instant communication mode is 175 * defined as per Wi-Fi Alliance (WFA) Wi-Fi Aware specifications version 3.1 Section 12.3. 176 * @return True if supported, false otherwise. 177 */ 178 @RequiresApi(Build.VERSION_CODES.S) isInstantCommunicationModeSupported()179 public boolean isInstantCommunicationModeSupported() { 180 if (!SdkLevel.isAtLeastS()) { 181 throw new UnsupportedOperationException(); 182 } 183 return mCharacteristics.getBoolean(KEY_IS_INSTANT_COMMUNICATION_MODE_SUPPORTED); 184 } 185 186 187 /** 188 * Check if the Aware Pairing and all associated security features as defined in Wi-Fi Alliance 189 * (WFA) Wi-Fi Aware Specification version 4.0 are supported. 190 * This includes: 191 * <ol> 192 * <li>NAN Pairing (as in Wi-Fi Aware Specification Version 4.0 section 7.6) with NIK caching 193 * <li>NDP unicast data frame encryption (as in Wi-Fi Aware Specification Version 4.0 section 194 * 7.3.1) 195 * <li>Group addressed data frame encryption (as in Wi-Fi Aware Specification Version 4.0 196 * section 7.3.3) 197 * <li>Management frame protection (as in Wi-Fi Aware Specification Version 4.0 section 7.3.2 198 * for both unicast and multicast frames) 199 * <li>Beacon integrity protection (as in Wi-Fi Aware Specification Version 4.0 section 7.3.4) 200 * </ol> 201 * 202 * @return True if supported, false otherwise. 203 */ isAwarePairingSupported()204 public boolean isAwarePairingSupported() { 205 return mCharacteristics.getBoolean(KEY_SUPPORT_NAN_PAIRING); 206 } 207 208 /** 209 * Check if Aware Suspension is supported. Aware Suspension is a mechanism of putting an Aware 210 * connection in and out of a low-power mode while preserving the discovery sessions and data 211 * paths. 212 * @return True if supported, false otherwise. 213 */ isSuspensionSupported()214 public boolean isSuspensionSupported() { 215 return mCharacteristics.getBoolean(KEY_SUPPORT_SUSPENSION); 216 } 217 218 /** 219 * Check if Periodic Ranging is supported. 220 * Periodic Ranging on Aware allows applications to get the asynchronous ranging 221 * report periodically. 222 * @return True if supported, false otherwise. 223 * @hide 224 */ 225 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 226 @SystemApi isPeriodicRangingSupported()227 public boolean isPeriodicRangingSupported() { 228 return mCharacteristics.getBoolean(KEY_SUPPORT_PERIODIC_RANGING); 229 } 230 231 /** @hide */ 232 @IntDef(flag = true, prefix = { "SUPPORTED_RX_CHAINS_" }, value = { 233 SUPPORTED_RX_CHAINS_UNSPECIFIED, 234 SUPPORTED_RX_CHAINS_1, 235 SUPPORTED_RX_CHAINS_2, 236 SUPPORTED_RX_CHAINS_3, 237 SUPPORTED_RX_CHAINS_4, 238 }) 239 @Retention(RetentionPolicy.SOURCE) 240 public @interface SupportedRxChains {} 241 242 /** @hide */ 243 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 244 @SystemApi 245 public static final int SUPPORTED_RX_CHAINS_1 = 1; 246 /** @hide */ 247 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 248 @SystemApi 249 public static final int SUPPORTED_RX_CHAINS_2 = 2; 250 /** @hide */ 251 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 252 @SystemApi 253 public static final int SUPPORTED_RX_CHAINS_3 = 3; 254 /** @hide */ 255 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 256 @SystemApi 257 public static final int SUPPORTED_RX_CHAINS_4 = 4; 258 /** @hide */ 259 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 260 @SystemApi 261 public static final int SUPPORTED_RX_CHAINS_UNSPECIFIED = 0; 262 263 /** 264 * Get the supported number of receive chains. 265 * 266 * @return Number of supported receive chains. 267 * @hide 268 */ 269 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 270 @SystemApi getMaxSupportedRxChains()271 public @SupportedRxChains int getMaxSupportedRxChains() { 272 return mCharacteristics.getInt(KEY_MAX_SUPPORTED_RX_CHAINS); 273 } 274 275 /** 276 * Get Max supported ranging per packet Bandwidth 277 * 278 * @return the bandwidth representation of the Wi-Fi channel from 279 * {@link ScanResult#CHANNEL_WIDTH_20MHZ}, {@link ScanResult#CHANNEL_WIDTH_40MHZ}, 280 * {@link ScanResult#CHANNEL_WIDTH_80MHZ}, {@link ScanResult#CHANNEL_WIDTH_160MHZ}, 281 * or {@link ScanResult#CHANNEL_WIDTH_320MHZ}. 282 * @hide 283 */ 284 @FlaggedApi(FLAG_RANGING_RTT_ENABLED) 285 @SystemApi getMaxSupportedRangingPacketBandwidth()286 public @WifiAnnotations.ChannelWidth int getMaxSupportedRangingPacketBandwidth() { 287 return mCharacteristics.getInt(KEY_MAX_SUPPORTED_RANGING_PKT_BANDWIDTH); 288 } 289 290 /** @hide */ 291 @IntDef(flag = true, prefix = { "WIFI_AWARE_CIPHER_SUITE_" }, value = { 292 WIFI_AWARE_CIPHER_SUITE_NONE, 293 WIFI_AWARE_CIPHER_SUITE_NCS_SK_128, 294 WIFI_AWARE_CIPHER_SUITE_NCS_SK_256, 295 WIFI_AWARE_CIPHER_SUITE_NCS_PK_128, 296 WIFI_AWARE_CIPHER_SUITE_NCS_PK_256, 297 }) 298 @Retention(RetentionPolicy.SOURCE) 299 public @interface WifiAwareDataPathCipherSuites {} 300 301 /** 302 * Wi-Fi Aware supported open (unencrypted) data-path. 303 */ 304 public static final int WIFI_AWARE_CIPHER_SUITE_NONE = 0; 305 /** 306 * Wi-Fi Aware supported cipher suite representing NCS SK 128: 128 bit shared-key. 307 */ 308 public static final int WIFI_AWARE_CIPHER_SUITE_NCS_SK_128 = 1 << 0; 309 310 /** 311 * Wi-Fi Aware supported cipher suite representing NCS SK 256: 256 bit shared-key. 312 */ 313 public static final int WIFI_AWARE_CIPHER_SUITE_NCS_SK_256 = 1 << 1; 314 315 /** 316 * Wi-Fi Aware supported cipher suite representing NCS PK 2WDH 128: 128 bit public-key. 317 */ 318 public static final int WIFI_AWARE_CIPHER_SUITE_NCS_PK_128 = 1 << 2; 319 320 /** 321 * Wi-Fi Aware supported cipher suite representing NCS 2WDH 256: 256 bit public-key. 322 */ 323 public static final int WIFI_AWARE_CIPHER_SUITE_NCS_PK_256 = 1 << 3; 324 325 /** 326 * Wi-Fi Aware supported cipher suite representing NCS PASN 128: 128 bit public-key. 327 */ 328 public static final int WIFI_AWARE_CIPHER_SUITE_NCS_PK_PASN_128 = 1 << 4; 329 330 /** 331 * Wi-Fi Aware supported cipher suite representing NCS PASN 256: 256 bit public-key. 332 */ 333 public static final int WIFI_AWARE_CIPHER_SUITE_NCS_PK_PASN_256 = 1 << 5; 334 335 /** @hide */ 336 @IntDef(flag = true, prefix = { "WIFI_AWARE_CIPHER_SUITE_NCS_PK_PASN_" }, value = { 337 WIFI_AWARE_CIPHER_SUITE_NCS_PK_PASN_128, 338 WIFI_AWARE_CIPHER_SUITE_NCS_PK_PASN_256 339 }) 340 @Retention(RetentionPolicy.SOURCE) 341 public @interface WifiAwarePairingCipherSuites {} 342 343 /** 344 * Returns the set of cipher suites supported by the device for use in Wi-Fi Aware data-paths. 345 * The device automatically picks the strongest cipher suite when initiating a data-path setup. 346 * 347 * @return A set of flags from {@link #WIFI_AWARE_CIPHER_SUITE_NCS_SK_128}, 348 * {@link #WIFI_AWARE_CIPHER_SUITE_NCS_SK_256}, {@link #WIFI_AWARE_CIPHER_SUITE_NCS_PK_128}, 349 * or {@link #WIFI_AWARE_CIPHER_SUITE_NCS_PK_256} 350 */ getSupportedCipherSuites()351 public @WifiAwareDataPathCipherSuites int getSupportedCipherSuites() { 352 return mCharacteristics.getInt(KEY_SUPPORTED_DATA_PATH_CIPHER_SUITES); 353 } 354 355 /** 356 * Returns the set of cipher suites supported by the device for use in Wi-Fi Aware pairing. 357 * 358 * @return A set of flags from {@link #WIFI_AWARE_CIPHER_SUITE_NCS_PK_PASN_256}, 359 * or {@link #WIFI_AWARE_CIPHER_SUITE_NCS_PK_PASN_256} 360 */ getSupportedPairingCipherSuites()361 public @WifiAwarePairingCipherSuites int getSupportedPairingCipherSuites() { 362 return mCharacteristics.getInt(KEY_SUPPORTED_PAIRING_CIPHER_SUITES); 363 } 364 365 @Override writeToParcel(Parcel dest, int flags)366 public void writeToParcel(Parcel dest, int flags) { 367 dest.writeBundle(mCharacteristics); 368 } 369 370 @Override describeContents()371 public int describeContents() { 372 return 0; 373 } 374 375 public static final @android.annotation.NonNull Creator<Characteristics> CREATOR = 376 new Creator<Characteristics>() { 377 @Override 378 public Characteristics createFromParcel(Parcel in) { 379 Characteristics c = new Characteristics(in.readBundle()); 380 return c; 381 } 382 383 @Override 384 public Characteristics[] newArray(int size) { 385 return new Characteristics[size]; 386 } 387 }; 388 } 389