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    /** @hide */
64    public void invalidate() {
65        deviceAddress = "";
66    }
67
68    /** P2P-GO-NEG-REQUEST 42:fc:89:a8:96:09 dev_passwd_id=4 {@hide}*/
69    public WifiP2pConfig(String supplicantEvent) throws IllegalArgumentException {
70        String[] tokens = supplicantEvent.split(" ");
71
72        if (tokens.length < 2 || !tokens[0].equals("P2P-GO-NEG-REQUEST")) {
73            throw new IllegalArgumentException("Malformed supplicant event");
74        }
75
76        deviceAddress = tokens[1];
77        wps = new WpsInfo();
78
79        if (tokens.length > 2) {
80            String[] nameVal = tokens[2].split("=");
81            int devPasswdId;
82            try {
83                devPasswdId = Integer.parseInt(nameVal[1]);
84            } catch (NumberFormatException e) {
85                devPasswdId = 0;
86            }
87            //Based on definitions in wps/wps_defs.h
88            switch (devPasswdId) {
89                //DEV_PW_USER_SPECIFIED = 0x0001,
90                case 0x01:
91                    wps.setup = WpsInfo.DISPLAY;
92                    break;
93                //DEV_PW_PUSHBUTTON = 0x0004,
94                case 0x04:
95                    wps.setup = WpsInfo.PBC;
96                    break;
97                //DEV_PW_REGISTRAR_SPECIFIED = 0x0005
98                case 0x05:
99                    wps.setup = WpsInfo.KEYPAD;
100                    break;
101                default:
102                    wps.setup = WpsInfo.PBC;
103                    break;
104            }
105        }
106    }
107
108    public String toString() {
109        StringBuffer sbuf = new StringBuffer();
110        sbuf.append("\n address: ").append(deviceAddress);
111        sbuf.append("\n wps: ").append(wps);
112        sbuf.append("\n groupOwnerIntent: ").append(groupOwnerIntent);
113        sbuf.append("\n persist: ").append(netId);
114        return sbuf.toString();
115    }
116
117    /** Implement the Parcelable interface */
118    public int describeContents() {
119        return 0;
120    }
121
122    /** copy constructor */
123    public WifiP2pConfig(WifiP2pConfig source) {
124        if (source != null) {
125            deviceAddress = source.deviceAddress;
126            wps = new WpsInfo(source.wps);
127            groupOwnerIntent = source.groupOwnerIntent;
128            netId = source.netId;
129        }
130    }
131
132    /** Implement the Parcelable interface */
133    public void writeToParcel(Parcel dest, int flags) {
134        dest.writeString(deviceAddress);
135        dest.writeParcelable(wps, flags);
136        dest.writeInt(groupOwnerIntent);
137        dest.writeInt(netId);
138    }
139
140    /** Implement the Parcelable interface */
141    public static final Creator<WifiP2pConfig> CREATOR =
142        new Creator<WifiP2pConfig>() {
143            public WifiP2pConfig createFromParcel(Parcel in) {
144                WifiP2pConfig config = new WifiP2pConfig();
145                config.deviceAddress = in.readString();
146                config.wps = (WpsInfo) in.readParcelable(null);
147                config.groupOwnerIntent = in.readInt();
148                config.netId = in.readInt();
149                return config;
150            }
151
152            public WifiP2pConfig[] newArray(int size) {
153                return new WifiP2pConfig[size];
154            }
155        };
156}
157