WifiP2pConfig.java revision 55bc5f3e0408bcb5a39a6732de0b2d1aa99a55be
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.WpsConfiguration;
20import android.net.wifi.WpsConfiguration.Setup;
21import android.os.Parcelable;
22import android.os.Parcel;
23
24/**
25 * A class representing a Wi-Fi P2p configuration
26 * @hide
27 */
28public class WifiP2pConfig implements Parcelable {
29
30    /**
31     * Device name
32     */
33    public String deviceName;
34
35    /**
36     * Device address
37     */
38    public String deviceAddress;
39
40    /**
41     * WPS configuration
42     */
43    public WpsConfiguration wpsConfig;
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    public int groupOwnerIntent;
51
52    public boolean isPersistent;
53
54    public boolean joinExistingGroup;
55
56    /**
57     * Channel frequency in MHz
58     */
59    public int channel;
60
61    public WifiP2pConfig() {
62        //set defaults
63        wpsConfig = new WpsConfiguration();
64        wpsConfig.setup = Setup.PBC;
65    }
66
67    /* P2P-GO-NEG-REQUEST 42:fc:89:a8:96:09 dev_passwd_id=4 */
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        wpsConfig = new WpsConfiguration();
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            //As defined in wps/wps_defs.h
87            switch (devPasswdId) {
88                case 0x00:
89                    wpsConfig.setup = Setup.LABEL;
90                    break;
91                case 0x01:
92                    wpsConfig.setup = Setup.KEYPAD;
93                    break;
94                case 0x04:
95                    wpsConfig.setup = Setup.PBC;
96                    break;
97                case 0x05:
98                    wpsConfig.setup = Setup.DISPLAY;
99                    break;
100                default:
101                    wpsConfig.setup = Setup.PBC;
102                    break;
103            }
104        }
105    }
106
107    public String toString() {
108        StringBuffer sbuf = new StringBuffer();
109        sbuf.append("Device: ").append(deviceName);
110        sbuf.append("\n address: ").append(deviceAddress);
111        sbuf.append("\n wps: ").append(wpsConfig);
112        sbuf.append("\n groupOwnerIntent: ").append(groupOwnerIntent);
113        sbuf.append("\n isPersistent: ").append(isPersistent);
114        sbuf.append("\n joinExistingGroup: ").append(joinExistingGroup);
115        sbuf.append("\n channel: ").append(channel);
116        return sbuf.toString();
117    }
118
119    /** Implement the Parcelable interface {@hide} */
120    public int describeContents() {
121        return 0;
122    }
123
124    /** copy constructor {@hide} */
125    public WifiP2pConfig(WifiP2pConfig source) {
126        if (source != null) {
127            //TODO: implement
128       }
129    }
130
131    /** Implement the Parcelable interface {@hide} */
132    public void writeToParcel(Parcel dest, int flags) {
133        dest.writeString(deviceName);
134        dest.writeString(deviceAddress);
135        dest.writeParcelable(wpsConfig, flags);
136        dest.writeInt(groupOwnerIntent);
137        dest.writeInt(isPersistent ? 1 : 0);
138        dest.writeInt(joinExistingGroup ? 1 : 0);
139        dest.writeInt(channel);
140    }
141
142    /** Implement the Parcelable interface {@hide} */
143    public static final Creator<WifiP2pConfig> CREATOR =
144        new Creator<WifiP2pConfig>() {
145            public WifiP2pConfig createFromParcel(Parcel in) {
146                WifiP2pConfig config = new WifiP2pConfig();
147                config.deviceName = in.readString();
148                config.deviceAddress = in.readString();
149                config.wpsConfig = (WpsConfiguration) in.readParcelable(null);
150                config.groupOwnerIntent = in.readInt();
151                config.isPersistent = (in.readInt() == 1);
152                config.joinExistingGroup = (in.readInt() == 1);
153                config.channel = in.readInt();
154                return config;
155            }
156
157            public WifiP2pConfig[] newArray(int size) {
158                return new WifiP2pConfig[size];
159            }
160        };
161}
162