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 /** 41 * This is an integer value between 0 and 15 where 0 indicates the least 42 * inclination to be a group owner and 15 indicates the highest inclination 43 * to be a group owner. 44 * 45 * A value of -1 indicates the system can choose an appropriate value. 46 */ 47 public int groupOwnerIntent = -1; 48 49 /** 50 * Indicates whether the configuration is saved 51 * @hide 52 */ 53 public enum Persist { 54 SYSTEM_DEFAULT, 55 YES, 56 NO 57 } 58 59 /** @hide */ 60 public Persist persist = Persist.SYSTEM_DEFAULT; 61 62 public WifiP2pConfig() { 63 //set defaults 64 wps = new WpsInfo(); 65 wps.setup = WpsInfo.PBC; 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 //As defined in wps/wps_defs.h 88 switch (devPasswdId) { 89 case 0x00: 90 wps.setup = WpsInfo.LABEL; 91 break; 92 case 0x01: 93 wps.setup = WpsInfo.KEYPAD; 94 break; 95 case 0x04: 96 wps.setup = WpsInfo.PBC; 97 break; 98 case 0x05: 99 wps.setup = WpsInfo.DISPLAY; 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(persist.toString()); 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 persist = source.persist; 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.writeString(persist.name()); 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.persist = Persist.valueOf(in.readString()); 149 return config; 150 } 151 152 public WifiP2pConfig[] newArray(int size) { 153 return new WifiP2pConfig[size]; 154 } 155 }; 156} 157