diff options
author | Etan Cohen <etancohen@google.com> | 2017-07-13 14:41:22 -0700 |
---|---|---|
committer | Etan Cohen <etancohen@google.com> | 2017-07-14 12:58:16 -0700 |
commit | eddf3faabf426d18b7d3dff187ec91fe6a96665d (patch) | |
tree | 3f91945a2b493de21d832c1a7c2c2b9125cf1cff /service | |
parent | 80f9ea680d1fc7a2e343c8b5df0505cca5c3bc7f (diff) |
[AWARE] Create a locally maintained peer ID instead of firmware ID
The firmware provides a peer requestor ID + peer MAC to identify the
peer. The peer requestor ID alone is insufficient.
Original code used the peer requestor ID as the only identifier. This
failed once started testing with >2 devices.
Allocate a local (unique) peer ID which is mapped to the peer requestor
ID and peer MAC by the framework.
Bug: 63642354
Test: all unit tests + integration (sl4a) tests
Change-Id: Ic1b3a97b9675be8d8561f4500e8c777ddff42950
Diffstat (limited to 'service')
3 files changed, 71 insertions, 35 deletions
diff --git a/service/java/com/android/server/wifi/aware/WifiAwareDataPathStateManager.java b/service/java/com/android/server/wifi/aware/WifiAwareDataPathStateManager.java index ca12d5d52..82a3264ee 100644 --- a/service/java/com/android/server/wifi/aware/WifiAwareDataPathStateManager.java +++ b/service/java/com/android/server/wifi/aware/WifiAwareDataPathStateManager.java @@ -673,7 +673,7 @@ public class WifiAwareDataPathStateManager { } nnri.interfaceName = selectInterfaceForRequest(nnri); - mMgr.initiateDataPathSetup(networkSpecifier, nnri.networkSpecifier.peerId, + mMgr.initiateDataPathSetup(networkSpecifier, nnri.peerInstanceId, NanDataPathChannelCfg.CHANNEL_NOT_REQUESTED, selectChannelForRequest(nnri), nnri.peerDiscoveryMac, nnri.interfaceName, nnri.networkSpecifier.pmk, nnri.networkSpecifier.passphrase, nnri.networkSpecifier.isOutOfBand()); @@ -846,6 +846,7 @@ public class WifiAwareDataPathStateManager { public int uid; public String interfaceName; public int pubSubId = 0; + public int peerInstanceId = 0; public byte[] peerDiscoveryMac = null; public int ndpId; public byte[] peerDataMac; @@ -857,6 +858,7 @@ public class WifiAwareDataPathStateManager { static AwareNetworkRequestInformation processNetworkSpecifier(WifiAwareNetworkSpecifier ns, WifiAwareStateManager mgr, WifiPermissionsWrapper permissionWrapper) { int uid, pubSubId = 0; + int peerInstanceId = 0; byte[] peerMac = ns.peerMac; if (VDBG) { @@ -919,15 +921,17 @@ public class WifiAwareDataPathStateManager { if (ns.type == WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_IB) { pubSubId = session.getPubSubId(); - String peerMacStr = session.getMac(ns.peerId, null); - if (peerMacStr == null) { + WifiAwareDiscoverySessionState.PeerInfo peerInfo = session.getPeerInfo( + ns.peerId); + if (peerInfo == null) { Log.e(TAG, "processNetworkSpecifier: networkSpecifier=" + ns - + " -- no MAC address associated with this peer id -- peerId=" + + " -- no peer info associated with this peer id -- peerId=" + ns.peerId); return null; } + peerInstanceId = peerInfo.mInstanceId; try { - peerMac = HexEncoding.decode(peerMacStr.toCharArray(), false); + peerMac = peerInfo.mMac; if (peerMac == null || peerMac.length != 6) { Log.e(TAG, "processNetworkSpecifier: networkSpecifier=" + ns + " -- invalid peer MAC address"); @@ -979,6 +983,7 @@ public class WifiAwareDataPathStateManager { : AwareNetworkRequestInformation.STATE_RESPONDER_IDLE; nnri.uid = uid; nnri.pubSubId = pubSubId; + nnri.peerInstanceId = peerInstanceId; nnri.peerDiscoveryMac = peerMac; nnri.networkSpecifier = ns; @@ -990,7 +995,8 @@ public class WifiAwareDataPathStateManager { StringBuilder sb = new StringBuilder("AwareNetworkRequestInformation: "); sb.append("state=").append(state).append(", ns=").append(networkSpecifier).append( ", uid=").append(uid).append(", interfaceName=").append(interfaceName).append( - ", pubSubId=").append(pubSubId).append(", peerDiscoveryMac=").append( + ", pubSubId=").append(pubSubId).append(", peerInstanceId=").append( + peerInstanceId).append(", peerDiscoveryMac=").append( peerDiscoveryMac == null ? "" : String.valueOf(HexEncoding.encode(peerDiscoveryMac))).append( ", ndpId=").append(ndpId).append(", peerDataMac=").append( diff --git a/service/java/com/android/server/wifi/aware/WifiAwareDiscoverySessionState.java b/service/java/com/android/server/wifi/aware/WifiAwareDiscoverySessionState.java index ac3dee414..86f4e37c2 100644 --- a/service/java/com/android/server/wifi/aware/WifiAwareDiscoverySessionState.java +++ b/service/java/com/android/server/wifi/aware/WifiAwareDiscoverySessionState.java @@ -28,6 +28,7 @@ import libcore.util.HexEncoding; import java.io.FileDescriptor; import java.io.PrintWriter; +import java.util.Arrays; /** * Manages the state of a single Aware discovery session (publish or subscribe). @@ -40,6 +41,8 @@ public class WifiAwareDiscoverySessionState { private static final boolean DBG = false; private static final boolean VDBG = false; // STOPSHIP if true + private int mNextPeerIdToBeAllocated = 100; // used to create a unique peer ID + private final WifiAwareNativeApi mWifiAwareNativeApi; private int mSessionId; private byte mPubSubId; @@ -47,7 +50,24 @@ public class WifiAwareDiscoverySessionState { private boolean mIsPublishSession; private final long mCreationTime; - private final SparseArray<String> mMacByRequestorInstanceId = new SparseArray<>(); + static class PeerInfo { + PeerInfo(int instanceId, byte[] mac) { + mInstanceId = instanceId; + mMac = mac; + } + + int mInstanceId; + byte[] mMac; + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("instanceId ["); + sb.append(mInstanceId).append(", mac=").append(HexEncoding.encode(mMac)).append("]"); + return sb.toString(); + } + } + + private final SparseArray<PeerInfo> mPeerInfoByRequestorInstanceId = new SparseArray<>(); public WifiAwareDiscoverySessionState(WifiAwareNativeApi wifiAwareNativeApi, int sessionId, byte pubSubId, IWifiAwareDiscoverySessionCallback callback, boolean isPublishSession, @@ -81,16 +101,11 @@ public class WifiAwareDiscoverySessionState { } /** - * Return the MAC address (String) of the specified peer ID - or a null if no such address is + * Return the peer information of the specified peer ID - or a null if no such peer ID is * registered. */ - public String getMac(int peerId, String sep) { - String mac = mMacByRequestorInstanceId.get(peerId); - if (mac != null && sep != null && !sep.isEmpty()) { - mac = new StringBuilder(mac).insert(10, sep).insert(8, sep).insert(6, sep) - .insert(4, sep).insert(2, sep).toString(); - } - return mac; + public PeerInfo getPeerInfo(int peerId) { + return mPeerInfoByRequestorInstanceId.get(peerId); } /** @@ -190,8 +205,8 @@ public class WifiAwareDiscoverySessionState { * callbacks related to the message (success/failure). */ public boolean sendMessage(short transactionId, int peerId, byte[] message, int messageId) { - String peerMacStr = mMacByRequestorInstanceId.get(peerId); - if (peerMacStr == null) { + PeerInfo peerInfo = mPeerInfoByRequestorInstanceId.get(peerId); + if (peerInfo == null) { Log.e(TAG, "sendMessage: attempting to send a message to an address which didn't " + "match/contact us"); try { @@ -201,10 +216,9 @@ public class WifiAwareDiscoverySessionState { } return false; } - byte[] peerMac = HexEncoding.decode(peerMacStr.toCharArray(), false); - boolean success = mWifiAwareNativeApi.sendMessage(transactionId, mPubSubId, peerId, peerMac, - message, messageId); + boolean success = mWifiAwareNativeApi.sendMessage(transactionId, mPubSubId, + peerInfo.mInstanceId, peerInfo.mMac, message, messageId); if (!success) { try { mCallback.onMessageSendFail(messageId, NanStatusType.INTERNAL_FAILURE); @@ -233,13 +247,10 @@ public class WifiAwareDiscoverySessionState { */ public void onMatch(int requestorInstanceId, byte[] peerMac, byte[] serviceSpecificInfo, byte[] matchFilter) { - String prevMac = mMacByRequestorInstanceId.get(requestorInstanceId); - mMacByRequestorInstanceId.put(requestorInstanceId, new String(HexEncoding.encode(peerMac))); - - if (DBG) Log.d(TAG, "onMatch: previous peer MAC replaced - " + prevMac); + int peerId = getPeerIdOrAddIfNew(requestorInstanceId, peerMac); try { - mCallback.onMatch(requestorInstanceId, serviceSpecificInfo, matchFilter); + mCallback.onMatch(peerId, serviceSpecificInfo, matchFilter); } catch (RemoteException e) { Log.w(TAG, "onMatch: RemoteException (FYI): " + e); } @@ -256,20 +267,35 @@ public class WifiAwareDiscoverySessionState { * @param message The received message. */ public void onMessageReceived(int requestorInstanceId, byte[] peerMac, byte[] message) { - String prevMac = mMacByRequestorInstanceId.get(requestorInstanceId); - mMacByRequestorInstanceId.put(requestorInstanceId, new String(HexEncoding.encode(peerMac))); - - if (DBG) { - Log.d(TAG, "onMessageReceived: previous peer MAC replaced - " + prevMac); - } + int peerId = getPeerIdOrAddIfNew(requestorInstanceId, peerMac); try { - mCallback.onMessageReceived(requestorInstanceId, message); + mCallback.onMessageReceived(peerId, message); } catch (RemoteException e) { Log.w(TAG, "onMessageReceived: RemoteException (FYI): " + e); } } + private int getPeerIdOrAddIfNew(int requestorInstanceId, byte[] peerMac) { + for (int i = 0; i < mPeerInfoByRequestorInstanceId.size(); ++i) { + PeerInfo peerInfo = mPeerInfoByRequestorInstanceId.valueAt(i); + if (peerInfo.mInstanceId == requestorInstanceId && Arrays.equals(peerMac, + peerInfo.mMac)) { + return mPeerInfoByRequestorInstanceId.keyAt(i); + } + } + + int newPeerId = mNextPeerIdToBeAllocated++; + PeerInfo newPeerInfo = new PeerInfo(requestorInstanceId, peerMac); + mPeerInfoByRequestorInstanceId.put(newPeerId, newPeerInfo); + + if (DBG) { + Log.d(TAG, "New peer info: peerId=" + newPeerId + ", peerInfo=" + newPeerInfo); + } + + return newPeerId; + } + /** * Dump the internal state of the class. */ @@ -278,6 +304,6 @@ public class WifiAwareDiscoverySessionState { pw.println(" mSessionId: " + mSessionId); pw.println(" mIsPublishSession: " + mIsPublishSession); pw.println(" mPubSubId: " + mPubSubId); - pw.println(" mMacByRequestorInstanceId: [" + mMacByRequestorInstanceId + "]"); + pw.println(" mPeerInfoByRequestorInstanceId: [" + mPeerInfoByRequestorInstanceId + "]"); } } diff --git a/service/java/com/android/server/wifi/aware/WifiAwareStateManager.java b/service/java/com/android/server/wifi/aware/WifiAwareStateManager.java index aa5a426ed..30aa42a26 100644 --- a/service/java/com/android/server/wifi/aware/WifiAwareStateManager.java +++ b/service/java/com/android/server/wifi/aware/WifiAwareStateManager.java @@ -48,6 +48,7 @@ import com.android.internal.util.MessageUtils; import com.android.internal.util.State; import com.android.internal.util.StateMachine; import com.android.internal.util.WakeupMessage; +import com.android.server.wifi.util.NativeUtil; import com.android.server.wifi.util.WifiPermissionsWrapper; import libcore.util.HexEncoding; @@ -2332,10 +2333,13 @@ public class WifiAwareStateManager implements WifiAwareShellCommand.DelegatedShe for (RttManager.RttParams param : params) { String peerIdStr = param.bssid; try { - param.bssid = session.getMac(Integer.parseInt(peerIdStr), ":"); - if (param.bssid == null) { + WifiAwareDiscoverySessionState.PeerInfo peerInfo = session.getPeerInfo( + Integer.parseInt(peerIdStr)); + if (peerInfo == null || peerInfo.mMac == null) { Log.d(TAG, "startRangingLocal: no MAC address for peer ID=" + peerIdStr); param.bssid = ""; + } else { + param.bssid = NativeUtil.macAddressFromByteArray(peerInfo.mMac); } } catch (NumberFormatException e) { Log.e(TAG, "startRangingLocal: invalid peer ID specification (in bssid field): '" |