• 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.annotation.IntRange;
21 import android.os.Build;
22 import android.os.Bundle;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import androidx.annotation.RequiresApi;
27 
28 import com.android.modules.utils.build.SdkLevel;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 
33 /**
34  * The characteristics of the Wi-Fi Aware implementation.
35  */
36 public final class Characteristics implements Parcelable {
37     /** @hide */
38     public static final String KEY_MAX_SERVICE_NAME_LENGTH = "key_max_service_name_length";
39     /** @hide */
40     public static final String KEY_MAX_SERVICE_SPECIFIC_INFO_LENGTH =
41             "key_max_service_specific_info_length";
42     /** @hide */
43     public static final String KEY_MAX_MATCH_FILTER_LENGTH = "key_max_match_filter_length";
44     /** @hide */
45     public static final String KEY_SUPPORTED_CIPHER_SUITES = "key_supported_cipher_suites";
46     /** @hide */
47     public static final String KEY_IS_INSTANT_COMMUNICATION_MODE_SUPPORTED =
48             "key_is_instant_communication_mode_supported";
49     /** @hide */
50     public static final String KEY_MAX_NDP_NUMBER = "key_max_ndp_number";
51     /** @hide */
52     public static final String KEY_MAX_PUBLISH_NUMBER = "key_max_publish_number";
53     /** @hide */
54     public static final String KEY_MAX_SUBSCRIBE_NUMBER = "key_max_subscribe_number";
55     /** @hide */
56     public static final String KEY_MAX_NDI_NUMBER = "key_max_ndi_number";
57 
58     private final Bundle mCharacteristics;
59 
60     /** @hide : should not be created by apps */
Characteristics(Bundle characteristics)61     public Characteristics(Bundle characteristics) {
62         mCharacteristics = characteristics;
63     }
64 
65     /**
66      * Returns the maximum string length that can be used to specify a Aware service name. Restricts
67      * the parameters of the {@link PublishConfig.Builder#setServiceName(String)} and
68      * {@link SubscribeConfig.Builder#setServiceName(String)}.
69      *
70      * @return A positive integer, maximum string length of Aware service name.
71      */
getMaxServiceNameLength()72     public int getMaxServiceNameLength() {
73         return mCharacteristics.getInt(KEY_MAX_SERVICE_NAME_LENGTH);
74     }
75 
76     /**
77      * Returns the maximum length of byte array that can be used to specify a Aware service specific
78      * information field: the arbitrary load used in discovery or the message length of Aware
79      * message exchange. Restricts the parameters of the
80      * {@link PublishConfig.Builder#setServiceSpecificInfo(byte[])},
81      * {@link SubscribeConfig.Builder#setServiceSpecificInfo(byte[])}, and
82      * {@link DiscoverySession#sendMessage(PeerHandle, int, byte[])}
83      * variants.
84      *
85      * @return A positive integer, maximum length of byte array for Aware messaging.
86      */
getMaxServiceSpecificInfoLength()87     public int getMaxServiceSpecificInfoLength() {
88         return mCharacteristics.getInt(KEY_MAX_SERVICE_SPECIFIC_INFO_LENGTH);
89     }
90 
91     /**
92      * Returns the maximum length of byte array that can be used to specify a Aware match filter.
93      * Restricts the parameters of the
94      * {@link PublishConfig.Builder#setMatchFilter(java.util.List)} and
95      * {@link SubscribeConfig.Builder#setMatchFilter(java.util.List)}.
96      *
97      * @return A positive integer, maximum length of byte array for Aware discovery match filter.
98      */
getMaxMatchFilterLength()99     public int getMaxMatchFilterLength() {
100         return mCharacteristics.getInt(KEY_MAX_MATCH_FILTER_LENGTH);
101     }
102 
103     /**
104      * Returns the maximum number of Aware data interfaces supported by the device.
105      *
106      * @return A positive integer, maximum number of Aware data interfaces supported by the device.
107      */
108     @IntRange(from = 1)
getNumberOfSupportedDataInterfaces()109     public int getNumberOfSupportedDataInterfaces() {
110         return mCharacteristics.getInt(KEY_MAX_NDI_NUMBER);
111     }
112 
113     /**
114      * Returns the maximum number of Aware publish sessions supported by the device.
115      * Use {@link AwareResources#getAvailablePublishSessionsCount()} to get the number of available
116      * publish sessions which are not currently used by any app.
117      *
118      * @return A positive integer, maximum number of publish sessions supported by the device.
119      */
120     @IntRange(from = 1)
getNumberOfSupportedPublishSessions()121     public int getNumberOfSupportedPublishSessions() {
122         return mCharacteristics.getInt(KEY_MAX_PUBLISH_NUMBER);
123     }
124 
125     /**
126      * Returns the maximum number of Aware subscribe session supported by the device.
127      * Use {@link AwareResources#getAvailableSubscribeSessionsCount()} to get the number of
128      * available subscribe sessions which are not currently used by any app.
129      *
130      * @return A positive integer, maximum number of subscribe sessions supported by the device.
131      */
132     @IntRange(from = 1)
getNumberOfSupportedSubscribeSessions()133     public int getNumberOfSupportedSubscribeSessions() {
134         return mCharacteristics.getInt(KEY_MAX_SUBSCRIBE_NUMBER);
135     }
136 
137     /**
138      * Returns the maximum number of Aware data paths(also known as NDPs - NAN Data Paths) supported
139      * by the device.
140      * Use {@link AwareResources#getAvailableDataPathsCount()} to get the number of available Aware
141      * data paths which are not currently used by any app.
142      *
143      * @return A positive integer, maximum number of Aware data paths supported by the device.
144      */
145     @IntRange(from = 1)
getNumberOfSupportedDataPaths()146     public int getNumberOfSupportedDataPaths() {
147         return mCharacteristics.getInt(KEY_MAX_NDP_NUMBER);
148     }
149 
150     /**
151      * Check if instant communication mode is supported by device. The instant communication mode is
152      * defined as per Wi-Fi Alliance (WFA) Wi-Fi Aware specifications version 3.1 Section 12.3.
153      * @return True if supported, false otherwise.
154      */
155     @RequiresApi(Build.VERSION_CODES.S)
isInstantCommunicationModeSupported()156     public boolean isInstantCommunicationModeSupported() {
157         if (!SdkLevel.isAtLeastS()) {
158             throw new UnsupportedOperationException();
159         }
160         return mCharacteristics.getBoolean(KEY_IS_INSTANT_COMMUNICATION_MODE_SUPPORTED);
161     }
162 
163     /** @hide */
164     @IntDef(flag = true, prefix = { "WIFI_AWARE_CIPHER_SUITE_" }, value = {
165             WIFI_AWARE_CIPHER_SUITE_NONE,
166             WIFI_AWARE_CIPHER_SUITE_NCS_SK_128,
167             WIFI_AWARE_CIPHER_SUITE_NCS_SK_256,
168             WIFI_AWARE_CIPHER_SUITE_NCS_PK_128,
169             WIFI_AWARE_CIPHER_SUITE_NCS_PK_256,
170     })
171     @Retention(RetentionPolicy.SOURCE)
172     public @interface WifiAwareCipherSuites {}
173 
174     /**
175      * Wi-Fi Aware supported open (unencrypted) data-path.
176      */
177     public static final int WIFI_AWARE_CIPHER_SUITE_NONE = 0;
178     /**
179      * Wi-Fi Aware supported cipher suite representing NCS SK 128: 128 bit shared-key.
180      */
181     public static final int WIFI_AWARE_CIPHER_SUITE_NCS_SK_128 = 1 << 0;
182 
183     /**
184      * Wi-Fi Aware supported cipher suite representing NCS SK 256: 256 bit shared-key.
185      */
186     public static final int WIFI_AWARE_CIPHER_SUITE_NCS_SK_256 = 1 << 1;
187 
188     /**
189      * Wi-Fi Aware supported cipher suite representing NCS PK 128: 128 bit public-key.
190      */
191     public static final int WIFI_AWARE_CIPHER_SUITE_NCS_PK_128 = 1 << 2;
192 
193     /**
194      * Wi-Fi Aware supported cipher suite representing NCS PK 256: 256 bit public-key.
195      */
196     public static final int WIFI_AWARE_CIPHER_SUITE_NCS_PK_256 = 1 << 3;
197 
198     /**
199      * Returns the set of cipher suites supported by the device for use in Wi-Fi Aware data-paths.
200      * The device automatically picks the strongest cipher suite when initiating a data-path setup.
201      *
202      * @return A set of flags from {@link #WIFI_AWARE_CIPHER_SUITE_NCS_SK_128},
203      * {@link #WIFI_AWARE_CIPHER_SUITE_NCS_SK_256}, {@link #WIFI_AWARE_CIPHER_SUITE_NCS_PK_128},
204      * or {@link #WIFI_AWARE_CIPHER_SUITE_NCS_PK_256}
205      */
getSupportedCipherSuites()206     public @WifiAwareCipherSuites int getSupportedCipherSuites() {
207         return mCharacteristics.getInt(KEY_SUPPORTED_CIPHER_SUITES);
208     }
209 
210     @Override
writeToParcel(Parcel dest, int flags)211     public void writeToParcel(Parcel dest, int flags) {
212         dest.writeBundle(mCharacteristics);
213     }
214 
215     @Override
describeContents()216     public int describeContents() {
217         return 0;
218     }
219 
220     public static final @android.annotation.NonNull Creator<Characteristics> CREATOR =
221             new Creator<Characteristics>() {
222                 @Override
223                 public Characteristics createFromParcel(Parcel in) {
224                     Characteristics c = new Characteristics(in.readBundle());
225                     return c;
226                 }
227 
228                 @Override
229                 public Characteristics[] newArray(int size) {
230                     return new Characteristics[size];
231                 }
232             };
233 }
234