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.compat.annotation.UnsupportedAppUsage;
20 
21 /**
22  * A class representing a Wi-Fi p2p provisional discovery request/response
23  * See {@link #WifiP2pProvDiscEvent} for supported types
24  *
25  * @hide
26  */
27 public class WifiP2pProvDiscEvent {
28 
29     private static final String TAG = "WifiP2pProvDiscEvent";
30 
31     public static final int PBC_REQ     = 1;
32     public static final int PBC_RSP     = 2;
33     public static final int ENTER_PIN   = 3;
34     public static final int SHOW_PIN    = 4;
35 
36     /* One of PBC_REQ, PBC_RSP, ENTER_PIN or SHOW_PIN */
37     @UnsupportedAppUsage
38     public int event;
39 
40     @UnsupportedAppUsage
41     public WifiP2pDevice device;
42 
43     /* Valid when event = SHOW_PIN */
44     @UnsupportedAppUsage
45     public String pin;
46 
47     @UnsupportedAppUsage
WifiP2pProvDiscEvent()48     public WifiP2pProvDiscEvent() {
49         device = new WifiP2pDevice();
50     }
51 
52     /**
53      * @param string formats supported include
54      *
55      *  P2P-PROV-DISC-PBC-REQ 42:fc:89:e1:e2:27
56      *  P2P-PROV-DISC-PBC-RESP 02:12:47:f2:5a:36
57      *  P2P-PROV-DISC-ENTER-PIN 42:fc:89:e1:e2:27
58      *  P2P-PROV-DISC-SHOW-PIN 42:fc:89:e1:e2:27 44490607
59      *
60      *  Note: The events formats can be looked up in the wpa_supplicant code
61      * @hide
62      */
WifiP2pProvDiscEvent(String string)63     public WifiP2pProvDiscEvent(String string) throws IllegalArgumentException {
64         String[] tokens = string.split(" ");
65 
66         if (tokens.length < 2) {
67             throw new IllegalArgumentException("Malformed event " + string);
68         }
69 
70         if (tokens[0].endsWith("PBC-REQ")) event = PBC_REQ;
71         else if (tokens[0].endsWith("PBC-RESP")) event = PBC_RSP;
72         else if (tokens[0].endsWith("ENTER-PIN")) event = ENTER_PIN;
73         else if (tokens[0].endsWith("SHOW-PIN")) event = SHOW_PIN;
74         else throw new IllegalArgumentException("Malformed event " + string);
75 
76 
77         device = new WifiP2pDevice();
78         device.deviceAddress = tokens[1];
79 
80         if (event == SHOW_PIN) {
81             pin = tokens[2];
82         }
83     }
84 
toString()85     public String toString() {
86         StringBuffer sbuf = new StringBuffer();
87         sbuf.append(device);
88         sbuf.append("\n event: ").append(event);
89         sbuf.append("\n pin: ").append(pin);
90         return sbuf.toString();
91     }
92 }
93