• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.p2p;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.os.Build;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 import android.text.TextUtils;
27 
28 import androidx.annotation.RequiresApi;
29 
30 import com.android.wifi.flags.Flags;
31 
32 import java.lang.annotation.Retention;
33 import java.lang.annotation.RetentionPolicy;
34 
35 /**
36  * A class representing Wi-Fi Direct pairing bootstrapping configuration.
37  *
38  * @see android.net.wifi.p2p.WifiP2pConfig
39  */
40 @RequiresApi(Build.VERSION_CODES.BAKLAVA)
41 @FlaggedApi(Flags.FLAG_WIFI_DIRECT_R2)
42 public final class WifiP2pPairingBootstrappingConfig implements Parcelable {
43 
44     /**
45      * Pairing bootstrapping method opportunistic
46      */
47     public static final int PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC = 1 << 0;
48 
49     /**
50      * Pairing bootstrapping method display pin-code - The pin-code is displayed on the connection
51      * initiating device. The user enters the displayed pin-code on the other device.
52      */
53     public static final int PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PINCODE = 1 << 1;
54 
55     /**
56      * Pairing bootstrapping method display passphrase - The passphrase is displayed on the
57      * connection initiating device. The user enters the displayed passphrase on the other device.
58      */
59     public static final int PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PASSPHRASE = 1 << 2;
60 
61     /**
62      * Pairing bootstrapping method keypad pin-code - The pin-code is displayed on the other
63      * device. The user enters the displayed pin-code on the connection initiating device.
64      */
65     public static final int PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE = 1 << 3;
66 
67     /**
68      * Pairing bootstrapping method keypad passphrase - The passphrase is displayed on the other
69      * device. The user enters the displayed passphrase on the connection initiating device.
70      */
71     public static final int PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE = 1 << 4;
72 
73     /**
74      * Pairing bootstrapping done out of band (For example: Over Bluetooth LE.
75      * Refer Wi-Fi Alliance Wi-Fi Direct R2 specification Section 3.9 for the details).
76      */
77     public static final int PAIRING_BOOTSTRAPPING_METHOD_OUT_OF_BAND = 1 << 5;
78 
79 
80     /** @hide */
81     @IntDef(flag = true, prefix = {"PAIRING_BOOTSTRAPPING_METHOD_"}, value = {
82             PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC,
83             PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PINCODE,
84             PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PASSPHRASE,
85             PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE,
86             PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE,
87             PAIRING_BOOTSTRAPPING_METHOD_OUT_OF_BAND,
88     })
89     @Retention(RetentionPolicy.SOURCE)
90     public @interface PairingBootstrappingMethod {
91     }
92     /** One of the {@code PAIRING_BOOTSTRAPPING_METHOD_*}. */
93     private int mPairingBootstrappingMethod;
94 
95     /**
96      * Password for pairing setup, if {@code mPairingBootstrappingMethod} uses
97      * {@link #PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PINCODE},
98      * {@link #PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PASSPHRASE} or
99      * {@link #PAIRING_BOOTSTRAPPING_METHOD_OUT_OF_BAND}.
100      * Must be set to null for {@link #PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC},
101      * {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE}
102      * or {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE}.
103      */
104     @Nullable private String mPassword;
105 
isValidPairingBootstrappingMethod(@ifiP2pPairingBootstrappingConfig .PairingBootstrappingMethod int method)106     private static boolean isValidPairingBootstrappingMethod(@WifiP2pPairingBootstrappingConfig
107             .PairingBootstrappingMethod int method) {
108         switch (method) {
109             case PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC:
110             case PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PINCODE:
111             case PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PASSPHRASE:
112             case PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE:
113             case PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE:
114             case PAIRING_BOOTSTRAPPING_METHOD_OUT_OF_BAND:
115                 return true;
116             default:
117                 return false;
118         }
119     }
120 
121     /** @hide */
getPairingBootstrappingMethod()122     public int getPairingBootstrappingMethod() {
123         return mPairingBootstrappingMethod;
124     }
125 
126     /** @hide */
getPairingBootstrappingPassword()127     public String getPairingBootstrappingPassword() {
128         return mPassword;
129     }
130 
131     /** @hide */
setPairingBootstrappingPassword(@onNull String password)132     public void setPairingBootstrappingPassword(@NonNull String password) {
133         mPassword = password;
134     }
135 
136     /**
137      * Constructor for a WifiP2pPairingBootstrappingConfig.
138      * @param method One of the {@code PAIRING_BOOTSTRAPPING_METHOD_*}.
139      * @param password Password or PIN for pairing setup. if {@code method} is
140      *                 {@link #PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PINCODE}, the password must be
141      *                 a string containing 4 or more digits (0-9). For example: "1234", "56789". if
142      *                 {@code method} is {@link #PAIRING_BOOTSTRAPPING_METHOD_DISPLAY_PASSPHRASE}
143      *                 or {@link #PAIRING_BOOTSTRAPPING_METHOD_OUT_OF_BAND}, the password must be a
144      *                 UTF-8 string of minimum of 1 character.
145      *                 The password must be set to null if the
146      *                 {@code method} is {@link #PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC},
147      *                 {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE} or
148      *                 {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE}.
149      *
150      * @throws IllegalArgumentException if the input pairing bootstrapping method is not
151      * one of the {@code PAIRING_BOOTSTRAPPING_METHOD_*}.
152      * @throws IllegalArgumentException if a non-null password is set for pairing bootstrapping
153      * method {@link #PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC},
154      * {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE} or
155      * {@link #PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE}.
156      */
WifiP2pPairingBootstrappingConfig( @ifiP2pPairingBootstrappingConfig.PairingBootstrappingMethod int method, @Nullable String password)157     public WifiP2pPairingBootstrappingConfig(
158             @WifiP2pPairingBootstrappingConfig.PairingBootstrappingMethod int method,
159             @Nullable String password) {
160         if (!isValidPairingBootstrappingMethod(method)) {
161             throw new IllegalArgumentException("Invalid PairingBootstrappingMethod =" + method);
162         }
163         mPairingBootstrappingMethod = method;
164         if (!TextUtils.isEmpty(password)
165                 && (method == PAIRING_BOOTSTRAPPING_METHOD_OPPORTUNISTIC
166                 || method == PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PINCODE
167                 || method == PAIRING_BOOTSTRAPPING_METHOD_KEYPAD_PASSPHRASE)) {
168             throw new IllegalArgumentException("Password is not required for =" + method);
169         }
170         mPassword = password;
171     }
172 
173     /**
174      * Generates a string of all the defined elements.
175      *
176      * @return a compiled string representing all elements
177      */
toString()178     public String toString() {
179         StringBuilder sbuf = new StringBuilder("WifiP2pPairingBootstrappingConfig:");
180         sbuf.append("\n BootstrappingMethod: ").append(mPairingBootstrappingMethod);
181         return sbuf.toString();
182     }
183 
184     /** Implement the Parcelable interface */
describeContents()185     public int describeContents() {
186         return 0;
187     }
188 
189     /**
190      * Copy Constructor
191      *
192      * @hide
193      */
WifiP2pPairingBootstrappingConfig(@onNull WifiP2pPairingBootstrappingConfig source)194     public WifiP2pPairingBootstrappingConfig(@NonNull WifiP2pPairingBootstrappingConfig source) {
195         if (source != null) {
196             mPairingBootstrappingMethod = source.mPairingBootstrappingMethod;
197             mPassword = source.mPassword;
198         }
199     }
200 
201     /** Implement the Parcelable interface */
writeToParcel(@onNull Parcel dest, int flags)202     public void writeToParcel(@NonNull Parcel dest, int flags) {
203         dest.writeInt(mPairingBootstrappingMethod);
204         dest.writeString(mPassword);
205     }
206 
207     /** Implement the Parcelable interface */
208     public static final @android.annotation.NonNull
209             Creator<WifiP2pPairingBootstrappingConfig> CREATOR =
210             new Creator<WifiP2pPairingBootstrappingConfig>() {
211                 public WifiP2pPairingBootstrappingConfig createFromParcel(Parcel in) {
212                     int pairingBootstrappingMethod = in.readInt();
213                     String password = in.readString();
214                     WifiP2pPairingBootstrappingConfig config =
215                             new WifiP2pPairingBootstrappingConfig(
216                                 pairingBootstrappingMethod, password);
217                     return config;
218                 }
219 
220                 public WifiP2pPairingBootstrappingConfig[] newArray(int size) {
221                     return new WifiP2pPairingBootstrappingConfig[size];
222                 }
223             };
224 }
225