Skip to content

Commit 5fa4729

Browse files
isheriffAndroid (Google) Code Review
authored andcommitted
Merge "p2p fixes"
2 parents 001dd0c + 61472a8 commit 5fa4729

File tree

6 files changed

+112
-154
lines changed

6 files changed

+112
-154
lines changed

wifi/java/android/net/wifi/WifiMonitor.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -560,18 +560,12 @@ private void handleP2pEvents(String dataString) {
560560
*/
561561
private void handleHostApEvents(String dataString) {
562562
String[] tokens = dataString.split(" ");
563-
/* AP-STA-CONNECTED 42:fc:89:a8:96:09 dev_addr=02:90:4c:a0:92:54 */
563+
/* AP-STA-CONNECTED 42:fc:89:a8:96:09 p2p_dev_addr=02:90:4c:a0:92:54 */
564564
if (tokens[0].equals(AP_STA_CONNECTED_STR)) {
565-
String[] nameValue = tokens[2].split("=");
566-
if (nameValue.length != 2) return;
567-
WifiP2pDevice device = new WifiP2pDevice();
568-
device.interfaceAddress = tokens[1];
569-
device.deviceAddress = nameValue[1];
570-
mStateMachine.sendMessage(AP_STA_CONNECTED_EVENT, device);
571-
/* AP-STA-DISCONNECTED 42:fc:89:a8:96:09 */
565+
mStateMachine.sendMessage(AP_STA_CONNECTED_EVENT, new WifiP2pDevice(dataString));
566+
/* AP-STA-DISCONNECTED 42:fc:89:a8:96:09 p2p_dev_addr=02:90:4c:a0:92:54 */
572567
} else if (tokens[0].equals(AP_STA_DISCONNECTED_STR)) {
573-
//TODO: fix this once wpa_supplicant reports this consistently
574-
mStateMachine.sendMessage(AP_STA_DISCONNECTED_EVENT, tokens[1]);
568+
mStateMachine.sendMessage(AP_STA_DISCONNECTED_EVENT, new WifiP2pDevice(dataString));
575569
}
576570
}
577571

wifi/java/android/net/wifi/WifiNative.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -577,26 +577,6 @@ public boolean p2pReinvoke(int netId, String deviceAddress) {
577577
}
578578

579579

580-
public String p2pGetInterfaceAddress(String deviceAddress) {
581-
if (TextUtils.isEmpty(deviceAddress)) return null;
582-
583-
// "p2p_peer deviceAddress" returns a multi-line result containing
584-
// intended_addr=fa:7b:7a:42:82:13
585-
String peerInfo = p2pPeer(deviceAddress);
586-
if (TextUtils.isEmpty(peerInfo)) return null;
587-
String[] tokens= peerInfo.split("\n");
588-
589-
for (String token : tokens) {
590-
//TODO: update from interface_addr when wpa_supplicant implementation is fixed
591-
if (token.startsWith("intended_addr=")) {
592-
String[] nameValue = token.split("=");
593-
if (nameValue.length != 2) break;
594-
return nameValue[1];
595-
}
596-
}
597-
return null;
598-
}
599-
600580
public String p2pGetDeviceAddress() {
601581
String status = status();
602582
if (status == null) return "";
@@ -612,6 +592,13 @@ public String p2pGetDeviceAddress() {
612592
return "";
613593
}
614594

595+
public boolean isGroupOwner(String deviceAddress) {
596+
/* BSS returns details only for a GO */
597+
String bssInfo = doStringCommand("BSS p2p_dev_addr=" + deviceAddress);
598+
if (TextUtils.isEmpty(bssInfo)) return false;
599+
return true;
600+
}
601+
615602
public String p2pPeer(String deviceAddress) {
616603
return doStringCommand("P2P_PEER " + deviceAddress);
617604
}

wifi/java/android/net/wifi/p2p/WifiP2pDevice.java

Lines changed: 73 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.util.Log;
2222

2323
import 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;

wifi/java/android/net/wifi/p2p/WifiP2pDeviceList.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,6 @@ public void update(WifiP2pDevice device) {
7979
mDevices.add(device);
8080
}
8181

82-
/** @hide */
83-
public void updateInterfaceAddress(WifiP2pDevice device) {
84-
for (WifiP2pDevice d : mDevices) {
85-
//Found, update interface address
86-
if (d.equals(device)) {
87-
d.interfaceAddress = device.interfaceAddress;
88-
return;
89-
}
90-
}
91-
//Not found, add a new one
92-
mDevices.add(device);
93-
}
94-
9582
/** @hide */
9683
public boolean remove(WifiP2pDevice device) {
9784
if (device == null) return false;

wifi/java/android/net/wifi/p2p/WifiP2pGroup.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.List;
2424
import java.util.Collection;
2525
import java.util.Collections;
26+
import java.util.regex.Pattern;
27+
import java.util.regex.Matcher;
2628

2729
/**
2830
* A class representing a Wi-Fi P2p group
@@ -48,6 +50,15 @@ public class WifiP2pGroup implements Parcelable {
4850

4951
private String mInterface;
5052

53+
/** P2P group started string pattern */
54+
private static final Pattern groupStartedPattern = Pattern.compile(
55+
"ssid=\"(.+)\" " +
56+
"freq=(\\d+) " +
57+
"(?:psk=)?([0-9a-fA-F]{64})?" +
58+
"(?:passphrase=)?(?:\"(.{8,63})\")? " +
59+
"go_dev_addr=((?:[0-9a-f]{2}:){5}[0-9a-f]{2})"
60+
);
61+
5162
public WifiP2pGroup() {
5263
}
5364

@@ -78,24 +89,18 @@ public WifiP2pGroup(String supplicantEvent) throws IllegalArgumentException {
7889
mInterface = tokens[1];
7990
mIsGroupOwner = tokens[2].equals("GO");
8091

81-
for (String token : tokens) {
82-
String[] nameValue = token.split("=");
83-
if (nameValue.length != 2) continue;
84-
85-
if (nameValue[0].equals("ssid")) {
86-
mNetworkName = nameValue[1];
87-
continue;
88-
}
92+
Matcher match = groupStartedPattern.matcher(supplicantEvent);
93+
if (!match.find()) {
94+
return;
95+
}
8996

90-
if (nameValue[0].equals("passphrase")) {
91-
mPassphrase = nameValue[1];
92-
continue;
93-
}
97+
mNetworkName = match.group(1);
98+
//freq and psk are unused right now
99+
//int freq = Integer.parseInt(match.group(2));
100+
//String psk = match.group(3);
101+
mPassphrase = match.group(4);
102+
mOwner = new WifiP2pDevice(match.group(5));
94103

95-
if (nameValue[0].equals("go_dev_addr")) {
96-
mOwner = new WifiP2pDevice(nameValue[1]);
97-
}
98-
}
99104
} else if (tokens[0].equals("P2P-INVITATION-RECEIVED")) {
100105
for (String token : tokens) {
101106
String[] nameValue = token.split("=");

0 commit comments

Comments
 (0)