• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.NonNull;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.net.wifi.OuiKeyedData;
22 import android.os.Build;
23 
24 import androidx.annotation.RequiresApi;
25 
26 import com.android.modules.utils.build.SdkLevel;
27 
28 import java.util.Collections;
29 import java.util.List;
30 
31 /**
32  * A class representing a Wi-Fi p2p provisional discovery request/response
33  * See {@link #WifiP2pProvDiscEvent} for supported types
34  *
35  * @hide
36  */
37 public class WifiP2pProvDiscEvent {
38 
39     private static final String TAG = "WifiP2pProvDiscEvent";
40 
41     public static final int WPS_PBC_REQ     = 1;
42     public static final int WPS_PBC_RSP     = 2;
43     public static final int WPS_ENTER_PIN   = 3;
44     public static final int WPS_SHOW_PIN    = 4;
45     public static final int PAIRING_BOOTSTRAPPING_OPPORTUNISTIC_REQ = 5;
46     public static final int PAIRING_BOOTSTRAPPING_OPPORTUNISTIC_RSP = 6;
47     public static final int PAIRING_BOOTSTRAPPING_ENTER_PIN = 7;
48     public static final int PAIRING_BOOTSTRAPPING_SHOW_PIN = 8;
49     public static final int PAIRING_BOOTSTRAPPING_ENTER_PASSPHRASE = 9;
50     public static final int PAIRING_BOOTSTRAPPING_SHOW_PASSPHRASE = 10;
51 
52     /*
53      * One of WPS_PBC_REQ, WPS_PBC_RSP, WPS_ENTER_PIN, WPS_SHOW_PIN,
54      * PAIRING_BOOTSTRAPPING_OPPORTUNISTIC_REQ, PAIRING_BOOTSTRAPPING_OPPORTUNISTIC_RSP,
55      * PAIRING_BOOTSTRAPPING_ENTER_PIN, PAIRING_BOOTSTRAPPING_SHOW_PIN,
56      * PAIRING_BOOTSTRAPPING_ENTER_PASSPHRASE or PAIRING_BOOTSTRAPPING_SHOW_PASSPHRASE.
57      */
58     @UnsupportedAppUsage
59     public int event;
60 
61     @UnsupportedAppUsage
62     public WifiP2pDevice device;
63 
64     /*
65      * Valid when event = WPS_SHOW_PIN
66      */
67     @UnsupportedAppUsage
68     public String wpsPin;
69 
70     /*
71      * Whether the provision discovery frame is with a comeback request.
72      */
73     @UnsupportedAppUsage
74     public boolean isComeback;
75 
76     /** List of {@link OuiKeyedData} providing vendor-specific configuration data. */
77     private @NonNull List<OuiKeyedData> mVendorData = Collections.emptyList();
78 
79     /**
80      * Return the vendor-provided configuration data, if it exists. See also {@link
81      * #setVendorData(List)}
82      *
83      * @return Vendor configuration data, or empty list if it does not exist.
84      */
85     @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
86     @NonNull
getVendorData()87     public List<OuiKeyedData> getVendorData() {
88         if (!SdkLevel.isAtLeastV()) {
89             throw new UnsupportedOperationException();
90         }
91         return mVendorData;
92     }
93 
94     @UnsupportedAppUsage
WifiP2pProvDiscEvent()95     public WifiP2pProvDiscEvent() {
96         device = new WifiP2pDevice();
97     }
98 
99     /**
100      * Set additional vendor-provided configuration data.
101      *
102      * @param vendorData List of {@link android.net.wifi.OuiKeyedData} containing the
103      *                   vendor-provided configuration data. Note that multiple elements with
104      *                   the same OUI are allowed.
105      */
106     @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
107     @NonNull
setVendorData(@onNull List<OuiKeyedData> vendorData)108     public void setVendorData(@NonNull List<OuiKeyedData> vendorData) {
109         if (!SdkLevel.isAtLeastV()) {
110             throw new UnsupportedOperationException();
111         }
112         if (vendorData == null) {
113             throw new IllegalArgumentException("setVendorData received a null value");
114         }
115         mVendorData = vendorData;
116     }
117 
118     /**
119      * @param string formats supported include
120      *
121      *  P2P-PROV-DISC-PBC-REQ 42:fc:89:e1:e2:27
122      *  P2P-PROV-DISC-PBC-RESP 02:12:47:f2:5a:36
123      *  P2P-PROV-DISC-ENTER-PIN 42:fc:89:e1:e2:27
124      *  P2P-PROV-DISC-SHOW-PIN 42:fc:89:e1:e2:27 44490607
125      *
126      *  Note: The events formats can be looked up in the wpa_supplicant code
127      * @hide
128      */
WifiP2pProvDiscEvent(String string)129     public WifiP2pProvDiscEvent(String string) throws IllegalArgumentException {
130         String[] tokens = string.split(" ");
131 
132         if (tokens.length < 2) {
133             throw new IllegalArgumentException("Malformed event " + string);
134         }
135 
136         if (tokens[0].endsWith("PBC-REQ")) {
137             event = WPS_PBC_REQ;
138         } else if (tokens[0].endsWith("PBC-RESP")) {
139             event = WPS_PBC_RSP;
140         } else if (tokens[0].endsWith("ENTER-PIN")) {
141             event = WPS_ENTER_PIN;
142         } else if (tokens[0].endsWith("SHOW-PIN")) {
143             event = WPS_SHOW_PIN;
144         } else {
145             throw new IllegalArgumentException("Malformed event " + string);
146         }
147 
148 
149         device = new WifiP2pDevice();
150         device.deviceAddress = tokens[1];
151 
152         if (event == WPS_SHOW_PIN) {
153             wpsPin = tokens[2];
154         }
155     }
156 
toString()157     public String toString() {
158         StringBuffer sbuf = new StringBuffer();
159         sbuf.append(device);
160         sbuf.append("\n event: ").append(event);
161         sbuf.append("\n wpsPin: ").append(wpsPin);
162         sbuf.append("\n vendorData: ").append(mVendorData);
163         sbuf.append("\n isComeback: ").append(isComeback);
164         return sbuf.toString();
165     }
166 }
167