2121import android .util .Log ;
2222
2323import java .util .regex .Pattern ;
24+ import java .util .regex .Matcher ;
2425
2526/**
2627 * A class representing a Wi-Fi p2p device
@@ -41,17 +42,6 @@ public class WifiP2pDevice implements Parcelable {
4142 */
4243 public String deviceAddress = "" ;
4344
44- /**
45- * interfaceAddress
46- *
47- * This address is used during group owner negotiation as the Intended
48- * P2P Interface Address and the group interface will be created with
49- * address as the local address in case of successfully completed
50- * negotiation.
51- * @hide
52- */
53- public String interfaceAddress ;
54-
5545 /**
5646 * Primary device type identifies the type of device. For example, an application
5747 * could filter the devices discovered to only display printers if the purpose is to
@@ -117,6 +107,43 @@ public class WifiP2pDevice implements Parcelable {
117107 /** Device connection status */
118108 public int status = UNAVAILABLE ;
119109
110+ /** Detailed device string pattern
111+ * Example:
112+ * P2P-DEVICE-FOUND fa:7b:7a:42:02:13 p2p_dev_addr=fa:7b:7a:42:02:13
113+ * pri_dev_type=1-0050F204-1 name='p2p-TEST1' config_methods=0x188 dev_capab=0x27
114+ * group_capab=0x0
115+ *
116+ */
117+ private static final Pattern detailedDevicePattern = Pattern .compile (
118+ "((?:[0-9a-f]{2}:){5}[0-9a-f]{2}) " +
119+ "(\\ d+ )?" +
120+ "p2p_dev_addr=((?:[0-9a-f]{2}:){5}[0-9a-f]{2}) " +
121+ "pri_dev_type=(\\ d+-[0-9a-fA-F]+-\\ d+) " +
122+ "name='(.*)' " +
123+ "config_methods=(0x[0-9a-fA-F]+) " +
124+ "dev_capab=(0x[0-9a-fA-F]+) " +
125+ "group_capab=(0x[0-9a-fA-F]+)"
126+ );
127+
128+ /** 2 token device address pattern
129+ * Example:
130+ * P2P-DEVICE-LOST p2p_dev_addr=fa:7b:7a:42:02:13
131+ * AP-STA-DISCONNECTED 42:fc:89:a8:96:09
132+ */
133+ private static final Pattern twoTokenPattern = Pattern .compile (
134+ "(p2p_dev_addr=)?((?:[0-9a-f]{2}:){5}[0-9a-f]{2})"
135+ );
136+
137+ /** 3 token device address pattern
138+ * Example:
139+ * AP-STA-CONNECTED 42:fc:89:a8:96:09 p2p_dev_addr=fa:7b:7a:42:02:13
140+ * AP-STA-DISCONNECTED 42:fc:89:a8:96:09 p2p_dev_addr=fa:7b:7a:42:02:13
141+ */
142+ private static final Pattern threeTokenPattern = Pattern .compile (
143+ "(?:[0-9a-f]{2}:){5}[0-9a-f]{2} p2p_dev_addr=((?:[0-9a-f]{2}:){5}[0-9a-f]{2})"
144+ );
145+
146+
120147 public WifiP2pDevice () {
121148 }
122149
@@ -128,63 +155,55 @@ public WifiP2pDevice() {
128155 *
129156 * P2P-DEVICE-LOST p2p_dev_addr=fa:7b:7a:42:02:13
130157 *
158+ * AP-STA-CONNECTED 42:fc:89:a8:96:09 [p2p_dev_addr=02:90:4c:a0:92:54]
159+ *
160+ * AP-STA-DISCONNECTED 42:fc:89:a8:96:09 [p2p_dev_addr=02:90:4c:a0:92:54]
161+ *
131162 * fa:7b:7a:42:02:13
132163 *
133164 * Note: The events formats can be looked up in the wpa_supplicant code
134165 * @hide
135166 */
136167 public WifiP2pDevice (String string ) throws IllegalArgumentException {
137168 String [] tokens = string .split ("[ \n ]" );
169+ Matcher match ;
138170
139171 if (tokens .length < 1 ) {
140172 throw new IllegalArgumentException ("Malformed supplicant event" );
141173 }
142174
143- /* Just a device address */
144- if (tokens .length == 1 ) {
145- deviceAddress = string ;
146- return ;
147- }
148-
149- for (String token : tokens ) {
150- String [] nameValue = token .split ("=" );
151- if (nameValue .length != 2 ) {
152- //mac address without key is device address
153- if (token .matches ("(([0-9a-f]{2}:){5}[0-9a-f]{2})" )) {
154- deviceAddress = token ;
175+ switch (tokens .length ) {
176+ case 1 :
177+ /* Just a device address */
178+ deviceAddress = string ;
179+ return ;
180+ case 2 :
181+ match = twoTokenPattern .matcher (string );
182+ if (!match .find ()) {
183+ throw new IllegalArgumentException ("Malformed supplicant event" );
184+ }
185+ deviceAddress = match .group (2 );
186+ return ;
187+ case 3 :
188+ match = threeTokenPattern .matcher (string );
189+ if (!match .find ()) {
190+ throw new IllegalArgumentException ("Malformed supplicant event" );
191+ }
192+ deviceAddress = match .group (1 );
193+ return ;
194+ default :
195+ match = detailedDevicePattern .matcher (string );
196+ if (!match .find ()) {
197+ throw new IllegalArgumentException ("Malformed supplicant event" );
155198 }
156- continue ;
157- }
158-
159- if (nameValue [0 ].equals ("p2p_dev_addr" )) {
160- deviceAddress = nameValue [1 ];
161- continue ;
162- }
163-
164- if (nameValue [0 ].equals ("pri_dev_type" )) {
165- primaryDeviceType = nameValue [1 ];
166- continue ;
167- }
168-
169- if (nameValue [0 ].equals ("name" ) || nameValue [0 ].equals ("device_name" )) {
170- deviceName = trimQuotes (nameValue [1 ]);
171- continue ;
172- }
173-
174- if (nameValue [0 ].equals ("config_methods" )) {
175- wpsConfigMethodsSupported = parseHex (nameValue [1 ]);
176- continue ;
177- }
178-
179- if (nameValue [0 ].equals ("dev_capab" )) {
180- deviceCapability = parseHex (nameValue [1 ]);
181- continue ;
182- }
183199
184- if (nameValue [0 ].equals ("group_capab" )) {
185- groupCapability = parseHex (nameValue [1 ]);
186- continue ;
187- }
200+ deviceAddress = match .group (3 );
201+ primaryDeviceType = match .group (4 );
202+ deviceName = match .group (5 );
203+ wpsConfigMethodsSupported = parseHex (match .group (6 ));
204+ deviceCapability = parseHex (match .group (7 ));
205+ groupCapability = parseHex (match .group (8 ));
206+ break ;
188207 }
189208
190209 if (tokens [0 ].startsWith ("P2P-DEVICE-FOUND" )) {
@@ -233,7 +252,6 @@ public String toString() {
233252 StringBuffer sbuf = new StringBuffer ();
234253 sbuf .append ("Device: " ).append (deviceName );
235254 sbuf .append ("\n deviceAddress: " ).append (deviceAddress );
236- sbuf .append ("\n interfaceAddress: " ).append (interfaceAddress );
237255 sbuf .append ("\n primary type: " ).append (primaryDeviceType );
238256 sbuf .append ("\n secondary type: " ).append (secondaryDeviceType );
239257 sbuf .append ("\n wps: " ).append (wpsConfigMethodsSupported );
@@ -253,7 +271,6 @@ public WifiP2pDevice(WifiP2pDevice source) {
253271 if (source != null ) {
254272 deviceName = source .deviceName ;
255273 deviceAddress = source .deviceAddress ;
256- interfaceAddress = source .interfaceAddress ;
257274 primaryDeviceType = source .primaryDeviceType ;
258275 secondaryDeviceType = source .secondaryDeviceType ;
259276 wpsConfigMethodsSupported = source .wpsConfigMethodsSupported ;
@@ -267,7 +284,6 @@ public WifiP2pDevice(WifiP2pDevice source) {
267284 public void writeToParcel (Parcel dest , int flags ) {
268285 dest .writeString (deviceName );
269286 dest .writeString (deviceAddress );
270- dest .writeString (interfaceAddress );
271287 dest .writeString (primaryDeviceType );
272288 dest .writeString (secondaryDeviceType );
273289 dest .writeInt (wpsConfigMethodsSupported );
@@ -283,7 +299,6 @@ public WifiP2pDevice createFromParcel(Parcel in) {
283299 WifiP2pDevice device = new WifiP2pDevice ();
284300 device .deviceName = in .readString ();
285301 device .deviceAddress = in .readString ();
286- device .interfaceAddress = in .readString ();
287302 device .primaryDeviceType = in .readString ();
288303 device .secondaryDeviceType = in .readString ();
289304 device .wpsConfigMethodsSupported = in .readInt ();
@@ -298,15 +313,6 @@ public WifiP2pDevice[] newArray(int size) {
298313 }
299314 };
300315
301- private String trimQuotes (String str ) {
302- str = str .trim ();
303- if (str .startsWith ("'" ) && str .endsWith ("'" )) {
304- if (str .length () <= 2 ) return "" ;
305- else return str .substring (1 , str .length ()-1 );
306- }
307- return str ;
308- }
309-
310316 //supported formats: 0x1abc, 0X1abc, 1abc
311317 private int parseHex (String hexString ) {
312318 int num = 0 ;
0 commit comments