WifiP2pProvDiscEvent.java revision 618455f7e7255019c8cc08a734ba7c52b67a7dc8
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.os.Parcelable; 20import android.os.Parcel; 21import android.util.Log; 22 23/** 24 * A class representing a Wi-Fi p2p provisional discovery request/response 25 * See {@link #WifiP2pProvDiscEvent} for supported types 26 * 27 * @hide 28 */ 29public class WifiP2pProvDiscEvent { 30 31 private static final String TAG = "WifiP2pProvDiscEvent"; 32 33 public static final int PBC_REQ = 1; 34 public static final int PBC_RSP = 2; 35 public static final int ENTER_PIN = 3; 36 public static final int SHOW_PIN = 4; 37 38 /* One of PBC_REQ, PBC_RSP, ENTER_PIN or SHOW_PIN */ 39 public int event; 40 41 public WifiP2pDevice device; 42 43 /* Valid when event = SHOW_PIN */ 44 public String pin; 45 46 public WifiP2pProvDiscEvent() { 47 device = new WifiP2pDevice(); 48 } 49 50 /** 51 * @param string formats supported include 52 * 53 * P2P-PROV-DISC-PBC-REQ 42:fc:89:e1:e2:27 p2p_dev_addr=42:fc:89:e1:e2:27 54 * pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27 55 * group_capab=0x0 56 * 57 * P2P-PROV-DISC-PBC-RESP 02:12:47:f2:5a:36 58 * 59 * P2P-PROV-DISC-ENTER-PIN 42:fc:89:e1:e2:27 p2p_dev_addr=42:fc:89:e1:e2:27 60 * pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27 61 * group_capab=0x0 62 * 63 * P2P-PROV-DISC-SHOW-PIN 42:fc:89:e1:e2:27 44490607 p2p_dev_addr=42:fc:89:e1:e2:27 64 * pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27 65 * group_capab=0x0 66 * 67 * Note: The events formats can be looked up in the wpa_supplicant code 68 * @hide 69 */ 70 public WifiP2pProvDiscEvent(String string) throws IllegalArgumentException { 71 String[] tokens = string.split(" "); 72 73 if (tokens.length < 2) { 74 throw new IllegalArgumentException("Malformed event " + string); 75 } 76 77 if (tokens[0].endsWith("PBC-REQ")) event = PBC_REQ; 78 else if (tokens[0].endsWith("PBC-RESP")) event = PBC_RSP; 79 else if (tokens[0].endsWith("ENTER-PIN")) event = ENTER_PIN; 80 else if (tokens[0].endsWith("SHOW-PIN")) event = SHOW_PIN; 81 else throw new IllegalArgumentException("Malformed event " + string); 82 83 device = new WifiP2pDevice(); 84 85 for (String token : tokens) { 86 String[] nameValue = token.split("="); 87 if (nameValue.length != 2) { 88 //mac address without key is device address 89 if (token.matches("(([0-9a-f]{2}:){5}[0-9a-f]{2})")) { 90 device.deviceAddress = token; 91 } else if (token.matches("[0-9]+")) { 92 pin = token; 93 } else { 94 //ignore; 95 } 96 continue; 97 } 98 99 if (nameValue[0].equals("p2p_dev_addr")) { 100 device.deviceAddress = nameValue[1]; 101 continue; 102 } 103 104 if (nameValue[0].equals("pri_dev_type")) { 105 device.primaryDeviceType = nameValue[1]; 106 continue; 107 } 108 109 if (nameValue[0].equals("name")) { 110 device.deviceName = trimQuotes(nameValue[1]); 111 continue; 112 } 113 114 if (nameValue[0].equals("config_methods")) { 115 device.wpsConfigMethodsSupported = parseHex(nameValue[1]); 116 continue; 117 } 118 119 if (nameValue[0].equals("dev_capab")) { 120 device.deviceCapability = parseHex(nameValue[1]); 121 continue; 122 } 123 124 if (nameValue[0].equals("group_capab")) { 125 device.groupCapability = parseHex(nameValue[1]); 126 continue; 127 } 128 } 129 } 130 131 public String toString() { 132 StringBuffer sbuf = new StringBuffer(); 133 sbuf.append(device); 134 sbuf.append("\n event: ").append(event); 135 sbuf.append("\n pin: ").append(pin); 136 return sbuf.toString(); 137 } 138 139 private String trimQuotes(String str) { 140 str = str.trim(); 141 if (str.startsWith("'") && str.endsWith("'")) { 142 return str.substring(1, str.length()-1); 143 } 144 return str; 145 } 146 147 //supported formats: 0x1abc, 0X1abc, 1abc 148 private int parseHex(String hexString) { 149 int num = 0; 150 if (hexString.startsWith("0x") || hexString.startsWith("0X")) { 151 hexString = hexString.substring(2); 152 } 153 154 try { 155 num = Integer.parseInt(hexString, 16); 156 } catch(NumberFormatException e) { 157 Log.e(TAG, "Failed to parse hex string " + hexString); 158 } 159 return num; 160 } 161} 162