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 PBC_REQ     = 1;
42     public static final int PBC_RSP     = 2;
43     public static final int ENTER_PIN   = 3;
44     public static final int SHOW_PIN    = 4;
45 
46     /* One of PBC_REQ, PBC_RSP, ENTER_PIN or SHOW_PIN */
47     @UnsupportedAppUsage
48     public int event;
49 
50     @UnsupportedAppUsage
51     public WifiP2pDevice device;
52 
53     /* Valid when event = SHOW_PIN */
54     @UnsupportedAppUsage
55     public String pin;
56 
57     /** List of {@link OuiKeyedData} providing vendor-specific configuration data. */
58     private @NonNull List<OuiKeyedData> mVendorData = Collections.emptyList();
59 
60     /**
61      * Return the vendor-provided configuration data, if it exists. See also {@link
62      * #setVendorData(List)}
63      *
64      * @return Vendor configuration data, or empty list if it does not exist.
65      */
66     @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
67     @NonNull
getVendorData()68     public List<OuiKeyedData> getVendorData() {
69         if (!SdkLevel.isAtLeastV()) {
70             throw new UnsupportedOperationException();
71         }
72         return mVendorData;
73     }
74 
75     @UnsupportedAppUsage
WifiP2pProvDiscEvent()76     public WifiP2pProvDiscEvent() {
77         device = new WifiP2pDevice();
78     }
79 
80     /**
81      * Set additional vendor-provided configuration data.
82      *
83      * @param vendorData List of {@link android.net.wifi.OuiKeyedData} containing the
84      *                   vendor-provided configuration data. Note that multiple elements with
85      *                   the same OUI are allowed.
86      */
87     @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
88     @NonNull
setVendorData(@onNull List<OuiKeyedData> vendorData)89     public void setVendorData(@NonNull List<OuiKeyedData> vendorData) {
90         if (!SdkLevel.isAtLeastV()) {
91             throw new UnsupportedOperationException();
92         }
93         if (vendorData == null) {
94             throw new IllegalArgumentException("setVendorData received a null value");
95         }
96         mVendorData = vendorData;
97     }
98 
99     /**
100      * @param string formats supported include
101      *
102      *  P2P-PROV-DISC-PBC-REQ 42:fc:89:e1:e2:27
103      *  P2P-PROV-DISC-PBC-RESP 02:12:47:f2:5a:36
104      *  P2P-PROV-DISC-ENTER-PIN 42:fc:89:e1:e2:27
105      *  P2P-PROV-DISC-SHOW-PIN 42:fc:89:e1:e2:27 44490607
106      *
107      *  Note: The events formats can be looked up in the wpa_supplicant code
108      * @hide
109      */
WifiP2pProvDiscEvent(String string)110     public WifiP2pProvDiscEvent(String string) throws IllegalArgumentException {
111         String[] tokens = string.split(" ");
112 
113         if (tokens.length < 2) {
114             throw new IllegalArgumentException("Malformed event " + string);
115         }
116 
117         if (tokens[0].endsWith("PBC-REQ")) event = PBC_REQ;
118         else if (tokens[0].endsWith("PBC-RESP")) event = PBC_RSP;
119         else if (tokens[0].endsWith("ENTER-PIN")) event = ENTER_PIN;
120         else if (tokens[0].endsWith("SHOW-PIN")) event = SHOW_PIN;
121         else throw new IllegalArgumentException("Malformed event " + string);
122 
123 
124         device = new WifiP2pDevice();
125         device.deviceAddress = tokens[1];
126 
127         if (event == SHOW_PIN) {
128             pin = tokens[2];
129         }
130     }
131 
toString()132     public String toString() {
133         StringBuffer sbuf = new StringBuffer();
134         sbuf.append(device);
135         sbuf.append("\n event: ").append(event);
136         sbuf.append("\n pin: ").append(pin);
137         sbuf.append("\n vendorData: ").append(mVendorData);
138         return sbuf.toString();
139     }
140 }
141