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