1 /*
2  * Copyright (C) 2022 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.NonNull;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.util.Objects;
27 
28 /**
29  * The config for the Aware Pairing. Set to
30  * {@link PublishConfig.Builder#setPairingConfig(AwarePairingConfig)} and
31  * {@link SubscribeConfig.Builder#setPairingConfig(AwarePairingConfig)}.
32  * Only valid when {@link Characteristics#isAwarePairingSupported()} is true.
33  */
34 public final class AwarePairingConfig implements Parcelable {
35 
36     /**
37      * Aware Pairing bootstrapping method opportunistic
38      */
39     public static final int PAIRING_BOOTSTRAPPING_OPPORTUNISTIC = 1 << 0;
40 
41     /**
42      * Aware Pairing bootstrapping method pin-code display
43      */
44     public static final int PAIRING_BOOTSTRAPPING_PIN_CODE_DISPLAY = 1 << 1;
45 
46     /**
47      * Aware Pairing bootstrapping method passphrase display
48      */
49     public static final int PAIRING_BOOTSTRAPPING_PASSPHRASE_DISPLAY = 1 << 2;
50 
51     /**
52      * Aware Pairing bootstrapping method QR-code display
53      */
54     public static final int PAIRING_BOOTSTRAPPING_QR_DISPLAY = 1 << 3;
55 
56     /**
57      * Aware Pairing bootstrapping method NFC tag
58      */
59     public static final int PAIRING_BOOTSTRAPPING_NFC_TAG = 1 << 4;
60     /**
61      * Aware Pairing bootstrapping method pin-code keypad
62      */
63     public static final int PAIRING_BOOTSTRAPPING_PIN_CODE_KEYPAD = 1 << 5;
64     /**
65      * Aware Pairing bootstrapping method passphrase keypad
66      */
67     public static final int PAIRING_BOOTSTRAPPING_PASSPHRASE_KEYPAD = 1 << 6;
68     /**
69      * Aware Pairing bootstrapping method QR-code scan
70      */
71     public static final int PAIRING_BOOTSTRAPPING_QR_SCAN = 1 << 7;
72     /**
73      * Aware Pairing bootstrapping method NFC reader
74      */
75     public static final int PAIRING_BOOTSTRAPPING_NFC_READER = 1 << 8;
76 
77 
78     /** @hide */
79     @IntDef(flag = true, prefix = {"PAIRING_BOOTSTRAPPING_"}, value = {
80             PAIRING_BOOTSTRAPPING_OPPORTUNISTIC,
81             PAIRING_BOOTSTRAPPING_PIN_CODE_DISPLAY,
82             PAIRING_BOOTSTRAPPING_PASSPHRASE_DISPLAY,
83             PAIRING_BOOTSTRAPPING_QR_DISPLAY,
84             PAIRING_BOOTSTRAPPING_NFC_TAG,
85             PAIRING_BOOTSTRAPPING_PIN_CODE_KEYPAD,
86             PAIRING_BOOTSTRAPPING_PASSPHRASE_KEYPAD,
87             PAIRING_BOOTSTRAPPING_QR_SCAN,
88             PAIRING_BOOTSTRAPPING_NFC_READER
89 
90     })
91     @Retention(RetentionPolicy.SOURCE)
92     public @interface BootstrappingMethod {
93     }
94 
95     private final boolean mPairingSetup;
96     private final boolean mPairingCache;
97     private final boolean mPairingVerification;
98     private final int mBootstrappingMethods;
99 
100     /**
101      * Check if the NPK/NIK cache is support in the config
102      */
isPairingCacheEnabled()103     public boolean isPairingCacheEnabled() {
104         return mPairingCache;
105     }
106 
107     /**
108      * Check if the Aware Pairing setup is support in the config.
109      * Setup is the first time for two device establish Aware Pairing
110      */
isPairingSetupEnabled()111     public boolean isPairingSetupEnabled() {
112         return mPairingSetup;
113     }
114 
115     /**
116      * Check if the Aware Pairing verification is support in the config.
117      * Verification is for two device already paired and re-establish with cached NPK/NIK
118      */
isPairingVerificationEnabled()119     public boolean isPairingVerificationEnabled() {
120         return mPairingVerification;
121     }
122 
123     /**
124      * Get the supported bootstrapping methods in this config. Set of the
125      * STATUS_NETWORK_SUGGESTIONS_ values.
126      */
127     @BootstrappingMethod
getBootstrappingMethods()128     public int getBootstrappingMethods() {
129         return mBootstrappingMethods;
130     }
131 
132     @Override
equals(Object o)133     public boolean equals(Object o) {
134         if (this == o) return true;
135         if (!(o instanceof AwarePairingConfig)) return false;
136         AwarePairingConfig that = (AwarePairingConfig) o;
137         return mPairingSetup == that.mPairingSetup && mPairingCache == that.mPairingCache
138                 && mPairingVerification == that.mPairingVerification
139                 && mBootstrappingMethods == that.mBootstrappingMethods;
140     }
141 
142     @Override
hashCode()143     public int hashCode() {
144         return Objects.hash(mPairingSetup, mPairingCache, mPairingVerification,
145                 mBootstrappingMethods);
146     }
147 
148     /** @hide */
AwarePairingConfig(boolean setup, boolean cache, boolean verification, int method)149     public AwarePairingConfig(boolean setup, boolean cache, boolean verification, int method) {
150         mPairingSetup = setup;
151         mPairingCache = cache;
152         mPairingVerification = verification;
153         mBootstrappingMethods = method;
154     }
155 
156     /** @hide */
AwarePairingConfig(@onNull Parcel in)157     protected AwarePairingConfig(@NonNull Parcel in) {
158         mPairingSetup = in.readBoolean();
159         mPairingCache = in.readBoolean();
160         mPairingVerification = in.readBoolean();
161         mBootstrappingMethods = in.readInt();
162     }
163 
164     @Override
writeToParcel(@onNull Parcel dest, int flags)165     public void writeToParcel(@NonNull Parcel dest, int flags) {
166         dest.writeBoolean(mPairingSetup);
167         dest.writeBoolean(mPairingCache);
168         dest.writeBoolean(mPairingVerification);
169         dest.writeInt(mBootstrappingMethods);
170     }
171 
172     @Override
describeContents()173     public int describeContents() {
174         return 0;
175     }
176 
177     @NonNull public static final Creator<AwarePairingConfig> CREATOR =
178             new Creator<AwarePairingConfig>() {
179         @Override
180         public AwarePairingConfig createFromParcel(@NonNull Parcel in) {
181             return new AwarePairingConfig(in);
182         }
183 
184         @Override
185         public AwarePairingConfig[] newArray(int size) {
186             return new AwarePairingConfig[size];
187         }
188     };
189     /**
190      * Builder used to build {@link AwarePairingConfig} objects.
191      */
192     public static final class Builder {
193         private boolean mPairingSetup = false;
194         private boolean mPairingCache = false;
195         private boolean mPairingVerification = false;
196         private int mBootStrappingMethods = 0;
197 
198         /**
199          * Set whether enable the Aware Pairing setup
200          * @param enabled true to enable, false otherwise
201          * @return the current {@link Builder} builder, enabling chaining of builder methods.
202          */
203         @NonNull
setPairingSetupEnabled(boolean enabled)204         public Builder setPairingSetupEnabled(boolean enabled) {
205             mPairingSetup = enabled;
206             return this;
207         }
208 
209         /**
210          * Set whether enable the Aware Pairing verification
211          * @param enabled if set to true will accept Aware Pairing verification request from peer
212          *                with cached NPK/NIK, otherwise will reject the request .
213          * @return the current {@link Builder} builder, enabling chaining of builder methods.
214          */
setPairingVerificationEnabled(boolean enabled)215         @NonNull public Builder setPairingVerificationEnabled(boolean enabled) {
216             mPairingVerification = enabled;
217             return this;
218         }
219 
220         /**
221          * Set whether enable cache of the NPK/NIK of Aware Pairing setup
222          * @param enabled true to enable caching, false otherwise
223          * @return the current {@link Builder} builder, enabling chaining of builder methods.
224          */
setPairingCacheEnabled(boolean enabled)225         @NonNull public Builder setPairingCacheEnabled(boolean enabled) {
226             mPairingCache = enabled;
227             return this;
228         }
229 
230         /**
231          * Set the supported bootstrapping methods
232          * @param methods methods supported, set of PAIRING_BOOTSTRAPPING_ values
233          * @return the current {@link Builder} builder, enabling chaining of builder methods.
234          */
setBootstrappingMethods(@ootstrappingMethod int methods)235         @NonNull public Builder setBootstrappingMethods(@BootstrappingMethod int methods) {
236             mBootStrappingMethods = methods;
237             return this;
238         }
239 
240         /**
241          * Build {@link AwarePairingConfig} given the current requests made on the
242          * builder.
243          */
244         @NonNull
build()245         public AwarePairingConfig build() {
246             return new AwarePairingConfig(mPairingSetup, mPairingCache, mPairingVerification,
247                     mBootStrappingMethods);
248         }
249     }
250 }
251