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
19import android.net.wifi.WpsInfo;
20import android.os.Parcelable;
21import android.os.Parcel;
22
23/**
24 * A class representing a Wi-Fi P2p configuration for setting up a connection
25 *
26 * {@see WifiP2pManager}
27 */
28public class WifiP2pConfig implements Parcelable {
29
30    /**
31     * The device MAC address uniquely identifies a Wi-Fi p2p device
32     */
33    public String deviceAddress = "";
34
35    /**
36     * Wi-Fi Protected Setup information
37     */
38    public WpsInfo wps;
39
40    /** @hide */
41    public static final int MAX_GROUP_OWNER_INTENT   =   15;
42    /** @hide */
43    public static final int MIN_GROUP_OWNER_INTENT   =   0;
44
45    /**
46     * This is an integer value between 0 and 15 where 0 indicates the least
47     * inclination to be a group owner and 15 indicates the highest inclination
48     * to be a group owner.
49     *
50     * A value of -1 indicates the system can choose an appropriate value.
51     */
52    public int groupOwnerIntent = -1;
53
54    /** @hide */
55    public int netId = WifiP2pGroup.PERSISTENT_NET_ID;
56
57    public WifiP2pConfig() {
58        //set defaults
59        wps = new WpsInfo();
60        wps.setup = WpsInfo.PBC;
61    }
62
63    void invalidate() {
64        deviceAddress = "";
65    }
66
67    /** P2P-GO-NEG-REQUEST 42:fc:89:a8:96:09 dev_passwd_id=4 {@hide}*/
68    public WifiP2pConfig(String supplicantEvent) throws IllegalArgumentException {
69        String[] tokens = supplicantEvent.split(" ");
70
71        if (tokens.length < 2 || !tokens[0].equals("P2P-GO-NEG-REQUEST")) {
72            throw new IllegalArgumentException("Malformed supplicant event");
73        }
74
75        deviceAddress = tokens[1];
76        wps = new WpsInfo();
77
78        if (tokens.length > 2) {
79            String[] nameVal = tokens[2].split("=");
80            int devPasswdId;
81            try {
82                devPasswdId = Integer.parseInt(nameVal[1]);
83            } catch (NumberFormatException e) {
84                devPasswdId = 0;
85            }
86            //Based on definitions in wps/wps_defs.h
87            switch (devPasswdId) {
88                //DEV_PW_USER_SPECIFIED = 0x0001,
89                case 0x01:
90                    wps.setup = WpsInfo.DISPLAY;
91                    break;
92                //DEV_PW_PUSHBUTTON = 0x0004,
93                case 0x04:
94                    wps.setup = WpsInfo.PBC;
95                    break;
96                //DEV_PW_REGISTRAR_SPECIFIED = 0x0005
97                case 0x05:
98                    wps.setup = WpsInfo.KEYPAD;
99                    break;
100                default:
101                    wps.setup = WpsInfo.PBC;
102                    break;
103            }
104        }
105    }
106
107    public String toString() {
108        StringBuffer sbuf = new StringBuffer();
109        sbuf.append("\n address: ").append(deviceAddress);
110        sbuf.append("\n wps: ").append(wps);
111        sbuf.append("\n groupOwnerIntent: ").append(groupOwnerIntent);
112        sbuf.append("\n persist: ").append(netId);
113        return sbuf.toString();
114    }
115
116    /** Implement the Parcelable interface */
117    public int describeContents() {
118        return 0;
119    }
120
121    /** copy constructor */
122    public WifiP2pConfig(WifiP2pConfig source) {
123        if (source != null) {
124            deviceAddress = source.deviceAddress;
125            wps = new WpsInfo(source.wps);
126            groupOwnerIntent = source.groupOwnerIntent;
127            netId = source.netId;
128        }
129    }
130
131    /** Implement the Parcelable interface */
132    public void writeToParcel(Parcel dest, int flags) {
133        dest.writeString(deviceAddress);
134        dest.writeParcelable(wps, flags);
135        dest.writeInt(groupOwnerIntent);
136        dest.writeInt(netId);
137    }
138
139    /** Implement the Parcelable interface */
140    public static final Creator<WifiP2pConfig> CREATOR =
141        new Creator<WifiP2pConfig>() {
142            public WifiP2pConfig createFromParcel(Parcel in) {
143                WifiP2pConfig config = new WifiP2pConfig();
144                config.deviceAddress = in.readString();
145                config.wps = (WpsInfo) in.readParcelable(null);
146                config.groupOwnerIntent = in.readInt();
147                config.netId = in.readInt();
148                return config;
149            }
150
151            public WifiP2pConfig[] newArray(int size) {
152                return new WifiP2pConfig[size];
153            }
154        };
155}
156