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