Skip to content

Commit 0879d03

Browse files
committed
Added a persistent feature in WiFi Direct.
Provide a new feature to use persistent group at default. In the normal connect sequence, if the persistent profile has been stored, try to use it. Otherwise, a new persistent group is created. If the persistent profiles are stored over 32, an old profile is deleted automatically. Change-Id: Iccb9fa044f1907f0818cd259275e1675f4c3e222 Signed-off-by: Yoshihiko Ikenaga <yoshihiko.ikenaga@jp.sony.com>
1 parent 48de12c commit 0879d03

File tree

9 files changed

+927
-92
lines changed

9 files changed

+927
-92
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import android.net.wifi.p2p.WifiP2pConfig;
2121
import android.net.wifi.p2p.WifiP2pDevice;
2222
import android.net.wifi.p2p.WifiP2pGroup;
23+
import android.net.wifi.p2p.WifiP2pService;
24+
import android.net.wifi.p2p.WifiP2pService.P2pStatus;
2325
import android.net.wifi.p2p.WifiP2pProvDiscEvent;
2426
import android.net.wifi.p2p.nsd.WifiP2pServiceResponse;
2527
import android.net.wifi.StateChangeResult;
@@ -186,7 +188,7 @@ public class WifiMonitor {
186188

187189
/* P2P-GROUP-STARTED p2p-wlan0-0 [client|GO] ssid="DIRECT-W8" freq=2437
188190
[psk=2182b2e50e53f260d04f3c7b25ef33c965a3291b9b36b455a82d77fd82ca15bc|passphrase="fKG4jMe3"]
189-
go_dev_addr=fa:7b:7a:42:02:13 */
191+
go_dev_addr=fa:7b:7a:42:02:13 [PERSISTENT] */
190192
private static final String P2P_GROUP_STARTED_STR = "P2P-GROUP-STARTED";
191193

192194
/* P2P-GROUP-REMOVED p2p-wlan0-0 [client|GO] reason=REQUESTED */
@@ -594,7 +596,13 @@ private void handleP2pEvents(String dataString) {
594596
if (tokens.length != 2) return;
595597
String[] nameValue = tokens[1].split("=");
596598
if (nameValue.length != 2) return;
597-
mStateMachine.sendMessage(P2P_INVITATION_RESULT_EVENT, nameValue[1]);
599+
P2pStatus err = P2pStatus.UNKNOWN;
600+
try {
601+
err = P2pStatus.valueOf(Integer.parseInt(nameValue[1]));
602+
} catch (NumberFormatException e) {
603+
e.printStackTrace();
604+
}
605+
mStateMachine.sendMessage(P2P_INVITATION_RESULT_EVENT, err);
598606
} else if (dataString.startsWith(P2P_PROV_DISC_PBC_REQ_STR)) {
599607
mStateMachine.sendMessage(P2P_PROV_DISC_PBC_REQ_EVENT,
600608
new WifiP2pProvDiscEvent(dataString));

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,9 @@ public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
547547
break;
548548
}
549549

550-
//TODO: Add persist behavior once the supplicant interaction is fixed for both
551-
// group and client scenarios
552-
/* Persist unless there is an explicit request to not do so*/
553-
//if (config.persist != WifiP2pConfig.Persist.NO) args.add("persistent");
550+
if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
551+
args.add("persistent");
552+
}
554553

555554
if (joinExistingGroup) {
556555
args.add("join");
@@ -592,10 +591,17 @@ public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
592591
return false;
593592
}
594593

