• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.annotation.IntDef;
20 import android.os.Build;
21 import android.os.Bundle;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import androidx.annotation.RequiresApi;
26 
27 import com.android.modules.utils.build.SdkLevel;
28 
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 
32 /**
33  * The characteristics of the Wi-Fi Aware implementation.
34  */
35 public final class Characteristics implements Parcelable {
36     /** @hide */
37     public static final String KEY_MAX_SERVICE_NAME_LENGTH = "key_max_service_name_length";
38     /** @hide */
39     public static final String KEY_MAX_SERVICE_SPECIFIC_INFO_LENGTH =
40             "key_max_service_specific_info_length";
41     /** @hide */
42     public static final String KEY_MAX_MATCH_FILTER_LENGTH = "key_max_match_filter_length";
43     /** @hide */
44     public static final String KEY_SUPPORTED_CIPHER_SUITES = "key_supported_cipher_suites";
45     /** @hide */
46     public static final String KEY_IS_INSTANT_COMMUNICATION_MODE_SUPPORTED =
47             "key_is_instant_communication_mode_supported";
48 
49     private Bundle mCharacteristics = new Bundle();
50 
51     /** @hide : should not be created by apps */
Characteristics(Bundle characteristics)52     public Characteristics(Bundle characteristics) {
53         mCharacteristics = characteristics;
54     }
55 
56     /**
57      * Returns the maximum string length that can be used to specify a Aware service name. Restricts
58      * the parameters of the {@link PublishConfig.Builder#setServiceName(String)} and
59      * {@link SubscribeConfig.Builder#setServiceName(String)}.
60      *
61      * @return A positive integer, maximum string length of Aware service name.
62      */
getMaxServiceNameLength()63     public int getMaxServiceNameLength() {
64         return mCharacteristics.getInt(KEY_MAX_SERVICE_NAME_LENGTH);
65     }
66 
67     /**
68      * Returns the maximum length of byte array that can be used to specify a Aware service specific
69      * information field: the arbitrary load used in discovery or the message length of Aware
70      * message exchange. Restricts the parameters of the
71      * {@link PublishConfig.Builder#setServiceSpecificInfo(byte[])},
72      * {@link SubscribeConfig.Builder#setServiceSpecificInfo(byte[])}, and
73      * {@link DiscoverySession#sendMessage(PeerHandle, int, byte[])}
74      * variants.
75      *
76      * @return A positive integer, maximum length of byte array for Aware messaging.
77      */
getMaxServiceSpecificInfoLength()78     public int getMaxServiceSpecificInfoLength() {
79         return mCharacteristics.getInt(KEY_MAX_SERVICE_SPECIFIC_INFO_LENGTH);
80     }
81 
82     /**
83      * Returns the maximum length of byte array that can be used to specify a Aware match filter.
84      * Restricts the parameters of the
85      * {@link PublishConfig.Builder#setMatchFilter(java.util.List)} and
86      * {@link SubscribeConfig.Builder#setMatchFilter(java.util.List)}.
87      *
88      * @return A positive integer, maximum length of byte array for Aware discovery match filter.
89      */
getMaxMatchFilterLength()90     public int getMaxMatchFilterLength() {
91         return mCharacteristics.getInt(KEY_MAX_MATCH_FILTER_LENGTH);
92     }
93 
94     /**
95      * Check if instant communication mode is supported by device. The instant communication mode is
96      * defined as per Wi-Fi Alliance (WFA) Wi-Fi Aware specifications version 3.1 Section 12.3.
97      * @return True if supported, false otherwise.
98      */
99     @RequiresApi(Build.VERSION_CODES.S)
isInstantCommunicationModeSupported()100     public boolean isInstantCommunicationModeSupported() {
101         if (!SdkLevel.isAtLeastS()) {
102             throw new UnsupportedOperationException();
103         }
104         return mCharacteristics.getBoolean(KEY_IS_INSTANT_COMMUNICATION_MODE_SUPPORTED);
105     }
106 
107     /** @hide */
108     @IntDef(flag = true, prefix = { "WIFI_AWARE_CIPHER_SUITE_" }, value = {
109             WIFI_AWARE_CIPHER_SUITE_NCS_SK_128,
110             WIFI_AWARE_CIPHER_SUITE_NCS_SK_256,
111     })
112     @Retention(RetentionPolicy.SOURCE)
113     public @interface WifiAwareCipherSuites {}
114 
115     /**
116      * Wi-Fi Aware supported ciphier suite representing NCS SK 128: 128 bit shared-key.
117      */
118     public static final int WIFI_AWARE_CIPHER_SUITE_NCS_SK_128 = 1 << 0;
119 
120     /**
121      * Wi-Fi Aware supported ciphier suite representing NCS SK 256: 256 bit shared-key.
122      */
123     public static final int WIFI_AWARE_CIPHER_SUITE_NCS_SK_256 = 1 << 1;
124 
125     /**
126      * Returns the set of cipher suites supported by the device for use in Wi-Fi Aware data-paths.
127      * The device automatically picks the strongest cipher suite when initiating a data-path setup.
128      *
129      * @return A set of flags from {@link #WIFI_AWARE_CIPHER_SUITE_NCS_SK_128}, or
130      * {@link #WIFI_AWARE_CIPHER_SUITE_NCS_SK_256}.
131      */
getSupportedCipherSuites()132     public @WifiAwareCipherSuites int getSupportedCipherSuites() {
133         return mCharacteristics.getInt(KEY_SUPPORTED_CIPHER_SUITES);
134     }
135 
136     @Override
writeToParcel(Parcel dest, int flags)137     public void writeToParcel(Parcel dest, int flags) {
138         dest.writeBundle(mCharacteristics);
139     }
140 
141     @Override
describeContents()142     public int describeContents() {
143         return 0;
144     }
145 
146     public static final @android.annotation.NonNull Creator<Characteristics> CREATOR =
147             new Creator<Characteristics>() {
148                 @Override
149                 public Characteristics createFromParcel(Parcel in) {
150                     Characteristics c = new Characteristics(in.readBundle());
151                     return c;
152                 }
153 
154                 @Override
155                 public Characteristics[] newArray(int size) {
156                     return new Characteristics[size];
157                 }
158             };
159 }
160