595-
public boolean p2pGroupAdd() {
594+
public boolean p2pGroupAdd(boolean persistent) {
595+
if (persistent) {
596+
return doBooleanCommand("P2P_GROUP_ADD persistent");
597+
}
596598
return doBooleanCommand("P2P_GROUP_ADD");
597599
}
598600

601+
public boolean p2pGroupAdd(int netId) {
602+
return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
603+
}
604+
599605
public boolean p2pGroupRemove(String iface) {
600606
if (TextUtils.isEmpty(iface)) return false;
601607
return doBooleanCommand("P2P_GROUP_REMOVE " + iface);
@@ -624,6 +630,9 @@ public boolean p2pReinvoke(int netId, String deviceAddress) {
624630
return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
625631
}
626632

633+
public String p2pGetSsid(String deviceAddress) {
634+
return p2pGetParam(deviceAddress, "oper_ssid");
635+
}
627636

628637
public String p2pGetDeviceAddress() {
629638
String status = status();
@@ -665,6 +674,24 @@ public String p2pPeer(String deviceAddress) {
665674
return doStringCommand("P2P_PEER " + deviceAddress);
666675
}
667676

677+
private String p2pGetParam(String deviceAddress, String key) {
678+
if (deviceAddress == null) return null;
679+
680+
String peerInfo = p2pPeer(deviceAddress);
681+
if (peerInfo == null) return null;
682+
String[] tokens= peerInfo.split("\n");
683+
684+
key += "=";
685+
for (String token : tokens) {
686+
if (token.startsWith(key)) {
687+
String[] nameValue = token.split("=");
688+
if (nameValue.length != 2) break;
689+
return nameValue[1];
690+
}
691+
}
692+
return null;
693+
}
694+
668695
public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
669696
/*
670697
* P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>

wifi/java/android/net/wifi/p2p/WifiP2pConfig.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,8 @@ public class WifiP2pConfig implements Parcelable {
4646
*/
4747
public int groupOwnerIntent = -1;
4848

49-
/**
50-
* Indicates whether the configuration is saved
51-
* @hide
52-
*/
53-
public enum Persist {
54-
SYSTEM_DEFAULT,
55-
YES,
56-
NO
57-
}
58-
5949
/** @hide */
60-
public Persist persist = Persist.SYSTEM_DEFAULT;
50+
public int netId = WifiP2pGroup.PERSISTENT_NET_ID;
6151

6252
public WifiP2pConfig() {
6353
//set defaults
@@ -110,7 +100,7 @@ public String toString() {
110100
sbuf.append("\n address: ").append(deviceAddress);
111101
sbuf.append("\n wps: ").append(wps);
112102
sbuf.append("\n groupOwnerIntent: ").append(groupOwnerIntent);
113-
sbuf.append("\n persist: ").append(persist.toString());
103+
sbuf.append("\n persist: ").append(netId);
114104
return sbuf.toString();
115105
}
116106

@@ -125,7 +115,7 @@ public WifiP2pConfig(WifiP2pConfig source) {
125115
deviceAddress = source.deviceAddress;
126116
wps = new WpsInfo(source.wps);
127117
groupOwnerIntent = source.groupOwnerIntent;
128-
persist = source.persist;
118+
netId = source.netId;
129119
}
130120
}
131121

@@ -134,7 +124,7 @@ public void writeToParcel(Parcel dest, int flags) {
134124
dest.writeString(deviceAddress);
135125
dest.writeParcelable(wps, flags);
136126
dest.writeInt(groupOwnerIntent);
137-
dest.writeString(persist.name());
127+
dest.writeInt(netId);
138128
}
139129

140130
/** Implement the Parcelable interface */
@@ -145,7 +135,7 @@ public WifiP2pConfig createFromParcel(Parcel in) {
145135
config.deviceAddress = in.readString();
146136
config.wps = (WpsInfo) in.readParcelable(null);
147137
config.groupOwnerIntent = in.readInt();
148-
config.persist = Persist.valueOf(in.readString());
138+
config.netId = in.readInt();
149139
return config;
150140
}
151141

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,26 @@ public boolean isServiceDiscoveryCapable() {
231231
return (deviceCapability & DEVICE_CAPAB_SERVICE_DISCOVERY) != 0;
232232
}
233233

234+
/** Returns true if the device is capable of invitation {@hide}*/
235+
public boolean isInvitationCapable() {
236+
return (deviceCapability & DEVICE_CAPAB_INVITATION_PROCEDURE) != 0;
237+
}
238+
239+
/** Returns true if the device reaches the limit. {@hide}*/
240+
public boolean isDeviceLimit() {
241+
return (deviceCapability & DEVICE_CAPAB_DEVICE_LIMIT) != 0;
242+
}
243+
234244
/** Returns true if the device is a group owner */
235245
public boolean isGroupOwner() {
236246
return (groupCapability & GROUP_CAPAB_GROUP_OWNER) != 0;
237247
}
238248

249+
/** Returns true if the group reaches the limit. {@hide}*/
250+
public boolean isGroupLimit() {
251+
return (groupCapability & GROUP_CAPAB_GROUP_LIMIT) != 0;
252+
}
253+
239254
@Override
240255
public boolean equals(Object obj) {
241256
if (this == obj) return true;

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

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333
*/
3434
public class WifiP2pGroup implements Parcelable {
3535

36+
/** The temporary network id.
37+
* {@hide} */
38+
public static final int TEMPORARY_NET_ID = -1;
39+
40+
/** The persistent network id.
41+
* If a matching persistent profile is found, use it.
42+
* Otherwise, create a new persistent profile.
43+
* {@hide} */
44+
public static final int PERSISTENT_NET_ID = -2;
45+
3646
/** The network name */
3747
private String mNetworkName;
3848

@@ -50,13 +60,17 @@ public class WifiP2pGroup implements Parcelable {
5060

5161
private String mInterface;
5262

63+
/** The network id in the wpa_supplicant */
64+
private int mNetId;
65+
5366
/** P2P group started string pattern */
5467
private static final Pattern groupStartedPattern = Pattern.compile(
5568
"ssid=\"(.+)\" " +
5669
"freq=(\\d+) " +
5770
"(?:psk=)?([0-9a-fA-F]{64})?" +
5871
"(?:passphrase=)?(?:\"(.{8,63})\")? " +
59-
"go_dev_addr=((?:[0-9a-f]{2}:){5}[0-9a-f]{2})"
72+
"go_dev_addr=((?:[0-9a-f]{2}:){5}[0-9a-f]{2})" +
73+
" ?(\\[PERSISTENT\\])?"
6074
);
6175

6276
public WifiP2pGroup() {
@@ -67,13 +81,15 @@ public WifiP2pGroup() {
6781
*
6882
* P2P-GROUP-STARTED p2p-wlan0-0 [client|GO] ssid="DIRECT-W8" freq=2437
6983
* [psk=2182b2e50e53f260d04f3c7b25ef33c965a3291b9b36b455a82d77fd82ca15bc|
70-
* passphrase="fKG4jMe3"] go_dev_addr=fa:7b:7a:42:02:13
84+
* passphrase="fKG4jMe3"] go_dev_addr=fa:7b:7a:42:02:13 [PERSISTENT]
7185
*
7286
* P2P-GROUP-REMOVED p2p-wlan0-0 [client|GO] reason=REQUESTED
7387
*
7488
* P2P-INVITATION-RECEIVED sa=fa:7b:7a:42:02:13 go_dev_addr=f8:7b:7a:42:02:13
7589
* bssid=fa:7b:7a:42:82:13 unknown-network
7690
*
91+
* P2P-INVITATION-RECEIVED sa=b8:f9:34:2a:c7:9d persistent=0
92+
*
7793
* Note: The events formats can be looked up in the wpa_supplicant code
7894
* @hide
7995
*/
@@ -100,16 +116,38 @@ public WifiP2pGroup(String supplicantEvent) throws IllegalArgumentException {
100116
//String psk = match.group(3);
101117
mPassphrase = match.group(4);
102118
mOwner = new WifiP2pDevice(match.group(5));
103-
119+
if (match.group(6) != null) {
120+
mNetId = PERSISTENT_NET_ID;
121+
} else {
122+
mNetId = TEMPORARY_NET_ID;
123+
}
104124
} else if (tokens[0].equals("P2P-INVITATION-RECEIVED")) {
125+
String sa = null;
126+
mNetId = PERSISTENT_NET_ID;
105127
for (String token : tokens) {
106128
String[] nameValue = token.split("=");
107129
if (nameValue.length != 2) continue;
108130

131+
if (nameValue[0].equals("sa")) {
132+
sa = nameValue[1];
133+
134+
// set source address into the client list.
135+
WifiP2pDevice dev = new WifiP2pDevice();
136+
dev.deviceAddress = nameValue[1];
137+
mClients.add(dev);
138+
continue;
139+
}
140+
109141
if (nameValue[0].equals("go_dev_addr")) {
110142
mOwner = new WifiP2pDevice(nameValue[1]);
111143
continue;
112144
}
145+
146+
if (nameValue[0].equals("persistent")) {
147+
mOwner = new WifiP2pDevice(sa);
148+
mNetId = Integer.parseInt(nameValue[1]);
149+
continue;
150+
}
113151
}
114152
} else {
115153
throw new IllegalArgumentException("Malformed supplicant event");
@@ -212,6 +250,16 @@ public String getInterface() {
212250
return mInterface;
213251
}
214252

253+
/** @hide */
254+
public int getNetworkId() {
255+
return mNetId;
256+
}
257+
258+
/** @hide */
259+
public void setNetworkId(int netId) {
260+
this.mNetId = netId;
261+
}
262+
215263
public String toString() {
216264
StringBuffer sbuf = new StringBuffer();
217265
sbuf.append("network: ").append(mNetworkName);
@@ -221,6 +269,7 @@ public String toString() {
221269
sbuf.append("\n Client: ").append(client);
222270
}
223271
sbuf.append("\n interface: ").append(mInterface);
272+
sbuf.append("\n networkId: ").append(mNetId);
224273
return sbuf.toString();
225274
}
226275

@@ -238,6 +287,7 @@ public WifiP2pGroup(WifiP2pGroup source) {
238287
for (WifiP2pDevice d : source.getClientList()) mClients.add(d);
239288
mPassphrase = source.getPassphrase();
240289
mInterface = source.getInterface();
290+
mNetId = source.getNetworkId();
241291
}
242292
}
243293

@@ -252,6 +302,7 @@ public void writeToParcel(Parcel dest, int flags) {
252302
}
253303
dest.writeString(mPassphrase);
254304
dest.writeString(mInterface);
305+
dest.writeInt(mNetId);
255306
}
256307

257308
/** Implement the Parcelable interface */
@@ -268,6 +319,7 @@ public WifiP2pGroup createFromParcel(Parcel in) {
268319
}
269320
group.setPassphrase(in.readString());
270321
group.setInterface(in.readString());
322+
group.setNetworkId(in.readInt());
271323
return group;
272324
}
273325

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) 2012, 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+
17+
package android.net.wifi.p2p;
18+
19+
parcelable WifiP2pGroupList;

0 commit comments

Comments
 (0)